Fix mem leak in hash, add reset tail for allocator

This commit is contained in:
Doyle Thai 2018-02-05 17:42:27 +11:00
parent 701140287f
commit b0217483db

30
dqn.h
View File

@ -485,7 +485,9 @@ struct DqnMemStack
bool FreeLastBlock (); bool FreeLastBlock ();
// Reverts the stack and its usage back to the first block // Reverts the stack and its usage back to the first block
void Reset(); void Reset ();
void ResetTail ();
// Reset the current memory block usage to 0. // Reset the current memory block usage to 0.
void ClearCurrBlock(bool zeroClear); void ClearCurrBlock(bool zeroClear);
@ -633,7 +635,7 @@ template <typename T>
bool DqnArray<T>::Grow(isize multiplier) bool DqnArray<T>::Grow(isize multiplier)
{ {
isize newMax = this->max * multiplier; isize newMax = this->max * multiplier;
newMax = (newMax == 0) ? 8 : newMax; newMax = (newMax < 8) ? 8 : newMax;
bool result = this->Resize(newMax); bool result = this->Resize(newMax);
return result; return result;
} }
@ -659,7 +661,7 @@ T *DqnArray<T>::Make()
template <typename T> template <typename T>
T *DqnArray<T>::Push(T const *item, isize num) T *DqnArray<T>::Push(T const *item, isize num)
{ {
if (!this->data || (this->count + num) >= this->max) if (!this->data || (this->count + num) > this->max)
{ {
if (!this->Grow()) if (!this->Grow())
{ {
@ -1512,7 +1514,14 @@ typename DqnHashTable<T>::Entry *DqnHashTable<T>::Make(char const *const key, i3
this->usedEntries[indexToEndAt] = hashIndex; this->usedEntries[indexToEndAt] = hashIndex;
} }
// TODO(doyle): Is this a robust check? If we retrieve from the freeEntryList, the memAPI
// may already be initialised.
if (!newEntry->key.memAPI)
{
newEntry->key.InitSize(keyLen, this->memAPI); newEntry->key.InitSize(keyLen, this->memAPI);
DQN_ASSERT(newEntry->key.memAPI == this->memAPI);
}
newEntry->key.Append(key, keyLen); newEntry->key.Append(key, keyLen);
newEntry->next = this->entries[hashIndex]; newEntry->next = this->entries[hashIndex];
this->entries[hashIndex] = newEntry; this->entries[hashIndex] = newEntry;
@ -3867,6 +3876,19 @@ bool DqnMemStack::FreeMemBlock(DqnMemStack::Block *memBlock)
return false; return false;
} }
void DqnMemStack::ResetTail()
{
u8 *start = this->block->tail;
u8 *end = this->block->memory + this->block->size;
if (Dqn_BitIsSet(this->flags, Flag::BoundsGuard))
{
DqnMemStackInternal_KillMetadataPtrsExistingIn(&this->metadata, start, end);
}
this->block->tail = end;
}
void DqnMemStack::Reset() void DqnMemStack::Reset()
{ {
while(this->block && this->block->prevBlock) while(this->block && this->block->prevBlock)
@ -5236,7 +5258,7 @@ DQN_FILE_SCOPE bool DqnStr_EndsWith(char const *src, i32 srcLen, char const *fin
if (srcLen < findLen) if (srcLen < findLen)
return false; return false;
char const *srcEnd = src + (srcLen - 1); char const *srcEnd = src + (srcLen);
char const *checkSrcFrom = srcEnd - findLen; char const *checkSrcFrom = srcEnd - findLen;
bool result = (DqnStr_Cmp(checkSrcFrom, find, findLen, ignoreCase) == 0); bool result = (DqnStr_Cmp(checkSrcFrom, find, findLen, ignoreCase) == 0);