From: Nicolas Rouquette
Subject: Re: Kcl C function interface problem
Date: 
Message-ID: <16576@oberon.USC.EDU>
The following function shows a lot of information about KCL strings.
The "self" slot is where the string is actually found.  The function
below will work fine (with KCL 3 June 1987 on a Sun 3, SunOs 3.2).
For more details, look at the cmpinclude.h function which defines
KCL's data structures.

However, can a A/KCL guru tell me how to actually *safely* modify that string?

Sun Common Lisp (Lucid) 3.0 on Sun 3,4 provides more information about
this.  To prevent the garbage collector from moving a string data
suceptible of modifications by a foreign (e.g., C) function, such a
string must be allocated in foreign space (static area would do for
reading only).  I haven't seen constructs in KCL for taking similar
caution. 

(eval-when (compile)
	   (proclaim '(optimize (safety 2) (space 3)))
	   (set-macro-character
	    #\%
	    #'(lambda (stream char) (values (read-line stream)))))

(defCfun "object ShowString (str) object str;" 0
% printf("t,m        = %d,%d\n",str->st.t,str->st.m);
% printf("hasfillp   = %d\n",str->st.st_hasfillp);
% printf("adjustable = %d\n",str->st.st_adjustable);
% printf("dim, fillp = %d,%d\n",str->st.st_dim,str->st.st_fillp);
% printf("self       = %x,'%s'\n",(int)str->st.st_self,str->st.st_self);
% printf("displaced  = %x\n",(int)str->st.st_displaced);
% return(str->st.st_displaced);
)
(defentry show-string (object) (object "ShowString"))

save the above as "string.lsp" and try:
> (compile-file "string")
> (load "string")
> (setq x "1234")
> (show-string x) will produce:
t,m        = 13,0
hasfillp   = 0
adjustable = 0
dim, fillp = 5,5
self       = 1e24c4,'12345'
displaced  = e7c14
NIL

if you try:
> (setq z (make-array '(10) :element-type 'string-char))
and 
> (setq w (make-array '(5) :element-type 'string-char
              :displaced-to z
              :displaced-index-offset 3))
and 
> (setf (aref w 3) #\4)
> (show-string w)
t,m        = 13,0
hasfillp   = 0
adjustable = 0
dim, fillp = 5,5
self       = 1e24d3,'4      '
displaced  = 1693cc
("   4      ")

> (eq (car *) z) gives: 
T

Nick.

DAI Group.
Compt. Sci. Dept.
Univ. of So. Calif.
Los Angeles, CA 90089-0782