[Top][All Lists]

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

Re: [Chicken-users] how to declare foreign variably size structs?

From: Geoffrey
Subject: Re: [Chicken-users] how to declare foreign variably size structs?
Date: Sat, 22 Jun 2013 21:09:14 +1000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130510 Thunderbird/17.0.6

Thanks db05.

So far i think the best technique is to use the SDL egg (which i think Mario pointed me at) as a reference. I believe it basically creates a blob and casts to the struct so the fields can be accessed.

Honestly at the moment, simply writing it in C looks more intelligible.


On 22/06/13 07:05, db05 wrote:
One possible solution read a whole object into predefined record

(define-record inotify wd mask cookie len name)

; this function make record and pass it into hepler function
(define (read-event fd)
   (c-read-event (make-inotify) fd))

; map inotify fields to record fields using lowlevel function C_block_item
(define c-read-event
    (foreign-safe-lambda* void ((scheme-object res) (int fd))
    "struct inotify_event* e;
    char buffer[sizeof(*e) + MAX_PATH];
    if (read(fd,buffer,sizeof(buffer)) < 0)
    C_word* ptr = C_alloc(C_SIZEOF_STRING(e->len));
    C_block_item(res,1) = C_fix(e->wd);
    C_block_item(res,2) = C_fix(e->mask);
    C_block_item(res,3) = C_fix(e->cookie);
    C_block_item(res,4) = C_fix(e->len);
    C_block_item(res,5) = C_string(&ptr,&e->name,numbytes);

this should work but i worry that uint32 didnt't fit into the fixnum (C_fix stuff)

;; or something like this

(match (file-read fd 1000)
    ((data size)
        (make-inotify wd: (read-uint32 data)
                      mask: (read-uint32 data)
                      cookie: (read-uint32 data)
                      len: (read-uint32 data)
                      name: (read-string data))))


06/20/13 23:58:52, Geoffrey <address@hidden>:
Hi I am trying to declare of variably sized c struct as a foreign declaration.

The one i am after is:
struct inotify_event
  int wd;                /* Watch descriptor. 
  uint32_t mask;        /* Watch mask.  */
  uint32_t cookie;        /* Cookie to synchronize
two events.  */
  uint32_t len;                /* Length (including
NULs) of name.  */
  char name __flexarr;        /* Name.  */
Note that name field is variably sized. I believe in C you would allocate a
larger block of memory and then cast it to 
By the skills of copy and paste i can do fixed size,and i can do a blob.
But not a variable struct.
Some help would be appreciated.
Chicken-users mailing list

reply via email to

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