Hi folks!
Hopefully here is the right place to ask CFFI user related questions. What would be an appropriate CFFI definition for the following C entities?
1) typedef enum { a = 0x01, b = 0x02, c = a | b, d = 0x04 | c } test;
2) typedef char XXX[8];
How are the C arrays handled in CFFI?
Any feedback is very much appreciated!
Regards Nik
Hello Nik,
On Mar 2, 2011 6:01 PM, "nitralime" nitralime@googlemail.com wrote:
Hopefully here is the right place to ask CFFI user related questions. What would be an appropriate CFFI definition for the following C entities?
- typedef enum {
a = 0x01, b = 0x02, c = a | b, d = 0x04 | c } test;
It's a bit cumbersome to express that with CFFI. Something like (defcenum (:a 1) ... (:c #.(logior 1 2)) ...).
- typedef char XXX[8];
How are the C arrays handled in CFFI?
There are some array operations such as mem-aref, and you can allocate them using foreign-alloc. defcstruct supports arrays via :count. There's also an :array type that will convert C arrays to Lisp arrays, but it has some limitations and it's undocumented (see src/types.lisp).
HTH,
-- Luís Oliveira http://r42.eu/~luis
Hello Luis,
Thank you for your reply!
(defcenum (:a 1) ... (:c #.(logior 1 2)) ...).
My point regarding "enum" definition was actually if it is possible to write down a definition of enum in CFFI without breaking the "abstraction". In the body of enum as you see there is an interdependency between the fields of enum being defined: the value of c depends on a and b and the value of d depends on c.
but it has some limitations and it's undocumented (see src/types.lisp).
With that CFFI array type "*/typedef char XXX[8]/*" can be translated now as "/*(defctype XXX (:array :char 8))*/"!
What are those limitations?
Regards Nik
On 03/07/2011 11:14 AM, Luís Oliveira wrote:
Hello Nik,
On Mar 2, 2011 6:01 PM, "nitralime"nitralime@googlemail.com wrote:
Hopefully here is the right place to ask CFFI user related questions. What would be an appropriate CFFI definition for the following C entities?
- typedef enum { a = 0x01, b = 0x02, c = a | b, d = 0x04 | c } test;
It's a bit cumbersome to express that with CFFI. Something like (defcenum (:a 1) ... (:c #.(logior 1 2)) ...).
- typedef char XXX[8];
How are the C arrays handled in CFFI?
There are some array operations such as mem-aref, and you can allocate them using foreign-alloc. defcstruct supports arrays via :count. There's also an :array type that will convert C arrays to Lisp arrays, but it has some limitations and it's undocumented (see src/types.lisp).
HTH,
-- Luís Oliveira http://r42.eu/~luis
On Mon, Mar 7, 2011 at 4:55 PM, nitralime nitralime@googlemail.com wrote:
(defcenum (:a 1) ... (:c #.(logior 1 2)) ...).
My point regarding "enum" definition was actually if it is possible to write down a definition of enum in CFFI without breaking the "abstraction". In the body of enum as you see there is an interdependency between the fields of enum being defined: the value of c depends on a and b and the value of d depends on c.
Yeah. I'm afraid you'd need to do something along the lines of:
(defconstant +a+ 1) (defcenunum (:a #.+a+) (:b #.(frob +a+)))
but it has some limitations and it's undocumented (see src/types.lisp).
With that CFFI array type "typedef char XXX[8]" can be translated now as "(defctype XXX (:array :char 8))"!
What are those limitations?
One of the limitations is that it doesn't do proper memory management on the array element type, which is not a problem for primitive types such as :char.
Another limitation is that if you pass a Lisp array via the :array type to a foreign function that modifies it you won't see any changes reflected in the Lisp array, as the Lisp/foreign conversion is currently one-way only.
Cheers,