[Ecls-list] Problem with foreign data pointers from a C library on 64 bits

Juan Jose Garcia-Ripoll juanjose.garciaripoll at googlemail.com
Fri Sep 2 08:24:18 UTC 2011


On Fri, Sep 2, 2011 at 1:19 AM, Philippe Hanrigou <
philippe.hanrigou at gmail.com> wrote:

> Hi,
>
> I am having some problems using ffi to relay pointers between
> ECL and a custom C library. It looks as if the pointers end up
> being truncated to 32 bits for a reason that completely escapes
> me...
>

The reason is simple: in the lisp file you call C functions without having
them declared beforehand. If you do not provide a declaration of
"use_pointer" then C assumes that all of the arguments are integers (32-bits
in this compiler) and similar for the output.

I would advise you to use some other FFI, such as UFFI or CFFI, until you
are confident enough to resort to low-level C-type programming, which is
what c-inline does.

A quick hack that solves your problem:

(ffi:clines "
extern const char *use_pointer(void *);
extern void *create_pointer();
")

(defpackage :foo
   (:use :common-lisp :ffi))
(in-package :foo)

(proclaim '(OPTIMIZE (SAFETY 3) (DEBUG 3) (SPEED 0)))

(ffi:load-foreign-library "foo" :system-library t)

(defun new-p ()
 (c-inline () () :pointer-void "create_pointer()" :side-effects t :one-liner
t))

(defun use-p (p)
 (c-inline (p) (:pointer-void) :cstring "use_pointer(#0)" :side-effects t
:one-liner t))

(defun use2-p (p)
 (c-inline (p) (:pointer-void) :cstring "use_pointer(#0)" :side-effects t
:one-liner t))

(use-p (new-p))


-- 
Instituto de Física Fundamental, CSIC
c/ Serrano, 113b, Madrid 28006 (Spain)
http://juanjose.garciaripoll.googlepages.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/ecl-devel/attachments/20110902/a9971fbd/attachment.html>


More information about the ecl-devel mailing list