00001 #ifdef CH_LANG_CC
00002
00003
00004
00005
00006
00007
00008
00009 #endif
00010
00011 #ifndef _ARENA_H_
00012 #define _ARENA_H_
00013
00014 #include <cstddef>
00015
00016 #ifdef CH_USE_MEMORY_TRACKING
00017 #include <list>
00018 #include <cstddef>
00019 #endif
00020
00021 #include <set>
00022 #include <vector>
00023 #include "BaseNamespaceHeader.H"
00024
00025 #ifdef CH_USE_MEMORY_TRACKING
00026 class Arena;
00027 typedef std::list<Arena*> ArenaList;
00028 #endif
00029
00030 using std::set;
00031
00033
00040 class Arena
00041 {
00042 public:
00044
00047 Arena();
00048
00050
00053 virtual ~Arena();
00054
00059 virtual void* alloc(size_t a_sz) = 0;
00060
00064 virtual void free(void* a_pt) = 0;
00065
00072 static size_t align(size_t a_sz);
00073
00074 typedef void (*FP)();
00075
00076 #ifdef CH_USE_MEMORY_TRACKING
00077
00080 long int bytes;
00081 long int peak;
00082 static ArenaList* arenaList_;
00083 char name_[120];
00085 #endif
00086
00087
00088 protected:
00089
00090
00091
00092 #ifndef DOXYGEN
00093
00094 union Word
00095 {
00096 void* p;
00097 double d;
00098 long l;
00099 FP f;
00100 };
00101 #endif
00102 };
00103
00104
00105
00106
00107
00108 inline size_t Arena::align (size_t a_s)
00109 {
00110 size_t x = a_s + sizeof(Word) - 1;
00111 x -= x % sizeof(Word);
00112 return x;
00113 }
00114
00116
00123 class BArena: public Arena
00124 {
00125 public:
00127
00131 BArena(const char* a_name = "unnamed");
00132
00136 virtual void* alloc (size_t a_sz);
00137
00139 virtual void free (void* a_pt);
00140 };
00141
00143
00150 class CArena: public Arena
00151 {
00152 public:
00157 CArena(size_t a_hunk_size = 0);
00158
00160 virtual ~CArena();
00161
00163 virtual void* alloc(size_t a_nbytes);
00164
00168 virtual void free(void* a_vp);
00169
00170 #if 0
00171
00175 void* calloc(size_t a_nmemb,
00176 size_t a_size);
00177
00182 void* realloc (void* a_ptr,
00183 size_t a_size);
00184 #endif
00185
00187 enum { DefaultHunkSize = 1024*1024 };
00188
00189
00190 protected:
00191
00192
00193
00194 #ifndef DOXYGEN
00195 class Node
00196 {
00197 public:
00198
00199
00200
00201 Node()
00202 :
00203 m_block(0),
00204 m_size(0)
00205 {}
00206
00207
00208
00209 Node(void* a_block, size_t a_size)
00210 :
00211 m_block(a_block),
00212 m_size(a_size)
00213 {}
00214
00215
00216
00217 Node(const Node& a_rhs)
00218 :
00219 m_block(a_rhs.m_block),
00220 m_size(a_rhs.m_size)
00221 {}
00222
00223
00224
00225 Node& operator = (const Node& a_rhs)
00226 {
00227 m_block = a_rhs.m_block;
00228 m_size = a_rhs.m_size;
00229 return *this;
00230 }
00231
00232
00233
00234 bool operator < (const Node& a_rhs) const
00235 {
00236 return m_block < a_rhs.m_block;
00237 }
00238
00239
00240
00241 bool operator == (const Node& a_rhs) const
00242 {
00243 return m_block == a_rhs.m_block;
00244 }
00245
00246
00247
00248 void* block() const
00249 {
00250 return m_block;
00251 }
00252
00253
00254
00255 void block (void* a_blk)
00256 {
00257 m_block = a_blk;
00258 }
00259
00260
00261
00262 size_t size() const
00263 {
00264 return m_size;
00265 }
00266
00267
00268
00269 void size(size_t a_sz)
00270 {
00271 m_size = a_sz;
00272 }
00273
00274
00275 private:
00276
00277
00278
00279 void* m_block;
00280
00281
00282
00283 size_t m_size;
00284 };
00285
00286
00287
00288
00289
00290 typedef set < Node > NL;
00291
00292
00293
00294
00295 std::vector<void*> m_alloc;
00296
00297
00298
00299
00300
00301 NL m_freelist;
00302
00303
00304
00305
00306
00307 NL m_busylist;
00308
00309
00310
00311
00312 size_t m_hunk;
00313 #endif
00314
00315
00316 private:
00317
00318
00319
00320 CArena (const CArena& a_rhs);
00321 CArena& operator= (const CArena& a_rhs);
00322 };
00323
00324
00325
00326
00327 extern Arena* The_FAB_Arena;
00328
00329 #include "BaseNamespaceFooter.H"
00330 #endif