allocators.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_ALLOCATORS_H_
  19. #define RAPIDJSON_ALLOCATORS_H_
  20. #include "rapidjson.h"
  21. RAPIDJSON_NAMESPACE_BEGIN
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Allocator
  24. /*! \class rapidjson::Allocator
  25. \brief Concept for allocating, resizing and freeing memory block.
  26. Note that Malloc() and Realloc() are non-static but Free() is static.
  27. So if an allocator need to support Free(), it needs to put its pointer in
  28. the header of memory block.
  29. \code
  30. concept Allocator {
  31. static const bool kNeedFree; //!< Whether this allocator needs to call
  32. Free().
  33. // Allocate a memory block.
  34. // \param size of the memory block in bytes.
  35. // \returns pointer to the memory block.
  36. void* Malloc(size_t size);
  37. // Resize a memory block.
  38. // \param originalPtr The pointer to current memory block. Null pointer is
  39. permitted.
  40. // \param originalSize The current size in bytes. (Design issue: since some
  41. allocator may not book-keep this, explicitly pass to it can save memory.)
  42. // \param newSize the new size in bytes.
  43. void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
  44. // Free a memory block.
  45. // \param pointer to the memory block. Null pointer is permitted.
  46. static void Free(void *ptr);
  47. };
  48. \endcode
  49. */
  50. /*! \def RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
  51. \ingroup RAPIDJSON_CONFIG
  52. \brief User-defined kDefaultChunkCapacity definition.
  53. User can define this as any \c size that is a power of 2.
  54. */
  55. #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
  56. #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
  57. #endif
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // CrtAllocator
  60. //! C-runtime library allocator.
  61. /*! This class is just wrapper for standard C library memory routines.
  62. \note implements Allocator concept
  63. */
  64. class CrtAllocator {
  65. public:
  66. static const bool kNeedFree = true;
  67. void *Malloc(size_t size) {
  68. if (size) // behavior of malloc(0) is implementation defined.
  69. return std::malloc(size);
  70. else
  71. return NULL; // standardize to returning NULL.
  72. }
  73. void *Realloc(void *originalPtr, size_t originalSize, size_t newSize) {
  74. (void)originalSize;
  75. if (newSize == 0) {
  76. std::free(originalPtr);
  77. return NULL;
  78. }
  79. return std::realloc(originalPtr, newSize);
  80. }
  81. static void Free(void *ptr) { std::free(ptr); }
  82. };
  83. ///////////////////////////////////////////////////////////////////////////////
  84. // MemoryPoolAllocator
  85. //! Default memory allocator used by the parser and DOM.
  86. /*! This allocator allocate memory blocks from pre-allocated memory chunks.
  87. It does not free memory blocks. And Realloc() only allocate new memory.
  88. The memory chunks are allocated by BaseAllocator, which is CrtAllocator by
  89. default.
  90. User may also supply a buffer as the first chunk.
  91. If the user-buffer is full then additional chunks are allocated by
  92. BaseAllocator.
  93. The user-buffer is not deallocated by this allocator.
  94. \tparam BaseAllocator the allocator type for allocating memory chunks.
  95. Default is CrtAllocator. \note implements Allocator concept
  96. */
  97. template <typename BaseAllocator = CrtAllocator>
  98. class MemoryPoolAllocator {
  99. public:
  100. static const bool kNeedFree =
  101. false; //!< Tell users that no need to call Free() with this allocator.
  102. //!< (concept Allocator)
  103. //! Constructor with chunkSize.
  104. /*! \param chunkSize The size of memory chunk. The default is
  105. kDefaultChunkSize. \param baseAllocator The allocator for allocating memory
  106. chunks.
  107. */
  108. MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity,
  109. BaseAllocator *baseAllocator = 0)
  110. : chunkHead_(0),
  111. chunk_capacity_(chunkSize),
  112. userBuffer_(0),
  113. baseAllocator_(baseAllocator),
  114. ownBaseAllocator_(0) {}
  115. //! Constructor with user-supplied buffer.
  116. /*! The user buffer will be used firstly. When it is full, memory pool
  117. allocates new chunk with chunk size.
  118. The user buffer will not be deallocated when this allocator is destructed.
  119. \param buffer User supplied buffer.
  120. \param size Size of the buffer in bytes. It must at least larger than
  121. sizeof(ChunkHeader). \param chunkSize The size of memory chunk. The default
  122. is kDefaultChunkSize. \param baseAllocator The allocator for allocating
  123. memory chunks.
  124. */
  125. MemoryPoolAllocator(void *buffer, size_t size,
  126. size_t chunkSize = kDefaultChunkCapacity,
  127. BaseAllocator *baseAllocator = 0)
  128. : chunkHead_(0),
  129. chunk_capacity_(chunkSize),
  130. userBuffer_(buffer),
  131. baseAllocator_(baseAllocator),
  132. ownBaseAllocator_(0) {
  133. RAPIDJSON_ASSERT(buffer != 0);
  134. RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
  135. chunkHead_ = reinterpret_cast<ChunkHeader *>(buffer);
  136. chunkHead_->capacity = size - sizeof(ChunkHeader);
  137. chunkHead_->size = 0;
  138. chunkHead_->next = 0;
  139. }
  140. //! Destructor.
  141. /*! This deallocates all memory chunks, excluding the user-supplied buffer.
  142. */
  143. ~MemoryPoolAllocator() {
  144. Clear();
  145. RAPIDJSON_DELETE(ownBaseAllocator_);
  146. }
  147. //! Deallocates all memory chunks, excluding the user-supplied buffer.
  148. void Clear() {
  149. while (chunkHead_ && chunkHead_ != userBuffer_) {
  150. ChunkHeader *next = chunkHead_->next;
  151. baseAllocator_->Free(chunkHead_);
  152. chunkHead_ = next;
  153. }
  154. if (chunkHead_ && chunkHead_ == userBuffer_)
  155. chunkHead_->size = 0; // Clear user buffer
  156. }
  157. //! Computes the total capacity of allocated memory chunks.
  158. /*! \return total capacity in bytes.
  159. */
  160. size_t Capacity() const {
  161. size_t capacity = 0;
  162. for (ChunkHeader *c = chunkHead_; c != 0; c = c->next)
  163. capacity += c->capacity;
  164. return capacity;
  165. }
  166. //! Computes the memory blocks allocated.
  167. /*! \return total used bytes.
  168. */
  169. size_t Size() const {
  170. size_t size = 0;
  171. for (ChunkHeader *c = chunkHead_; c != 0; c = c->next) size += c->size;
  172. return size;
  173. }
  174. //! Allocates a memory block. (concept Allocator)
  175. void *Malloc(size_t size) {
  176. if (!size) return NULL;
  177. size = RAPIDJSON_ALIGN(size);
  178. if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
  179. if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
  180. return NULL;
  181. void *buffer = reinterpret_cast<char *>(chunkHead_) +
  182. RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
  183. chunkHead_->size += size;
  184. return buffer;
  185. }
  186. //! Resizes a memory block (concept Allocator)
  187. void *Realloc(void *originalPtr, size_t originalSize, size_t newSize) {
  188. if (originalPtr == 0) return Malloc(newSize);
  189. if (newSize == 0) return NULL;
  190. originalSize = RAPIDJSON_ALIGN(originalSize);
  191. newSize = RAPIDJSON_ALIGN(newSize);
  192. // Do not shrink if new size is smaller than original
  193. if (originalSize >= newSize) return originalPtr;
  194. // Simply expand it if it is the last allocation and there is sufficient
  195. // space
  196. if (originalPtr ==
  197. reinterpret_cast<char *>(chunkHead_) +
  198. RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size -
  199. originalSize) {
  200. size_t increment = static_cast<size_t>(newSize - originalSize);
  201. if (chunkHead_->size + increment <= chunkHead_->capacity) {
  202. chunkHead_->size += increment;
  203. return originalPtr;
  204. }
  205. }
  206. // Realloc process: allocate and copy memory, do not free original buffer.
  207. if (void *newBuffer = Malloc(newSize)) {
  208. if (originalSize) std::memcpy(newBuffer, originalPtr, originalSize);
  209. return newBuffer;
  210. } else
  211. return NULL;
  212. }
  213. //! Frees a memory block (concept Allocator)
  214. static void Free(void *ptr) { (void)ptr; } // Do nothing
  215. private:
  216. //! Copy constructor is not permitted.
  217. MemoryPoolAllocator(const MemoryPoolAllocator &rhs) /* = delete */;
  218. //! Copy assignment operator is not permitted.
  219. MemoryPoolAllocator &operator=(const MemoryPoolAllocator &rhs) /* = delete */;
  220. //! Creates a new chunk.
  221. /*! \param capacity Capacity of the chunk in bytes.
  222. \return true if success.
  223. */
  224. bool AddChunk(size_t capacity) {
  225. if (!baseAllocator_)
  226. ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
  227. if (ChunkHeader *chunk =
  228. reinterpret_cast<ChunkHeader *>(baseAllocator_->Malloc(
  229. RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
  230. chunk->capacity = capacity;
  231. chunk->size = 0;
  232. chunk->next = chunkHead_;
  233. chunkHead_ = chunk;
  234. return true;
  235. } else
  236. return false;
  237. }
  238. static const int kDefaultChunkCapacity =
  239. RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity.
  240. //! Chunk header for perpending to each chunk.
  241. /*! Chunks are stored as a singly linked list.
  242. */
  243. struct ChunkHeader {
  244. size_t capacity; //!< Capacity of the chunk in bytes (excluding the header
  245. //!< itself).
  246. size_t size; //!< Current size of allocated memory in bytes.
  247. ChunkHeader *next; //!< Next chunk in the linked list.
  248. };
  249. ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head
  250. //!< chunk serves allocation.
  251. size_t chunk_capacity_; //!< The minimum capacity of chunk when they are
  252. //!< allocated.
  253. void *userBuffer_; //!< User supplied buffer.
  254. BaseAllocator
  255. *baseAllocator_; //!< base allocator for allocating memory chunks.
  256. BaseAllocator *ownBaseAllocator_; //!< base allocator created by this object.
  257. };
  258. RAPIDJSON_NAMESPACE_END
  259. #endif // RAPIDJSON_ENCODINGS_H_