How to extract

36 views
Skip to first unread message

Nate Griswold

unread,
Jul 13, 2020, 12:43:49 PM7/13/20
to Racket Users
I had a question. In embedded racket, I am passing a _cpointer value back to c code by way of racket_apply's return value.

Looking over https://docs.racket-lang.org/inside/cs-values_types.html , there appears to be a group of functions associated with extracting values from ptrs. I do not see one for a pointer ptr there.

Is there a way to get at a returned _cpointer value from c code?

Thanks

Nate

Nate Griswold

unread,
Jul 13, 2020, 1:09:28 PM7/13/20
to Racket Users
A bit more context:

I am using a communications library (zeromq) and i need to share a c context object between c and racket for in-process communication.

I can either create it in c or create it in racket. The library i'm using happens to create it in racket.

The only way i know of solving this problem is to create an init foreign function in racket and pass in the _cpointer data and grab the c pointer when my c function is called.

Is there any other way to solve this problem?

Nate

Matthew Flatt

unread,
Jul 13, 2020, 1:20:55 PM7/13/20
to Nate Griswold, Racket Users
I see that there's not a good way right now, but here's a workaround
that uses information about the current layout:

A cpointer value is implemented as a Chez Scheme record with either 1
field or 2 fields. There are 2 fields only when the cpointer has an
offset as a result of `ptr-add`, so you can probably ignore that.

To extract the first field, assume that a record has the same layout as
a vector, so use `Svector_ref(p, 0)` to extra the field from the
cpointer `p`.

Then you can use `Sunsigned_value()` to convert that field value to a
pointer-sized integer, then case.

I might have some part of that wrong, but it should be close... Of
course, there should be better support for record-field access and
cpointer extraction, so I'll add to the API.
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/CAM-xLPrKGgAgii7BjyfvCs6i0BmbMp0
> yoo09UoUF0nqVzX_CXQ%40mail.gmail.com.

Nate Griswold

unread,
Jul 13, 2020, 1:43:31 PM7/13/20
to Matthew Flatt, Racket Users
Awesome, thanks!

I actually just implemented it with the roundabout callback system, i think i might try out your suggestion though.

Thanks for working on racket!

Nate

Reply all
Reply to author
Forward
0 new messages