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