Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef mempool_allocator_INCLUDED
00039 #define mempool_allocator_INCLUDED
00040
00041 #ifndef mempool_INCLUDED
00042 #include "mempool.h"
00043 #endif
00044
00045 template <class T>
00046 class mempool_allocator {
00047 private:
00048 MEM_POOL* pool;
00049 template <class U> friend class mempool_allocator;
00050
00051 public:
00052 typedef size_t size_type;
00053 typedef ptrdiff_t difference_type;
00054 typedef T* pointer;
00055 typedef const T* const_pointer;
00056 typedef T& reference;
00057 typedef const T& const_reference;
00058 typedef T value_type;
00059
00060 template <class U> struct rebind {
00061 typedef mempool_allocator<U> other;
00062 };
00063
00064 mempool_allocator() : pool(Malloc_Mem_Pool) {}
00065 mempool_allocator(MEM_POOL* p) : pool(p) {}
00066 mempool_allocator(const mempool_allocator& a) : pool(a.pool) {}
00067 template <class U> mempool_allocator(const mempool_allocator<U>& a)
00068 : pool(a.pool) {}
00069 ~mempool_allocator() {}
00070
00071 pointer address(reference x) const { return &x; }
00072 const_pointer address(const_reference x) const { return &x; }
00073
00074
00075
00076
00077
00078
00079
00080
00081 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00082 void* allocate(size_type n, const void* = 0)
00083 { return n != 0 ? TYPE_MEM_POOL_ALLOC_N(T, pool, n) : pointer(0); }
00084 #else
00085 pointer allocate(size_type n, const void* = 0)
00086 { return n != 0 ? TYPE_MEM_POOL_ALLOC_N(T, pool, n) : pointer(0); }
00087 #endif
00088
00089
00090 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00091 void deallocate(void* p, size_type) {
00092 if (p)
00093 MEM_POOL_FREE(pool, p);
00094 }
00095 #else
00096 void deallocate(pointer p, size_type ) {
00097 if (p)
00098 MEM_POOL_FREE(pool, p);
00099 }
00100 #endif
00101
00102
00103
00104
00105
00106
00107
00108
00109 #if !defined(__GNUC__) && defined(_SOLARIS_SOLARIS)
00110 size_type max_size(size_t) const { return size_t(-1) / sizeof(T); }
00111 #endif
00112
00113 size_type max_size() const { return size_t(-1) / sizeof(T); }
00114
00115 void construct(pointer p, const T& val) { new(p) T(val); }
00116 void destroy(pointer p) { p->~T(); }
00117 };
00118
00119 template<>
00120 class mempool_allocator<void> {
00121 public:
00122 typedef size_t size_type;
00123 typedef ptrdiff_t difference_type;
00124 typedef void* pointer;
00125 typedef const void* const_pointer;
00126 typedef void value_type;
00127
00128 template <class U> struct rebind {
00129 typedef mempool_allocator<U> other;
00130 };
00131 };
00132 #endif // mempool_allocator_INCLUDED