From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: adriano Newsgroups: gmane.lisp.guile.user Subject: Re: foreign objects and the garbage collector Date: Tue, 21 Sep 2021 16:00:52 +0200 Message-ID: <5daca356c077a108d530b7a88d2361b4a0d63ce1.camel@riseup.net> References: <42b93d9ba50eceff320f3dde53ffd6ad77ab6070.camel@planete-kraus.eu> <87czp4h1un.fsf@laura> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25282"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Sep 21 16:01:45 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSgLB-0006Jf-8f for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 16:01:45 +0200 Original-Received: from localhost ([::1]:35028 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mSgL9-0001WU-95 for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 10:01:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41450) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSgKa-0001V4-Cj for guile-user@gnu.org; Tue, 21 Sep 2021 10:01:08 -0400 Original-Received: from mx1.riseup.net ([198.252.153.129]:57704) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSgKT-0005ci-CT for guile-user@gnu.org; Tue, 21 Sep 2021 10:01:07 -0400 Original-Received: from fews2.riseup.net (fews2-pn.riseup.net [10.0.1.84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "fews2.riseup.net", Issuer "R3" (not verified)) by mx1.riseup.net (Postfix) with ESMTPS id 4HDNPN6gt9zF54s; Tue, 21 Sep 2021 07:00:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1632232856; bh=N+Q5fQ+OEov4x87bIpwHVRJ9rP+lNaLsqx+9SiOposE=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=kSGLLNfiOJ+ykPFBl/slheE61RVY47/H4IWDLtgKhCx6fWvBoN+YWi2z52MkP6Jop udm1rvYqQ0iOfe3SL0QD1Qh/2nangj2lIlYHGsjWY09sLPqFAXmUWzDdx5VIBNno9u OzKOE5OLUkONMCCGtd52qnEBCyI8c27blEeWOaVU= X-Riseup-User-ID: 07A36210F803D5380DA9BB084108DF8B2A49EBEAA8A648C7682404C7D406C3D9 Original-Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews2.riseup.net (Postfix) with ESMTPSA id 4HDNPM5rmFz1yQp; Tue, 21 Sep 2021 07:00:55 -0700 (PDT) In-Reply-To: <87czp4h1un.fsf@laura> Received-SPF: pass client-ip=198.252.153.129; envelope-from=randomlooser@riseup.net; helo=mx1.riseup.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17769 Archived-At: Hi Olivier, thank you very much for your reply Il giorno dom, 19/09/2021 alle 14.11 -0400, Olivier Dion ha scritto: > On Sun, 19 Sep 2021, adriano wrote: > > > > > > It'd be so nice to have an example > > (define (with-my-resource token proc) >   (let ((resource #f)) >     (dynamic-wind >       (lambda () >         (set! resource (open-my-resource% token))) > >       (proc resource) > >       (lambda () >         (when resource >           (close-my-resource% resource)))))) > > (with-my-resource "some-internal-token" (lambda ())) Oh my, thank you for this ! This should be included in the manual ! The example that's there currently is totally indequate, in my opinion > > > > Says you have `open_my_resource()` and `close_my_resource()` in C in > library "libfoo.so" where open_my_resource takes a C string and returns > an integer for the resource while close_my_resource takes the integer > of > the resource: > > (define open-my-resource% >   (eval-when (eval load compile) >     (let ((this-lib (load-foreign-library "libfoo"))) >       (foreign-library-function this-lib "open_my_resource" >                                 #:return-type int >                                 #:arg-types (list '*)))) > > (define open-my-resource% >   (eval-when (eval load compile) >     (let ((this-lib (load-foreign-library "libfoo"))) >       (foreign-library-function this-lib "open_my_resource" >                                 #:return-type int >                                 #:arg-types (list int))))) Uhmm... I see 2 versions of open-my-resource% The only slight difference I see is in the #:arg-types The first one has  (list '*)  and the second one has (list int) Maybe you you got confused while editing ? This would be my version of close-my-resource% (define close-my-resource% (eval-when (eval load compile) (let ((this-lib (load-foreign-library "libfoo"))) (foreign-library-function this-lib "close_my_resource" #:return-type int #:arg-types (int))))) >                                 > Note that you probably need to do a wrapper named `open-my-resource` > to > do the conversion of scm_string to C raw pointer before calling > `open-my-resource%` in this particular case. Ok > > This is just an example, but it shows you that you can call foreign C > primitives easily without any C code, and that you have to use a > dynamic context to manage the lifetime of the C resources. > Thank you again