Repository navigation
Expand file tree
/
Copy pathalloc.c
More file actions
2931 lines (2483 loc) · 75.2 KB
/
Copy pathalloc.c
File metadata and controls
2931 lines (2483 loc) · 75.2 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
/*
* Copyright (c) 1993-2012 David Gay and Gustav Hållberg
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY OR GUSTAV HALLBERG BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY OR
* GUSTAV HALLBERG HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY AND GUSTAV HALLBERG SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND DAVID GAY AND GUSTAV HALLBERG HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "mudlle-config.h"
#include <errno.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/mman.h>
#include "alloc.h"
#include "assoc.h"
#include "dwarf.h"
#include "jitdump.h"
#include "mvalgrind.h"
#include "profile.h"
#include "table.h"
#include "utils.h"
struct weak_ref {
struct weak_ref *next;
struct obj *obj;
void *cbdata;
weak_ref_free_fn cbfree;
};
/* List of all weak refs */
static struct weak_ref *weak_refs;
/* A semi-generational GC:
- is copying for mutable data
- a generational for immutable data.
A few principles:
- immutable data may not point at mutable data
- on each minor GC, the mutable data stays in gen 0, the immutable data
moves to gen 1
Advantages:
- 2/3 of the data is immutable and doesn't change much
=> a lot of effort is wasted in copying it.
- the allocation rate of very short lived information is rather
high (see above)
- Avoids having to keep track of assignments (esp. painful for the C code)
- the interpreter can easily enforce the 'immutable' clause.
Disadvantages:
- More implementation effort => more bugs
- What will happen to the memory requirements ?
(copying is 2x, generational is 3x, this may well be 4x)
- On each minor GC, all the mutable data is copied
=> minimise amount of mutable data ?
- GCDEBUG becomes more complicated
*/
/* New: generation of machine code complicates garbage collection:
- registers are now roots
- pc & (processor) stack are roots, but do not point to beginning
of objects
- code under execution may move
- flow of control may thread from C to machine code to C to ...
==> each call to machine code has an area reserved for it to save
its registers and stack when it calls C code. This area also contains the
stack pointer value when the machine code was called.
The set of all such active areas serves as GC roots. The stack areas
delimited in these areas are also examined for pointers into code objects.
The beginning of code objects is recognised by a special magic sequence
which is never generated as machine code (currently 8 bytes of 0xff is
used).
The instruction cache must be flushed after garbage collection.
Important optimisation: if no machine code objects are seen during
GC, there is no need to flush the cache (i.e. most minor collections).
*/
/* Offset from a multiple of CODE_ALIGNMENT at which to place struct mcode's so
that the mcode field is aligned on a multiple of CODE_ALIGNMENT */
#define MCODE_OFFSET PADDING(offsetof(struct mcode, mcode), CODE_ALIGNMENT)
/* adds 'bytes' bytes to pointer 'ptr' */
#define PTRADD(ptr, bytes) \
_Generic((ptr), \
/* add new types as needed */ \
uint64_t *: (uint64_t *)((ulong)(ptr) + (bytes)), \
struct obj *: (struct obj *)((ulong)(ptr) + (bytes)))
#define PTR_IN_RANGE(ptr, start, end) \
((uint8_t *)(ptr) >= (start) && (uint8_t *)(ptr) < (end))
/* Roots */
struct gcpro *gcpro; /* Local (C) variables which need protection */
static struct dynpro dynpro_head = {
.next = &dynpro_head,
.prev = &dynpro_head
};
#define MAXROOTS 100
static struct gc_root {
const char *desc, *file;
int line;
value *data;
} roots[MAXROOTS];
static ulong static_roots_used;
union free_obj {
struct grecord g;
union free_obj *next; /* reuses the size field */
};
CASSERT(offsetof(union free_obj, next)
== offsetof(union free_obj, g.o.size));
#define FREE_SIZES 12
static union free_obj *free_lists[FREE_SIZES];
enum gcmem_result {
gcmem_success = 0,
gcmem_grow_memory,
gcmem_out_of_memory
};
static bool alloc_can_fail; /* if true, gc_reserve() may longjmp() */
static sigjmp_buf nomem;
/* machine code roots */
static void forward_registers(void);
static void unmark_safe_registers(void);
static void save_restore_mcode(struct mcode *code);
static void save_restore(struct obj *obj);
#ifndef NOCOMPILER
static void scan_mcode(struct mcode *code);
#endif
static void flush_icache(const uint8_t *from, const uint8_t *to);
static struct weak_ref *minor_forward_weak_refs(void);
static struct weak_ref *major_forward_weak_refs(void);
static void free_weak_refs(struct weak_ref *node);
static struct assoc_array static_data_table = {
.type = &long_to_voidp_assoc_array_type
};
static void free_static_data_table(void)
{
assoc_array_free(&static_data_table);
}
static ulong *static_data(struct obj *obj)
{
return (ulong *)assoc_array_force_ref(&static_data_table, obj);
}
static inline ulong htonlong(ulong l)
{
#ifdef WORDS_BIGENDIAN
return l;
#else
CASSERT(sizeof l == 4 || sizeof l == 8);
if (sizeof l == 4)
return htonl(l);
/* __builtin_bswap64() is available in GCC versions 4.3 and later */
return __builtin_bswap64(l);
#endif
}
static inline ulong ntohlong(ulong l)
{
return htonlong(l);
}
void internal_staticpro(void *pro, const char *desc, const char *file,
int line)
{
assert(static_roots_used < MAXROOTS);
if (*desc == '&')
++desc;
roots[static_roots_used++] = (struct gc_root){
.data = pro,
.desc = desc,
.file = file,
.line = line
};
}
struct list *get_dynpro_data(void)
{
struct list *result = NULL;
GCPRO(result);
for (struct dynpro *dyn = dynpro_head.next;
dyn != &dynpro_head;
dyn = dyn->next)
{
struct vector *v = alloc_vector(3);
SET_VECTOR(v, 0, dyn->obj);
SET_VECTOR(v, 1, alloc_string(dyn->file));
SET_VECTOR(v, 2, makeint(dyn->lineno));
result = alloc_list(v, result);
}
UNGCPRO();
return result;
}
struct vector *get_staticpro_data(void)
{
struct vector *res = alloc_vector(static_roots_used);
GCPRO(res);
for (size_t i = 0; i < static_roots_used; ++i)
{
struct vector *v = alloc_vector(4);
v->data[2] = makeint(roots[i].line);
v->data[3] = *roots[i].data;
res->data[i] = v;
SET_VECTOR((struct vector *)res->data[i], 0,
alloc_string(roots[i].desc));
SET_VECTOR((struct vector *)res->data[i], 1,
alloc_string(roots[i].file));
}
UNGCPRO();
return res;
}
void internal_dynpro(struct dynpro *what, value obj, const char *file,
int lineno)
{
assert(what->next == NULL && what->prev == NULL);
GCCHECK(obj);
*what = (struct dynpro){
.next = dynpro_head.next,
.prev = &dynpro_head,
.obj = obj,
.file = file,
.lineno = lineno
};
dynpro_head.next->prev = what;
dynpro_head.next = what;
}
void internal_undynpro(struct dynpro *what, const char *file, int lineno)
{
what->prev->next = what->next;
what->next->prev = what->prev;
*what = (struct dynpro){ .file = file, .lineno = lineno };
}
void internal_maybe_dynpro(struct dynpro *what, value obj,
const char *file, int lineno)
{
if (staticp(what->obj))
{
assert(what->next == NULL && what->prev == NULL);
if (!staticp(obj))
{
internal_dynpro(what, obj, file, lineno);
return;
}
}
else
{
assert(what->next != NULL && what->prev != NULL);
if (staticp(obj))
undynpro(what);
}
GCCHECK(obj);
what->obj = obj;
what->file = file;
what->lineno = lineno;
}
struct dynpro *internal_protect(value v, const char *file, int lineno)
{
struct dynpro *newp = malloc(sizeof *newp);
*newp = (struct dynpro){ 0 };
internal_dynpro(newp, v, file, lineno);
return newp;
}
value internal_unprotect(struct dynpro *pro, const char *file, int lineno)
{
value v = pro->obj;
internal_undynpro(pro, file, lineno);
free(pro);
return v;
}
/* A semi-generational garbage collector */
/* The GC block */
uint8_t *gcblock;
ulong gcblocksize;
/* Current allocation state */
/* Memory for generation 0 extends from startgen0 to endgen0. Allocation
is downward, with posgen0 pointing at the start of the last allocation.
*/
extern uint8_t *endgen0; /* used by x64builtins.S */
uint8_t *startgen0, *endgen0, *posgen0;
static ulong minor_offset, save_offset;
static struct mcode_array mcode_array; /* currently seen mcode objects */
/* During GC, the newstart0, newend0, newpos0 variables fulfill the same roles
for the next copy of generation 0
*/
static uint8_t *newstart0, *newend0, *newpos0;
/* Memory for generation 1 extends from startgen1 to endgen1. No memory is
allocated directly. */
static uint8_t *startgen1;
#ifdef GCDEBUG
uint8_t *endgen1;
#else
static uint8_t *endgen1;
#endif
static ulong oldsize1;
/* During GC, newstart1, newend1 and newpos1 indicate the memory for the next
copy of generation 1. Allocation is upwards, with newpos1 pointing at the
first byte after the last allocation. */
static uint8_t *newstart1, *newend1, *newpos1;
/* During a major GC we may run out of memory. In that case we spill to a
temporary block, then increase the GC area. */
static uint8_t *tempblock1, *oldpos1, *oldstart1;
static ulong tempsize1;
static bool newarea; /* true during new_major_collection */
static ulong major_offset;
static bool saw_mcode; /* true if forwarded an mcode object */
static gc_forward_fn special_forward_obj;
#ifdef GCDEBUG
ulong minorgen, majorgen;
#else
static ulong minorgen;
#endif
static ulong newminorgen, newmajorgen;
struct gcstats gcstats;
#ifdef GCQDEBUG
static unsigned allowed_obj_flags = OBJ_READONLY | OBJ_IMMUTABLE;
void gccheck_qdebug(value x)
{
if (!pointerp(x))
return;
assert(((ulong)x & (sizeof (value) - 1)) == 0);
struct obj *o = x;
assert(!((o->size > MAX_MUDLLE_OBJECT_SIZE
&& o->garbage_type != garbage_forwarded)
|| o->size < sizeof *o
|| o->garbage_type >= garbage_free
|| o->type >= mudlle_types
|| o->flags & ~allowed_obj_flags));
}
#endif
/* Range affected by current GC */
static uint8_t *gcrange_start, *gcrange_end;
void set_gcblock_fork_mode(bool enable)
{
#ifdef __linux__
if (gcblocksize > 0
&& madvise(gcblock, gcblocksize,
enable ? MADV_DOFORK : MADV_DONTFORK) != 0)
perror("set_gcblock_fork_mode madvise()");
#endif
}
static void free_gc_block(void *b, size_t size)
{
if (b == NULL)
{
assert(size == 0);
return;
}
mprotect(b, size, PROT_READ | PROT_WRITE);
free(b);
}
static void *alloc_gc_block(size_t size)
{
void *b;
int r = posix_memalign(&b, pagesize, size);
if (r != 0)
{
assert(r == ENOMEM);
if (alloc_can_fail)
siglongjmp(nomem, gcmem_out_of_memory);
mudlle_log("alloc_gc_block(): out of memory");
abort();
}
if (mprotect(b, size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0)
perror("mprotect()");
return b;
}
struct obj *static_gone;
/* Garbage collector initialisation & cleanup */
void garbage_init(void)
{
/* All hell will break loose if this isn't true */
CASSERT(sizeof (value) == 4 || sizeof (value) == 8);
CASSERT(sizeof (ulong) == sizeof (value));
gcblocksize = INITIAL_BLOCKSIZE;
gcblock = alloc_gc_block(gcblocksize);
set_gcblock_fork_mode(false);
/* No first generation yet */
startgen1 = endgen1 = gcblock;
/* Divide remainder into 2 */
ulong half = (gcblocksize / 2) & ~(sizeof (value) - 1);
posgen0 = endgen0 = gcblock + gcblocksize;
startgen0 = endgen0 - half;
#ifdef GCDEBUG
minorgen = 98146523; /* Must be odd */
majorgen = 19643684; /* Must be even */
#endif
static_gone = allocate_ctemp(type_gone, sizeof *static_gone);
staticpro(&static_gone);
}
static value move_object(struct obj *obj, struct obj *newobj, long newgen)
{
long size = obj->size;
assert(obj->garbage_type < garbage_free);
GCCHECK(obj);
memcpy(newobj, obj, size);
#ifdef GCDEBUG
newobj->generation = newgen;
#endif
newobj = PTRADD(newobj, major_offset);
if (obj->garbage_type == garbage_static_string)
{
newobj->garbage_type = garbage_string;
*static_data(obj) = (long)newobj;
}
else
{
obj->garbage_type = garbage_forwarded;
obj->size = (long)newobj;
}
return newobj;
}
#define special_forward(ptr) maybe_forward(special_forward_obj, (ptr))
static void special_forward_code(struct code *code)
{
special_forward(&code->help);
special_forward(&code->filenames);
special_forward(&code->varname);
special_forward(&code->arguments);
special_forward(&code->linenos);
}
static void save_restore_code(struct code *code)
{
save_restore(&code->help->o);
save_restore(&code->filenames->o);
save_restore(&code->varname->o);
save_restore(&code->arguments->o);
save_restore(&code->linenos->o);
}
#define GRECORD_DATA(rec) ((struct obj **)((struct grecord *)(rec))->data)
#define GRECORD_END(rec) ((struct obj **)((char *)(rec) + (rec)->size))
#define for_grecords(rec, o) \
for (struct obj **o = GRECORD_DATA(rec), \
**const _recend = GRECORD_END(rec); \
(o) < _recend; \
++o)
static uint8_t *scan(uint8_t *ptr)
{
struct obj *obj = (struct obj *)ptr;
ptr += MUDLLE_ALIGN(obj->size, sizeof (value));
switch (obj->garbage_type)
{
case garbage_record:
case garbage_mtemp:
for_grecords (obj, o)
special_forward(o);
return ptr;
case garbage_icode:
{
struct icode *icode = (struct icode *)obj;
#ifndef NOCOMPILER
saw_mcode = true; /* code objects contain jump to interpreter */
#endif
special_forward_code(&icode->code);
value *const cend = icode->constants + icode->nb_constants;
for (value *c = icode->constants; c < cend; ++c)
special_forward(c);
return ptr;
}
case garbage_mcode:
#ifdef NOCOMPILER
abort();
#else
saw_mcode = true;
scan_mcode((struct mcode *)obj);
return ptr;
#endif
case garbage_primitive: case garbage_ctemp:
case garbage_string: case garbage_forwarded:
return ptr;
case garbage_static_string: case garbage_free:
abort();
}
abort();
}
#define MOVE_PAST_ZERO(data, endpos) \
while (data < endpos && *(ulong *)data == 0) data += sizeof (ulong)
static uint8_t *major_scan(uint8_t *data)
{
data = scan(data);
/* There may be holes filled with 0 bytes before code objects. Skip
over them */
MOVE_PAST_ZERO(data, newpos1);
return data;
}
static uint8_t *major_scan2(uint8_t *data)
{
data = scan(data);
/* There may be holes filled with 0 bytes before code objects. Skip
over them */
MOVE_PAST_ZERO(data, oldpos1);
return data;
}
static void safe_forward(value *ptr)
{
/* A C variable may be protected several times. It should be forwarded
only once */
if (pointerp(*ptr) && !((ulong)*ptr & 0x2)) /* Hack */
{
special_forward_obj(ptr);
*ptr = (value)((ulong)*ptr | 0x2);
}
}
static void unmark_safe(value *ptr)
{
if (pointerp(*ptr)) *ptr = (value)((ulong)*ptr & ~0x2);
}
static void forward_roots(void)
/* Forward all roots */
{
/* Forward activation stack */
forward_registers();
/* Static roots */
for (size_t i = 0; i < static_roots_used; i++)
safe_forward(roots[i].data);
/* Dynamic roots */
for (struct dynpro *dyn = dynpro_head.next;
dyn != &dynpro_head;
dyn = dyn->next)
special_forward(&dyn->obj);
/* Forward protected C vars */
for (struct gcpro *aprotect = gcpro; aprotect; aprotect = aprotect->next)
safe_forward(aprotect->obj);
/* Forward jmpbufs in ccontexts */
for (struct catch_context *mcc = catch_context; mcc; mcc = mcc->parent)
special_forward(&mcc->mjmpbuf);
/* Forward the call stack */
for (struct call_stack *stk = call_stack; stk; stk = stk->next)
{
struct call_stack_c *cstk = (struct call_stack_c *)stk;
switch (stk->type)
{
case call_c:
special_forward(&cstk->c.u.prim);
FALLTHROUGH;
case call_string_args:
case call_string_argv:
case call_primop:
for (int i = 0; i < cstk->c.nargs; ++i)
special_forward(&cstk->args[i]);
continue;
case call_mstring:
special_forward(&cstk->c.u.mstring);
continue;
case call_bytecode:
{
struct call_stack_mudlle *mstk = (struct call_stack_mudlle *)stk;
special_forward(&mstk->fn);
special_forward(&mstk->code);
special_forward(&mstk->locals);
continue;
}
case call_session:
case call_compiled:
continue;
case call_invalid:
case call_invalid_argp:
case call_invalid_argv:
special_forward(&cstk->c.u.value);
continue;
}
abort();
}
/* Forward the oports in the session_context */
for (struct session_context *sc = session_context; sc; sc = sc->parent)
{
special_forward(&sc->ports.out);
special_forward(&sc->ports.err);
}
forward_profile_roots(special_forward_obj);
/* Static roots */
for (size_t i = 0; i < static_roots_used; i++)
unmark_safe(roots[i].data);
/* Forward protected C vars */
for (struct gcpro *aprotect = gcpro; aprotect; aprotect = aprotect->next)
unmark_safe(aprotect->obj);
unmark_safe_registers();
}
#ifdef GCSTATS
static inline void gcstats_add_gen(struct obj *obj, ulong size, int gen)
{
gcstats.gen[gen].types[obj->type].nb++;
if (obj_readonlyp(obj))
gcstats.gen[gen].types[obj->type].rosize += size;
else
gcstats.gen[gen].types[obj->type].rwsize += size;
}
#endif
static bool minor_forward_immutablep(void *_ptr)
/* The forward function for minor collections; return true if object
is immutable */
{
struct obj **ptr = _ptr;
struct obj *obj = *ptr, *newobj;
long size = obj->size;
if (obj->garbage_type == garbage_forwarded)
{
*ptr = (struct obj *)size;
return (uint8_t *)size < newpos1; /* true iff forwarded to gen1 */
}
if (obj->garbage_type == garbage_static_string)
return true;
GCCHECK(obj);
if (obj->flags & OBJ_IMMUTABLE)
{
/* Immutable, forward to generation 1 */
/* In minor collections only bother with generation 0 */
if ((uint8_t *)obj < newpos1)
return true;
bool is_mcode = obj->garbage_type == garbage_mcode;
/* Gen 1 grows upward */
if (is_mcode)
/* We align the actual code on a 16 byte boundary. This is
a good idea on x86, and probably elsewhere too */
{
uint8_t *alignedpos =
(uint8_t *)(MUDLLE_ALIGN((ulong)newpos1, CODE_ALIGNMENT)
+ MCODE_OFFSET);
if (alignedpos - CODE_ALIGNMENT >= newpos1)
alignedpos -= CODE_ALIGNMENT;
/* We clear memory between newpos1 and alignedpos so that scan
can find the start of the code object */
if (alignedpos > newpos1)
memset(newpos1, 0, alignedpos - newpos1);
newpos1 = alignedpos;
}
newobj = (struct obj *)newpos1;
newpos1 += MUDLLE_ALIGN(size, sizeof (value));
memcpy(newobj, obj, size);
if (is_mcode)
{
struct mcode *mcode = (struct mcode *)newobj;
assert(TYPE(mcode, mcode));
ARRAY_ADD(mcode_array, mcode);
jitdump_move((struct mcode *)obj, mcode);
}
#ifdef GCSTATS
gcstats_add_gen(obj, size, 1);
#endif
#ifdef GCDEBUG
newobj->generation = newmajorgen;
#endif
obj->garbage_type = garbage_forwarded;
obj->size = (long)newobj;
*ptr = newobj;
return true;
}
/* Mutable, forward to generation 0 */
/* Must have been in gen 0 before */
/* assert(PTR_IN_RANGE(obj, startgen0, endgen0); */
newpos0 -= MUDLLE_ALIGN(size, sizeof (value));
newobj = (struct obj *)newpos0;
/*assert(newpos0 >= newstart0);*/
memcpy(newobj, obj, size);
#ifdef GCSTATS
gcstats_add_gen(obj, size, 0);
#endif
#ifdef GCDEBUG
newobj->generation = newminorgen;
#endif
obj->garbage_type = garbage_forwarded;
newobj = PTRADD(newobj, minor_offset);
obj->size = (long)newobj;
*ptr = newobj;
return false;
}
static void minor_forward(void *ptr)
{
minor_forward_immutablep(ptr);
}
static uint8_t *minor_scan(uint8_t *ptr)
{
struct obj *obj = (struct obj *)ptr;
ptr += MUDLLE_ALIGN(obj->size, sizeof (value));
/* Generation 0 never contains code, so only records contain pointers */
if (obj->garbage_type == garbage_record
|| obj->garbage_type == garbage_mtemp)
{
bool imm = (obj->flags & (OBJ_READONLY | OBJ_IMMUTABLE)) == OBJ_READONLY;
/* Scan & check if it can be made immutable */
for_grecords (obj, o)
if (pointerp(*o) && !minor_forward_immutablep(o))
imm = false;
/* Readonly objects can be made immutable if all their contents
are themselves immutable.
Note that this will not detect recursive immutable objects */
if (imm)
obj->flags |= OBJ_IMMUTABLE;
}
return ptr;
}
static void minor_collection(void)
{
uint8_t *unscanned0, *oldstart0, *data;
ulong nsize0;
saw_mcode = false;
special_forward_obj = minor_forward;
major_offset = 0;
/* Allocate below current block, but move it up afterwards */
newpos0 = newend0 = startgen0;
newstart0 = endgen1;
minor_offset = endgen0 - newend0;
newstart1 = startgen1;
newpos1 = endgen1; /* Add to end of gen1 */
newend1 = newend0;
gcrange_start = startgen0; gcrange_end = endgen0;
#ifdef GCDEBUG
newminorgen = minorgen + 2;
newmajorgen = majorgen;
#endif
++gcstats.minor_count;
#ifdef GCSTATS
/* Minor stats */
gcstats.gen[0] = GCSTATS_GEN_NULL;
#endif
forward_roots();
/* Scan mutable copied data */
/* Must scan forwards, but data is allocated downwards => double loop */
unscanned0 = newend0; /* Upper bound of unscanned data */
do
{
oldstart0 = data = newpos0;
while (data < unscanned0) data = minor_scan(data);
assert(data == unscanned0);
unscanned0 = oldstart0;
}
while (oldstart0 != newpos0); /* Till nothing forwarded */
/* Scan immutable data */
data = endgen1;
MOVE_PAST_ZERO(data, newpos1);
while (data < newpos1) data = major_scan(data);
assert(data == newpos1);
/* this must be done last */
struct weak_ref *weak_tofree = minor_forward_weak_refs();
/* Move new generation 0 into place */
nsize0 = newend0 - newpos0;
posgen0 = endgen0 - nsize0;
memmove(posgen0, newpos0, nsize0);
/* Flush cache if necessary */
if (saw_mcode)
flush_icache(gcblock, gcblock + gcblocksize);
/* Reset blocks */
endgen1 = newpos1;
/* endgen0 is unchanged */
/* Divide available mem into 2 */
nsize0 = (endgen0 - endgen1) / 2 & ~(sizeof (value) - 1);
startgen0 = endgen0 - nsize0;
/* NOTE: Here startgen0 may be > posgen0 !!! */
#ifdef GCDEBUG
minorgen = newminorgen;
#endif
#ifdef GCSTATS
gcstats.usage_minor = (ulong)(endgen0 - posgen0);
gcstats.usage_major = (ulong)(endgen1 - startgen1);
#endif
free_weak_refs(weak_tofree);
}
static void scan_temp(uint8_t *data)
{
/* Scan main & temp blocks */
MOVE_PAST_ZERO(data, oldpos1);
while (data < oldpos1) data = major_scan2(data);
assert(data == oldpos1);
data = newstart1;
MOVE_PAST_ZERO(data, newpos1);
while (data < newpos1) data = major_scan(data);
assert(data == newpos1);
}
static void major_forward(void *_ptr)
{
struct obj **ptr = _ptr;
struct obj *obj = *ptr, *newobj;
ulong size = obj->size;
long newgen;
if (obj->garbage_type == garbage_forwarded)
{
*ptr = (struct obj *)size;
return;
}
if (obj->garbage_type == garbage_static_string)
return;
GCCHECK(obj);
/* Objects in generation 0 stay in generation 0 during major
collections (even if they are immutable)
(Otherwise you can't scan them for roots to gen 1) */
bool is_mcode = false;
if (PTR_IN_RANGE(obj, posgen0, endgen0))
{
if (!newarea) return; /* No gen 0 data needs copying */
/* forward to new generation 0 */
newobj = (struct obj *)(newpos0 -= MUDLLE_ALIGN(size, sizeof (value)));
assert(newpos0 >= newstart0);
newgen = newminorgen;
#ifdef GCSTATS
gcstats_add_gen(obj, size, 0);
#endif
}
else
{
/* forward to new generation 1 */
/* Grows upward */
is_mcode = obj->garbage_type == garbage_mcode;
if (is_mcode)
/* We align the actual code on a 16 byte boundary. This is
a good idea on x86, and probably elsewhere too */
{
uint8_t *alignedpos = (uint8_t *)(
MUDLLE_ALIGN((ulong)newpos1 + major_offset, CODE_ALIGNMENT)
+ MCODE_OFFSET
- major_offset);
if (alignedpos - CODE_ALIGNMENT >= newpos1)
alignedpos -= CODE_ALIGNMENT;
/* We clear memory between newpos1 and alignedpos so
that scan can find the start of the code object */
if (alignedpos > newend1) alignedpos = newend1;
if (alignedpos > newpos1)
memset(newpos1, 0, alignedpos - newpos1);
newpos1 = alignedpos;
}
newobj = (struct obj *)newpos1;
newpos1 += MUDLLE_ALIGN(size, sizeof (value));
if (newpos1 > newend1) /* We are in trouble, spill to temp block */
{
/* Allocate a block big enough to handle the spill */
assert(tempblock1 == NULL && !newarea); /* Only once ! */
oldpos1 = (uint8_t *)newobj;
oldstart1 = newstart1;
ulong tsz1 = (endgen1 - startgen1) - (oldpos1 - oldstart1);
/* allocate extra space to handle code alignment overhead */
tempsize1 = (tsz1 * ((sizeof (struct mcode) + CODE_ALIGNMENT - 1.0)
/ sizeof (struct mcode)));
/* make sure there's no overflow */
assert(tempsize1 > tsz1);
#ifdef GCSTATS
mudlle_log("Temp block size %ld (%ld prealign)"
" remain %td already-moved %td",