From 3571f998b28fbc8d9250ba04c983935f10a16c15 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 20 Jan 2018 15:02:59 +0100 Subject: removed dat files --- projects/12/Memory.jack | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'projects/12/Memory.jack') diff --git a/projects/12/Memory.jack b/projects/12/Memory.jack index b1e67ef..54e04aa 100644 --- a/projects/12/Memory.jack +++ b/projects/12/Memory.jack @@ -11,7 +11,7 @@ class Memory { static Array memory; static Array heap; - static int first; + static int last; /** Initializes the class. */ function void init() { @@ -19,6 +19,8 @@ class Memory { let heap = 2048; let heap[0] = -1; let heap[1] = 14334; + let last = 0; + return; } /** Returns the RAM value at the given address. */ @@ -29,6 +31,7 @@ class Memory { /** Sets the RAM value at the given address to the given value. */ function void poke(int address, int value) { let memory[address] = value; + return; } /** Finds an available RAM block of the given size and returns @@ -43,8 +46,9 @@ class Memory { let current = heap[current]; } if (current = -1) { - String.println("Insufficient space for allocation!"); - Sys.error(1); + do String.println("Insufficient space for allocation!"); + do Sys.error(1); + return -1; } else { let heap[current + 1] = heap[current + 1] - size2; let newLast = current + heap[current + 1] + 2; @@ -58,23 +62,11 @@ class Memory { /** De-allocates the given object (cast as an array) by making * it available for future allocations. */ function void deAlloc(Array o) { - var int base; - var int current; - var int prev; - let prev = 0; - let current = heap[prev]; + var int oBase; let oBase = o - heap - 2; - while ((current > -1) & (~(current = oBase))){ - let prev = current; - let current = heap[current]; - } - if (current = -1) { - String.println("The impossible happened"); - Sys.error(1); - } else { - let heap[prev] = heap[current] - let heap[prev + 1] = heap[prev] - prev - 2; - return; - } + let heap[oBase] = -1; + let heap[last] = oBase; + let last = oBase; + return; } } -- cgit v1.2.3