TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
allocators.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_ALLOCATORS_H_
16 #define RAPIDJSON_ALLOCATORS_H_
17 
18 #include "rapidjson.h"
19 
21 
23 // Allocator
24 
55 // CrtAllocator
57 
59 
62 class CrtAllocator {
63 public:
64  static const bool kNeedFree = true;
65  void* Malloc(size_t size) {
66  if (size) // behavior of malloc(0) is implementation defined.
67  return std::malloc(size);
68  else
69  return NULL; // standardize to returning NULL.
70  }
71  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
72  (void)originalSize;
73  if (newSize == 0) {
74  std::free(originalPtr);
75  return NULL;
76  }
77  return std::realloc(originalPtr, newSize);
78  }
79  static void Free(void *ptr) { std::free(ptr); }
80 };
81 
83 // MemoryPoolAllocator
84 
86 
101 template <typename BaseAllocator = CrtAllocator>
103 public:
104  static const bool kNeedFree = false;
105 
107 
110  MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
111  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
112  {
113  }
114 
116 
125  MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
126  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
127  {
128  RAPIDJSON_ASSERT(buffer != 0);
129  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
130  chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
131  chunkHead_->capacity = size - sizeof(ChunkHeader);
132  chunkHead_->size = 0;
133  chunkHead_->next = 0;
134  }
135 
137 
140  Clear();
142  }
143 
145  void Clear() {
146  while (chunkHead_ && chunkHead_ != userBuffer_) {
148  baseAllocator_->Free(chunkHead_);
149  chunkHead_ = next;
150  }
152  chunkHead_->size = 0; // Clear user buffer
153  }
154 
156 
158  size_t Capacity() const {
159  size_t capacity = 0;
160  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
161  capacity += c->capacity;
162  return capacity;
163  }
164 
166 
168  size_t Size() const {
169  size_t size = 0;
170  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
171  size += c->size;
172  return size;
173  }
174 
176  void* Malloc(size_t size) {
177  if (!size)
178  return NULL;
179 
180  size = RAPIDJSON_ALIGN(size);
181  if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
182  AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
183 
184  void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size;
185  chunkHead_->size += size;
186  return buffer;
187  }
188 
190  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
191  if (originalPtr == 0)
192  return Malloc(newSize);
193 
194  if (newSize == 0)
195  return NULL;
196 
197  // Do not shrink if new size is smaller than original
198  if (originalSize >= newSize)
199  return originalPtr;
200 
201  // Simply expand it if it is the last allocation and there is sufficient space
202  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
203  size_t increment = static_cast<size_t>(newSize - originalSize);
204  increment = RAPIDJSON_ALIGN(increment);
205  if (chunkHead_->size + increment <= chunkHead_->capacity) {
206  chunkHead_->size += increment;
207  return originalPtr;
208  }
209  }
210 
211  // 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;
217  }
218 
220  static void Free(void *ptr) { (void)ptr; } // Do nothing
221 
222 private:
224  MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
226  MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
227 
229 
231  void AddChunk(size_t capacity) {
232  if (!baseAllocator_)
233  ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
234  ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity));
235  chunk->capacity = capacity;
236  chunk->size = 0;
237  chunk->next = chunkHead_;
238  chunkHead_ = chunk;
239  }
240 
241  static const int kDefaultChunkCapacity = 64 * 1024;
242 
244 
246  struct ChunkHeader {
247  size_t capacity;
248  size_t size;
250  };
251 
254  void *userBuffer_;
255  BaseAllocator* baseAllocator_;
256  BaseAllocator* ownBaseAllocator_;
257 };
258 
260 
261 #endif // RAPIDJSON_ENCODINGS_H_
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:158
BaseAllocator * ownBaseAllocator_
base allocator created by this object.
Definition: allocators.h:256
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:176
void * userBuffer_
User supplied buffer.
Definition: allocators.h:254
static const int kDefaultChunkCapacity
Default chunk capacity.
Definition: allocators.h:241
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
void * Malloc(size_t size)
Definition: allocators.h:65
int next(int i, int n)
Definition: RecastContour.cpp:469
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:168
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: adtfile.h:57
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:145
Chunk header for perpending to each chunk.
Definition: allocators.h:246
ChunkHeader * chunkHead_
Head of the chunk linked-list. Only the head chunk serves allocation.
Definition: allocators.h:252
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Definition: allocators.h:71
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
MemoryPoolAllocator & operator=(const MemoryPoolAllocator &rhs)
Copy assignment operator is not permitted.
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484
BaseAllocator * baseAllocator_
base allocator for allocating memory chunks.
Definition: allocators.h:255
void AddChunk(size_t capacity)
Creates a new chunk.
Definition: allocators.h:231
size_t size
Current size of allocated memory in bytes.
Definition: allocators.h:248
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:190
C-runtime library allocator.
Definition: allocators.h:62
size_t capacity
Capacity of the chunk in bytes (excluding the header itself).
Definition: allocators.h:247
common definitions and configuration
static void Free(void *ptr)
Definition: allocators.h:79
size_t chunk_capacity_
The minimum capacity of chunk when they are allocated.
Definition: allocators.h:253
static void Free(void *ptr)
Frees a memory block (concept Allocator)
Definition: allocators.h:220
static const bool kNeedFree
Definition: allocators.h:64
ChunkHeader * next
Next chunk in the linked list.
Definition: allocators.h:249
#define RAPIDJSON_ALIGN(x)
Data alignment of the machine.
Definition: rapidjson.h:247
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:110
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
Definition: allocators.h:104
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:125
Default memory allocator used by the parser and DOM.
Definition: allocators.h:102
~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:139