summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergeanur <s.anureev@yandex.ua>2021-01-19 20:32:55 +0100
committerSergeanur <s.anureev@yandex.ua>2021-01-20 18:24:08 +0100
commitc7c7eff9180afc3e0271bdbdad28297fcd374694 (patch)
tree5e6f057ab13d4d67b0caa27da171d5bbcd774020
parentSync matrix with master (diff)
downloadre3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar.gz
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar.bz2
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar.lz
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar.xz
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.tar.zst
re3-c7c7eff9180afc3e0271bdbdad28297fcd374694.zip
-rw-r--r--src/control/Garages.cpp8
-rw-r--r--src/core/Pools.cpp2
-rw-r--r--src/core/templates.h71
3 files changed, 50 insertions, 31 deletions
diff --git a/src/control/Garages.cpp b/src/control/Garages.cpp
index 1e547a38..1fecdf22 100644
--- a/src/control/Garages.cpp
+++ b/src/control/Garages.cpp
@@ -1526,7 +1526,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
m_bRecreateDoorOnNextRefresh = false;
if (m_pDoor1) {
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;
else {
if (m_bDoor1PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor1) & 0x7F))
@@ -1536,7 +1536,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
}
}
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;
else {
if (m_bDoor1PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor1) & 0x7F))
@@ -1548,7 +1548,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
}
if (m_pDoor2) {
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;
else {
if (m_bDoor2PoolIndex != (CPools::GetDummyPool()->GetIndex((CDummy*)m_pDoor2) & 0x7F))
@@ -1558,7 +1558,7 @@ void CGarage::RefreshDoorPointers(bool bCreate)
}
}
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;
else {
if (m_bDoor2PoolIndex != (CPools::GetObjectPool()->GetIndex((CObject*)m_pDoor2) & 0x7F))
diff --git a/src/core/Pools.cpp b/src/core/Pools.cpp
index ee60f316..bd246b71 100644
--- a/src/core/Pools.cpp
+++ b/src/core/Pools.cpp
@@ -111,7 +111,7 @@ CPools::CheckPoolsEmpty()
void
CPools::MakeSureSlotInObjectPoolIsEmpty(int32 slot)
{
- if (ms_pObjectPool->IsFreeSlot(slot)) return;
+ if (ms_pObjectPool->GetIsFree(slot)) return;
CObject *object = ms_pObjectPool->GetSlot(slot);
if (object->ObjectCreatedBy == TEMP_OBJECT) {
diff --git a/src/core/templates.h b/src/core/templates.h
index 19881219..7bc85ee6 100644
--- a/src/core/templates.h
+++ b/src/core/templates.h
@@ -29,38 +29,58 @@ public:
}
};
+#define POOLFLAG_ID 0x7f
+#define POOLFLAG_ISFREE 0x80
+
template<typename T, typename U = T>
class CPool
{
U *m_entries;
- union Flags {
- struct {
- uint8 id : 7;
- uint8 free : 1;
- };
- uint8 u;
- } *m_flags;
+ uint8 *m_flags;
int32 m_size;
int32 m_allocPtr;
public:
CPool(int32 size, const char *name){
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_allocPtr = -1;
for(int i = 0; i < size; i++){
- m_flags[i].id = 0;
- m_flags[i].free = 1;
+ SetId(i, 0);
+ 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() {
Flush();
}
void Flush() {
if (m_size > 0) {
delete[] (uint8*)m_entries;
- delete[] (uint8*)m_flags;
+ delete[] m_flags;
m_entries = nil;
m_flags = nil;
m_size = 0;
@@ -86,9 +106,9 @@ public:
m_allocPtr = 0;
}
#endif
- while(!m_flags[m_allocPtr].free);
- m_flags[m_allocPtr].free = 0;
- m_flags[m_allocPtr].id++;
+ while(!GetIsFree(m_allocPtr));
+ SetIsFree(m_allocPtr, false);
+ SetId(m_allocPtr, GetId(m_allocPtr)+1);
return (T*)&m_entries[m_allocPtr];
}
T *New(int32 handle){
@@ -98,37 +118,37 @@ public:
}
void SetNotFreeAt(int32 handle){
int idx = handle>>8;
- m_flags[idx].free = 0;
- m_flags[idx].id = handle & 0x7F;
+ SetIsFree(idx, false);
+ SetId(idx, handle & POOLFLAG_ID);
for(m_allocPtr = 0; m_allocPtr < m_size; m_allocPtr++)
- if(m_flags[m_allocPtr].free)
+ if(GetIsFree(m_allocPtr))
return;
}
void Delete(T *entry){
int i = GetJustIndex(entry);
- m_flags[i].free = 1;
+ SetIsFree(i, true);
if(i < m_allocPtr)
m_allocPtr = 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){
#ifdef FIX_BUGS
if (handle == -1)
return nil;
#endif
- return m_flags[handle>>8].u == (handle & 0xFF) ?
+ return m_flags[handle>>8] == (handle & 0xFF) ?
(T*)&m_entries[handle >> 8] : nil;
}
int32 GetIndex(T* entry) {
int i = GetJustIndex_NoFreeAssert(entry);
- return m_flags[i].u + (i << 8);
+ return m_flags[i] + (i<<8);
}
int32 GetJustIndex(T* entry) {
int index = GetJustIndex_NoFreeAssert(entry);
assert((U*)entry == (U*)&m_entries[index]); // cast is unsafe - check required
- assert(!IsFreeSlot(index));
+ assert(!GetIsFree(index));
return index;
}
int32 GetJustIndex_NoFreeAssert(T* entry) {
@@ -140,13 +160,12 @@ public:
int i;
int n = 0;
for(i = 0; i < m_size; i++)
- if(!m_flags[i].free)
+ if(!GetIsFree(i))
n++;
return n;
}
- bool IsFreeSlot(int i) { return !!m_flags[i].free; }
void ClearStorage(uint8 *&flags, U *&entries){
- delete[] (uint8*)flags;
+ delete[] flags;
delete[] (uint8*)entries;
flags = nil;
entries = nil;
@@ -155,7 +174,7 @@ public:
void CopyBack(uint8 *&flags, U *&entries){
memcpy(m_flags, flags, sizeof(uint8)*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;
ClearStorage(flags, entries);
debug("CopyBack:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */