All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
allocators.h
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_ALLOCATORS_H_
22 #define RAPIDJSON_ALLOCATORS_H_
23 
24 #include "rapidjson.h"
25 
26 namespace rapidjson {
27 
28 ///////////////////////////////////////////////////////////////////////////////
29 // Allocator
30 
31 /*! \class rapidjson::Allocator
32  \brief Concept for allocating, resizing and freeing memory block.
33 
34  Note that Malloc() and Realloc() are non-static but Free() is static.
35 
36  So if an allocator need to support Free(), it needs to put its pointer in
37  the header of memory block.
38 
39 \code
40 concept Allocator {
41  static const bool kNeedFree; //!< Whether this allocator needs to call Free().
42 
43  // Allocate a memory block.
44  // \param size of the memory block in bytes.
45  // \returns pointer to the memory block.
46  void* Malloc(size_t size);
47 
48  // Resize a memory block.
49  // \param originalPtr The pointer to current memory block. Null pointer is permitted.
50  // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
51  // \param newSize the new size in bytes.
52  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
53 
54  // Free a memory block.
55  // \param pointer to the memory block. Null pointer is permitted.
56  static void Free(void *ptr);
57 };
58 \endcode
59 */
60 
61 ///////////////////////////////////////////////////////////////////////////////
62 // CrtAllocator
63 
64 //! C-runtime library allocator.
65 /*! This class is just wrapper for standard C library memory routines.
66  \note implements Allocator concept
67 */
68 class CrtAllocator {
69 public:
70  static const bool kNeedFree = true;
71  void* Malloc(size_t size) { return malloc(size); }
72  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return realloc(originalPtr, newSize); }
73  static void Free(void *ptr) { free(ptr); }
74 };
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 // MemoryPoolAllocator
78 
79 //! Default memory allocator used by the parser and DOM.
80 /*! This allocator allocate memory blocks from pre-allocated memory chunks.
81 
82  It does not free memory blocks. And Realloc() only allocate new memory.
83 
84  The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.
85 
86  User may also supply a buffer as the first chunk.
87 
88  If the user-buffer is full then additional chunks are allocated by BaseAllocator.
89 
90  The user-buffer is not deallocated by this allocator.
91 
92  \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator.
93  \note implements Allocator concept
94 */
95 template <typename BaseAllocator = CrtAllocator>
97 public:
98  static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
99 
100  //! Constructor with chunkSize.
101  /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
102  \param baseAllocator The allocator for allocating memory chunks.
103  */
104  MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
105  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
106  {
107  if (!baseAllocator_)
108  ownBaseAllocator_ = baseAllocator_ = new BaseAllocator();
109  AddChunk(chunk_capacity_);
110  }
111 
112  //! Constructor with user-supplied buffer.
113  /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.
114 
115  The user buffer will not be deallocated when this allocator is destructed.
116 
117  \param buffer User supplied buffer.
118  \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
119  \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
120  \param baseAllocator The allocator for allocating memory chunks.
121  */
122  MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
123  chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0)
124  {
125  RAPIDJSON_ASSERT(buffer != 0);
126  RAPIDJSON_ASSERT(size > sizeof(ChunkHeader));
127  chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer);
128  chunkHead_->capacity = size - sizeof(ChunkHeader);
129  chunkHead_->size = 0;
130  chunkHead_->next = 0;
131  }
132 
133  //! Destructor.
134  /*! This deallocates all memory chunks, excluding the user-supplied buffer.
135  */
137  Clear();
138  delete ownBaseAllocator_;
139  }
140 
141  //! Deallocates all memory chunks, excluding the user-supplied buffer.
142  void Clear() {
143  while(chunkHead_ != 0 && chunkHead_ != userBuffer_) {
144  ChunkHeader* next = chunkHead_->next;
145  baseAllocator_->Free(chunkHead_);
146  chunkHead_ = next;
147  }
148  }
149 
150  //! Computes the total capacity of allocated memory chunks.
151  /*! \return total capacity in bytes.
152  */
153  size_t Capacity() const {
154  size_t capacity = 0;
155  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
156  capacity += c->capacity;
157  return capacity;
158  }
159 
160  //! Computes the memory blocks allocated.
161  /*! \return total used bytes.
162  */
163  size_t Size() const {
164  size_t size = 0;
165  for (ChunkHeader* c = chunkHead_; c != 0; c = c->next)
166  size += c->size;
167  return size;
168  }
169 
170  //! Allocates a memory block. (concept Allocator)
171  void* Malloc(size_t size) {
172  size = RAPIDJSON_ALIGN(size);
173  if (chunkHead_->size + size > chunkHead_->capacity)
174  AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
175 
176  void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size;
177  chunkHead_->size += size;
178  return buffer;
179  }
180 
181  //! Resizes a memory block (concept Allocator)
182  void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
183  if (originalPtr == 0)
184  return Malloc(newSize);
185 
186  // Do not shrink if new size is smaller than original
187  if (originalSize >= newSize)
188  return originalPtr;
189 
190  // Simply expand it if it is the last allocation and there is sufficient space
191  if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) {
192  size_t increment = static_cast<size_t>(newSize - originalSize);
193  increment = RAPIDJSON_ALIGN(increment);
194  if (chunkHead_->size + increment <= chunkHead_->capacity) {
195  chunkHead_->size += increment;
196  return originalPtr;
197  }
198  }
199 
200  // Realloc process: allocate and copy memory, do not free original buffer.
201  void* newBuffer = Malloc(newSize);
202  RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
203  return memcpy(newBuffer, originalPtr, originalSize);
204  }
205 
206  //! Frees a memory block (concept Allocator)
207  static void Free(void *ptr) { (void)ptr; } // Do nothing
208 
209 private:
210  //! Copy constructor is not permitted.
211  MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */;
212  //! Copy assignment operator is not permitted.
213  MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */;
214 
215  //! Creates a new chunk.
216  /*! \param capacity Capacity of the chunk in bytes.
217  */
218  void AddChunk(size_t capacity) {
219  ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity));
220  chunk->capacity = capacity;
221  chunk->size = 0;
222  chunk->next = chunkHead_;
223  chunkHead_ = chunk;
224  }
225 
226  static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
227 
228  //! Chunk header for perpending to each chunk.
229  /*! Chunks are stored as a singly linked list.
230  */
231  struct ChunkHeader {
232  size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
233  size_t size; //!< Current size of allocated memory in bytes.
234  ChunkHeader *next; //!< Next chunk in the linked list.
235  };
236 
237  ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
238  size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
239  void *userBuffer_; //!< User supplied buffer.
240  BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
241  BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object.
242 };
243 
244 } // namespace rapidjson
245 
246 #endif // RAPIDJSON_ENCODINGS_H_
~MemoryPoolAllocator()
Destructor.
Definition: allocators.h:136
void * Realloc(void *originalPtr, size_t originalSize, size_t newSize)
Resizes a memory block (concept Allocator)
Definition: allocators.h:182
MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with user-supplied buffer.
Definition: allocators.h:122
size_t Capacity() const
Computes the total capacity of allocated memory chunks.
Definition: allocators.h:153
size_t Size() const
Computes the memory blocks allocated.
Definition: allocators.h:163
void Clear()
Deallocates all memory chunks, excluding the user-supplied buffer.
Definition: allocators.h:142
#define RAPIDJSON_ALIGN(x)
Endianness of the machine.
Definition: rapidjson.h:125
MemoryPoolAllocator(size_t chunkSize=kDefaultChunkCapacity, BaseAllocator *baseAllocator=0)
Constructor with chunkSize.
Definition: allocators.h:104
C-runtime library allocator.
Definition: allocators.h:68
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:175
void * Malloc(size_t size)
Allocates a memory block. (concept Allocator)
Definition: allocators.h:171
common definitions and configuration
static const bool kNeedFree
Tell users that no need to call Free() with this allocator. (concept Allocator)
Definition: allocators.h:98
Default memory allocator used by the parser and DOM.
Definition: allocators.h:96
static void Free(void *ptr)
Frees a memory block (concept Allocator)
Definition: allocators.h:207