next up previous contents
Next: 9 Interprocess Communication under Up: 8 Alien Objects Previous: 8.7.5 Accessing Lisp Arrays

Step-by-Step Alien Example

This section presents a complete example of an interface to a somewhat complicated C function. This example should give a fairly good idea of how to get the effect you want for almost any kind of C function. Suppose you have the following C function which you want to be able to call from Lisp in the file `test.c':

                
struct c_struct
{
  int x;
  char *s;
};
 
struct c_struct *c_function (i, s, r, a)
    int i;
    char *s;
    struct c_struct *r;
    int a[10];
{
  int j;
  struct c_struct *r2;
 
  printf("i = %d\n", i);
  printf("s = %s\n", s);
  printf("r->x = %d\n", r->x);
  printf("r->s = %s\n", r->s);
  for (j = 0; j < 10; j++) printf("a[%d] = %d.\n", j, a[j]);
  r2 = (struct c_struct *) malloc (sizeof(struct c_struct));
  r2->x = i + 5;
  r2->s = "A C string";
  return(r2);
};
It is possible to call this function from Lisp using the file `test.lisp' whose contents is:
;;; -*- Package: test-c-call -*-
(in-package "TEST-C-CALL")
(use-package "ALIEN")
(use-package "C-CALL")

;;; Define the record c-struct in Lisp. (def-alien-type nil (struct c-struct (x int) (s c-string)))

;;; Define the Lisp function interface to the C routine. It returns a ;;; pointer to a record of type c-struct. It accepts four parameters: ;;; i, an int; s, a pointer to a string; r, a pointer to a c-struct ;;; record; and a, a pointer to the array of 10 ints. ;;; ;;; The INLINE declaration eliminates some efficiency notes about heap ;;; allocation of Alien values. (declaim (inline c-function)) (def-alien-routine c-function (* (struct c-struct)) (i int) (s c-string) (r (* (struct c-struct))) (a (array int 10)))

;;; A function which sets up the parameters to the C function and ;;; actually calls it. (defun call-cfun () (with-alien ((ar (array int 10)) (c-struct (struct c-struct))) (dotimes (i 10) ; Fill array. (setf (deref ar i) i)) (setf (slot c-struct 'x) 20) (setf (slot c-struct 's) "A Lisp String")

(with-alien ((res (* (struct c-struct)) (c-function 5 "Another Lisp String" (addr c-struct) ar))) (format t "Returned from C function. (multiple-value-prog1 (values (slot res 'x) (slot res 's)) ;; ;; Deallocate result ıafter we are done using it. (free-alien res)))))

To execute the above example, it is necessary to compile the C routine as follows:
cc -c test.c
In order to enable incremental loading with some linkers, you may need to say:
cc -G 0 -c test.c
Once the C code has been compiled, you can start up Lisp and load it in:
;;; Lisp should start up with its normal prompt.

;;; Compile the Lisp file. This step can be done separately. You don't have ;;; to recompile every time. * (compile-file "test.lisp")

;;; Load the foreign object file to define the necessary symbols. This must ;;; be done before loading any code that refers to these symbols. next block ;;; of comments are actually the output of LOAD-FOREIGN. Different linkers ;;; will give different warnings, but some warning about redefining the code ;;; size is typical. * (load-foreign "test.o")

;;; Running library:load-foreign.csh... ;;; Loading object file... ;;; Parsing symbol table... Warning: "_gp" moved from #x00C082C0 to #x00C08460.

Warning: "end" moved from #x00C00340 to #x00C004E0.

;;; o.k. now load the compiled Lisp object file. * (load "test")

;;; Now we can call the routine that sets up the parameters and calls the C ;;; function. * (test-c-call::call-cfun)

;;; The C routine prints the following information to standard output. i = 5 s = Another Lisp string r->x = 20 r->s = A Lisp string a[0] = 0. a[1] = 1. a[2] = 2. a[3] = 3. a[4] = 4. a[5] = 5. a[6] = 6. a[7] = 7. a[8] = 8. a[9] = 9. ;;; Lisp prints out the following information. Returned from C function. ;;; Return values from the call to test-c-call::call-cfun. 10 "A C string" *

If any of the foreign functions do output, they should not be called from within Hemlock. Depending on the situation, various strange behavior occurs. Under X, the output goes to the window in which Lisp was started; on a terminal, the output will overwrite the Hemlock screen image; in a Hemlock slave, standard output is `/dev/null' by default, so any output is discarded.


next up previous contents
Next: 9 Interprocess Communication under Up: 8 Alien Objects Previous: 8.7.5 Accessing Lisp Arrays

Raymond Toy
Mon Jul 14 09:11:27 EDT 1997