summaryrefslogtreecommitdiffstats
path: root/src/core/templates.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/templates.h')
-rw-r--r--src/core/templates.h79
1 files changed, 42 insertions, 37 deletions
diff --git a/src/core/templates.h b/src/core/templates.h
index 9f5bd5ea..19881219 100644
--- a/src/core/templates.h
+++ b/src/core/templates.h
@@ -1,31 +1,31 @@
#pragma once
-template<typename T, int n>
+template<typename T, int32 n>
class CStore
{
public:
- int allocPtr;
+ int32 allocPtr;
T store[n];
- T *alloc(void){
- if(this->allocPtr >= n){
+ T *Alloc(void){
+ if(allocPtr >= n){
printf("Size of this thing:%d needs increasing\n", n);
assert(0);
}
- return &this->store[this->allocPtr++];
+ return &store[allocPtr++];
}
- void clear(void){
- this->allocPtr = 0;
+ void Clear(void){
+ allocPtr = 0;
}
- int getIndex(T *item){
- assert(item >= &this->store[0]);
- assert(item < &this->store[n]);
- return item - this->store;
+ int32 GetIndex(T *item){
+ assert(item >= &store[0]);
+ assert(item < &store[n]);
+ return item - store;
}
- T *getItem(int index){
+ T *GetItem(int32 index){
assert(index >= 0);
assert(index < n);
- return &this->store[index];
+ return &store[index];
}
};
@@ -40,15 +40,13 @@ class CPool
};
uint8 u;
} *m_flags;
- int m_size;
- int m_allocPtr;
+ int32 m_size;
+ int32 m_allocPtr;
public:
- // TODO(MIAMI): remove ctor without name argument
- CPool(int size, const char *name){
- // TODO: use new here
- m_entries = (U*)malloc(sizeof(U)*size);
- m_flags = (Flags*)malloc(sizeof(Flags)*size);
+ CPool(int32 size, const char *name){
+ m_entries = (U*)new uint8[sizeof(U)*size];
+ m_flags = (Flags*)new uint8[sizeof(Flags)*size];
m_size = size;
m_allocPtr = -1;
for(int i = 0; i < size; i++){
@@ -61,15 +59,15 @@ public:
}
void Flush() {
if (m_size > 0) {
- free(m_entries);
- free(m_flags);
+ delete[] (uint8*)m_entries;
+ delete[] (uint8*)m_flags;
m_entries = nil;
m_flags = nil;
m_size = 0;
m_allocPtr = 0;
}
}
- int GetSize(void) const { return m_size; }
+ int32 GetSize(void) const { return m_size; }
T *New(void){
bool wrapped = false;
do
@@ -93,12 +91,12 @@ public:
m_flags[m_allocPtr].id++;
return (T*)&m_entries[m_allocPtr];
}
- T *New(int handle){
+ T *New(int32 handle){
T *entry = (T*)&m_entries[handle>>8];
SetNotFreeAt(handle);
return entry;
}
- void SetNotFreeAt(int handle){
+ void SetNotFreeAt(int32 handle){
int idx = handle>>8;
m_flags[idx].free = 0;
m_flags[idx].id = handle & 0x7F;
@@ -123,15 +121,22 @@ public:
return m_flags[handle>>8].u == (handle & 0xFF) ?
(T*)&m_entries[handle >> 8] : nil;
}
- int GetIndex(T *entry){
- int i = GetJustIndex(entry);
- return m_flags[i].u + (i<<8);
+ int32 GetIndex(T* entry) {
+ int i = GetJustIndex_NoFreeAssert(entry);
+ return m_flags[i].u + (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));
+ return index;
}
- int GetJustIndex(T *entry){
- // TODO: the cast is unsafe
- return (int)((U*)entry - m_entries);
+ int32 GetJustIndex_NoFreeAssert(T* entry) {
+ int index = ((U*)entry - m_entries);
+ // Please don't add unsafe assert here, because at least one func. use this to check if entity is ped or vehicle.
+ return index;
}
- int GetNoOfUsedSpaces(void) const {
+ int32 GetNoOfUsedSpaces(void) const {
int i;
int n = 0;
for(i = 0; i < m_size; i++)
@@ -141,8 +146,8 @@ public:
}
bool IsFreeSlot(int i) { return !!m_flags[i].free; }
void ClearStorage(uint8 *&flags, U *&entries){
- free(flags);
- free(entries);
+ delete[] (uint8*)flags;
+ delete[] (uint8*)entries;
flags = nil;
entries = nil;
}
@@ -156,8 +161,8 @@ public:
debug("CopyBack:%d (/%d)\n", GetNoOfUsedSpaces(), m_size); /* Assumed inlining */
}
void Store(uint8 *&flags, U *&entries){
- flags = (uint8*)malloc(sizeof(uint8)*m_size);
- entries = (U*)malloc(sizeof(U)*m_size);
+ flags = (uint8*)new uint8[sizeof(uint8)*m_size];
+ entries = (U*)new uint8[sizeof(U)*m_size];
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 */
@@ -236,7 +241,7 @@ public:
link->Remove(); // remove from list
freeHead.Insert(link); // insert into free list
}
- int Count(void){
+ int32 Count(void){
int n = 0;
CLink<T> *lnk;
for(lnk = head.next; lnk != &tail; lnk = lnk->next)