Common Lisp CFFI:将结构值分配给数组索引
Common Lisp CFFI: Assign a struct value to an array index
如何将外部数组的索引值指定为外部结构的值。例如,以下内容:
(cffi:with-foreign-objects ((x '(:struct my-struct))
(arr '(:struct my-struct) 2))
(setf (cffi:mem-aref arr '(:struct my-struct) 0)
(cffi:mem-ref x '(:struct my-struct))))
我预计大致相当于
struct my_struct *x;
struct my_struct arr[2];
// x is initialized somewhere
arr[0] = *x;
相反,它想调用通用函数 cffi:translate-into-foreign-memory
。有没有办法通过传递这个来将外部内存设置为外部内存?
在 irc.freenode.net #lisp 上其他一些人的帮助下,我们弄明白了:使用 memcpy
:
(cffi:with-foreign-objects ((x '(:struct my-struct))
(arr '(:struct my-struct) 2))
(cffi:foreign-funcall "memcpy"
:pointer (cffi:mem-aptr arr '(:struct my-struct) 0)
:pointer x
:int (cffi:foreign-type-size '(:struct my-struct))
:void))
如何将外部数组的索引值指定为外部结构的值。例如,以下内容:
(cffi:with-foreign-objects ((x '(:struct my-struct))
(arr '(:struct my-struct) 2))
(setf (cffi:mem-aref arr '(:struct my-struct) 0)
(cffi:mem-ref x '(:struct my-struct))))
我预计大致相当于
struct my_struct *x;
struct my_struct arr[2];
// x is initialized somewhere
arr[0] = *x;
相反,它想调用通用函数 cffi:translate-into-foreign-memory
。有没有办法通过传递这个来将外部内存设置为外部内存?
在 irc.freenode.net #lisp 上其他一些人的帮助下,我们弄明白了:使用 memcpy
:
(cffi:with-foreign-objects ((x '(:struct my-struct))
(arr '(:struct my-struct) 2))
(cffi:foreign-funcall "memcpy"
:pointer (cffi:mem-aptr arr '(:struct my-struct) 0)
:pointer x
:int (cffi:foreign-type-size '(:struct my-struct))
:void))