Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / gc / pack.c
CommitLineData
7f918cf1
CE
1/* Copyright (C) 2012 Matthew Fluet.
2 * Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
3 * Jagannathan, and Stephen Weeks.
4 * Copyright (C) 1997-2000 NEC Research Institute.
5 *
6 * MLton is released under a BSD-style license.
7 * See the file MLton-LICENSE for details.
8 */
9
10void GC_pack (GC_state s) {
11 size_t keep;
12
13 enter (s);
14 if (DEBUG or s->controls.messages)
15 fprintf (stderr,
16 "[GC: Packing heap at "FMTPTR" of size %s bytes.]\n",
17 (uintptr_t)(s->heap.start),
18 uintmaxToCommaString(s->heap.size));
19 /* Could put some code here to skip the GC if there hasn't been much
20 * allocated since the last collection. But you would still need to
21 * do a minor GC to make all objects contiguous.
22 */
23 performGC (s, 0, 0, TRUE, FALSE);
24 keep = (size_t)(s->heap.oldGenSize * 1.1);
25 if (keep <= s->heap.size) {
26 shrinkHeap (s, &s->heap, keep);
27 setCardMapAndCrossMap (s);
28 setGCStateCurrentHeap (s, 0, 0);
29 setGCStateCurrentThreadAndStack (s);
30 }
31 releaseHeap (s, &s->secondaryHeap);
32 if (DEBUG or s->controls.messages)
33 fprintf (stderr,
34 "[GC: Packed heap at "FMTPTR" to size %s bytes.]\n",
35 (uintptr_t)(s->heap.start),
36 uintmaxToCommaString(s->heap.size));
37 leave (s);
38}
39
40void GC_unpack (GC_state s) {
41 enter (s);
42 if (DEBUG or s->controls.messages)
43 fprintf (stderr,
44 "[GC: Unpacking heap at "FMTPTR" of size %s bytes.]\n",
45 (uintptr_t)(s->heap.start),
46 uintmaxToCommaString(s->heap.size));
47 /* The enterGC is needed here because minorGC and resizeHeap might
48 * move the stack, and the SIGPROF catcher would then see a bogus
49 * stack. The leaveGC has to happen after the setStack.
50 */
51 enterGC (s);
52 minorGC (s);
53 resizeHeap (s, s->heap.oldGenSize);
54 setCardMapAndCrossMap (s);
55 resizeHeapSecondary (s);
56 setGCStateCurrentHeap (s, 0, 0);
57 setGCStateCurrentThreadAndStack (s);
58 leaveGC (s);
59 if (DEBUG or s->controls.messages)
60 fprintf (stderr,
61 "[GC: Unpacked heap at "FMTPTR" to size %s bytes.]\n",
62 (uintptr_t)(s->heap.start),
63 uintmaxToCommaString(s->heap.size));
64 leave (s);
65}