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>
35 , objectsPerPage_( objectsPerPage )
38 assert(
sizeof(AllocatedType) * objectPerAllocation >=
sizeof(AllocatedType *) );
39 assert( objectsPerPage >= 16 );
40 batches_ = allocateBatch( 0 );
41 currentBatch_ = batches_;
46 for ( BatchInfo *batch = batches_; batch; )
48 BatchInfo *nextBatch = batch->next_;
60 AllocatedType *
object = freeHead_;
61 freeHead_ = *(AllocatedType **)
object;
64 if ( currentBatch_->used_ == currentBatch_->end_ )
66 currentBatch_ = currentBatch_->next_;
67 while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
68 currentBatch_ = currentBatch_->next_;
72 currentBatch_ = allocateBatch( objectsPerPage_ );
73 currentBatch_->next_ = batches_;
74 batches_ = currentBatch_;
77 AllocatedType *allocated = currentBatch_->used_;
78 currentBatch_->used_ += objectPerAllocation;
86 assert(
object != 0 );
87 *(AllocatedType **)
object = freeHead_;
97 AllocatedType
buffer_[objectPerAllocation];
104 static BatchInfo *allocateBatch(
unsigned int objectsPerPage )
106 const unsigned int mallocSize =
sizeof(BatchInfo) -
sizeof(AllocatedType)* objectPerAllocation
107 +
sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
108 BatchInfo *batch =
static_cast<BatchInfo*
>(
malloc( mallocSize ) );
110 batch->used_ = batch->buffer_;
111 batch->end_ = batch->buffer_ + objectsPerPage;
116 BatchInfo *currentBatch_;
118 AllocatedType *freeHead_;
119 unsigned int objectsPerPage_;
125 # endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION 127 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED AllocatedType * allocate()
Definition: json_batchallocator.h:56
NSMutableData * buffer_
Definition: GPBCodedOutputStream.m:49
Definition: json_batchallocator.h:30
JSON (JavaScript Object Notation).
Definition: value.h:26
#define malloc
Definition: mbmalloc.h:49
~BatchAllocator()
Definition: json_batchallocator.h:44
void release(AllocatedType *object)
Definition: json_batchallocator.h:84
EGLenum EGLObjectKHR object
Definition: eglext.h:121
#define free
Definition: mbmalloc.h:50
BatchAllocator(unsigned int objectsPerPage=255)
Definition: json_batchallocator.h:33