How to pass a C structure to a C library callback?

Andrew Whitworth wknight8111 at gmail.com
Thu Mar 3 01:05:12 UTC 2011


I've just committed two new functions, Parrot_api_wrap_pointer and
Parrot_api_unwrap_pointer. The first takes an opaque user data pointer
and wraps it up into a Ptr or PtrBuf PMC. The second takes that Ptr
PMC and returns your original pointer. Also, there's an optional size
parameter you can pass.

I'm going to put together some tests for the new functions, but you
should be okay to start using them right now if you don't mind living
a little dangerously.

Use this PMC with the "p" NCI signature element to pass the pointer
value to your callback.

On Wed, Mar 2, 2011 at 7:21 PM, Shockwave <vinfobag at gmail.com> wrote:
> Andrew, I know you probably know about this, but just as a reminder. When
> the pointer/pmc is passed to the external C function, that C function won't
> have access to the INTERP pointer.  If a PMC is passed to the C function, it
> may be difficult to extract the void*.

You can pass a reference to the interp PMC. First, you can get the
interp PMC in PIR like this:

$P0 = getinterp

Then, you can pass it to your callback with the "P" (capital P) NCI
signature element. Here's some example code (you have to fill in some
of the details):

.sub my_func
.param pmc data # my pointer PMC from Parrot_api_wrap_pointer
$P0 = getinterp
$P1 = dlfunc mylib, "my_callback", "vPp"
$P1($P0, data)
.end

I hope this gets you started, let us know if you have any questions.

--Andrew Whitworth


More information about the parrot-dev mailing list