RavEngine
Loading...
Searching...
No Matches
FreeList.hpp
1#pragma once
2#include <queue>
3
4namespace RGL {
5
6 template<typename index_t, uint32_t totalCount>
7 struct FreeList {
8
9 index_t Allocate() {
10 index_t allocatedIndex = 0;
11 if (freeList.empty()) {
12 if (nextFreeIndexNotInQueue == totalCount) {
13 // full -- cannot allocate!
14 throw std::out_of_range("Descriptor heap is full!");
15 }
16 // place the descriptor at the end
17 allocatedIndex = nextFreeIndexNotInQueue;
18 nextFreeIndexNotInQueue++;
19 }
20 else {
21 // fill the hole
22 allocatedIndex = freeList.front();
23 freeList.pop();
24 }
25
26 return allocatedIndex;
27 }
28
29 void Deallocate(index_t index) {
30 // if the index was the end, then decrement end
31 if (index == nextFreeIndexNotInQueue) {
32 nextFreeIndexNotInQueue--;
33 }
34 else {
35 // add the index to the free list
36 freeList.emplace(index);
37 }
38 }
39
40 private:
41 std::queue<index_t> freeList{};
42 index_t nextFreeIndexNotInQueue = 0; // __NOT__ one past the end
43 };
44
45}
Definition FreeList.hpp:7