[Oberon] ... MOD 65536
schierlm at gmx.de
Mon Apr 13 23:22:33 CEST 2020
Am 13.04.2020 um 21:09 schrieb Joerg:
> The garbage collector freeing up unused heap space is programmed in
> module Oberon but it‘s not exported. So officially, you can’t run the GC
> while your program runs.
And when you try, you may accidentally collect objects that are only
referenced on the stack by stack frames of your callers.
> With a little bit of trickery you could program your own garbage
> collector that frees up all data structure Trees allocated with NEW
> after every call of Trees. But this is a little far fetched...
Change the tree node to have an extra node pointer "chain" , and add two
more global node pointers "allocated" and "garbage" both initially NIL.
Replace every call to NEW(node) by code that
* if "garbage" is non-nil, use its value and advance it to
* if "garbage" is nil, allocate a new node.
In both cases, store the old value of "allocated" in the "new" node's
"chain" and store the new node in "allocated".
At the end of the algorithm (when discarding the old "tree"), move
"allocated" to "garbage".
Effectively have two linked lists of "allocated" and "free" tree nodes,
and use those before allocating new ones. That way, you can run the
algorithm as often as you want, it will always use the same tree nodes.
But you may also consider that cheating, as it is no longer benchmarking
the memory allocator after the first run.
More information about the Oberon