如何从方案字节向量中检索外部原始字节指针?

How to retrieve a foreign raw bytes pointer from a scheme bytevector?

Scheme 提供了一个 bytevector 类型,可用于对字节和字节数组进行低级操作(参见 r6rs and chez 手册)。但是,它似乎没有提供一种方法来检索底层 指针 到它正在存储的字节数组,我需要该指针传递给外部 C 函数来填充或读取数据字节向量。

为了更准确地说明上下文,我正在尝试为使用 Scheme 作为后端的 Idris2 中的字节低级处理编写一些代码,但我是 Scheme 的新手,所以我肯定忽略了一些东西显而易见:从字节向量中提取此指针的首选方法是什么?

将字节向量的地址作为整数返回的操作是非常不安全的,因为垃圾收集器随后可能会移动字节向量并将其他对象放在那里。将过时的地址传递给 C 代码可能会导致内存损坏。

Chez Scheme foreign-procedure say that an argument declared as u8* accepts a bytevector and passes the address of its contents to the foreign function. That's safe, because the FFI and GC cooperate to make sure the object is not moved between taking the address and calling the foreign function --- but see the warning about retaining the pointer in foreign data structures. See also lock-object 的文档,它暂时阻止 GC 移动或回收对象。

在 Racket 中,_bytes and _pointer foreign types work similarly. There's also a ptr-add 将类似指针的对象与偏移量组合在一起的操作。例如,如果 bs 是字节串,那么 (ptr-add bs 1 _byte) 会可靠地转换为 bs 的第二个字节的地址,即使 GC 移动 bs。不知道Chez有没有类似的功能