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: Jim Ursetto
Subject: Re: [Chicken-users] foreign-lambda* question
Date: Thu, 10 Mar 2011 01:50:16 -0600

John,
Presuming you no longer need the contents of "values" after the call to 
window-property-set, you can then use "free" from unit lolevel to free the 
memory. Is that the case?

On Mar 9, 2011, at 21:35, John J Foerch <address@hidden> wrote:

> Hello,
> 
> I'm writing a program that uses xlib and have hit a question that I
> cannot make heads or tails of from the web docs.  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.  I malloc
> memory and copy the data into the block, then return the pointer to the
> block for use in scheme.  My question is, what is the idiomatic way in
> Chicken to free the allocated memory, or turn it over to the garbage
> collector?  Code follows:
> 
> (define (set-struts win strut-spec)
>  (let ((values ((foreign-lambda* c-pointer ((u32vector s) (int length))
>                   ;;"unsigned long strut[12] ="
>                   ;;"    { s[0], s[1], s[2], s[3], s[4], s[5],"
>                   ;;"      s[6], s[7], s[8], s[9], s[10], s[11] };"
>                   "unsigned long * strut = malloc(sizeof(unsigned long) * 
> length);"
>                   "int i;"
>                   "for (i = 0; i < length; i++) {"
>                   "    strut[i] = s[i];"
>                   "}"
>                   "C_return(strut);")
>                 (list->u32vector strut-spec)
>                 (length strut-spec))))
>    (window-property-set win "_NET_WM_STRUT_PARTIAL"
>                         (vector "CARDINAL" 32 values
>                                 (length strut-spec)))))
> 
> Thank you,
> 
> -- 
> John Foerch
> 
> 
> _______________________________________________
> Chicken-users mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/chicken-users



reply via email to

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