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