@@ -52,6 +52,19 @@ concept Allocator {
5252\endcode
5353*/
5454
55+
56+ /* ! \def RAPIDJSON_ALLOCATOR_DEFUALT_CHUNK_CAPACITY
57+ \ingroup RAPIDJSON_CONFIG
58+ \brief User-defined kDefaultChunkCapacity definition.
59+
60+ User can define this as any \c size that is a power of 2.
61+ */
62+
63+ #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
64+ #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024 )
65+ #endif
66+
67+
5568// /////////////////////////////////////////////////////////////////////////////
5669// CrtAllocator
5770
@@ -179,7 +192,8 @@ class MemoryPoolAllocator {
179192
180193 size = RAPIDJSON_ALIGN (size);
181194 if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity )
182- AddChunk (chunk_capacity_ > size ? chunk_capacity_ : size);
195+ if (!AddChunk (chunk_capacity_ > size ? chunk_capacity_ : size))
196+ return NULL ;
183197
184198 void *buffer = reinterpret_cast <char *>(chunkHead_) + RAPIDJSON_ALIGN (sizeof (ChunkHeader)) + chunkHead_->size ;
185199 chunkHead_->size += size;
@@ -194,26 +208,30 @@ class MemoryPoolAllocator {
194208 if (newSize == 0 )
195209 return NULL ;
196210
211+ originalSize = RAPIDJSON_ALIGN (originalSize);
212+ newSize = RAPIDJSON_ALIGN (newSize);
213+
197214 // Do not shrink if new size is smaller than original
198215 if (originalSize >= newSize)
199216 return originalPtr;
200217
201218 // Simply expand it if it is the last allocation and there is sufficient space
202- if (originalPtr == ( char *) (chunkHead_) + RAPIDJSON_ALIGN (sizeof (ChunkHeader)) + chunkHead_->size - originalSize) {
219+ if (originalPtr == reinterpret_cast < char *> (chunkHead_) + RAPIDJSON_ALIGN (sizeof (ChunkHeader)) + chunkHead_->size - originalSize) {
203220 size_t increment = static_cast <size_t >(newSize - originalSize);
204- increment = RAPIDJSON_ALIGN (increment);
205221 if (chunkHead_->size + increment <= chunkHead_->capacity ) {
206222 chunkHead_->size += increment;
207223 return originalPtr;
208224 }
209225 }
210226
211227 // Realloc process: allocate and copy memory, do not free original buffer.
212- void * newBuffer = Malloc (newSize);
213- RAPIDJSON_ASSERT (newBuffer != 0 ); // Do not handle out-of-memory explicitly.
214- if (originalSize)
215- std::memcpy (newBuffer, originalPtr, originalSize);
216- return newBuffer;
228+ if (void * newBuffer = Malloc (newSize)) {
229+ if (originalSize)
230+ std::memcpy (newBuffer, originalPtr, originalSize);
231+ return newBuffer;
232+ }
233+ else
234+ return NULL ;
217235 }
218236
219237 // ! Frees a memory block (concept Allocator)
@@ -227,18 +245,23 @@ class MemoryPoolAllocator {
227245
228246 // ! Creates a new chunk.
229247 /* ! \param capacity Capacity of the chunk in bytes.
248+ \return true if success.
230249 */
231- void AddChunk (size_t capacity) {
250+ bool AddChunk (size_t capacity) {
232251 if (!baseAllocator_)
233- ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW (BaseAllocator ());
234- ChunkHeader* chunk = reinterpret_cast <ChunkHeader*>(baseAllocator_->Malloc (RAPIDJSON_ALIGN (sizeof (ChunkHeader)) + capacity));
235- chunk->capacity = capacity;
236- chunk->size = 0 ;
237- chunk->next = chunkHead_;
238- chunkHead_ = chunk;
252+ ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW (BaseAllocator)();
253+ if (ChunkHeader* chunk = reinterpret_cast <ChunkHeader*>(baseAllocator_->Malloc (RAPIDJSON_ALIGN (sizeof (ChunkHeader)) + capacity))) {
254+ chunk->capacity = capacity;
255+ chunk->size = 0 ;
256+ chunk->next = chunkHead_;
257+ chunkHead_ = chunk;
258+ return true ;
259+ }
260+ else
261+ return false ;
239262 }
240263
241- static const int kDefaultChunkCapacity = 64 * 1024 ; // !< Default chunk capacity.
264+ static const int kDefaultChunkCapacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY ; // !< Default chunk capacity.
242265
243266 // ! Chunk header for perpending to each chunk.
244267 /* ! Chunks are stored as a singly linked list.
0 commit comments