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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id 5DEB97FE07 for ; Wed, 3 Feb 2016 21:15:46 +0100 (CET) IronPort-PHdr: 9a23:l0lgaRM/f6lfPJd0NrYl6mtUPXoX/o7sNwtQ0KIMzox0KPv9rarrMEGX3/hxlliBBdydsKIbzbOI+Pm4BiQp2tWojjMrSNR0TRgLiMEbzUQLIfWuLgnFFsPsdDEwB89YVVVorDmROElRH9viNRWJ+iXhpQAbFhi3DwdpPOO9QteU1JTokbnssMSLPE1hv3mUX/BbFF2OtwLft80b08NJC50a7V/3mEZOYPlc3mhyJFiezF7W78a0+4N/oWwL46pyv+YJa6jxfrw5QLpEF3xmdjltvIy4/SXEGCmC/HYZQy0nmx5MBBCNuADzWpP2qgP1s+N83G+ROsigHp4uXjH3yq5vQxjsjW8iPiIw9GLewph1iatHqRairjRwxofVZMeeM/8oLfCVRs8TWWcUBpUZbCdGGI7pKtJXV+c= Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=mmatalka@gmail.com; spf=Pass smtp.mailfrom=mmatalka@gmail.com; spf=None smtp.helo=postmaster@mail-wm0-f47.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of mmatalka@gmail.com) identity=pra; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of mmatalka@gmail.com designates 74.125.82.47 as permitted sender) identity=mailfrom; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="mmatalka@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-wm0-f47.google.com) identity=helo; client-ip=74.125.82.47; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="mmatalka@gmail.com"; x-sender="postmaster@mail-wm0-f47.google.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CNAACTX7JWlC9SfUpehGoPiFuxFgENgWaGDQKBSjgUAQEBAQEBAQEQAQEBAQcLCwkfMIItghQBAQEDARIuARsdAQMBCwYFCw0JJQ8BBA0CEQEFASITGweHYwEDCggEo1+BMT4xjR2CV4UyChknDVGDbgEBAQEBAQEDAQEBAQEBFAEFCgSEAYIDhDeCN4Y1BZZxi1qBc4kShV+Gf4YFL4ENHgEBgjgegVBqiWsBAQE X-IPAS-Result: A0CNAACTX7JWlC9SfUpehGoPiFuxFgENgWaGDQKBSjgUAQEBAQEBAQEQAQEBAQcLCwkfMIItghQBAQEDARIuARsdAQMBCwYFCw0JJQ8BBA0CEQEFASITGweHYwEDCggEo1+BMT4xjR2CV4UyChknDVGDbgEBAQEBAQEDAQEBAQEBFAEFCgSEAYIDhDeCN4Y1BZZxi1qBc4kShV+Gf4YFL4ENHgEBgjgegVBqiWsBAQE X-IronPort-AV: E=Sophos;i="5.22,391,1449529200"; d="scan'208";a="201140108" Received: from mail-wm0-f47.google.com ([74.125.82.47]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 03 Feb 2016 21:15:25 +0100 Received: by mail-wm0-f47.google.com with SMTP id p63so87562068wmp.1 for ; Wed, 03 Feb 2016 12:15:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=tW9randIGopygAjF12dQhsGoOA55Gnnuxo1B21giD5U=; b=unDaebbcScOWZXLo30iRhhnBN2yEWEiwXNqM3KqsKUdVdToyyVuU8WnV0D4c8TKJ5V pgU9DpG8JcW7h7UokzunK9VXeQvBt7XTz4aOZIHVfA01LG1x9RBx5G/hcPrv3CX58yB2 8H6rDSfk6Tt+XYW12NzGfEcJf2Mycw9wvNncI7hrLuttz3kLC6ZsYuU+L/XmfzXWX49H ZpnNhS6HhA2KQdll7AzqOo7g/iU7lcEBEzsgcdOqwN1JCcJ+zX1Q3imiePotmAtTKfNU JYd/LwR4WqQehZbn4spPUt95bcrglKn4x/hpYNYpMb/tE04alkTs3EcGWlyqLAn0HFkI ZeSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=tW9randIGopygAjF12dQhsGoOA55Gnnuxo1B21giD5U=; b=D9IjQuL0O+9/Xp+lgAQnHhCFkHU85DKlXCuxxSEcPExubSXVgOd6XKjC+NG5caJSQk S5jnz+xaquQ4aJwUVN6HAeVPtpPM9oXI0BI8mfziyufWEK0i/9LihPCE1QwjB05+LWwg TZJM1on2yBiwZxZVybFr+xalZ6XR/kh+/FCYUNrCryCLH+oh1kZ+Q+WLWCF+7dlWGY1e whyDDe0SbIPWZEnJp1C45d4I93LhX/rdpUkWWRynP2fiWqR8a1GpA6H+ifTWmyqQNlGG /GEy7NT8bikTg4br7YYLBnlUHcxw559kvVzsZq2DkhTjqtHJOI7LVGSNHjIpUic0gXSj SiSA== X-Gm-Message-State: AG10YOSyCFupwHwz7iAGf3htAWNI76K2KfGGVagyTttVM//WZE45uhc5XgDd8VB7CiieNw== X-Received: by 10.28.212.85 with SMTP id l82mr5690875wmg.11.1454530525327; Wed, 03 Feb 2016 12:15:25 -0800 (PST) Received: from localhost ([37.153.108.22]) by smtp.gmail.com with ESMTPSA id c7sm8532853wmd.13.2016.02.03.12.15.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Feb 2016 12:15:23 -0800 (PST) From: Malcolm Matalka To: Jeremy Yallop Cc: David Sheets , Jeremie Dimino , Christoph =?utf-8?Q?H=C3=B6ger?= , caml users References: <56B1DC54.1060109@tu-berlin.de> <86r3guq8t4.fsf@gmail.com> Date: Wed, 03 Feb 2016 20:15:10 +0000 In-Reply-To: (Jeremy Yallop's message of "Wed, 3 Feb 2016 10:02:06 -0800") Message-ID: <86mvrhr1o1.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Caml-list] Save callbacks from OCaml to C Jeremy Yallop writes: > On 3 February 2016 at 05:44, David Sheets wrote: >> On Wed, Feb 3, 2016 at 12:26 PM, Malcolm Matalka wrote: >>> Jeremie Dimino writes: >>>> 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: >>>> >>> 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... >> >> If you use ctypes and pass OCaml closures to C, you *must* retain a >> reference to the closure to avoid it being GCed. If you do not, you >> may experience the exception CallToExpiredClosure sporadically. > > Besides David's caveat, the answer is yes: ctypes will take care of > registering arguments as GC roots as necessary. Can you clarify this a bit? I'm not that familiar with how the C FFI works. If I pass in a closure to a C function and it is registered as a GC root, doesn't that mean it won't be GCd if my Ocaml program forgets about it or? Also, David and I were talking about how to solve this on IRC. In my specific case, callbacks are one-shot, which means I know they need to be remembered until they are called then they can (possibly) be freed. Is there a nice solution here? I'd prefer not to store them in some other data structure and remove them later just to keep a reference alive, if possible. That is overhead I'd prefer to avoid, if possible. I plan on having possibly hundreds of thousands of these callbacks alive at any point in time. Thanks