user1569339
user1569339

Reputation: 683

Common Lisp CFFI: Assign a struct value to an array index

How can I assign the value of an index of a foreign array to be the value of a foreign struct. For example, the following:

 (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))))

Which I expected to be equivalent roughly to

struct my_struct *x;
struct my_struct arr[2];

// x is initialized somewhere

arr[0] = *x;

Instead it wants to call the generic function cffi:translate-into-foreign-memory. Is there any way SETF foreign memory to foreign memory, by passing this?

Upvotes: 3

Views: 842

Answers (1)

user1569339
user1569339

Reputation: 683

With the help of a few others on irc.freenode.net #lisp, we figured it out: use 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))

Upvotes: 2

Related Questions