-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnofl-space.h
More file actions
2163 lines (1919 loc) · 78.1 KB
/
Copy pathnofl-space.h
File metadata and controls
2163 lines (1919 loc) · 78.1 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
#ifndef NOFL_SPACE_H
#define NOFL_SPACE_H
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <string.h>
#include "gc-api.h"
#define GC_IMPL 1
#include "gc-internal.h"
#include "assert.h"
#include "debug.h"
#include "extents.h"
#include "gc-align.h"
#include "gc-atomics.h"
#include "gc-attrs.h"
#include "gc-inline.h"
#include "gc-lock.h"
#include "gc-platform.h"
#include "spin.h"
#include "swar.h"
// This is the nofl space! It is a mark space which doesn't use
// free-lists to allocate, and which can evacuate objects if
// fragmentation is too high, inspired by Immix. Nofl stands for "no
// free-list", but also "novel", in the sense that it hasn't been tried
// before.
#define NOFL_GRANULE_SIZE 16
#define NOFL_GRANULE_SIZE_LOG_2 4
#define NOFL_MEDIUM_OBJECT_THRESHOLD 256
#define NOFL_MEDIUM_OBJECT_GRANULE_THRESHOLD 16
STATIC_ASSERT_EQ(NOFL_GRANULE_SIZE, 1 << NOFL_GRANULE_SIZE_LOG_2);
STATIC_ASSERT_EQ(NOFL_MEDIUM_OBJECT_THRESHOLD,
NOFL_MEDIUM_OBJECT_GRANULE_THRESHOLD * NOFL_GRANULE_SIZE);
#include "nofl-holeset.h"
#define NOFL_SLAB_SIZE (4 * 1024 * 1024)
#define NOFL_BLOCK_SIZE (64 * 1024)
#define NOFL_METADATA_BYTES_PER_BLOCK (NOFL_BLOCK_SIZE / NOFL_GRANULE_SIZE)
#define NOFL_BLOCKS_PER_SLAB (NOFL_SLAB_SIZE / NOFL_BLOCK_SIZE)
#define NOFL_META_BLOCKS_PER_SLAB (NOFL_METADATA_BYTES_PER_BLOCK * NOFL_BLOCKS_PER_SLAB / NOFL_BLOCK_SIZE)
#define NOFL_NONMETA_BLOCKS_PER_SLAB (NOFL_BLOCKS_PER_SLAB - NOFL_META_BLOCKS_PER_SLAB)
#define NOFL_METADATA_BYTES_PER_SLAB (NOFL_NONMETA_BLOCKS_PER_SLAB * NOFL_METADATA_BYTES_PER_BLOCK)
#define NOFL_SLACK_METADATA_BYTES_PER_SLAB (NOFL_META_BLOCKS_PER_SLAB * NOFL_METADATA_BYTES_PER_BLOCK)
#define NOFL_VESTIGIAL_BYTES_PER_BLOCK (NOFL_SLACK_METADATA_BYTES_PER_SLAB / NOFL_BLOCKS_PER_SLAB)
#define NOFL_VESTIGIAL_BYTES_PER_SLAB (NOFL_VESTIGIAL_BYTES_PER_BLOCK * NOFL_NONMETA_BLOCKS_PER_SLAB)
#define NOFL_SLACK_VESTIGIAL_BYTES_PER_SLAB (NOFL_VESTIGIAL_BYTES_PER_BLOCK * NOFL_META_BLOCKS_PER_SLAB)
#define NOFL_SUMMARY_BYTES_PER_BLOCK (NOFL_SLACK_VESTIGIAL_BYTES_PER_SLAB / NOFL_BLOCKS_PER_SLAB)
#define NOFL_SUMMARY_BYTES_PER_SLAB (NOFL_SUMMARY_BYTES_PER_BLOCK * NONMETA_BLOCKS_PER_SLAB)
#define NOFL_SLACK_SUMMARY_BYTES_PER_SLAB (NOFL_SUMMARY_BYTES_PER_BLOCK * NOFL_META_BLOCKS_PER_SLAB)
#define NOFL_HEADER_BYTES_PER_SLAB NOFL_SLACK_SUMMARY_BYTES_PER_SLAB
struct nofl_slab;
struct nofl_slab_header {
union {
struct {
uint8_t block_marks[NOFL_BLOCKS_PER_SLAB];
};
uint8_t padding[NOFL_HEADER_BYTES_PER_SLAB];
};
};
STATIC_ASSERT_EQ(sizeof(struct nofl_slab_header), NOFL_HEADER_BYTES_PER_SLAB);
// Sometimes we want to put a block on a singly-linked list. For that
// there's a pointer reserved in the block summary. But because the
// pointer is aligned (32kB on 32-bit, 64kB on 64-bit), we can portably
// hide up to 15 flags in the low bits. These flags are accessed
// non-atomically, in two situations: one, when a block is not on a
// list, which guarantees that no other thread can access it; or when no
// pushing or popping is happening, for example during an evacuation
// cycle.
enum nofl_block_summary_flag {
NOFL_BLOCK_EVACUATE = 0x1,
NOFL_BLOCK_ZERO = 0x2,
NOFL_BLOCK_UNAVAILABLE = 0x4,
NOFL_BLOCK_PAGED_OUT = 0x8,
NOFL_BLOCK_FLAG_UNUSED_3 = 0x8,
NOFL_BLOCK_FLAG_UNUSED_4 = 0x10,
NOFL_BLOCK_FLAG_UNUSED_5 = 0x20,
NOFL_BLOCK_FLAG_UNUSED_6 = 0x40,
NOFL_BLOCK_FLAG_UNUSED_7 = 0x80,
NOFL_BLOCK_FLAG_UNUSED_8 = 0x100,
NOFL_BLOCK_FLAG_UNUSED_9 = 0x200,
NOFL_BLOCK_FLAG_UNUSED_10 = 0x400,
NOFL_BLOCK_FLAG_UNUSED_11 = 0x800,
NOFL_BLOCK_FLAG_UNUSED_12 = 0x1000,
NOFL_BLOCK_FLAG_UNUSED_13 = 0x2000,
NOFL_BLOCK_FLAG_UNUSED_14 = 0x4000,
};
struct nofl_block_summary {
union {
struct {
// Counters related to previous collection: how many holes there
// were, and how much space they had.
uint16_t hole_count;
uint16_t hole_granules;
// Counters related to allocation since previous collection:
// wasted space due to fragmentation. Also used by blocks on the
// "partly full" list, which have zero holes_with_fragmentation
// but nonzero fragmentation_granules.
uint16_t holes_with_fragmentation;
uint16_t fragmentation_granules;
// Next pointer, and flags in low bits. See comment above
// regarding enum nofl_block_summary_flag.
uintptr_t next_and_flags;
};
uint8_t padding[NOFL_SUMMARY_BYTES_PER_BLOCK];
};
};
STATIC_ASSERT_EQ(sizeof(struct nofl_block_summary),
NOFL_SUMMARY_BYTES_PER_BLOCK);
struct nofl_block {
char data[NOFL_BLOCK_SIZE];
};
struct nofl_block_ref {
struct nofl_block_summary *summary;
uintptr_t addr;
};
struct nofl_slab {
struct nofl_slab_header header;
struct nofl_block_summary summaries[NOFL_NONMETA_BLOCKS_PER_SLAB];
uint8_t unused[NOFL_VESTIGIAL_BYTES_PER_SLAB];
uint8_t metadata[NOFL_METADATA_BYTES_PER_SLAB];
struct nofl_block blocks[NOFL_NONMETA_BLOCKS_PER_SLAB];
};
STATIC_ASSERT_EQ(sizeof(struct nofl_slab), NOFL_SLAB_SIZE);
// Lock-free block list, which either only has threads removing items
// from it or only has threads adding items to it -- i.e., adding and
// removing items don't happen concurrently.
struct nofl_block_list {
size_t count;
uintptr_t blocks;
};
// A block list that has concurrent threads adding and removing items
// from it.
struct nofl_block_stack {
struct nofl_block_list list;
};
#define NOFL_PAGE_OUT_QUEUE_SIZE 4
struct nofl_space {
uint8_t current_mark;
uint8_t survivor_mark;
uint8_t evacuating;
struct extents *extents;
uint8_t last_collection_was_minor;
uint8_t heap_has_ambiguous_edges;
struct nofl_block_stack empty;
struct nofl_block_stack paged_out[NOFL_PAGE_OUT_QUEUE_SIZE];
struct nofl_block_list to_sweep;
struct nofl_block_stack partly_full;
struct nofl_block_list full;
struct nofl_block_list promoted;
struct nofl_block_list old;
struct nofl_block_list evacuation_targets;
struct nofl_holeset holes;
pthread_mutex_t lock;
double evacuation_minimum_reserve;
double evacuation_reserve;
double promotion_threshold;
ssize_t pending_unavailable_bytes; // atomically
struct nofl_slab **slabs;
size_t nslabs;
uintptr_t old_generation_granules; // atomically
uintptr_t survivor_granules_at_last_collection; // atomically
uintptr_t allocated_granules_since_last_collection; // atomically
uintptr_t fragmentation_granules_since_last_collection; // atomically
uintptr_t second_chance_granules_since_last_collection; // atomically
};
struct nofl_allocator {
uintptr_t alloc;
uintptr_t sweep;
struct nofl_block_ref block;
struct nofl_holeset holes;
};
#if GC_CONSERVATIVE_TRACE && GC_CONCURRENT_TRACE
// There are just not enough bits in the mark table.
#error Unsupported configuration
#endif
// Each granule has one mark byte stored in a side table. A granule's
// mark state is a whole byte instead of a bit to facilitate parallel
// marking. (Parallel markers are allowed to race.) We also use this
// byte to compute object extent, via a bit flag indicating
// end-of-object.
//
// Because we want to allow for conservative roots, we need to know
// whether an address indicates an object or not. That means that when
// an object is allocated, it has to set a bit, somewhere. We use the
// metadata byte for this purpose, setting the "young" mark.
//
// The "young" mark's name might make you think about generational
// collection, and indeed all objects collected in a minor collection
// will have this bit set. However, the nofl space never needs to check
// for the young mark; if it weren't for the need to identify
// conservative roots, we wouldn't need a young mark at all. Perhaps in
// an all-precise system, we would be able to avoid the overhead of
// initializing mark byte upon each fresh allocation.
//
// When an object becomes dead after a GC, it will still have a mark set
// -- maybe the young mark, or maybe a survivor mark. The sweeper has
// to clear these marks before the next collection. The same goes for
// "forwarded" marks left in the metadata of evacuated objects. If we
// add concurrent marking, we will also be marking "live" objects,
// updating their mark bits. So there are three and possibly four
// object states concurrently observable: young, dead, survivor, and
// marked. (We don't currently have concurrent marking, though.) We
// store this state in the low 3 bits of the byte. After each major
// collection, the dead, survivor, and marked states rotate.
//
// It can be useful to support "raw" allocations, most often
// pointerless, but for compatibility with BDW-GC, sometimes
// conservatively-traced tagless data. We reserve one or two bits for
// the "kind" of the allocation: either a normal object traceable via
// `gc_trace_object`, a pointerless untagged allocation that doesn't
// need tracing, an allocation that should be traced conservatively.
// The latter state is only used when conservative tracing is enabled.
//
// An object can be pinned, preventing it from being evacuated during
// collection. Pinning does not keep the object alive; if it is
// otherwise unreachable, it will be collected. To pin an object, a
// running mutator can set the pinned bit, using atomic
// compare-and-swap. This bit overlaps the "trace conservatively" trace
// kind, but that's OK because we don't use the pinned bit in those
// cases, as all objects are implicitly pinned.
//
// For generational collectors, the nofl space supports a field-logging
// write barrier. The two logging bits correspond to the two words in a
// granule. When a field is written to, the write barrier should check
// the logged bit; if it is unset, it should try to atomically set the
// bit, and if that works, then we record the field location as a
// generational root, adding it to a sequential-store buffer.
enum nofl_metadata_byte {
NOFL_METADATA_BYTE_NONE = 0,
NOFL_METADATA_BYTE_YOUNG = 1,
NOFL_METADATA_BYTE_MARK_0 = 2,
NOFL_METADATA_BYTE_MARK_1 = 3,
NOFL_METADATA_BYTE_MARK_2 = 4,
NOFL_METADATA_BYTE_BUSY = 5,
NOFL_METADATA_BYTE_FORWARDED = 6,
NOFL_METADATA_BYTE_UNUSED = 7,
NOFL_METADATA_BYTE_MARK_MASK = 7,
NOFL_METADATA_BYTE_TRACE_PRECISELY = 0,
NOFL_METADATA_BYTE_TRACE_NONE = 8,
NOFL_METADATA_BYTE_TRACE_CONSERVATIVELY = 16,
NOFL_METADATA_BYTE_TRACE_UNUSED = 24,
NOFL_METADATA_BYTE_TRACE_KIND_MASK = 0|8|16|24,
NOFL_METADATA_BYTE_PINNED = 16,
NOFL_METADATA_BYTE_END = 32,
NOFL_METADATA_BYTE_LOGGED_0 = 64,
NOFL_METADATA_BYTE_LOGGED_1 = 128,
};
STATIC_ASSERT_EQ(0,
NOFL_METADATA_BYTE_TRACE_PRECISELY&NOFL_METADATA_BYTE_PINNED);
STATIC_ASSERT_EQ(0,
NOFL_METADATA_BYTE_TRACE_NONE&NOFL_METADATA_BYTE_PINNED);
static uint8_t
nofl_advance_current_mark(uint8_t mark) {
switch (mark) {
case NOFL_METADATA_BYTE_MARK_0:
return NOFL_METADATA_BYTE_MARK_1;
case NOFL_METADATA_BYTE_MARK_1:
return NOFL_METADATA_BYTE_MARK_2;
case NOFL_METADATA_BYTE_MARK_2:
return NOFL_METADATA_BYTE_MARK_0;
default:
GC_CRASH();
}
}
static struct gc_lock
nofl_space_lock(struct nofl_space *space) {
return gc_lock_acquire(&space->lock);
}
static struct nofl_slab*
nofl_object_slab(void *obj) {
uintptr_t addr = (uintptr_t) obj;
uintptr_t base = align_down(addr, NOFL_SLAB_SIZE);
return (struct nofl_slab*) base;
}
static uint8_t*
nofl_metadata_byte_for_addr(uintptr_t addr) {
uintptr_t base = align_down(addr, NOFL_SLAB_SIZE);
uintptr_t granule = (addr & (NOFL_SLAB_SIZE - 1)) >> NOFL_GRANULE_SIZE_LOG_2;
return (uint8_t*) (base + granule);
}
static uint8_t*
nofl_metadata_byte_for_object(struct gc_ref ref) {
uint8_t *ret = nofl_metadata_byte_for_addr(gc_ref_value(ref));
GC_ASSERT(*ret & NOFL_METADATA_BYTE_MARK_MASK);
return ret;
}
static uint8_t*
nofl_block_mark_loc(uintptr_t addr) {
uintptr_t base = align_down(addr, NOFL_SLAB_SIZE);
struct nofl_slab *slab = (struct nofl_slab *) base;
unsigned block_idx = (addr / NOFL_BLOCK_SIZE) % NOFL_BLOCKS_PER_SLAB;
return &slab->header.block_marks[block_idx];
}
static int
nofl_block_is_marked(uintptr_t addr) {
return gc_atomic_load_relaxed(nofl_block_mark_loc(addr));
}
static void
nofl_block_set_mark(uintptr_t addr) {
uint8_t *loc = nofl_block_mark_loc(addr);
if (!gc_atomic_load_relaxed(loc))
gc_atomic_store_relaxed(loc, 1);
}
#define NOFL_GRANULES_PER_BLOCK (NOFL_BLOCK_SIZE / NOFL_GRANULE_SIZE)
static struct nofl_block_summary*
nofl_block_summary_for_addr(uintptr_t addr) {
uintptr_t base = align_down(addr, NOFL_SLAB_SIZE);
uintptr_t block = (addr & (NOFL_SLAB_SIZE - 1)) / NOFL_BLOCK_SIZE;
return (struct nofl_block_summary*)
(base + block * sizeof(struct nofl_block_summary));
}
static uintptr_t
nofl_block_summary_has_flag(struct nofl_block_summary *summary,
enum nofl_block_summary_flag flag) {
return (summary->next_and_flags & flag) == flag;
}
static void
nofl_block_summary_set_flag(struct nofl_block_summary *summary,
enum nofl_block_summary_flag flag) {
summary->next_and_flags |= flag;
}
static void
nofl_block_summary_clear_flag(struct nofl_block_summary *summary,
enum nofl_block_summary_flag flag) {
summary->next_and_flags &= ~(uintptr_t)flag;
}
static uintptr_t
nofl_block_summary_next(struct nofl_block_summary *summary) {
return align_down(summary->next_and_flags, NOFL_BLOCK_SIZE);
}
static void
nofl_block_summary_set_next(struct nofl_block_summary *summary,
uintptr_t next) {
GC_ASSERT((next & (NOFL_BLOCK_SIZE - 1)) == 0);
summary->next_and_flags =
(summary->next_and_flags & (NOFL_BLOCK_SIZE - 1)) | next;
}
static struct nofl_block_ref
nofl_block_for_addr(uintptr_t addr) {
return (struct nofl_block_ref) {
nofl_block_summary_for_addr(addr),
align_down(addr, NOFL_BLOCK_SIZE)
};
}
static struct nofl_block_ref
nofl_block_null(void) {
return (struct nofl_block_ref) { NULL, 0 };
}
static int
nofl_block_is_null(struct nofl_block_ref block) {
return block.summary == NULL;
}
static uintptr_t
nofl_block_has_flag(struct nofl_block_ref block, uintptr_t flags) {
GC_ASSERT(!nofl_block_is_null(block));
return nofl_block_summary_has_flag(block.summary, flags);
}
static void
nofl_block_set_flag(struct nofl_block_ref block, uintptr_t flags) {
GC_ASSERT(!nofl_block_is_null(block));
nofl_block_summary_set_flag(block.summary, flags);
}
static void
nofl_block_clear_flag(struct nofl_block_ref block, uintptr_t flags) {
GC_ASSERT(!nofl_block_is_null(block));
nofl_block_summary_clear_flag(block.summary, flags);
}
static struct nofl_block_ref
nofl_block_next(struct nofl_block_ref block) {
GC_ASSERT(!nofl_block_is_null(block));
return nofl_block_for_addr(nofl_block_summary_next(block.summary));
}
static void
nofl_block_set_next(struct nofl_block_ref head, struct nofl_block_ref tail) {
GC_ASSERT(!nofl_block_is_null(head));
nofl_block_summary_set_next(head.summary, tail.addr);
}
static int
nofl_allocator_has_block(struct nofl_allocator *alloc) {
return !nofl_block_is_null(alloc->block);
}
static struct nofl_block_ref
nofl_block_head(struct nofl_block_list *list) {
uintptr_t head = gc_atomic_load(&list->blocks);
if (!head)
return nofl_block_null();
return (struct nofl_block_ref){ nofl_block_summary_for_addr(head), head };
}
static int
nofl_block_compare_and_exchange(struct nofl_block_list *list,
struct nofl_block_ref *expected,
struct nofl_block_ref desired) {
if (gc_atomic_cmpxchg_weak(&list->blocks, &expected->addr, desired.addr))
return 1;
expected->summary = nofl_block_summary_for_addr(expected->addr);
return 0;
}
static void
nofl_block_list_push(struct nofl_block_list *list,
struct nofl_block_ref block) {
gc_atomic_fetch_add(&list->count, 1);
GC_ASSERT(nofl_block_is_null(nofl_block_next(block)));
struct nofl_block_ref next = nofl_block_head(list);
do {
nofl_block_set_next(block, next);
} while (!nofl_block_compare_and_exchange(list, &next, block));
}
static struct nofl_block_ref
nofl_block_list_pop(struct nofl_block_list *list) {
struct nofl_block_ref head = nofl_block_head(list);
struct nofl_block_ref next;
do {
if (nofl_block_is_null(head))
return nofl_block_null();
next = nofl_block_next(head);
} while (!nofl_block_compare_and_exchange(list, &head, next));
nofl_block_set_next(head, nofl_block_null());
gc_atomic_fetch_sub(&list->count, 1);
return head;
}
static void
nofl_block_stack_push(struct nofl_block_stack *stack,
struct nofl_block_ref block,
const struct gc_lock *lock) {
struct nofl_block_list *list = &stack->list;
list->count++;
GC_ASSERT(nofl_block_is_null(nofl_block_next(block)));
struct nofl_block_ref next = nofl_block_head(list);
nofl_block_set_next(block, next);
list->blocks = block.addr;
}
static struct nofl_block_ref
nofl_block_stack_pop(struct nofl_block_stack *stack,
const struct gc_lock *lock) {
struct nofl_block_list *list = &stack->list;
struct nofl_block_ref head = nofl_block_head(list);
if (!nofl_block_is_null(head)) {
list->count--;
list->blocks = nofl_block_next(head).addr;
nofl_block_set_next(head, nofl_block_null());
}
return head;
}
static size_t
nofl_block_count(struct nofl_block_list *list) {
return gc_atomic_load(&list->count);
}
static void
nofl_push_unavailable_block(struct nofl_space *space,
struct nofl_block_ref block,
const struct gc_lock *lock) {
nofl_block_set_flag(block, NOFL_BLOCK_UNAVAILABLE);
nofl_block_stack_push(nofl_block_has_flag(block, NOFL_BLOCK_PAGED_OUT)
? &space->paged_out[NOFL_PAGE_OUT_QUEUE_SIZE-1]
: &space->paged_out[0],
block, lock);
}
static struct nofl_block_ref
nofl_pop_unavailable_block(struct nofl_space *space,
const struct gc_lock *lock) {
for (int age = 0; age < NOFL_PAGE_OUT_QUEUE_SIZE; age++) {
struct nofl_block_ref block =
nofl_block_stack_pop(&space->paged_out[age], lock);
if (!nofl_block_is_null(block)) {
nofl_block_clear_flag(block, NOFL_BLOCK_UNAVAILABLE);
return block;
}
}
return nofl_block_null();
}
static size_t
nofl_empty_block_count(struct nofl_space *space) {
struct gc_lock lock = nofl_space_lock(space);
size_t ret = nofl_block_count(&space->empty.list);
gc_lock_release(&lock);
return ret;
}
static void
nofl_push_empty_block(struct nofl_space *space,
struct nofl_block_ref block,
const struct gc_lock *lock) {
nofl_block_stack_push(&space->empty, block, lock);
}
static struct nofl_block_ref
nofl_pop_empty_block_with_lock(struct nofl_space *space,
const struct gc_lock *lock) {
return nofl_block_stack_pop(&space->empty, lock);
}
static struct nofl_block_ref
nofl_pop_empty_block(struct nofl_space *space) {
struct gc_lock lock = nofl_space_lock(space);
struct nofl_block_ref ret = nofl_pop_empty_block_with_lock(space, &lock);
gc_lock_release(&lock);
return ret;
}
static size_t
nofl_active_block_count(struct nofl_space *space) {
size_t total = space->nslabs * NOFL_NONMETA_BLOCKS_PER_SLAB;
size_t unavailable = 0;
for (int age = 0; age < NOFL_PAGE_OUT_QUEUE_SIZE; age++)
unavailable += nofl_block_count(&space->paged_out[age].list);
GC_ASSERT(unavailable <= total);
return total - unavailable;
}
static size_t
nofl_evacuation_block_count(struct nofl_space *space) {
return nofl_block_count(&space->evacuation_targets);
}
static int
nofl_maybe_push_evacuation_target(struct nofl_space *space,
struct nofl_block_ref block,
double reserve) {
size_t targets = nofl_evacuation_block_count(space);
size_t active = nofl_active_block_count(space);
if (targets >= ceil(active * reserve))
return 0;
nofl_block_list_push(&space->evacuation_targets, block);
return 1;
}
static int
nofl_push_evacuation_target_if_needed(struct nofl_space *space,
struct nofl_block_ref block) {
return nofl_maybe_push_evacuation_target(space, block,
space->evacuation_minimum_reserve);
}
static int
nofl_push_evacuation_target_if_possible(struct nofl_space *space,
struct nofl_block_ref block) {
return nofl_maybe_push_evacuation_target(space, block,
space->evacuation_reserve);
}
static inline void
nofl_clear_memory(uintptr_t addr, size_t size) {
memset((char*)addr, 0, size);
}
static size_t
nofl_space_live_object_granules(uint8_t *metadata) {
return scan_for_byte_with_bits(metadata, -1, NOFL_METADATA_BYTE_END) + 1;
}
static void
nofl_allocator_reset(struct nofl_allocator *alloc) {
alloc->alloc = alloc->sweep = 0;
alloc->block = nofl_block_null();
}
static void
nofl_allocator_init(struct nofl_allocator *alloc) {
nofl_allocator_reset(alloc);
nofl_holeset_clear(&alloc->holes);
}
static int
nofl_should_promote_block(struct nofl_space *space,
struct nofl_block_ref block) {
// If this block has mostly survivors, we can promote it to the old
// generation. Old-generation blocks won't be used for allocation
// until after the next full GC.
if (!GC_GENERATIONAL) return 0;
size_t threshold = NOFL_GRANULES_PER_BLOCK * space->promotion_threshold;
return block.summary->hole_granules < threshold;
}
static void
nofl_allocator_release_full_block(struct nofl_allocator *alloc,
struct nofl_space *space) {
GC_ASSERT(nofl_allocator_has_block(alloc));
struct nofl_block_ref block = alloc->block;
GC_ASSERT(alloc->alloc == alloc->sweep);
gc_atomic_fetch_add(&space->allocated_granules_since_last_collection,
block.summary->hole_granules);
gc_atomic_fetch_add(&space->survivor_granules_at_last_collection,
NOFL_GRANULES_PER_BLOCK - block.summary->hole_granules);
gc_atomic_fetch_add(&space->fragmentation_granules_since_last_collection,
block.summary->fragmentation_granules);
if (nofl_should_promote_block(space, block))
nofl_block_list_push(&space->promoted, block);
else
nofl_block_list_push(&space->full, block);
nofl_allocator_reset(alloc);
}
static void
nofl_allocator_release_full_evacuation_target(struct nofl_allocator *alloc,
struct nofl_space *space) {
GC_ASSERT(nofl_allocator_has_block(alloc));
struct nofl_block_ref block = alloc->block;
GC_ASSERT(alloc->alloc > block.addr);
GC_ASSERT(alloc->sweep == block.addr + NOFL_BLOCK_SIZE);
size_t hole_size = alloc->sweep - alloc->alloc;
// FIXME: Check how this affects statistics.
GC_ASSERT_EQ(block.summary->hole_count, 1);
GC_ASSERT_EQ(block.summary->hole_granules, NOFL_GRANULES_PER_BLOCK);
gc_atomic_fetch_add(&space->old_generation_granules,
NOFL_GRANULES_PER_BLOCK);
if (hole_size) {
hole_size >>= NOFL_GRANULE_SIZE_LOG_2;
block.summary->holes_with_fragmentation = 1;
block.summary->fragmentation_granules = hole_size / NOFL_GRANULE_SIZE;
} else {
GC_ASSERT_EQ(block.summary->fragmentation_granules, 0);
GC_ASSERT_EQ(block.summary->holes_with_fragmentation, 0);
}
nofl_block_list_push(&space->old, block);
nofl_allocator_reset(alloc);
}
static void
nofl_allocator_release_partly_full_block(struct nofl_allocator *alloc,
struct nofl_space *space) {
// A block can go on the partly full list if it has exactly one
// hole, located at the end of the block.
GC_ASSERT(nofl_allocator_has_block(alloc));
struct nofl_block_ref block = alloc->block;
GC_ASSERT(alloc->alloc > block.addr);
GC_ASSERT(alloc->sweep == block.addr + NOFL_BLOCK_SIZE);
size_t hole_size = alloc->sweep - alloc->alloc;
GC_ASSERT(hole_size);
block.summary->fragmentation_granules = hole_size / NOFL_GRANULE_SIZE;
struct gc_lock lock = nofl_space_lock(space);
nofl_block_stack_push(&space->partly_full, block, &lock);
gc_lock_release(&lock);
nofl_allocator_reset(alloc);
}
static size_t
nofl_allocator_acquire_partly_full_block(struct nofl_allocator *alloc,
struct nofl_space *space) {
struct gc_lock lock = nofl_space_lock(space);
struct nofl_block_ref block = nofl_block_stack_pop(&space->partly_full,
&lock);
gc_lock_release(&lock);
if (nofl_block_is_null(block))
return 0;
GC_ASSERT_EQ(block.summary->holes_with_fragmentation, 0);
alloc->block = block;
alloc->sweep = block.addr + NOFL_BLOCK_SIZE;
size_t hole_granules = block.summary->fragmentation_granules;
block.summary->fragmentation_granules = 0;
alloc->alloc = alloc->sweep - (hole_granules << NOFL_GRANULE_SIZE_LOG_2);
return hole_granules;
}
static size_t
nofl_allocator_acquire_empty_block(struct nofl_allocator *alloc,
struct nofl_space *space) {
struct nofl_block_ref block = nofl_pop_empty_block(space);
if (nofl_block_is_null(block))
return 0;
block.summary->hole_count = 1;
block.summary->hole_granules = NOFL_GRANULES_PER_BLOCK;
block.summary->holes_with_fragmentation = 0;
block.summary->fragmentation_granules = 0;
alloc->block = block;
alloc->alloc = block.addr;
alloc->sweep = block.addr + NOFL_BLOCK_SIZE;
if (nofl_block_has_flag(block, NOFL_BLOCK_ZERO))
nofl_block_clear_flag(block, NOFL_BLOCK_ZERO | NOFL_BLOCK_PAGED_OUT);
else
nofl_clear_memory(block.addr, NOFL_BLOCK_SIZE);
return NOFL_GRANULES_PER_BLOCK;
}
static size_t
nofl_allocator_acquire_evacuation_target(struct nofl_allocator* alloc,
struct nofl_space *space) {
size_t granules = nofl_allocator_acquire_partly_full_block(alloc, space);
if (granules)
return granules;
return nofl_allocator_acquire_empty_block(alloc, space);
}
static inline void
nofl_allocator_finish_hole(struct nofl_allocator *alloc, int collect_holes) {
size_t granules = (alloc->sweep - alloc->alloc) / NOFL_GRANULE_SIZE;
if (granules) {
if (collect_holes)
nofl_holeset_push_local(&alloc->holes, alloc->alloc, granules);
alloc->block.summary->holes_with_fragmentation++;
alloc->block.summary->fragmentation_granules += granules;
alloc->alloc = alloc->sweep;
}
}
static inline int
nofl_metadata_byte_has_mark(uint8_t byte, uint8_t marked) {
return (byte & NOFL_METADATA_BYTE_MARK_MASK) == marked;
}
static inline int
nofl_metadata_byte_is_young_or_has_mark(uint8_t byte, uint8_t marked) {
return (nofl_metadata_byte_has_mark(byte, NOFL_METADATA_BYTE_YOUNG)
|| nofl_metadata_byte_has_mark(byte, marked));
}
// Sweep some heap to reclaim free space, advancing alloc->alloc and
// alloc->sweep. Return the size of the hole in granules, or 0 if we
// reached the end of the block.
static size_t
nofl_allocator_next_hole_in_block(struct nofl_allocator *alloc,
uint8_t survivor_mark) {
GC_ASSERT(nofl_allocator_has_block(alloc));
GC_ASSERT_EQ(alloc->alloc, alloc->sweep);
uintptr_t sweep = alloc->sweep;
uintptr_t limit = alloc->block.addr + NOFL_BLOCK_SIZE;
if (sweep == limit)
return 0;
GC_ASSERT((sweep & (NOFL_GRANULE_SIZE - 1)) == 0);
uint8_t* metadata = nofl_metadata_byte_for_addr(sweep);
size_t limit_granules = (limit - sweep) >> NOFL_GRANULE_SIZE_LOG_2;
// Except for when we first get a block, alloc->sweep is positioned
// right after a hole, which can point to either the end of the
// block or to a live object. Assume that a live object is more
// common.
while (limit_granules &&
nofl_metadata_byte_has_mark(metadata[0], survivor_mark)) {
// Object survived collection; skip over it and continue sweeping.
size_t object_granules = nofl_space_live_object_granules(metadata);
sweep += object_granules * NOFL_GRANULE_SIZE;
limit_granules -= object_granules;
metadata += object_granules;
}
if (!limit_granules) {
GC_ASSERT_EQ(sweep, limit);
alloc->alloc = alloc->sweep = limit;
return 0;
}
size_t hole_granules = scan_for_byte_with_tag(metadata, limit_granules,
NOFL_METADATA_BYTE_MARK_MASK,
survivor_mark);
size_t free_bytes = hole_granules * NOFL_GRANULE_SIZE;
GC_ASSERT(hole_granules);
GC_ASSERT(hole_granules <= limit_granules);
memset(metadata, 0, hole_granules);
memset((char*)sweep, 0, free_bytes);
alloc->block.summary->hole_count++;
GC_ASSERT(hole_granules <=
NOFL_GRANULES_PER_BLOCK - alloc->block.summary->hole_granules);
alloc->block.summary->hole_granules += hole_granules;
alloc->alloc = sweep;
alloc->sweep = sweep + free_bytes;
return hole_granules;
}
static void
nofl_allocator_finish_sweeping_in_block(struct nofl_allocator *alloc,
uint8_t survivor_mark,
int collect_holes) {
do {
nofl_allocator_finish_hole(alloc, collect_holes);
} while (nofl_allocator_next_hole_in_block(alloc, survivor_mark));
}
static void
nofl_allocator_release_block(struct nofl_allocator *alloc,
struct nofl_space *space) {
GC_ASSERT(nofl_allocator_has_block(alloc));
if (alloc->alloc < alloc->sweep &&
alloc->sweep == alloc->block.addr + NOFL_BLOCK_SIZE &&
alloc->block.summary->holes_with_fragmentation == 0) {
nofl_allocator_release_partly_full_block(alloc, space);
} else if (space->evacuating) {
nofl_allocator_release_full_evacuation_target(alloc, space);
} else {
nofl_allocator_finish_sweeping_in_block(alloc, space->survivor_mark,
!space->evacuating);
nofl_allocator_release_full_block(alloc, space);
}
}
static void
nofl_allocator_finish(struct nofl_allocator *alloc, struct nofl_space *space) {
if (nofl_allocator_has_block(alloc))
nofl_allocator_release_block(alloc, space);
nofl_holeset_release(&alloc->holes, &space->holes, &space->lock);
}
static int
nofl_allocator_acquire_block_to_sweep(struct nofl_allocator *alloc,
struct nofl_space *space) {
struct nofl_block_ref block = nofl_block_list_pop(&space->to_sweep);
if (nofl_block_is_null(block))
return 0;
alloc->block = block;
alloc->alloc = alloc->sweep = block.addr;
return 1;
}
static size_t
nofl_allocator_next_hole_in_block_of_size(struct nofl_allocator *alloc,
struct nofl_space *space,
size_t min_granules) {
if (!nofl_allocator_has_block(alloc))
return 0;
while (1) {
nofl_allocator_finish_hole(alloc, min_granules && !space->evacuating);
size_t granules =
nofl_allocator_next_hole_in_block(alloc, space->survivor_mark);
if (granules == 0) {
nofl_allocator_release_full_block(alloc, space);
return 0;
}
else if (min_granules <= granules)
return granules;
}
}
static size_t
nofl_blocks_to_sweep_for_size(size_t granules) {
if (!granules)
return -1;
size_t max_granules = NOFL_GRANULES_PER_BLOCK;
GC_ASSERT(granules <= max_granules);
return __builtin_clzll(granules) - __builtin_clzll(max_granules);
}
static size_t
nofl_allocator_next_hole(struct nofl_allocator *alloc,
struct nofl_space *space,
size_t min_granules) {
// Sweep current block for a hole.
{
size_t granules =
nofl_allocator_next_hole_in_block_of_size(alloc, space, min_granules);
if (granules)
return granules;
}
// Current block
size_t blocks_to_sweep = nofl_blocks_to_sweep_for_size(min_granules);
while (1) {
size_t blocks_swept = 0;
for (;
blocks_swept < blocks_to_sweep
&& nofl_allocator_acquire_block_to_sweep(alloc, space);
blocks_swept++) {
// This block was marked in the last GC and needs sweeping.
// As we sweep we'll want to record how many bytes were live
// at the last collection. As we allocate we'll record how
// many granules were wasted because of fragmentation.
alloc->block.summary->hole_count = 0;
alloc->block.summary->hole_granules = 0;
alloc->block.summary->holes_with_fragmentation = 0;
alloc->block.summary->fragmentation_granules = 0;
size_t granules =
nofl_allocator_next_hole_in_block_of_size(alloc, space, min_granules);
if (granules)
return granules;
}
while (1) {
size_t granules = nofl_allocator_acquire_partly_full_block(alloc, space);
if (!granules)
break;
if (min_granules <= granules)
return granules;
nofl_allocator_finish_hole(alloc, !space->evacuating);
nofl_allocator_release_full_block(alloc, space);
}
// We are done sweeping for blocks. Now take from the empties list.
if (nofl_allocator_acquire_empty_block(alloc, space))
return NOFL_GRANULES_PER_BLOCK;
// Couldn't acquire another block; return 0 to cause collection.
if (blocks_swept == 0)
return 0;
}
}
static inline struct gc_ref
nofl_allocate_bump_pointer(struct nofl_allocator *alloc, size_t size,
enum gc_allocation_kind kind) {
GC_ASSERT(size <= alloc->sweep - alloc->alloc);
struct gc_ref ret = gc_ref(alloc->alloc);
alloc->alloc += size;
gc_update_alloc_table(ret, size, kind);
return ret;
}
static struct gc_ref
nofl_allocate_second_chance(struct nofl_allocator *alloc,
struct nofl_space *space, size_t granules) {
struct gc_ref local = nofl_holeset_try_pop(&alloc->holes, granules);
if (!gc_ref_is_null(local))
return local;
if (nofl_holeset_acquire(&alloc->holes, &space->holes, &space->lock,
granules))
return nofl_holeset_try_pop(&alloc->holes, granules);
return gc_ref_null();
}
static struct gc_ref
nofl_allocate_slow(struct nofl_allocator *alloc, struct nofl_space *space,
size_t size, enum gc_allocation_kind kind) {
size_t granules = size >> NOFL_GRANULE_SIZE_LOG_2;
if (nofl_allocator_next_hole_in_block_of_size(alloc, space, granules))
return nofl_allocate_bump_pointer(alloc, size, kind);
struct gc_ref second_chance =
nofl_allocate_second_chance(alloc, space, granules);
if (!gc_ref_is_null(second_chance)) {
gc_update_alloc_table(second_chance, size, kind);
gc_atomic_fetch_add(&space->second_chance_granules_since_last_collection,
granules);
return second_chance;
}
if (nofl_allocator_next_hole(alloc, space, granules))
return nofl_allocate_bump_pointer(alloc, size, kind);
return gc_ref_null();
}
static struct gc_ref
nofl_allocate(struct nofl_allocator *alloc, struct nofl_space *space,
size_t size, enum gc_allocation_kind kind) {
GC_ASSERT(size > 0);
GC_ASSERT(size <= gc_allocator_large_threshold());