|
|
<!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 - 037abandon.cc</title>
<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,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-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Constant { color: #00a0a0; }
.cSpecial { color: #008000; }
.traceContains { color: #008000; }
.Comment { color: #9090ff; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.CommentedCode { color: #6c6c6c; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.Identifier { color: #fcb165; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Reclaiming memory when it's no longer used.</span>
<span class="Comment">//: The top of the address layer has the complete life cycle of memory.</span>
<span class="Delimiter">:(scenario new_reclaim)</span>
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:num <span class="Comment"># because 1 will get reset during abandon below</span>
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span> <span class="Comment"># abandon</span>
<span class="Constant">3</span>:address:num<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">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">3</span>:address:num
<span class="Constant">5</span>:<span class="Normal">bool</span><span class="Special"> <- </span>equal <span class="Constant">2</span>:num<span class="Delimiter">,</span> <span class="Constant">4</span>:num
]
<span class="Comment"># both allocations should have returned the same address</span>
<span class="traceContains">+mem: storing 1 in location 5</span>
<span class="Delimiter">:(before "End Decrement Refcount(old_address, payload_type, payload_size)")</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>old_refcount == <span class="Constant">0</span><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">"mem"</span><span class="Delimiter">)</span> << <span class="Constant">"automatically abandoning "</span> << old_address << end<span class="Delimiter">();</span>
abandon<span class="Delimiter">(</span>old_address<span class="Delimiter">,</span> payload_type<span class="Delimiter">,</span> payload_size<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: When abandoning addresses we'll save them to a 'free list', segregated by size.</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">:(code)</span>
<span class="Normal">void</span> abandon<span class="Delimiter">(</span><span class="Normal">int</span> address<span class="Delimiter">,</span> <span class="Normal">const</span> type_tree* payload_type<span class="Delimiter">,</span> <span class="Normal">int</span> payload_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">"updating refcounts inside "</span> << address << <span class="Constant">": "</span> << to_string<span class="Delimiter">(</span>payload_type<span class="Delimiter">)</span> << 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">// decrement any contained refcounts</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_array<span class="Delimiter">(</span>payload_type<span class="Delimiter">))</span> <span class="Delimiter">{</span>
reagent element<span class="Delimiter">;</span>
element<span class="Delimiter">.</span>type = copy_array_element<span class="Delimiter">(</span>payload_type<span class="Delimiter">);</span>
<span class="Normal">int</span> array_length = get_or_insert<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> address+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>element<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name != <span class="Constant">"array"</span><span class="Delimiter">);</span>
<span class="Normal">int</span> element_size = size_of<span class="Delimiter">(</span>element<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 < array_length<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
element<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>address + <span class="Comment">/*</span><span class="Comment">skip refcount and length</span><span class="Comment">*/</span><span class="Constant">2</span> + i*element_size<span class="Delimiter">);</span>
decrement_any_refcounts<span class="Delimiter">(</span>element<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Normal">else</span> <span class="Normal">if</span> <span class="Delimiter">(</span>is_mu_container<span class="Delimiter">(</span>payload_type<span class="Delimiter">)</span> || is_mu_exclusive_container<span class="Delimiter">(</span>payload_type<span class="Delimiter">))</span> <span class="Delimiter">{</span>
reagent tmp<span class="Delimiter">;</span>
tmp<span class="Delimiter">.</span>type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span>*payload_type<span class="Delimiter">);</span>
tmp<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>address + <span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">);</span>
decrement_any_refcounts<span class="Delimiter">(</span>tmp<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">// clear memory</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> curr = address<span class="Delimiter">;</span> curr < address+payload_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>
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 "</span> << address << <span class="Constant">" in free-list of size "</span> << payload_size << end<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> payload_size<span class="Delimiter">));</span>
put<span class="Delimiter">(</span>Current_routine<span class="Delimiter">-></span>free_list<span class="Delimiter">,</span> payload_size<span class="Delimiter">,</span> address<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(after "Allocate Special-cases")</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">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>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result<span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Normal">for</span> <span class="Delimiter">(</span><span class="Normal">int</span> curr = result<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>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario new_differing_size_no_reclaim)</span>
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:num
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span> <span class="Comment"># abandon</span>
<span class="Constant">3</span>:address:array:num<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">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">3</span>:address:array:num
<span class="Constant">5</span>:<span class="Normal">bool</span><span class="Special"> <- </span>equal <span class="Constant">2</span>:num<span class="Delimiter">,</span> <span class="Constant">4</span>:num
]
<span class="Comment"># no reuse</span>
<span class="traceContains">+mem: storing 0 in location 5</span>
<span class="Delimiter">:(scenario new_reclaim_array)</span>
def main [
<span class="Constant">1</span>:address:array:num<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">2</span>
<span class="Constant">2</span>:num<span class="Special"> <- </span>copy <span class="Constant">1</span>:address:array:num
<span class="Constant">1</span>:address:array:num<span class="Special"> <- </span>copy <span class="Constant">0</span> <span class="Comment"># abandon</span>
<span class="Constant">3</span>:address:array:num<span class="Special"> <- </span><span class="Normal">new</span> number:type<span class="Delimiter">,</span> <span class="Constant">2</span> <span class="Comment"># same size</span>
<span class="Constant">4</span>:num<span class="Special"> <- </span>copy <span class="Constant">3</span>:address:array:num
<span class="Constant">5</span>:<span class="Normal">bool</span><span class="Special"> <- </span>equal <span class="Constant">2</span>:num<span class="Delimiter">,</span> <span class="Constant">4</span>:num
]
<span class="Comment"># both calls to new returned identical addresses</span>
<span class="traceContains">+mem: storing 1 in location 5</span>
<span class="Delimiter">:(scenario abandon_on_overwrite)</span>
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Comment"># over-writing one allocation with another</span>
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>
<span class="Delimiter">:(scenario abandon_after_call)</span>
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Comment"># passing in addresses to recipes increments refcount</span>
foo <span class="Constant">1</span>:address:num
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
def foo [
<span class="Constant">2</span>:address:num<span class="Special"> <- </span>next-ingredient
<span class="Comment"># return does NOT yet decrement refcount; memory must be explicitly managed</span>
<span class="Constant">2</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: foo {1: ("address" "number")}</span>
<span class="Comment"># leave ambiguous precisely when the next increment happens; a later layer</span>
<span class="Comment"># will mess with that</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: {2: ("address" "number")} <- copy {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: {1: ("address" "number")} <- copy {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+mem: automatically abandoning 1000</span>
<span class="Delimiter">:(scenario abandon_on_overwrite_array)</span>
def main [
<span class="Constant">1</span>:num<span class="Special"> <- </span>copy <span class="Constant">30</span>
<span class="Comment"># allocate an array</span>
<span class="Constant">10</span>:address:array:num<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>:num<span class="Special"> <- </span>copy <span class="Constant">10</span>:address:array:num <span class="Comment"># doesn't increment refcount</span>
<span class="Comment"># allocate another array in its place, implicitly freeing the previous allocation</span>
<span class="Constant">10</span>:address:array:num<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" "array" "number")} <- new {number: "type"}, {25: "literal"}</span>
<span class="Comment"># abandoned array is of old size (20, not 25)</span>
<span class="traceContains">+abandon: saving 1000 in free-list of size 22</span>
<span class="Delimiter">:(scenario refcounts_abandon_address_in_container)</span>
<span class="Comment"># container containing an address</span>
container foo [
<span class="Normal">x</span>:address:num
]
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:foo<span class="Special"> <- </span><span class="Normal">new</span> foo:type
*<span class="Constant">2</span>:address:foo<span class="Special"> <- </span>put *<span class="Constant">2</span>:address:foo<span class="Delimiter">,</span> x:offset<span class="Delimiter">,</span> <span class="Constant">1</span>:address:num
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:address:foo<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: {2: ("address" "foo")} <- new {foo: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1002: 0 -> 1</span>
<span class="traceContains">+run: {2: ("address" "foo"), "lookup": ()} <- put {2: ("address" "foo"), "lookup": ()}, {x: "offset"}, {1: ("address" "number")}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: {1: ("address" "number")} <- copy {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: {2: ("address" "foo")} <- copy {0: "literal"}</span>
<span class="Comment"># start abandoning container containing address</span>
<span class="traceContains">+mem: decrementing refcount of 1002: 1 -> 0</span>
<span class="Comment"># nested abandon</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+abandon: saving 1000 in free-list of size 2</span>
<span class="Comment"># actually abandon the container containing address</span>
<span class="traceContains">+abandon: saving 1002 in free-list of size 2</span>
<span class="Comment"># todo: move past dilated reagent</span>
<span class="Delimiter">:(scenario refcounts_abandon_address_in_array)</span>
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:array:address:num<span class="Special"> <- </span><span class="Normal">new</span> <span class="Delimiter">{(</span>address number<span class="Delimiter">)</span>: type<span class="Delimiter">},</span> <span class="Constant">3</span>
*<span class="Constant">2</span>:address:array:address:num<span class="Special"> <- </span>put-index *<span class="Constant">2</span>:address:array:address:num<span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">1</span>:address:num
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">2</span>:address:array:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: {2: ("address" "array" "address" "number"), "lookup": ()} <- put-index {2: ("address" "array" "address" "number"), "lookup": ()}, {1: "literal"}, {1: ("address" "number")}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: {1: ("address" "number")} <- copy {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: {2: ("address" "array" "address" "number")} <- copy {0: "literal"}</span>
<span class="Comment"># nested abandon</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+abandon: saving 1000 in free-list of size 2</span>
<span class="Delimiter">:(scenario refcounts_abandon_address_in_container_in_array)</span>
<span class="Comment"># container containing an address</span>
container foo [
<span class="Normal">x</span>:address:num
]
def main [
<span class="Constant">1</span>:address:num<span class="Special"> <- </span><span class="Normal">new</span> number:type
<span class="Constant">2</span>:address:array:foo<span class="Special"> <- </span><span class="Normal">new</span> foo:type<span class="Delimiter">,</span> <span class="Constant">3</span>
<span class="Constant">3</span>:foo<span class="Special"> <- </span>merge <span class="Constant">1</span>:address:num
*<span class="Constant">2</span>:address:array:foo<span class="Special"> <- </span>put-index *<span class="Constant">2</span>:address:array:foo<span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">,</span> <span class="Constant">3</span>:foo
<span class="Constant">1</span>:address:num<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Constant">3</span>:foo<span class="Special"> <- </span>merge <span class="Constant">0</span>
<span class="Constant">2</span>:address:array:foo<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="traceContains">+run: {1: ("address" "number")} <- new {number: "type"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 0 -> 1</span>
<span class="traceContains">+run: {3: "foo"} <- merge {1: ("address" "number")}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 1 -> 2</span>
<span class="traceContains">+run: {2: ("address" "array" "foo"), "lookup": ()} <- put-index {2: ("address" "array" "foo"), "lookup": ()}, {1: "literal"}, {3: "foo"}</span>
<span class="traceContains">+mem: incrementing refcount of 1000: 2 -> 3</span>
<span class="traceContains">+run: {1: ("address" "number")} <- copy {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 3 -> 2</span>
<span class="traceContains">+run: {3: "foo"} <- merge {0: "literal"}</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 2 -> 1</span>
<span class="traceContains">+run: {2: ("address" "array" "foo")} <- copy {0: "literal"}</span>
<span class="Comment"># nested abandon</span>
<span class="traceContains">+mem: decrementing refcount of 1000: 1 -> 0</span>
<span class="traceContains">+abandon: saving 1000 in free-list of size 2</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|