diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2016-03-26 23:59:59 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2016-03-27 17:43:41 -0700 |
commit | a654e4ecace2d506d1b10f1dde2c287ebe84ef37 (patch) | |
tree | 1e34a3ad99a56e32bdf9ee03ecfe7d339e7942ae /html/037new.cc.html | |
parent | 859f35fbe2f6a78157b875e12eb7dc8cd95c1152 (diff) | |
download | mu-a654e4ecace2d506d1b10f1dde2c287ebe84ef37.tar.gz |
2812
Diffstat (limited to 'html/037new.cc.html')
-rw-r--r-- | html/037new.cc.html | 198 |
1 files changed, 111 insertions, 87 deletions
diff --git a/html/037new.cc.html b/html/037new.cc.html index 4f181562..c932c8af 100644 --- a/html/037new.cc.html +++ b/html/037new.cc.html @@ -3,29 +3,37 @@ <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Mu - 037new.cc</title> -<meta name="Generator" content="Vim/7.3"> -<meta name="plugin-version" content="vim7.3_v6"> +<meta name="Generator" content="Vim/7.4"> +<meta name="plugin-version" content="vim7.4_v2"> <meta name="syntax" content="cpp"> -<meta name="settings" content="use_css"> +<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy="> +<meta name="colorscheme" content="minimal"> <style type="text/css"> <!-- -pre { font-family: monospace; color: #eeeeee; background-color: #080808; } +pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; } -.CommentedCode { color: #6c6c6c; } -.Identifier { color: #fcb165; } +* { font-size: 12pt; font-size: 1em; } +.Constant { color: #00a0a0; } .cSpecial { color: #008000; } -.SalientComment { color: #00ffff; } .traceContains { color: #008000; } -.Special { color: #c00000; } -.Normal { color: #eeeeee; background-color: #080808; } -.Constant { color: #00a0a0; } -.Delimiter { color: #800080; } +.SalientComment { color: #00ffff; } .Comment { color: #9090ff; } +.Delimiter { color: #800080; } +.Special { color: #c00000; } +.Identifier { color: #fcb165; } +.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; } +.CommentedCode { color: #6c6c6c; } --> </style> + +<script type='text/javascript'> +<!-- + +--> +</script> </head> <body> -<pre> +<pre id='vimCodeElement'> <span class="Comment">//: Creating space for new variables at runtime.</span> <span class="Comment">//: Mu has two primitives for managing allocations:</span> @@ -83,13 +91,14 @@ def main [ <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> 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="Normal">const</span> <span class="Normal">int</span> Reserved_for_tests = <span class="Constant">1000</span><span class="Delimiter">;</span> +<span class="Normal">int</span> Memory_allocated_until = Reserved_for_tests<span class="Delimiter">;</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="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> @@ -109,7 +118,7 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="Normal">case</span> NEW: <span class="Delimiter">{</span> <span class="Normal">const</span> recipe& caller = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">);</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> || SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> > <span class="Constant">2</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"'new' requires one or two ingredients, but got "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> + raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"'new' requires one or two ingredients, but got "</span> << to_original_string<span class="Delimiter">(</span>inst<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="Comment">// End NEW Check Special-cases</span> @@ -123,7 +132,7 @@ put<span class="Delimiter">(</span>Recipe_ordinal<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>!product_of_new_is_valid<span class="Delimiter">(</span>inst<span class="Delimiter">))</span> <span class="Delimiter">{</span> - raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"product of 'new' has incorrect type: "</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span> << end<span class="Delimiter">();</span> + raise << maybe<span class="Delimiter">(</span>caller<span class="Delimiter">.</span>name<span class="Delimiter">)</span> << <span class="Constant">"product of 'new' has incorrect type: "</span> << to_original_string<span class="Delimiter">(</span>inst<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="Identifier">break</span><span class="Delimiter">;</span> @@ -153,7 +162,7 @@ Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</spa <span class="Delimiter">:(code)</span> <span class="Normal">void</span> transform_new_to_allocate<span class="Delimiter">(</span><span class="Normal">const</span> recipe_ordinal r<span class="Delimiter">)</span> <span class="Delimiter">{</span> trace<span class="Delimiter">(</span><span class="Constant">9991</span><span class="Delimiter">,</span> <span class="Constant">"transform"</span><span class="Delimiter">)</span> << <span class="Constant">"--- convert 'new' to 'allocate' for recipe "</span> << get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name << end<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 < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span> instruction& inst = get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">);</span> <span class="Comment">// Convert 'new' To 'allocate'</span> <span class="Normal">if</span> <span class="Delimiter">(</span>inst<span class="Delimiter">.</span>name == <span class="Constant">"new"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> @@ -178,7 +187,7 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="Delimiter">:(before "End Primitive Recipe Implementations")</span> <span class="Normal">case</span> ALLOCATE: <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 = 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> + <span class="Normal">int</span> size = 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> <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> <span class="Comment">// array</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"array size is "</span> << 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> << end<span class="Delimiter">();</span> @@ -192,13 +201,13 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</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> + <span class="Normal">const</span> <span class="Normal">int</span> result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><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="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> address = result<span class="Delimiter">;</span> address < result+size<span class="Delimiter">;</span> ++address<span class="Delimiter">)</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// initialize array length</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> @@ -227,10 +236,10 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="Delimiter">}</span> <span class="CommentedCode">//? :(before "End Globals")</span> -<span class="CommentedCode">//? long long int Total_alloc = 0;</span> -<span class="CommentedCode">//? long long int Num_alloc = 0;</span> -<span class="CommentedCode">//? long long int Total_free = 0;</span> -<span class="CommentedCode">//? long long int Num_free = 0;</span> +<span class="CommentedCode">//? int Total_alloc = 0;</span> +<span class="CommentedCode">//? int Num_alloc = 0;</span> +<span class="CommentedCode">//? int Total_free = 0;</span> +<span class="CommentedCode">//? int Num_free = 0;</span> <span class="CommentedCode">//? :(before "End Setup")</span> <span class="CommentedCode">//? Total_alloc = Num_alloc = Total_free = Num_free = 0;</span> <span class="CommentedCode">//? :(before "End Teardown")</span> @@ -239,7 +248,7 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="CommentedCode">//? cerr << SIZE(Memory) << '\n';</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> +<span class="Normal">void</span> ensure_space<span class="Delimiter">(</span><span class="Normal">int</span> size<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Normal">if</span> <span class="Delimiter">(</span>size > Initial_memory_per_routine<span class="Delimiter">)</span> <span class="Delimiter">{</span> tb_shutdown<span class="Delimiter">();</span> cerr << <span class="Constant">"can't allocate "</span> << size << <span class="Constant">" locations, that's too much compared to "</span> << Initial_memory_per_routine << <span class="Constant">".</span><span class="cSpecial">\n</span><span class="Constant">"</span><span class="Delimiter">;</span> @@ -276,7 +285,7 @@ def main [ <span class="Constant">2</span>:address:shared: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:shared:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw</span> ] -<span class="traceContains">+run: 1:address:shared:array:number/raw <- new number:type, 5</span> +<span class="traceContains">+run: {1: ("address" "shared" "array" "number"), "raw": ()} <- new {number: "type"}, {5: "literal"}</span> <span class="traceContains">+mem: array size is 5</span> <span class="Comment"># don't forget the extra location for array size, and the second extra location for the refcount</span> <span class="traceContains">+mem: storing 7 in location 3</span> @@ -287,7 +296,7 @@ def main [ <span class="Constant">2</span>:address:shared: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:shared:number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:shared:array:number/<span class="Special">raw</span> ] -<span class="traceContains">+run: 1:address:shared:array:number/raw <- new number:type, 0</span> +<span class="traceContains">+run: {1: ("address" "shared" "array" "number"), "raw": ()} <- new {number: "type"}, {0: "literal"}</span> <span class="traceContains">+mem: array size is 0</span> <span class="Comment"># one location for array size, and one for the refcount</span> <span class="traceContains">+mem: storing 2 in location 3</span> @@ -316,10 +325,8 @@ def main [ <span class="Comment"># both allocations should have returned the same address</span> <span class="traceContains">+mem: storing 1 in location 4</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 routine Fields")</span> +map<<span class="Normal">int</span><span class="Delimiter">,</span> <span class="Normal">int</span>> free_list<span class="Delimiter">;</span> <span class="Delimiter">:(before "End Primitive Recipe Declarations")</span> ABANDON<span class="Delimiter">,</span> @@ -328,7 +335,7 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <span class="Delimiter">:(before "End Primitive Recipe Checks")</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>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">)</span> != <span class="Constant">1</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'abandon' requires one ingredient, but got '"</span> << to_string<span class="Delimiter">(</span>inst<span class="Delimiter">)</span> << <span class="Constant">"'</span><span class="cSpecial">\n</span><span class="Constant">"</span> << end<span class="Delimiter">();</span> + raise << maybe<span class="Delimiter">(</span>get<span class="Delimiter">(</span>Recipe<span class="Delimiter">,</span> r<span class="Delimiter">).</span>name<span class="Delimiter">)</span> << <span class="Constant">"'abandon' requires one ingredient, but got '"</span> << to_original_string<span class="Delimiter">(</span>inst<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> reagent types = inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> @@ -341,14 +348,14 @@ put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span <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">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> + <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> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"address to abandon is "</span> << address << end<span class="Delimiter">();</span> reagent types = 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> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"value of ingredient is "</span> << types<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span> canonize<span class="Delimiter">(</span>types<span class="Delimiter">);</span> <span class="Comment">// lookup_memory without drop_one_lookup {</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"value of ingredient after canonization is "</span> << types<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> address_location = types<span class="Delimiter">.</span>value<span class="Delimiter">;</span> + <span class="Normal">int</span> address_location = types<span class="Delimiter">.</span>value<span class="Delimiter">;</span> types<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> types<span class="Delimiter">.</span>value<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span> drop_from_type<span class="Delimiter">(</span>types<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span> drop_from_type<span class="Delimiter">(</span>types<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span> @@ -361,25 +368,26 @@ put<span class="Delimiter">(</span>Recipe_ordinal<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="Normal">void</span> abandon<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">,</span> <span class="Normal">int</span> size<span class="Delimiter">)</span> <span class="Delimiter">{</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"saving in free-list of size "</span> << size << end<span class="Delimiter">();</span> <span class="CommentedCode">//? Total_free += size;</span> <span class="CommentedCode">//? Num_free++;</span> <span class="CommentedCode">//? cerr << "abandon: " << size << '\n';</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> + <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> curr = address<span class="Delimiter">;</span> curr < address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// append existing free list to address</span> - put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">));</span> - put<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> address<span class="Delimiter">);</span> + put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> size<span class="Delimiter">));</span> + put<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> address<span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Delimiter">:(before "ensure_space(size)" following "case ALLOCATE")</span> -<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">))</span> <span class="Delimiter">{</span> +<span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> size<span class="Delimiter">))</span> <span class="Delimiter">{</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"abandon"</span><span class="Delimiter">)</span> << <span class="Constant">"picking up space from free-list of size "</span> << size << end<span class="Delimiter">();</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> result = get_or_insert<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">);</span> - put<span class="Delimiter">(</span>Free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> 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">int</span> result = get_or_insert<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> size<span class="Delimiter">);</span> + trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"new alloc from free list: "</span> << result << end<span class="Delimiter">();</span> + put<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> size<span class="Delimiter">,</span> get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result<span class="Delimiter">));</span> + <span class="Normal">for</span> <span class="Delimiter">(</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>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)</span> != <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> raise << maybe<span class="Delimiter">(</span>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> @@ -422,7 +430,7 @@ def main [ abandon <span class="Constant">1</span>:address:shared:number ] <span class="Comment"># reuse</span> -<span class="traceContains">+run: abandon 1:address:shared:number</span> +<span class="traceContains">+run: abandon {1: ("address" "shared" "number")}</span> <span class="traceContains">+mem: resetting location 1</span> <span class="SalientComment">//:: Manage refcounts when copying addresses.</span> @@ -434,54 +442,55 @@ def main [ <span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span> <span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span> ] -<span class="traceContains">+run: 1:address:shared:number <- copy 1000/unsafe</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- copy {1000: "literal", "unsafe": ()}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> -<span class="traceContains">+run: 2:address:shared:number <- copy 1:address:shared:number</span> +<span class="traceContains">+run: {2: ("address" "shared" "number")} <- copy {1: ("address" "shared" "number")}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span> -<span class="traceContains">+run: 1:address:shared:number <- copy 0</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- copy {0: "literal"}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span> -<span class="traceContains">+run: 2:address:shared:number <- copy 0</span> +<span class="traceContains">+run: {2: ("address" "shared" "number")} <- copy {0: "literal"}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span> <span class="Comment"># the /unsafe corrupts memory but fortunately we won't be running any more 'new' in this scenario</span> <span class="traceContains">+mem: automatically abandoning 1000</span> -<span class="Delimiter">:(before "End write_memory(reagent x, long long int base) Special-cases")</span> +<span class="Delimiter">:(before "End write_memory(reagent x) Special-cases")</span> <span class="Normal">if</span> <span class="Delimiter">(</span>x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">)</span> && x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>right && x<span class="Delimiter">.</span>type<span class="Delimiter">-></span>right<span class="Delimiter">-></span>value == get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">))</span> <span class="Delimiter">{</span> <span class="Comment">// compute old address of x, as well as new address we want to write in</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> old_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">);</span> + <span class="Normal">int</span> old_address = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">);</span> assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>data<span class="Delimiter">));</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> new_address = data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> + <span class="Normal">int</span> new_address = data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// decrement refcount of old address</span> <span class="Normal">if</span> <span class="Delimiter">(</span>old_address<span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> old_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">);</span> + <span class="Normal">int</span> old_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">);</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"decrementing refcount of "</span> << old_address << <span class="Constant">": "</span> << old_refcount << <span class="Constant">" -> "</span> << <span class="Delimiter">(</span>old_refcount-<span class="Constant">1</span><span class="Delimiter">)</span> << end<span class="Delimiter">();</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">,</span> old_refcount-<span class="Constant">1</span><span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Comment">// perform the write</span> - trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << no_scientific<span class="Delimiter">(</span>data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="Constant">" in location "</span> << base << end<span class="Delimiter">();</span> - put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> base<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span> + trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"storing "</span> << no_scientific<span class="Delimiter">(</span>data<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">))</span> << <span class="Constant">" in location "</span> << x<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span> + put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span> <span class="Comment">// increment refcount of new address</span> <span class="Normal">if</span> <span class="Delimiter">(</span>new_address<span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> new_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span> + <span class="Normal">int</span> new_refcount = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> new_address<span class="Delimiter">);</span> assert<span class="Delimiter">(</span>new_refcount >= <span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// == 0 only when new_address == old_address</span> trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"incrementing refcount of "</span> << new_address << <span class="Constant">": "</span> << new_refcount << <span class="Constant">" -> "</span> << <span class="Delimiter">(</span>new_refcount+<span class="Constant">1</span><span class="Delimiter">)</span> << end<span class="Delimiter">();</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> new_address<span class="Delimiter">,</span> new_refcount+<span class="Constant">1</span><span class="Delimiter">);</span> <span class="Delimiter">}</span> <span class="Comment">// abandon old address if necessary</span> <span class="Comment">// do this after all refcount updates are done just in case old and new are identical</span> + assert<span class="Delimiter">(</span>old_address >= <span class="Constant">0</span><span class="Delimiter">);</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>old_address == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> assert<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">)</span> >= <span class="Constant">0</span><span class="Delimiter">);</span> - <span class="Normal">if</span> <span class="Delimiter">(</span>old_address && get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span> - <span class="Comment">// lookup_memory without drop_one_lookup {</span> - trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"automatically abandoning "</span> << old_address << end<span class="Delimiter">();</span> - trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"computing size to abandon at "</span> << x<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span> - x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> x<span class="Delimiter">.</span>value<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span> - drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span> - drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span> - <span class="Comment">// }</span> - abandon<span class="Delimiter">(</span>old_address<span class="Delimiter">,</span> size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span> - <span class="Delimiter">}</span> + <span class="Normal">if</span> <span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> old_address<span class="Delimiter">)</span> > <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span><span class="Delimiter">;</span> + <span class="Comment">// lookup_memory without drop_one_lookup {</span> + trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"automatically abandoning "</span> << old_address << end<span class="Delimiter">();</span> + trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"computing size to abandon at "</span> << x<span class="Delimiter">.</span>value << end<span class="Delimiter">();</span> + x<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>old_address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span> + drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"address"</span><span class="Delimiter">);</span> + drop_from_type<span class="Delimiter">(</span>x<span class="Delimiter">,</span> <span class="Constant">"shared"</span><span class="Delimiter">);</span> + <span class="Comment">// }</span> + abandon<span class="Delimiter">(</span>old_address<span class="Delimiter">,</span> size_of<span class="Delimiter">(</span>x<span class="Delimiter">)</span>+<span class="Comment">/*</span><span class="Comment">refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span> <span class="Identifier">return</span><span class="Delimiter">;</span> <span class="Delimiter">}</span> @@ -492,9 +501,9 @@ def main [ <span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span><span class="Normal">new</span> number:type <span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span> ] -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: automatically abandoning 1000</span> <span class="Delimiter">:(scenario refcounts_3)</span> @@ -509,13 +518,13 @@ def foo [ <span class="Comment"># return does NOT yet decrement refcount; memory must be explicitly managed</span> <span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">0</span> ] -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> -<span class="traceContains">+run: 2:address:shared:number <- next-ingredient</span> +<span class="traceContains">+run: {2: ("address" "shared" "number")} <- next-ingredient</span> <span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span> -<span class="traceContains">+run: 2:address:shared:number <- copy 0</span> +<span class="traceContains">+run: {2: ("address" "shared" "number")} <- copy {0: "literal"}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span> -<span class="traceContains">+run: 1:address:shared:number <- copy 0</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- copy {0: "literal"}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span> <span class="traceContains">+mem: automatically abandoning 1000</span> @@ -525,9 +534,9 @@ def main [ <span class="Comment"># idempotent copies leave refcount unchanged</span> <span class="Constant">1</span>:address:shared:number<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:shared:number ] -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> -<span class="traceContains">+run: 1:address:shared:number <- copy 1:address:shared:number</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- copy {1: ("address" "shared" "number")}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> @@ -542,13 +551,26 @@ def main [ def foo [ <span class="Constant">2</span>:address:shared:number<span class="Special"> <- </span>next-ingredient ] -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span> -<span class="traceContains">+run: 2:address:shared:number <- next-ingredient</span> +<span class="traceContains">+run: {2: ("address" "shared" "number")} <- next-ingredient</span> <span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span> -<span class="traceContains">+run: 1:address:shared:number <- new number:type</span> +<span class="traceContains">+run: {1: ("address" "shared" "number")} <- new {number: "type"}</span> <span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span> +<span class="Delimiter">:(scenario refcounts_array)</span> +def main [ + <span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">30</span> + <span class="Comment"># allocate an array</span> + <span class="Constant">10</span>:address:shared:array:number<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">20</span> + <span class="Constant">11</span>:number<span class="Special"> <- </span>copy <span class="Constant">10</span>:address:shared:array:number + <span class="Comment"># allocate another array in its place, implicitly freeing the previous allocation</span> + <span class="Constant">10</span>:address:shared:array:number<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">25</span> +] +<span class="traceContains">+run: {10: ("address" "shared" "array" "number")} <- new {number: "type"}, {20: "literal"}</span> +<span class="Comment"># abandoned array is of old size (20, not 25)</span> +<span class="traceContains">+abandon: saving in free-list of size 22</span> + <span class="SalientComment">//:: Extend 'new' to handle a unicode string literal argument.</span> <span class="Delimiter">:(scenario new_string)</span> @@ -577,25 +599,26 @@ def main [ <span class="Normal">if</span> <span class="Delimiter">(</span>is_literal_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> <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> + trace<span class="Delimiter">(</span><span class="Constant">9999</span><span class="Delimiter">,</span> <span class="Constant">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"new string alloc: "</span> << products<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> << end<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="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="Normal">int</span> string_length = unicode_length<span class="Delimiter">(</span>contents<span class="Delimiter">);</span> <span class="CommentedCode">//? Total_alloc += string_length+1;</span> <span class="CommentedCode">//? Num_alloc++;</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> + <span class="Normal">int</span> result = Current_routine<span class="Delimiter">-></span>alloc<span class="Delimiter">;</span> <span class="Comment">// initialize refcount</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>alloc++<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span> <span class="Comment">// store length</span> put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> Current_routine<span class="Delimiter">-></span>alloc++<span class="Delimiter">,</span> 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">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">for</span> <span class="Delimiter">(</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> @@ -648,10 +671,10 @@ def main [ <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">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">int</span> result = <span class="Constant">0</span><span class="Delimiter">;</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> @@ -660,13 +683,13 @@ def main [ <span class="Identifier">return</span> result<span class="Delimiter">;</span> <span class="Delimiter">}</span> -string read_mu_string<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="Delimiter">{</span> +string read_mu_string<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">)</span> <span class="Delimiter">{</span> <span class="Normal">if</span> <span class="Delimiter">(</span>address == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span> address++<span class="Delimiter">;</span> <span class="Comment">// skip refcount</span> - <span class="Normal">long</span> <span class="Normal">long</span> <span class="Normal">int</span> size = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">);</span> + <span class="Normal">int</span> size = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address<span class="Delimiter">);</span> <span class="Normal">if</span> <span class="Delimiter">(</span>size == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">return</span> <span class="Constant">""</span><span class="Delimiter">;</span> ostringstream tmp<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 = address+<span class="Constant">1</span><span class="Delimiter">;</span> curr <= address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span> + <span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> curr = address+<span class="Constant">1</span><span class="Delimiter">;</span> curr <= address+size<span class="Delimiter">;</span> ++curr<span class="Delimiter">)</span> <span class="Delimiter">{</span> tmp << to_unicode<span class="Delimiter">(</span><span class="Normal">static_cast</span><<span class="Normal">uint32_t</span>><span class="Delimiter">(</span>get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> curr<span class="Delimiter">)));</span> <span class="Delimiter">}</span> <span class="Identifier">return</span> tmp<span class="Delimiter">.</span>str<span class="Delimiter">();</span> @@ -678,3 +701,4 @@ string read_mu_string<span class="Delimiter">(</span><span class="Normal">long</ </pre> </body> </html> +<!-- vim: set foldmethod=manual : --> |