Nebula
Loading...
Searching...
No Matches
idallocator.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
23//------------------------------------------------------------------------------
24#include "id.h"
25#include "idpool.h"
26#include "util/array.h"
28#include <tuple>
29#include <utility>
30#include "util/tupleutility.h"
31#include "util/arrayallocator.h"
33
34namespace Ids
35{
36
37template<class ... TYPES>
38class IdAllocator : public Util::ArrayAllocator<TYPES...>
39{
40public:
42 IdAllocator(Ids::Id32 maxid = 0xFFFFFFFF) : maxId(maxid) {};
43
47 {
48 Ids::Id32 index;
49 if (this->freeIds.Size() > 0)
50 {
51 index = this->freeIds.Back();
52 this->freeIds.EraseBack();
53 }
54 else
55 {
57 n_assert2(this->maxId > index, "max amount of allocations exceeded!\n");
58 }
59
60 return index;
61 }
62
64 void Dealloc(Ids::Id32 index)
65 {
66 // TODO: We could possibly get better performance when defragging if we insert it in reverse order (high to low)
67 this->freeIds.Append(index);
68 }
69
72 {
73 return this->freeIds;
74 }
75
77 const Ids::Id32 Size() const
78 {
79 return this->size;
80 }
81
82private:
83 Ids::Id32 maxId = 0xFFFFFFFF;
85};
86
87#define _DECL_ACQUIRE_RELEASE(ty) \
88 bool ty##Acquire(const ty id); \
89 void ty##Release(const ty id); \
90 struct ty##Lock \
91 { \
92 ty##Lock(const ty element) : element(element) { this->didAcquire = ty##Acquire(this->element); } \
93 ~ty##Lock() { if (this->didAcquire) ty##Release(this->element); } \
94 private: \
95 bool didAcquire; \
96 ty element; \
97 };
98
99#define _IMPL_ACQUIRE_RELEASE(ty, allocator) \
100 bool ty##Acquire(const ty id) { return allocator.Acquire(id.id); } \
101 void ty##Release(const ty id) { allocator.Release(id.id); }
102
103template<int MAX_ALLOCS, class... TYPES>
104class IdAllocatorSafe : public Util::ArrayAllocatorSafe<MAX_ALLOCS, TYPES...>
105{
106public:
109 {
110 };
111
114 {
116 this->allocationLock.Lock();
117 Ids::Id32 index;
118 if (this->freeIds.Size() > 0)
119 {
120 index = this->freeIds.Back();
121 this->owners[index] = Threading::Thread::GetMyThreadId();
122 this->freeIds.EraseBack();
123 }
124 else
125 {
126 alloc_for_each_in_tuple(this->objects);
127 index = this->size++;
128 this->owners.Append(Threading::Thread::GetMyThreadId());
129 this->generations.Append(0);
130 n_assert2(MAX_ALLOCS > index, "max amount of allocations exceeded!\n");
131 }
132 this->allocationLock.Unlock();
133 Ids::Id8 generation = this->generations[index];
134 return index;
135 }
136
138 void Dealloc(Ids::Id32 index)
139 {
140 // TODO: We could possibly get better performance when defragging if we insert it in reverse order (high to low)
141 this->allocationLock.Lock();
142 this->freeIds.Append(index);
143 this->generations[index]++;
144 this->allocationLock.Unlock();
145 }
146
147private:
150};
151
152} // namespace Ids
Util::Array< Ids::Id32 > & FreeIds()
Returns the list of free ids.
Definition idallocator.h:71
const Ids::Id32 Size() const
return number of allocated ids
Definition idallocator.h:77
Ids::Id32 Alloc()
Allocate an object.
Definition idallocator.h:46
IdAllocator(Ids::Id32 maxid=0xFFFFFFFF)
constructor
Definition idallocator.h:42
void Dealloc(Ids::Id32 index)
Deallocate an object. Just places it in freeids array for recycling.
Definition idallocator.h:64
Util::Array< Ids::Id8 > generations
Definition idallocator.h:149
Ids::Id32 Alloc()
Allocate an object.
Definition idallocator.h:113
void Dealloc(Ids::Id32 index)
Deallocate an object. Just places it in freeids array for recycling.
Definition idallocator.h:138
Util::Array< Ids::Id32 > freeIds
Definition idallocator.h:148
IdAllocatorSafe()
constructor
Definition idallocator.h:108
The ArrayAllocator provides a variadic list of types which is to be contained in the allocator and fe...
Definition arrayallocator.h:34
uint32_t size
Definition arrayallocator.h:104
uint32_t Alloc()
allocate a new resource
Definition arrayallocator.h:175
The ArrayAllocatorSafe provides a thread safe variadic list of types which is to be contained in the ...
Definition arrayallocatorsafe.h:65
uint32_t size
Definition arrayallocatorsafe.h:143
Threading::Spinlock allocationLock
Definition arrayallocatorsafe.h:147
Util::PinnedArray< MAX_ALLOCS, Threading::ThreadId > owners
Definition arrayallocatorsafe.h:145
std::tuple< Util::PinnedArray< MAX_ALLOCS, TYPES >... > objects
Definition arrayallocatorsafe.h:144
Nebula's dynamic array class.
Definition array.h:60
#define n_assert2(exp, msg)
Definition debug.h:51
This simple Id pool implements a set of free and used consecutive integers.
Definition id.h:135
uint8_t Id8
Definition id.h:141
uint32_t Id32
Definition id.h:138