From: Malcolm Matalka <mmatalka@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: caml-list@inria.fr, ctypes@lists.ocaml.org
Subject: Re: [Caml-list] ctypes - Advice for binding big structs?
Date: Sun, 28 Feb 2016 19:10:21 +0000 [thread overview]
Message-ID: <86si0chcrm.fsf@gmail.com> (raw)
In-Reply-To: <CAAxsn=GHFANVfrPsLVH8qD0+JswvykF4-Pfp2jseXE_NvhMj1Q@mail.gmail.com> (Jeremy Yallop's message of "Sun, 28 Feb 2016 00:56:37 +0000")
Jeremy Yallop <yallop@gmail.com> writes:
> Dear Malcolm,
>
> On 28/02/2016, Malcolm Matalka <mmatalka@gmail.com> wrote:
>> I have a large/complex struct I am trying to create bindings for
>> operations on it in Ocaml. I have an API that tells me how many bytes
>> the struct is so I can allocate it just fine and pass it around to C
>> functions I've bound with ctypes. But some data in it is accessed via
>> members. I started implementing a structure in ctypes for it, but it's
>> getting large and awkward. Are there any best practices around doing
>> this?
>
> The best approach is to use the Cstubs_structs module, which allows
> you to declare just the parts of the structs that you need to access
> in your program, and which generates code that uses the C struct
> declarations to work out sizes, alignments, field offsets, etc. The
> basic API is the familiar set of functions "structure", "field" and
> "seal" from the Ctypes module, but the build process is a little more
> involved. However, in return for the more complex build, all the
> issues that you're concerned about are addressed.
>
> The Cstubs_structs API is not yet very well documented, but there's a
> brief guide with examples in the pull request that introduced it:
>
> https://github.com/ocamllabs/ocaml-ctypes/pull/62
>
Great, with some small modifications this worked like a charm.
>> Some concerns I have:
>>
>> - It seems fragile - a different version of the library might have
>> different members in the struct so keeping my ocaml code in-synch
>> seems error prone.
>
> The Cstubs_structs module addresses this by using generated C code to
> determine the offsets of fields each time you build your library.
>
>> - It's annoying because the struct has a lot of members I don't care
>> about in my case. I only want access to a few members that have
>> important details.
>
> Since Cstubs_structs retrieves layout rather than computing it you
> only need to declare the members that you care about.
>
>> - The struct is large with lots of types that I don't necessarily want
>> to create so creating the struct becomes somewhat awkward. If I know
>> the size of the types I might be able to pretend it's an array of N
>> chars or something instead of trying to implement the type just to
>> fill out this struct, but I don't know if that is valid.
>
> Again, since Cstubs_structs retrieves struct layout and alignment
> information from C, you can use Ctypes.make to create struct values,
> even if you haven't declared all the fields.
>
> Kind regards,
>
> Jeremy.
prev parent reply other threads:[~2016-02-28 19:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-28 0:12 Malcolm Matalka
2016-02-28 0:56 ` Jeremy Yallop
2016-02-28 19:10 ` Malcolm Matalka [this message]
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86si0chcrm.fsf@gmail.com \
--to=mmatalka@gmail.com \
--cc=caml-list@inria.fr \
--cc=ctypes@lists.ocaml.org \
--cc=yallop@gmail.com \
/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