6 #ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
7 # define JSONCPP_BATCHALLOCATOR_H_INCLUDED
12 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
28 template<
typename AllocatedType
29 ,
const unsigned int objectPerAllocation>
33 typedef AllocatedType Type;
35 BatchAllocator(
unsigned int objectsPerPage = 255 )
37 , objectsPerPage_( objectsPerPage )
40 assert(
sizeof(AllocatedType) * objectPerAllocation >=
sizeof(AllocatedType *) );
41 assert( objectsPerPage >= 16 );
42 batches_ = allocateBatch( 0 );
43 currentBatch_ = batches_;
48 for ( BatchInfo *batch = batches_; batch; )
50 BatchInfo *nextBatch = batch->next_;
58 AllocatedType *allocate()
62 AllocatedType *
object = freeHead_;
63 freeHead_ = *(AllocatedType **)
object;
66 if ( currentBatch_->used_ == currentBatch_->end_ )
68 currentBatch_ = currentBatch_->next_;
69 while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
70 currentBatch_ = currentBatch_->next_;
74 currentBatch_ = allocateBatch( objectsPerPage_ );
75 currentBatch_->next_ = batches_;
76 batches_ = currentBatch_;
79 AllocatedType *allocated = currentBatch_->used_;
80 currentBatch_->used_ += objectPerAllocation;
86 void release( AllocatedType *
object )
88 assert(
object != 0 );
89 *(AllocatedType **)
object = freeHead_;
99 AllocatedType buffer_[objectPerAllocation];
103 BatchAllocator(
const BatchAllocator & );
104 void operator =(
const BatchAllocator &);
106 static BatchInfo *allocateBatch(
unsigned int objectsPerPage )
108 const unsigned int mallocSize =
sizeof(BatchInfo) -
sizeof(AllocatedType)* objectPerAllocation
109 +
sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
110 BatchInfo *batch =
static_cast<BatchInfo*
>( malloc( mallocSize ) );
112 batch->used_ = batch->buffer_;
113 batch->end_ = batch->buffer_ + objectsPerPage;
118 BatchInfo *currentBatch_;
120 AllocatedType *freeHead_;
121 unsigned int objectsPerPage_;
127 # endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
129 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED
JSON (JavaScript Object Notation).