-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathvalue_runtime.c
More file actions
2288 lines (2205 loc) · 74.9 KB
/
Copy pathvalue_runtime.c
File metadata and controls
2288 lines (2205 loc) · 74.9 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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPEC: PJ2026-0501 V2 kernel v0.47; typed value and object storage. */
#include "runtime_internal.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#if PIKA_TYPED_RUNTIME_ENABLE
#define PIKA_RUNTIME_FIELD_ASSIGNED 1u
static PikaRuntimeValue runtime_integer_value(int64_t integer) {
PikaRuntimeValue value;
memset(&value, 0, sizeof(value));
value.kind = PIKA_RUNTIME_VALUE_INTEGER;
value.as.integer = integer;
return value;
}
static void release_pointer(PikaObjectStorage* storage, void* pointer) {
if (pointer == NULL) {
return;
}
if (storage->release != NULL) {
storage->release(storage->context, pointer);
}
}
static uint16_t object_slot_limit(
const PikaObjectStorage* storage) {
return storage->slot_limit != 0u
? storage->slot_limit
: storage->capacity;
}
static uint32_t object_arena_limit(
const PikaObjectStorage* storage) {
return storage->arena_limit != 0u
? storage->arena_limit
: storage->arena_capacity;
}
static int object_arena_offset(
const PikaObjectStorage* storage,
const void* pointer,
size_t size,
uint32_t* offset) {
uintptr_t arena_address;
uintptr_t pointer_address;
uintptr_t distance;
if (storage == NULL || storage->arena == NULL ||
pointer == NULL || offset == NULL) {
return 0;
}
arena_address = (uintptr_t)storage->arena;
pointer_address = (uintptr_t)pointer;
if (pointer_address < arena_address) return 0;
distance = pointer_address - arena_address;
if (distance > storage->arena_used ||
size > (size_t)(storage->arena_used - (uint32_t)distance)) {
return 0;
}
*offset = (uint32_t)distance;
return 1;
}
static PikaStatus reserve_object_storage(
PikaObjectStorage* storage,
uint16_t slot_capacity,
uint32_t arena_capacity) {
PikaStatus status;
if (storage == NULL ||
slot_capacity > object_slot_limit(storage) ||
arena_capacity > object_arena_limit(storage)) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
if ((slot_capacity == 0u ||
(storage->slots != NULL &&
storage->capacity >= slot_capacity)) &&
(arena_capacity == 0u ||
(storage->arena != NULL &&
storage->arena_capacity >= arena_capacity))) {
return PIKA_STATUS_OK;
}
if (storage->reserve == NULL) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
status = storage->reserve(
storage->context, slot_capacity, arena_capacity);
if (status != PIKA_STATUS_OK) return status;
if ((slot_capacity > 0u &&
(storage->slots == NULL ||
storage->capacity < slot_capacity)) ||
(arena_capacity > 0u &&
(storage->arena == NULL ||
storage->arena_capacity < arena_capacity))) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
return PIKA_STATUS_OK;
}
void pika_object_storage_reset(PikaObjectStorage* storage) {
uint16_t index;
if (storage == NULL) return;
if (storage->slots != NULL) {
for (index = 0u; index < storage->count; ++index) {
PikaObjectSlot* slot = &storage->slots[index];
if (slot->active != 0u && slot->owned != 0u) {
release_pointer(storage, slot->data);
}
}
memset(storage->slots, 0,
(size_t)storage->capacity * sizeof(PikaObjectSlot));
}
storage->count = 0u;
storage->arena_used = 0u;
}
void pika_object_storage_release(PikaObjectStorage* storage) {
pika_object_storage_reset(storage);
}
static void* allocate_object_data(PikaObjectStorage* storage,
uint32_t size,
uint8_t* owned) {
uint32_t alignment = (uint32_t)sizeof(void*);
uint32_t aligned;
uint32_t required;
void* pointer = NULL;
if (storage->arena_used > UINT32_MAX - alignment + 1u) {
return NULL;
}
aligned = (storage->arena_used + alignment - 1u) /
alignment * alignment;
if (size <= UINT32_MAX - aligned) {
required = aligned + size;
if ((storage->arena == NULL ||
required > storage->arena_capacity) &&
reserve_object_storage(
storage, storage->capacity, required) ==
PIKA_STATUS_OK) {
aligned = (storage->arena_used + alignment - 1u) /
alignment * alignment;
}
}
if (storage->arena != NULL) {
if (aligned <= storage->arena_capacity &&
size <= storage->arena_capacity - aligned) {
pointer = &storage->arena[aligned];
storage->arena_used = aligned + size;
*owned = 0u;
return pointer;
}
}
if (storage->allocate != NULL && storage->release != NULL) {
pointer = storage->allocate(storage->context, size);
}
if (pointer != NULL) {
*owned = 1u;
}
return pointer;
}
static PikaStatus reserve_object_data(PikaRuntimeContext* context,
PikaObjectSlot** slot_pointer,
uint32_t required,
uint32_t element_size) {
PikaObjectStorage* storage = context->objects;
PikaObjectSlot* slot = *slot_pointer;
uint32_t capacity;
uint32_t size;
uint32_t old_size;
uint8_t owned = 0u;
void* pointer;
if (required <= slot->capacity && slot->data != NULL) {
return PIKA_STATUS_OK;
}
capacity = slot->capacity;
if (capacity == 0u) capacity = 1u;
while (capacity < required) {
if (capacity > UINT32_MAX / 2u) {
capacity = required;
break;
}
capacity *= 2u;
}
if (capacity > UINT32_MAX / element_size) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
size = capacity * element_size;
pointer = allocate_object_data(storage, size, &owned);
#if PIKA_GC_ENABLE
if (pointer == NULL && context->typed_values != NULL &&
context->object_collection_pending != 0u &&
context->gc_retry_block_depth == 0u &&
context->gc_root_active == 0u) {
uint32_t object_index =
(uint32_t)(slot - storage->slots);
memset(&context->gc_root, 0, sizeof(context->gc_root));
context->gc_root.kind =
(PikaRuntimeValueKind)slot->kind;
context->gc_root.as.object_index = object_index;
context->gc_root_active = 1u;
pika_runtime_collect_objects(context);
object_index = context->gc_root.as.object_index;
context->gc_root_active = 0u;
if (object_index < storage->count) {
slot = &storage->slots[object_index];
*slot_pointer = slot;
pointer = allocate_object_data(storage, size, &owned);
}
}
#endif
if (pointer == NULL) return PIKA_STATUS_STORAGE_TOO_SMALL;
memset(pointer, 0, size);
old_size = slot->length;
if (slot->data != NULL && old_size != 0u) {
memcpy(pointer, slot->data, old_size * element_size);
}
if (slot->owned != 0u) release_pointer(storage, slot->data);
slot->data = pointer;
slot->capacity = capacity;
slot->owned = owned;
#if PIKA_GC_ENABLE
pika_runtime_note_object_pressure(context);
#endif
if (slot->kind == PIKA_RUNTIME_VALUE_BYTEARRAY) {
((char*)slot->data)[slot->length] = '\0';
}
return PIKA_STATUS_OK;
}
PikaStatus pika_runtime_create_object(
PikaRuntimeContext* context,
PikaRuntimeValueKind kind,
uint32_t length,
uint32_t element_size,
uint32_t* object_index,
void** data) {
PikaObjectStorage* storage;
PikaObjectSlot* slot;
uint8_t owned = 0u;
uint32_t size;
void* pointer = NULL;
#if PIKA_GC_ENABLE
int collected = 0;
#endif
if (context == NULL || context->objects == NULL ||
object_index == NULL || data == NULL) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
storage = context->objects;
if ((storage->slots == NULL && storage->reserve == NULL) ||
(element_size != 0u &&
length > UINT32_MAX / element_size)) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
#if PIKA_GC_ENABLE
retry:
#endif
if (storage->count >= storage->capacity) {
#if PIKA_GC_ENABLE
if (!collected && context->typed_values != NULL &&
context->object_collection_pending != 0u &&
context->gc_retry_block_depth == 0u) {
collected = 1;
pika_runtime_collect_objects(context);
goto retry;
}
#endif
if (storage->count == UINT16_MAX ||
reserve_object_storage(
storage,
(uint16_t)(storage->count + 1u),
storage->arena_used) != PIKA_STATUS_OK) {
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
}
size = length * element_size;
if (size != 0u) {
pointer = allocate_object_data(storage, size, &owned);
if (pointer == NULL) {
#if PIKA_GC_ENABLE
if (!collected && context->typed_values != NULL &&
context->object_collection_pending != 0u &&
context->gc_retry_block_depth == 0u) {
collected = 1;
pika_runtime_collect_objects(context);
goto retry;
}
#endif
return PIKA_STATUS_STORAGE_TOO_SMALL;
}
memset(pointer, 0, size);
}
*object_index = storage->count;
slot = &storage->slots[storage->count++];
slot->kind = (uint8_t)kind;
slot->active = 1u;
slot->owned = owned;
slot->length = length;
slot->capacity = length;
slot->data = pointer;
*data = pointer;
if (context->metrics != NULL &&
storage->count > context->metrics->object_peak) {
context->metrics->object_peak = storage->count;
}
#if PIKA_GC_ENABLE
pika_runtime_note_object_pressure(context);
#endif
return PIKA_STATUS_OK;
}
PikaObjectSlot* pika_runtime_object(
PikaRuntimeContext* context,
PikaRuntimeValue value) {
if (context == NULL || context->objects == NULL ||
context->objects->slots == NULL ||
value.as.object_index >= context->objects->count) {
return NULL;
}
if (context->objects->slots[value.as.object_index].active == 0u) {
return NULL;
}
return &context->objects->slots[value.as.object_index];
}
PikaRuntimeValue pika_runtime_read_value(
const PikaRuntimeContext* context,
uint32_t index) {
PikaRuntimeValue value;
memset(&value, 0, sizeof(value));
if (context == NULL) {
return value;
}
if (context->typed_values != NULL) {
return context->typed_values[index];
}
value.kind = PIKA_RUNTIME_VALUE_INTEGER;
value.as.integer = context->values[index];
return value;
}
PikaStatus pika_runtime_write_value(
PikaRuntimeContext* context,
uint32_t index,
PikaRuntimeValue value) {
if (context == NULL) {
return PIKA_STATUS_INVALID_ARGUMENT;
}
if (context->typed_values != NULL) {
context->typed_values[index] = value;
return PIKA_STATUS_OK;
}
if (value.kind == PIKA_RUNTIME_VALUE_INTEGER ||
value.kind == PIKA_RUNTIME_VALUE_BOOLEAN ||
value.kind == PIKA_RUNTIME_VALUE_NONE) {
context->values[index] =
value.kind == PIKA_RUNTIME_VALUE_INTEGER
? value.as.integer
: value.kind == PIKA_RUNTIME_VALUE_BOOLEAN
? (value.as.integer != 0 ? 1 : 0)
: 0;
return PIKA_STATUS_OK;
}
return PIKA_STATUS_TYPE_MISMATCH;
}
int pika_runtime_value_truthy(
const PikaRuntimeContext* context,
PikaRuntimeValue value) {
PikaObjectSlot* slot;
if (value.kind == PIKA_RUNTIME_VALUE_NONE) {
return 0;
}
if (value.kind == PIKA_RUNTIME_VALUE_BOOLEAN ||
value.kind == PIKA_RUNTIME_VALUE_INTEGER) {
return value.as.integer != 0;
}
if (value.kind == PIKA_RUNTIME_VALUE_FLOAT) {
return value.as.floating != 0.0;
}
if (value.kind == PIKA_RUNTIME_VALUE_RANGE) {
uint32_t length = 0u;
return pika_runtime_range_length(
(PikaRuntimeContext*)context, value, &length) ==
PIKA_STATUS_OK &&
length != 0u;
}
if (value.kind == PIKA_RUNTIME_VALUE_INSTANCE ||
value.kind == PIKA_RUNTIME_VALUE_NATIVE_OBJECT) {
return 1;
}
slot = pika_runtime_object((PikaRuntimeContext*)context, value);
return slot != NULL && slot->length != 0u;
}
static int is_value_sequence(PikaRuntimeValueKind kind) {
return kind == PIKA_RUNTIME_VALUE_LIST ||
kind == PIKA_RUNTIME_VALUE_TUPLE ||
kind == PIKA_RUNTIME_VALUE_SET;
}
static int is_byte_sequence(PikaRuntimeValueKind kind) {
return kind == PIKA_RUNTIME_VALUE_BYTES ||
kind == PIKA_RUNTIME_VALUE_BYTEARRAY;
}
#if PIKA_GC_ENABLE
typedef void (*PikaRuntimeValueVisitor)(
PikaRuntimeValue* value,
void* context);
typedef struct {
uint32_t source;
uint32_t destination;
} PikaObjectIndexRemap;
static int is_object_value_kind(PikaRuntimeValueKind kind) {
return is_value_sequence(kind) ||
is_byte_sequence(kind) ||
kind == PIKA_RUNTIME_VALUE_STRING ||
kind == PIKA_RUNTIME_VALUE_DICT ||
kind == PIKA_RUNTIME_VALUE_RANGE ||
kind == PIKA_RUNTIME_VALUE_ITERATOR ||
kind == PIKA_RUNTIME_VALUE_SLICE ||
kind == PIKA_RUNTIME_VALUE_TYPE ||
kind == PIKA_RUNTIME_VALUE_MODULE ||
#if PIKA_CAPABILITY_EXCEPTION_BASIC_ENABLE
kind == PIKA_RUNTIME_VALUE_INSTANCE ||
kind == PIKA_RUNTIME_VALUE_EXCEPTION;
#else
kind == PIKA_RUNTIME_VALUE_INSTANCE;
#endif
}
static int object_slot_payload_size(
const PikaObjectSlot* slot,
uint32_t* size) {
uint32_t element_size;
switch ((PikaRuntimeValueKind)slot->kind) {
case PIKA_RUNTIME_VALUE_STRING:
case PIKA_RUNTIME_VALUE_BYTES:
case PIKA_RUNTIME_VALUE_BYTEARRAY:
case PIKA_RUNTIME_VALUE_TYPE:
case PIKA_RUNTIME_VALUE_MODULE:
element_size = 1u;
break;
case PIKA_RUNTIME_VALUE_LIST:
case PIKA_RUNTIME_VALUE_TUPLE:
case PIKA_RUNTIME_VALUE_SET:
case PIKA_RUNTIME_VALUE_INSTANCE:
element_size = (uint32_t)sizeof(PikaRuntimeValue);
break;
case PIKA_RUNTIME_VALUE_DICT:
element_size = (uint32_t)sizeof(PikaMapEntry);
break;
case PIKA_RUNTIME_VALUE_RANGE:
element_size = (uint32_t)sizeof(PikaRangeData);
break;
case PIKA_RUNTIME_VALUE_ITERATOR:
element_size = (uint32_t)sizeof(PikaIteratorData);
break;
case PIKA_RUNTIME_VALUE_SLICE:
element_size = (uint32_t)sizeof(PikaSliceData);
break;
default:
return 0;
}
if (slot->capacity > UINT32_MAX / element_size) {
return 0;
}
*size = slot->capacity * element_size;
return 1;
}
static void visit_object_values(
PikaObjectSlot* slot,
PikaRuntimeValueVisitor visitor,
void* visitor_context) {
uint32_t index;
if (slot->data == NULL || slot->length > slot->capacity) {
return;
}
if (slot->kind == PIKA_RUNTIME_VALUE_LIST ||
slot->kind == PIKA_RUNTIME_VALUE_TUPLE ||
slot->kind == PIKA_RUNTIME_VALUE_SET ||
slot->kind == PIKA_RUNTIME_VALUE_INSTANCE) {
PikaRuntimeValue* values =
(PikaRuntimeValue*)slot->data;
for (index = 0u; index < slot->length; ++index) {
visitor(&values[index], visitor_context);
}
return;
}
if (slot->kind == PIKA_RUNTIME_VALUE_DICT) {
PikaMapEntry* entries = (PikaMapEntry*)slot->data;
for (index = 0u; index < slot->length; ++index) {
visitor(&entries[index].key, visitor_context);
visitor(&entries[index].value, visitor_context);
}
return;
}
if (slot->kind == PIKA_RUNTIME_VALUE_SLICE &&
slot->length > 0u) {
PikaSliceData* slice = (PikaSliceData*)slot->data;
visitor(&slice->start, visitor_context);
visitor(&slice->stop, visitor_context);
visitor(&slice->step, visitor_context);
}
}
static void mark_object_value(
PikaRuntimeValue* value,
void* context) {
PikaObjectStorage* storage = (PikaObjectStorage*)context;
uint32_t index;
if (!is_object_value_kind(value->kind)) {
return;
}
index = value->as.object_index;
if (index < storage->count &&
storage->slots[index].active == 1u) {
storage->slots[index].active = 2u;
}
}
static void remap_object_value(
PikaRuntimeValue* value,
void* context) {
const PikaObjectIndexRemap* remap =
(const PikaObjectIndexRemap*)context;
if (is_object_value_kind(value->kind) &&
value->as.object_index == remap->source) {
value->as.object_index = remap->destination;
}
}
static void visit_runtime_roots(
PikaRuntimeContext* context,
PikaRuntimeValueVisitor visitor,
void* visitor_context) {
uint32_t depth;
if (context->frames != NULL &&
context->typed_values != NULL) {
for (depth = 0u; depth <= context->depth; ++depth) {
const PikaRuntimeFrame* frame =
&context->frames[depth];
uint32_t count = frame->slot_count;
uint32_t index;
if (count > context->max_slots) {
count = context->max_slots;
}
for (index = 0u; index < count; ++index) {
visitor(
&context->typed_values[
frame->value_base + index],
visitor_context);
}
}
}
if (context->result != NULL) {
visitor(
&context->result->typed_value,
visitor_context);
}
if (context->gc_root_active != 0u) {
visitor(&context->gc_root, visitor_context);
}
#if PIKA_CAPABILITY_EXCEPTION_BASIC_ENABLE
if (context->has_pending_exception != 0u) {
visitor(
&context->pending_exception,
visitor_context);
}
#endif
}
static void mark_object_graph(PikaRuntimeContext* context) {
PikaObjectStorage* storage = context->objects;
uint32_t index;
int pending;
for (index = 0u; index < storage->count; ++index) {
if (storage->slots[index].active != 0u) {
storage->slots[index].active = 1u;
}
}
visit_runtime_roots(
context, mark_object_value, storage);
do {
pending = 0;
for (index = 0u; index < storage->count; ++index) {
PikaObjectSlot* slot = &storage->slots[index];
if (slot->active != 2u) {
continue;
}
slot->active = 3u;
pending = 1;
visit_object_values(
slot, mark_object_value, storage);
if (slot->kind ==
PIKA_RUNTIME_VALUE_ITERATOR &&
slot->data != NULL &&
slot->length > 0u &&
slot->length <= slot->capacity) {
PikaIteratorData* iterator =
(PikaIteratorData*)slot->data;
if (iterator->source_index <
storage->count &&
storage->slots[
iterator->source_index]
.active == 1u) {
storage->slots[
iterator->source_index]
.active = 2u;
}
}
}
} while (pending != 0);
}
static void remap_object_graph(
PikaRuntimeContext* context,
uint32_t source,
uint32_t destination) {
PikaObjectStorage* storage = context->objects;
PikaObjectIndexRemap remap = {source, destination};
uint32_t index;
visit_runtime_roots(
context, remap_object_value, &remap);
for (index = 0u; index < storage->count; ++index) {
PikaObjectSlot* slot = &storage->slots[index];
if (slot->active == 0u) {
continue;
}
visit_object_values(
slot, remap_object_value, &remap);
if (slot->kind ==
PIKA_RUNTIME_VALUE_ITERATOR &&
slot->data != NULL &&
slot->length > 0u &&
slot->length <= slot->capacity) {
PikaIteratorData* iterator =
(PikaIteratorData*)slot->data;
if (iterator->source_index == source) {
iterator->source_index = destination;
}
}
}
}
static void compact_object_slots(
PikaRuntimeContext* context) {
PikaObjectStorage* storage = context->objects;
uint32_t old_count = storage->count;
uint32_t destination = 0u;
uint32_t source;
for (source = 0u; source < old_count; ++source) {
PikaObjectSlot* slot = &storage->slots[source];
if (slot->active == 1u) {
if (slot->owned != 0u) {
release_pointer(storage, slot->data);
}
memset(slot, 0, sizeof(*slot));
}
}
for (source = 0u; source < old_count; ++source) {
PikaObjectSlot* slot = &storage->slots[source];
if (slot->active != 3u) {
continue;
}
if (destination != source) {
storage->slots[destination] = *slot;
storage->slots[destination].active = 1u;
memset(slot, 0, sizeof(*slot));
remap_object_graph(
context, source, destination);
} else {
slot->active = 1u;
}
++destination;
}
if (destination < old_count) {
memset(
&storage->slots[destination], 0,
(old_count - destination) *
sizeof(PikaObjectSlot));
}
storage->count = (uint16_t)destination;
}
#if PIKA_BINDING_RUNTIME_ENABLE
static void visit_reachable_values(
PikaRuntimeContext* context,
PikaRuntimeValueVisitor visitor,
void* visitor_context) {
uint32_t index;
visit_runtime_roots(
context, visitor, visitor_context);
if (context->objects == NULL ||
context->objects->slots == NULL) {
return;
}
for (index = 0u;
index < context->objects->count;
++index) {
PikaObjectSlot* slot =
&context->objects->slots[index];
if (slot->active != 0u) {
visit_object_values(
slot, visitor, visitor_context);
}
}
}
static void mark_binding_value(
PikaRuntimeValue* value,
void* context) {
PikaRuntimeContext* runtime =
(PikaRuntimeContext*)context;
uint32_t index;
if (value->kind !=
PIKA_RUNTIME_VALUE_NATIVE_OBJECT) {
return;
}
index = value->as.object_index;
if (index < runtime->binding_object_count &&
runtime->binding_storage
->objects[index]
.active == 1u) {
runtime->binding_storage
->objects[index]
.active = 2u;
}
}
static void invalidate_released_binding_value(
PikaRuntimeValue* value,
void* context) {
PikaRuntimeContext* runtime =
(PikaRuntimeContext*)context;
uint32_t index;
if (value->kind !=
PIKA_RUNTIME_VALUE_NATIVE_OBJECT) {
return;
}
index = value->as.object_index;
if (index < runtime->binding_object_count &&
runtime->binding_storage->objects[index].active == 0u) {
memset(value, 0, sizeof(*value));
value->kind = PIKA_RUNTIME_VALUE_NONE;
}
}
static void remap_binding_value(
PikaRuntimeValue* value,
void* context) {
const PikaObjectIndexRemap* remap =
(const PikaObjectIndexRemap*)context;
if (value->kind ==
PIKA_RUNTIME_VALUE_NATIVE_OBJECT &&
value->as.object_index == remap->source) {
value->as.object_index = remap->destination;
}
}
static void invalidate_released_binding_storage_values(
PikaRuntimeContext* context,
uint32_t object_count) {
uint32_t depth;
PikaBindingObject* objects =
context->binding_storage->objects;
if (context->binding_storage->values == NULL ||
context->frames == NULL) {
return;
}
for (depth = 0u; depth <= context->depth; ++depth) {
const PikaRuntimeFrame* frame =
&context->frames[depth];
uint32_t count = frame->slot_count;
uint32_t index;
if (frame->value_base >=
context->binding_storage->value_capacity) {
continue;
}
if (count > context->max_slots) {
count = context->max_slots;
}
if (count >
context->binding_storage->value_capacity -
frame->value_base) {
count =
context->binding_storage->value_capacity -
frame->value_base;
}
for (index = 0u; index < count; ++index) {
PikaBindingValue* value =
&context->binding_storage->values[
frame->value_base + index];
uint32_t object_index;
if (value->kind !=
PIKA_BINDING_VALUE_OPAQUE) {
continue;
}
for (object_index = 0u;
object_index < object_count;
++object_index) {
if (objects[object_index].active == 0u &&
value->as.opaque ==
&objects[object_index]) {
memset(value, 0, sizeof(*value));
value->kind = PIKA_BINDING_VALUE_NONE;
break;
}
}
}
}
}
static void remap_binding_storage_values(
PikaRuntimeContext* context,
uint32_t source,
uint32_t destination) {
uint32_t depth;
PikaBindingObject* objects =
context->binding_storage->objects;
if (context->binding_storage->values == NULL ||
context->frames == NULL) {
return;
}
for (depth = 0u; depth <= context->depth; ++depth) {
const PikaRuntimeFrame* frame =
&context->frames[depth];
uint32_t count = frame->slot_count;
uint32_t index;
if (frame->value_base >=
context->binding_storage->value_capacity) {
continue;
}
if (count > context->max_slots) {
count = context->max_slots;
}
if (count >
context->binding_storage->value_capacity -
frame->value_base) {
count =
context->binding_storage->value_capacity -
frame->value_base;
}
for (index = 0u; index < count; ++index) {
PikaBindingValue* value =
&context->binding_storage->values[
frame->value_base + index];
if (value->kind ==
PIKA_BINDING_VALUE_OPAQUE &&
value->as.opaque ==
&objects[source]) {
value->as.opaque =
&objects[destination];
}
}
}
}
static void compact_binding_objects(
PikaRuntimeContext* context) {
PikaBindingObject* objects;
uint32_t old_count;
uint32_t destination = 0u;
uint32_t source;
if (context->binding_storage == NULL ||
context->binding_storage->objects == NULL ||
context->binding_object_count == 0u) {
return;
}
if (context->binding_object_count >
context->binding_storage->object_capacity) {
return;
}
objects = context->binding_storage->objects;
old_count = context->binding_object_count;
for (source = 0u; source < old_count; ++source) {
if (objects[source].active != 0u) {
objects[source].active = 1u;
}
}
visit_reachable_values(
context, invalidate_released_binding_value, context);
invalidate_released_binding_storage_values(
context, old_count);
visit_reachable_values(
context, mark_binding_value, context);
for (source = 0u; source < old_count; ++source) {
if (objects[source].active == 1u) {
pika_binding_object_release(
&objects[source]);
}
}
for (source = 0u; source < old_count; ++source) {
PikaObjectIndexRemap remap;
if (objects[source].active != 2u) {
continue;
}
if (source != destination) {
objects[destination] = objects[source];
objects[destination].active = 1u;
memset(
&objects[source], 0,
sizeof(PikaBindingObject));
remap.source = source;
remap.destination = destination;
visit_reachable_values(
context, remap_binding_value,
&remap);
remap_binding_storage_values(
context, source, destination);
} else {
objects[source].active = 1u;
}
++destination;
}
if (destination < old_count) {
memset(
&objects[destination], 0,
(old_count - destination) *
sizeof(PikaBindingObject));
}
context->binding_object_count =
(uint16_t)destination;
}
#endif
static void compact_object_arena(
PikaObjectStorage* storage) {
uintptr_t begin;
uintptr_t end;
uintptr_t previous_pointer = 0u;
uintptr_t previous_end = 0u;
uint32_t index;
uint32_t destination = 0u;
uint32_t previous_index = 0u;
uint32_t alignment = (uint32_t)sizeof(void*);
int have_previous = 0;
if (storage->slots == NULL ||
storage->arena == NULL ||
storage->arena_capacity == 0u) {
return;
}
begin = (uintptr_t)storage->arena;
end = begin + storage->arena_capacity;
if (end < begin ||
storage->arena_used >
storage->arena_capacity) {
return;
}
for (index = 0u; index < storage->count; ++index) {
PikaObjectSlot* slot = &storage->slots[index];
uint32_t size;
uintptr_t pointer;
if (slot->active == 0u ||
slot->owned != 0u ||
slot->data == NULL) {
continue;
}
if (!object_slot_payload_size(slot, &size)) {
return;
}
pointer = (uintptr_t)slot->data;
if (pointer < begin || pointer > end ||
size > end - pointer) {
return;
}
}
for (;;) {
uint32_t selected = UINT32_MAX;
uintptr_t selected_pointer = UINTPTR_MAX;
uint32_t selected_size = 0u;
uint64_t aligned;
for (index = 0u; index < storage->count; ++index) {
PikaObjectSlot* slot = &storage->slots[index];
uintptr_t pointer;
uint32_t size;
if (slot->active != 1u ||
slot->owned != 0u ||
slot->data == NULL ||
!object_slot_payload_size(slot, &size) ||
size == 0u) {
continue;
}
pointer = (uintptr_t)slot->data;
if (have_previous != 0 &&
(pointer < previous_pointer ||
(pointer == previous_pointer &&
index <= previous_index))) {
continue;
}