From 92460791e651bf62216a14992c4bfb7da5926952 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Wed, 21 Sep 2016 22:01:14 +0200 Subject: [PATCH] Refactor PoolAllocator and add SingleFrameAllocator --- ...{MemoryAllocator.cpp => PoolAllocator.cpp} | 16 ++-- .../{MemoryAllocator.h => PoolAllocator.h} | 12 +-- src/memory/SingleFrameAllocator.cpp | 80 +++++++++++++++++++ src/memory/SingleFrameAllocator.h | 76 ++++++++++++++++++ 4 files changed, 170 insertions(+), 14 deletions(-) rename src/memory/{MemoryAllocator.cpp => PoolAllocator.cpp} (95%) rename src/memory/{MemoryAllocator.h => PoolAllocator.h} (96%) create mode 100644 src/memory/SingleFrameAllocator.cpp create mode 100644 src/memory/SingleFrameAllocator.h diff --git a/src/memory/MemoryAllocator.cpp b/src/memory/PoolAllocator.cpp similarity index 95% rename from src/memory/MemoryAllocator.cpp rename to src/memory/PoolAllocator.cpp index 46c47e14..dc1306bc 100644 --- a/src/memory/MemoryAllocator.cpp +++ b/src/memory/PoolAllocator.cpp @@ -24,19 +24,19 @@ ********************************************************************************/ // Libraries -#include "MemoryAllocator.h" +#include "PoolAllocator.h" #include #include using namespace reactphysics3d; // Initialization of static variables -bool MemoryAllocator::isMapSizeToHeadIndexInitialized = false; -size_t MemoryAllocator::mUnitSizes[NB_HEAPS]; -int MemoryAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; +bool PoolAllocator::isMapSizeToHeadIndexInitialized = false; +size_t PoolAllocator::mUnitSizes[NB_HEAPS]; +int PoolAllocator::mMapSizeToHeapIndex[MAX_UNIT_SIZE + 1]; // Constructor -MemoryAllocator::MemoryAllocator() { +PoolAllocator::PoolAllocator() { // Allocate some memory to manage the blocks mNbAllocatedMemoryBlocks = 64; @@ -78,7 +78,7 @@ MemoryAllocator::MemoryAllocator() { } // Destructor -MemoryAllocator::~MemoryAllocator() { +PoolAllocator::~PoolAllocator() { // Release the memory allocated for each block for (uint i=0; i @@ -33,14 +33,14 @@ /// ReactPhysics3D namespace namespace reactphysics3d { -// Class MemoryAllocator +// Class PoolAllocator /** * This class is used to efficiently allocate memory on the heap. * It allows us to allocate small blocks of memory (smaller or equal to 1024 bytes) * efficiently. This implementation is inspired by the small block allocator * described here : http://www.codeproject.com/useritems/Small_Block_Allocator.asp */ -class MemoryAllocator { +class PoolAllocator { private : @@ -126,10 +126,10 @@ class MemoryAllocator { // -------------------- Methods -------------------- // /// Constructor - MemoryAllocator(); + PoolAllocator(); /// Destructor - ~MemoryAllocator(); + ~PoolAllocator(); /// Allocate memory of a given size (in bytes) and return a pointer to the /// allocated memory. diff --git a/src/memory/SingleFrameAllocator.cpp b/src/memory/SingleFrameAllocator.cpp new file mode 100644 index 00000000..a2e436cd --- /dev/null +++ b/src/memory/SingleFrameAllocator.cpp @@ -0,0 +1,80 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://www.reactphysics3d.com * +* Copyright (c) 2010-2016 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 "SingleFrameAllocator.h" +#include +#include + +using namespace reactphysics3d; + +// Constructor +SingleFrameAllocator::SingleFrameAllocator(size_t totalSizeBytes) + : mTotalSizeBytes(totalSizeBytes), mCurrentOffset(0) { + + // Allocate a whole block of memory at the beginning + mMemoryBufferStart = static_cast(malloc(mTotalSizeBytes)); + assert(mMemoryBufferStart != nullptr); +} + +// Destructor +SingleFrameAllocator::~SingleFrameAllocator() { + + // Release the memory allocated at the beginning + free(mMemoryBufferStart); +} + + +// Allocate memory of a given size (in bytes) and return a pointer to the +// allocated memory. +void* SingleFrameAllocator::allocate(size_t size) { + + // Check that there is enough remaining memory in the buffer + if (static_cast(mCurrentOffset) + size > mTotalSizeBytes) { + + // This should never occur. If it does, you must increase the initial + // size of memory of this allocator + assert(false); + + // Return null + return nullptr; + } + + // Next available memory location + void* nextAvailableMemory = mMemoryBufferStart + mCurrentOffset; + + // Increment the offset + mCurrentOffset += size; + + // Return the next available memory location + return nextAvailableMemory; +} + +// Reset the marker of the current allocated memory +void SingleFrameAllocator::reset() { + + // Reset the current offset at the beginning of the block + mCurrentOffset = 0; +} diff --git a/src/memory/SingleFrameAllocator.h b/src/memory/SingleFrameAllocator.h new file mode 100644 index 00000000..e3151f0a --- /dev/null +++ b/src/memory/SingleFrameAllocator.h @@ -0,0 +1,76 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://www.reactphysics3d.com * +* Copyright (c) 2010-2016 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. * +* * +********************************************************************************/ + +#ifndef REACTPHYSICS3D_SINGLE_FRAME_ALLOCATOR_H +#define REACTPHYSICS3D_SINGLE_FRAME_ALLOCATOR_H + +// Libraries +#include +#include "configuration.h" + +/// ReactPhysics3D namespace +namespace reactphysics3d { + +// Class SingleFrameAllocator +/** + * This class represent a memory allocator used to efficiently allocate + * memory on the heap that is used during a single frame. + */ +class SingleFrameAllocator { + + private : + + // -------------------- Attributes -------------------- // + + /// Total size (in bytes) of memory of the allocator + size_t mTotalSizeBytes; + + /// Pointer to the beginning of the allocated memory block + char* mMemoryBufferStart; + + /// Pointer to the next available memory location in the buffer + int mCurrentOffset; + + public : + + // -------------------- Methods -------------------- // + + /// Constructor + SingleFrameAllocator(size_t totalSizeBytes); + + /// Destructor + ~SingleFrameAllocator(); + + /// Allocate memory of a given size (in bytes) + void* allocate(size_t size); + + /// Reset the marker of the current allocated memory + void reset(); + +}; + +} + +#endif