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 #ifndef _terimber_memory_h_
00029 #define _terimber_memory_h_
00030
00031 #include "allinc.h"
00032 #include "base/proto.h"
00033
00034 BEGIN_TERIMBER_NAMESPACE
00035 #pragma pack(4)
00036
00037
00038
00039
00040
00041
00043 #define ALIGNED_MASK_SIZEOF(s, m) ((s+(m-1))&~(m-1))
00045 #define ALIGNED_SIZEOF(s) ALIGNED_MASK_SIZEOF(s, sizeof(void*))
00046
00049 class mem_chunk
00050 {
00051 public:
00052 size_t _chunk_size;
00053 mem_chunk* _next_chunk;
00054 unsigned char _mem[sizeof(void*)];
00055 };
00056
00059 class byte_allocator
00060 {
00062 byte_allocator(const byte_allocator& x);
00064 byte_allocator& operator=(const byte_allocator& x);
00065 public:
00067 byte_allocator( size_t capacity = os_def_size
00068 );
00070 ~byte_allocator();
00072 void
00073 clear_all( bool secure = false
00074 );
00076 void
00077 clear_extra( bool secure = false
00078 );
00080 inline
00081 void
00082 reset( bool secure = false
00083 );
00085 inline
00086 void*
00087 allocate( size_t size
00088 );
00089
00091
00092
00093 inline
00094 void
00095 deallocate( void*
00096 );
00098 inline
00099 size_t
00100 capacity() const;
00102 inline
00103 size_t
00104 count() const;
00105 private:
00107 void*
00108 new_chunk( size_t size
00109 );
00111 void*
00112 next_chunk( size_t size
00113 );
00114 protected:
00115 const size_t _capacity;
00116 size_t _count;
00117 unsigned char* _free_pos;
00118 mem_chunk* _start_chunk;
00119 mem_chunk* _using_chunk;
00120 };
00121
00124 class chunk_stack
00125 {
00126 public:
00128 inline
00129 chunk_stack();
00131 inline
00132 void
00133 clear();
00135 inline
00136 void
00137 push( size_t* chunk
00138 );
00140 inline
00141 size_t*
00142 pop();
00144 inline
00145 size_t*
00146 top();
00148 inline
00149 bool
00150 empty();
00151
00152 private:
00153 size_t* _head;
00154 };
00155
00158 class rep_allocator : public byte_allocator
00159 {
00161 rep_allocator(const rep_allocator& x);
00163 rep_allocator operator=(const rep_allocator& x);
00164 protected:
00166 rep_allocator( size_t capacity = os_def_size
00167 );
00169 ~rep_allocator();
00170
00171 public:
00173 inline
00174 void
00175 clear_all( bool secure = false
00176 );
00178 inline
00179 void
00180 clear_extra( bool secure = false
00181 );
00183 inline
00184 void
00185 reset( bool secure = false
00186 );
00188 inline
00189 void
00190 deallocate( void* p
00191 );
00192 protected:
00193 chunk_stack _rep;
00194 };
00195
00199 template < class T >
00200 class node_allocator : public rep_allocator
00201 {
00202 public:
00204 node_allocator< T >(size_t capacity = os_def_size
00205 );
00207 inline
00208 T*
00209 allocate();
00210 };
00211
00216 template < class T >
00217 class array_allocator : public rep_allocator
00218 {
00219 public:
00221 array_allocator< T >(size_t capacity = os_def_size
00222 );
00224 ~array_allocator< T >();
00226 inline
00227 T*
00228 allocate( size_t n
00229 );
00230
00231 private:
00233 inline
00234 T*
00235 pop( size_t n
00236 );
00237 };
00238
00241 class byte_allocator_creator : public proto_creator< byte_allocator_creator, byte_allocator, size_t >
00242 {
00243 public:
00245 static
00246 byte_allocator*
00247 create( size_t size
00248 );
00250 static
00251 bool
00252 find( byte_allocator* obj,
00253 size_t size
00254 );
00256 static
00257 void
00258 back( byte_allocator* obj,
00259 size_t
00260 );
00262 static
00263 void
00264 deactivate( byte_allocator* obj,
00265 size_t
00266 );
00267 };
00268
00271 template < class T >
00272 class node_allocator_creator : public proto_creator< node_allocator_creator< T >, node_allocator< T >, size_t >
00273 {
00274 public:
00276 static
00277 node_allocator< T >*
00278 create( size_t size
00279 );
00281 static
00282 bool
00283 find( node_allocator< T >* obj,
00284 size_t size
00285 );
00287 static
00288 void
00289 back( node_allocator< T >* obj,
00290 size_t
00291 );
00293 static
00294 void
00295 deactivate( node_allocator< T >* obj,
00296 size_t
00297 );
00298 };
00299
00302 template < class T >
00303 class array_allocator_creator : public proto_creator< array_allocator_creator< T >, array_allocator< T >, size_t >
00304 {
00305 public:
00307 static
00308 array_allocator< T >*
00309 create( size_t size
00310 );
00312 static
00313 bool
00314 find( array_allocator< T >* obj,
00315 size_t size
00316 );
00318 static
00319 void
00320 back( array_allocator< T >* obj,
00321 size_t
00322 );
00324 static
00325 void
00326 deactivate( array_allocator< T >* obj,
00327 size_t
00328 );
00329 };
00330
00331 #pragma pack()
00332 END_TERIMBER_NAMESPACE
00333
00334 #endif // _terimber_memory_h_
00335