[Openmcl-devel] FFI help...get address to element in structure

R. Matthew Emerson rme at clozure.com
Fri Sep 18 22:05:57 PDT 2009


On Sep 18, 2009, at 6:24 PM, Michael Kohout wrote:

> Next week I'm doing a presentation to my local lisp user group on FFI 
> (CFFI and CCL's FFI).  My example is a simple network client and  
> server.
>
> Anyways, the code I'm writing requires me to get the address to a  
> field in a struct( &sa.sa_mask, where sa is a sigaction structure).
>
> Does CCL's ffi system allow me to get this without munging around  
> with the internal layout of the structure?  If so, how?

I don't know if this qualifies as munging around, but maybe this  
example will help.

;; struct sigaction sa;
;; (sort of;  in ccl, sa is a macptr referencing a stack-allocated  
structure)
(rlet ((sa (:struct sigaction)))
   ;; sa.sa_mask = 999;
   (setf (pref sa :sigaction.sa_mask) 999)
   ;; char *p = &sa.sa_mask
   (with-macptrs ((p (%inc-ptr sa (ccl::get-field- 
offset :sigaction.sa_mask))))
     ;; return *(long *)p;
     (%get-long p)))

If you don't really need a pointer to the sa_mask field and just want  
to know what its contents are (or to set them), you can use (pref  
sa :sigaction.sa_mask).




More information about the Openmcl-devel mailing list