-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinit-world.c
204 lines (171 loc) · 6.68 KB
/
init-world.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright (C) 2020 Sam Westrick.
* Copyright (C) 2011-2012,2014,2016 Matthew Fluet.
* Copyright (C) 1999-2008 Henry Cejtin, Matthew Fluet, Suresh
* Jagannathan, and Stephen Weeks.
* Copyright (C) 1997-2000 NEC Research Institute.
*
* MLton is released under a HPND-style license.
* See the file MLton-LICENSE for details.
*/
/* ---------------------------------------------------------------- */
/* Initialization */
/* ---------------------------------------------------------------- */
size_t sizeofInitialBytesLive (GC_state s) {
uint32_t i;
size_t dataBytes;
size_t total;
total = 0;
for (i = 0; i < s->vectorInitsLength; ++i) {
dataBytes =
s->vectorInits[i].elementSize
* s->vectorInits[i].length;
total += align (GC_SEQUENCE_METADATA_SIZE + dataBytes, s->alignment);
}
return total;
}
void initVectors(GC_state s, GC_thread thread) {
struct GC_vectorInit *inits;
HM_chunk currentChunk;
pointer frontier;
pointer limit;
uint32_t i;
assert(isFrontierAligned(s, s->frontier));
inits = s->vectorInits;
frontier = s->frontier;
limit = s->limitPlusSlop;
currentChunk = HM_getChunkOf(frontier);
assert(currentChunk == thread->currentChunk);
assert(0 == thread->currentDepth);
for (i = 0; i < s->vectorInitsLength; i++) {
size_t elementSize;
size_t dataBytes;
size_t objectSize;
uint32_t typeIndex;
elementSize = inits[i].elementSize;
dataBytes = elementSize * inits[i].length;
objectSize = align(GC_SEQUENCE_METADATA_SIZE + dataBytes, s->alignment);
#if ASSERT
assert(limit == HM_getChunkLimit(currentChunk));
assert(frontier >= HM_getChunkFrontier(currentChunk));
assert(frontier <= limit);
#endif
/* Extend with a new chunk, if there is not enough free space or if we have
* crossed a block boundary. */
if ((size_t)(limit - frontier) < objectSize ||
!inFirstBlockOfChunk(currentChunk, frontier + GC_SEQUENCE_METADATA_SIZE))
{
HM_HH_updateValues(thread, frontier);
if (!HM_HH_extend(s, thread, objectSize)) {
DIE("Ran out of space for Hierarchical Heap!");
}
s->frontier = HM_HH_getFrontier(thread);
s->limitPlusSlop = HM_HH_getLimit(thread);
s->limit = s->limitPlusSlop - GC_HEAP_LIMIT_SLOP;
frontier = s->frontier;
limit = s->limitPlusSlop;
currentChunk = HM_getChunkOf(frontier);
assert(currentChunk == thread->currentChunk);
}
assert(isFrontierAligned(s, frontier));
assert((size_t)(limit - frontier) >= objectSize);
assert(inFirstBlockOfChunk(currentChunk, frontier + GC_SEQUENCE_METADATA_SIZE));
*((GC_sequenceCounter*)(frontier)) = 0;
frontier = frontier + GC_SEQUENCE_COUNTER_SIZE;
*((GC_sequenceLength*)(frontier)) = inits[i].length;
frontier = frontier + GC_SEQUENCE_LENGTH_SIZE;
switch (elementSize) {
case 1:
typeIndex = WORD8_VECTOR_TYPE_INDEX;
break;
case 2:
typeIndex = WORD16_VECTOR_TYPE_INDEX;
break;
case 4:
typeIndex = WORD32_VECTOR_TYPE_INDEX;
break;
case 8:
typeIndex = WORD64_VECTOR_TYPE_INDEX;
break;
default:
die ("unknown element size in vectorInit: %"PRIuMAX"",
(uintmax_t)elementSize);
}
*((GC_header*)(frontier)) = buildHeaderFromTypeIndex (typeIndex);
frontier = frontier + GC_HEADER_SIZE;
// *((objptr*)(frontier)) = BOGUS_OBJPTR;
// frontier = frontier + OBJPTR_SIZE;
s->globals[inits[i].globalIndex] = pointerToObjptr(frontier, NULL);
if (DEBUG_DETAILED)
fprintf (stderr, "allocated vector at "FMTPTR"\n",
(uintptr_t)(s->globals[inits[i].globalIndex]));
memcpy (frontier, inits[i].words, dataBytes);
frontier += objectSize - GC_SEQUENCE_METADATA_SIZE;
}
s->frontier = frontier;
/* If the last allocation passed a block boundary, we need to extend to have
* a valid frontier. Extending with GC_HEAP_LIMIT_SLOP is arbitrary. */
if (!inFirstBlockOfChunk(currentChunk, frontier + GC_SEQUENCE_METADATA_SIZE))
{
HM_HH_updateValues(thread, frontier);
if (!HM_HH_extend(s, thread, GC_HEAP_LIMIT_SLOP)) {
DIE("Ran out of space for Hierarchical Heap!");
}
s->frontier = HM_HH_getFrontier(thread);
s->limitPlusSlop = HM_HH_getLimit(thread);
s->limit = s->limitPlusSlop - GC_HEAP_LIMIT_SLOP;
frontier = s->frontier;
limit = s->limitPlusSlop;
currentChunk = HM_getChunkOf(frontier);
assert(currentChunk == thread->currentChunk);
}
assert(isFrontierAligned(s, s->frontier));
assert(inFirstBlockOfChunk(currentChunk, s->frontier + GC_SEQUENCE_METADATA_SIZE));
}
GC_thread initThreadAndHeap(GC_state s, uint32_t depth) {
GC_thread thread = newThreadWithHeap(s, sizeofStackInitialReserved(s), depth);
s->frontier = HM_HH_getFrontier(thread);
s->limitPlusSlop = HM_HH_getLimit(thread);
s->limit = s->limitPlusSlop - GC_HEAP_LIMIT_SLOP;
#if ASSERT
HM_chunk current = HM_getChunkOf(s->frontier);
assert(HM_HH_getDepth(thread->hierarchicalHeap) == depth);
assert(current == thread->currentChunk);
assert(current->mightContainMultipleObjects);
assert(inFirstBlockOfChunk(current, s->frontier));
assert(s->frontier >= HM_getChunkFrontier(current));
assert(s->limitPlusSlop == HM_getChunkLimit(current));
assert(s->limit == s->limitPlusSlop - GC_HEAP_LIMIT_SLOP);
#endif
switchToThread(s, pointerToObjptr((pointer)thread - offsetofThread(s), NULL));
return thread;
}
void initWorld(GC_state s) {
for (uint32_t i = 0; i < s->globalsLength; ++i)
s->globals[i] = BOGUS_OBJPTR;
GC_thread thread = initThreadAndHeap(s, 0);
struct HM_HierarchicalHeap *hh = thread->hierarchicalHeap;
/* Copy vectors into the heap, implicitly updating
* s->{frontier,limit,limitPlusSlop} */
initVectors(s, thread);
size_t currentSize = HM_getChunkListSize(HM_HH_getChunkList(hh));
/* SAM_NOTE: some of these statistics may be maintained incorrectly
* elsewhere in the runtime. */
s->cumulativeStatistics->bytesAllocated += currentSize;
s->lastMajorStatistics->bytesLive = sizeofInitialBytesLive(s);
#if ASSERT
HM_chunk current = HM_getChunkOf(s->frontier);
assert(HM_HH_getDepth(hh) == 0);
assert(current == thread->currentChunk);
assert(current->mightContainMultipleObjects);
assert(inFirstBlockOfChunk(current, s->frontier));
assert(s->frontier >= HM_getChunkFrontier(current));
assert(s->limitPlusSlop == HM_getChunkLimit(current));
assert(s->limit == s->limitPlusSlop - GC_HEAP_LIMIT_SLOP);
#endif
}
void duplicateWorld (GC_state d, GC_state s) {
d->lastMajorStatistics->bytesLive = 0;
initThreadAndHeap(d, 0);
/* Now copy stats, heap data from original */
d->cumulativeStatistics->maxHeapSize = s->cumulativeStatistics->maxHeapSize;
}