MCPcopy Create free account
hub / github.com/USCiLab/cereal / Realloc

Method Realloc

include/cereal/external/rapidjson/allocators.h:204–235  ·  view source on GitHub ↗

Resizes a memory block (concept Allocator)

Source from the content-addressed store, hash-verified

202
203 //! Resizes a memory block (concept Allocator)
204 void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) {
205 if (originalPtr == 0)
206 return Malloc(newSize);
207
208 if (newSize == 0)
209 return NULL;
210
211 originalSize = CEREAL_RAPIDJSON_ALIGN(originalSize);
212 newSize = CEREAL_RAPIDJSON_ALIGN(newSize);
213
214 // Do not shrink if new size is smaller than original
215 if (originalSize >= newSize)
216 return originalPtr;
217
218 // Simply expand it if it is the last allocation and there is sufficient space
219 if (originalPtr == reinterpret_cast<char *>(chunkHead_) + CEREAL_RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
220 size_t increment = static_cast<size_t>(newSize - originalSize);
221 if (chunkHead_->size + increment <= chunkHead_->capacity) {
222 chunkHead_->size += increment;
223 return originalPtr;
224 }
225 }
226
227 // Realloc process: allocate and copy memory, do not free original buffer.
228 if (void* newBuffer = Malloc(newSize)) {
229 if (originalSize)
230 std::memcpy(newBuffer, originalPtr, originalSize);
231 return newBuffer;
232 }
233 else
234 return NULL;
235 }
236
237 //! Frees a memory block (concept Allocator)
238 static void Free(void *ptr) { (void)ptr; } // Do nothing

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected