From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Olivier Dion via General Guile related discussions Newsgroups: gmane.lisp.guile.user Subject: Re: foreign objects and the garbage collector Date: Sun, 19 Sep 2021 14:11:44 -0400 Message-ID: <87czp4h1un.fsf@laura> References: <42b93d9ba50eceff320f3dde53ffd6ad77ab6070.camel@planete-kraus.eu> Reply-To: Olivier Dion Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25332"; mail-complaints-to="usenet@ciao.gmane.io" To: adriano , guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 19 20:12:24 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 1mS1Id-0006OE-QC for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 20:12:23 +0200 Original-Received: from localhost ([::1]:58146 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mS1Ib-0002LU-F5 for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 14:12:21 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55960) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mS1IF-0001sz-2p for guile-user@gnu.org; Sun, 19 Sep 2021 14:11:59 -0400 Original-Received: from smtp.polymtl.ca ([132.207.4.11]:37187) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mS1IC-0004hj-Qu for guile-user@gnu.org; Sun, 19 Sep 2021 14:11:58 -0400 Original-Received: from localhost (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 18JIBiLI009335 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 19 Sep 2021 14:11:49 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 18JIBiLI009335 In-Reply-To: X-Poly-FromMTA: (modemcable094.169-200-24.mc.videotron.ca [24.200.169.94]) at Sun, 19 Sep 2021 18:11:44 +0000 Received-SPF: pass client-ip=132.207.4.11; envelope-from=olivier.dion@polymtl.ca; helo=smtp.polymtl.ca X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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:17760 Archived-At: On Sun, 19 Sep 2021, adriano wrote: >> If you want to avoid the problem, you should explicitely bind and >> call the gps-close function and not rely on the garbage collector to >> do it for you. You can use dynamic-wind to open and close >> resources as needed. > > 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 ())) > >> That being said, make-pointer (from (system foreign-library)) is >> probably what you are expecting. It should work with gps_close. > > Another exmple of what you mean, here, would be so nice :-) 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))))) 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. 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. -- Olivier Dion Polymtl