00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00015 #ifndef __DERS__STL_ALLOC_HPP__
00016 #define __DERS__STL_ALLOC_HPP__
00017
00018 #include <ders/config.hpp>
00019 #include <ders/mem_pool.hpp>
00020
00021 namespace ders {
00022
00026 template<class T>
00027 class stl_allocator {
00028 mem_pool& mp;
00029
00030 public:
00031 typedef size_t size_type;
00032 typedef ptrdiff_t difference_type;
00033 typedef T* pointer;
00034 typedef const T* const_pointer;
00035 typedef T& reference;
00036 typedef const T& const_reference;
00037 typedef T value_type;
00038
00040 template<class U> struct rebind { typedef stl_allocator<U> other; };
00041
00042 stl_allocator();
00043 explicit stl_allocator(mem_pool& m) : mp(m) {}
00044 template<class U> stl_allocator(const stl_allocator<U>& sa) :
00045 mp(sa.pool()) {}
00046
00047 pointer address(reference x) const { return &x; }
00048 const_pointer address(const_reference x) const { return &x; }
00049 pointer allocate(size_type n, std::allocator<void>::const_pointer = 0);
00050 void deallocate(pointer p, size_type n);
00051 size_type max_size() const throw() { return size_t(-1)/sizeof(T); }
00052 void construct(pointer p, const T& val) { new(p) T(val); }
00053 void destroy(pointer p) { p->~T(); }
00054
00055 mem_pool& pool() const { return mp; }
00056 };
00057
00058 template<class T>
00059 inline typename stl_allocator<T>::pointer stl_allocator<T>::allocate(size_type
00060 n, std::allocator<void>::const_pointer)
00061 { return (n!=0) ? static_cast<pointer>(mp.allocate(n*sizeof(T))) : 0; }
00062
00063 template<class T>
00064 inline void stl_allocator<T>::deallocate(pointer p, size_type n)
00065 { if (n!=0) mp.deallocate(p, n*sizeof(T)); }
00066
00067 template<class T, class U>
00068 inline bool operator==(const stl_allocator<T>& at, const stl_allocator<U>& au)
00069 { return &at.pool()==&au.pool(); }
00070
00071 template<class T, class U>
00072 inline bool operator!=(const stl_allocator<T>& at, const stl_allocator<U>& au)
00073 { return !(at==au); }
00074
00075 }
00076
00077 #endif // __DERS__STL_ALLOC_HPP__
00078