[comp.lang.c] Lisp-C interface

edw@ius2.cs.cmu.edu.UUCP (03/13/87)

      
   Question to thoses of you that are familiar with Sun's Common Lisp and
its foreign function call feature.

   I am designing an interface in lisp to a library written in C.  There is
no problem calling the C functions, the problem is that this library
extensively uses malloc and free.  As I have been told by other people,
when lisp goes to garbage collect, it also collects the memory that
was allocated by malloc (all memory up to sbrk).  

    One, is this true?
    Second if so are there any plans to fix it so malloc will work right
    or am I stuck re-implementing malloc.


						Eddie Wyatt

mccaugh@uiucdcsm.cs.uiuc.edu (03/18/87)

 I hesitate to submit this response, since I am unfamiliar with common-lisp
 and really know only Franz and Xlisp, but what it seems you may need is an
 un-lisp-like interface between lisp and C: I'm sure you don't wish to re-
 write the C library to "go after" the mallocs and frees if they are that
 extensively employed, and you may not have access to the garbage-collector
 source. 

 Without further info, it is difficult to comment and I wouldn't have butted
 in here but for the fact your note is almost a week old with no comments to
 it, so at least the following is better than nothing:

 If Common Lisp is anything like Franz or Xlisp -- and you wish to hide some
 data-structures (the C-malloc'd ones, presumably, since you indicate they
 get free'd by C) from the garbage-collector, don't let those data-structures
 get to the symbol-table where garbage-collection initiates its sweep. This is
 just one peon's solution: others involve twiddling with the mark-bits so that
 even if garbage-collection finds a node it doesn't try to recover it with the
 right bit set, but the simplest way is to prevent it from accessing your data-
 structures in the first place. 
 Of course, this crudest of solutions carries with it the onus that if the data-
 structure doesn't reside in the symbol-table, Lisp never finds it to begin with
 so other steps need to be taken to ensure Lisp finds it, and that is indeed a
 problem. If your data-structures aren't parameters, you can't install them on
 the A-list.
 However, constants are symbols to Lisp and so enjoy places on the symbol-table.
 The trick, then, is to find a constant and attach your data-structures to its
 property-list. The fact that it's constant should prevent garbage-collection
 from ever trying to recover it, and thus the structures hanging off its prop-
 erty-list should be safe -- of course, all this presumes ease of their access.

 -- Scott McCaughrin (mccaugh @ uiucmsl)