DeepCover Embedded Security in IoT: Public-key Secured Data Paths
Dependencies: MaximInterface
The MAXREFDES155# is an internet-of-things (IoT) embedded-security reference design, built to authenticate and control a sensing node using elliptic-curve-based public-key cryptography with control and notification from a web server.
The hardware includes an ARM® mbed™ shield and attached sensor endpoint. The shield contains a DS2476 DeepCover® ECDSA/SHA-2 coprocessor, Wifi communication, LCD push-button controls, and status LEDs. The sensor endpoint is attached to the shield using a 300mm cable and contains a DS28C36 DeepCover ECDSA/SHA-2 authenticator, IR-thermal sensor, and aiming laser for the IR sensor. The MAXREFDES155# is equipped with a standard Arduino® form-factor shield connector for immediate testing using an mbed board such as the MAX32600MBED#. The combination of these two devices represent an IoT device. Communication to the web server is accomplished with the shield Wifi circuitry. Communication from the shield to the attached sensor module is accomplished over I2C . The sensor module represents an IoT endpoint that generates small data with a requirement for message authenticity/integrity and secure on/off operational control.
The design is hierarchical with each mbed platform and shield communicating data from the sensor node to a web server that maintains a centralized log and dispatches notifications as necessary. The simplicity of this design enables rapid integration into any star-topology IoT network to provide security with the low overhead and cost provided by the ECDSA-P256 asymmetric-key and SHA-256 symmetric-key algorithms.
More information about the MAXREFDES155# is available on the Maxim Integrated website.
xternal/rapidjson/allocators.h@18:c2631e985780, 2019-12-03 (annotated)
- Committer:
- IanBenzMaxim
- Date:
- Tue Dec 03 12:56:25 2019 -0600
- Revision:
- 18:c2631e985780
- Parent:
- 16:a004191a79ab
Updated MaximInterface to version 2.1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IanBenzMaxim | 0:33d4e66780c0 | 1 | // Tencent is pleased to support the open source community by making RapidJSON available. |
IanBenzMaxim | 0:33d4e66780c0 | 2 | // |
IanBenzMaxim | 0:33d4e66780c0 | 3 | // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. |
IanBenzMaxim | 0:33d4e66780c0 | 4 | // |
IanBenzMaxim | 0:33d4e66780c0 | 5 | // Licensed under the MIT License (the "License"); you may not use this file except |
IanBenzMaxim | 0:33d4e66780c0 | 6 | // in compliance with the License. You may obtain a copy of the License at |
IanBenzMaxim | 0:33d4e66780c0 | 7 | // |
IanBenzMaxim | 0:33d4e66780c0 | 8 | // http://opensource.org/licenses/MIT |
IanBenzMaxim | 0:33d4e66780c0 | 9 | // |
IanBenzMaxim | 0:33d4e66780c0 | 10 | // Unless required by applicable law or agreed to in writing, software distributed |
IanBenzMaxim | 0:33d4e66780c0 | 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
IanBenzMaxim | 0:33d4e66780c0 | 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
IanBenzMaxim | 0:33d4e66780c0 | 13 | // specific language governing permissions and limitations under the License. |
IanBenzMaxim | 0:33d4e66780c0 | 14 | |
IanBenzMaxim | 0:33d4e66780c0 | 15 | #ifndef RAPIDJSON_ALLOCATORS_H_ |
IanBenzMaxim | 0:33d4e66780c0 | 16 | #define RAPIDJSON_ALLOCATORS_H_ |
IanBenzMaxim | 0:33d4e66780c0 | 17 | |
IanBenzMaxim | 0:33d4e66780c0 | 18 | #include "rapidjson.h" |
IanBenzMaxim | 0:33d4e66780c0 | 19 | |
IanBenzMaxim | 0:33d4e66780c0 | 20 | RAPIDJSON_NAMESPACE_BEGIN |
IanBenzMaxim | 0:33d4e66780c0 | 21 | |
IanBenzMaxim | 0:33d4e66780c0 | 22 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 23 | // Allocator |
IanBenzMaxim | 0:33d4e66780c0 | 24 | |
IanBenzMaxim | 0:33d4e66780c0 | 25 | /*! \class rapidjson::Allocator |
IanBenzMaxim | 0:33d4e66780c0 | 26 | \brief Concept for allocating, resizing and freeing memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 27 | |
IanBenzMaxim | 0:33d4e66780c0 | 28 | Note that Malloc() and Realloc() are non-static but Free() is static. |
IanBenzMaxim | 0:33d4e66780c0 | 29 | |
IanBenzMaxim | 0:33d4e66780c0 | 30 | So if an allocator need to support Free(), it needs to put its pointer in |
IanBenzMaxim | 0:33d4e66780c0 | 31 | the header of memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 32 | |
IanBenzMaxim | 0:33d4e66780c0 | 33 | \code |
IanBenzMaxim | 0:33d4e66780c0 | 34 | concept Allocator { |
IanBenzMaxim | 0:33d4e66780c0 | 35 | static const bool kNeedFree; //!< Whether this allocator needs to call Free(). |
IanBenzMaxim | 0:33d4e66780c0 | 36 | |
IanBenzMaxim | 0:33d4e66780c0 | 37 | // Allocate a memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 38 | // \param size of the memory block in bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 39 | // \returns pointer to the memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 40 | void* Malloc(size_t size); |
IanBenzMaxim | 0:33d4e66780c0 | 41 | |
IanBenzMaxim | 0:33d4e66780c0 | 42 | // Resize a memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 43 | // \param originalPtr The pointer to current memory block. Null pointer is permitted. |
IanBenzMaxim | 0:33d4e66780c0 | 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.) |
IanBenzMaxim | 0:33d4e66780c0 | 45 | // \param newSize the new size in bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 46 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); |
IanBenzMaxim | 0:33d4e66780c0 | 47 | |
IanBenzMaxim | 0:33d4e66780c0 | 48 | // Free a memory block. |
IanBenzMaxim | 0:33d4e66780c0 | 49 | // \param pointer to the memory block. Null pointer is permitted. |
IanBenzMaxim | 0:33d4e66780c0 | 50 | static void Free(void *ptr); |
IanBenzMaxim | 0:33d4e66780c0 | 51 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 52 | \endcode |
IanBenzMaxim | 0:33d4e66780c0 | 53 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 54 | |
IanBenzMaxim | 0:33d4e66780c0 | 55 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 56 | // CrtAllocator |
IanBenzMaxim | 0:33d4e66780c0 | 57 | |
IanBenzMaxim | 0:33d4e66780c0 | 58 | //! C-runtime library allocator. |
IanBenzMaxim | 0:33d4e66780c0 | 59 | /*! This class is just wrapper for standard C library memory routines. |
IanBenzMaxim | 0:33d4e66780c0 | 60 | \note implements Allocator concept |
IanBenzMaxim | 0:33d4e66780c0 | 61 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 62 | class CrtAllocator { |
IanBenzMaxim | 0:33d4e66780c0 | 63 | public: |
IanBenzMaxim | 0:33d4e66780c0 | 64 | static const bool kNeedFree = true; |
IanBenzMaxim | 0:33d4e66780c0 | 65 | void* Malloc(size_t size) { |
IanBenzMaxim | 0:33d4e66780c0 | 66 | if (size) // behavior of malloc(0) is implementation defined. |
IanBenzMaxim | 0:33d4e66780c0 | 67 | return std::malloc(size); |
IanBenzMaxim | 0:33d4e66780c0 | 68 | else |
IanBenzMaxim | 0:33d4e66780c0 | 69 | return NULL; // standardize to returning NULL. |
IanBenzMaxim | 0:33d4e66780c0 | 70 | } |
IanBenzMaxim | 0:33d4e66780c0 | 71 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { |
IanBenzMaxim | 0:33d4e66780c0 | 72 | (void)originalSize; |
IanBenzMaxim | 0:33d4e66780c0 | 73 | if (newSize == 0) { |
IanBenzMaxim | 0:33d4e66780c0 | 74 | std::free(originalPtr); |
IanBenzMaxim | 0:33d4e66780c0 | 75 | return NULL; |
IanBenzMaxim | 0:33d4e66780c0 | 76 | } |
IanBenzMaxim | 0:33d4e66780c0 | 77 | return std::realloc(originalPtr, newSize); |
IanBenzMaxim | 0:33d4e66780c0 | 78 | } |
IanBenzMaxim | 0:33d4e66780c0 | 79 | static void Free(void *ptr) { std::free(ptr); } |
IanBenzMaxim | 0:33d4e66780c0 | 80 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 81 | |
IanBenzMaxim | 0:33d4e66780c0 | 82 | /////////////////////////////////////////////////////////////////////////////// |
IanBenzMaxim | 0:33d4e66780c0 | 83 | // MemoryPoolAllocator |
IanBenzMaxim | 0:33d4e66780c0 | 84 | |
IanBenzMaxim | 0:33d4e66780c0 | 85 | //! Default memory allocator used by the parser and DOM. |
IanBenzMaxim | 0:33d4e66780c0 | 86 | /*! This allocator allocate memory blocks from pre-allocated memory chunks. |
IanBenzMaxim | 0:33d4e66780c0 | 87 | |
IanBenzMaxim | 0:33d4e66780c0 | 88 | It does not free memory blocks. And Realloc() only allocate new memory. |
IanBenzMaxim | 0:33d4e66780c0 | 89 | |
IanBenzMaxim | 0:33d4e66780c0 | 90 | The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. |
IanBenzMaxim | 0:33d4e66780c0 | 91 | |
IanBenzMaxim | 0:33d4e66780c0 | 92 | User may also supply a buffer as the first chunk. |
IanBenzMaxim | 0:33d4e66780c0 | 93 | |
IanBenzMaxim | 0:33d4e66780c0 | 94 | If the user-buffer is full then additional chunks are allocated by BaseAllocator. |
IanBenzMaxim | 0:33d4e66780c0 | 95 | |
IanBenzMaxim | 0:33d4e66780c0 | 96 | The user-buffer is not deallocated by this allocator. |
IanBenzMaxim | 0:33d4e66780c0 | 97 | |
IanBenzMaxim | 0:33d4e66780c0 | 98 | \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. |
IanBenzMaxim | 0:33d4e66780c0 | 99 | \note implements Allocator concept |
IanBenzMaxim | 0:33d4e66780c0 | 100 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 101 | template <typename BaseAllocator = CrtAllocator> |
IanBenzMaxim | 0:33d4e66780c0 | 102 | class MemoryPoolAllocator { |
IanBenzMaxim | 0:33d4e66780c0 | 103 | public: |
IanBenzMaxim | 0:33d4e66780c0 | 104 | static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) |
IanBenzMaxim | 0:33d4e66780c0 | 105 | |
IanBenzMaxim | 0:33d4e66780c0 | 106 | //! Constructor with chunkSize. |
IanBenzMaxim | 0:33d4e66780c0 | 107 | /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. |
IanBenzMaxim | 0:33d4e66780c0 | 108 | \param baseAllocator The allocator for allocating memory chunks. |
IanBenzMaxim | 0:33d4e66780c0 | 109 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 110 | MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : |
IanBenzMaxim | 0:33d4e66780c0 | 111 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) |
IanBenzMaxim | 0:33d4e66780c0 | 112 | { |
IanBenzMaxim | 0:33d4e66780c0 | 113 | } |
IanBenzMaxim | 0:33d4e66780c0 | 114 | |
IanBenzMaxim | 0:33d4e66780c0 | 115 | //! Constructor with user-supplied buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 116 | /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. |
IanBenzMaxim | 0:33d4e66780c0 | 117 | |
IanBenzMaxim | 0:33d4e66780c0 | 118 | The user buffer will not be deallocated when this allocator is destructed. |
IanBenzMaxim | 0:33d4e66780c0 | 119 | |
IanBenzMaxim | 0:33d4e66780c0 | 120 | \param buffer User supplied buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 121 | \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). |
IanBenzMaxim | 0:33d4e66780c0 | 122 | \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. |
IanBenzMaxim | 0:33d4e66780c0 | 123 | \param baseAllocator The allocator for allocating memory chunks. |
IanBenzMaxim | 0:33d4e66780c0 | 124 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 125 | MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : |
IanBenzMaxim | 0:33d4e66780c0 | 126 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) |
IanBenzMaxim | 0:33d4e66780c0 | 127 | { |
IanBenzMaxim | 0:33d4e66780c0 | 128 | RAPIDJSON_ASSERT(buffer != 0); |
IanBenzMaxim | 0:33d4e66780c0 | 129 | RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); |
IanBenzMaxim | 0:33d4e66780c0 | 130 | chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer); |
IanBenzMaxim | 0:33d4e66780c0 | 131 | chunkHead_->capacity = size - sizeof(ChunkHeader); |
IanBenzMaxim | 0:33d4e66780c0 | 132 | chunkHead_->size = 0; |
IanBenzMaxim | 0:33d4e66780c0 | 133 | chunkHead_->next = 0; |
IanBenzMaxim | 0:33d4e66780c0 | 134 | } |
IanBenzMaxim | 0:33d4e66780c0 | 135 | |
IanBenzMaxim | 0:33d4e66780c0 | 136 | //! Destructor. |
IanBenzMaxim | 0:33d4e66780c0 | 137 | /*! This deallocates all memory chunks, excluding the user-supplied buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 138 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 139 | ~MemoryPoolAllocator() { |
IanBenzMaxim | 0:33d4e66780c0 | 140 | Clear(); |
IanBenzMaxim | 0:33d4e66780c0 | 141 | RAPIDJSON_DELETE(ownBaseAllocator_); |
IanBenzMaxim | 0:33d4e66780c0 | 142 | } |
IanBenzMaxim | 0:33d4e66780c0 | 143 | |
IanBenzMaxim | 0:33d4e66780c0 | 144 | //! Deallocates all memory chunks, excluding the user-supplied buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 145 | void Clear() { |
IanBenzMaxim | 0:33d4e66780c0 | 146 | while (chunkHead_ && chunkHead_ != userBuffer_) { |
IanBenzMaxim | 0:33d4e66780c0 | 147 | ChunkHeader* next = chunkHead_->next; |
IanBenzMaxim | 0:33d4e66780c0 | 148 | baseAllocator_->Free(chunkHead_); |
IanBenzMaxim | 0:33d4e66780c0 | 149 | chunkHead_ = next; |
IanBenzMaxim | 0:33d4e66780c0 | 150 | } |
IanBenzMaxim | 0:33d4e66780c0 | 151 | if (chunkHead_ && chunkHead_ == userBuffer_) |
IanBenzMaxim | 0:33d4e66780c0 | 152 | chunkHead_->size = 0; // Clear user buffer |
IanBenzMaxim | 0:33d4e66780c0 | 153 | } |
IanBenzMaxim | 0:33d4e66780c0 | 154 | |
IanBenzMaxim | 0:33d4e66780c0 | 155 | //! Computes the total capacity of allocated memory chunks. |
IanBenzMaxim | 0:33d4e66780c0 | 156 | /*! \return total capacity in bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 157 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 158 | size_t Capacity() const { |
IanBenzMaxim | 0:33d4e66780c0 | 159 | size_t capacity = 0; |
IanBenzMaxim | 0:33d4e66780c0 | 160 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) |
IanBenzMaxim | 0:33d4e66780c0 | 161 | capacity += c->capacity; |
IanBenzMaxim | 0:33d4e66780c0 | 162 | return capacity; |
IanBenzMaxim | 0:33d4e66780c0 | 163 | } |
IanBenzMaxim | 0:33d4e66780c0 | 164 | |
IanBenzMaxim | 0:33d4e66780c0 | 165 | //! Computes the memory blocks allocated. |
IanBenzMaxim | 0:33d4e66780c0 | 166 | /*! \return total used bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 167 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 168 | size_t Size() const { |
IanBenzMaxim | 0:33d4e66780c0 | 169 | size_t size = 0; |
IanBenzMaxim | 0:33d4e66780c0 | 170 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) |
IanBenzMaxim | 0:33d4e66780c0 | 171 | size += c->size; |
IanBenzMaxim | 0:33d4e66780c0 | 172 | return size; |
IanBenzMaxim | 0:33d4e66780c0 | 173 | } |
IanBenzMaxim | 0:33d4e66780c0 | 174 | |
IanBenzMaxim | 0:33d4e66780c0 | 175 | //! Allocates a memory block. (concept Allocator) |
IanBenzMaxim | 0:33d4e66780c0 | 176 | void* Malloc(size_t size) { |
IanBenzMaxim | 0:33d4e66780c0 | 177 | if (!size) |
IanBenzMaxim | 0:33d4e66780c0 | 178 | return NULL; |
IanBenzMaxim | 0:33d4e66780c0 | 179 | |
IanBenzMaxim | 0:33d4e66780c0 | 180 | size = RAPIDJSON_ALIGN(size); |
IanBenzMaxim | 0:33d4e66780c0 | 181 | if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) |
IanBenzMaxim | 0:33d4e66780c0 | 182 | if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) |
IanBenzMaxim | 0:33d4e66780c0 | 183 | return NULL; |
IanBenzMaxim | 0:33d4e66780c0 | 184 | |
IanBenzMaxim | 0:33d4e66780c0 | 185 | void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; |
IanBenzMaxim | 0:33d4e66780c0 | 186 | chunkHead_->size += size; |
IanBenzMaxim | 0:33d4e66780c0 | 187 | return buffer; |
IanBenzMaxim | 0:33d4e66780c0 | 188 | } |
IanBenzMaxim | 0:33d4e66780c0 | 189 | |
IanBenzMaxim | 0:33d4e66780c0 | 190 | //! Resizes a memory block (concept Allocator) |
IanBenzMaxim | 0:33d4e66780c0 | 191 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { |
IanBenzMaxim | 0:33d4e66780c0 | 192 | if (originalPtr == 0) |
IanBenzMaxim | 0:33d4e66780c0 | 193 | return Malloc(newSize); |
IanBenzMaxim | 0:33d4e66780c0 | 194 | |
IanBenzMaxim | 0:33d4e66780c0 | 195 | if (newSize == 0) |
IanBenzMaxim | 0:33d4e66780c0 | 196 | return NULL; |
IanBenzMaxim | 0:33d4e66780c0 | 197 | |
IanBenzMaxim | 0:33d4e66780c0 | 198 | originalSize = RAPIDJSON_ALIGN(originalSize); |
IanBenzMaxim | 0:33d4e66780c0 | 199 | newSize = RAPIDJSON_ALIGN(newSize); |
IanBenzMaxim | 0:33d4e66780c0 | 200 | |
IanBenzMaxim | 0:33d4e66780c0 | 201 | // Do not shrink if new size is smaller than original |
IanBenzMaxim | 0:33d4e66780c0 | 202 | if (originalSize >= newSize) |
IanBenzMaxim | 0:33d4e66780c0 | 203 | return originalPtr; |
IanBenzMaxim | 0:33d4e66780c0 | 204 | |
IanBenzMaxim | 0:33d4e66780c0 | 205 | // Simply expand it if it is the last allocation and there is sufficient space |
IanBenzMaxim | 0:33d4e66780c0 | 206 | if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { |
IanBenzMaxim | 0:33d4e66780c0 | 207 | size_t increment = static_cast<size_t>(newSize - originalSize); |
IanBenzMaxim | 0:33d4e66780c0 | 208 | if (chunkHead_->size + increment <= chunkHead_->capacity) { |
IanBenzMaxim | 0:33d4e66780c0 | 209 | chunkHead_->size += increment; |
IanBenzMaxim | 0:33d4e66780c0 | 210 | return originalPtr; |
IanBenzMaxim | 0:33d4e66780c0 | 211 | } |
IanBenzMaxim | 0:33d4e66780c0 | 212 | } |
IanBenzMaxim | 0:33d4e66780c0 | 213 | |
IanBenzMaxim | 0:33d4e66780c0 | 214 | // Realloc process: allocate and copy memory, do not free original buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 215 | if (void* newBuffer = Malloc(newSize)) { |
IanBenzMaxim | 0:33d4e66780c0 | 216 | if (originalSize) |
IanBenzMaxim | 0:33d4e66780c0 | 217 | std::memcpy(newBuffer, originalPtr, originalSize); |
IanBenzMaxim | 0:33d4e66780c0 | 218 | return newBuffer; |
IanBenzMaxim | 0:33d4e66780c0 | 219 | } |
IanBenzMaxim | 0:33d4e66780c0 | 220 | else |
IanBenzMaxim | 0:33d4e66780c0 | 221 | return NULL; |
IanBenzMaxim | 0:33d4e66780c0 | 222 | } |
IanBenzMaxim | 0:33d4e66780c0 | 223 | |
IanBenzMaxim | 0:33d4e66780c0 | 224 | //! Frees a memory block (concept Allocator) |
IanBenzMaxim | 0:33d4e66780c0 | 225 | static void Free(void *ptr) { (void)ptr; } // Do nothing |
IanBenzMaxim | 0:33d4e66780c0 | 226 | |
IanBenzMaxim | 0:33d4e66780c0 | 227 | private: |
IanBenzMaxim | 0:33d4e66780c0 | 228 | //! Copy constructor is not permitted. |
IanBenzMaxim | 0:33d4e66780c0 | 229 | MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; |
IanBenzMaxim | 0:33d4e66780c0 | 230 | //! Copy assignment operator is not permitted. |
IanBenzMaxim | 0:33d4e66780c0 | 231 | MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; |
IanBenzMaxim | 0:33d4e66780c0 | 232 | |
IanBenzMaxim | 0:33d4e66780c0 | 233 | //! Creates a new chunk. |
IanBenzMaxim | 0:33d4e66780c0 | 234 | /*! \param capacity Capacity of the chunk in bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 235 | \return true if success. |
IanBenzMaxim | 0:33d4e66780c0 | 236 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 237 | bool AddChunk(size_t capacity) { |
IanBenzMaxim | 0:33d4e66780c0 | 238 | if (!baseAllocator_) |
IanBenzMaxim | 0:33d4e66780c0 | 239 | ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); |
IanBenzMaxim | 0:33d4e66780c0 | 240 | if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { |
IanBenzMaxim | 0:33d4e66780c0 | 241 | chunk->capacity = capacity; |
IanBenzMaxim | 0:33d4e66780c0 | 242 | chunk->size = 0; |
IanBenzMaxim | 0:33d4e66780c0 | 243 | chunk->next = chunkHead_; |
IanBenzMaxim | 0:33d4e66780c0 | 244 | chunkHead_ = chunk; |
IanBenzMaxim | 0:33d4e66780c0 | 245 | return true; |
IanBenzMaxim | 0:33d4e66780c0 | 246 | } |
IanBenzMaxim | 0:33d4e66780c0 | 247 | else |
IanBenzMaxim | 0:33d4e66780c0 | 248 | return false; |
IanBenzMaxim | 0:33d4e66780c0 | 249 | } |
IanBenzMaxim | 0:33d4e66780c0 | 250 | |
IanBenzMaxim | 0:33d4e66780c0 | 251 | static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. |
IanBenzMaxim | 0:33d4e66780c0 | 252 | |
IanBenzMaxim | 0:33d4e66780c0 | 253 | //! Chunk header for perpending to each chunk. |
IanBenzMaxim | 0:33d4e66780c0 | 254 | /*! Chunks are stored as a singly linked list. |
IanBenzMaxim | 0:33d4e66780c0 | 255 | */ |
IanBenzMaxim | 0:33d4e66780c0 | 256 | struct ChunkHeader { |
IanBenzMaxim | 0:33d4e66780c0 | 257 | size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). |
IanBenzMaxim | 0:33d4e66780c0 | 258 | size_t size; //!< Current size of allocated memory in bytes. |
IanBenzMaxim | 0:33d4e66780c0 | 259 | ChunkHeader *next; //!< Next chunk in the linked list. |
IanBenzMaxim | 0:33d4e66780c0 | 260 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 261 | |
IanBenzMaxim | 0:33d4e66780c0 | 262 | ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. |
IanBenzMaxim | 0:33d4e66780c0 | 263 | size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. |
IanBenzMaxim | 0:33d4e66780c0 | 264 | void *userBuffer_; //!< User supplied buffer. |
IanBenzMaxim | 0:33d4e66780c0 | 265 | BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. |
IanBenzMaxim | 0:33d4e66780c0 | 266 | BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. |
IanBenzMaxim | 0:33d4e66780c0 | 267 | }; |
IanBenzMaxim | 0:33d4e66780c0 | 268 | |
IanBenzMaxim | 0:33d4e66780c0 | 269 | RAPIDJSON_NAMESPACE_END |
IanBenzMaxim | 0:33d4e66780c0 | 270 | |
IanBenzMaxim | 0:33d4e66780c0 | 271 | #endif // RAPIDJSON_ENCODINGS_H_ |