Dynamically sized allocation with NEW()
Am 02.08.2018 um 20:10 schrieb Tomas Kral <thomas.kral at email.cz>:
FontDesc = RECORD raster: ARRAY 2000 OF BYTE END; LargeFontDesc = RECORD (FontDesc) extRaster: ARRAY 1000 OF BYTE END; VAR F: = ; LF: LargeFont; IF len<2000 THEN NEW (F) ELSE NEW(LF); F := LF END;
Rather clever, the below two pointers are needed, correct?
Font = POINTER TO FontDesc; LargeFont = POINTER TO LargeFontDesc;
Just as an FYI: In EO this is (now) done as follows: TYPE Raster = POINTER TO RasterArray; RasterArray = ARRAY OF BYTE; Font* = POINTER TO FontDesc; FontDesc* = RECORD ... raster: Raster (*dynamic open array*) END ; PROCEDURE This*(name: ARRAY OF CHAR): Font; VAR F: Font; ... BEGIN ... NEW(F); NEW(F.raster, NofBytes); ... END This; i.e. using dynamically allocated open *arrays* for the font raster and therefore without the need for LargeFont and LargeFontDesc.
On Mon, 14 Jan 2019 22:34:03 +0100 Andreas Pirklbauer <andreas_pirklbauer@yahoo.com> wrote:
NEW(F.raster, NofBytes);
Hi, very interesting. Is this allocated on the heap, or stack? I am also interested in the other world without dynamic allocation, i.e. NEW(p); Can we also toy with some allocation templates based on NEW(p); MODULE Heap; IMPORT S := SYSTEM; TYPE Size = INTEGER; Block = INTEGER; Buf16 = POINTER TO S16; Buf32 = POINTER TO S32; Buf64 = POINTER TO S64; Buf128 = POINTER TO S128; Buf256 = POINTER TO S256; Buf512 = POINTER TO S512; Buf1024 = POINTER TO S1024; S16 = RECORD s: ARRAY 16 OF BYTE END ; S32 = RECORD s: ARRAY 32 OF BYTE END ; S64 = RECORD s: ARRAY 64 OF BYTE END ; S128 = RECORD s: ARRAY 128 OF BYTE END ; S256 = RECORD s: ARRAY 256 OF BYTE END ; S512 = RECORD s: ARRAY 512 OF BYTE END ; S1024 = RECORD s: ARRAY 1024 OF BYTE END ; VAR buf16: Buf16; ... buf1024: Buf104; PROCEDURE Alloc*(sz: Size; VAR b: Block) BEGIN IF sz < 16 THEN NEW(Buf16); b := S.VAL(Block, Buf16) ELSIF sz < 32 THEN NEW(Buf32); b := S.VAL(Block, Buf32) ... ELSIF sz < 1024 THEN NEW(Buf1024); b := S.VAL(Block, Buf1024) ELSE b := NIL END END Alloc; END Heap; -- Tomas Kral <thomas.kral@email.cz>
participants (2)
-
Andreas Pirklbauer -
Tomas Kral