1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 20:42:21 +02:00

Changed to single alloc

This commit is contained in:
Brian Fiete 2020-02-13 07:53:32 -08:00
parent 2d3518dcd4
commit 468b5e292d

View file

@ -256,11 +256,13 @@ private:
void Resize(intptr newSize, bool forceNewHashCodes) void Resize(intptr newSize, bool forceNewHashCodes)
{ {
BF_ASSERT(newSize >= mAllocSize); BF_ASSERT(newSize >= mAllocSize);
int_cosize* newBuckets = (int_cosize*)this->rawAllocate(sizeof(int_cosize) * newSize); Entry* newEntries;
int_cosize* newBuckets;
AllocData(newSize, newEntries, newBuckets);
for (int_cosize i = 0; i < newSize; i++) for (int_cosize i = 0; i < newSize; i++)
newBuckets[i] = -1; newBuckets[i] = -1;
Entry* newEntries = (Entry*)this->rawAllocate(sizeof(Entry)*newSize);
for (int i = 0; i < mCount; i++) for (int i = 0; i < mCount; i++)
{ {
@ -294,8 +296,7 @@ private:
} }
} }
this->rawDeallocate(mBuckets); DeleteData();
this->rawDeallocate(mEntries);
mBuckets = newBuckets; mBuckets = newBuckets;
mEntries = newEntries; mEntries = newEntries;
@ -331,11 +332,11 @@ private:
void Initialize(intptr capacity) void Initialize(intptr capacity)
{ {
int_cosize size = GetPrimeish((int_cosize)capacity); int_cosize size = GetPrimeish((int_cosize)capacity);
mBuckets = (int_cosize*)this->rawAllocate(sizeof(int_cosize) * size); AllocData(size, mEntries, mBuckets);
mAllocSize = size; mAllocSize = size;
for (int_cosize i = 0; i < (int_cosize)mAllocSize; i++) mBuckets[i] = -1; for (int_cosize i = 0; i < (int_cosize)mAllocSize; i++) mBuckets[i] = -1;
mEntries = (Entry*)this->rawAllocate(sizeof(Entry) * size);
mFreeList = -1; mFreeList = -1;
} }
@ -430,9 +431,8 @@ public:
mEntries = NULL; mEntries = NULL;
} }
else else
{ {
mBuckets = (int_cosize*)this->rawAllocate(sizeof(int_cosize) * mAllocSize); AllocData(mAllocSize, mEntries, mBuckets);
mEntries = (Entry*)this->rawAllocate(sizeof(Entry) * mAllocSize);
for (int_cosize i = 0; i < mAllocSize; i++) for (int_cosize i = 0; i < mAllocSize; i++)
mBuckets[i] = val.mBuckets[i]; mBuckets[i] = val.mBuckets[i];
@ -465,6 +465,18 @@ public:
mFreeList = val.mFreeList; mFreeList = val.mFreeList;
} }
void AllocData(intptr size, Entry*& outEntries, int_cosize*& outBuckets)
{
uint8* data = (uint8*)this->rawAllocate(size * (sizeof(Entry) + sizeof(int_cosize)));
outEntries = (Entry*)data;
outBuckets = (int_cosize*)(data + size * sizeof(Entry));
}
void DeleteData()
{
this->rawDeallocate(mEntries);
}
~HashSet() ~HashSet()
{ {
if (!std::is_pod<TKey>::value) if (!std::is_pod<TKey>::value)
@ -476,8 +488,7 @@ public:
} }
} }
this->rawDeallocate(mBuckets); DeleteData();
this->rawDeallocate(mEntries);
} }
HashSet& operator=(const HashSet& rhs) HashSet& operator=(const HashSet& rhs)