Version française
Home     About     Download     Resources     Contact us    
Browse thread
Converting C arrays to Ocaml arrays
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Converting C arrays to Ocaml arrays
> 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