<!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 - 034address.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; }
.Identifier { color: #fcb165; }
.Normal { color: #eeeeee; background-color: #080808; padding-bottom: 1px; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Addresses help us spend less time copying data around.</span>
<span class="Comment">//: So far we've been operating on primitives like numbers and characters, and</span>
<span class="Comment">//: we've started combining these primitives together into larger logical</span>
<span class="Comment">//: units (containers or arrays) that may contain many different primitives at</span>
<span class="Comment">//: once. Containers and arrays can grow quite large in complex programs, and</span>
<span class="Comment">//: we'd like some way to efficiently share them between recipes without</span>
<span class="Comment">//: constantly having to make copies. Right now 'next-ingredient' and 'reply'</span>
<span class="Comment">//: copy data across recipe boundaries. To avoid copying large quantities of</span>
<span class="Comment">//: data around, we'll use *addresses*. An address is a bookmark to some</span>
<span class="Comment">//: arbitrary quantity of data (the *payload*). It's a primitive, so it's as</span>
<span class="Comment">//: efficient to copy as a number. To read or modify the payload 'pointed to'</span>
<span class="Comment">//: by an address, we'll perform a *lookup*.</span>
<span class="Comment">//:</span>
<span class="Comment">//: The notion of 'lookup' isn't an instruction like 'add' or 'subtract'.</span>
<span class="Comment">//: Instead it's an operation that can be performed when reading any of the</span>
<span class="Comment">//: ingredients of an instruction, and when writing to any of the products. To</span>
<span class="Comment">//: write to the payload of an ingredient rather than its value, simply add</span>
<span class="Comment">//: the /lookup property to it. Modern computers provide efficient support for</span>
<span class="Comment">//: addresses and lookups, making this a realistic feature.</span>
<span class="Comment">//:</span>
<span class="Comment">//: To recap: an address is a bookmark to some potentially large payload, and</span>
<span class="Comment">//: you can replace any ingredient or product with a lookup to an address of</span>
<span class="Comment">//: the appropriate type. But how do we get addresses to begin with? That</span>
<span class="Comment">//: requires a little more explanation. Once we introduce the notion of</span>
<span class="Comment">//: bookmarks to data, we have to think about the life cycle of a piece of</span>
<span class="Comment">//: data and its bookmarks (because remember, bookmarks can be copied around</span>
<span class="Comment">//: just like anything else). Otherwise several bad outcomes can result (and</span>
<span class="Comment">//: indeed *have* resulted in past languages like C):</span>
<span class="Comment">//:</span>
<span class="Comment">//: a) You can run out of memory if you don't have a way to reclaim</span>
<span class="Comment">//: data.</span>
<span class="Comment">//: b) If you allow data to be reclaimed, you have to be careful not to</span>
<span class="Comment">//: leave any stale addresses pointing at it. Otherwise your program might</span>
<span class="Comment">//: try to lookup such an address and find something unexpected. Such</span>
<span class="Comment">//: problems can be very hard to track down, and they can also be exploited</span>
<span class="Comment">//: to break into your computer over the network, etc.</span>
<span class="Comment">//:</span>
<span class="Comment">//: To avoid these problems, we introduce the notion of a *reference count* or</span>
<span class="Comment">//: refcount. The life cycle of a bit of data accessed through addresses looks</span>
<span class="Comment">//: like this.</span>
<span class="Comment">//:</span>
<span class="Comment">//: We create space in computer memory for it using the 'new' instruction.</span>
<span class="Comment">//: The 'new' instruction takes a type as an ingredient, allocates</span>
<span class="Comment">//: sufficient space to hold that type, and returns an address (bookmark)</span>
<span class="Comment">//: to the allocated space.</span>
<span class="Comment">//:</span>
<span class="Comment">//: x:address:number <- new number:type</span>
<span class="Comment">//:</span>
<span class="Comment">//: +------------+</span>
<span class="Comment">//: x -------> | number |</span>
<span class="Comment">//: +------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: That isn't entirely accurate. Under the hood, 'new' allocates an extra</span>
<span class="Comment">//: number -- the refcount:</span>
<span class="Comment">//:</span>
<span class="Comment">//: +------------+------------+</span>
<span class="Comment">//: x -------> | refcount | number |</span>
<span class="Comment">//: +------------+------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: This probably seems like a waste of space. In practice it isn't worth</span>
<span class="Comment">//: allocating individual numbers and our payload will tend to be larger,</span>
<span class="Comment">//: so the picture would look more like this (zooming out a bit):</span>
<span class="Comment">//:</span>
<span class="Comment">//: +-------------------------+</span>
<span class="Comment">//: +---+ |</span>
<span class="Comment">//: x -------> | r | |</span>
<span class="Comment">//: +---+ DATA |</span>
<span class="Comment">//: | |</span>
<span class="Comment">//: | |</span>
<span class="Comment">//: +-------------------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: (Here 'r' denotes the refcount. It occupies a tiny amount of space</span>
<span class="Comment">//: compared to the payload.)</span>
<span class="Comment">//:</span>
<span class="Comment">//: Anyways, back to our example where the data is just a single number.</span>
<span class="Comment">//: After the call to 'new', Mu's map of memory looks like this:</span>
<span class="Comment">//:</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//: x -------> | 1 | number |</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: The refcount of 1 here indicates that this number has one bookmark</span>
<span class="Comment">//: outstanding. If you then make a copy of x, the refcount increments:</span>
<span class="Comment">//:</span>
<span class="Comment">//: y:address:number <- copy x</span>
<span class="Comment">//:</span>
<span class="Comment">//: x ---+ +---+------------+</span>
<span class="Comment">//: +---> | 2 | number |</span>
<span class="Comment">//: y ---+ +---+------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: Whether you access the payload through x or y, Mu knows how many</span>
<span class="Comment">//: bookmarks are outstanding to it. When you change x or y, the refcount</span>
<span class="Comment">//: transparently decrements:</span>
<span class="Comment">//:</span>
<span class="Comment">//: x <- copy 0 # an address is just a number, you can always write 0 to it</span>
<span class="Comment">//:</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//: y -------> | 1 | number |</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: The final flourish is what happens when the refcount goes down to 0: Mu</span>
<span class="Comment">//: reclaims the space occupied by both refcount and payload in memory, and</span>
<span class="Comment">//: they're ready to be reused by later calls to 'new'.</span>
<span class="Comment">//:</span>
<span class="Comment">//: y <- copy 0</span>
<span class="Comment">//:</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//: | 0 | XXXXXXX |</span>
<span class="Comment">//: +---+------------+</span>
<span class="Comment">//:</span>
<span class="Comment">//: Using refcounts fixes both our problems a) and b) above: you can use</span>
<span class="Comment">//: memory for many different purposes as many times as you want without</span>
<span class="Comment">//: running out of memory, and you don't have to worry about ever leaving a</span>
<span class="Comment">//: dangling bookmark when you reclaim memory.</span>
<span class="Comment">//:</span>
<span class="Comment">//: This layer implements creating addresses using 'new'. The next few layers</span>
<span class="Comment">//: will flesh out the rest of the life cycle.</span>
<span class="Comment">//:</span>
<span class="Comment">//: The tests in this layer use unsafe operations so as to stay decoupled from</span>
<span class="Comment">//: 'new'.</span>
<span class="Comment">//: todo: give 'new' a custodian ingredient. Following malloc/free is a temporary hack.</span>
<span class="Delimiter">:(scenario new)</span>
<span class="Comment"># call 'new' two times with identical types without modifying the results; you</span>
<span class="Comment"># should get back different results</span>
def 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">:(scenario dilated_reagent_with_new)</span>
def main [
<span class="Constant">1</span>:address:address:number<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="traceContains">+new: size of ("address" "number") is 1</span>
<span class="Comment">//: 'new' takes a weird 'type' as its first ingredient; don't error on it</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
put<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"type"</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> is_mu_type_literal<span class="Delimiter">(</span><span class="Normal">const</span> reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Identifier">return</span> is_literal<span class="Delimiter">(</span>r<span class="Delimiter">)</span> && r<span class="Delimiter">.</span>type && r<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name == <span class="Constant">"type"</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
NEW<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">,</span> NEW<span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</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_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>
<span class="Comment">// End NEW Check Special-cases</span>
<span class="Normal">const</span> reagent& type = inst<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>!is_mu_type_literal<span class="Delimiter">(</span>type<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">"first ingredient of 'new' should be a type, but got '"</span> << type<span class="Delimiter">.</span>original_string << <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>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>empty<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">"result of 'new' should never be ignored</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>!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_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>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
<span class="Normal">bool</span> product_of_new_is_valid<span class="Delimiter">(</span><span class="Normal">const</span> instruction& inst<span class="Delimiter">)</span> <span class="Delimiter">{</span>
reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> product = inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<span class="Comment">// Update NEW product in Check</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!product<span class="Delimiter">.</span>type || product<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>
<span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
drop_from_type<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">"address"</span><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>
<span class="Comment">// array allocation</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>!product<span class="Delimiter">.</span>type || product<span class="Delimiter">.</span>type<span class="Delimiter">-></span>value != get<span class="Delimiter">(</span>Type_ordinal<span class="Delimiter">,</span> <span class="Constant">"array"</span><span class="Delimiter">))</span> <span class="Identifier">return</span> <span class="Constant">false</span><span class="Delimiter">;</span>
drop_from_type<span class="Delimiter">(</span>product<span class="Delimiter">,</span> <span class="Constant">"array"</span><span class="Delimiter">);</span>
<span class="Delimiter">}</span>
reagent<span class="Comment">/*</span><span class="Comment">copy</span><span class="Comment">*/</span> expected_product<span class="Delimiter">(</span><span class="Constant">"x:"</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>
<span class="Delimiter">{</span>
string_tree* tmp_type_names = parse_string_tree<span class="Delimiter">(</span>expected_product<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name<span class="Delimiter">);</span>
<span class="Normal">delete</span> expected_product<span class="Delimiter">.</span>type<span class="Delimiter">;</span>
expected_product<span class="Delimiter">.</span>type = new_type_tree<span class="Delimiter">(</span>tmp_type_names<span class="Delimiter">);</span>
<span class="Normal">delete</span> tmp_type_names<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> types_strictly_match<span class="Delimiter">(</span>product<span class="Delimiter">,</span> expected_product<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Normal">void</span> drop_from_type<span class="Delimiter">(</span>reagent& r<span class="Delimiter">,</span> string expected_type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Normal">if</span> <span class="Delimiter">(</span>r<span class="Delimiter">.</span>type<span class="Delimiter">-></span>name != expected_type<span class="Delimiter">)</span> <span class="Delimiter">{</span>
raise << <span class="Constant">"can't drop2 "</span> << expected_type << <span class="Constant">" from '"</span> << to_string<span class="Delimiter">(</span>r<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">return</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
type_tree* tmp = r<span class="Delimiter">.</span>type<span class="Delimiter">;</span>
r<span class="Delimiter">.</span>type = tmp<span class="Delimiter">-></span>right<span class="Delimiter">;</span>
tmp<span class="Delimiter">-></span>right = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span class="Normal">delete</span> tmp<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Comment">//: To implement 'new', a Mu transform turns all 'new' instructions into</span>
<span class="Comment">//: 'allocate' instructions that precompute the amount of memory they want to</span>
<span class="Comment">//: allocate.</span>
<span class="Comment">//: Ensure that we never call 'allocate' directly, and that there's no 'new'</span>
<span class="Comment">//: instructions left after the transforms have run.</span>
<span class="Delimiter">:(before "End Primitive Recipe Checks")</span>
<span class="Normal">case</span> ALLOCATE: <span class="Delimiter">{</span>
raise << <span class="Constant">"never call 'allocate' directly'; always use 'new'</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="Delimiter">:(before "End Primitive Recipe Implementations")</span>
<span class="Normal">case</span> NEW: <span class="Delimiter">{</span>
raise << <span class="Constant">"no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.</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="Delimiter">:(after "Transform.push_back(check_instruction)")</span> <span class="Comment">// check_instruction will guard against direct 'allocate' instructions below</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>transform_new_to_allocate<span class="Delimiter">);</span> <span class="Comment">// idempotent</span>
<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">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>
inst<span class="Delimiter">.</span>operation = ALLOCATE<span class="Delimiter">;</span>
string_tree* type_name = <span class="Normal">new</span> string_tree<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_name = parse_string_tree<span class="Delimiter">(</span>type_name<span class="Delimiter">);</span>
type_tree* type = new_type_tree<span class="Delimiter">(</span>type_name<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>size_of<span class="Delimiter">(</span>type<span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">9992</span><span class="Delimiter">,</span> <span class="Constant">"new"</span><span class="Delimiter">)</span> << <span class="Constant">"size of "</span> << to_string<span class="Delimiter">(</span>type_name<span class="Delimiter">)</span> << <span class="Constant">" is "</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>value << end<span class="Delimiter">();</span>
<span class="Normal">delete</span> type<span class="Delimiter">;</span>
<span class="Normal">delete</span> type_name<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: implement 'allocate' based on size</span>
<span class="Delimiter">:(before "End Globals")</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">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><span class="Constant">9999</span><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="Delimiter">:(before "End Primitive Recipe Declarations")</span>
ALLOCATE<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
put<span class="Delimiter">(</span>Recipe_ordinal<span class="Delimiter">,</span> <span class="Constant">"allocate"</span><span class="Delimiter">,</span> ALLOCATE<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">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 allocation</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>
size = <span class="Comment">/*</span><span class="Comment">space for length</span><span class="Comment">*/</span><span class="Constant">1</span> + size*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>
<span class="Delimiter">}</span>
<span class="Comment">// include space for refcount</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">"allocating size "</span> << size << end<span class="Delimiter">();</span>
<span class="CommentedCode">//? Total_alloc += size;</span>
<span class="CommentedCode">//? Num_alloc++;</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">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">int</span> address = result<span class="Delimiter">;</span> address < result+size<span class="Delimiter">;</span> ++address<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">"storing 0 in location "</span> << address << end<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="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">// initialize array length</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> << 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> << <span class="Constant">" in location "</span> << result+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span> << end<span class="Delimiter">();</span>
put<span class="Delimiter">(</span>Memory<span class="Delimiter">,</span> result+<span class="Comment">/*</span><span class="Comment">skip refcount</span><span class="Comment">*/</span><span class="Constant">1</span><span class="Delimiter">,</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>
<span class="Delimiter">}</span>
Current_routine<span class="Delimiter">-></span>alloc += size<span class="Delimiter">;</span>
<span class="Comment">// no support yet for reclaiming memory between routines</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="Comment">//: statistics for debugging</span>
<span class="CommentedCode">//? :(before "End Globals")</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>
<span class="CommentedCode">//? cerr << Total_alloc << "/" << Num_alloc</span>
<span class="CommentedCode">//? << " vs " << Total_free << "/" << Num_free << '\n';</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">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>
exit<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
<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><span class="Constant">9999</span><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">% put(Memory, Memory_allocated_until, 1);</span>
def main [
<span class="Constant">1</span>:address:number<span class="Special"> <- </span><span class="Normal">new</span> number:type
]
<span class="traceContains">+mem: storing 0 in location 10</span>
<span class="Delimiter">:(scenario new_array)</span>
def 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: "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>
<span class="Delimiter">:(scenario new_empty_array)</span>
def 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: "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>
<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 = 3; // barely enough room for point allocation below</span>
def 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 1003</span>
<span class="traceContains">+new: routine allocated memory from 1003 to 1006</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->