chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] foreign-lambda* question


From: Thomas Chust
Subject: Re: [Chicken-users] foreign-lambda* question
Date: Thu, 10 Mar 2011 10:10:38 +0100

2011/3/10 John J Foerch <address@hidden>:
> [...]
> I have a procedure that takes a window and a list of numbers and
> sets a property on the window which is an array of those
> numbers.  The array must be an array of unsigned long, and
> foreign-lambda* seems to be the tool for the job to make this
> object.  In an earlier version, I hardcoded the length of the array
> (commented out in the version below), but in the interest of
> code-reuse, I want to generalize it to work on any size list.
> [...]

Hello John,

looking at your code I have two immediate questions: Why do you
recompute the length of a list twice, which is a linear operation,
when you could simply store the length or access the length of the
vector you create in constant time?

> I malloc memory and copy the data into the block, then return the
> pointer to the block for use in scheme.

And why do you allocate this block at all when you already have a
u32vector with precisely the same memory layout?

> My question is, what is the idiomatic way in Chicken to free the
> allocated memory, or turn it over to the garbage collector?
> [...]

If you really need this block of unmanaged memory for some obscure
reason, you can either manually free it at some point where it is safe
to do so, or you can register the free function as a finalizer on the
foreign pointer object and let the garbage collector invoke it after
the pointer has gone out of scope from the viewpoint of your CHICKEN
program. Something like this should do:

  (set-finalizer! YOUR-POINTER (foreign-lambda void "free" c-pointer))

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]