39 #ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
40 # define JSONCPP_BATCHALLOCATOR_H_INCLUDED
45 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
61 template<
typename AllocatedType
62 ,
const unsigned int objectPerAllocation>
66 typedef AllocatedType Type;
70 , objectsPerPage_( objectsPerPage )
73 assert(
sizeof(AllocatedType) * objectPerAllocation >=
sizeof(AllocatedType *) );
74 assert( objectsPerPage >= 16 );
75 batches_ = allocateBatch( 0 );
76 currentBatch_ = batches_;
81 for (
BatchInfo *batch = batches_; batch; )
99 if ( currentBatch_->used_ == currentBatch_->end_ )
101 currentBatch_ = currentBatch_->next_;
102 while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
103 currentBatch_ = currentBatch_->next_;
105 if ( !currentBatch_ )
107 currentBatch_ = allocateBatch( objectsPerPage_ );
108 currentBatch_->next_ = batches_;
109 batches_ = currentBatch_;
112 AllocatedType *allocated = currentBatch_->used_;
113 currentBatch_->used_ += objectPerAllocation;
121 assert(
object != 0 );
130 AllocatedType *used_;
132 AllocatedType buffer_[objectPerAllocation];
139 static BatchInfo *allocateBatch(
unsigned int objectsPerPage )
141 const unsigned int mallocSize =
sizeof(
BatchInfo) -
sizeof(AllocatedType)* objectPerAllocation
142 +
sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
145 batch->used_ = batch->buffer_;
146 batch->end_ = batch->buffer_ + objectsPerPage;
151 BatchInfo *currentBatch_;
154 unsigned int objectsPerPage_;
160 # endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
162 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED