Get rid of bitfields in CPool

This commit is contained in:
Sergeanur 2021-01-19 21:32:55 +02:00
parent 25d3066eae
commit a9b8d30ce0
3 changed files with 50 additions and 31 deletions

View file

@ -1539,7 +1539,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
m_bRecreateDoorOnNextRefresh = false; m_bRecreateDoorOnNextRefresh = false;
if (m_pDoor1) { if (m_pDoor1) {
if (m_bDoor1IsDummy) { if (m_bDoor1IsDummy) {
if (CPools::GetDummyPool()->IsFreeSlot(CPools::GetDummyPool()->GetJustIndex_NoFreeAssert((CDummy*)m_pDoor1))) if (CPools::GetDummyPool()->GetIsFree(CPools::GetDummyPool()->GetJustIndex_NoFreeAssert((CDummy*)m_pDoor1)))
bNeedToFindDoorEntities = true; bNeedToFindDoorEntities = true;
else { else {
if (m_bDoor1PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor1) & 0x7F)) if (m_bDoor1PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor1) & 0x7F))
@ -1549,7 +1549,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
} }
} }
else { else {
if (CPools::GetObjectPool()->IsFreeSlot(CPools::GetObjectPool()->GetJustIndex_NoFreeAssert((CObject*)m_pDoor1))) if (CPools::GetObjectPool()->GetIsFree(CPools::GetObjectPool()->GetJustIndex_NoFreeAssert((CObject*)m_pDoor1)))
bNeedToFindDoorEntities = true; bNeedToFindDoorEntities = true;
else { else {
if (m_bDoor1PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor1) & 0x7F)) if (m_bDoor1PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor1) & 0x7F))
@ -1561,7 +1561,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
} }
if (m_pDoor2) { if (m_pDoor2) {
if (m_bDoor2IsDummy) { if (m_bDoor2IsDummy) {
if (CPools::GetDummyPool()->IsFreeSlot(CPools::GetDummyPool()->GetJustIndex_NoFreeAssert((CDummy*)m_pDoor2))) if (CPools::GetDummyPool()->GetIsFree(CPools::GetDummyPool()->GetJustIndex_NoFreeAssert((CDummy*)m_pDoor2)))
bNeedToFindDoorEntities = true; bNeedToFindDoorEntities = true;
else { else {
if (m_bDoor2PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor2) & 0x7F)) if (m_bDoor2PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor2) & 0x7F))
@ -1571,7 +1571,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
} }
} }
else { else {
if (CPools::GetObjectPool()->IsFreeSlot(CPools::GetObjectPool()->GetJustIndex_NoFreeAssert((CObject*)m_pDoor2))) if (CPools::GetObjectPool()->GetIsFree(CPools::GetObjectPool()->GetJustIndex_NoFreeAssert((CObject*)m_pDoor2)))
bNeedToFindDoorEntities = true; bNeedToFindDoorEntities = true;
else { else {
if (m_bDoor2PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor2) & 0x7F)) if (m_bDoor2PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor2) & 0x7F))

View file

@ -102,7 +102,7 @@ CPools::CheckPoolsEmpty()
void void
CPools::MakeSureSlotInObjectPoolIsEmpty(int32 slot) CPools::MakeSureSlotInObjectPoolIsEmpty(int32 slot)
{ {
if (ms_pObjectPool->IsFreeSlot(slot)) return; if (ms_pObjectPool->GetIsFree(slot)) return;
CObject *object = ms_pObjectPool->GetSlot(slot); CObject *object = ms_pObjectPool->GetSlot(slot);
if (object->ObjectCreatedBy == TEMP_OBJECT) { if (object->ObjectCreatedBy == TEMP_OBJECT) {

View file

@ -29,39 +29,59 @@ public:
} }
}; };
#define POOLFLAG_ID 0x7f
#define POOLFLAG_ISFREE 0x80
template<typename T, typename U = T> template<typename T, typename U = T>
class CPool class CPool
{ {
U *m_entries; U *m_entries;
union Flags { uint8 *m_flags;
struct {
uint8 id : 7;
uint8 free : 1;
};
uint8 u;
} *m_flags;
int32 m_size; int32 m_size;
int32 m_allocPtr; int32 m_allocPtr;
public: public:
CPool(int32 size){ CPool(int32 size){
m_entries = (U*)new uint8[sizeof(U)*size]; m_entries = (U*)new uint8[sizeof(U)*size];
m_flags = (Flags*)new uint8[sizeof(Flags)*size]; m_flags = new uint8[size];
m_size = size; m_size = size;
m_allocPtr = 0; m_allocPtr = 0;
for(int i = 0; i < size; i++){ for(int i = 0; i < size; i++){
m_flags[i].id = 0; SetId(i, 0);
m_flags[i].free = 1; SetIsFree(i, true);
} }
} }
int GetId(int i) const
{
return m_flags[i] & POOLFLAG_ID;
}
bool GetIsFree(int i) const
{
return !!(m_flags[i] & POOLFLAG_ISFREE);
}
void SetId(int i, int id)
{
m_flags[i] = (m_flags[i] & POOLFLAG_ISFREE) | (id & POOLFLAG_ID);
}
void SetIsFree(int i, bool isFree)
{
if (isFree)
m_flags[i] |= POOLFLAG_ISFREE;
else
m_flags[i] &= ~POOLFLAG_ISFREE;
}
~CPool() { ~CPool() {
Flush(); Flush();
} }
void Flush() { void Flush() {
if (m_size > 0) { if (m_size > 0) {
delete[] (uint8*)m_entries; delete[] (uint8*)m_entries;
delete[] (uint8*)m_flags; delete[] m_flags;
m_entries = nil; m_entries = nil;
m_flags = nil; m_flags = nil;
m_size = 0; m_size = 0;
@ -87,9 +107,9 @@ public:
m_allocPtr = 0; m_allocPtr = 0;
} }
#endif #endif
while(!m_flags[m_allocPtr].free); while(!GetIsFree(m_allocPtr));
m_flags[m_allocPtr].free = 0; SetIsFree(m_allocPtr, false);
m_flags[m_allocPtr].id++; SetId(m_allocPtr, GetId(m_allocPtr)+1);
return (T*)&m_entries[m_allocPtr]; return (T*)&m_entries[m_allocPtr];
} }
T *New(int32 handle){ T *New(int32 handle){
@ -99,36 +119,36 @@ public:
} }
void SetNotFreeAt(int32 handle){ void SetNotFreeAt(int32 handle){
int idx = handle>>8; int idx = handle>>8;
m_flags[idx].free = 0; SetIsFree(idx, false);
m_flags[idx].id = handle & 0x7F; SetId(idx, handle & POOLFLAG_ID);
for(m_allocPtr = 0; m_allocPtr < m_size; m_allocPtr++) for(m_allocPtr = 0; m_allocPtr < m_size; m_allocPtr++)
if(m_flags[m_allocPtr].free) if(GetIsFree(m_allocPtr))
return; return;
} }
void Delete(T *entry){ void Delete(T *entry){
int i = GetJustIndex(entry); int i = GetJustIndex(entry);
m_flags[i].free = 1; SetIsFree(i, true);
if(i < m_allocPtr) if(i < m_allocPtr)
m_allocPtr = i; m_allocPtr = i;
} }
T *GetSlot(int i){ T *GetSlot(int i){
return m_flags[i].free ? nil : (T*)&m_entries[i]; return GetIsFree(i) ? nil : (T*)&m_entries[i];
} }
T *GetAt(int handle){ T *GetAt(int handle){
#ifdef FIX_BUGS #ifdef FIX_BUGS
if (handle == -1) if (handle == -1)
return nil; return nil;
#endif #endif
return m_flags[handle>>8].u == (handle & 0xFF) ? return m_flags[handle>>8] == (handle & 0xFF) ?
(T*)&m_entries[handle >> 8] : nil; (T*)&m_entries[handle >> 8] : nil;
} }
int32 GetIndex(T *entry){ int32 GetIndex(T *entry){
int i = GetJustIndex_NoFreeAssert(entry); int i = GetJustIndex_NoFreeAssert(entry);
return m_flags[i].u + (i<<8); return m_flags[i] + (i<<8);
} }
int32 GetJustIndex(T *entry){ int32 GetJustIndex(T *entry){
int index = GetJustIndex_NoFreeAssert(entry); int index = GetJustIndex_NoFreeAssert(entry);
assert(!IsFreeSlot(index)); assert(!GetIsFree(index));
return index; return index;
} }
int32 GetJustIndex_NoFreeAssert(T* entry){ int32 GetJustIndex_NoFreeAssert(T* entry){
@ -140,13 +160,12 @@ public:
int i; int i;
int n = 0; int n = 0;
for(i = 0; i < m_size; i++) for(i = 0; i < m_size; i++)
if(!m_flags[i].free) if(!GetIsFree(i))
n++; n++;
return n; return n;
} }
bool IsFreeSlot(int i) { return !!m_flags[i].free; }
void ClearStorage(uint8 *&flags, U *&entries){ void ClearStorage(uint8 *&flags, U *&entries){
delete[] (uint8*)flags; delete[] flags;
delete[] (uint8*)entries; delete[] (uint8*)entries;
flags = nil; flags = nil;
entries = nil; entries = nil;
@ -155,7 +174,7 @@ public:
void CopyBack(uint8 *&flags, U *&entries){ void CopyBack(uint8 *&flags, U *&entries){
memcpy(m_flags, flags, sizeof(uint8)*m_size); memcpy(m_flags, flags, sizeof(uint8)*m_size);
memcpy(m_entries, entries, sizeof(U)*m_size); memcpy(m_entries, entries, sizeof(U)*m_size);
debug("Size copied:%d (%d)\n", sizeof(U)*m_size, sizeof(Flags)*m_size); debug("Size copied:%d (%d)\n", sizeof(U)*m_size, m_size);
m_allocPtr = 0; m_allocPtr = 0;
ClearStorage(flags, entries); ClearStorage(flags, entries);
debug("CopyBack:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */ debug("CopyBack:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */