From c72bf4aecaa4d112de9cb2bc6cece999c18d6ee7 Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Fri, 16 Sep 2011 22:41:41 +0000 Subject: [PATCH] Continuation of the MemoryPool class git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@442 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- src/constants.h | 3 +++ src/memory/MemoryPool.h | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/constants.h b/src/constants.h index be9c4fe5..c6489bf7 100644 --- a/src/constants.h +++ b/src/constants.h @@ -52,6 +52,9 @@ const double PENETRATION_FACTOR = 0.2; // Penetration factor (b // penetration depth in order to calculate the correct impulse for the contact const double PERSISTENT_CONTACT_DIST_THRESHOLD = 0.02; // Distance threshold for two contact points for a valid persistent contact +// TODO : Change this number +const int NB_MAX_CONTACTS = 10000; // Maximum number of contacts (for memory pool allocation) + // Constraint solver constants const uint MAX_LCP_ITERATIONS = 10; // Maximum number of iterations when solving a LCP problem const double AV_COUNTER_LIMIT = 500; // Maximum number value of the avBodiesCounter or avConstraintsCounter diff --git a/src/memory/MemoryPool.h b/src/memory/MemoryPool.h index 8d8d34f4..a398cdc5 100644 --- a/src/memory/MemoryPool.h +++ b/src/memory/MemoryPool.h @@ -43,7 +43,7 @@ namespace reactphysics3d { and deallocation. ------------------------------------------------------------------- */ -template // TODO : Check if we need to use a template here +template class MemoryPool { private: @@ -57,15 +57,17 @@ class MemoryPool { struct Unit* pAllocatedLinkedList; // Pointer to the linked list of allocated memory units struct Unit* pFreeLinkedList; // Pointer to the linked list of free memory units size_t memorySize; // Total allocated memory in the pool + const uint maxNbObjects; // Maximum number of objects in the pool uint currentNbObjects; // Current number of objects in the pool - const uint initNbAllocatedObjects; // Number of objects allocated at the beginning (used for reallocation) public: MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc); // Constructor ~MemoryPool(); // Destructor - void* allocateObject(); // Return a pointer to an memory allocated location to store a new object - void freeObject(void* pObjectToFree); // Tell the pool that an object doesn't need to be store in the pool anymore + uint getMaxNbObjects() const; // Return the maximum number of objects allowed in the pool + uint getCurrentNbObjects() const; // Return the current number of objects in the pool + void* allocateObject(); // Return a pointer to an memory allocated location to store a new object + void freeObject(void* pObjectToFree); // Tell the pool that an object doesn't need to be store in the pool anymore }; @@ -74,7 +76,7 @@ class MemoryPool { // a given number of object of the template type T template MemoryPool::MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc) - : initNbAllocatedObjects(nbObjectsToAllocate), currentNbObjects(0) { + : currentNbObjects(0), maxNbObjects(nbObjectsToAllocate) { pMemoryBlock = NULL; pAllocatedLinkedList = NULL; pFreeLinkedList = NULL; @@ -120,12 +122,9 @@ MemoryPool::~MemoryPool() { template void* MemoryPool::allocateObject() { // If no memory unit is available in the current allocated memory block - if (!pFreeLinkedList) { - // TODO : REALLOCATE MEMORY HERE - assert(false); - } - + assert(currentNbObjects < maxNbObjects); assert(pFreeLinkedList); + struct Unit* currentUnit = pFreeLinkedList; pFreeLinkedList = currentUnit->pNext; if (pFreeLinkedList) { @@ -169,7 +168,17 @@ void MemoryPool::freeObject(void* pObjectToFree) { currentNbObjects--; } +// Return the maximum number of objects allowed in the pool +template +uint MemoryPool::getMaxNbObjects() const { + return maxNbObjects; +} +// Return the current number of objects in the pool +template +uint MemoryPool::getCurrentNbObjects() const { + return currentNbObjects; +} }