Refactor PoolAllocator and add SingleFrameAllocator
This commit is contained in:
parent
1a26241fa8
commit
92460791e6
|
@ -24,19 +24,19 @@
|
|||
********************************************************************************/
|
||||
|
||||
// Libraries
|
||||
#include "MemoryAllocator.h"
|
||||
#include "PoolAllocator.h"
|
||||
#include <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
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<mNbCurrentMemoryBlocks; i++) {
|
||||
|
@ -96,7 +96,7 @@ MemoryAllocator::~MemoryAllocator() {
|
|||
|
||||
// Allocate memory of a given size (in bytes) and return a pointer to the
|
||||
// allocated memory.
|
||||
void* MemoryAllocator::allocate(size_t size) {
|
||||
void* PoolAllocator::allocate(size_t size) {
|
||||
|
||||
// We cannot allocate zero bytes
|
||||
if (size == 0) return nullptr;
|
||||
|
@ -164,7 +164,7 @@ void* MemoryAllocator::allocate(size_t size) {
|
|||
}
|
||||
|
||||
// Release previously allocated memory.
|
||||
void MemoryAllocator::release(void* pointer, size_t size) {
|
||||
void PoolAllocator::release(void* pointer, size_t size) {
|
||||
|
||||
// Cannot release a 0-byte allocated memory
|
||||
if (size == 0) return;
|
|
@ -23,8 +23,8 @@
|
|||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef REACTPHYSICS3D_MEMORY_ALLOCATOR_H
|
||||
#define REACTPHYSICS3D_MEMORY_ALLOCATOR_H
|
||||
#ifndef REACTPHYSICS3D_POOL_ALLOCATOR_H
|
||||
#define REACTPHYSICS3D_POOL_ALLOCATOR_H
|
||||
|
||||
// Libraries
|
||||
#include <cstring>
|
||||
|
@ -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.
|
80
src/memory/SingleFrameAllocator.cpp
Normal file
80
src/memory/SingleFrameAllocator.cpp
Normal file
|
@ -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 <cstdlib>
|
||||
#include <cassert>
|
||||
|
||||
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<char*>(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<size_t>(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;
|
||||
}
|
76
src/memory/SingleFrameAllocator.h
Normal file
76
src/memory/SingleFrameAllocator.h
Normal file
|
@ -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 <cstring>
|
||||
#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
|
Loading…
Reference in New Issue
Block a user