unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: cong gu <gucong43216@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: dynamic ffi and C struct
Date: Sat, 19 May 2012 20:45:44 -0400	[thread overview]
Message-ID: <871umf8ytj.fsf@netris.org> (raw)
In-Reply-To: <CAH_4JjM3Waefjyt3mEc2cbc-VeSzNTPc=sa2hU3X7EeKegvPvQ@mail.gmail.com> (cong gu's message of "Sat, 19 May 2012 06:32:07 -0500")

cong gu <gucong43216@gmail.com> writes:
> Is it possible to use the current dynamic ffi to call a C function
> whose parameter is a C struct (not a pointer to a C struct) ?

Although it is not documented in the manual, I see from the source code
that a C struct parameter can be specified using a nested list in the
foreign type passed to 'pointer->procedure'.

> For example, what should I do for the following function?
>
> typedef struct {
>   int dat[2];
> } foo_t;
>
> int func ( foo_t arg );

For this example, there's an additional complication of the inline array
within the struct, which is not supported by libffi, upon which Guile's
dynamic FFI is based.  I'm not an expert on this subject, but I strongly
suspect that on most (if not all) platforms, the struct above has the
same layout as the following one:

  typedef struct {
    int dat_0;
    int dat_1;
  } foo_t;

Assuming this is the case, here's one way to wrap 'func':

  (use-modules (system foreign))

  (define foo-library-obj (dynamic-link "libfoo"))
  (define foo-struct-type (list int int))
  
  (define (make-foo-struct dat-0 dat-1)
    (make-c-struct foo-struct-type (list dat-0 dat-1)))
  
  (define func
    (pointer->procedure int
                        (dynamic-func "func" foo-library-obj)
                        (list foo-struct-type)))

and here's an example of how to use it:

  (func (make-foo-struct 4 5))

It is also possible to wrap C functions that return structs, by passing
a list to the first parameter of 'pointer->procedure', and then using
'parse-c-struct' to extract the fields of the returned struct.

Hope this helps!

    Mark



  reply	other threads:[~2012-05-20  0:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-19 11:32 dynamic ffi and C struct cong gu
2012-05-20  0:45 ` Mark H Weaver [this message]
2012-05-20  3:40   ` Nala Ginrut

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871umf8ytj.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=gucong43216@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).