Continuation of the MemoryPool class

git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@442 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2011-09-16 22:41:41 +00:00
parent 0c7269d5ba
commit c72bf4aeca
2 changed files with 22 additions and 10 deletions

View File

@ -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 // 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 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 // Constraint solver constants
const uint MAX_LCP_ITERATIONS = 10; // Maximum number of iterations when solving a LCP problem 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 const double AV_COUNTER_LIMIT = 500; // Maximum number value of the avBodiesCounter or avConstraintsCounter

View File

@ -43,7 +43,7 @@ namespace reactphysics3d {
and deallocation. and deallocation.
------------------------------------------------------------------- -------------------------------------------------------------------
*/ */
template<class T> // TODO : Check if we need to use a template here template<class T>
class MemoryPool { class MemoryPool {
private: private:
@ -57,15 +57,17 @@ class MemoryPool {
struct Unit* pAllocatedLinkedList; // Pointer to the linked list of allocated memory units struct Unit* pAllocatedLinkedList; // Pointer to the linked list of allocated memory units
struct Unit* pFreeLinkedList; // Pointer to the linked list of free memory units struct Unit* pFreeLinkedList; // Pointer to the linked list of free memory units
size_t memorySize; // Total allocated memory in the pool 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 uint currentNbObjects; // Current number of objects in the pool
const uint initNbAllocatedObjects; // Number of objects allocated at the beginning (used for reallocation)
public: public:
MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc); // Constructor MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc); // Constructor
~MemoryPool(); // Destructor ~MemoryPool(); // Destructor
void* allocateObject(); // Return a pointer to an memory allocated location to store a new object uint getMaxNbObjects() const; // Return the maximum number of objects allowed in the pool
void freeObject(void* pObjectToFree); // Tell the pool that an object doesn't need to be store in the pool anymore 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 // a given number of object of the template type T
template<class T> template<class T>
MemoryPool<T>::MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc) MemoryPool<T>::MemoryPool(uint nbObjectsToAllocate) throw(std::bad_alloc)
: initNbAllocatedObjects(nbObjectsToAllocate), currentNbObjects(0) { : currentNbObjects(0), maxNbObjects(nbObjectsToAllocate) {
pMemoryBlock = NULL; pMemoryBlock = NULL;
pAllocatedLinkedList = NULL; pAllocatedLinkedList = NULL;
pFreeLinkedList = NULL; pFreeLinkedList = NULL;
@ -120,12 +122,9 @@ MemoryPool<T>::~MemoryPool() {
template<class T> template<class T>
void* MemoryPool<T>::allocateObject() { void* MemoryPool<T>::allocateObject() {
// If no memory unit is available in the current allocated memory block // If no memory unit is available in the current allocated memory block
if (!pFreeLinkedList) { assert(currentNbObjects < maxNbObjects);
// TODO : REALLOCATE MEMORY HERE
assert(false);
}
assert(pFreeLinkedList); assert(pFreeLinkedList);
struct Unit* currentUnit = pFreeLinkedList; struct Unit* currentUnit = pFreeLinkedList;
pFreeLinkedList = currentUnit->pNext; pFreeLinkedList = currentUnit->pNext;
if (pFreeLinkedList) { if (pFreeLinkedList) {
@ -169,7 +168,17 @@ void MemoryPool<T>::freeObject(void* pObjectToFree) {
currentNbObjects--; currentNbObjects--;
} }
// Return the maximum number of objects allowed in the pool
template<class T>
uint MemoryPool<T>::getMaxNbObjects() const {
return maxNbObjects;
}
// Return the current number of objects in the pool
template<class T>
uint MemoryPool<T>::getCurrentNbObjects() const {
return currentNbObjects;
}
} }