Working on memory allocators
This commit is contained in:
parent
b05f12850d
commit
75a64a65b8
|
@ -229,6 +229,7 @@ set (REACTPHYSICS3D_SOURCES
|
||||||
"src/memory/SingleFrameAllocator.cpp"
|
"src/memory/SingleFrameAllocator.cpp"
|
||||||
"src/memory/HeapAllocator.cpp"
|
"src/memory/HeapAllocator.cpp"
|
||||||
"src/memory/MemoryManager.cpp"
|
"src/memory/MemoryManager.cpp"
|
||||||
|
"src/memory/MemoryAllocator.cpp"
|
||||||
"src/utils/Profiler.cpp"
|
"src/utils/Profiler.cpp"
|
||||||
"src/utils/DefaultLogger.cpp"
|
"src/utils/DefaultLogger.cpp"
|
||||||
"src/utils/DebugRenderer.cpp"
|
"src/utils/DebugRenderer.cpp"
|
||||||
|
|
|
@ -57,7 +57,7 @@ class DefaultAllocator : public MemoryAllocator {
|
||||||
virtual void* allocate(size_t size) override {
|
virtual void* allocate(size_t size) override {
|
||||||
|
|
||||||
// If compiler is Visual Studio
|
// 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
|
// Visual Studio doesn't not support standard std:aligned_alloc() method from c++ 17
|
||||||
return _alligned_malloc(size, GLOBAL_ALIGNMENT);
|
return _alligned_malloc(size, GLOBAL_ALIGNMENT);
|
||||||
|
@ -72,7 +72,7 @@ class DefaultAllocator : public MemoryAllocator {
|
||||||
virtual void release(void* pointer, size_t /*size*/) override {
|
virtual void release(void* pointer, size_t /*size*/) override {
|
||||||
|
|
||||||
// If compiler is Visual Studio
|
// 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
|
// Visual Studio doesn't not support standard std:aligned_alloc() method from c++ 17
|
||||||
return _aligned_free(GLOBAL_ALIGNMENT, pointer);
|
return _aligned_free(GLOBAL_ALIGNMENT, pointer);
|
||||||
|
|
|
@ -28,6 +28,7 @@
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <reactphysics3d/configuration.h>
|
||||||
|
|
||||||
/// ReactPhysics3D namespace
|
/// ReactPhysics3D namespace
|
||||||
namespace reactphysics3d {
|
namespace reactphysics3d {
|
||||||
|
@ -55,6 +56,14 @@ class MemoryAllocator {
|
||||||
|
|
||||||
/// Release previously allocated memory.
|
/// Release previously allocated memory.
|
||||||
virtual void release(void* pointer, size_t size)=0;
|
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);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ using namespace reactphysics3d;
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
TransformComponents::TransformComponents(MemoryAllocator& allocator)
|
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 memory for the components data
|
||||||
allocate(INIT_NB_ALLOCATED_COMPONENTS);
|
allocate(INIT_NB_ALLOCATED_COMPONENTS);
|
||||||
|
@ -55,7 +55,9 @@ void TransformComponents::allocate(uint32 nbComponentsToAllocate) {
|
||||||
|
|
||||||
// New pointers to components data
|
// New pointers to components data
|
||||||
Entity* newEntities = static_cast<Entity*>(newBuffer);
|
Entity* newEntities = static_cast<Entity*>(newBuffer);
|
||||||
|
//assert(reinterpret_cast<uintptr_t>(newEntities) % GLOBAL_ALIGNMENT == 0);
|
||||||
Transform* newTransforms = reinterpret_cast<Transform*>(newEntities + nbComponentsToAllocate);
|
Transform* newTransforms = reinterpret_cast<Transform*>(newEntities + nbComponentsToAllocate);
|
||||||
|
//assert(reinterpret_cast<uintptr_t>(newTransforms) % GLOBAL_ALIGNMENT == 0);
|
||||||
|
|
||||||
// If there was already components before
|
// If there was already components before
|
||||||
if (mNbComponents > 0) {
|
if (mNbComponents > 0) {
|
||||||
|
|
|
@ -194,24 +194,20 @@ void* HeapAllocator::computeAlignedAddress(void* unalignedAddress) {
|
||||||
// Take care of alignment to make sure that we always return an address to the
|
// Take care of alignment to make sure that we always return an address to the
|
||||||
// enforce the global alignment of the library
|
// 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
|
// 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.
|
// 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
|
// This way we can find again the original allocated memory address returned by malloc
|
||||||
// when this memory unit is released.
|
// when this memory unit is released.
|
||||||
assert(alignmentOffset < 256);
|
assert(alignmentOffset <= GLOBAL_ALIGNMENT);
|
||||||
uint8* pAlignedMemory = reinterpret_cast<uint8*>(alignedAddress);
|
uint8* pAlignedMemory = reinterpret_cast<uint8*>(alignedAddress);
|
||||||
pAlignedMemory[-1] = static_cast<uint8>(alignmentOffset);
|
pAlignedMemory[-1] = static_cast<uint8>(alignmentOffset);
|
||||||
|
|
||||||
return reinterpret_cast<void*>(alignedAddress);
|
return alignedPointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release previously allocated memory.
|
// Release previously allocated memory.
|
||||||
|
|
71
src/memory/MemoryAllocator.cpp
Normal file
71
src/memory/MemoryAllocator.cpp
Normal 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;
|
||||||
|
}
|
|
@ -77,19 +77,8 @@ void* SingleFrameAllocator::allocate(size_t size) {
|
||||||
// Next available memory location
|
// Next available memory location
|
||||||
void* nextAvailableMemory = mMemoryBufferStart + mCurrentOffset;
|
void* nextAvailableMemory = mMemoryBufferStart + mCurrentOffset;
|
||||||
|
|
||||||
// Take care of alignment to make sure that we always return an address to the
|
// Compute the next aligned memory address
|
||||||
// enforce the global alignment of the library
|
nextAvailableMemory = alignAddress(nextAvailableMemory, GLOBAL_ALIGNMENT);
|
||||||
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);
|
|
||||||
|
|
||||||
// Increment the offset
|
// Increment the offset
|
||||||
mCurrentOffset += totalSize;
|
mCurrentOffset += totalSize;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user