1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* JSON Library, originally from http://jsoncpp.sourceforge.net/
5
* Copyright (C) 2011 Stewart Smith
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above
16
* copyright notice, this list of conditions and the following disclaimer
17
* in the documentation and/or other materials provided with the
20
* * The names of its contributors may not be used to endorse or
21
* promote products derived from this software without specific prior
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
#ifndef JSONCPP_BATCHALLOCATOR_H_INCLUDED
40
# define JSONCPP_BATCHALLOCATOR_H_INCLUDED
45
# ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
49
/* Fast memory allocator.
51
* This memory allocator allocates memory for a batch of object (specified by
52
* the page size, the number of object in each page).
54
* It does not allow the destruction of a single object. All the allocated objects
55
* can be destroyed at once. The memory can be either released or reused for future
58
* The in-place new operator must be used to construct the object using the pointer
59
* returned by allocate.
61
template<typename AllocatedType
62
,const unsigned int objectPerAllocation>
66
typedef AllocatedType Type;
68
BatchAllocator( unsigned int objectsPerPage = 255 )
70
, objectsPerPage_( objectsPerPage )
72
// printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() );
73
assert( sizeof(AllocatedType) * objectPerAllocation >= sizeof(AllocatedType *) ); // We must be able to store a slist in the object free space.
74
assert( objectsPerPage >= 16 );
75
batches_ = allocateBatch( 0 ); // allocated a dummy page
76
currentBatch_ = batches_;
81
for ( BatchInfo *batch = batches_; batch; )
83
BatchInfo *nextBatch = batch->next_;
89
/// allocate space for an array of objectPerAllocation object.
90
/// @warning it is the responsability of the caller to call objects constructors.
91
AllocatedType *allocate()
93
if ( freeHead_ ) // returns node from free list.
95
AllocatedType *object = freeHead_;
96
freeHead_ = *(AllocatedType **)object;
99
if ( currentBatch_->used_ == currentBatch_->end_ )
101
currentBatch_ = currentBatch_->next_;
102
while ( currentBatch_ && currentBatch_->used_ == currentBatch_->end_ )
103
currentBatch_ = currentBatch_->next_;
105
if ( !currentBatch_ ) // no free batch found, allocate a new one
107
currentBatch_ = allocateBatch( objectsPerPage_ );
108
currentBatch_->next_ = batches_; // insert at the head of the list
109
batches_ = currentBatch_;
112
AllocatedType *allocated = currentBatch_->used_;
113
currentBatch_->used_ += objectPerAllocation;
117
/// Release the object.
118
/// @warning it is the responsability of the caller to actually destruct the object.
119
void release( AllocatedType *object )
121
assert( object != 0 );
122
*(AllocatedType **)object = freeHead_;
130
AllocatedType *used_;
132
AllocatedType buffer_[objectPerAllocation];
135
// disabled copy constructor and assignement operator.
136
BatchAllocator( const BatchAllocator & );
137
void operator =( const BatchAllocator &);
139
static BatchInfo *allocateBatch( unsigned int objectsPerPage )
141
const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType)* objectPerAllocation
142
+ sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
143
BatchInfo *batch = static_cast<BatchInfo*>( malloc( mallocSize ) );
145
batch->used_ = batch->buffer_;
146
batch->end_ = batch->buffer_ + objectsPerPage;
151
BatchInfo *currentBatch_;
152
/// Head of a single linked list within the allocated space of freeed object
153
AllocatedType *freeHead_;
154
unsigned int objectsPerPage_;
160
# endif // ifndef JSONCPP_DOC_INCLUDE_IMPLEMENTATION
162
#endif // JSONCPP_BATCHALLOCATOR_H_INCLUDED