Array.new() is a way objects outside of Memory can allocate memory. It is just a wrapper around Memory.alloc.
What you can do in Memory, is to declare an Array object and set the object's address to an arbitrary memory address. The is using Arrays as pointers.
At a minimum, your Memory needs a pointer to the the free list. You can type 2048 wherever you need the heap base, but the code reads better if you also make a pointer for it.
class Memory {
static Array heap; // Heap base pointer
static Array freeList; // Free List head pointer
Memory.init() is responsible for initializing the heap and the free list, and only gets called once.
For instance, if your heap block headers are two words -- next_pointer and block_size -- then you do something like this to initialize the heap as a single block and put that block on the free list.
class Memory {
var Array block;
// Initialize heap base,
let heap = 2048;
// Initialize heap as one large block.
let block = heap;
let block[0] = null;
let block[1] = 14334; // 14K-2
// Put the block on the free list.
let free_list = block;
return;
}
Memory.alloc() can do similar things with Array variables to traverse the free list looking for an acceptable block.
(You can do all of this using ints and peek() and poke() but the code is much slower and uglier...)
--Mark