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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 043new.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.traceContains { color: #008000; }
.SalientComment { color: #00ffff; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Comment { color: #9090ff; }
.Delimiter { color: #a04060; }
.Special { color: #ff6060; }
.Identifier { color: #804000; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: A simple memory allocator to create space for new variables at runtime.</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario new)</span>
<span class="Comment"># call new two times with identical arguments; you should get back different results</span>
recipe main [
<span class="Constant">1</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">3</span>:boolean/<span class="Special">raw <- </span>equal <span class="Constant">1</span>:address:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">2</span>:address:number/<span class="Special">raw</span>
]
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Delimiter">:(before "End Globals")</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> Reserved_for_tests = <span class="Constant">1000</span><span class="Delimiter">;</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> Memory_allocated_until = Reserved_for_tests<span class="Delimiter">;</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> Initial_memory_per_routine = <span class="Constant">100000</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Memory_allocated_until = Reserved_for_tests<span class="Delimiter">;</span>
Initial_memory_per_routine = <span class="Constant">100000</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End routine Fields")</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> alloc<span class="Delimiter">,</span> alloc_max<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End routine Constructor")</span>
alloc = Memory_allocated_until<span class="Delimiter">;</span>
Memory_allocated_until += Initial_memory_per_routine<span class="Delimiter">;</span>
alloc_max = Memory_allocated_until<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"routine allocated memory from "</span> << alloc << <span class="Constant">" to "</span> << alloc_max << end<span class="Delimiter">();</span>
<span class="SalientComment">//:: First handle 'type' operands.</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
Type_ordinal[<span class="Constant">"type"</span>] = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">:(after "Per-recipe Transforms")</span>
<span class="Comment">// replace type names with type_ordinals</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>operation == Recipe_ordinal[<span class="Constant">"new"</span>]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// End NEW Transform Special-cases</span>
<span class="Comment">// first arg must be of type 'type'</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">())</span>
raise << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">": 'new' expects one or two ingredients</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
|| inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
|| inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != <span class="Constant">"type"</span><span class="Delimiter">)</span>
raise << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">": first ingredient of 'new' should be a type, but got "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">.</span>find<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">)</span> == Type_ordinal<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
raise << Recipe[r]<span class="Delimiter">.</span>name << <span class="Constant">": unknown type "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>set_value<span class="Delimiter">(</span>Type_ordinal[inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name]<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name << <span class="Constant">" -> "</span> << inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name << end<span class="Delimiter">();</span>
<span class="Normal">end_new_transform</span>:<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Now implement the primitive recipe.</span>
<span class="Comment">//: todo: build 'new' in mu itself</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
NEW<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_ordinal[<span class="Constant">"new"</span>] = NEW<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> NEW: <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">()</span> || SIZE<span class="Delimiter">(</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": 'new' requires one or two ingredients, but got "</span> << current_instruction<span class="Delimiter">().</span>to_string<span class="Delimiter">()</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": first ingredient of 'new' should be a type, but got "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// compute the space we need</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> size = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> array_length = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">{</span>
vector<type_ordinal> type<span class="Delimiter">;</span>
type<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>value<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// array</span>
array_length = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"array size is "</span> << array_length << end<span class="Delimiter">();</span>
size = array_length*size_of<span class="Delimiter">(</span>type<span class="Delimiter">)</span> + <span class="Comment">/*</span><span class="Comment">space for length</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Delimiter">{</span>
<span class="Comment">// scalar</span>
size = size_of<span class="Delimiter">(</span>type<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">//? Total_alloc += size; //? 1</span>
<span class="CommentedCode">//? Num_alloc++; //? 1</span>
<span class="Comment">// compute the region of memory to return</span>
<span class="Comment">// really crappy at the moment</span>
ensure_space<span class="Delimiter">(</span>size<span class="Delimiter">);</span>
<span class="Normal">const</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"new alloc: "</span> << result << end<span class="Delimiter">();</span>
<span class="Comment">// save result</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Comment">// initialize allocated space</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> address = result<span class="Delimiter">;</span> address < result+size<span class="Delimiter">;</span> ++address<span class="Delimiter">)</span> <span class="Delimiter">{</span>
Memory[address] = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
Memory[result] = array_length<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// bump</span>
Current_routine<span class="Delimiter">-></span>alloc += size<span class="Delimiter">;</span>
<span class="Comment">// no support for reclaiming memory</span>
assert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>alloc <= Current_routine<span class="Delimiter">-></span>alloc_max<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="CommentedCode">//? :(before "End Globals") //? 1</span>
<span class="CommentedCode">//? long long int Total_alloc = 0; //? 1</span>
<span class="CommentedCode">//? long long int Num_alloc = 0; //? 1</span>
<span class="CommentedCode">//? long long int Total_free = 0; //? 1</span>
<span class="CommentedCode">//? long long int Num_free = 0; //? 1</span>
<span class="CommentedCode">//? :(before "End Setup") //? 1</span>
<span class="CommentedCode">//? Total_alloc = Num_alloc = Total_free = Num_free = 0; //? 1</span>
<span class="CommentedCode">//? :(before "End Teardown") //? 1</span>
<span class="CommentedCode">//? cerr << Total_alloc << "/" << Num_alloc //? 1</span>
<span class="CommentedCode">//? << " vs " << Total_free << "/" << Num_free << '\n'; //? 1</span>
<span class="CommentedCode">//? cerr << SIZE(Memory) << '\n'; //? 1</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> ensure_space<span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> size<span class="Delimiter">)</span> <span class="Delimiter">{</span>
assert<span class="Delimiter">(</span>size <= Initial_memory_per_routine<span class="Delimiter">);</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>alloc + size > Current_routine<span class="Delimiter">-></span>alloc_max<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// waste the remaining space and create a new chunk</span>
Current_routine<span class="Delimiter">-></span>alloc = Memory_allocated_until<span class="Delimiter">;</span>
Memory_allocated_until += Initial_memory_per_routine<span class="Delimiter">;</span>
Current_routine<span class="Delimiter">-></span>alloc_max = Memory_allocated_until<span class="Delimiter">;</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"routine allocated memory from "</span> << Current_routine<span class="Delimiter">-></span>alloc << <span class="Constant">" to "</span> << Current_routine<span class="Delimiter">-></span>alloc_max << end<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario new_initializes)</span>
<span class="Special">% Memory_allocated_until = 10;</span>
<span class="Special">% Memory[Memory_allocated_until] = 1;</span>
recipe main [
<span class="Constant">1</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy *<span class="Constant">1</span>:address:number
]
<span class="traceContains">+mem: storing 0 in location 2</span>
<span class="Delimiter">:(scenario new_array)</span>
recipe main [
<span class="Constant">1</span>:address:array:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">5</span>
<span class="Constant">2</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">3</span>:number/<span class="Special">raw <- </span>subtract <span class="Constant">2</span>:address:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:array:number/<span class="Special">raw</span>
]
<span class="traceContains">+run: 1:address:array:number/raw <- new number:type, 5</span>
<span class="traceContains">+mem: array size is 5</span>
<span class="Comment"># don't forget the extra location for array size</span>
<span class="traceContains">+mem: storing 6 in location 3</span>
<span class="Delimiter">:(scenario new_empty_array)</span>
recipe main [
<span class="Constant">1</span>:address:array:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">0</span>
<span class="Constant">2</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">3</span>:number/<span class="Special">raw <- </span>subtract <span class="Constant">2</span>:address:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:array:number/<span class="Special">raw</span>
]
<span class="traceContains">+run: 1:address:array:number/raw <- new number:type, 0</span>
<span class="traceContains">+mem: array size is 0</span>
<span class="traceContains">+mem: storing 1 in location 3</span>
<span class="Comment">//: Make sure that each routine gets a different alloc to start.</span>
<span class="Delimiter">:(scenario new_concurrent)</span>
recipe f1 [
start-running f2:recipe
<span class="Constant">1</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Comment"># wait for f2 to complete</span>
<span class="Delimiter">{</span>
loop-unless <span class="Constant">4</span>:number/<span class="Special">raw</span>
<span class="Delimiter">}</span>
]
recipe f2 [
<span class="Constant">2</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Comment"># hack: assumes scheduler implementation</span>
<span class="Constant">3</span>:boolean/<span class="Special">raw <- </span>equal <span class="Constant">1</span>:address:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">2</span>:address:number/<span class="Special">raw</span>
<span class="Comment"># signal f2 complete</span>
<span class="Constant">4</span>:number/<span class="Special">raw <- </span>copy <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Comment">//: If a routine runs out of its initial allocation, it should allocate more.</span>
<span class="Delimiter">:(scenario new_overflow)</span>
<span class="Special">% Initial_memory_per_routine = 2;</span>
recipe main [
<span class="Constant">1</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:point/<span class="Special">raw <- </span><span class="Normal">new</span> point:type <span class="Comment"># not enough room in initial page</span>
]
<span class="traceContains">+new: routine allocated memory from 1000 to 1002</span>
<span class="traceContains">+new: routine allocated memory from 1002 to 1004</span>
<span class="Comment">//: We also provide a way to return memory, and to reuse reclaimed memory.</span>
<span class="Comment">//: todo: custodians, etc. Following malloc/free is a temporary hack.</span>
<span class="Delimiter">:(scenario new_reclaim)</span>
recipe main [
<span class="Constant">1</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
abandon <span class="Constant">1</span>:address:number
<span class="Constant">2</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type <span class="Comment"># must be same size as abandoned memory to reuse</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:address:number<span class="Delimiter">,</span> <span class="Constant">2</span>:address:number
]
<span class="Comment"># both allocations should have returned the same address</span>
<span class="traceContains">+mem: storing 1 in location 3</span>
<span class="Delimiter">:(before "End Globals")</span>
map<<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span>> Free_list<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Setup")</span>
Free_list<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
ABANDON<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_ordinal[<span class="Constant">"abandon"</span>] = ABANDON<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> ABANDON: <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": 'abandon' requires one ingredient, but got '"</span> << current_instruction<span class="Delimiter">().</span>to_string<span class="Delimiter">()</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)))</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": first ingredient of 'abandon' should be an address, but got "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> address = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
reagent types = canonize<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>types<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != Type_ordinal[<span class="Constant">"address"</span>]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": first ingredient of 'abandon' should be an address, but got "</span> << current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>original_string << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
reagent target_type = lookup_memory<span class="Delimiter">(</span>types<span class="Delimiter">);</span>
abandon<span class="Delimiter">(</span>address<span class="Delimiter">,</span> size_of<span class="Delimiter">(</span>target_type<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">void</span> abandon<span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> address<span class="Delimiter">,</span> <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> size<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? Total_free += size; //? 1</span>
<span class="CommentedCode">//? Num_free++; //? 1</span>
<span class="CommentedCode">//? cerr << "abandon: " << size << '\n'; //? 2</span>
<span class="Comment">// clear memory</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> curr = address<span class="Delimiter">;</span> curr < address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span>
Memory[curr] = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Comment">// append existing free list to address</span>
Memory[address] = Free_list[size]<span class="Delimiter">;</span>
Free_list[size] = address<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "ensure_space(size)" following "case NEW")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Free_list[size]<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> result = Free_list[size]<span class="Delimiter">;</span>
Free_list[size] = Memory[result]<span class="Delimiter">;</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> curr = result+<span class="Constant">1</span><span class="Delimiter">;</span> curr < result+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>Memory[curr] != <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << current_recipe_name<span class="Delimiter">()</span> << <span class="Constant">": memory in free list was not zeroed out: "</span> << curr << <span class="Constant">'/'</span> << result << <span class="Constant">"; somebody wrote to us after free!!!</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span>
<span class="Identifier">break</span><span class="Delimiter">;</span> <span class="Comment">// always fatal</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">1</span><span class="Delimiter">)</span>
Memory[result] = array_length<span class="Delimiter">;</span>
<span class="Normal">else</span>
Memory[result] = <span class="Constant">0</span><span class="Delimiter">;</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario new_differing_size_no_reclaim)</span>
recipe main [
<span class="Constant">1</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
abandon <span class="Constant">1</span>:address:number
<span class="Constant">2</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">2</span> <span class="Comment"># different size</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:address:number<span class="Delimiter">,</span> <span class="Constant">2</span>:address:number
]
<span class="Comment"># no reuse</span>
<span class="traceContains">+mem: storing 0 in location 3</span>
<span class="Delimiter">:(scenario new_reclaim_array)</span>
recipe main [
<span class="Constant">1</span>:address:array:number<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">2</span>
abandon <span class="Constant">1</span>:address:array:number
<span class="Constant">2</span>:address:array:number<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">2</span>
<span class="Constant">3</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:address:array:number<span class="Delimiter">,</span> <span class="Constant">2</span>:address:array:number
]
<span class="Comment"># reuse</span>
<span class="traceContains">+mem: storing 1 in location 3</span>
<span class="SalientComment">//:: Next, extend 'new' to handle a unicode string literal argument.</span>
<span class="Delimiter">:(scenario new_string)</span>
recipe main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span><span class="Normal">new</span> [abc def]
<span class="Constant">2</span>:character<span class="Special"> <- </span>index *<span class="Constant">1</span>:address:array:character<span class="Delimiter">,</span> <span class="Constant">5</span>
]
<span class="Comment"># number code for 'e'</span>
<span class="traceContains">+mem: storing 101 in location 2</span>
<span class="Delimiter">:(scenario new_string_handles_unicode)</span>
recipe main [
<span class="Constant">1</span>:address:array:character<span class="Special"> <- </span><span class="Normal">new</span> [a«c]
<span class="Constant">2</span>:number<span class="Special"> <- </span>length *<span class="Constant">1</span>:address:array:character
<span class="Constant">3</span>:character<span class="Special"> <- </span>index *<span class="Constant">1</span>:address:array:character<span class="Delimiter">,</span> <span class="Constant">1</span>
]
<span class="traceContains">+mem: storing 3 in location 2</span>
<span class="Comment"># unicode for '«'</span>
<span class="traceContains">+mem: storing 171 in location 3</span>
<span class="Delimiter">:(before "End NEW Transform Special-cases")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
&& !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
&& !inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>empty<span class="Delimiter">()</span>
&& inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">"literal-string"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// skip transform</span>
inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>initialized = <span class="Constant">true</span><span class="Delimiter">;</span>
<span class="Identifier">goto</span> end_new_transform<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after "case NEW" following "Primitive Recipe Implementations")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_literal<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span>
&& current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> == <span class="Constant">"literal-string"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>new_mu_string<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>name<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> new_mu_string<span class="Delimiter">(</span><span class="Normal">const</span> string& contents<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// allocate an array just large enough for it</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> string_length = unicode_length<span class="Delimiter">(</span>contents<span class="Delimiter">);</span>
<span class="CommentedCode">//? cout << "string_length is " << string_length << '\n'; //? 1</span>
<span class="CommentedCode">//? Total_alloc += string_length+1; //? 1</span>
<span class="CommentedCode">//? Num_alloc++; //? 1</span>
ensure_space<span class="Delimiter">(</span>string_length+<span class="Constant">1</span><span class="Delimiter">);</span> <span class="Comment">// don't forget the extra location for array size</span>
<span class="Comment">// initialize string</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
Memory[Current_routine<span class="Delimiter">-></span>alloc++] = string_length<span class="Delimiter">;</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> curr = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">const</span> <span class="Normal">char</span>* raw_contents = contents<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < string_length<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">uint32_t</span> curr_character<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>curr < SIZE<span class="Delimiter">(</span>contents<span class="Delimiter">));</span>
tb_utf8_char_to_unicode<span class="Delimiter">(</span>&curr_character<span class="Delimiter">,</span> &raw_contents[curr]<span class="Delimiter">);</span>
Memory[Current_routine<span class="Delimiter">-></span>alloc] = curr_character<span class="Delimiter">;</span>
curr += tb_utf8_char_length<span class="Delimiter">(</span>raw_contents[curr]<span class="Delimiter">);</span>
++Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">// mu strings are not null-terminated in memory</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: Allocate more to routine when initializing a literal string</span>
<span class="Delimiter">:(scenario new_string_overflow)</span>
<span class="Special">% Initial_memory_per_routine = 2;</span>
recipe main [
<span class="Constant">1</span>:address:number/<span class="Special">raw <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:array:character/<span class="Special">raw <- </span><span class="Normal">new</span> [a] <span class="Comment"># not enough room in initial page, if you take the array size into account</span>
]
<span class="traceContains">+new: routine allocated memory from 1000 to 1002</span>
<span class="traceContains">+new: routine allocated memory from 1002 to 1004</span>
<span class="Comment">//: helpers</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> unicode_length<span class="Delimiter">(</span><span class="Normal">const</span> string& s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">const</span> <span class="Normal">char</span>* in = s<span class="Delimiter">.</span>c_str<span class="Delimiter">();</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> curr = <span class="Constant">0</span><span class="Delimiter">;</span>
<span class="Normal">while</span> <span class="Delimiter">(</span>curr < SIZE<span class="Delimiter">(</span>s<span class="Delimiter">))</span> <span class="Delimiter">{</span> <span class="Comment">// carefully bounds-check on the string</span>
<span class="Comment">// before accessing its raw pointer</span>
++result<span class="Delimiter">;</span>
curr += tb_utf8_char_length<span class="Delimiter">(</span>in[curr]<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|