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

View File

@ -23,6 +23,7 @@ public:
LPlanesPool();
private:
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;
};