Racket FFI——C 数组 (_array) 用法示例?
Racket FFI -- example of C array (_array) usage?
我正在包装一个 C 函数,该函数将指针作为参数,并将其视为 int 指针、float 指针或 int 或 float 数组,具体取决于另一个参数。该函数是游戏中渲染器的一部分,每秒调用多次(60+)次。
话虽如此,我知道我可以使用 _list
来传递数组类型参数,但是
它会碎片化堆吗?如果我理解正确,球拍在每次调用之前调用 malloc
,在
之后调用 free
它需要一个特定的类型,所以我必须创建 2 个函数(一个用于 _int
,一个用于 _float
),这不是理想的
我偶然发现了 https://docs.racket-lang.org/foreign/C_Array_Types.html ,这似乎正是我想要的——既更高效 ("The array is not copied; the Racket representation is backed by the underlying C representation") 又适用于不止一种类型 ("it can have a different element type as long as that type matches the layout of the expected type")。
问题是,我似乎找不到从 Racket 端构建它的方法?这些都是陈述 return #f
,我没有想法
#lang racket
(require ffi/unsafe)
(displayln (array? (make-array-type _float 5)))
(displayln (array? (_array _float 5)))
(displayln (array? (malloc (_array _float 5))))
(displayln (array? (malloc (make-array-type _float 5))))
make-array-type
和 _array
函数创建 C types,它们是简单的 Racket run-time 值,描述了如何将各种值与 [=28= 相互转换] 表示:例如,_unt8
是 C 类型。谓词 array?
识别用基于 _array
的 C 类型表示的实际值,而不是 C 类型本身。
就构造数组值而言,the docs 说:
Since an array is treated like a struct, casting a pointer type to an array type does not work. Instead, use ptr-ref with a pointer, an array type constructed with _array, and index 0 to convert a pointer to a Racket representation that works with array-ref and array-set!.
以下是它在实践中的工作原理——文档可以使用示例:
#lang racket
(require ffi/unsafe
rackunit)
(define 5floats (_array _float 5))
(check-true
(array? (ptr-ref (malloc 5floats) 5floats)))
供将来参考,Racket 社区在 racket-users 邮件列表和 Slack 上比在 Stack Overflow 上活跃得多。
我正在包装一个 C 函数,该函数将指针作为参数,并将其视为 int 指针、float 指针或 int 或 float 数组,具体取决于另一个参数。该函数是游戏中渲染器的一部分,每秒调用多次(60+)次。
话虽如此,我知道我可以使用 _list
来传递数组类型参数,但是
它会碎片化堆吗?如果我理解正确,球拍在每次调用之前调用
malloc
,在 之后调用 它需要一个特定的类型,所以我必须创建 2 个函数(一个用于
_int
,一个用于_float
),这不是理想的
free
我偶然发现了 https://docs.racket-lang.org/foreign/C_Array_Types.html ,这似乎正是我想要的——既更高效 ("The array is not copied; the Racket representation is backed by the underlying C representation") 又适用于不止一种类型 ("it can have a different element type as long as that type matches the layout of the expected type")。
问题是,我似乎找不到从 Racket 端构建它的方法?这些都是陈述 return #f
,我没有想法
#lang racket
(require ffi/unsafe)
(displayln (array? (make-array-type _float 5)))
(displayln (array? (_array _float 5)))
(displayln (array? (malloc (_array _float 5))))
(displayln (array? (malloc (make-array-type _float 5))))
make-array-type
和 _array
函数创建 C types,它们是简单的 Racket run-time 值,描述了如何将各种值与 [=28= 相互转换] 表示:例如,_unt8
是 C 类型。谓词 array?
识别用基于 _array
的 C 类型表示的实际值,而不是 C 类型本身。
就构造数组值而言,the docs 说:
Since an array is treated like a struct, casting a pointer type to an array type does not work. Instead, use ptr-ref with a pointer, an array type constructed with _array, and index 0 to convert a pointer to a Racket representation that works with array-ref and array-set!.
以下是它在实践中的工作原理——文档可以使用示例:
#lang racket
(require ffi/unsafe
rackunit)
(define 5floats (_array _float 5))
(check-true
(array? (ptr-ref (malloc 5floats) 5floats)))
供将来参考,Racket 社区在 racket-users 邮件列表和 Slack 上比在 Stack Overflow 上活跃得多。