- °³¿ä
- For Visual C++ 8.0
- From "The STL Tutorial and Reference Guide"
- ¸µÅ©
1 °³¿ä
ÄÄÆÄÀÏ·¯¸¶´Ù ¹Ì¹¦ÇÏ°Ô Æ²¸° °ÍÀÌ Â¥Áõ³ª´Âµ¥...
2 For Visual C++ 8.0
template <class T>
class custom_allocator
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef const value_type* const_pointer;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template<class _Other>
struct rebind { typedef custom_allocator<_Other> other; };
pointer address(reference _Val) const
{
return (&_Val);
}
const_pointer address(const_reference _Val) const
{
return (&_Val);
}
custom_allocator() throw() {}
custom_allocator(const custom_allocator<T>&) throw() {}
template <class _Other>
custom_allocator(const custom_allocator<_Other>&) throw() {}
template<class _Other>
custom_allocator<T>& operator = (const custom_allocator<_Other>&)
{
return (*this);
}
void deallocate(pointer _Ptr, size_type)
{
//ÀÌ ºÎºÐ¿¡¼ ±âº» delete ¿¬»êÀÚ ´ë½Å¿¡ ´Ù¸¥ °É È£ÃâÇØÁØ´Ù.
::operator delete(_Ptr);
}
pointer allocate(size_type _Count)
{
if (_Count <= 0)
_Count = 0;
else if (((size_t)(-1) / _Count) < sizeof(T))
throw std::bad_alloc(NULL);
//ÀÌ ºÎºÐ¿¡¼ ±âº» new ¿¬»êÀÚ ´ë½Å¿¡ ´Ù¸¥ °É È£ÃâÇØÁØ´Ù.
return ((T*)::operator new(_Count * sizeof(T)));
}
pointer allocate(size_type _Count, const void*)
{
return allocate(_Count);
}
void construct(pointer _Ptr, const T& _Val)
{
new ((void *)_Ptr)T(_Val);
}
void destroy(pointer _Ptr)
{
_Ptr->T::~T();
}
size_t max_size() const throw()
{
size_t _Count = (size_t)(-1) / sizeof (T);
return (0 < _Count ? _Count : 1);
}
};
template <class T, class _Other>
inline bool operator == (const custom_allocator<T>&, const custom_allocator<_Other>&) throw()
{
return true;
}
template <class T, class _Other>
inline bool operator != (const custom_allocator<T>&, const custom_allocator<_Other>&) throw()
{
return false;
}
3 From "The STL Tutorial and Reference Guide"
// Define an allocator adaptor that logs memory allocation and deallocation.
#include <memory>
#include <iostream>
using namespace std;
template <typename T, typename Allocator = allocator<T> >
class logging_allocator
{
private:
Allocator alloc;
public:
typedef typename Allocator::size_type size_type;
typedef typename Allocator::difference_type difference_type;
typedef typename Allocator::pointer pointer;
typedef typename Allocator::const_pointer const_pointer;
typedef typename Allocator::reference reference;
typedef typename Allocator::const_reference const_reference;
typedef typename Allocator::value_type value_type;
template <typename U> struct rebind {
typedef logging_allocator<U,
typename Allocator::template rebind<U>::other> other;
};
logging_allocator() {}
logging_allocator(const logging_allocator& x)
: alloc(x.alloc) {}
template <typename U>
logging_allocator(const logging_allocator<U,
typename Allocator::template rebind<U>::other>& x)
: alloc(x.alloc) {}
~logging_allocator() {}
pointer address(reference x) const {
return alloc.address(x);
}
const_pointer address(const_reference x) const {
return alloc.address(x);
}
size_type max_size() const { return alloc.max_size(); }
void construct(pointer p, const value_type& val) {
alloc.construct(p, val);
}
void destroy(pointer p) { alloc.destroy(p); }
pointer allocate(size_type n, const void* hint = 0) {
ios::fmtflags flags = cerr.flags();
cerr << "allocate(" << n << ", "
<< hex << hint << dec << ") = ";
pointer result = alloc.allocate(n, hint);
cerr << hex << result << dec << endl;
cerr.setf(flags);
return result;
}
void deallocate(pointer p, size_type n) {
ios::fmtflags flags = cerr.flags();
cerr << "deallocate(" << hex << p << dec << ", "
<< n << ")" << endl;
alloc.deallocate(p, n);
cerr.setf(flags);
}
};
template <typename T, typename Allocator1,
typename U, typename Allocator2>
bool operator==(const logging_allocator<T, Allocator1>& x,
const logging_allocator<U, Allocator2>& y) {
return x.alloc == y.alloc;
}
template <typename T, typename Allocator1,
typename U, typename Allocator2>
bool operator!=(const logging_allocator<T, Allocator1>& x,
const logging_allocator<U, Allocator2>& y) {
return x.alloc != y.alloc;
}
// Demonstrating use of a custom allocator
#include <iostream>
#include <vector>
using namespace std;
#include "logalloc.h"
int main()
{
cout << "Demonstrating use of a custom allocator." << endl;
cout << "-- Default allocator --" << endl;
vector<int> v1;
for (int i = 0; i < 10; ++i) {
cout << " Inserting " << i << endl;
v1.push_back(i);
}
cout << "-- Done. --" << endl;
cout << "\n-- Custom allocator --" << endl;
vector<int, logging_allocator<int> > v2;
for (int i = 0; i < 10; ++i) {
cout << " Inserting " << i << endl;
v2.push_back(i);
}
cout << "-- Done. --" << endl;
cout << "\n-- Custom allocator with reserve --" << endl;
vector<int, logging_allocator<int> > v3;
v3.reserve(10);
for (int i = 0; i < 10; ++i) {
cout << " Inserting " << i << endl;
v3.push_back(i);
}
cout << "-- Done. --" << endl;
return 0;
}
4 ¸µÅ©
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)