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
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef BLOCK_ALLOC_H
00053 #define BLOCK_ALLOC_H
00054
00055 #include "dynarray.h"
00056 #include "mem_block.h"
00057
00058
00059
00060 #include <new>
00061 #include <w.h>
00062 #include <stdlib.h>
00063 #include <deque>
00064
00065 #if defined(SOLARIS2)
00066
00067 #include <typeinfo.h>
00068 #endif
00069
00070
00071
00072 template<class T, size_t MaxBytes>
00073 class block_alloc;
00074
00075 template<class T, size_t MaxBytes>
00076 inline
00077 void* operator new(size_t nbytes, block_alloc<T, MaxBytes> &alloc);
00078
00079 template<class T, size_t MaxBytes>
00080 inline
00081 void operator delete(void* ptr, block_alloc<T, MaxBytes> &alloc);
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 class dynpool : public memory_block::pool_of_blocks {
00105 public:
00106 typedef memory_block::block_of_chips mblock;
00107 private:
00108 pthread_mutex_t _lock;
00109 dynarray _arr;
00110 std::deque<mblock*> _free_list;
00111 size_t _chip_size;
00112 size_t _chip_count;
00113 size_t _log2_block_size;
00114 size_t _arr_end;
00115
00116 public:
00117 NORET dynpool(size_t chip_size, size_t chip_count,
00118 size_t log2_block_size, size_t max_bytes);
00119
00120
00121
00122
00123 virtual
00124 NORET ~dynpool();
00125
00126
00127
00128
00129
00130 virtual
00131 bool validate_pointer(void* ptr);
00132
00133
00134
00135 size_t capacity() const { return _arr.capacity(); }
00136
00137
00138 size_t freelist_size() const { return _free_list.size(); }
00139
00140 virtual void dump() const;
00141
00142 protected:
00143 size_t _size() const {
00144 return _arr_end >> _log2_block_size;
00145 }
00146 mblock* _at(size_t i) {
00147 size_t offset = i << _log2_block_size;
00148 union { char* c; mblock* b; } u = {_arr+offset};
00149 return u.b;
00150 }
00151
00152 virtual
00153 mblock* _acquire_block();
00154
00155 virtual
00156 void _release_block(mblock* b);
00157
00158 };
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201 template<class T, class Pool=dynpool, size_t MaxBytes=0>
00202 struct block_pool
00203 {
00204
00205 typedef memory_block::meta_block_size<sizeof(T)> BlockSize;
00206
00207
00208 static size_t block_size() { return BlockSize::BYTES; }
00209 static size_t chip_count() { return BlockSize::COUNT; }
00210 static size_t chip_size() { return sizeof(T); }
00211
00212
00213 static Pool* get_static_pool() {
00214
00215
00216
00217
00218
00219
00220 static Pool p(chip_size(), chip_count(), BlockSize::LOG2,
00221 MaxBytes? MaxBytes : 1024*1024*1024);
00222
00223 return &p;
00224 }
00225
00226 block_pool()
00227 : _blist(get_static_pool(), chip_size(), chip_count(), block_size())
00228 , _acquires(0)
00229 {
00230 }
00231
00232 void* acquire() {
00233 _acquires++;
00234 memory_block::chip_t result =
00235 _blist.acquire(chip_size(), chip_count(), block_size());
00236 return result.v;
00237
00238 }
00239
00240
00241
00242
00243
00244 static
00245 void release(void* ptr) {
00246 w_assert0(get_static_pool()->validate_pointer(ptr));
00247 memory_block::chip_t chip = {ptr};
00248 memory_block::block_of_chips::release_chip(chip, chip_size(), chip_count(), block_size());
00249 }
00250
00251 void recycle() {
00252 _blist.recycle(chip_size(), chip_count(), block_size());
00253 }
00254 void dump() const {
00255
00256 fprintf(stderr, "DUMP ---> block_pool<%s> %p acquires %d \n",
00257 typeid(T).name(),
00258 (void*)this,
00259 _acquires);
00260 _blist.dump();
00261 }
00262
00263 size_t freelist_size() const { return _blist.pool_freelist_size(); }
00264
00265 private:
00266 memory_block::block_list _blist;
00267 int _acquires;
00268 };
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 template<class T, size_t MaxBytes=0>
00289 struct block_alloc {
00290
00291 typedef block_pool<T, dynpool, MaxBytes> Pool;
00292
00293
00294
00295 void recycle() { _pool.recycle(); }
00296
00297
00298
00299 static
00300 void destroy_object(T* ptr) {
00301 if(ptr == NULL) return;
00302
00303 ptr->~T();
00304 Pool::release(ptr);
00305
00306
00307 }
00308
00309 size_t freelist_size() const { return _pool.freelist_size(); }
00310
00311 private:
00312 Pool _pool;
00313
00314
00315 friend void* operator new<>(size_t, block_alloc<T,MaxBytes> &);
00316 friend void operator delete<>(void*, block_alloc<T,MaxBytes> &);
00317 };
00318
00319 template<class T, size_t MaxBytes>
00320 inline
00321 void* operator new(size_t nbytes, block_alloc<T,MaxBytes> &alloc)
00322 {
00323 (void) nbytes;
00324 w_assert1(nbytes == sizeof(T));
00325 return alloc._pool.acquire();
00326 }
00327
00328
00329
00330
00331
00332 template<class T, size_t MaxBytes>
00333 inline
00334 void operator delete(void* ptr, block_alloc<T,MaxBytes> & )
00335 {
00336 if(ptr == NULL) return;
00337 block_alloc<T,MaxBytes>::Pool::release(ptr);
00338 w_assert2(0);
00339 }
00340
00341
00342
00343
00344
00345
00346
00347 template<class T>
00348 struct object_cache_default_factory {
00349
00350 static T*
00351 construct(memory_block::chip_t ptr) { return new (ptr.v) T; }
00352 static T*
00353 construct(void *ptr) { return new (ptr) T; }
00354
00355 static void
00356 reset(T* t) { }
00357
00358 static T*
00359 init(T* t) { return t; }
00360 };
00361
00362 template<class T>
00363 struct object_cache_initializing_factory {
00364
00365 static T*
00366 construct(memory_block::chip_t ptr) { return new (ptr.v) T; }
00367 static T*
00368 construct(void *ptr) { return new (ptr) T; }
00369
00370 static void
00371 reset(T* t) { t->reset(); }
00372
00373 static T*
00374 init(T* t) { t->init(); return t; }
00375
00376
00377 template<class Arg1>
00378 static T* init(T* t, Arg1 arg1) { t->init(arg1); return t; }
00379 template<class Arg1, class Arg2>
00380 static T* init(T* t, Arg1 arg1, Arg2 arg2) { t->init(arg1, arg2); return t; }
00381 template<class Arg1, class Arg2, class Arg3>
00382 static T* init(T* t, Arg1 arg1, Arg2 arg2, Arg3 arg3) { t->init(arg1, arg2, arg3); return t; }
00383 };
00384
00385
00386
00387
00388
00389
00390
00391 template <class T, class TFactory=object_cache_default_factory<T>, size_t MaxBytes=0>
00392 struct object_cache {
00393
00394 object_cache() {
00395 }
00396 ~object_cache() {
00397 }
00398
00399
00400 T* acquire() {
00401 T* initialized = TFactory::init(_acquire());
00402 w_assert1(initialized != NULL);
00403 return initialized;
00404 }
00405 template<class Arg1>
00406 T* acquire(Arg1 arg1) {
00407 T* initialized = TFactory::init(_acquire(), arg1);
00408 w_assert1(initialized != NULL);
00409 return initialized;
00410 }
00411 template<class Arg1, class Arg2>
00412 T* acquire(Arg1 arg1, Arg2 arg2) {
00413 T* initialized = TFactory::init(_acquire(), arg1, arg2);
00414 w_assert1(initialized != NULL);
00415 return initialized;
00416 }
00417 template<class Arg1, class Arg2, class Arg3>
00418 T* acquire(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
00419 T* initialized = TFactory::init(_acquire(), arg1, arg2, arg3);
00420 w_assert1(initialized != NULL);
00421 return initialized;
00422 }
00423
00424 T* _acquire() {
00425
00426 union { void* v; T* t; } u = {_pool.acquire()};
00427 w_assert1(u.t != NULL);
00428 return u.t;
00429 }
00430
00431 static
00432 void release(T* obj) {
00433 TFactory::reset(obj);
00434 Pool::release(obj);
00435 }
00436
00437 void recycle() { _pool.recycle(); }
00438 void dump() const {
00439 fprintf(stderr,
00440 "\nDUMP object_cache<%s> %p\n" , typeid(T).name(), (void*)this);
00441 _pool.dump();
00442 }
00443
00444 size_t freelist_size() const { return _pool.freelist_size(); }
00445
00446 private:
00447
00448 struct cache_pool : public dynpool {
00449
00450
00451 NORET cache_pool(size_t cs, size_t cc, size_t l2bs, size_t mb)
00452 : dynpool(cs, cc, l2bs, mb)
00453 {
00454 }
00455 virtual void _release_block(mblock* b);
00456 virtual mblock* _acquire_block();
00457 virtual NORET ~cache_pool();
00458 };
00459
00460 typedef block_pool<T, cache_pool, MaxBytes> Pool;
00461
00462 Pool _pool;
00463
00464
00465 };
00466
00467 template <class T, class TF, size_t M>
00468 inline
00469 void object_cache<T,TF,M>::cache_pool::_release_block(mblock* b) {
00470 union { cache_pool* cp; memory_block::block_list* bl; } u={this};
00471 b->_owner = u.bl;
00472 dynpool::_release_block(b);
00473 }
00474
00475
00476
00477
00478 template <class T, class TF, size_t M>
00479 inline
00480 dynpool::mblock* object_cache<T,TF,M>::cache_pool::_acquire_block() {
00481 dynpool::mblock* b = dynpool::_acquire_block();
00482 void* me = this;
00483 if(me != b->_owner) {
00484
00485
00486
00487 for(size_t j=0; j < Pool::chip_count(); j++) {
00488 memory_block::chip_t chip = b->get(j, Pool::chip_size());
00489 T *constructed = TF::construct(chip.v);
00490 w_assert0(constructed != NULL);
00491 }
00492 b->_owner = 0;
00493 }
00494 return b;
00495 }
00496
00497
00498
00499 template <class T, class TF, size_t M>
00500 inline
00501 NORET object_cache<T,TF,M>::cache_pool::~cache_pool() {
00502 size_t size = _size();
00503 for(size_t i=0; i < size; i++) {
00504 mblock* b = _at(i);
00505 for(size_t j=0; j < Pool::chip_count(); j++) {
00506 memory_block::chip_t chip = b->get(j, Pool::chip_size());
00507 union { char* c; T* t; } u = {chip.c};
00508 u.t->~T();
00509 }
00510 }
00511 }
00512
00513 #endif