00001 #ifndef GAMMA_ALLOCATOR_H_INC
00002 #define GAMMA_ALLOCATOR_H_INC
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stddef.h>
00012 #include <stdlib.h>
00013
00014 namespace gam{
00015
00016
00017 template <class T> class Allocator;
00018
00019
00020 template<> class Allocator<void> {
00021 public:
00022 typedef void* pointer;
00023 typedef const void* const_pointer;
00024
00025 typedef void value_type;
00026 template <class U> struct rebind { typedef Allocator<U> other; };
00027 };
00028
00029 template <class T> class Allocator{
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 template <class U> struct rebind { typedef Allocator<U> other; };
00039
00040 public:
00041 explicit Allocator(){}
00042 explicit Allocator(const Allocator&){}
00043 template <class U> explicit Allocator(const Allocator<U>&){}
00044 ~Allocator(){}
00045
00046 pointer address(reference x) const { return &x; }
00047 const_pointer address(const_reference x) const { return &x; }
00048
00049 pointer allocate(size_type n, Allocator<void>::const_pointer hint = 0){
00050 return reinterpret_cast<pointer>(::operator new(n * sizeof(T)));
00051 }
00052
00053 void deallocate(pointer p, size_type n){ ::operator delete(p); }
00054
00055 size_type max_size() const {
00056 return static_cast<size_type>(-1) / sizeof(T);
00057 }
00058
00059 void construct(pointer p, const T& val){ new(p) T(val); }
00060 void destroy(pointer p){ p->~T(); }
00061 };
00062
00063 template <class T1, class T2>
00064 bool operator==(const Allocator<T1>&, const Allocator<T2>&){ return true; }
00065
00066 template <class T1, class T2>
00067 bool operator!=(const Allocator<T1>&, const Allocator<T2>&){ return false; }
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 }
00179
00180 #endif