[Chicken-users] foreign-lambda* question
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 Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] foreign-lambda* question
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 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 > Chicken-users@nongnu.org > http://lists.nongnu.org/mailman/listinfo/chicken-users ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users
Re: [Chicken-users] foreign-lambda* question
2011/3/10 John J Foerch : > [...] > 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. ___ Chicken-users mailing list Chicken-users@nongnu.org http://lists.nongnu.org/mailman/listinfo/chicken-users