guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Passing C pointers through guile


From: Greg Troxel
Subject: Re: Passing C pointers through guile
Date: Wed, 09 Jul 2008 12:48:24 -0400
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.1 (berkeley-unix)

"Kjetil S. Matheussen" <address@hidden> writes:

> Ludovic Court?s:
>> Hi,
>>
>> "Maciek Godek" <address@hidden> 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 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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]