From: Peter Miehle
Subject: FFI and strings
Date: 
Message-ID: <2mpf48INNmeu@mickey.informatik.uni-kiel.de>
I design a Foreign Function Interface for our Common Lisp to C
Compiler (CLiCC). My problem is, how to design C-strings.

My idea is to implement two functions:
  (c-string <lisp-string>) copies the lisp-string into the
    foreign-space, adds a '\0' and returns a pointer to this memory
    tagged with something like C_STRING.

  (lisp-string <c-string>) copies the c-string into the Lisp-heap and
    makes a SIMPLE-STRING out of it.


I dont like this aproach, because the strings are being copied.
You must copy the Lisp-String into foreign-space, because a called
C-Funktion can callback [call a Lisp-Function] which causes a Garbage
Collection. Since we use a simple stop-and-copy GC, this can produce 
erroneous results.

Any suggestions?

Thanks in advance
Peter Miehle.
-- 
Peter Miehle                       | Wenn ich nicht will,
Institut fuer Informatik, CAU Kiel | dass ich was tu,
Preusserstr. 1-9                   | dann schieb ichs
24105 Kiel (Deutschland)           | einem andren zu.
From: Barry Margolin
Subject: Re: FFI and strings
Date: 
Message-ID: <2msl8aINN406@early-bird.think.com>
In article <············@mickey.informatik.uni-kiel.de> ··@informatik.uni-kiel.de (Peter Miehle) writes:
>My idea is to implement two functions:
>  (c-string <lisp-string>) copies the lisp-string into the
>    foreign-space, adds a '\0' and returns a pointer to this memory
>    tagged with something like C_STRING.
>
>  (lisp-string <c-string>) copies the c-string into the Lisp-heap and
>    makes a SIMPLE-STRING out of it.
>
>I dont like this aproach, because the strings are being copied.

I think this is how most other Lisps do this.

As you pointed out, garbage collection makes it necessary for C-STRING to
copy into the foreign heap.  However, you might be able to avoid copying in
LISP-STRING.  Instead of returning a SIMPLE-STRING, return a type of string
that contains a pointer to the storage in foriegn space.  Programmers
should be warned that this string shares space with the foreign data, so
that future calls back into C may modify the string contents; if they want
to avoid this they can use COPY-SEQ to get a normal Lisp string.

If you take this approach, you can probably do without the LISP-STRING
function altogether.  Just make C-STRING a subtype of STRING and make sure
all your string, vector, and array functions know how to handle it as if it
were a normal string.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar