Fix return to pool negative free indexes

This commit is contained in:
Doyle T 2018-07-05 01:38:35 +10:00
parent 8b2df0d098
commit da4ba37771

30
dqn.h
View File

@ -870,9 +870,33 @@ struct DqnPool
i16 freeIndex; i16 freeIndex;
i16 numFree; i16 numFree;
DqnPool() : freeIndex(0) , numFree(SIZE) { DQN_FOR_EACH(i, SIZE - 1) { Entry *entry = pool + i; entry->nextIndex = i + 1; } Entry *last = pool + (SIZE - 1); last->nextIndex = SENTINEL_INDEX; } DqnPool() : freeIndex(0) , numFree(SIZE)
T *GetNext() { if (freeIndex == SENTINEL_INDEX) return nullptr; Entry *result = pool + freeIndex; freeIndex = result->nextIndex; numFree--; return result; } {
void Return (T *item) { auto *entry = reinterpret_cast<Entry *>(item); entry->nextIndex = freeIndex; freeIndex = pool - entry; numFree++; } DQN_FOR_EACH(i, SIZE - 1)
{
Entry *entry = pool + i;
entry->nextIndex = i + 1;
}
Entry *last = pool + (SIZE - 1);
last->nextIndex = SENTINEL_INDEX;
}
T *GetNext()
{
if (freeIndex == SENTINEL_INDEX) return nullptr;
Entry *result = pool + freeIndex;
freeIndex = result->nextIndex;
numFree--;
return result;
}
void Return(T *item)
{
auto *entry = reinterpret_cast<Entry *>(item);
entry->nextIndex = freeIndex;
freeIndex = entry - pool;
numFree++;
}
}; };
FILE_SCOPE DqnMemAPI DQN_DEFAULT_HEAP_ALLOCATOR_ = DqnMemAPI::HeapAllocator(); FILE_SCOPE DqnMemAPI DQN_DEFAULT_HEAP_ALLOCATOR_ = DqnMemAPI::HeapAllocator();