CMS 3D CMS Logo

json_batchallocator.h
Go to the documentation of this file.
1 #ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
2 #define JSONCPP_BATCHALLOCATOR_H_INCLUDED
3 
4 #include <cassert>
5 #include <cstdlib>
6 
7 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
8 
9 namespace jsoncollector {
10  namespace Json {
11 
12  /* Fast memory allocator.
13  *
14  * This memory allocator allocates memory for a batch of object (specified by
15  * the page size, the number of object in each page).
16  *
17  * It does not allow the destruction of a single object. All the allocated objects
18  * can be destroyed at once. The memory can be either released or reused for future
19  * allocation.
20  *
21  * The in-place new operator must be used to construct the object using the pointer
22  * returned by allocate.
23  */
24  template <typename AllocatedType, const unsigned int objectPerAllocation>
26  public:
27  typedef AllocatedType Type;
28 
29  BatchAllocator(unsigned int objectsPerPage = 255) : freeHead_(0), objectsPerPage_(objectsPerPage) {
30  // printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() );
31  assert(sizeof(AllocatedType) * objectPerAllocation >=
32  sizeof(AllocatedType *)); // We must be able to store a slist in the object free space.
33  assert(objectsPerPage >= 16);
34  batches_ = allocateBatch(0); // allocated a dummy page
36  }
37 
39  for (BatchInfo *batch = batches_; batch;) {
40  BatchInfo *nextBatch = batch->next_;
41  free(batch);
42  batch = nextBatch;
43  }
44  }
45 
48  AllocatedType *allocate() {
49  if (freeHead_) // returns node from free list.
50  {
51  AllocatedType *object = freeHead_;
52  freeHead_ = *(AllocatedType **)object;
53  return object;
54  }
59 
60  if (!currentBatch_) // no free batch found, allocate a new one
61  {
63  currentBatch_->next_ = batches_; // insert at the head of the list
65  }
66  }
67  AllocatedType *allocated = currentBatch_->used_;
68  currentBatch_->used_ += objectPerAllocation;
69  return allocated;
70  }
71 
74  void release(AllocatedType *object) {
75  assert(object != 0);
76  *(AllocatedType **)object = freeHead_;
77  freeHead_ = object;
78  }
79 
80  // disabled copy constructor and assignement operator.
81  BatchAllocator(const BatchAllocator &) = delete;
82  void operator=(const BatchAllocator &) = delete;
83 
84  private:
85  struct BatchInfo {
87  AllocatedType *used_;
88  AllocatedType *end_;
89  AllocatedType buffer_[objectPerAllocation];
90  };
91 
92  static BatchInfo *allocateBatch(unsigned int objectsPerPage) {
93  const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType) * objectPerAllocation +
94  sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
95  BatchInfo *batch = static_cast<BatchInfo *>(malloc(mallocSize));
96  batch->next_ = 0;
97  batch->used_ = batch->buffer_;
98  batch->end_ = batch->buffer_ + objectsPerPage;
99  return batch;
100  }
101 
105  AllocatedType *freeHead_;
106  unsigned int objectsPerPage_;
107  };
108 
109  } // namespace Json
110 } //namespace jsoncollector
111 
112 #endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
113 
114 #endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED
void release(AllocatedType *object)
AllocatedType buffer_[objectPerAllocation]
assert(be >=bs)
BatchAllocator(unsigned int objectsPerPage=255)
void free(void *ptr) noexcept
void operator=(const BatchAllocator &)=delete
void * malloc(size_t size) noexcept
static BatchInfo * allocateBatch(unsigned int objectsPerPage)
AllocatedType * freeHead_
Head of a single linked list within the allocated space of freeed object.
JSON (JavaScript Object Notation).
Definition: DataPoint.h:26