4#ifndef TRAM_SDK_TEMPLATES_POOL_H
5#define TRAM_SDK_TEMPLATES_POOL_H
39 Pool(std::string name, uint64_t initialSize,
bool skipFirst =
false){
44 first =
static_cast<T*
>(::operator
new((initialSize *
sizeof(T)) +
sizeof(uint64_t) * 2));
54 *((uint64_t*)
last) = 0;
55 *(((uint64_t*)
last) + 1) = 0;
58 template <
typename... Args>
61 std::cout <<
"Pool " <<
poolName <<
" out of space!" << std::endl;
69 uint64_t* skip =
reinterpret_cast<uint64_t*
>(
lastfree);
71 T** skip2 =
reinterpret_cast<T**
>(skip);
78 *((uint64_t*)
last) = 0;
79 *(((uint64_t*)
last) + 1) = 0;
82 new(newobj) T(std::forward<Args>(args)...);
90 assert(removeptr >=
first && removeptr <
last);
92 uint64_t* skip =
reinterpret_cast<uint64_t*
>(removeptr);
95 T** nextfree =
reinterpret_cast<T**
>(skip);
111 return ptr >=
first && ptr <=
last && *((uint64_t*)ptr) != 0;
115 static_assert(
sizeof(T) >=
sizeof(T*) +
sizeof(uint64_t));
122 template <
typename... Args>
123 static T*
New(Args&&... args){
return pool.AddNew(std::forward<Args>(args)...);}
140 explicit operator bool() {
return ptr !=
nullptr; }
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
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
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
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