Tramway SDK
pool.h
Go to the documentation of this file.
1// Tramway Drifting and Dungeon Exploration Simulator SDK Runtime
2
3#ifndef TRAM_SDK_TEMPLATES_POOL_H
4#define TRAM_SDK_TEMPLATES_POOL_H
5
6#include <string>
7#include <iostream>
8#include <cassert>
9
10/* this is one of the oldest files in the library, hence why it looks so awful.
11 * it probably would be a good idea to clean up the code, but editing this file
12 * causes the whole library to re-compile and I don't really feel like doing it
13 * now. also adding this comment caused the whole library to recompile. also
14 * adding this bit at the end caused the library to re-compile. I should
15 * probably stop editing this file.
16 */
17
18namespace tram {
19
20template <typename T>
21class Pool {
22protected:
23 std::string poolName;
24 uint64_t poolSize;
25 uint64_t maxSize;
27 T* last;
29public:
30 struct iterator {
31 iterator(T* ptr) : ptr(ptr) {}
32
33 T& operator*() const { return *ptr; }
34 T* operator->() { return ptr; }
35
36 iterator& operator++() { do ptr++; while (*((uint64_t*)ptr) == 0 && *(((uint64_t*)ptr) + 1) != 0); return *this; }
37
38 iterator operator++(int) { iterator tmp = *this; ++(*this); return tmp; }
39
40 friend bool operator== (const iterator& a, const iterator& b) { return a.ptr == b.ptr; };
41 friend bool operator!= (const iterator& a, const iterator& b) { return a.ptr != b.ptr; };
42
43 T* ptr;
44 };
45
46 constexpr Pool(std::string name, uint64_t initialSize, bool skipFirst = false){
47 poolName = name;
48 maxSize = initialSize;
49 poolSize = skipFirst ? 1 : 0;
50
51 first = static_cast<T*>(::operator new((initialSize * sizeof(T)) + sizeof(uint64_t) * 2));
52 last = first;
54
55 if (skipFirst) {
56 last++;
57 lastfree++;
58 }
59
60 *((uint64_t*)last) = 0;
61 *(((uint64_t*)last) + 1) = 0;
62 };
63
64 template <typename... Args>
65 T* AddNew(Args&&... args){
66 if(poolSize == maxSize){
67 std::cout << "Pool " << poolName << " out of space!" << std::endl;
68 abort();
69 }
70
71 T* newobj;
72
73 if(lastfree != last){
74 newobj = lastfree;
75 uint64_t* skip = reinterpret_cast<uint64_t*>(lastfree);
76 skip++;
77 T** skip2 = reinterpret_cast<T**>(skip);
78 lastfree = *skip2;
79 } else {
80 newobj = lastfree;
81 last++;
82 lastfree++;
83
84 *((uint64_t*)last) = 0;
85 *(((uint64_t*)last) + 1) = 0;
86 }
87
88 new(newobj) T(std::forward<Args>(args)...);
89
90 poolSize++;
91
92 return newobj;
93 };
94
95 void Remove(T* removeptr){
96 assert(removeptr >= first && removeptr < last); // pointer is in pool
97 removeptr->~T(); // destruct
98 uint64_t* skip = reinterpret_cast<uint64_t*>(removeptr);
99 *skip = 0; // mark as empty
100 skip++;
101 T** nextfree = reinterpret_cast<T**>(skip);
102 *nextfree = lastfree; // add pointer to previous free place
103 lastfree = removeptr;
104 poolSize--;
105 };
106 T& operator[](size_t index) { return *(first + index); } // note that there is no checking for whether the index is valid
107 T* GetFirst() {return first;}; // yeet?
108 T* GetLast() {return last;}; // also yeet?
109 iterator begin() {auto ptr = first; while (*((uint64_t*)ptr) == 0 && ptr < last) ptr++; return ptr;};
110 iterator end() {return last;};
111 size_t GetSize() const {return poolSize;}; // yeet too?
112 size_t size() const {return poolSize;};
113 size_t index(const T* ptr) const {return ptr - first;};
114
116 bool validate (const T* ptr) const {
117 return ptr >= first && ptr <= last && *((uint64_t*)ptr) != 0;
118 }
119
120 // make sure that there will be enough room for the empty place marker and free list pointer
121 static_assert(sizeof(T) >= sizeof(T*) + sizeof(uint64_t));
122 //static_assert(sizeof(T) % sizeof(uint64_t) == 0); // idk what this is
123};
124
125template <typename T>
127public:
128 template <typename... Args>
129 static T* New(Args&&... args){return pool.AddNew(std::forward<Args>(args)...);}
130 static void Delete(T* obj){pool.Remove(obj);}
131 static Pool<T>& GetPool(){return pool;}
132protected:
133 static Pool<T> pool;
134};
135
136template <typename T> using PoopProxy = PoolProxy<T>;
137
138template <typename T>
139class PoolPtr {
140public:
143 T* GetResource() { return ptr; }
144 T* operator->() { return ptr; }
145 T& operator*() { return ptr; }
146 explicit operator bool() { return ptr != nullptr; }
147protected:
148 T* ptr;
149};
150
151}
152
153#endif // TRAM_SDK_TEMPLATES_POOL_H
Definition: pool.h:21
T * lastfree
Definition: pool.h:28
bool validate(const T *ptr) const
Checks if an object can be accessed through iteration.
Definition: pool.h:116
T * AddNew(Args &&... args)
Definition: pool.h:65
uint64_t poolSize
Definition: pool.h:24
T * last
Definition: pool.h:27
iterator end()
Definition: pool.h:110
void Remove(T *removeptr)
Definition: pool.h:95
T * GetLast()
Definition: pool.h:108
std::string poolName
Definition: pool.h:23
size_t index(const T *ptr) const
Definition: pool.h:113
iterator begin()
Definition: pool.h:109
uint64_t maxSize
Definition: pool.h:25
T * first
Definition: pool.h:26
constexpr Pool(std::string name, uint64_t initialSize, bool skipFirst=false)
Definition: pool.h:46
T & operator[](size_t index)
Definition: pool.h:106
T * GetFirst()
Definition: pool.h:107
size_t size() const
Definition: pool.h:112
size_t GetSize() const
Definition: pool.h:111
Definition: pool.h:126
static void Delete(T *obj)
Definition: pool.h:130
static Pool< T > & GetPool()
Definition: pool.h:131
static T * New(Args &&... args)
Definition: pool.h:129
static Pool< T > pool
Definition: pool.h:133
Definition: pool.h:139
T * GetResource()
Definition: pool.h:143
T * operator->()
Definition: pool.h:144
~PoolPtr()
Definition: pool.h:142
T & operator*()
Definition: pool.h:145
T * ptr
Definition: pool.h:148
PoolPtr()
Definition: pool.h:141
Serialization, i.e.
Definition: pool.h:30
iterator(T *ptr)
Definition: pool.h:31
T * operator->()
Definition: pool.h:34
friend bool operator!=(const iterator &a, const iterator &b)
Definition: pool.h:41
T * ptr
Definition: pool.h:41
friend bool operator==(const iterator &a, const iterator &b)
Definition: pool.h:40
iterator operator++(int)
Definition: pool.h:38
T & operator*() const
Definition: pool.h:33
iterator & operator++()
Definition: pool.h:36