about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cpp/.traces/new_overflow20
-rw-r--r--cpp/042new.cc21
2 files changed, 41 insertions, 0 deletions
diff --git a/cpp/.traces/new_overflow b/cpp/.traces/new_overflow
new file mode 100644
index 00000000..6a24ab18
--- /dev/null
+++ b/cpp/.traces/new_overflow
@@ -0,0 +1,20 @@
+parse/0: instruction: 36
+parse/0:   ingredient: {name: "integer", value: 0, type: 0, properties: ["integer": "type"]}
+parse/0:   product: {name: "1", value: 0, type: 2-1, properties: ["1": "address":"integer", "raw": ]}
+parse/0: instruction: 36
+parse/0:   ingredient: {name: "point", value: 0, type: 0, properties: ["point": "type"]}
+parse/0:   product: {name: "2", value: 0, type: 2-6, properties: ["2": "address":"point", "raw": ]}
+new/0: integer -> 1
+new/0: point -> 6
+after-brace/0: recipe main
+after-brace/0: new ...
+after-brace/0: new ...
+new/0: routine allocated memory from 1000 to 1002
+schedule/0: main
+run/0: instruction main/0
+mem/0: new alloc: 1000
+mem/0: storing 1000 in location 1
+run/0: instruction main/1
+new/0: routine allocated memory from 1002 to 1004
+mem/0: new alloc: 1002
+mem/0: storing 1002 in location 2
diff --git a/cpp/042new.cc b/cpp/042new.cc
index c7a5cc11..ea620b85 100644
--- a/cpp/042new.cc
+++ b/cpp/042new.cc
@@ -71,6 +71,15 @@ case NEW: {
     }
   }
   // compute the resulting location
+  // really crappy at the moment
+  assert(size <= Initial_memory_per_routine);
+  if (Current_routine->alloc + size >= Current_routine->alloc_max) {
+    // waste the remaining space and create a new chunk
+    Current_routine->alloc = Memory_allocated_until;
+    Memory_allocated_until += Initial_memory_per_routine;
+    Current_routine->alloc_max = Memory_allocated_until;
+    trace("new") << "routine allocated memory from " << Current_routine->alloc << " to " << Current_routine->alloc_max;
+  }
   const size_t result = Current_routine->alloc;
   trace("mem") << "new alloc: " << result;
   if (current_instruction().ingredients.size() > 1) {
@@ -83,6 +92,8 @@ case NEW: {
   write_memory(current_instruction().products[0], tmp);
   // bump
   Current_routine->alloc += size;
+  // no support for reclaiming memory
+  assert(Current_routine->alloc <= Current_routine->alloc_max);
   break;
 }
 
@@ -111,6 +122,16 @@ recipe f2 [
 ]
 +mem: storing 0 in location 3
 
+//: If a routine runs out of its initial allocation, it should allocate more.
+:(scenario new_overflow)
+% Initial_memory_per_routine = 2;
+recipe main [
+  1:address:integer/raw <- new integer:type
+  2:address:point/raw <- new point:type  # not enough room in initial page
+]
++new: routine allocated memory from 1000 to 1002
++new: routine allocated memory from 1002 to 1004
+
 //:: Next, extend 'new' to handle a string literal argument.
 
 :(scenario new_string)