Improved pool

This commit is contained in:
lawnjelly 2019-09-25 07:48:35 +01:00 committed by GitHub
parent 805b6153b6
commit 7f5eb565e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 21 deletions

View File

@ -13,36 +13,29 @@ LPlanesPool::LPlanesPool()
void LPlanesPool::Reset() void LPlanesPool::Reset()
{ {
memset(m_ucTaken, 0, sizeof ( m_ucTaken)); for (int n=0; n<POOL_MAX; n++)
m_uiCount = 0; {
m_ucFreeList[n] = POOL_MAX - n - 1;
}
m_uiNumFree = POOL_MAX;
} }
unsigned int LPlanesPool::Request() unsigned int LPlanesPool::Request()
{ {
// can't do, pool run out if (!m_uiNumFree)
if (m_uiCount >= POOL_MAX)
return -1; return -1;
// could be done more efficiently .. NYI m_uiNumFree--;
for (unsigned int n=0; n<POOL_MAX; n++) return m_ucFreeList[m_uiNumFree];
{
if (m_ucTaken[n] == 0)
{
m_ucTaken[n] = 255;
m_uiCount++;
return n;
}
}
assert (0);
} }
void LPlanesPool::Free(unsigned int ui) void LPlanesPool::Free(unsigned int ui)
{ {
assert (ui <= POOL_MAX); assert (ui <= POOL_MAX);
assert (m_ucTaken[ui]); assert (m_uiNumFree < POOL_MAX);
assert (m_uiCount);
m_ucTaken[ui] = 0; m_ucFreeList[m_uiNumFree] = ui;
m_uiCount--; m_uiNumFree++;
} }

View File

@ -23,6 +23,7 @@ public:
LPlanesPool(); LPlanesPool();
private: private:
LVector<Plane> m_Planes[ POOL_MAX]; LVector<Plane> m_Planes[ POOL_MAX];
unsigned char m_ucTaken[POOL_MAX];
unsigned int m_uiCount; uint8_t m_ucFreeList[POOL_MAX];
uint32_t m_uiNumFree;
}; };