caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Converting C arrays to Ocaml arrays
@ 2006-04-10 20:42 Chris Campbell
  2006-04-10 21:21 ` [Caml-list] " Michael Wohlwend
  2006-04-11  3:41 ` Jonathan Roewen
  0 siblings, 2 replies; 4+ messages in thread
From: Chris Campbell @ 2006-04-10 20:42 UTC (permalink / raw)
  To: caml-list

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


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

end of thread, other threads:[~2006-04-11  9:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-10 20:42 Converting C arrays to Ocaml arrays Chris Campbell
2006-04-10 21:21 ` [Caml-list] " Michael Wohlwend
2006-04-11  3:41 ` Jonathan Roewen
2006-04-11  3:46   ` Jonathan Roewen

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).