[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: foreign function interface

Date: Fri, 6 Nov 87 11:27:34 MST
From: sandra < (Sandra J Loosemore)sandra%arpa.orion@edu.utah.cs>

> I'm looking at how to implement a Lisp interface to a graphics library in
> KCL.  Two questions:

> (1) Some of the graphics routines take arrays and structures as arguments.
>     How do I specify conversion of Lisp-flavored data structures to the
>     appropriate C-flavored ones?

If the C data is type int, char, float, or double, KCL will generate
conversion code automatically when it compiles a DEFENTRY.  For C
structures and arrays, though, you have to write your own conversion
code (or write macros, etc. to write it for you).  The DEFENTRY then
uses the type OBJECT and you have to write an interface routine that
converts from Lisp data to C and back.

Suppose you want a Lisp routine make-window that calls a C routine
make_window and that make_window has an argument and result of some
type that can't be handled automatically.  You'd need a DEFENTRY to
define a Lisp procedure that called the interface procedure:

(defentry make-window (object) (object make_window_interface))

make_window_interface would convert the argument, call the real
make_window, convert the result, and return.  (It would be defined
using CLINES or DEFCFUN.)  One problem with this approach is that you
end up calling two extra procedures (the one created by the DEFENTRY
and the interface procedure) every time you call something in the
graphics library, but to do better you'd have to eliminate the

The data conversion lisp <--> C may be relatively trivial, though, if
the structures are going to be directly manipulated only by C code.  You
may be able to turn them into fixnum vectors, for example.

> (2) How do I "load" the C-flavored library into KCL?

On BSD systems, you can use SYS:FASLINK.  (The documentation
string gives details.)

One thing to watch out for (at least in the June 3, 1987 KCL) is
that while KCL defines its own versions of most routines in the
malloc family it leaves out valloc and memalign.  If you load C
code that uses valloc or memalign, the ones from the C library
will be loaded and things may get confused.

I can post KCL versions of valloc and memalign if anyone is

> Any help would be appreciated....

Well, I haven't been much help, but ...

Jeff Dalton,                      JANET: J.Dalton@uk.ac.ed             
AI Applications Institute,        ARPA:  J.Dalton%uk.ac.ed@nss.cs.ucl.ac.uk
Edinburgh University.             UUCP:  ...!ukc!ed.ac.uk!J.Dalton