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