I'm rather digging the idea of using Lisp/SBCL as a high-level glue-like
language (ala Perl), and have been playing with aliens lately. There
are code examples for returning allocated structures, but I'm having a
bit of difficulty returning a NUL-terminated block of allocated memory
to be used as a c-string. In a .so I have something similar to:
char *
mkstring(const char *s) {
char *buf = malloc(5 + strlen(s) + 2);
sprintf(buf,"got '%s'",s);
return buf;
}
On the Lisp side, I want to get the value in a Lisp string and do
something with it, but SBCL is giving me the usual warning about not
being able to optimize away %SAP-ALIEN (let alone other compiler
errors). I want to do something like:
(let (s)
(with-alien ((p (* char)))
(setf p (alien-funcall
(extern-alien "mkstring" (function (* char) c-string))
"foo"))
(setf s p) ;; or other operations
(free-alien p))
s)
Anyone able to tell me what the correct call to `mkstring' would be from
SBCL, particularly with respect to memory management and preventing
leakage?
thx --
--
Michael J. Barillier /// http://www.blackwolfinfosys.net/~blackwolf/
_O_| Greenspun's Tenth Rule of Programming: "Any sufficiently
__O| complicated C or Fortran program contains an ad-hoc, informally-
OOO| specified bug-ridden slow implementation of half of Common Lisp."
|