From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?utf-8?Q?Court=C3=A8s?=) Newsgroups: gmane.lisp.guile.user Subject: Re: smob gc protection, and inheritance Date: Wed, 04 Sep 2013 23:13:27 +0200 Message-ID: <87y57c70js.fsf@gnu.org> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1378329530 14319 80.91.229.3 (4 Sep 2013 21:18:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 4 Sep 2013 21:18:50 +0000 (UTC) Cc: guile-user@gnu.org To: Doug Evans Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Sep 04 23:18:52 2013 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VHKTU-0005et-2v for guile-user@m.gmane.org; Wed, 04 Sep 2013 23:18:52 +0200 Original-Received: from localhost ([::1]:55771 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHKTT-0002zC-LR for guile-user@m.gmane.org; Wed, 04 Sep 2013 17:18:51 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:54086) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHKTE-0002yh-Ci for guile-user@gnu.org; Wed, 04 Sep 2013 17:18:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VHKT8-0007SN-Pz for guile-user@gnu.org; Wed, 04 Sep 2013 17:18:36 -0400 Original-Received: from hera.aquilenet.fr ([141.255.128.1]:37303) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VHKT8-0007SH-FL for guile-user@gnu.org; Wed, 04 Sep 2013 17:18:30 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 3949A1201; Wed, 4 Sep 2013 23:13:28 +0200 (CEST) Original-Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 41vBFOhkQTAl; Wed, 4 Sep 2013 23:13:28 +0200 (CEST) Original-Received: from pluto (reverse-83.fdn.fr [80.67.176.83]) by hera.aquilenet.fr (Postfix) with ESMTPSA id CA4D2E06; Wed, 4 Sep 2013 23:13:27 +0200 (CEST) X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 18 Fructidor an 221 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu In-Reply-To: (Doug Evans's message of "Tue, 03 Sep 2013 12:27:51 -0700") User-Agent: Gnus/5.130007 (Ma Gnus v0.7) Emacs/24.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x [fuzzy] X-Received-From: 141.255.128.1 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10752 Archived-At: Hi Doug, Doug Evans skribis: > I have a few questions about smobs: I=E2=80=99m assuming Guile 2.x here. > 1) Suppose I have some C code that creates a smob and its containing > SCM, but does not always expose the SCM to Scheme. > > E.g. > > struct foo_object > { > int bar; > SCM baz; > } > > static SCM > make_foo_smob (void) > { > struct foo_object *foo_smob =3D (struct foo_object *) > scm_gc_malloc (sizeof (struct foo_object), "foo"); > SCM foo_scm; > > foo_smob->bar =3D -1; > foo_smob->baz =3D SCM_BOOL_F; > > foo_scm =3D scm_new_smob (foo_smob_tag, (scm_t_bits) foo_smob); > > return foo_scm;=20=20 > } > > If the caller stores foo_smob in the heap somewhere, and not foo_scm, > is that enough to prevent the object from being garbage collected? Yes, because the region returned by =E2=80=98scm_gc_malloc=E2=80=99 is scan= ned by the GC. > 2) Is it possible to inherit, e.g., with goops, a smob? > IOW, can I extend a smob through inheritance? > Or must I store the smob in a class, and provide accessors? > [kinda like the "is a" vs "has a" relationship] Presumably, at least to some extent: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> (use-modules (gnutls)) scheme@(guile-user)> (make-session connection-end/client) $1 =3D # scheme@(guile-user)> (use-modules (oop goops)) scheme@(guile-user)> (class-of $1) $2 =3D #< 2e26000> scheme@(guile-user)> (define-class ()) scheme@(guile-user)> (change-class $1 ) ERROR: In procedure scm-error: ERROR: No applicable method for #< change-class (1)> in call (chan= ge-class # #< 2f33000>) Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. --8<---------------cut here---------------end--------------->8--- Andy can say more, I think. :-) > 3) The docs aren't as clear as they could be on whether the "smob" > free function needs to scm_gc_free all results of calls to scm_gc_malloc > made when constructing the smob. IIUC, this is not necessary. The =E2=80=98scm_gc_free=E2=80=99 function doesn=E2=80=99t need to be calle= d nowadays, because the GC automatically frees =E2=80=98scm_gc_malloc=E2=80=99 regions when the= y are no longer referenced. So chances are you don=E2=80=99t even need a SMOB =E2=80=98free=E2=80=99 fu= nction. > However, why does the image example do this? Indeed, the =E2=80=98mark=E2=80=99 and =E2=80=98free=E2=80=99 functions in = that example could be removed altogether, since the only resources associated with the SMOB is memory returned by =E2=80=98scm_gc_malloc=E2=80=99. Thanks, Ludo=E2=80=99.