|
| 1 | +var assert = require('assert'); |
| 2 | +var util = require('util'); |
| 3 | + |
| 4 | +// The heap (not yet initialized properly) |
| 5 | +var heap = new Array(1000); |
| 6 | + |
| 7 | +// Heap objects |
| 8 | + |
| 9 | +function HeapObject(next) { |
| 10 | + if (!this instanceof HeapObject) { |
| 11 | + return new HeapObject(next); |
| 12 | + } |
| 13 | + |
| 14 | + assert(isPointer(next)); |
| 15 | + |
| 16 | + // Pointers to other objects |
| 17 | + this.fst = null; |
| 18 | + this.snd = null; |
| 19 | + |
| 20 | + this._free = true; // Is this object free? |
| 21 | + this._next = next; // Next object on free list |
| 22 | + |
| 23 | + this._refcount = 0; |
| 24 | + this._traced = false; |
| 25 | +} |
| 26 | +exports.HeapObject = HeapObject; |
| 27 | + |
| 28 | +function isHeapObject(obj) { |
| 29 | + return (obj instanceof HeapObject); |
| 30 | +} |
| 31 | + |
| 32 | +// Low level pointers |
| 33 | + |
| 34 | +function Ptr(idx) { |
| 35 | + if (!this instanceof Ptr) { |
| 36 | + return new Ptr(idx); |
| 37 | + } |
| 38 | + assert(idx < heap.length); |
| 39 | + this._idx = idx; |
| 40 | +} |
| 41 | +exports.Ptr = Ptr; |
| 42 | + |
| 43 | +Ptr.prototype.equals = function(other) { |
| 44 | + if (other instanceof Ptr) { |
| 45 | + return (this._idx === other._idx); |
| 46 | + } |
| 47 | + return false; |
| 48 | +}; |
| 49 | + |
| 50 | +Ptr.prototype.deref = function() { |
| 51 | + // Task 1 {{ |
| 52 | + // }} |
| 53 | +}; |
| 54 | + |
| 55 | +Ptr.prototype.write = function(v) { |
| 56 | + if (!isHeapObject(v)) { |
| 57 | + throw new Error('Can only write HeapObject to the heap'); |
| 58 | + } |
| 59 | + // Task 1 {{ |
| 60 | + // }} |
| 61 | +}; |
| 62 | + |
| 63 | +Ptr.prototype.toString = function() { |
| 64 | + return this._idx.toString(); |
| 65 | +}; |
| 66 | + |
| 67 | +function isPointer(p) { |
| 68 | + return (p instanceof Ptr) || p === null; |
| 69 | +} |
| 70 | +exports.isPointer = isPointer; |
| 71 | + |
| 72 | + |
| 73 | +// Initialize the heap |
| 74 | + |
| 75 | + |
| 76 | +function initHeap() { |
| 77 | + // Task 2(a) {{ |
| 78 | + // }} |
| 79 | +} |
| 80 | + |
| 81 | +var free_list_head = new Ptr(0); |
| 82 | +var free_list_tail = new Ptr(heap.length - 1); |
| 83 | + |
| 84 | +initHeap(); |
| 85 | + |
| 86 | +// Exposed function for iterating over whole heap: |
| 87 | +exports.heapIterate = function(f) { |
| 88 | + for (var i = 0; i < heap.length - 1; i++) { |
| 89 | + f(new Ptr(i)); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +// Memory allocation |
| 94 | + |
| 95 | +function malloc() { |
| 96 | + // Task 2(b) {{ |
| 97 | + // }} |
| 98 | +} |
| 99 | +exports.malloc = malloc; |
| 100 | + |
| 101 | +function free(ptr) { |
| 102 | + assert(isPointer(ptr)); |
| 103 | + if (ptr === null) { |
| 104 | + throw new Error('Cannot free a null pointer'); |
| 105 | + } |
| 106 | + // Task 2(b) {{ |
| 107 | + // }} |
| 108 | +} |
| 109 | +exports.free = free; |
| 110 | + |
| 111 | +// An easier way of freeing the object pointed to by this pointer: |
| 112 | +Ptr.prototype.free = function() { free(this); }; |
| 113 | + |
| 114 | +// Diagnostics |
| 115 | + |
| 116 | +function showUsage() { |
| 117 | + var total = heap.length; |
| 118 | + var free = 1; |
| 119 | + var ptr = free_list_head; |
| 120 | + while (!ptr.equals(free_list_tail)) { |
| 121 | + free++; |
| 122 | + ptr = ptr.deref()._next; |
| 123 | + } |
| 124 | + return '[ free: ' + free + |
| 125 | + ', used: ' + (total - free) + |
| 126 | + ', total: ' + total + ' ]'; |
| 127 | +} |
| 128 | +exports.showUsage = showUsage; |
| 129 | + |
| 130 | +exports.showHeap = function(type) { |
| 131 | + var res = []; |
| 132 | + for (var i = 0; i < heap.length; i++) { |
| 133 | + var obj = heap[i]; |
| 134 | + var fst = obj.fst ? obj.fst._idx : null; |
| 135 | + var snd = obj.snd ? obj.snd._idx : null; |
| 136 | + if (!obj._free) { |
| 137 | + switch (type) { |
| 138 | + case 'ref': |
| 139 | + res.push(util.format('{%d @ [%d] : (%s, %s)}', i, |
| 140 | + obj._refcount, fst, snd)); |
| 141 | + break; |
| 142 | + case 'trace': |
| 143 | + res.push(util.format('{%d @ [%s] : (%s, %s)}', i, |
| 144 | + obj._traced ? '*' : '-', fst, snd)); |
| 145 | + break; |
| 146 | + default: |
| 147 | + res.push(util.format('{%d @ [%d|%s] : (%s, %s)}', i, |
| 148 | + obj._refcount, |
| 149 | + obj._traced ? '*' : '-', fst, snd)); |
| 150 | + break; |
| 151 | + }; |
| 152 | + } |
| 153 | + } |
| 154 | + return '[ ' + res.toString() + ' ]'; |
| 155 | +}; |
0 commit comments