unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
To: Greg Troxel <gdt@ir.bbn.com>
Cc: guile-user@gnu.org, "Kjetil S. Matheussen" <k.s.matheussen@notam02.no>
Subject: Re: Passing C pointers through guile
Date: Wed, 9 Jul 2008 18:55:03 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.58.0807091850550.3461@notam02.uio.no> (raw)
In-Reply-To: <rmi4p6zrokn.fsf@fnord.ir.bbn.com>

On Wed, 9 Jul 2008, Greg Troxel wrote:

> "Kjetil S. Matheussen" <k.s.matheussen@notam02.no> writes:
> 
> > Ludovic Court?s:
> >> Hi,
> >>
> >> "Maciek Godek" <pstrychuj@gmail.com> 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.




  reply	other threads:[~2008-07-09 16:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cmu-lmtpd-16223-1215360367-2@mail-imap1.uio.no>
2008-07-06 16:13 ` Passing C pointers through guile Kjetil S. Matheussen
2008-07-06 19:20   ` Ludovic Courtès
2008-07-09 16:48   ` Greg Troxel
2008-07-09 16:55     ` Kjetil S. Matheussen [this message]
2008-07-09 19:54       ` Ludovic Courtès
2008-07-10 15:11       ` Ken Raeburn
2008-07-23 11:19         ` Greg Troxel
2008-07-10 12:54 Kjetil S. Matheussen
  -- strict thread matches above, loose matches on Subject: below --
2008-07-04 22:18 Maciek Godek
2008-07-05 16:59 ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.58.0807091850550.3461@notam02.uio.no \
    --to=k.s.matheussen@notam02.no \
    --cc=gdt@ir.bbn.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).