MCPcopy Create free account
hub / github.com/Illumina/hap.py / BatchAllocator

Class BatchAllocator

external/jsoncpp/jsoncpp.cpp:1074–1160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1072 */
1073template <typename AllocatedType, const unsigned int objectPerAllocation>
1074class BatchAllocator {
1075public:
1076 BatchAllocator(unsigned int objectsPerPage = 255)
1077 : freeHead_(0), objectsPerPage_(objectsPerPage) {
1078 // printf( "Size: %d => %s\n", sizeof(AllocatedType),
1079 // typeid(AllocatedType).name() );
1080 assert(sizeof(AllocatedType) * objectPerAllocation >=
1081 sizeof(AllocatedType *)); // We must be able to store a slist in the
1082 // object free space.
1083 assert(objectsPerPage >= 16);
1084 batches_ = allocateBatch(0); // allocated a dummy page
1085 currentBatch_ = batches_;
1086 }
1087
1088 ~BatchAllocator() {
1089 for (BatchInfo *batch = batches_; batch;) {
1090 BatchInfo *nextBatch = batch->next_;
1091 free(batch);
1092 batch = nextBatch;
1093 }
1094 }
1095
1096 /// allocate space for an array of objectPerAllocation object.
1097 /// @warning it is the responsability of the caller to call objects
1098 /// constructors.
1099 AllocatedType *allocate() {
1100 if (freeHead_) // returns node from free list.
1101 {
1102 AllocatedType *object = freeHead_;
1103 freeHead_ = *(AllocatedType **)object;
1104 return object;
1105 }
1106 if (currentBatch_->used_ == currentBatch_->end_) {
1107 currentBatch_ = currentBatch_->next_;
1108 while (currentBatch_ && currentBatch_->used_ == currentBatch_->end_)
1109 currentBatch_ = currentBatch_->next_;
1110
1111 if (!currentBatch_) // no free batch found, allocate a new one
1112 {
1113 currentBatch_ = allocateBatch(objectsPerPage_);
1114 currentBatch_->next_ = batches_; // insert at the head of the list
1115 batches_ = currentBatch_;
1116 }
1117 }
1118 AllocatedType *allocated = currentBatch_->used_;
1119 currentBatch_->used_ += objectPerAllocation;
1120 return allocated;
1121 }
1122
1123 /// Release the object.
1124 /// @warning it is the responsability of the caller to actually destruct the
1125 /// object.
1126 void release(AllocatedType *object) {
1127 assert(object != 0);
1128 *(AllocatedType **)object = freeHead_;
1129 freeHead_ = object;
1130 }
1131

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected