[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: db05
Subject: Re: [Chicken-users] how to declare foreign variably size structs?
Date: Sat, 22 Jun 2013 01:05:47 +0400
User-agent: Simple Mail V2.85

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]