From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Kjetil S. Matheussen" Newsgroups: gmane.lisp.guile.user Subject: Re: Passing C pointers through guile Date: Wed, 9 Jul 2008 18:55:03 +0200 (CEST) Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Trace: ger.gmane.org 1215622532 4453 80.91.229.12 (9 Jul 2008 16:55:32 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 9 Jul 2008 16:55:32 +0000 (UTC) Cc: guile-user@gnu.org, "Kjetil S. Matheussen" To: Greg Troxel Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Jul 09 18:56:18 2008 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1KGcxh-0001xO-Tz for guile-user@m.gmane.org; Wed, 09 Jul 2008 18:56:16 +0200 Original-Received: from localhost ([127.0.0.1]:58914 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KGcwp-0004vg-Hg for guile-user@m.gmane.org; Wed, 09 Jul 2008 12:55:19 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KGcwj-0004uF-EH for guile-user@gnu.org; Wed, 09 Jul 2008 12:55:13 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KGcwh-0004sQ-My for guile-user@gnu.org; Wed, 09 Jul 2008 12:55:12 -0400 Original-Received: from [199.232.76.173] (port=34294 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KGcwh-0004sG-BO for guile-user@gnu.org; Wed, 09 Jul 2008 12:55:11 -0400 Original-Received: from mail-out1.uio.no ([129.240.10.57]:36227) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1KGcwg-0006HS-0W for guile-user@gnu.org; Wed, 09 Jul 2008 12:55:10 -0400 Original-Received: from mail-mx2.uio.no ([129.240.10.30]) by mail-out1.uio.no with esmtp (Exim 4.69) (envelope-from ) id 1KGcwZ-00075A-L3; Wed, 09 Jul 2008 18:55:03 +0200 Original-Received: from notam02.uio.no ([129.240.197.34]) by mail-mx2.uio.no with esmtp (Exim 4.67) (envelope-from ) id 1KGcwZ-0006pu-Fs; Wed, 09 Jul 2008 18:55:03 +0200 Original-Received: from kjetism (helo=localhost) by notam02.uio.no with local-esmtp (Exim 4.44) id 1KGcwZ-0002Mf-8u; Wed, 09 Jul 2008 18:55:03 +0200 X-X-Sender: kjetism@notam02.uio.no In-Reply-To: X-UiO-Spam-info: not spam, SpamAssassin (score=-5.0, required=5.0, autolearn=disabled, UIO_MAIL_IS_INTERNAL=-5, uiobl=NO, uiouri=NO) X-UiO-Scanned: 6E0D14C58EDE64B8599D6A6B48288F83B5BDB5BC X-UiO-SPAM-Test: remote_host: 129.240.197.34 spam_score: -49 maxlevel 200 minaction 1 bait 0 mail/h: 3 total 9488 max/h 25 blacklist 0 greylist 0 ratelimit 0 X-detected-kernel: by monty-python.gnu.org: Linux 2.6 (newer, 3) X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:6633 Archived-At: On Wed, 9 Jul 2008, Greg Troxel wrote: > "Kjetil S. Matheussen" writes: > > > Ludovic Court?s: > >> Hi, > >> > >> "Maciek Godek" writes: > >> > >>> is there any portable and recommended way for passing C pointers around > >>> in guile environment? > >>> I think of something like scm_to_ptr (analogous to scm_to_int etc.). > >>> Certainly such a value would be completely useless for the interpreter. > >>> One can achieve simillar functionality using the aforementioned > >>> scm_to_int (keeping track on the word length of the build target), > >>> but it seems rather unnatural. > >> > >> If the goal is to make a C object pointed to by some pointer available > >> to Scheme functions, the recommended (and simplest) way is to use a > >> "SMOB" (see the manual for details). In practice, SMOBs incur some > >> storage overhead because they hold 4 machines words, while you may only > >> need one. > >> > >> Another possibility is to use a "uo" field in a struct: it allows you to > >> have a struct field holding a machine word inaccessible to Scheme > >> functions (see the "Vtables" node of the manual). > >> > > > > I haven't heard of the "uo" field before, but at least using a SMOB > > is really inconvenient, and using an unsigned long for storing > > pointers is really convenient. (BTW. How does swig and gwrap handle > > pointers?) > > > > I agree with Maciek that it would at least be mind-comforting to have > > functions like scm_to_ptr/etc, although not strictly necessary, > > since using integers works just fine. > > Does C guarantee that pointers fit in unsigned long? > I don't know. But in practice: Yes. > I suspect SMOBs are the right answer. Part of the point is to do the > right thing when the scheme object is garbage collected. When > discarding a pointer often one should be freeing an object or > decrementing a refcount. > I've use SMOBs, and they really weren't that hard once I dug in. > Sure, if you just do this now and then, SMOBs aren't a problem. But that doesn't change the fact that all the functionality SMOB provides is overkill when the only thing you need is to hold a pointer. I just submitted a patch for unsigned ptr to guile-devel though, and if that patch is accepted, there is nothing more to discuss: For those who needs the full functionality of SMOB's, or need the extra speed and memory advantages of SMOB's, they are free to use SMOB's. If not, the functions scm_to/from_uintptr can be used instead, which is a billion times easier to use.