RapidJSON: add aligned memory allocator class

Added, but not implemented yet. We can revisit this in the future and check if its worth using the aligned memory allocator.
This commit is contained in:
Kawe Mazidjatari 2025-01-13 15:52:27 +01:00
parent de4a3d294c
commit d049b2df1c
2 changed files with 85 additions and 0 deletions

View File

@ -32,6 +32,17 @@
namespace rapidjson { typedef ::std::size_t SizeType; }
#include "rapidjson/rapidjson.h"
#ifdef RAPIDJSON_USE_CUSTOM_ALLOCATOR
// Must be included before the RapidJSON includes
// as this replaces the default allocator. The new
// allocator takes SIMD alignment into account, but
// isn't strictly necessary when using RAPIDJSON_SIMD.
#include "tier2/jsonalloc.h"
#define RAPIDJSON_DEFAULT_ALLOCATOR ::RAPIDJSON_NAMESPACE::MemoryPoolAllocator<JSONAllocator>
#define RAPIDJSON_DEFAULT_STACK_ALLOCATOR ::RAPIDJSON_NAMESPACE::MemoryPoolAllocator<JSONAllocator>
#endif // RAPIDJSON_USE_CUSTOM_ALLOCATOR
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"

View File

@ -0,0 +1,74 @@
//===========================================================================//
//
// Purpose: RapidJSON allocator class
//
//===========================================================================//
#ifndef TIER2_JSONALLOC_H
#define TIER2_JSONALLOC_H
// 16 byte alignment as we only support up to 128 bits SIMD.
#define JSON_SIMD_ALIGNMENT 16
class JSONAllocator
{
public:
static const bool kNeedFree; //!< Whether this allocator needs to call Free().
// Allocate a memory block.
// \param size of the memory block in bytes.
// \returns pointer to the memory block.
void* Malloc(size_t size)
{
if (!size)
return nullptr;
#ifdef RAPIDJSON_SIMD
return _aligned_malloc(AlignValue(size, JSON_SIMD_ALIGNMENT), JSON_SIMD_ALIGNMENT);
#else
return malloc(size);
#endif
}
// Resize a memory block.
// \param originalPtr The pointer to current memory block. Null pointer is permitted.
// \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.)
// \param newSize the new size in bytes.
void* Realloc(void* originalPtr, size_t originalSize, size_t newSize)
{
(void)originalSize;
if (newSize == 0)
{
Free(originalPtr);
return nullptr;
}
#ifdef RAPIDJSON_SIMD
return _aligned_realloc(originalPtr, AlignValue(newSize, JSON_SIMD_ALIGNMENT), JSON_SIMD_ALIGNMENT);
#else
return realloc(originalPtr, newSize);
#endif
}
// Free a memory block.
// \param pointer to the memory block. Null pointer is permitted.
static void Free(void* ptr) noexcept
{
#ifdef RAPIDJSON_SIMD
_aligned_free(ptr);
#else
free(ptr);
#endif
}
bool operator==(const JSONAllocator&) const noexcept
{
return true;
}
bool operator!=(const JSONAllocator&) const noexcept
{
return false;
}
};
#endif // TIER2_JSONALLOC_H