-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferMap.cpp
More file actions
294 lines (248 loc) · 7.19 KB
/
BufferMap.cpp
File metadata and controls
294 lines (248 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "BufferMap.h"
#include <Engine/Systems/Memory/forge_memory_debugger.h>
#include <stdint.h>
static u32 AlignUp(u32 Value, u32 Alignment)
{
return (Value + Alignment - 1) & ~(Alignment - 1);
}
static void* AlignPointer(void* Ptr, u32 Alignment)
{
uintptr_t Addr = (uintptr_t)Ptr;
uintptr_t AlignedAddr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
return (void*)AlignedAddr;
}
void Systems_PoolAllocator_Init(PoolAllocator* Allocator, u64 InitialCapacity, u32 DataSize, u32 Alignment)
{
Allocator->FreeList = nullptr;
Allocator->FreeCount = 0;
Allocator->FreeCapacity = 0;
Allocator->Count = 0;
Allocator->capacity = InitialCapacity;
Allocator->DataSize = DataSize;
Allocator->Alignment = Alignment;
u32 Stride = AlignUp(DataSize, Allocator->Alignment);
u64 TotalSize = InitialCapacity * Stride + Allocator->Alignment - 1;
Allocator->RawData = calloc(1, TotalSize);
Allocator->Data = AlignPointer(Allocator->RawData, Allocator->Alignment);
}
void Systems_PoolAllocator_Free(PoolAllocator* Allocator, System_PoolAllocator_OnFreeDelegate OnFreeDelegate)
{
if (OnFreeDelegate != nullptr && Allocator->Data != nullptr)
{
u32 Stride = AlignUp(Allocator->DataSize, Allocator->Alignment);
for (u64 i = 0; i < Allocator->Count; ++i)
{
bool IsFree = false;
for (u64 j = 0; j < Allocator->FreeCount; ++j)
{
if (Allocator->FreeList[j] == (u32)i)
{
IsFree = true;
break;
}
}
if (!IsFree)
{
void* DataPtr = (char*)Allocator->Data + i * Stride;
OnFreeDelegate(DataPtr);
}
}
}
free(Allocator->RawData);
free(Allocator->FreeList);
}
u32 Systems_PoolAllocator_PushData(PoolAllocator* Allocator, const void* Data)
{
u32 Stride = AlignUp(Allocator->DataSize, Allocator->Alignment);
if (Allocator->FreeCount > 0)
{
u32 Index = Allocator->FreeList[--Allocator->FreeCount];
memcpy((char*)Allocator->Data + Index * Stride, Data, Allocator->DataSize);
return Index;
}
if (Allocator->Count >= Allocator->capacity)
{
Allocator->capacity *= 2;
u64 TotalSize = Allocator->capacity * Stride + Allocator->Alignment - 1;
void* NewRawData = calloc(1, TotalSize);
void* NewData = AlignPointer(NewRawData, Allocator->Alignment);
// Copy existing data
for (u64 i = 0; i < Allocator->Count; ++i)
{
memcpy((char*)NewData + i * Stride, (char*)Allocator->Data + i * Stride, Allocator->DataSize);
}
free(Allocator->RawData);
Allocator->RawData = NewRawData;
Allocator->Data = NewData;
}
memcpy((char*)Allocator->Data + Allocator->Count * Stride, Data, Allocator->DataSize);
return (u32)(Allocator->Count++);
}
void Systems_PoolAllocator_GetData(PoolAllocator* Allocator, u32 Index, void* OutData)
{
u32 Stride = AlignUp(Allocator->DataSize, Allocator->Alignment);
memcpy(OutData, (char*)Allocator->Data + Index * Stride, Allocator->DataSize);
}
void Systems_PoolAllocator_FreeData(PoolAllocator* Allocator, u32 Index)
{
if (Allocator->FreeCount >= Allocator->FreeCapacity)
{
Allocator->FreeCapacity = Allocator->FreeCapacity ? Allocator->FreeCapacity * 2 : 8;
Allocator->FreeList = (u32*)realloc(Allocator->FreeList, Allocator->FreeCapacity * sizeof(u32));
}
Allocator->FreeList[Allocator->FreeCount++] = Index;
}
// -----------------------------------------------------------------------------
// BufferMap implementation
static u64 HashVkHandle(u64 VkHandle)
{
VkHandle >>= 3; // Drop low bits if handles are aligned
VkHandle ^= VkHandle >> 30; VkHandle *= UINT64_C(0xBF58476D1CE4E5B9);
VkHandle ^= VkHandle >> 27; VkHandle *= UINT64_C(0x94D049BB133111EB);
VkHandle ^= VkHandle >> 31;
return VkHandle;
}
static inline u64 GetIndexFromHandleMask(u64 h, u64 Capacity)
{
assert((Capacity & (Capacity - 1)) == 0); // Power of two required
u64 Mask = Capacity - 1;
return HashVkHandle(h) & Mask;
}
static inline u64 NextIndex(u64 i, u64 Mask)
{
return (i + 1) & Mask;
}
static void SparceHashMap_Resize(SparceHashMap* Map)
{
u64 OldCapacity = Map->Capacity;
u64* OldKeys = Map->Keys;
u32* OldIndices = Map->Indices;
u8* OldProbe = Map->ProbeDist;
u8* OldOccupied = Map->Occupied;
u64 NewCapacity = OldCapacity * 2;
Systems_SparceHashMap_Init(Map, NewCapacity);
for (u64 i = 0; i < OldCapacity; ++i)
{
if (OldOccupied[i])
{
Systems_SparceHashMap_Insert(Map, OldKeys[i], OldIndices[i]);
}
}
free(OldKeys);
free(OldIndices);
free(OldProbe);
free(OldOccupied);
}
void Systems_SparceHashMap_Init(SparceHashMap* Map, u64 InitialCapacity)
{
assert((InitialCapacity & (InitialCapacity - 1)) == 0);
Map->Capacity = InitialCapacity;
Map->Count = 0;
Map->Keys = (u64*)calloc(InitialCapacity, sizeof(u64));
Map->Indices = (u32*)calloc(InitialCapacity, sizeof(u32));
Map->ProbeDist = (u8*)calloc(InitialCapacity, sizeof(u8));
Map->Occupied = (u8*)calloc(InitialCapacity, sizeof(u8));
}
void Systems_SparceHashMap_Free(SparceHashMap* Map)
{
free(Map->Keys);
free(Map->Indices);
free(Map->ProbeDist);
free(Map->Occupied);
}
void Systems_SparceHashMap_Insert(SparceHashMap* Map, u64 Key, u32 Index)
{
const float LoadFactor = 0.8f;
if (Map->Count + 1 > (u64)(Map->Capacity * LoadFactor))
{
SparceHashMap_Resize(Map);
}
u64 Mask = Map->Capacity - 1;
u64 i = GetIndexFromHandleMask(Key, Map->Capacity);
u8 Dist = 0;
while (true)
{
if (!Map->Occupied[i])
{
Map->Keys[i] = Key;
Map->Indices[i] = Index;
Map->ProbeDist[i] = Dist;
Map->Occupied[i] = 1;
Map->Count++;
return;
}
if (Map->Keys[i] == Key)
{
Map->Indices[i] = Index;
return;
}
if (Map->ProbeDist[i] < Dist)
{
u64 tmp_Key = Map->Keys[i];
u32 tmp_Index = Map->Indices[i];
u8 tmp_Dist = Map->ProbeDist[i];
Map->Keys[i] = Key;
Map->Indices[i] = Index;
Map->ProbeDist[i] = Dist;
Key = tmp_Key;
Index = tmp_Index;
Dist = tmp_Dist;
}
i = NextIndex(i, Mask);
Dist++;
}
}
bool Systems_SparceHashMap_Get(const SparceHashMap* Map, u64 Key, u32* OutIndex)
{
u64 Mask = Map->Capacity - 1;
u64 i = GetIndexFromHandleMask(Key, Map->Capacity);
u8 Dist = 0;
while (true)
{
if (!Map->Occupied[i] || Map->ProbeDist[i] < Dist)
{
return false;
}
if (Map->Keys[i] == Key)
{
*OutIndex = Map->Indices[i];
return true;
}
i = NextIndex(i, Mask);
Dist++;
}
}
bool Systems_SparceHashMap_Remove(SparceHashMap* Map, u64 Key, u32* OutIndex)
{
u64 Mask = Map->Capacity - 1;
u64 i = GetIndexFromHandleMask(Key, Map->Capacity);
u8 Dist = 0;
while (true)
{
if (!Map->Occupied[i] || Map->ProbeDist[i] < Dist)
{
return false;
}
if (Map->Keys[i] == Key)
{
*OutIndex = Map->Indices[i];
Map->Occupied[i] = 0;
// Backward-shift deletion
u64 j = NextIndex(i, Mask);
while (Map->Occupied[j] && Map->ProbeDist[j] > 0)
{
Map->Keys[i] = Map->Keys[j];
Map->Indices[i] = Map->Indices[j];
Map->ProbeDist[i] = Map->ProbeDist[j] - 1;
Map->Occupied[i] = 1;
Map->Occupied[j] = 0;
i = j;
j = NextIndex(j, Mask);
}
Map->Count--;
return true;
}
i = NextIndex(i, Mask);
Dist++;
}
}