From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 9D17C7FE07 for ; Wed, 3 Feb 2016 12:48:49 +0100 (CET) IronPort-PHdr: 9a23:tIJS+haWdUbpTq0iYEXvign/LSx+4OfEezUN459isYplN5qZpcu6bnLW6fgltlLVR4KTs6sC0LqJ9fm+EjVYqb+681k8M7V0HycfjssXmwFySOWkMmbcaMDQUiohAc5ZX0Vk9XzoeWJcGcL5ekGA6ibqtW1aJBzzOEJPK/jvHcaK1oLsh7/0osOYOl0ArQH+SI0xBS3+lR/WuMgSjNkqAYcK4TyNnEF1ff9Lz3hjP1OZkkW0zM6x+Jl+73YY4Kp5pIYTGZn9Ko84V71DEDM+M1cVesLmr1GXRguV52AAVX0W1BpPDgfI9jnmQ9L7vzH+t+w71CTMbuPsSrVhezm44qJqRVfEiSIGMTlxpGPbi8lxiqVzohunqgdj2YPZfMeeM/8oLfCVRs8TWWcUBpUZbCdGGI7pN4Y= Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=jdimino@janestreet.com; spf=Pass smtp.mailfrom=jdimino@janestreet.com; spf=None smtp.helo=postmaster@mxout1.mail.janestreet.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of jdimino@janestreet.com) identity=pra; client-ip=38.105.200.112; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="jdimino@janestreet.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of jdimino@janestreet.com designates 38.105.200.112 as permitted sender) identity=mailfrom; client-ip=38.105.200.112; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="jdimino@janestreet.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mxout1.mail.janestreet.com) identity=helo; client-ip=38.105.200.112; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="jdimino@janestreet.com"; x-sender="postmaster@mxout1.mail.janestreet.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0A7AADX57FWnHDIaSZehHkGiFWxPgENgWSGDQKBOwc4FAEBAQEBAQEBEAEBAQEBBhYJT4ItghUBAQMBEhEEGQEBNwEECwkCBAc3AgIiEgEFARwGEyKHcQgDkziPP4ExPjGKTWeEQAEEikgBAQEBAQUBAQEBAQEBARIGCoYIhDeHMoE6lnaNTY5xjQQRHoENHgEBgjgNEQiBSGqJawEBAQ X-IPAS-Result: A0A7AADX57FWnHDIaSZehHkGiFWxPgENgWSGDQKBOwc4FAEBAQEBAQEBEAEBAQEBBhYJT4ItghUBAQMBEhEEGQEBNwEECwkCBAc3AgIiEgEFARwGEyKHcQgDkziPP4ExPjGKTWeEQAEEikgBAQEBAQUBAQEBAQEBARIGCoYIhDeHMoE6lnaNTY5xjQQRHoENHgEBgjgNEQiBSGqJawEBAQ X-IronPort-AV: E=Sophos;i="5.22,390,1449529200"; d="scan'208,217";a="162777675" Received: from mxout1.mail.janestreet.com ([38.105.200.112]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 03 Feb 2016 12:48:48 +0100 Received: from tot-qpr-mailcore2.delacy.com ([172.27.56.106] helo=tot-qpr-mailcore2) by mxout1.mail.janestreet.com with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.82) (envelope-from ) id 1aQvvT-0006hP-Kx for caml-list@inria.fr; Wed, 03 Feb 2016 06:48:47 -0500 X-JS-Flow: external Received: by tot-qpr-mailcore2 with JS-mailcore (0.1) (envelope-from ) id BWsekf-AAACko-S4; 2016-02-03 06:48:47.605095-05:00 Received: from mail-ig0-f182.google.com ([209.85.213.182]) by mxgoog1.mail.janestreet.com with esmtps (UNKNOWN:AES128-GCM-SHA256:128) (Exim 4.72) (envelope-from ) id 1aQvvT-0005H9-HE for caml-list@inria.fr; Wed, 03 Feb 2016 06:48:47 -0500 Received: by mail-ig0-f182.google.com with SMTP id hb3so9067121igb.0 for ; Wed, 03 Feb 2016 03:48:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=BR6xTdirCWEef7DJNZ9wdOlA3xdqXT5Kpmtwm/9Q9KY=; b=iWEGh47rl+He7KTJXOqwrEP/OY/khF+punwo9tJV4SDlIb8O4rMZLSceChQqg2UEod Xh3sBUEASU7B5/ZktrLRx4Wkge5bJT5KZfZ1L+BAsfZTNaNMopvO09d95IyuVdb3T2VG uaw1BwfjNqq/ivdW4g60uEQis4WnpmxHGgCP8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=BR6xTdirCWEef7DJNZ9wdOlA3xdqXT5Kpmtwm/9Q9KY=; b=LnVgt+DYPK/hFOjVKw357lXfvm78zt2kUTOC0FEKqhAYLH1qhaTcj8vY2zXBjAamRk euD1kV986sdQoMxlKZydzslMY5yor5mQdHEghz6K5TvY6PgPOFv0mW9ErNr3TMkTFzOV svFMEJO5fIA8UU0cZ7VsJ92zQX1InjJF1dvqFjnOv6/HXD7I4CnxEYh4ACbbkRJp2Yh7 +48KRB4mfTaQ9DzdZFdHc8KD/bWgo5hvvVHHdpzc8PxYRpdtdeBMcRoKbkqmDhCagKhr AGtTvgGi1nv2S23q2Q1NdB9ooZYdm7tnpTMD4Gd5a+dBduLnxaCXeLFjUdlwk9Z/KhB3 ZySw== X-Gm-Message-State: AG10YORGqVymxr1OEt5jnpOlCST5n+XQzZQV8GViJe/54KsgMEXLhA8UCAkw4EfZ7DK68fXlv1hj25p/zb7pSFi+3zywY/J2LI5k0Um0WMiWVw9Xsk6HDTbod1fzQQRa8nQY87sxLcxGHQHW+d6pkqmxQA== X-Received: by 10.50.129.97 with SMTP id nv1mr24398744igb.0.1454500127223; Wed, 03 Feb 2016 03:48:47 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.50.129.97 with SMTP id nv1mr24398736igb.0.1454500127093; Wed, 03 Feb 2016 03:48:47 -0800 (PST) Received: by 10.50.30.193 with HTTP; Wed, 3 Feb 2016 03:48:47 -0800 (PST) In-Reply-To: <56B1DC54.1060109@tu-berlin.de> References: <56B1DC54.1060109@tu-berlin.de> Date: Wed, 3 Feb 2016 11:48:47 +0000 Message-ID: From:Jeremie Dimino To:=?UTF-8?Q?Christoph_H=C3=B6ger?= Cc:caml users Content-Type: multipart/alternative; boundary=047d7b41431c66e483052adc33c9 X-JS-Processed-by: mailcore Subject: Re: [Caml-list] Save callbacks from OCaml to C --047d7b41431c66e483052adc33c9 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Wed, Feb 3, 2016 at 10:54 AM, Christoph H=C3=B6ger < christoph.hoeger@tu-berlin.de> wrote: > void g(void* user_data, double t, double const *x, double *g) { > const struct interface_data* data =3D ((struct interface_data*)user_dat= a); > static long count =3D 0; > count++; > > /* Wrap the values in fresh big arrays */ > value ml_t =3D caml_copy_double(t); > value ml_x =3D caml_ba_alloc_dims(CAML_BA_FLOAT64 | CAML_BA_C_LAYOUT, 1, > (double*)x, data->qs->n); > value ml_g =3D caml_ba_alloc_dims(CAML_BA_FLOAT64 | CAML_BA_C_LAYOUT, 1, > g, data->qs->mc); > > /* call the OCaml callback */ > caml_callback3(data->g, ml_t, ml_x, ml_g); > } > > Is there anything obvious, I am doing wrong? > =E2=80=8BYou need to register [ml_t], [ml_x] and [ml_g=E2=80=8B ] as GC roots. Otherwise if the GC runs in caml_ba_alloc for instance, [ml_t] might ends up containing garbage even before reaching [caml_callback3]. You can use the normal macros for that: void g(...) { CAMLparam0(); CAMLlocal3(ml_t, ml_x, ml_g); ... CAMLreturn0; } =E2=80=8BNote that &(user_data->g) must be a GC root as well. Are you regis= tering &(user_data->g) with the GC in any way? =E2=80=8B =E2=80=8B --=20 Jeremie --047d7b41431c66e483052adc33c9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Wed, Fe= b 3, 2016 at 10:54 AM, Christoph H=C3=B6ger <christoph.hoeger@tu-berlin.de><= span style=3D"font-family:arial,sans-serif"> wrote:
void g(void* user_data, double t, double const *x, double *g) {
=C2=A0 const struct interface_data* data =3D ((struct interface_data*)user_= data);
=C2=A0 static long count =3D 0;
=C2=A0 count++;

=C2=A0 /* Wrap the values in fresh big arrays */
=C2=A0 value ml_t =3D caml_copy_double(t);
=C2=A0 value ml_x =3D caml_ba_alloc_dims(CAML_BA_FLOAT64 | CAML_BA_C_LAYOUT= , 1,
(double*)x, data->qs->n);
=C2=A0 value ml_g =3D caml_ba_alloc_dims(CAML_BA_FLOAT64 | CAML_BA_C_LAYOUT= , 1,
g, data->qs->mc);

=C2=A0 /* call the OCaml callback */
=C2=A0 caml_callback3(data->g, ml_t, ml_x, ml_g);
}

Is there anything obvious, I am doing wrong?

=E2=80=8BYou need to register [ml_t], [ml_x] and [m= l_g=E2=80=8B
] a= s GC roots. Otherwise if the GC runs in caml_ba_alloc for instance, [ml_t] = might ends up containing garbage even before reaching [caml_callback3]. You= can use the normal macros for that:

void g(...) {
=C2=A0 CAMLparam0();
=C2=A0 CAMLlocal3(= ml_t, ml_x, ml_g);
=C2=A0 ...
=C2=A0 CAMLreturn0;
}

=E2=80=8BNote that = &(user_data->g) must be a GC root as well. Are you registering &= (user_data->g) with the GC in any way?
=E2=80=8B
=E2=80=8B
--
Jeremie
--047d7b41431c66e483052adc33c9--