From: Malcolm Matalka <mmatalka@gmail.com>
To: Jeremie Dimino <jdimino@janestreet.com>
Cc: "Christoph Höger" <christoph.hoeger@tu-berlin.de>,
"caml users" <caml-list@inria.fr>
Subject: Re: [Caml-list] Save callbacks from OCaml to C
Date: Wed, 03 Feb 2016 12:26:15 +0000 [thread overview]
Message-ID: <86r3guq8t4.fsf@gmail.com> (raw)
In-Reply-To: <CANhEzE4MUw6Do5xY05d5L6MJeVxApQFn-Q6c+rM444nhmK_8ig@mail.gmail.com> (Jeremie Dimino's message of "Wed, 3 Feb 2016 11:48:47 +0000")
Jeremie Dimino <jdimino@janestreet.com> writes:
> On Wed, Feb 3, 2016 at 10:54 AM, Christoph Höger <
> christoph.hoeger@tu-berlin.de> wrote:
>
>> void g(void* user_data, double t, double const *x, double *g) {
>> const struct interface_data* data = ((struct interface_data*)user_data);
>> static long count = 0;
>> count++;
>>
>> /* Wrap the values in fresh big arrays */
>> value ml_t = caml_copy_double(t);
>> value ml_x = caml_ba_alloc_dims(CAML_BA_FLOAT64 | CAML_BA_C_LAYOUT, 1,
>> (double*)x, data->qs->n);
>> value ml_g = 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?
>>
>
> You need to register [ml_t], [ml_x] and [ml_g
> ] 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;
> }
>
> Note that &(user_data->g) must be a GC root as well. Are you registering
> &(user_data->g) with the GC in any way?
>
>
> --
> Jeremie
If one is using ctypes, is all of this taken care of? I have a library
that registers a bunch of Ocaml functions in C code, which the C code
calls. I haven't experienced anything bad happening yet, but that
doesn't mean much...
next prev parent reply other threads:[~2016-02-03 12:26 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-03 10:54 Christoph Höger
2016-02-03 11:48 ` Jeremie Dimino
2016-02-03 12:26 ` Malcolm Matalka [this message]
2016-02-03 13:44 ` David Sheets
2016-02-03 18:02 ` Jeremy Yallop
2016-02-03 20:15 ` Malcolm Matalka
2016-02-04 0:14 ` Jeremy Yallop
2016-02-04 7:26 ` Malcolm Matalka
2016-02-04 19:29 ` Jeremy Yallop
[not found] ` <56B1EC33.2090303@tu-berlin.de>
2016-02-03 13:49 ` Jeremie Dimino
2016-02-03 14:38 ` Christoph Höger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86r3guq8t4.fsf@gmail.com \
--to=mmatalka@gmail.com \
--cc=caml-list@inria.fr \
--cc=christoph.hoeger@tu-berlin.de \
--cc=jdimino@janestreet.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox