1 #ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
2 # define JSONCPP_BATCHALLOCATOR_H_INCLUDED
7 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
23 template<
typename AllocatedType
24 ,
const unsigned int objectPerAllocation>
28 typedef AllocatedType Type;
30 BatchAllocator(
unsigned int objectsPerPage = 255 )
32 , objectsPerPage_( objectsPerPage )
35 assert(
sizeof(AllocatedType) * objectPerAllocation >=
sizeof(AllocatedType *) );
36 assert( objectsPerPage >= 16 );
37 batches_ = allocateBatch( 0 );
38 currentBatch_ = batches_;
43 for ( BatchInfo *batch = batches_; batch; )
45 BatchInfo *nextBatch = batch->next_;
53 AllocatedType *allocate()
57 AllocatedType *
object = freeHead_;
58 freeHead_ = *(AllocatedType **)
object;
61 if ( currentBatch_->used_ == currentBatch_->end_ )
63 currentBatch_ = currentBatch_->next_;
64 while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
65 currentBatch_ = currentBatch_->next_;
69 currentBatch_ = allocateBatch( objectsPerPage_ );
70 currentBatch_->next_ = batches_;
71 batches_ = currentBatch_;
74 AllocatedType *allocated = currentBatch_->used_;
75 currentBatch_->used_ += objectPerAllocation;
81 void release( AllocatedType *
object )
83 assert(
object != 0 );
84 *(AllocatedType **)
object = freeHead_;
94 AllocatedType buffer_[objectPerAllocation];
98 BatchAllocator(
const BatchAllocator & );
99 void operator =(
const BatchAllocator &);
101 static BatchInfo *allocateBatch(
unsigned int objectsPerPage )
103 const unsigned int mallocSize =
sizeof(BatchInfo) -
sizeof(AllocatedType)* objectPerAllocation
104 +
sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
105 BatchInfo *batch =
static_cast<BatchInfo*
>( malloc( mallocSize ) );
107 batch->used_ = batch->buffer_;
108 batch->end_ = batch->buffer_ + objectsPerPage;
113 BatchInfo *currentBatch_;
115 AllocatedType *freeHead_;
116 unsigned int objectsPerPage_;
122 # endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
124 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED