From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 5FBAEBB81 for ; Mon, 10 Apr 2006 22:42:22 +0200 (CEST) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.239]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k3AKgLAn002830 for ; Mon, 10 Apr 2006 22:42:22 +0200 Received: by wproxy.gmail.com with SMTP id i3so835831wra for ; Mon, 10 Apr 2006 13:42:21 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition; b=f80L9m4NHlbYGna3cudIIqfI25IHD7TigeB2fQj6eYk+gPGLv9B/cZ4MdhUpFgOZifwQo7yNFtZlbsvGE5sWF55+Anj4iYymV9dU5CLFUxWZZXkUZQgPVLpy1ZkFzh5eMyevaLMULSy5vwKYULN0MxLTzKKkDmN1KKMVWkrs0QE= Received: by 10.65.52.3 with SMTP id e3mr4550594qbk; Mon, 10 Apr 2006 13:42:20 -0700 (PDT) Received: by 10.65.228.16 with HTTP; Mon, 10 Apr 2006 13:42:20 -0700 (PDT) Message-ID: Date: Mon, 10 Apr 2006 21:42:20 +0100 From: "Chris Campbell" To: caml-list@yquem.inria.fr Subject: Converting C arrays to Ocaml arrays MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline X-Miltered: at nez-perce with ID 443AC32D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; arrays:01 ocaml:01 arrays:01 lablgl:01 ocaml:01 ffi:01 alloc:01 pointers:01 pointer:01 alloc:01 pointers:01 ffi:01 camlprim:01 camlparam:01 val:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_BY_IP autolearn=disabled version=3.0.3 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 =3D Int32_val(num); GLuint *ids =3D (GLunit *)calloc(numTextures, sizeof(GLuint)); glGenTextures(numTextures, ids); GLuint **id_ptrs =3D (GLuint **)calloc (numTextures+1, sizeof(GLunit *)); id_ptrs[numTextures] =3D NULL; for (i =3D 0; i < numTextures; i++) { id_ptrs[i] =3D &ids[i]; } ml_tex_ids =3D 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