Thanks Alex, The penny just dropped for me as I was writing this mail - yup, I remember now :) I think "finally" will do what I need :)
Regards, Kashyap On Sat, Dec 2, 2023 at 11:35 PM Alexander Burger <picolisp@software-lab.de> wrote: > Hi Kashyap, > > > Is there a reference implementation of external allocation using > > miniPicoLisp? > > > > Here's what I'd like to do - Create a new function called "alloc" that > > allocates some memory. > > > > (let A (alloc 100) > > (poke A 0 10)) # poke simply writes a byte(10) at offset 0 > > I do not know if anybody has done this in miniPicoLisp, but the official > PicoLisp has functions 'buf' and 'byte' which behave that way: > > (buf A 100 # Allocate on the stack > (byte A 10) > (byte (+ A 7) 11) > ... ) > > 'buf' allocates memory on the runtime stack, and cleans it up when done. > > > You can also explicitly allocate system memory to get the same effect, but > this > has more overhead: > > (let A (%@ "malloc" 'P 100) # Allocate memory > (byte A 10) > ... > (%@ "free" NIL A) ) # Clean up > > > > The part I want to ensure is that the memory that's allocated by alloc > gets > > collected by the GC. > > This does not make sense. The garbage collector traverses the 'heap' > structures, > managing used and unused cells (as discussed in our last mail). Memory > allocated > by 'buf' or malloc() does not contain cells reachable from Lisp data > structures, > and is therfore not known by the garbage collector. And as it is not part > of the > linked list of 'heap's, it will not be free'd. > > ☺/ A!ex > > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe >