caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] ctypes - to string or to bigarray?
@ 2016-02-28 19:15 Malcolm Matalka
  2016-02-28 19:42 ` Gerd Stolpmann
  2016-02-29  8:41 ` Jeremy Yallop
  0 siblings, 2 replies; 3+ messages in thread
From: Malcolm Matalka @ 2016-02-28 19:15 UTC (permalink / raw)
  To: caml-list

I'm writing bindings for a library that is just doing I/O and wondering
what the suggested best practice is around using string or bigarray.  It
seems like using Bigarray is probably the most versatile interface.
People can decide to do the copying themselves or just use Bigarray
everywhere.

Are there any other things to consider?  Suggestions?

Thanks,
/Malcolm

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] ctypes - to string or to bigarray?
  2016-02-28 19:15 [Caml-list] ctypes - to string or to bigarray? Malcolm Matalka
@ 2016-02-28 19:42 ` Gerd Stolpmann
  2016-02-29  8:41 ` Jeremy Yallop
  1 sibling, 0 replies; 3+ messages in thread
From: Gerd Stolpmann @ 2016-02-28 19:42 UTC (permalink / raw)
  To: Malcolm Matalka; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1071 bytes --]

Use bigarrays. There is one striking advantage: you need not to make a
copy when calling blocking I/O functions. With strings/bytes you need
to, because another thread could otherwise move the buffer while you are
reading/writing.

Gerd

Am Sonntag, den 28.02.2016, 19:15 +0000 schrieb Malcolm Matalka:
> I'm writing bindings for a library that is just doing I/O and wondering
> what the suggested best practice is around using string or bigarray.  It
> seems like using Bigarray is probably the most versatile interface.
> People can decide to do the copying themselves or just use Bigarray
> everywhere.
> 
> Are there any other things to consider?  Suggestions?
> 
> Thanks,
> /Malcolm
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] ctypes - to string or to bigarray?
  2016-02-28 19:15 [Caml-list] ctypes - to string or to bigarray? Malcolm Matalka
  2016-02-28 19:42 ` Gerd Stolpmann
@ 2016-02-29  8:41 ` Jeremy Yallop
  1 sibling, 0 replies; 3+ messages in thread
From: Jeremy Yallop @ 2016-02-29  8:41 UTC (permalink / raw)
  To: Malcolm Matalka; +Cc: Caml List, ctypes

On 28 February 2016 at 19:15, Malcolm Matalka <mmatalka@gmail.com> wrote:
> I'm writing bindings for a library that is just doing I/O and wondering
> what the suggested best practice is around using string or bigarray.  It
> seems like using Bigarray is probably the most versatile interface.
> People can decide to do the copying themselves or just use Bigarray
> everywhere.

Yes, Bigarray is the more versatile choice, and probably the better default.

> Are there any other things to consider?  Suggestions?

Since ctypes bindings are just OCaml programs, one possibility is to
abstract over the choice of storage.  David Sheets's libsodium
bindings are a good example of this approach; for example, here's a
functor which builds bindings for half a dozen functions based on an
abstract "Sodium_storage.S" signature:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib_gen/sodium_bindings.ml#L64-L76

Elsewhere in the library are implementations of Sodium_storage.S for
Bigarray and for Bytes:

    https://github.com/dsheets/ocaml-sodium/blob/95d26a85/lib/sodium_storage.ml

One pleasant aspect of this approach is that the abstraction over
storage type doesn't cause any loss of type safety.  You can use all
the typed abstraction facilities of OCaml to construct your binding
descriptions, and the generated C code is checked against the headers
for the bound library by the C compiler.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-02-29  8:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-28 19:15 [Caml-list] ctypes - to string or to bigarray? Malcolm Matalka
2016-02-28 19:42 ` Gerd Stolpmann
2016-02-29  8:41 ` Jeremy Yallop

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 NNTP newsgroup(s).