forked from SanderMertens/flecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id_record.c
631 lines (545 loc) · 18.4 KB
/
id_record.c
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/**
* @file id_record.c
* @brief Index for looking up tables by (component) id.
*
* An id record stores the administration for an in use (component) id, that is
* an id that has been used in tables.
*
* An id record contains a table cache, which stores the list of tables that
* have the id. Each entry in the cache (a table record) stores the first
* occurrence of the id in the table and the number of occurrences of the id in
* the table (in the case of wildcard ids).
*
* Id records are used in lots of scenarios, like uncached queries, or for
* getting a component array/component for an entity.
*/
#include "private_api.h"
static
ecs_id_record_elem_t* flecs_id_record_elem(
ecs_id_record_t *head,
ecs_id_record_elem_t *list,
ecs_id_record_t *idr)
{
return ECS_OFFSET(idr, (uintptr_t)list - (uintptr_t)head);
}
static
void flecs_id_record_elem_insert(
ecs_id_record_t *head,
ecs_id_record_t *idr,
ecs_id_record_elem_t *elem)
{
ecs_id_record_elem_t *head_elem = flecs_id_record_elem(idr, elem, head);
ecs_id_record_t *cur = head_elem->next;
elem->next = cur;
elem->prev = head;
if (cur) {
ecs_id_record_elem_t *cur_elem = flecs_id_record_elem(idr, elem, cur);
cur_elem->prev = idr;
}
head_elem->next = idr;
}
static
void flecs_id_record_elem_remove(
ecs_id_record_t *idr,
ecs_id_record_elem_t *elem)
{
ecs_id_record_t *prev = elem->prev;
ecs_id_record_t *next = elem->next;
ecs_assert(prev != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_id_record_elem_t *prev_elem = flecs_id_record_elem(idr, elem, prev);
prev_elem->next = next;
if (next) {
ecs_id_record_elem_t *next_elem = flecs_id_record_elem(idr, elem, next);
next_elem->prev = prev;
}
}
static
void flecs_insert_id_elem(
ecs_world_t *world,
ecs_id_record_t *idr,
ecs_id_t wildcard,
ecs_id_record_t *widr)
{
ecs_assert(ecs_id_is_wildcard(wildcard), ECS_INTERNAL_ERROR, NULL);
if (!widr) {
widr = flecs_id_record_ensure(world, wildcard);
}
ecs_assert(widr != NULL, ECS_INTERNAL_ERROR, NULL);
if (ECS_PAIR_SECOND(wildcard) == EcsWildcard) {
ecs_assert(ECS_PAIR_FIRST(wildcard) != EcsWildcard,
ECS_INTERNAL_ERROR, NULL);
flecs_id_record_elem_insert(widr, idr, &idr->first);
} else {
ecs_assert(ECS_PAIR_FIRST(wildcard) == EcsWildcard,
ECS_INTERNAL_ERROR, NULL);
flecs_id_record_elem_insert(widr, idr, &idr->second);
if (idr->flags & EcsIdTraversable) {
flecs_id_record_elem_insert(widr, idr, &idr->trav);
}
}
}
static
void flecs_remove_id_elem(
ecs_id_record_t *idr,
ecs_id_t wildcard)
{
ecs_assert(ecs_id_is_wildcard(wildcard), ECS_INTERNAL_ERROR, NULL);
if (ECS_PAIR_SECOND(wildcard) == EcsWildcard) {
ecs_assert(ECS_PAIR_FIRST(wildcard) != EcsWildcard,
ECS_INTERNAL_ERROR, NULL);
flecs_id_record_elem_remove(idr, &idr->first);
} else {
ecs_assert(ECS_PAIR_FIRST(wildcard) == EcsWildcard,
ECS_INTERNAL_ERROR, NULL);
flecs_id_record_elem_remove(idr, &idr->second);
if (idr->flags & EcsIdTraversable) {
flecs_id_record_elem_remove(idr, &idr->trav);
}
}
}
static
ecs_id_t flecs_id_record_hash(
ecs_id_t id)
{
id = ecs_strip_generation(id);
if (ECS_IS_PAIR(id)) {
ecs_entity_t r = ECS_PAIR_FIRST(id);
ecs_entity_t o = ECS_PAIR_SECOND(id);
if (r == EcsAny) {
r = EcsWildcard;
}
if (o == EcsAny) {
o = EcsWildcard;
}
id = ecs_pair(r, o);
}
return id;
}
static
ecs_id_record_t* flecs_id_record_new(
ecs_world_t *world,
ecs_id_t id)
{
ecs_id_record_t *idr, *idr_t = NULL;
ecs_id_t hash = flecs_id_record_hash(id);
if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = flecs_bcalloc(&world->allocators.id_record);
ecs_map_insert_ptr(&world->id_index_hi, hash, idr);
} else {
idr = &world->id_index_lo[hash];
ecs_os_zeromem(idr);
}
ecs_table_cache_init(world, &idr->cache);
idr->id = id;
idr->refcount = 1;
idr->reachable.current = -1;
bool is_wildcard = ecs_id_is_wildcard(id);
ecs_entity_t rel = 0, tgt = 0, role = id & ECS_ID_FLAGS_MASK;
if (ECS_HAS_ID_FLAG(id, PAIR)) {
rel = ecs_pair_first(world, id);
ecs_assert(rel != 0, ECS_INTERNAL_ERROR, NULL);
/* Relationship object can be 0, as tables without a ChildOf
* relationship are added to the (ChildOf, 0) id record */
tgt = ECS_PAIR_SECOND(id);
if (tgt) {
tgt = ecs_get_alive(world, tgt);
ecs_assert(tgt != 0, ECS_INTERNAL_ERROR, NULL);
}
/* Check constraints */
if (tgt && !ecs_id_is_wildcard(tgt)) {
/* Check if target of relationship satisfies OneOf property */
ecs_entity_t oneof = flecs_get_oneof(world, rel);
ecs_check( !oneof || ecs_has_pair(world, tgt, EcsChildOf, oneof),
ECS_CONSTRAINT_VIOLATED, NULL);
(void)oneof;
/* Check if we're not trying to inherit from a final target */
if (rel == EcsIsA) {
bool is_final = ecs_has_id(world, tgt, EcsFinal);
ecs_check(!is_final, ECS_CONSTRAINT_VIOLATED,
"cannot inherit from final entity");
(void)is_final;
}
}
if (!is_wildcard && ECS_IS_PAIR(id) && (rel != EcsFlag)) {
/* Inherit flags from (relationship, *) record */
ecs_id_record_t *idr_r = flecs_id_record_ensure(
world, ecs_pair(rel, EcsWildcard));
idr->parent = idr_r;
idr->flags = idr_r->flags;
/* If pair is not a wildcard, append it to wildcard lists. These
* allow for quickly enumerating all relationships for an object,
* or all objecs for a relationship. */
flecs_insert_id_elem(world, idr, ecs_pair(rel, EcsWildcard), idr_r);
idr_t = flecs_id_record_ensure(world, ecs_pair(EcsWildcard, tgt));
flecs_insert_id_elem(world, idr, ecs_pair(EcsWildcard, tgt), idr_t);
if (rel == EcsUnion) {
idr->flags |= EcsIdUnion;
}
}
} else {
rel = id & ECS_COMPONENT_MASK;
rel = ecs_get_alive(world, rel);
ecs_assert(rel != 0, ECS_INTERNAL_ERROR, NULL);
}
/* Initialize type info if id is not a tag */
if (!is_wildcard && (!role || ECS_IS_PAIR(id))) {
if (!(idr->flags & EcsIdTag)) {
const ecs_type_info_t *ti = flecs_type_info_get(world, rel);
if (!ti && tgt) {
ti = flecs_type_info_get(world, tgt);
}
idr->type_info = ti;
}
}
/* Mark entities that are used as component/pair ids. When a tracked
* entity is deleted, cleanup policies are applied so that the store
* won't contain any tables with deleted ids. */
/* Flag for OnDelete policies */
flecs_add_flag(world, rel, EcsEntityIsId);
if (tgt) {
/* Flag for OnDeleteTarget policies */
flecs_add_flag(world, tgt, EcsEntityIsTarget);
if (idr->flags & EcsIdTraversable) {
/* Flag used to determine if object should be traversed when
* propagating events or with super/subset queries */
ecs_record_t *r = flecs_add_flag(
world, tgt, EcsEntityIsTraversable);
/* Add reference to (*, tgt) id record to entity record */
r->idr = idr_t;
}
}
/* Flags for events */
if (flecs_check_observers_for_event(world, id, EcsOnAdd)) {
idr->flags |= EcsIdHasOnAdd;
}
if (flecs_check_observers_for_event(world, id, EcsOnRemove)) {
idr->flags |= EcsIdHasOnRemove;
}
if (flecs_check_observers_for_event(world, id, EcsOnSet)) {
idr->flags |= EcsIdHasOnSet;
}
if (flecs_check_observers_for_event(world, id, EcsUnSet)) {
idr->flags |= EcsIdHasUnSet;
}
if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
ecs_dbg_1("#[green]id#[normal] %s #[green]created", id_str);
ecs_os_free(id_str);
}
/* Update counters */
world->info.id_create_total ++;
if (!is_wildcard) {
world->info.id_count ++;
if (idr->type_info) {
world->info.component_id_count ++;
} else {
world->info.tag_id_count ++;
}
if (ECS_IS_PAIR(id)) {
world->info.pair_id_count ++;
}
} else {
world->info.wildcard_id_count ++;
}
return idr;
error:
return NULL;
}
static
void flecs_id_record_assert_empty(
ecs_id_record_t *idr)
{
(void)idr;
ecs_assert(flecs_table_cache_count(&idr->cache) == 0,
ECS_INTERNAL_ERROR, NULL);
ecs_assert(flecs_table_cache_empty_count(&idr->cache) == 0,
ECS_INTERNAL_ERROR, NULL);
}
static
void flecs_id_record_free(
ecs_world_t *world,
ecs_id_record_t *idr)
{
ecs_poly_assert(world, ecs_world_t);
ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_id_t id = idr->id;
flecs_id_record_assert_empty(idr);
if (!(world->flags & EcsWorldQuit)) {
/* Id is still in use by a filter, query, rule or observer */
ecs_assert((idr->keep_alive == 0),
ECS_ID_IN_USE, "cannot delete id that is queried for");
}
if (ECS_IS_PAIR(id)) {
ecs_entity_t rel = ecs_pair_first(world, id);
ecs_entity_t obj = ECS_PAIR_SECOND(id);
if (!ecs_id_is_wildcard(id)) {
if (ECS_PAIR_FIRST(id) != EcsFlag) {
/* If id is not a wildcard, remove it from the wildcard lists */
flecs_remove_id_elem(idr, ecs_pair(rel, EcsWildcard));
flecs_remove_id_elem(idr, ecs_pair(EcsWildcard, obj));
}
} else {
ecs_log_push_2();
/* If id is a wildcard, it means that all id records that match the
* wildcard are also empty, so release them */
if (ECS_PAIR_FIRST(id) == EcsWildcard) {
/* Iterate (*, Target) list */
ecs_id_record_t *cur, *next = idr->second.next;
while ((cur = next)) {
flecs_id_record_assert_empty(cur);
next = cur->second.next;
flecs_id_record_release(world, cur);
}
} else {
/* Iterate (Relationship, *) list */
ecs_assert(ECS_PAIR_SECOND(id) == EcsWildcard,
ECS_INTERNAL_ERROR, NULL);
ecs_id_record_t *cur, *next = idr->first.next;
while ((cur = next)) {
flecs_id_record_assert_empty(cur);
next = cur->first.next;
flecs_id_record_release(world, cur);
}
}
ecs_log_pop_2();
}
}
/* Update counters */
world->info.id_delete_total ++;
if (!ecs_id_is_wildcard(id)) {
world->info.id_count --;
if (ECS_IS_PAIR(id)) {
world->info.pair_id_count --;
}
if (idr->type_info) {
world->info.component_id_count --;
} else {
world->info.tag_id_count --;
}
} else {
world->info.wildcard_id_count --;
}
/* Unregister the id record from the world & free resources */
ecs_table_cache_fini(&idr->cache);
flecs_name_index_free(idr->name_index);
ecs_vec_fini_t(&world->allocator, &idr->reachable.ids, ecs_reachable_elem_t);
ecs_id_t hash = flecs_id_record_hash(id);
if (hash >= FLECS_HI_ID_RECORD_ID) {
ecs_map_remove(&world->id_index_hi, hash);
flecs_bfree(&world->allocators.id_record, idr);
} else {
idr->id = 0; /* Tombstone */
}
if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
ecs_dbg_1("#[green]id#[normal] %s #[red]deleted", id_str);
ecs_os_free(id_str);
}
}
ecs_id_record_t* flecs_id_record_ensure(
ecs_world_t *world,
ecs_id_t id)
{
ecs_id_record_t *idr = flecs_id_record_get(world, id);
if (!idr) {
idr = flecs_id_record_new(world, id);
}
return idr;
}
ecs_id_record_t* flecs_id_record_get(
const ecs_world_t *world,
ecs_id_t id)
{
ecs_poly_assert(world, ecs_world_t);
if (id == ecs_pair(EcsIsA, EcsWildcard)) {
return world->idr_isa_wildcard;
} else if (id == ecs_pair(EcsChildOf, EcsWildcard)) {
return world->idr_childof_wildcard;
} else if (id == ecs_pair_t(EcsIdentifier, EcsName)) {
return world->idr_identifier_name;
}
ecs_id_t hash = flecs_id_record_hash(id);
ecs_id_record_t *idr = NULL;
if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = ecs_map_get_deref(&world->id_index_hi, ecs_id_record_t, hash);
} else {
idr = &world->id_index_lo[hash];
if (!idr->id) {
idr = NULL;
}
}
return idr;
}
ecs_id_record_t* flecs_query_id_record_get(
const ecs_world_t *world,
ecs_id_t id)
{
ecs_id_record_t *idr = flecs_id_record_get(world, id);
if (!idr) {
ecs_entity_t first = ECS_PAIR_FIRST(id);
if (ECS_IS_PAIR(id) && (first != EcsWildcard)) {
idr = flecs_id_record_get(world, ecs_pair(EcsUnion, first));
}
return idr;
}
if (ECS_IS_PAIR(id) &&
ECS_PAIR_SECOND(id) == EcsWildcard &&
(idr->flags & EcsIdUnion))
{
idr = flecs_id_record_get(world,
ecs_pair(EcsUnion, ECS_PAIR_FIRST(id)));
}
return idr;
}
void flecs_id_record_claim(
ecs_world_t *world,
ecs_id_record_t *idr)
{
(void)world;
//if (idr->id == 9223374343252216866ull) printf("claim idr ptr %llu id: %llu (refcount: %llu)\n", idr, idr->id, idr->refcount);
idr->refcount ++;
}
int32_t flecs_id_record_release(
ecs_world_t *world,
ecs_id_record_t *idr)
{
//if (idr->id == 9223374343252216866ull) printf("release idr ptr %llu; id: %llu (refcount: %llu)\n", idr, idr->id, idr->refcount);
int32_t rc = -- idr->refcount;
ecs_assert(rc >= 0, ECS_INTERNAL_ERROR, NULL);
if (!rc) {
flecs_id_record_free(world, idr);
}
return rc;
}
void flecs_id_record_release_tables(
ecs_world_t *world,
ecs_id_record_t *idr)
{
/* Cache should not contain tables that aren't empty */
ecs_assert(flecs_table_cache_count(&idr->cache) == 0,
ECS_INTERNAL_ERROR, NULL);
ecs_table_cache_iter_t it;
if (flecs_table_cache_empty_iter(&idr->cache, &it)) {
ecs_table_record_t *tr;
while ((tr = flecs_table_cache_next(&it, ecs_table_record_t))) {
/* Tables can hold claims on each other, so releasing a table can
* cause the next element to get invalidated. Claim the next table
* so that we can safely iterate. */
ecs_table_t *next = NULL;
if (it.next) {
next = it.next->table;
flecs_table_claim(world, next);
}
/* Release current table */
flecs_table_release(world, tr->hdr.table);
/* Check if the only thing keeping the next table alive is our
* claim. If so, move to the next record before releasing */
if (next) {
if (next->refcount == 1) {
it.next = it.next->next;
}
flecs_table_release(world, next);
}
}
}
}
bool flecs_id_record_set_type_info(
ecs_world_t *world,
ecs_id_record_t *idr,
const ecs_type_info_t *ti)
{
bool is_wildcard = ecs_id_is_wildcard(idr->id);
if (!is_wildcard) {
if (ti) {
if (!idr->type_info) {
world->info.tag_id_count --;
world->info.component_id_count ++;
}
} else {
if (idr->type_info) {
world->info.tag_id_count ++;
world->info.component_id_count --;
}
}
}
bool changed = idr->type_info != ti;
idr->type_info = ti;
return changed;
}
ecs_hashmap_t* flecs_id_name_index_ensure(
ecs_world_t *world,
ecs_id_t id)
{
ecs_poly_assert(world, ecs_world_t);
ecs_id_record_t *idr = flecs_id_record_get(world, id);
ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL);
ecs_hashmap_t *map = idr->name_index;
if (!map) {
map = idr->name_index = flecs_name_index_new(world, &world->allocator);
}
return map;
}
ecs_hashmap_t* flecs_id_name_index_get(
const ecs_world_t *world,
ecs_id_t id)
{
ecs_poly_assert(world, ecs_world_t);
ecs_id_record_t *idr = flecs_id_record_get(world, id);
if (!idr) {
return NULL;
}
return idr->name_index;
}
ecs_table_record_t* flecs_table_record_get(
const ecs_world_t *world,
const ecs_table_t *table,
ecs_id_t id)
{
ecs_poly_assert(world, ecs_world_t);
ecs_id_record_t* idr = flecs_id_record_get(world, id);
if (!idr) {
return NULL;
}
return (ecs_table_record_t*)ecs_table_cache_get(&idr->cache, table);
}
const ecs_table_record_t* flecs_id_record_get_table(
const ecs_id_record_t *idr,
const ecs_table_t *table)
{
ecs_assert(idr != NULL, ECS_INTERNAL_ERROR, NULL);
return (ecs_table_record_t*)ecs_table_cache_get(&idr->cache, table);
}
void flecs_init_id_records(
ecs_world_t *world)
{
/* Cache often used id records on world */
world->idr_wildcard = flecs_id_record_ensure(world, EcsWildcard);
world->idr_wildcard_wildcard = flecs_id_record_ensure(world,
ecs_pair(EcsWildcard, EcsWildcard));
world->idr_any = flecs_id_record_ensure(world, EcsAny);
world->idr_isa_wildcard = flecs_id_record_ensure(world,
ecs_pair(EcsIsA, EcsWildcard));
}
void flecs_fini_id_records(
ecs_world_t *world)
{
/* Loop & delete first element until there are no elements left. Id records
* can recursively delete each other, this ensures we always have a
* valid iterator. */
while (ecs_map_count(&world->id_index_hi) > 0) {
ecs_map_iter_t it = ecs_map_iter(&world->id_index_hi);
ecs_map_next(&it);
flecs_id_record_release(world, ecs_map_ptr(&it));
}
int32_t i;
for (i = 0; i < FLECS_HI_ID_RECORD_ID; i ++) {
ecs_id_record_t *idr = &world->id_index_lo[i];
if (idr->id) {
flecs_id_record_release(world, idr);
}
}
ecs_assert(ecs_map_count(&world->id_index_hi) == 0,
ECS_INTERNAL_ERROR, NULL);
ecs_map_fini(&world->id_index_hi);
ecs_os_free(world->id_index_lo);
}