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

* Re: [Caml-list] Converting C arrays to Ocaml arrays
  2006-04-10 20:42 Converting C arrays to Ocaml arrays Chris Campbell
@ 2006-04-10 21:21 ` Michael Wohlwend
  2006-04-11  3:41 ` Jonathan Roewen
  1 sibling, 0 replies; 4+ messages in thread
From: Michael Wohlwend @ 2006-04-10 21:21 UTC (permalink / raw)
  To: caml-list

On Monday 10 April 2006 22:42, Chris Campbell wrote:
> 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?

no clue about the OpenGL stuff, but can't you just make a Bigarray of the 
c-array? Without copying anything?

cheers
 Michael


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

* Re: [Caml-list] Converting C arrays to Ocaml arrays
  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
  1 sibling, 1 reply; 4+ messages in thread
From: Jonathan Roewen @ 2006-04-11  3:41 UTC (permalink / raw)
  To: Chris Campbell; +Cc: caml-list

> 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);
> }

This may be naive of me, but couldn't it be much simpler?

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

 GLuint numTextures = Int_val(num);

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

 ml_tex_ids = caml_alloc_array(caml_copy_int32, ids);
 CAMLreturn(ml_tex_ids);
}

I believe the array should already be null-terminated. I'd have to
check the docs to see if caml_alloc_array really depends on the values
being pointers, but for now, I'm assuming that as long as type in the
array match the type required of the function to be applied, it should
work okay.

Jonathan


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

* Re: [Caml-list] Converting C arrays to Ocaml arrays
  2006-04-11  3:41 ` Jonathan Roewen
@ 2006-04-11  3:46   ` Jonathan Roewen
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Roewen @ 2006-04-11  3:46 UTC (permalink / raw)
  To: Chris Campbell; +Cc: caml-list

> I believe the array should already be null-terminated.

Well, I asked around, and some people say it's not.

So, instead, do: calloc(num+1), and then do: array[num] = NULL; and
null-termination problem will be gone.

And looking at caml_alloc_array, the most you'd prolly get is warning
about pointer vs int. caml_alloc_array doesn't appear to depend on it
being an actual pointer.

Jonathan


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