2019-05-15 16:52:37 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-18 13:58:59 +01:00
|
|
|
template<typename T, int32 n>
|
2019-05-15 16:52:37 +02:00
|
|
|
class CStore
|
|
|
|
{
|
|
|
|
public:
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 allocPtr;
|
2019-05-15 16:52:37 +02:00
|
|
|
T store[n];
|
|
|
|
|
2020-12-18 13:58:59 +01:00
|
|
|
T *Alloc(void){
|
|
|
|
if(allocPtr >= n){
|
2019-05-15 16:52:37 +02:00
|
|
|
printf("Size of this thing:%d needs increasing\n", n);
|
2019-06-17 10:30:02 +02:00
|
|
|
assert(0);
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
return &store[allocPtr++];
|
2019-05-15 16:52:37 +02:00
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
void Clear(void){
|
|
|
|
allocPtr = 0;
|
2019-05-15 16:52:37 +02:00
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetIndex(T *item){
|
|
|
|
assert(item >= &store[0]);
|
|
|
|
assert(item < &store[n]);
|
|
|
|
return item - store;
|
2020-05-07 21:56:09 +02:00
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
T *GetItem(int32 index){
|
2020-05-07 21:56:09 +02:00
|
|
|
assert(index >= 0);
|
|
|
|
assert(index < n);
|
2020-12-18 13:58:59 +01:00
|
|
|
return &store[index];
|
2020-05-07 21:56:09 +02:00
|
|
|
}
|
2019-05-15 16:52:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename U = T>
|
|
|
|
class CPool
|
|
|
|
{
|
|
|
|
U *m_entries;
|
|
|
|
union Flags {
|
|
|
|
struct {
|
|
|
|
uint8 id : 7;
|
|
|
|
uint8 free : 1;
|
|
|
|
};
|
|
|
|
uint8 u;
|
|
|
|
} *m_flags;
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 m_size;
|
|
|
|
int32 m_allocPtr;
|
2019-05-15 16:52:37 +02:00
|
|
|
|
|
|
|
public:
|
2020-12-18 13:58:59 +01:00
|
|
|
CPool(int32 size){
|
2020-11-26 16:47:19 +01:00
|
|
|
m_entries = (U*)new uint8[sizeof(U)*size];
|
|
|
|
m_flags = (Flags*)new uint8[sizeof(Flags)*size];
|
2019-05-15 16:52:37 +02:00
|
|
|
m_size = size;
|
|
|
|
m_allocPtr = 0;
|
|
|
|
for(int i = 0; i < size; i++){
|
|
|
|
m_flags[i].id = 0;
|
|
|
|
m_flags[i].free = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 17:04:18 +02:00
|
|
|
|
2019-10-16 23:53:25 +02:00
|
|
|
~CPool() {
|
|
|
|
Flush();
|
|
|
|
}
|
|
|
|
void Flush() {
|
|
|
|
if (m_size > 0) {
|
2020-11-26 16:47:19 +01:00
|
|
|
delete[] (uint8*)m_entries;
|
|
|
|
delete[] (uint8*)m_flags;
|
2019-10-16 23:53:25 +02:00
|
|
|
m_entries = nil;
|
|
|
|
m_flags = nil;
|
|
|
|
m_size = 0;
|
|
|
|
m_allocPtr = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetSize(void) const { return m_size; }
|
2019-05-15 16:52:37 +02:00
|
|
|
T *New(void){
|
|
|
|
bool wrapped = false;
|
|
|
|
do
|
2020-01-10 23:48:05 +01:00
|
|
|
#ifdef FIX_BUGS
|
|
|
|
if (++m_allocPtr >= m_size) {
|
|
|
|
m_allocPtr = 0;
|
|
|
|
if (wrapped)
|
|
|
|
return nil;
|
|
|
|
wrapped = true;
|
|
|
|
}
|
|
|
|
#else
|
2019-05-15 16:52:37 +02:00
|
|
|
if(++m_allocPtr == m_size){
|
|
|
|
if(wrapped)
|
|
|
|
return nil;
|
|
|
|
wrapped = true;
|
|
|
|
m_allocPtr = 0;
|
|
|
|
}
|
2020-01-10 23:48:05 +01:00
|
|
|
#endif
|
2019-05-15 16:52:37 +02:00
|
|
|
while(!m_flags[m_allocPtr].free);
|
|
|
|
m_flags[m_allocPtr].free = 0;
|
|
|
|
m_flags[m_allocPtr].id++;
|
|
|
|
return (T*)&m_entries[m_allocPtr];
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
T *New(int32 handle){
|
2019-06-30 12:59:55 +02:00
|
|
|
T *entry = (T*)&m_entries[handle>>8];
|
2019-05-15 16:52:37 +02:00
|
|
|
SetNotFreeAt(handle);
|
|
|
|
return entry;
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
void SetNotFreeAt(int32 handle){
|
2019-05-15 16:52:37 +02:00
|
|
|
int idx = handle>>8;
|
|
|
|
m_flags[idx].free = 0;
|
|
|
|
m_flags[idx].id = handle & 0x7F;
|
|
|
|
for(m_allocPtr = 0; m_allocPtr < m_size; m_allocPtr++)
|
|
|
|
if(m_flags[m_allocPtr].free)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
void Delete(T *entry){
|
|
|
|
int i = GetJustIndex(entry);
|
|
|
|
m_flags[i].free = 1;
|
|
|
|
if(i < m_allocPtr)
|
|
|
|
m_allocPtr = i;
|
|
|
|
}
|
|
|
|
T *GetSlot(int i){
|
|
|
|
return m_flags[i].free ? nil : (T*)&m_entries[i];
|
|
|
|
}
|
|
|
|
T *GetAt(int handle){
|
2020-04-20 00:04:57 +02:00
|
|
|
#ifdef FIX_BUGS
|
|
|
|
if (handle == -1)
|
|
|
|
return nil;
|
|
|
|
#endif
|
2019-06-20 11:26:15 +02:00
|
|
|
return m_flags[handle>>8].u == (handle & 0xFF) ?
|
|
|
|
(T*)&m_entries[handle >> 8] : nil;
|
2019-05-15 16:52:37 +02:00
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetIndex(T *entry){
|
2020-11-30 00:15:03 +01:00
|
|
|
int i = GetJustIndex_NoFreeAssert(entry);
|
2019-05-15 16:52:37 +02:00
|
|
|
return m_flags[i].u + (i<<8);
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetJustIndex(T *entry){
|
2020-11-30 00:15:03 +01:00
|
|
|
int index = GetJustIndex_NoFreeAssert(entry);
|
|
|
|
assert(!IsFreeSlot(index));
|
|
|
|
return index;
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetJustIndex_NoFreeAssert(T* entry){
|
2020-11-30 00:15:03 +01:00
|
|
|
int index = ((U*)entry - m_entries);
|
|
|
|
assert((U*)entry == (U*)&m_entries[index]); // cast is unsafe - check required
|
|
|
|
return index;
|
2019-05-15 16:52:37 +02:00
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 GetNoOfUsedSpaces(void) const{
|
2019-05-15 16:52:37 +02:00
|
|
|
int i;
|
|
|
|
int n = 0;
|
|
|
|
for(i = 0; i < m_size; i++)
|
|
|
|
if(!m_flags[i].free)
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
2019-10-16 23:53:25 +02:00
|
|
|
bool IsFreeSlot(int i) { return !!m_flags[i].free; }
|
2019-06-25 00:42:23 +02:00
|
|
|
void ClearStorage(uint8 *&flags, U *&entries){
|
2020-11-26 16:47:19 +01:00
|
|
|
delete[] (uint8*)flags;
|
|
|
|
delete[] (uint8*)entries;
|
2019-06-25 00:42:23 +02:00
|
|
|
flags = nil;
|
|
|
|
entries = nil;
|
|
|
|
}
|
2019-10-16 23:53:25 +02:00
|
|
|
uint32 GetMaxEntrySize() const { return sizeof(U); }
|
2019-06-25 00:42:23 +02:00
|
|
|
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);
|
2019-06-22 20:35:23 +02:00
|
|
|
m_allocPtr = 0;
|
|
|
|
ClearStorage(flags, entries);
|
2019-06-25 00:42:23 +02:00
|
|
|
debug("CopyBack:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */
|
|
|
|
}
|
|
|
|
void Store(uint8 *&flags, U *&entries){
|
2020-11-26 16:47:19 +01:00
|
|
|
flags = (uint8*)new uint8[sizeof(uint8)*m_size];
|
|
|
|
entries = (U*)new uint8[sizeof(U)*m_size];
|
2019-06-25 00:42:23 +02:00
|
|
|
memcpy(flags, m_flags, sizeof(uint8)*m_size);
|
|
|
|
memcpy(entries, m_entries, sizeof(U)*m_size);
|
|
|
|
debug("Stored:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */
|
2019-06-22 20:35:23 +02:00
|
|
|
}
|
2019-05-15 16:52:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class CLink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
T item;
|
|
|
|
CLink<T> *prev;
|
|
|
|
CLink<T> *next;
|
|
|
|
|
|
|
|
void Insert(CLink<T> *link){
|
|
|
|
link->next = this->next;
|
|
|
|
this->next->prev = link;
|
|
|
|
link->prev = this;
|
|
|
|
this->next = link;
|
|
|
|
}
|
|
|
|
void Remove(void){
|
|
|
|
this->prev->next = this->next;
|
|
|
|
this->next->prev = this->prev;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class CLinkList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CLink<T> head, tail;
|
|
|
|
CLink<T> freeHead, freeTail;
|
|
|
|
CLink<T> *links;
|
|
|
|
|
|
|
|
void Init(int n){
|
|
|
|
links = new CLink<T>[n];
|
|
|
|
head.next = &tail;
|
|
|
|
tail.prev = &head;
|
|
|
|
freeHead.next = &freeTail;
|
|
|
|
freeTail.prev = &freeHead;
|
|
|
|
while(n--)
|
|
|
|
freeHead.Insert(&links[n]);
|
|
|
|
}
|
2019-06-11 08:59:28 +02:00
|
|
|
void Shutdown(void){
|
|
|
|
delete[] links;
|
|
|
|
links = nil;
|
|
|
|
}
|
2019-05-15 16:52:37 +02:00
|
|
|
void Clear(void){
|
|
|
|
while(head.next != &tail)
|
|
|
|
Remove(head.next);
|
|
|
|
}
|
|
|
|
CLink<T> *Insert(T const &item){
|
|
|
|
CLink<T> *node = freeHead.next;
|
|
|
|
if(node == &freeTail)
|
|
|
|
return nil;
|
|
|
|
node->item = item;
|
|
|
|
node->Remove(); // remove from free list
|
|
|
|
head.Insert(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
CLink<T> *InsertSorted(T const &item){
|
|
|
|
CLink<T> *sort;
|
|
|
|
for(sort = head.next; sort != &tail; sort = sort->next)
|
|
|
|
if(sort->item.sort >= item.sort)
|
|
|
|
break;
|
|
|
|
CLink<T> *node = freeHead.next;
|
|
|
|
if(node == &freeTail)
|
|
|
|
return nil;
|
|
|
|
node->item = item;
|
|
|
|
node->Remove(); // remove from free list
|
|
|
|
sort->prev->Insert(node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
void Remove(CLink<T> *link){
|
|
|
|
link->Remove(); // remove from list
|
|
|
|
freeHead.Insert(link); // insert into free list
|
|
|
|
}
|
2020-12-18 13:58:59 +01:00
|
|
|
int32 Count(void){
|
2019-05-15 16:52:37 +02:00
|
|
|
int n = 0;
|
|
|
|
CLink<T> *lnk;
|
|
|
|
for(lnk = head.next; lnk != &tail; lnk = lnk->next)
|
|
|
|
n++;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
};
|