Subject: [clisp-list] FFI and C-POINTERs

From: Pascal J.Bourguignon <[email-masked]>
Date: Sun Aug 1 11:26:03 2004

I'm running in loops.

How can one get a "foreign variable" that seem so needed to FFI:CAST,
FFI:DEREF, etc, from a C-POINTER?

(FFI:DEF-CALL-OUT MALLOC
                  (:NAME "malloc")
                  (:ARGUMENTS (SIZE FFI:UINT32 :IN))
                  (:RETURN-TYPE FFI:C-POINTER)
                  (:LANGUAGE :STDC))
(setq m (malloc 4))

- How do I set a   FFI:UINT32 in the memory pointed to by M?
- How do I get the FFI:UINT32 pointed to by M?
- Same questions for the (FFI:C-ARRAY (2) FFI:UINT16)?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein

---

From: Sam Steingold <[email-masked]>
Date: Sun Aug 1 12:17:01 2004

> * Pascal J.Bourguignon <[email-masked]> [2004-08-01 20:25:00 +0200]:
>

<http://clisp.cons.org/impnotes/dffi.html#foreign-stack>
(FFI:WITH-FOREIGN-OBJECT (variable c-type  [initarg]) body)
(FFI:WITH-C-VAR (variable c-type [initarg]) body)

>                   (:RETURN-TYPE FFI:C-POINTER)

<http://clisp.cons.org/impnotes/dffi.html#foreign-heap>
(FFI:ALLOCATE-SHALLOW c-type  &KEY :COUNT :READ-ONLY)
(FFI:ALLOCATE-DEEP c-type contents &KEY :COUNT :READ-ONLY)
(FFI:FOREIGN-FREE foreign-entity &KEY :FULL)
(FFI:FOREIGN-ALLOCATE c-type-internal &KEY :INITIAL-CONTENTS :COUNT :READ-ONLY)

> - Same questions for the (FFI:C-ARRAY (2) FFI:UINT16)?

Joerg is the authority here...

-- 
Sam Steingold (http://www.podval.org/~sds) running w2k
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.honestreporting.com>
A man paints with his brains and not with his hands.

---

From: Pascal J.Bourguignon <[email-masked]>
Date: Sun Aug 1 18:57:01 2004

> (FFI:FOREIGN-ALLOCATE c-type-internal &KEY :INITIAL-CONTENTS :COUNT :READ-ONLY)

But, there is a big problem with the stuff returned by
ALLOCATE-SHALLOW:

CL-USER> (setf a (ffi:allocate-shallow 'ffi:uchar :count 4))
#<FOREIGN-VARIABLE FFI:FOREIGN-ALLOCATE #x0820FEC8>
CL-USER> (ffi:cast (ffi:foreign-value a) 'ffi:uint32)
0
CL-USER> (setf (ffi:cast (ffi:foreign-value a) 'ffi:uint32) #16r12345678)
305419896

So far so good, but then:

#<FOREIGN-VARIABLE FFI:FOREIGN-ALLOCATE #x0820FEC8> cannot be converted to the foreign type FFI:UINT16
   [Condition of type SIMPLE-ERROR]

Restarts:
  0: [ABORT] Abort handling SLIME request.

Backtrace:
  0: frame binding variables (~ = dynamically):
  | ~ SYSTEM::*FASOUTPUT-STREAM* <--> NIL
  1: EVAL frame for form (FFI::%CAST A (FFI:PARSE-C-TYPE 'FFI:UINT16))
  2: EVAL frame for form (FFI:FOREIGN-VALUE (FFI::%CAST A (FFI:PARSE-C-TYPE 'FFI:UINT16)))
  3: EVAL frame for form (FFI:CAST (FFI:FOREIGN-VALUE A) 'FFI:UINT16)
  4: EVAL frame for form (FORMAT T "~16R~%" (FFI:CAST (FFI:FOREIGN-VALUE A) 'FFI:UINT16))
  5: EVAL frame for form (SWANK:LISTENER-EVAL "(format t \"~16R~%\" (ffi:cast (ffi:foreign-value a) 'ffi:uint16))
")
  6: EVAL frame for form (SWANK:START-SERVER "/tmp/slime.30354")

That is, one cannot really type cast shallow-allocated object as in C.

While it still is possible to try to cast to an array covering the
whole "object", this won't be possible when then size of the wanted
type cast and the size of the "object" are not congurent!