[ocaml-ctypes] How to memcpy?

Andre Nathan andre at digirati.com.br
Tue Jul 26 18:24:40 BST 2016


Hello

Say I have a C struct that looks like this:

  struct thing {
    void *buffer;
    unsigned long length;
  }

This struct is used to pass values of multiple types to a function. For
example, given

  struct thing *t = malloc(...);
  unsigned long len;

you do something like

  int x = 42;
  len = sizeof(int);
  t->length = len;
  t->buffer = malloc(len);
  memcpy(t->buffer, &x, len);

or

  char *s = "foo";
  len = strlen(s);
  t->length = len;
  t->buffer = malloc(len);
  memcpy(t->buffer, s, len);

In OCaml I'm representing the struct as

  module Thing = struct
    type t
    type t = thing structure
    let t : t typ = structure "thing"
    let buffer = field t "buffer" (ptr void)
    let length = field t "length" ulong
  end

What I couldn't figure out was how do do the equivalent of the memcpy()
step using Ctypes. I've found Stubs.memcpy but it doesn't seem to be
exported.

Can this kind of thing be done directly using Ctypes or should I write a
stub to handle this case?

Thanks in advance,
Andre

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://lists.ocaml.org/pipermail/ctypes/attachments/20160726/132be8b4/attachment.sig>


More information about the Ctypes mailing list