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
46 {
48
49 Ids::Id32 index;
50 if (this->freeIds.Size() > 0)
51 {
52 index = this->freeIds.Back();
53 this->freeIds.EraseBack();
54 }
55 else
56 {
58 n_assert2(this->maxId > index, "max amount of allocations exceeded!\n");
59 }
60
61 return index;
62 }
63
65 void Dealloc(Ids::Id32 index)
66 {
67 // TODO: We could possibly get better performance when defragging if we insert it in reverse order (high to low)
68 this->freeIds.Append(index);
69 }
70
73 {
74 return this->freeIds;
75 }
76
78 const Ids::Id32 Size() const
79 {
80 return this->size;
81 }
82
83private:
84 Ids::Id32 maxId = 0xFFFFFFFF;
86};
87
88#define _DECL_ACQUIRE_RELEASE(ty) \
89 bool ty##Acquire(const ty id); \
90 void ty##Release(const ty id); \
91 struct ty##Lock \
92 { \
93 ty##Lock(const ty element) : element(element) { this->didAcquire = ty##Acquire(this->element); } \
94 ~ty##Lock() { if (this->didAcquire) ty##Release(this->element); } \
95 private: \
96 bool didAcquire; \
97 ty element; \
98 };
99
100#define _IMPL_ACQUIRE_RELEASE(ty, allocator) \
101 bool ty##Acquire(const ty id) { return allocator.Acquire(id.id); } \
102 void ty##Release(const ty id) { allocator.Release(id.id); }
103
104template<int MAX_ALLOCS, class... TYPES>
105class IdAllocatorSafe : public Util::ArrayAllocatorSafe<MAX_ALLOCS, TYPES...>
106{
107public:
110 {
111 };
112
115 {
117 this->allocationLock.Lock();
118 Ids::Id32 index;
119 if (this->freeIds.Size() > 0)
120 {
121 index = this->freeIds.Back();
122 this->owners[index] = Threading::Thread::GetMyThreadId();
123 this->freeIds.EraseBack();
124 }
125 else
126 {
127 alloc_for_each_in_tuple(this->objects);
128 index = this->size++;
129 this->owners.Append(Threading::Thread::GetMyThreadId());
130 n_assert2(MAX_ALLOCS > index, "max amount of allocations exceeded!\n");
131 }
132 this->allocationLock.Unlock();
133
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->allocationLock.Unlock();
144 }
145
146private:
148};
149
150} // namespace Ids
An ID allocator associates an id with a slice in an N number of arrays.
Definition idallocator.h:39
Util::Array< Ids::Id32 > & FreeIds()
Returns the list of free ids.
Definition idallocator.h:72
const Ids::Id32 Size() const
return number of allocated ids
Definition idallocator.h:78
Ids::Id32 maxId
Definition idallocator.h:84
Ids::Id32 Alloc()
Allocate an object.
Definition idallocator.h:45
Util::Array< Ids::Id32 > freeIds
Definition idallocator.h:85
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:65
Definition idallocator.h:106
Ids::Id32 Alloc()
Allocate an object.
Definition idallocator.h:114
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:147
IdAllocatorSafe()
constructor
Definition idallocator.h:109
void Lock()
Lock.
Definition spinlock.h:83
void Unlock()
Unlock.
Definition spinlock.h:103
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
TYPE & Back() const
return reference to last element
Definition array.h:993
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1334
void EraseBack()
erase back
Definition array.h:1131
const SizeT Size() const
get number of elements in array
Definition array.h:880
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition pinnedarray.h:279
#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
uint32_t Id32
Definition id.h:138