Skip to content

Commit

Permalink
Update rapid_json; Supress deprecation warning
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Oct 12, 2024
1 parent d1f7db6 commit aada7dc
Show file tree
Hide file tree
Showing 33 changed files with 5,454 additions and 814 deletions.
38 changes: 24 additions & 14 deletions swCommonLib/External/RapidJSON/include/rapidjson/allocators.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ class MemoryPoolAllocator {

size = RAPIDJSON_ALIGN(size);
if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity)
AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size);
if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size))
return NULL;

void *buffer = reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size;
chunkHead_->size += size;
Expand All @@ -194,26 +195,30 @@ class MemoryPoolAllocator {
if (newSize == 0)
return NULL;

originalSize = RAPIDJSON_ALIGN(originalSize);
newSize = RAPIDJSON_ALIGN(newSize);

// Do not shrink if new size is smaller than original
if (originalSize >= newSize)
return originalPtr;

// Simply expand it if it is the last allocation and there is sufficient space
if (originalPtr == (char *)(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
if (originalPtr == reinterpret_cast<char *>(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) {
size_t increment = static_cast<size_t>(newSize - originalSize);
increment = RAPIDJSON_ALIGN(increment);
if (chunkHead_->size + increment <= chunkHead_->capacity) {
chunkHead_->size += increment;
return originalPtr;
}
}

// Realloc process: allocate and copy memory, do not free original buffer.
void* newBuffer = Malloc(newSize);
RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly.
if (originalSize)
std::memcpy(newBuffer, originalPtr, originalSize);
return newBuffer;
if (void* newBuffer = Malloc(newSize)) {
if (originalSize)
std::memcpy(newBuffer, originalPtr, originalSize);
return newBuffer;
}
else
return NULL;
}

//! Frees a memory block (concept Allocator)
Expand All @@ -227,15 +232,20 @@ class MemoryPoolAllocator {

//! Creates a new chunk.
/*! \param capacity Capacity of the chunk in bytes.
\return true if success.
*/
void AddChunk(size_t capacity) {
bool AddChunk(size_t capacity) {
if (!baseAllocator_)
ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator());
ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity));
chunk->capacity = capacity;
chunk->size = 0;
chunk->next = chunkHead_;
chunkHead_ = chunk;
if (ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) {
chunk->capacity = capacity;
chunk->size = 0;
chunk->next = chunkHead_;
chunkHead_ = chunk;
return true;
}
else
return false;
}

static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity.
Expand Down
Loading

0 comments on commit aada7dc

Please sign in to comment.