|
Hey all,
I thought a length method could be implemented on the Array class fairly easily. This would simplify other classes using the Array object, as they wouldn't have to track the maximum size of the Array.
This requires two changes. Firstly, the Memory.alloc method has to set the requested size in the returned block. As the next pointer (block[0]) is unused once the block has been allocated, this can store the length of the Array. block[1] continues to store the actual size of the block.
class Memory {
method int alloc(int size) {
...
let block[0] = size;
return block + 2;
}
}
class Array {
method int length() {
return this[-2];
}
}
Depending on how you have implemented the heap allocation, this may not work (e.g. if you have already optimised the size of the returned blocks).
Thanks,
Damien.
|