allocators.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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.
  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. #ifndef RAPIDJSON_ALLOCATORS_H_
  15. #define RAPIDJSON_ALLOCATORS_H_
  16. #include "rapidjson.h"
  17. #include "internal/meta.h"
  18. #include <memory>
  19. #include <limits>
  20. #if RAPIDJSON_HAS_CXX11
  21. #include <type_traits>
  22. #endif
  23. RAPIDJSON_NAMESPACE_BEGIN
  24. ///////////////////////////////////////////////////////////////////////////////
  25. // Allocator
  26. /*! \class rapidjson::Allocator
  27. \brief Concept for allocating, resizing and freeing memory block.
  28. Note that Malloc() and Realloc() are non-static but Free() is static.
  29. So if an allocator need to support Free(), it needs to put its pointer in
  30. the header of memory block.
  31. \code
  32. concept Allocator {
  33. static const bool kNeedFree; //!< Whether this allocator needs to call Free().
  34. // Allocate a memory block.
  35. // \param size of the memory block in bytes.
  36. // \returns pointer to the memory block.
  37. void* Malloc(size_t size);
  38. // Resize a memory block.
  39. // \param originalPtr The pointer to current memory block. Null pointer is permitted.
  40. // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
  41. // \param newSize the new size in bytes.
  42. void* Realloc(void* originalPtr, size_t originalSize, size_t newSize);
  43. // Free a memory block.
  44. // \param pointer to the memory block. Null pointer is permitted.
  45. static void Free(void *ptr);
  46. };
  47. \endcode
  48. */
  49. /*! \def RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
  50. \ingroup RAPIDJSON_CONFIG
  51. \brief User-defined kDefaultChunkCapacity definition.
  52. User can define this as any \c size that is a power of 2.
  53. */
  54. #ifndef RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY
  55. #define RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY (64 * 1024)
  56. #endif
  57. ///////////////////////////////////////////////////////////////////////////////
  58. // CrtAllocator
  59. //! C-runtime library allocator.
  60. /*! This class is just wrapper for standard C library memory routines.
  61. \note implements Allocator concept
  62. */
  63. class CrtAllocator {
  64. public:
  65. static const bool kNeedFree = true;
  66. void* Malloc(size_t size) {
  67. if (size) // behavior of malloc(0) is implementation defined.
  68. return RAPIDJSON_MALLOC(size);
  69. else
  70. return NULL; // standardize to returning NULL.
  71. }
  72. void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
  73. (void)originalSize;
  74. if (newSize == 0) {
  75. RAPIDJSON_FREE(originalPtr);
  76. return NULL;
  77. }
  78. return RAPIDJSON_REALLOC(originalPtr, newSize);
  79. }
  80. static void Free(void *ptr) RAPIDJSON_NOEXCEPT { RAPIDJSON_FREE(ptr); }
  81. bool operator==(const CrtAllocator&) const RAPIDJSON_NOEXCEPT {
  82. return true;
  83. }
  84. bool operator!=(const CrtAllocator&) const RAPIDJSON_NOEXCEPT {
  85. return false;
  86. }
  87. };
  88. ///////////////////////////////////////////////////////////////////////////////
  89. // MemoryPoolAllocator
  90. //! Default memory allocator used by the parser and DOM.
  91. /*! This allocator allocate memory blocks from pre-allocated memory chunks.
  92. It does not free memory blocks. And Realloc() only allocate new memory.
  93. The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default.
  94. User may also supply a buffer as the first chunk.
  95. If the user-buffer is full then additional chunks are allocated by BaseAllocator.
  96. The user-buffer is not deallocated by this allocator.
  97. \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator.
  98. \note implements Allocator concept
  99. */
  100. template <typename BaseAllocator = CrtAllocator>
  101. class MemoryPoolAllocator {
  102. //! Chunk header for perpending to each chunk.
  103. /*! Chunks are stored as a singly linked list.
  104. */
  105. struct ChunkHeader {
  106. size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself).
  107. size_t size; //!< Current size of allocated memory in bytes.
  108. ChunkHeader *next; //!< Next chunk in the linked list.
  109. };
  110. struct SharedData {
  111. ChunkHeader *chunkHead; //!< Head of the chunk linked-list. Only the head chunk serves allocation.
  112. BaseAllocator* ownBaseAllocator; //!< base allocator created by this object.
  113. size_t refcount;
  114. bool ownBuffer;
  115. };
  116. static const size_t SIZEOF_SHARED_DATA = RAPIDJSON_ALIGN(sizeof(SharedData));
  117. static const size_t SIZEOF_CHUNK_HEADER = RAPIDJSON_ALIGN(sizeof(ChunkHeader));
  118. static inline ChunkHeader *GetChunkHead(SharedData *shared)
  119. {
  120. return reinterpret_cast<ChunkHeader*>(reinterpret_cast<uint8_t*>(shared) + SIZEOF_SHARED_DATA);
  121. }
  122. static inline uint8_t *GetChunkBuffer(SharedData *shared)
  123. {
  124. return reinterpret_cast<uint8_t*>(shared->chunkHead) + SIZEOF_CHUNK_HEADER;
  125. }
  126. static const size_t kDefaultChunkCapacity = RAPIDJSON_ALLOCATOR_DEFAULT_CHUNK_CAPACITY; //!< Default chunk capacity.
  127. public:
  128. static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator)
  129. static const bool kRefCounted = true; //!< Tell users that this allocator is reference counted on copy
  130. //! Constructor with chunkSize.
  131. /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
  132. \param baseAllocator The allocator for allocating memory chunks.
  133. */
  134. explicit
  135. MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
  136. chunk_capacity_(chunkSize),
  137. baseAllocator_(baseAllocator ? baseAllocator : RAPIDJSON_NEW(BaseAllocator)()),
  138. shared_(static_cast<SharedData*>(baseAllocator_ ? baseAllocator_->Malloc(SIZEOF_SHARED_DATA + SIZEOF_CHUNK_HEADER) : 0))
  139. {
  140. RAPIDJSON_ASSERT(baseAllocator_ != 0);
  141. RAPIDJSON_ASSERT(shared_ != 0);
  142. if (baseAllocator) {
  143. shared_->ownBaseAllocator = 0;
  144. }
  145. else {
  146. shared_->ownBaseAllocator = baseAllocator_;
  147. }
  148. shared_->chunkHead = GetChunkHead(shared_);
  149. shared_->chunkHead->capacity = 0;
  150. shared_->chunkHead->size = 0;
  151. shared_->chunkHead->next = 0;
  152. shared_->ownBuffer = true;
  153. shared_->refcount = 1;
  154. }
  155. //! Constructor with user-supplied buffer.
  156. /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size.
  157. The user buffer will not be deallocated when this allocator is destructed.
  158. \param buffer User supplied buffer.
  159. \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader).
  160. \param chunkSize The size of memory chunk. The default is kDefaultChunkSize.
  161. \param baseAllocator The allocator for allocating memory chunks.
  162. */
  163. MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) :
  164. chunk_capacity_(chunkSize),
  165. baseAllocator_(baseAllocator),
  166. shared_(static_cast<SharedData*>(AlignBuffer(buffer, size)))
  167. {
  168. RAPIDJSON_ASSERT(size >= SIZEOF_SHARED_DATA + SIZEOF_CHUNK_HEADER);
  169. shared_->chunkHead = GetChunkHead(shared_);
  170. shared_->chunkHead->capacity = size - SIZEOF_SHARED_DATA - SIZEOF_CHUNK_HEADER;
  171. shared_->chunkHead->size = 0;
  172. shared_->chunkHead->next = 0;
  173. shared_->ownBaseAllocator = 0;
  174. shared_->ownBuffer = false;
  175. shared_->refcount = 1;
  176. }
  177. MemoryPoolAllocator(const MemoryPoolAllocator& rhs) RAPIDJSON_NOEXCEPT :
  178. chunk_capacity_(rhs.chunk_capacity_),
  179. baseAllocator_(rhs.baseAllocator_),
  180. shared_(rhs.shared_)
  181. {
  182. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  183. ++shared_->refcount;
  184. }
  185. MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) RAPIDJSON_NOEXCEPT
  186. {
  187. RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
  188. ++rhs.shared_->refcount;
  189. this->~MemoryPoolAllocator();
  190. baseAllocator_ = rhs.baseAllocator_;
  191. chunk_capacity_ = rhs.chunk_capacity_;
  192. shared_ = rhs.shared_;
  193. return *this;
  194. }
  195. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  196. MemoryPoolAllocator(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT :
  197. chunk_capacity_(rhs.chunk_capacity_),
  198. baseAllocator_(rhs.baseAllocator_),
  199. shared_(rhs.shared_)
  200. {
  201. RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
  202. rhs.shared_ = 0;
  203. }
  204. MemoryPoolAllocator& operator=(MemoryPoolAllocator&& rhs) RAPIDJSON_NOEXCEPT
  205. {
  206. RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
  207. this->~MemoryPoolAllocator();
  208. baseAllocator_ = rhs.baseAllocator_;
  209. chunk_capacity_ = rhs.chunk_capacity_;
  210. shared_ = rhs.shared_;
  211. rhs.shared_ = 0;
  212. return *this;
  213. }
  214. #endif
  215. //! Destructor.
  216. /*! This deallocates all memory chunks, excluding the user-supplied buffer.
  217. */
  218. ~MemoryPoolAllocator() RAPIDJSON_NOEXCEPT {
  219. if (!shared_) {
  220. // do nothing if moved
  221. return;
  222. }
  223. if (shared_->refcount > 1) {
  224. --shared_->refcount;
  225. return;
  226. }
  227. Clear();
  228. BaseAllocator *a = shared_->ownBaseAllocator;
  229. if (shared_->ownBuffer) {
  230. baseAllocator_->Free(shared_);
  231. }
  232. RAPIDJSON_DELETE(a);
  233. }
  234. //! Deallocates all memory chunks, excluding the first/user one.
  235. void Clear() RAPIDJSON_NOEXCEPT {
  236. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  237. for (;;) {
  238. ChunkHeader* c = shared_->chunkHead;
  239. if (!c->next) {
  240. break;
  241. }
  242. shared_->chunkHead = c->next;
  243. baseAllocator_->Free(c);
  244. }
  245. shared_->chunkHead->size = 0;
  246. }
  247. //! Computes the total capacity of allocated memory chunks.
  248. /*! \return total capacity in bytes.
  249. */
  250. size_t Capacity() const RAPIDJSON_NOEXCEPT {
  251. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  252. size_t capacity = 0;
  253. for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next)
  254. capacity += c->capacity;
  255. return capacity;
  256. }
  257. //! Computes the memory blocks allocated.
  258. /*! \return total used bytes.
  259. */
  260. size_t Size() const RAPIDJSON_NOEXCEPT {
  261. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  262. size_t size = 0;
  263. for (ChunkHeader* c = shared_->chunkHead; c != 0; c = c->next)
  264. size += c->size;
  265. return size;
  266. }
  267. //! Whether the allocator is shared.
  268. /*! \return true or false.
  269. */
  270. bool Shared() const RAPIDJSON_NOEXCEPT {
  271. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  272. return shared_->refcount > 1;
  273. }
  274. //! Allocates a memory block. (concept Allocator)
  275. void* Malloc(size_t size) {
  276. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  277. if (!size)
  278. return NULL;
  279. size = RAPIDJSON_ALIGN(size);
  280. if (RAPIDJSON_UNLIKELY(shared_->chunkHead->size + size > shared_->chunkHead->capacity))
  281. if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
  282. return NULL;
  283. void *buffer = GetChunkBuffer(shared_) + shared_->chunkHead->size;
  284. shared_->chunkHead->size += size;
  285. return buffer;
  286. }
  287. //! Resizes a memory block (concept Allocator)
  288. void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
  289. if (originalPtr == 0)
  290. return Malloc(newSize);
  291. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  292. if (newSize == 0)
  293. return NULL;
  294. originalSize = RAPIDJSON_ALIGN(originalSize);
  295. newSize = RAPIDJSON_ALIGN(newSize);
  296. // Do not shrink if new size is smaller than original
  297. if (originalSize >= newSize)
  298. return originalPtr;
  299. // Simply expand it if it is the last allocation and there is sufficient space
  300. if (originalPtr == GetChunkBuffer(shared_) + shared_->chunkHead->size - originalSize) {
  301. size_t increment = static_cast<size_t>(newSize - originalSize);
  302. if (shared_->chunkHead->size + increment <= shared_->chunkHead->capacity) {
  303. shared_->chunkHead->size += increment;
  304. return originalPtr;
  305. }
  306. }
  307. // Realloc process: allocate and copy memory, do not free original buffer.
  308. if (void* newBuffer = Malloc(newSize)) {
  309. if (originalSize)
  310. std::memcpy(newBuffer, originalPtr, originalSize);
  311. return newBuffer;
  312. }
  313. else
  314. return NULL;
  315. }
  316. //! Frees a memory block (concept Allocator)
  317. static void Free(void *ptr) RAPIDJSON_NOEXCEPT { (void)ptr; } // Do nothing
  318. //! Compare (equality) with another MemoryPoolAllocator
  319. bool operator==(const MemoryPoolAllocator& rhs) const RAPIDJSON_NOEXCEPT {
  320. RAPIDJSON_NOEXCEPT_ASSERT(shared_->refcount > 0);
  321. RAPIDJSON_NOEXCEPT_ASSERT(rhs.shared_->refcount > 0);
  322. return shared_ == rhs.shared_;
  323. }
  324. //! Compare (inequality) with another MemoryPoolAllocator
  325. bool operator!=(const MemoryPoolAllocator& rhs) const RAPIDJSON_NOEXCEPT {
  326. return !operator==(rhs);
  327. }
  328. private:
  329. //! Creates a new chunk.
  330. /*! \param capacity Capacity of the chunk in bytes.
  331. \return true if success.
  332. */
  333. bool AddChunk(size_t capacity) {
  334. if (!baseAllocator_)
  335. shared_->ownBaseAllocator = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)();
  336. if (ChunkHeader* chunk = static_cast<ChunkHeader*>(baseAllocator_->Malloc(SIZEOF_CHUNK_HEADER + capacity))) {
  337. chunk->capacity = capacity;
  338. chunk->size = 0;
  339. chunk->next = shared_->chunkHead;
  340. shared_->chunkHead = chunk;
  341. return true;
  342. }
  343. else
  344. return false;
  345. }
  346. static inline void* AlignBuffer(void* buf, size_t &size)
  347. {
  348. RAPIDJSON_NOEXCEPT_ASSERT(buf != 0);
  349. const uintptr_t mask = sizeof(void*) - 1;
  350. const uintptr_t ubuf = reinterpret_cast<uintptr_t>(buf);
  351. if (RAPIDJSON_UNLIKELY(ubuf & mask)) {
  352. const uintptr_t abuf = (ubuf + mask) & ~mask;
  353. RAPIDJSON_ASSERT(size >= abuf - ubuf);
  354. buf = reinterpret_cast<void*>(abuf);
  355. size -= abuf - ubuf;
  356. }
  357. return buf;
  358. }
  359. size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated.
  360. BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks.
  361. SharedData *shared_; //!< The shared data of the allocator
  362. };
  363. namespace internal {
  364. template<typename, typename = void>
  365. struct IsRefCounted :
  366. public FalseType
  367. { };
  368. template<typename T>
  369. struct IsRefCounted<T, typename internal::EnableIfCond<T::kRefCounted>::Type> :
  370. public TrueType
  371. { };
  372. }
  373. template<typename T, typename A>
  374. inline T* Realloc(A& a, T* old_p, size_t old_n, size_t new_n)
  375. {
  376. RAPIDJSON_NOEXCEPT_ASSERT(old_n <= (std::numeric_limits<size_t>::max)() / sizeof(T) && new_n <= (std::numeric_limits<size_t>::max)() / sizeof(T));
  377. return static_cast<T*>(a.Realloc(old_p, old_n * sizeof(T), new_n * sizeof(T)));
  378. }
  379. template<typename T, typename A>
  380. inline T *Malloc(A& a, size_t n = 1)
  381. {
  382. return Realloc<T, A>(a, NULL, 0, n);
  383. }
  384. template<typename T, typename A>
  385. inline void Free(A& a, T *p, size_t n = 1)
  386. {
  387. static_cast<void>(Realloc<T, A>(a, p, n, 0));
  388. }
  389. #ifdef __GNUC__
  390. RAPIDJSON_DIAG_PUSH
  391. RAPIDJSON_DIAG_OFF(effc++) // std::allocator can safely be inherited
  392. #endif
  393. template <typename T, typename BaseAllocator = CrtAllocator>
  394. class StdAllocator :
  395. public std::allocator<T>
  396. {
  397. typedef std::allocator<T> allocator_type;
  398. #if RAPIDJSON_HAS_CXX11
  399. typedef std::allocator_traits<allocator_type> traits_type;
  400. #else
  401. typedef allocator_type traits_type;
  402. #endif
  403. public:
  404. typedef BaseAllocator BaseAllocatorType;
  405. StdAllocator() RAPIDJSON_NOEXCEPT :
  406. allocator_type(),
  407. baseAllocator_()
  408. { }
  409. StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT :
  410. allocator_type(rhs),
  411. baseAllocator_(rhs.baseAllocator_)
  412. { }
  413. template<typename U>
  414. StdAllocator(const StdAllocator<U, BaseAllocator>& rhs) RAPIDJSON_NOEXCEPT :
  415. allocator_type(rhs),
  416. baseAllocator_(rhs.baseAllocator_)
  417. { }
  418. #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
  419. StdAllocator(StdAllocator&& rhs) RAPIDJSON_NOEXCEPT :
  420. allocator_type(std::move(rhs)),
  421. baseAllocator_(std::move(rhs.baseAllocator_))
  422. { }
  423. #endif
  424. #if RAPIDJSON_HAS_CXX11
  425. using propagate_on_container_move_assignment = std::true_type;
  426. using propagate_on_container_swap = std::true_type;
  427. #endif
  428. /* implicit */
  429. StdAllocator(const BaseAllocator& baseAllocator) RAPIDJSON_NOEXCEPT :
  430. allocator_type(),
  431. baseAllocator_(baseAllocator)
  432. { }
  433. ~StdAllocator() RAPIDJSON_NOEXCEPT
  434. { }
  435. template<typename U>
  436. struct rebind {
  437. typedef StdAllocator<U, BaseAllocator> other;
  438. };
  439. typedef typename traits_type::size_type size_type;
  440. typedef typename traits_type::difference_type difference_type;
  441. typedef typename traits_type::value_type value_type;
  442. typedef typename traits_type::pointer pointer;
  443. typedef typename traits_type::const_pointer const_pointer;
  444. #if RAPIDJSON_HAS_CXX11
  445. typedef typename std::add_lvalue_reference<value_type>::type &reference;
  446. typedef typename std::add_lvalue_reference<typename std::add_const<value_type>::type>::type &const_reference;
  447. pointer address(reference r) const RAPIDJSON_NOEXCEPT
  448. {
  449. return std::addressof(r);
  450. }
  451. const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT
  452. {
  453. return std::addressof(r);
  454. }
  455. size_type max_size() const RAPIDJSON_NOEXCEPT
  456. {
  457. return traits_type::max_size(*this);
  458. }
  459. template <typename ...Args>
  460. void construct(pointer p, Args&&... args)
  461. {
  462. traits_type::construct(*this, p, std::forward<Args>(args)...);
  463. }
  464. void destroy(pointer p)
  465. {
  466. traits_type::destroy(*this, p);
  467. }
  468. #else // !RAPIDJSON_HAS_CXX11
  469. typedef typename allocator_type::reference reference;
  470. typedef typename allocator_type::const_reference const_reference;
  471. pointer address(reference r) const RAPIDJSON_NOEXCEPT
  472. {
  473. return allocator_type::address(r);
  474. }
  475. const_pointer address(const_reference r) const RAPIDJSON_NOEXCEPT
  476. {
  477. return allocator_type::address(r);
  478. }
  479. size_type max_size() const RAPIDJSON_NOEXCEPT
  480. {
  481. return allocator_type::max_size();
  482. }
  483. void construct(pointer p, const_reference r)
  484. {
  485. allocator_type::construct(p, r);
  486. }
  487. void destroy(pointer p)
  488. {
  489. allocator_type::destroy(p);
  490. }
  491. #endif // !RAPIDJSON_HAS_CXX11
  492. template <typename U>
  493. U* allocate(size_type n = 1, const void* = 0)
  494. {
  495. return RAPIDJSON_NAMESPACE::Malloc<U>(baseAllocator_, n);
  496. }
  497. template <typename U>
  498. void deallocate(U* p, size_type n = 1)
  499. {
  500. RAPIDJSON_NAMESPACE::Free<U>(baseAllocator_, p, n);
  501. }
  502. pointer allocate(size_type n = 1, const void* = 0)
  503. {
  504. return allocate<value_type>(n);
  505. }
  506. void deallocate(pointer p, size_type n = 1)
  507. {
  508. deallocate<value_type>(p, n);
  509. }
  510. #if RAPIDJSON_HAS_CXX11
  511. using is_always_equal = std::is_empty<BaseAllocator>;
  512. #endif
  513. template<typename U>
  514. bool operator==(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
  515. {
  516. return baseAllocator_ == rhs.baseAllocator_;
  517. }
  518. template<typename U>
  519. bool operator!=(const StdAllocator<U, BaseAllocator>& rhs) const RAPIDJSON_NOEXCEPT
  520. {
  521. return !operator==(rhs);
  522. }
  523. //! rapidjson Allocator concept
  524. static const bool kNeedFree = BaseAllocator::kNeedFree;
  525. static const bool kRefCounted = internal::IsRefCounted<BaseAllocator>::Value;
  526. void* Malloc(size_t size)
  527. {
  528. return baseAllocator_.Malloc(size);
  529. }
  530. void* Realloc(void* originalPtr, size_t originalSize, size_t newSize)
  531. {
  532. return baseAllocator_.Realloc(originalPtr, originalSize, newSize);
  533. }
  534. static void Free(void *ptr) RAPIDJSON_NOEXCEPT
  535. {
  536. BaseAllocator::Free(ptr);
  537. }
  538. private:
  539. template <typename, typename>
  540. friend class StdAllocator; // access to StdAllocator<!T>.*
  541. BaseAllocator baseAllocator_;
  542. };
  543. #if !RAPIDJSON_HAS_CXX17 // std::allocator<void> deprecated in C++17
  544. template <typename BaseAllocator>
  545. class StdAllocator<void, BaseAllocator> :
  546. public std::allocator<void>
  547. {
  548. typedef std::allocator<void> allocator_type;
  549. public:
  550. typedef BaseAllocator BaseAllocatorType;
  551. StdAllocator() RAPIDJSON_NOEXCEPT :
  552. allocator_type(),
  553. baseAllocator_()
  554. { }
  555. StdAllocator(const StdAllocator& rhs) RAPIDJSON_NOEXCEPT :
  556. allocator_type(rhs),
  557. baseAllocator_(rhs.baseAllocator_)
  558. { }
  559. template<typename U>
  560. StdAllocator(const StdAllocator<U, BaseAllocator>& rhs) RAPIDJSON_NOEXCEPT :
  561. allocator_type(rhs),
  562. baseAllocator_(rhs.baseAllocator_)
  563. { }
  564. /* implicit */
  565. StdAllocator(const BaseAllocator& baseAllocator) RAPIDJSON_NOEXCEPT :
  566. allocator_type(),
  567. baseAllocator_(baseAllocator)
  568. { }
  569. ~StdAllocator() RAPIDJSON_NOEXCEPT
  570. { }
  571. template<typename U>
  572. struct rebind {
  573. typedef StdAllocator<U, BaseAllocator> other;
  574. };
  575. typedef typename allocator_type::value_type value_type;
  576. private:
  577. template <typename, typename>
  578. friend class StdAllocator; // access to StdAllocator<!T>.*
  579. BaseAllocator baseAllocator_;
  580. };
  581. #endif
  582. #ifdef __GNUC__
  583. RAPIDJSON_DIAG_POP
  584. #endif
  585. RAPIDJSON_NAMESPACE_END
  586. #endif // RAPIDJSON_ENCODINGS_H_