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 4E142BBC0 for ; Tue, 11 Apr 2006 11:05:19 +0200 (CEST) Received: from zproxy.gmail.com (zproxy.gmail.com [64.233.162.204]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id k3B3f7oA001625 for ; Tue, 11 Apr 2006 05:41:07 +0200 Received: by zproxy.gmail.com with SMTP id 13so956754nzp for ; Mon, 10 Apr 2006 20:41:06 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=qzzgk6/EkfqKMhFvZ9iHjWMet3XqLONmjKTRzzaWtzD+AO2D1IdVmzQVdS02lquZqC9Ogs4K0rQ8+T1iYDCkWvBqLlGDTHB24ORJBAKlN6yk/cMu6yrprx80WEJxeyIj2kqxrvCI+Xs2vjPhI7CjRMzZhUjDOxny29qv6WjWLNQ= Received: by 10.36.81.17 with SMTP id e17mr1116519nzb; Mon, 10 Apr 2006 20:41:06 -0700 (PDT) Received: by 10.36.121.13 with HTTP; Mon, 10 Apr 2006 20:41:06 -0700 (PDT) Message-ID: Date: Tue, 11 Apr 2006 15:41:06 +1200 From: "Jonathan Roewen" To: "Chris Campbell" Subject: Re: [Caml-list] Converting C arrays to Ocaml arrays Cc: caml-list@yquem.inria.fr In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline References: X-Miltered: at nez-perce with ID 443B2553.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; arrays:01 ocaml:01 arrays:01 camlprim:01 camlparam:01 val:01 sizeof:01 sizeof:01 alloc:01 camlreturn:01 camlprim:01 camlparam:01 val:01 alloc:01 camlreturn: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 > 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); > } 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 =3D Int_val(num); GLuint *ids =3D (GLuint *)calloc(numTextures, sizeof(GLuint)); glGenTextures(numTextures, ids); ml_tex_ids =3D 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