forked from B-Y-P/4coder-community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4ed_dynamic_variables.cpp
531 lines (478 loc) · 20 KB
/
4ed_dynamic_variables.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* Mr. 4th Dimention - Allen Webster
*
* 22.06.2018
*
* Dynamic variable system
*
*/
// TOP
internal void
managed_ids_init(Base_Allocator *allocator, Managed_ID_Set *set){
set->arena = make_arena(allocator, KB(4), 8);
set->name_to_group_table = make_table_Data_u64(allocator, 20);
}
internal Managed_ID
managed_ids_group_highest_id(Managed_ID_Set *set, String_Const_u8 group_name){
Managed_ID result = 0;
String_Const_u8 data = make_data(group_name.str, group_name.size);
Table_Lookup lookup = table_lookup(&set->name_to_group_table, data);
if (lookup.found_match){
u64 val = 0;
table_read(&set->name_to_group_table, lookup, &val);
Managed_ID_Group *group = (Managed_ID_Group*)IntAsPtr(val);
result = group->id_counter - 1;
}
return(result);
}
internal Managed_ID
managed_ids_declare(Managed_ID_Set *set, String_Const_u8 group_name, String_Const_u8 name){
Managed_ID_Group *group = 0;
{
String_Const_u8 data = make_data(group_name.str, group_name.size);
Table_Lookup lookup = table_lookup(&set->name_to_group_table, data);
if (lookup.found_match){
u64 val = 0;
table_read(&set->name_to_group_table, lookup, &val);
group = (Managed_ID_Group*)IntAsPtr(val);
}
else{
group = push_array(&set->arena, Managed_ID_Group, 1);
group->id_counter = 1;
group->name_to_id_table = make_table_Data_u64(set->arena.base_allocator, 50);
data = push_data_copy(&set->arena, data);
table_insert(&set->name_to_group_table, data, PtrAsInt(group));
}
}
Managed_ID result = 0;
{
String_Const_u8 data = make_data(name.str, name.size);
Table_Lookup lookup = table_lookup(&group->name_to_id_table, data);
if (lookup.found_match){
table_read(&group->name_to_id_table, lookup, &result);
}
else{
result = group->id_counter;
group->id_counter += 1;
data = push_data_copy(&set->arena, data);
table_insert(&group->name_to_id_table, data, result);
}
}
return(result);
}
function Managed_ID
managed_ids_get(Managed_ID_Set *set, String_Const_u8 group_name, String_Const_u8 name){
Managed_ID_Group *group = 0;
{
String_Const_u8 data = make_data(group_name.str, group_name.size);
Table_Lookup lookup = table_lookup(&set->name_to_group_table, data);
if (lookup.found_match){
u64 val = 0;
table_read(&set->name_to_group_table, lookup, &val);
group = (Managed_ID_Group*)IntAsPtr(val);
}
}
Managed_ID result = 0;
if (group != 0){
String_Const_u8 data = make_data(name.str, name.size);
Table_Lookup lookup = table_lookup(&group->name_to_id_table, data);
if (lookup.found_match){
table_read(&group->name_to_id_table, lookup, &result);
}
}
return(result);
}
////////////////////////////////
internal void
dynamic_variable_block_init(Base_Allocator *allocator, Dynamic_Variable_Block *block){
block->arena = make_arena(allocator, KB(4), 8);
block->id_to_data_table = make_table_u64_Data(allocator, 20);
}
internal String_Const_u8
dynamic_variable_get(Dynamic_Variable_Block *block, Managed_ID id, u64 size){
String_Const_u8 result = {};
Table_Lookup lookup = table_lookup(&block->id_to_data_table, id);
if (lookup.found_match){
table_read(&block->id_to_data_table, lookup, &result);
}
else{
result = push_data(&block->arena, size);
block_zero(result);
table_insert(&block->id_to_data_table, id, result);
}
return(result);
}
internal void
dynamic_variable_erase(Dynamic_Variable_Block *block, Managed_ID id){
table_erase(&block->id_to_data_table, id);
}
////////////////////////////////
internal void
lifetime_allocator_init(Base_Allocator *base_allocator, Lifetime_Allocator *lifetime_allocator){
block_zero_struct(lifetime_allocator);
lifetime_allocator->allocator = base_allocator;
lifetime_allocator->node_arena = make_arena(base_allocator, KB(4));
lifetime_allocator->key_table = make_table_Data_u64(base_allocator, 100);
lifetime_allocator->key_check_table = make_table_u64_u64(base_allocator, 100);
lifetime_allocator->scope_id_to_scope_ptr_table = make_table_u64_u64(base_allocator, 100);
}
////////////////////////////////
internal void
dynamic_workspace_init(Lifetime_Allocator *lifetime_allocator, i32 user_type, void *user_back_ptr, Dynamic_Workspace *workspace){
block_zero_struct(workspace);
heap_init(&workspace->heap, lifetime_allocator->allocator);
workspace->heap_wrapper = base_allocator_on_heap(&workspace->heap);
workspace->object_id_to_object_ptr = make_table_u64_u64(&workspace->heap_wrapper, 10);
dynamic_variable_block_init(&workspace->heap_wrapper, &workspace->var_block);
if (lifetime_allocator->scope_id_counter == 0){
lifetime_allocator->scope_id_counter = 1;
}
workspace->scope_id = lifetime_allocator->scope_id_counter++;
table_insert(&lifetime_allocator->scope_id_to_scope_ptr_table,
workspace->scope_id, (u64)PtrAsInt(workspace));
workspace->user_type = user_type;
workspace->user_back_ptr = user_back_ptr;
}
internal void
dynamic_workspace_free(Lifetime_Allocator *lifetime_allocator, Dynamic_Workspace *workspace){
table_erase(&lifetime_allocator->scope_id_to_scope_ptr_table, workspace->scope_id);
heap_free_all(&workspace->heap);
}
internal void
dynamic_workspace_clear_contents(Dynamic_Workspace *workspace){
Base_Allocator *base_allocator = heap_get_base_allocator(&workspace->heap);
heap_free_all(&workspace->heap);
heap_init(&workspace->heap, base_allocator);
workspace->heap_wrapper = base_allocator_on_heap(&workspace->heap);
workspace->object_id_to_object_ptr = make_table_u64_u64(&workspace->heap_wrapper, 10);
dynamic_variable_block_init(&workspace->heap_wrapper, &workspace->var_block);
block_zero_struct(&workspace->buffer_markers_list);
workspace->total_marker_count = 0;
}
internal u32
dynamic_workspace_store_pointer(Dynamic_Workspace *workspace, void *ptr){
if (workspace->object_id_counter == 0){
workspace->object_id_counter = 1;
}
u32 id = workspace->object_id_counter++;
table_insert(&workspace->object_id_to_object_ptr, id, (u64)PtrAsInt(ptr));
return(id);
}
internal void
dynamic_workspace_erase_pointer(Dynamic_Workspace *workspace, u32 id){
table_erase(&workspace->object_id_to_object_ptr, id);
}
internal void*
dynamic_workspace_get_pointer(Dynamic_Workspace *workspace, u32 id){
void *result = 0;
Table_Lookup lookup = table_lookup(&workspace->object_id_to_object_ptr, id);
if (lookup.found_match){
u64 val = 0;
table_read(&workspace->object_id_to_object_ptr, lookup, &val);
result = IntAsPtr(val);
}
return(result);
}
////////////////////////////////
internal String_Const_u8
lifetime__key_as_data(Lifetime_Object **members, i32 count){
return(make_data(members, sizeof(*members)*count));
}
internal String_Const_u8
lifetime__key_as_data(Lifetime_Key *key){
return(lifetime__key_as_data(key->members, key->count));
}
internal void
lifetime__free_key(Lifetime_Allocator *lifetime_allocator, Lifetime_Key *key, Lifetime_Object *skip_object){
// Deinit
dynamic_workspace_free(lifetime_allocator, &key->dynamic_workspace);
// Remove From Objects
i32 count = key->count;
Lifetime_Object **object_ptr = key->members;
for (i32 i = 0; i < count; i += 1, object_ptr += 1){
if (*object_ptr == skip_object) continue;
Lifetime_Key_Ref_Node *delete_point_node = 0;
i32 delete_point_i = 0;
i32 key_i = 0;
Lifetime_Object *object = *object_ptr;
for (Lifetime_Key_Ref_Node *node = object->key_node_first;
node != 0;
node = node->next){
i32 one_past_last = clamp_top(ArrayCount(node->keys), object->key_count - key_i);
for (i32 j = 0; j < one_past_last; j += 1){
if (node->keys[j] == key){
delete_point_node = node;
delete_point_i = j;
goto double_break;
}
}
key_i += one_past_last;
}
double_break:;
Assert(delete_point_node != 0);
Lifetime_Key_Ref_Node *last_node = object->key_node_last;
Lifetime_Key *last_key = last_node->keys[(object->key_count - 1) % ArrayCount(last_node->keys)];
Assert(last_key != 0);
delete_point_node->keys[delete_point_i] = last_key;
object->key_count -= 1;
if ((object->key_count % lifetime_key_reference_per_node) == 0){
zdll_remove(object->key_node_first, object->key_node_last, last_node);
sll_stack_push(lifetime_allocator->free_key_references, last_node);
}
}
// Free
String_Const_u8 key_data = lifetime__key_as_data(key);
table_erase(&lifetime_allocator->key_table, key_data);
table_erase(&lifetime_allocator->key_check_table, (u64)PtrAsInt(key));
base_free(lifetime_allocator->allocator, key->members);
sll_stack_push(lifetime_allocator->free_keys, key);
}
internal Lifetime_Key_Ref_Node*
lifetime__alloc_key_reference_node(Lifetime_Allocator *lifetime_allocator){
Assert(lifetime_allocator != 0);
Lifetime_Key_Ref_Node *result = lifetime_allocator->free_key_references;
if (result == 0){
result = push_array(&lifetime_allocator->node_arena, Lifetime_Key_Ref_Node, 1);
}
else{
sll_stack_pop(lifetime_allocator->free_key_references);
}
return(result);
}
internal void
lifetime__object_add_key(Lifetime_Allocator *lifetime_allocator, Lifetime_Object *object, Lifetime_Key *key){
Lifetime_Key_Ref_Node *last_node = object->key_node_last;
b32 insert_on_new_node = false;
if (last_node == 0){
insert_on_new_node = true;
}
else{
i32 next_insert_slot = object->key_count%ArrayCount(last_node->keys);
if (next_insert_slot != 0){
last_node->keys[next_insert_slot] = key;
object->key_count += 1;
}
else{
insert_on_new_node = true;
}
}
if (insert_on_new_node){
Lifetime_Key_Ref_Node *new_node = lifetime__alloc_key_reference_node(lifetime_allocator);
zdll_push_back(object->key_node_first, object->key_node_last, new_node);
block_zero_struct(new_node->keys);
new_node->keys[0] = key;
object->key_count += 1;
}
}
internal Lifetime_Object*
lifetime_alloc_object(Lifetime_Allocator *lifetime_allocator, i32 user_type, void *user_back_ptr){
Lifetime_Object *object = lifetime_allocator->free_objects;
if (object == 0){
object = push_array(&lifetime_allocator->node_arena, Lifetime_Object, 1);
}
else{
sll_stack_pop(lifetime_allocator->free_objects);
}
block_zero_struct(object);
dynamic_workspace_init(lifetime_allocator, user_type, user_back_ptr, &object->workspace);
return(object);
}
internal void
lifetime__object_free_all_keys(Lifetime_Allocator *lifetime_allocator, Lifetime_Object *lifetime_object){
i32 key_i = 0;
for (Lifetime_Key_Ref_Node *node = lifetime_object->key_node_first;
node != 0;
node = node->next){
i32 one_past_last = clamp_top(ArrayCount(node->keys), lifetime_object->key_count - key_i);
for (i32 i = 0; i < one_past_last; i += 1){
lifetime__free_key(lifetime_allocator, node->keys[i], lifetime_object);
}
key_i += one_past_last;
}
if (lifetime_object->key_count > 0){
lifetime_object->key_node_last->next = lifetime_allocator->free_key_references;
lifetime_allocator->free_key_references = lifetime_object->key_node_first;
}
}
internal void
lifetime__object_clear_all_keys(Lifetime_Allocator *lifetime_allocator, Lifetime_Object *lifetime_object){
i32 key_i = 0;
for (Lifetime_Key_Ref_Node *node = lifetime_object->key_node_first;
node != 0;
node = node->next){
i32 one_past_last = clamp_top(ArrayCount(node->keys), lifetime_object->key_count - key_i);
Lifetime_Key **key_ptr = node->keys;
for (i32 i = 0; i < one_past_last; i += 1, key_ptr += 1){
dynamic_workspace_clear_contents(&(*key_ptr)->dynamic_workspace);
}
key_i += one_past_last;
}
}
internal void
lifetime_free_object(Lifetime_Allocator *lifetime_allocator, Lifetime_Object *lifetime_object){
lifetime__object_free_all_keys(lifetime_allocator, lifetime_object);
dynamic_workspace_free(lifetime_allocator, &lifetime_object->workspace);
sll_stack_push(lifetime_allocator->free_objects, lifetime_object);
}
internal void
lifetime_object_reset(Lifetime_Allocator *lifetime_allocator, Lifetime_Object *lifetime_object){
lifetime__object_clear_all_keys(lifetime_allocator, lifetime_object);
dynamic_workspace_clear_contents(&lifetime_object->workspace);
}
internal i32
lifetime_sort_object_set__part(Lifetime_Object **ptr_array, i32 first, i32 one_past_last){
i32 pivot_index = one_past_last - 1;
Lifetime_Object *pivot = ptr_array[pivot_index];
i32 j = first;
for (i32 i = first; i < pivot_index; i += 1){
Lifetime_Object *object = ptr_array[i];
if (object < pivot){
Swap(Lifetime_Object*, ptr_array[i], ptr_array[j]);
j += 1;
}
}
Swap(Lifetime_Object*, ptr_array[j], ptr_array[pivot_index]);
return(j);
}
internal void
lifetime_sort_object_set__quick(Lifetime_Object **ptr_array, i32 first, i32 one_past_last){
if (first + 1 < one_past_last){
i32 pivot = lifetime_sort_object_set__part(ptr_array, first, one_past_last);
lifetime_sort_object_set__quick(ptr_array, first, pivot);
lifetime_sort_object_set__quick(ptr_array, pivot + 1, one_past_last);
}
}
internal i32
lifetime_sort_and_dedup_object_set(Lifetime_Object **ptr_array, i32 count){
lifetime_sort_object_set__quick(ptr_array, 0, count);
Lifetime_Object **ptr_write = ptr_array + 1;
Lifetime_Object **ptr_read = ptr_array + 1;
for (i32 i = 1; i < count; i += 1, ptr_read += 1){
if (ptr_read[-1] < ptr_read[0]){
*ptr_write = *ptr_read;
ptr_write += 1;
}
}
return((i32)(ptr_write - ptr_array));
}
internal Lifetime_Key*
lifetime_get_or_create_intersection_key(Lifetime_Allocator *lifetime_allocator, Lifetime_Object **object_ptr_array, i32 count){
{
String_Const_u8 key_data = lifetime__key_as_data(object_ptr_array, count);
Table_Lookup lookup = table_lookup(&lifetime_allocator->key_table, key_data);
if (lookup.found_match){
u64 val = 0;
table_read(&lifetime_allocator->key_table, lookup, &val);
return((Lifetime_Key*)IntAsPtr(val));
}
}
// Allocate
Lifetime_Key *new_key = lifetime_allocator->free_keys;
if (new_key == 0){
new_key = push_array(&lifetime_allocator->node_arena, Lifetime_Key, 1);
}
else{
sll_stack_pop(lifetime_allocator->free_keys);
}
block_zero_struct(new_key);
// Add to Objects
Lifetime_Object **object_ptr = object_ptr_array;
for (i32 i = 0; i < count; i += 1, object_ptr += 1){
Lifetime_Object *object = *object_ptr;
lifetime__object_add_key(lifetime_allocator, object, new_key);
}
// Initialize
u64 new_memory_size = sizeof(Lifetime_Object*)*count;
String_Const_u8 new_memory = base_allocate(lifetime_allocator->allocator, new_memory_size);
new_key->members = (Lifetime_Object**)new_memory.str;
block_copy_dynamic_array(new_key->members, object_ptr_array, count);
new_key->count = count;
dynamic_workspace_init(lifetime_allocator,
DynamicWorkspace_Intersected, new_key,
&new_key->dynamic_workspace);
{
String_Const_u8 key_data = lifetime__key_as_data(new_key);
u64 new_key_val = (u64)PtrAsInt(new_key);
table_insert(&lifetime_allocator->key_table, key_data, new_key_val);
table_insert(&lifetime_allocator->key_check_table, new_key_val, new_key_val);
}
return(new_key);
}
internal b32
lifetime_key_check(Lifetime_Allocator *lifetime_allocator, Lifetime_Key *key){
Table_Lookup lookup = table_lookup(&lifetime_allocator->key_check_table, (u64)PtrAsInt(key));
return(lookup.found_match);
}
////////////////////////////////
// TODO(allen): move this shit somewhere real, clean up all object creation functions to be more cleanly layered.
internal u8*
get_dynamic_object_memory_ptr(Managed_Object_Standard_Header *header){
u8 *ptr = 0;
if (header != 0){
switch (header->type){
case ManagedObjectType_Memory:
case ManagedObjectType_Markers:
{
ptr = ((u8*)header) + managed_header_type_sizes[header->type];
}break;
}
}
return(ptr);
}
internal Managed_Object
managed_object_alloc_managed_memory(Dynamic_Workspace *workspace, i32 item_size, i32 count, void **ptr_out){
i32 size = item_size*count;
String_Const_u8 new_memory = base_allocate(&workspace->heap_wrapper, sizeof(Managed_Memory_Header) + size);
void *ptr = new_memory.str;
Managed_Memory_Header *header = (Managed_Memory_Header*)ptr;
header->std_header.type = ManagedObjectType_Memory;
header->std_header.item_size = item_size;
header->std_header.count = count;
if (ptr_out != 0){
*ptr_out = get_dynamic_object_memory_ptr(&header->std_header);
}
u32 id = dynamic_workspace_store_pointer(workspace, ptr);
return(((u64)workspace->scope_id << 32) | (u64)id);
}
internal Managed_Object
managed_object_alloc_buffer_markers(Dynamic_Workspace *workspace, Buffer_ID buffer_id, i32 count, Marker **markers_out){
i32 size = count*sizeof(Marker);
String_Const_u8 new_memory = base_allocate(&workspace->heap_wrapper, size + sizeof(Managed_Buffer_Markers_Header));
void *ptr = new_memory.str;
Managed_Buffer_Markers_Header *header = (Managed_Buffer_Markers_Header*)ptr;
header->std_header.type = ManagedObjectType_Markers;
header->std_header.item_size = sizeof(Marker);
header->std_header.count = count;
zdll_push_back(workspace->buffer_markers_list.first, workspace->buffer_markers_list.last, header);
workspace->buffer_markers_list.count += 1;
workspace->total_marker_count += count;
header->buffer_id = buffer_id;
if (markers_out != 0){
*markers_out = (Marker*)get_dynamic_object_memory_ptr(&header->std_header);
}
u32 id = dynamic_workspace_store_pointer(workspace, ptr);
return(((u64)workspace->scope_id << 32) | (u64)id);
}
internal b32
managed_object_free(Dynamic_Workspace *workspace, Managed_Object object){
b32 result = false;
u32 lo_id = object&max_u32;
u8 *object_ptr = (u8*)dynamic_workspace_get_pointer(workspace, lo_id);
if (object_ptr != 0){
Managed_Object_Type *type = (Managed_Object_Type*)object_ptr;
switch (*type){
case ManagedObjectType_Markers:
{
Managed_Buffer_Markers_Header *header = (Managed_Buffer_Markers_Header*)object_ptr;
workspace->total_marker_count -= header->std_header.count;
zdll_remove(workspace->buffer_markers_list.first, workspace->buffer_markers_list.last, header);
workspace->buffer_markers_list.count -= 1;
}break;
}
dynamic_workspace_erase_pointer(workspace, lo_id);
base_free(&workspace->heap_wrapper, object_ptr);
result = true;
}
return(result);
}
// BOTTOM