chicken-users
[Top][All Lists]
Advanced

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

[Chicken-users] foreign-lambda* question


From: John J Foerch
Subject: [Chicken-users] foreign-lambda* question
Date: Wed, 09 Mar 2011 22:35:18 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)

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




reply via email to

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