2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
#ifndef B3_RESIZABLE_POOL_H
|
|
|
|
#define B3_RESIZABLE_POOL_H
|
|
|
|
|
|
|
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
enum
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
B3_POOL_HANDLE_TERMINAL_FREE = -1,
|
|
|
|
B3_POOL_HANDLE_TERMINAL_USED = -2
|
2017-08-01 20:30:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename U>
|
|
|
|
struct b3PoolBodyHandle : public U
|
|
|
|
{
|
|
|
|
B3_DECLARE_ALIGNED_ALLOCATOR();
|
|
|
|
|
|
|
|
int m_nextFreeHandle;
|
|
|
|
void setNextFree(int next)
|
|
|
|
{
|
|
|
|
m_nextFreeHandle = next;
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
int getNextFree() const
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
return m_nextFreeHandle;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
template <typename T>
|
2017-08-01 20:30:58 +08:00
|
|
|
class b3ResizablePool
|
|
|
|
{
|
|
|
|
protected:
|
2019-01-03 21:26:51 +08:00
|
|
|
b3AlignedObjectArray<T> m_bodyHandles;
|
|
|
|
int m_numUsedHandles; // number of active handles
|
|
|
|
int m_firstFreeHandle; // free handles list
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
T* getHandleInternal(int handle)
|
|
|
|
{
|
|
|
|
return &m_bodyHandles[handle];
|
|
|
|
}
|
|
|
|
const T* getHandleInternal(int handle) const
|
|
|
|
{
|
|
|
|
return &m_bodyHandles[handle];
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
b3ResizablePool()
|
|
|
|
{
|
|
|
|
initHandles();
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
|
2017-08-01 20:30:58 +08:00
|
|
|
virtual ~b3ResizablePool()
|
|
|
|
{
|
|
|
|
exitHandles();
|
|
|
|
}
|
2019-01-03 21:26:51 +08:00
|
|
|
///handle management
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
int getNumHandles() const
|
|
|
|
{
|
|
|
|
return m_bodyHandles.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void getUsedHandles(b3AlignedObjectArray<int>& usedHandles) const
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
for (int i = 0; i < m_bodyHandles.size(); i++)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
if (m_bodyHandles[i].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
usedHandles.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* getHandle(int handle)
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(handle >= 0);
|
|
|
|
b3Assert(handle < m_bodyHandles.size());
|
|
|
|
if ((handle < 0) || (handle >= m_bodyHandles.size()))
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
return &m_bodyHandles[handle];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const T* getHandle(int handle) const
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(handle >= 0);
|
|
|
|
b3Assert(handle < m_bodyHandles.size());
|
|
|
|
if ((handle < 0) || (handle >= m_bodyHandles.size()))
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
return &m_bodyHandles[handle];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void increaseHandleCapacity(int extraCapacity)
|
|
|
|
{
|
|
|
|
int curCapacity = m_bodyHandles.size();
|
|
|
|
//b3Assert(curCapacity == m_numUsedHandles);
|
|
|
|
int newCapacity = curCapacity + extraCapacity;
|
|
|
|
m_bodyHandles.resize(newCapacity);
|
|
|
|
|
|
|
|
{
|
|
|
|
for (int i = curCapacity; i < newCapacity; i++)
|
|
|
|
m_bodyHandles[i].setNextFree(i + 1);
|
|
|
|
|
|
|
|
m_bodyHandles[newCapacity - 1].setNextFree(-1);
|
|
|
|
}
|
|
|
|
m_firstFreeHandle = curCapacity;
|
|
|
|
}
|
|
|
|
void initHandles()
|
|
|
|
{
|
|
|
|
m_numUsedHandles = 0;
|
|
|
|
m_firstFreeHandle = -1;
|
|
|
|
|
|
|
|
increaseHandleCapacity(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void exitHandles()
|
|
|
|
{
|
|
|
|
m_bodyHandles.resize(0);
|
|
|
|
m_firstFreeHandle = -1;
|
|
|
|
m_numUsedHandles = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int allocHandle()
|
|
|
|
{
|
2019-01-03 21:26:51 +08:00
|
|
|
b3Assert(m_firstFreeHandle >= 0);
|
2017-08-01 20:30:58 +08:00
|
|
|
|
|
|
|
int handle = m_firstFreeHandle;
|
|
|
|
m_firstFreeHandle = getHandleInternal(handle)->getNextFree();
|
|
|
|
m_numUsedHandles++;
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (m_firstFreeHandle < 0)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
//int curCapacity = m_bodyHandles.size();
|
2019-01-03 21:26:51 +08:00
|
|
|
int additionalCapacity = m_bodyHandles.size();
|
2017-08-01 20:30:58 +08:00
|
|
|
increaseHandleCapacity(additionalCapacity);
|
|
|
|
|
|
|
|
getHandleInternal(handle)->setNextFree(m_firstFreeHandle);
|
|
|
|
}
|
|
|
|
getHandleInternal(handle)->setNextFree(B3_POOL_HANDLE_TERMINAL_USED);
|
|
|
|
getHandleInternal(handle)->clear();
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeHandle(int handle)
|
|
|
|
{
|
|
|
|
b3Assert(handle >= 0);
|
|
|
|
|
2019-01-03 21:26:51 +08:00
|
|
|
if (m_bodyHandles[handle].getNextFree() == B3_POOL_HANDLE_TERMINAL_USED)
|
2017-08-01 20:30:58 +08:00
|
|
|
{
|
|
|
|
getHandleInternal(handle)->clear();
|
|
|
|
getHandleInternal(handle)->setNextFree(m_firstFreeHandle);
|
|
|
|
m_firstFreeHandle = handle;
|
|
|
|
m_numUsedHandles--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-01-03 21:26:51 +08:00
|
|
|
///end handle management
|
|
|
|
|
|
|
|
#endif //B3_RESIZABLE_POOL_H
|