Working on memory allocators

This commit is contained in:
Daniel Chappuis 2022-03-25 08:47:45 +01:00
parent b05f12850d
commit 75a64a65b8
7 changed files with 94 additions and 26 deletions

View File

@ -229,6 +229,7 @@ set (REACTPHYSICS3D_SOURCES
"src/memory/SingleFrameAllocator.cpp"
"src/memory/HeapAllocator.cpp"
"src/memory/MemoryManager.cpp"
"src/memory/MemoryAllocator.cpp"
"src/utils/Profiler.cpp"
"src/utils/DefaultLogger.cpp"
"src/utils/DebugRenderer.cpp"

View File

@ -57,7 +57,7 @@ class DefaultAllocator : public MemoryAllocator {
virtual void* allocate(size_t size) override {
// If compiler is Visual Studio
#ifdef _MSC_VER
#ifdef RP3D_COMPILER_VISUAL_STUDIO
// Visual Studio doesn't not support standard std:aligned_alloc() method from c++ 17
return _alligned_malloc(size, GLOBAL_ALIGNMENT);
@ -72,7 +72,7 @@ class DefaultAllocator : public MemoryAllocator {
virtual void release(void* pointer, size_t /*size*/) override {
// If compiler is Visual Studio
#ifdef _MSC_VER
#ifdef RP3D_COMPILER_VISUAL_STUDIO
// Visual Studio doesn't not support standard std:aligned_alloc() method from c++ 17
return _aligned_free(GLOBAL_ALIGNMENT, pointer);

View File

@ -28,6 +28,7 @@
// Libraries
#include <cstring>
#include <reactphysics3d/configuration.h>
/// ReactPhysics3D namespace
namespace reactphysics3d {
@ -55,6 +56,14 @@ class MemoryAllocator {
/// Release previously allocated memory.
virtual void release(void* pointer, size_t size)=0;
/// Given a pointer to memory, this method returns the next aligned address
// TODO : We need to use uint8 type instead of uint8_t here
static void* alignAddress(void* pointer, std::uint8_t alignment);
/// Given a pointer to memory, this method returns the next aligned address and also output the alignment offset
// TODO : We need to use uint8 type instead of uint8_t here
static void* alignAddress(void* pointer, std::uint8_t alignment, ptrdiff_t& alignmentOffset);
};
}

View File

@ -35,7 +35,7 @@ using namespace reactphysics3d;
// Constructor
TransformComponents::TransformComponents(MemoryAllocator& allocator)
:Components(allocator, sizeof(Entity) + sizeof(Transform)) {
:Components(allocator, sizeof(Entity) + sizeof(Transform) + 2 * GLOBAL_ALIGNMENT) {
// Allocate memory for the components data
allocate(INIT_NB_ALLOCATED_COMPONENTS);
@ -55,7 +55,9 @@ void TransformComponents::allocate(uint32 nbComponentsToAllocate) {
// New pointers to components data
Entity* newEntities = static_cast<Entity*>(newBuffer);
//assert(reinterpret_cast<uintptr_t>(newEntities) % GLOBAL_ALIGNMENT == 0);
Transform* newTransforms = reinterpret_cast<Transform*>(newEntities + nbComponentsToAllocate);
//assert(reinterpret_cast<uintptr_t>(newTransforms) % GLOBAL_ALIGNMENT == 0);
// If there was already components before
if (mNbComponents > 0) {

View File

@ -194,24 +194,20 @@ void* HeapAllocator::computeAlignedAddress(void* unalignedAddress) {
// Take care of alignment to make sure that we always return an address to the
// enforce the global alignment of the library
const uintptr_t currentAdress = reinterpret_cast<uintptr_t>(unalignedAddress);
// Calculate the adjustment by masking off the lower bits of the address, to determine how "misaligned" it is.
size_t mask = GLOBAL_ALIGNMENT - 1;
uintptr_t misalignment = currentAdress & mask;
ptrdiff_t alignmentOffset = GLOBAL_ALIGNMENT - misalignment;
// Compute the aligned address
uintptr_t alignedAddress = currentAdress + alignmentOffset;
ptrdiff_t alignmentOffset;
void* alignedPointer = alignAddress(unalignedAddress, GLOBAL_ALIGNMENT, alignmentOffset);
uintptr_t alignedAddress = reinterpret_cast<uintptr_t>(alignedPointer);
// Store the adjustment in the byte immediately preceding the adjusted address.
// This way we can find again the original allocated memory address returned by malloc
// when this memory unit is released.
assert(alignmentOffset < 256);
assert(alignmentOffset <= GLOBAL_ALIGNMENT);
uint8* pAlignedMemory = reinterpret_cast<uint8*>(alignedAddress);
pAlignedMemory[-1] = static_cast<uint8>(alignmentOffset);
return reinterpret_cast<void*>(alignedAddress);
return alignedPointer;
}
// Release previously allocated memory.

View File

@ -0,0 +1,71 @@
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2022 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include <reactphysics3d/memory/MemoryAllocator.h>
#include <cassert>
using namespace reactphysics3d;
/// Given a pointer to memory, this method returns the next aligned address
/**
* @param pointer Pointer to a memory location
* @param alignment Desired alignment
* @return Pointer to the next aligned memory location
*/
void* MemoryAllocator::alignAddress(void* pointer, std::uint8_t alignment) {
ptrdiff_t alignmentOffset;
return alignAddress(pointer, alignment, alignmentOffset);
}
/// Given a pointer to memory, this method returns the next aligned address and also output the alignment offset
/**
* @param pointer Pointer to a memory location
* @param alignment Desired alignment
* @param outAlignmentOffset Output alignment offset needed to align the initial pointer
* @return Pointer to the next aligned memory location
*/
void* MemoryAllocator::alignAddress(void* pointer, std::uint8_t alignment, ptrdiff_t& outAlignmentOffset) {
// Take care of alignment to make sure that we always return an address to the
// enforce the global alignment of the library
const uintptr_t currentAdress = reinterpret_cast<uintptr_t>(pointer);
// Calculate the adjustment by masking off the lower bits of the address, to determine how "misaligned" it is.
const size_t mask = alignment - 1;
const uintptr_t misalignment = currentAdress & mask;
outAlignmentOffset = alignment - misalignment;
assert(outAlignmentOffset <= alignment);
// Compute the aligned address
const uintptr_t alignedAdress = currentAdress + outAlignmentOffset;
void* alignedPointer = reinterpret_cast<void*>(alignedAdress);
// Check that allocated memory is correctly aligned
assert(reinterpret_cast<uintptr_t>(alignedPointer) % alignment == 0);
return alignedPointer;
}

View File

@ -77,19 +77,8 @@ void* SingleFrameAllocator::allocate(size_t size) {
// Next available memory location
void* nextAvailableMemory = mMemoryBufferStart + mCurrentOffset;
// Take care of alignment to make sure that we always return an address to the
// enforce the global alignment of the library
uintptr_t currentAdress = reinterpret_cast<uintptr_t>(nextAvailableMemory);
// Calculate the adjustment by masking off the lower bits of the address, to determine how "misaligned" it is.
const size_t mask = GLOBAL_ALIGNMENT - 1;
const uintptr_t misalignment = currentAdress & mask;
const ptrdiff_t alignmentOffset = GLOBAL_ALIGNMENT - misalignment;
assert(alignmentOffset <= GLOBAL_ALIGNMENT);
// Compute the aligned address
const uintptr_t alignedAdress = currentAdress + alignmentOffset;
nextAvailableMemory = reinterpret_cast<void*>(alignedAdress);
// Compute the next aligned memory address
nextAvailableMemory = alignAddress(nextAvailableMemory, GLOBAL_ALIGNMENT);
// Increment the offset
mCurrentOffset += totalSize;