Tomas, a couple of comments on your code: 1. It is normal that you see only a single reference to procedure Kernel.New in the Oberon 2013 system, namely in procedure System.Trap - which is the system's trap handler. This is because in Oberon 2013, the operator NEW is *implemented* using the trap mechanism (in order to avoid any reference to module Kernel in the compiler). So, when the programmer writes NEW (p), the compiler generates code that first assigns the address of p to register R0 (which will then appear as variable 'a' in procedure System.Trap), and the type tag (=the address of the type descriptor) to register R1 (which will then appears as variable 'b' in procedure System.Trap), following by an unconditional branch instruction via the R12 (the module table MT), using trap number 0. So, at runtime, this generates trap 0 which in turn lands you in procedure System.Trap which then simply calls Kernel.New(a, b). 2. In your code you allocate the bitmap (VAR b: Bitmap) as a *local* variable. Make it global. This is necessary because the garbage collector *expects* the type tag to be there. The Oberon garbage collector uses a mark-scan scheme. In the mark phase (Kernel.Mark) all dynamic objects reachable from all currently loaded modules (mod.ptr in the module table) are marked. In the scan phase (Kernel.Scan), all non-reachable objects are collected. In both cases, the garbage collector dereferences p to obtain its size (p.tag points to the type descriptor, which in turn has the size of the record in its first slot). 3. Instead of just storing the *size* of the allocated object, you may in fact want to exactly replicate the *full* Oberon type descriptor, and make it globally available in your program. See chapter 8.2., page 109, of the book Project Oberon, for a detailed description of the fields of an Oberon type descriptor. But in essence, it consists of a) the size of the allocated object, b) 3 entries for the so-called extensions, and c) a list of pointer field offsets needed for the garbage collector (terminated by a sentinel value of -1). You don't really need b) and c), so you could write something like: TYPE TypeTag = RECORD size: LONGINT; ext: ARRAY 3 OF LONGINT; ptr: LONGINT; <other fields that you may want to add> END ; Then create the "type tag" by writing: VAR tag: TypeTag; and initialise it with: tag.size := <size of your object to be allocated>; tag.ext[0] := 0; tag.ext[1] := 0; tag.ext[2] := 0; tag.ptr := -1; 4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod. Andreas
To be precise: If you don't make dynamically allocated objects reachable via at least one global pointer variable, they simply won't be marked during the mark phase of the garbage collector, which means that they *will* be collected during the subsequent scan phase. That's the whole point of garbage collection... From: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> To: ETH Oberon and Related Systems <oberon@lists.inf.ethz.ch> Cc: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> Sent: Friday, April 7, 2017 5:35 PM Subject: [Oberon] Fw: FPGA - Bitmaps.Mod ... 4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod.
Hi Tomas I refined the module "Bitmaps" a little bit. This code allocates variably sized bitmaps and makes sure the garbage collector collects them if no longer referenced. As Andreas pointed out: it is the responsibility of the module using "Bitmaps" to make the bitmaps globally reachable. If not, the allocated memory gets freed up more or less immediately... I compiled the code with the emulator on a Mac MODULE Bitmaps; (* jr/11apr17 *) IMPORT SYSTEM, Kernel := KernelOSX; TYPE TypeTag = RECORD size, ext1, ext2, ext3, offset: INTEGER END; (* see Project Oberon, figure 8.8 *) Address = POINTER TO RECORD END; (* this pointer will be redefined by TypeTag *) Bitmap* = POINTER TO RECORD width, height: INTEGER; tag: TypeTag; (* type descriptor of the pointer "base" *) base: Address END; PROCEDURE New*(w, h: INTEGER): Bitmap; VAR b: Bitmap; BEGIN NEW(b); IF b # NIL THEN b.width := w; b.height := h; (* the following code defines a type tag corresponding to ARRAY size OF BYTE *) b.tag.size := (w+7) DIV 8 * h; (* size of the bitmap in BYTEs *) b.tag.offset := -1; (* type has no pointers, important for GC *) (* allocate memory and define its type *) Kernel.New(SYSTEM.VAL(INTEGER, b.base), SYSTEM.ADR(b.tag)); IF b.base = NIL THEN b := NIL END END RETURN b END New; END Bitmaps. System.Free Bitmaps ~ ORP.Compile Bitmaps.Mod ~ 2017-04-07 17:41 GMT+02:00 Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> :
To be precise:
If you don't make dynamically allocated objects reachable via at least one global pointer variable, they simply won't be marked during the mark phase of the garbage collector, which means that they *will* be collected during the subsequent scan phase. That's the whole point of garbage collection...
------------------------------ *From:* Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> *To:* ETH Oberon and Related Systems <oberon@lists.inf.ethz.ch> *Cc:* Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> *Sent:* Friday, April 7, 2017 5:35 PM *Subject:* [Oberon] Fw: FPGA - Bitmaps.Mod
...
4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod.
-- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/mailman/listinfo/oberon
PS: There is a small proof-of-concept for a *generic* heap allocation routine New(ptr, size) that accepts a 'size' argument: andreaspirklbauer/Oberon-generic-heap-allocation It resembles the code in Bitmaps.Mod (and could be used within it). Andreas From: Jörg Straube <joerg.straube@iaeth.ch> To: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com>; ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Sent: Wednesday, April 12, 2017 8:05 AM Subject: Re: [Oberon] Fw: FPGA - Bitmaps.Mod Hi Tomas I refined the module "Bitmaps" a little bit. This code allocates variably sized bitmaps and makes sure the garbage collector collects them if no longer referenced. As Andreas pointed out: it is the responsibility of the module using "Bitmaps" to make the bitmaps globally reachable. If not, the allocated memory gets freed up more or less immediately... I compiled the code with the emulator on a Mac MODULE Bitmaps; (* jr/11apr17 *) IMPORT SYSTEM, Kernel := KernelOSX; TYPE TypeTag = RECORD size, ext1, ext2, ext3, offset: INTEGER END; (* see Project Oberon, figure 8.8 *) Address = POINTER TO RECORD END; (* this pointer will be redefined by TypeTag *) Bitmap* = POINTER TO RECORD width, height: INTEGER; tag: TypeTag; (* type descriptor of the pointer "base" *) base: Address END; PROCEDURE New*(w, h: INTEGER): Bitmap; VAR b: Bitmap; BEGIN NEW(b); IF b # NIL THEN b.width := w; b.height := h; (* the following code defines a type tag corresponding to ARRAY size OF BYTE *) b.tag.size := (w+7) DIV 8 * h; (* size of the bitmap in BYTEs *) b.tag.offset := -1; (* type has no pointers, important for GC *) (* allocate memory and define its type *) Kernel.New(SYSTEM.VAL(INTEGER, b.base), SYSTEM.ADR(b.tag)); IF b.base = NIL THEN b := NIL END END RETURN b END New; END Bitmaps. System.Free Bitmaps ~ORP.Compile Bitmaps.Mod ~ 2017-04-07 17:41 GMT+02:00 Andreas Pirklbauer <andreas_pirklbauer@yahoo.com>: To be precise: If you don't make dynamically allocated objects reachable via at least one global pointer variable, they simply won't be marked during the mark phase of the garbage collector, which means that they *will* be collected during the subsequent scan phase. That's the whole point of garbage collection... From: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> To: ETH Oberon and Related Systems <oberon@lists.inf.ethz.ch> Cc: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> Sent: Friday, April 7, 2017 5:35 PM Subject: [Oberon] Fw: FPGA - Bitmaps.Mod ... 4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod. -- Oberon@lists.inf.ethz.ch mailing list for ETH Oberon and related systems https://lists.inf.ethz.ch/ mailman/listinfo/oberon
Correction: The link should be: http://github.com/andreaspirklbauer/Oberon-generic-heap-allocation From: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> To: ETH Oberon and related systems <oberon@lists.inf.ethz.ch> Cc: Jörg Straube <joerg.straube@iaeth.ch>; Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> Sent: Wednesday, April 12, 2017 8:51 AM Subject: Re: [Oberon] Fw: FPGA - Bitmaps.Mod PS: There is a small proof-of-concept for a *generic* heap allocation routine New(ptr, size) that accepts a 'size' argument: andreaspirklbauer/Oberon-generic-heap-allocation It resembles the code in Bitmaps.Mod (and could be used within it). Andreas
Correction: This should read 'TypeDesc' of course, and not 'TypeTag' (which is the *address* of the TypeDesc). From: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> To: ETH Oberon and Related Systems <oberon@lists.inf.ethz.ch> ... You don't really need b) and c), so you could write something like: TYPE TypeTag = RECORD size: LONGINT; ext: ARRAY 3 OF LONGINT; ptr: LONGINT; <other fields that you may want to add> END ; Then create the "type tag" by writing: VAR tag: TypeTag; and initialise it with: tag.size := <size of your object to be allocated>; tag.ext[0] := 0; tag.ext[1] := 0; tag.ext[2] := 0; tag.ptr := -1; 4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod. Andreas
Hi Andreas, Joerg, I have tried both methods suggested here. (a) without tag declaration, simply calling Kernel.New(b.base, SYSTEM.ADR(b.size)); (b) with tag declaration and persistence tag.size := <size of your object to be allocated>; tag.ext[0] := 0; tag.ext[1] := 0; tag.ext[2] := 0; tag.ptr := -1; Kernel.New(b.base, SYSTEM.ADR(tag.size)) They both seem to allocate a block of requested size, however when GC is run, sometimes heap is inconsistent and `System.Watch' reports strange values, e.g. Heap speace -1111221223-1100000% Tomas On Fri, 7 Apr 2017 18:12:38 +0200 Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> wrote:
Correction: This should read 'TypeDesc' of course, and not 'TypeTag' (which is the *address* of the TypeDesc).
From: Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> To: ETH Oberon and Related Systems <oberon@lists.inf.ethz.ch>
...
You don't really need b) and c), so you could write something like:
TYPE TypeTag = RECORD size: LONGINT;
ext: ARRAY 3 OF LONGINT;
ptr: LONGINT;
<other fields that you may want to add>
END ;
Then create the "type tag" by writing:
VAR tag: TypeTag;
and initialise it with:
tag.size := <size of your object to be allocated>; tag.ext[0] := 0; tag.ext[1] := 0; tag.ext[2] := 0; tag.ptr := -1;
4. If you want your objects to be automatically collected by the Oberon garbage collector, you must make it globally reachable via a global pointer. Then they will appear in mod.ptr in the module block which are used as roots for the garbage collector. (Oberon uses all named pointer variables in existence as roots for the mark phase of the garbage collector. See Oberon.GC for the details. It simply calls Kernel.Mark(mod.ptr) for each loaded module mod.
Andreas
participants (3)
-
Andreas Pirklbauer -
Jörg Straube -
Tomas Kral