access a `#<cpointer>` from c code

瀏覽次數:22 次
跳到第一則未讀訊息

Nate Griswold

未讀,
2020年10月5日 凌晨4:25:542020/10/5
收件者:Racket Users
What is the best way to access a value that is a cpointer from c code? Should i convert it to an integer? I have been looking at this for a while, about an hour, and will continue to look but just thought the list might be able to answer faster. I know there are chez functions to convert `ptr`s to integers but those aren't working for me (not an integer error).

Nate

Nate Griswold

未讀,
2020年10月5日 凌晨4:26:572020/10/5
收件者:Racket Users
Note this is from a return value of racket_apply; i called a racket function from c code and want to store the cptr it has returned.

Nate

Nate Griswold

未讀,
2020年10月5日 凌晨4:59:582020/10/5
收件者:Racket Users
I have a temporary workaround where i have created an ffi function racket-side to call into c, so that i can automatically use the _cptr's racket-to-c function. I wonder if there is a way to manually extract the c value for the case where i want it in a return value of racket_apply.

Nate

Nate Griswold

未讀,
2020年10月6日 凌晨4:55:532020/10/6
收件者:Racket Users
I realized i already asked this question and Matthew Flatt had responded like this, for anyone wondering:

---

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.

---

Nate

回覆所有人
回覆作者
轉寄
0 則新訊息