caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Chris Campbell" <cyberdanx@gmail.com>
To: caml-list@yquem.inria.fr
Subject: Converting C arrays to Ocaml arrays
Date: Mon, 10 Apr 2006 21:42:20 +0100	[thread overview]
Message-ID: <c22844d10604101342t5e5796f1g9618eb9a07cbf658@mail.gmail.com> (raw)

Hi,

I'm trying to patch lablgl to return an array of texture ids using
glGenTextures, however am a little unsure how to go about the
conversion from C array to Ocaml array.  In this case we have an array
of GLuints allocated by calloc and we wish to create an ocaml array
containing these items.  This seems simple, create an ocaml array and
copy the values.  However, in the ffi the routine caml_alloc_array
takes an array of pointers to things (terminated by NULL) and a
function that takes a pointer to a thing and returns a ocaml value.

Is the correct way then to alloc another array, then fill it with the
pointers in the original array then call caml_alloc_array?  That seems
a bit redundant, although I think I might understand why ocaml ffi
might ask for this format.  Is there a macro to help with this, or a
better way?

This is what I reckon it would look like...

CAMLprim value ml_glGenTextures(value num)
{
  CAMLparam1(num);
  CAMLlocal1(ml_tex_ids);

  GLuint numTextures = Int32_val(num);

  GLuint *ids = (GLunit *)calloc(numTextures, sizeof(GLuint));
  glGenTextures(numTextures, ids);

  GLuint **id_ptrs = (GLuint **)calloc (numTextures+1, sizeof(GLunit *));
  id_ptrs[numTextures] = NULL;

  for (i = 0; i < numTextures; i++)
  {
     id_ptrs[i] = &ids[i];
  }

  ml_tex_ids = caml_alloc_array
(&wrapper_around_copy_int32_which_takes_ptr, id_ptrs);

  CAMLreturn(ml_tex_ids);
}

Am I on the right lines?


Cheers,
Chris Campbell


             reply	other threads:[~2006-04-10 20:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-10 20:42 Chris Campbell [this message]
2006-04-10 21:21 ` [Caml-list] " Michael Wohlwend
2006-04-11  3:41 ` Jonathan Roewen
2006-04-11  3:46   ` Jonathan Roewen

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=c22844d10604101342t5e5796f1g9618eb9a07cbf658@mail.gmail.com \
    --to=cyberdanx@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    /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 NNTP newsgroup(s).