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 #pragma once
00039 #ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
00040 # define JSONCPP_BATCHALLOCATOR_H_INCLUDED
00041
00042 # include <stdlib.h>
00043 # include <assert.h>
00044
00045 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00046
00047 namespace Json {
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 template<typename AllocatedType
00062 ,const unsigned int objectPerAllocation>
00063 class BatchAllocator
00064 {
00065 public:
00066 typedef AllocatedType Type;
00067
00068 BatchAllocator( unsigned int objectsPerPage = 255 )
00069 : freeHead_( 0 )
00070 , objectsPerPage_( objectsPerPage )
00071 {
00072
00073 assert( sizeof(AllocatedType) * objectPerAllocation >= sizeof(AllocatedType *) );
00074 assert( objectsPerPage >= 16 );
00075 batches_ = allocateBatch( 0 );
00076 currentBatch_ = batches_;
00077 }
00078
00079 ~BatchAllocator()
00080 {
00081 for ( BatchInfo *batch = batches_; batch; )
00082 {
00083 BatchInfo *nextBatch = batch->next_;
00084 free( batch );
00085 batch = nextBatch;
00086 }
00087 }
00088
00091 AllocatedType *allocate()
00092 {
00093 if ( freeHead_ )
00094 {
00095 AllocatedType *object = freeHead_;
00096 freeHead_ = *(AllocatedType **)object;
00097 return object;
00098 }
00099 if ( currentBatch_->used_ == currentBatch_->end_ )
00100 {
00101 currentBatch_ = currentBatch_->next_;
00102 while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
00103 currentBatch_ = currentBatch_->next_;
00104
00105 if ( !currentBatch_ )
00106 {
00107 currentBatch_ = allocateBatch( objectsPerPage_ );
00108 currentBatch_->next_ = batches_;
00109 batches_ = currentBatch_;
00110 }
00111 }
00112 AllocatedType *allocated = currentBatch_->used_;
00113 currentBatch_->used_ += objectPerAllocation;
00114 return allocated;
00115 }
00116
00119 void release( AllocatedType *object )
00120 {
00121 assert( object != 0 );
00122 *(AllocatedType **)object = freeHead_;
00123 freeHead_ = object;
00124 }
00125
00126 private:
00127 struct BatchInfo
00128 {
00129 BatchInfo *next_;
00130 AllocatedType *used_;
00131 AllocatedType *end_;
00132 AllocatedType buffer_[objectPerAllocation];
00133 };
00134
00135
00136 BatchAllocator( const BatchAllocator & );
00137 void operator =( const BatchAllocator &);
00138
00139 static BatchInfo *allocateBatch( unsigned int objectsPerPage )
00140 {
00141 const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType)* objectPerAllocation
00142 + sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
00143 BatchInfo *batch = static_cast<BatchInfo*>( malloc( mallocSize ) );
00144 batch->next_ = 0;
00145 batch->used_ = batch->buffer_;
00146 batch->end_ = batch->buffer_ + objectsPerPage;
00147 return batch;
00148 }
00149
00150 BatchInfo *batches_;
00151 BatchInfo *currentBatch_;
00153 AllocatedType *freeHead_;
00154 unsigned int objectsPerPage_;
00155 };
00156
00157
00158 }
00159
00160 # endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
00161
00162 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED
00163