gethostname example: trying to avoid superfluous alloc+free
"Hoehle, Joerg-Cyril" <[email protected]> Thu, 21 Mar 2002 12:48:49 +0100
| Newsgroups | gmane.lisp.uffi.general |
|---|---|
| Message-ID | <[email protected]> |
Hi, > Your technique of using a cstring should work fine. The > example actually shows the use of allocate-foreign-string / > convert-from-foreign-string / free-foreign-object and passing a (* > :unsigned-char) for the name. Actually, I'd like to point out that using alloc+free foreign strings is far away from the most efficient implementation on CMUCL (and CLISP which is not supported). I believe there should be easy ways in UFFI to call gethostname with stack allocated buffers were possible (CMUCL, maybe LispWorks too, CLISP). That's how I got the idea of my (untested) somehow odd code below. Esp. I think examples should show code that is amenable to efficient translation on the platforms where this is possible, whereas using alloc/free is hard (to much compiler type program or data flow analysis involved instead of simple macro transformations). The purpose is that there would be a chance that a portable style of writing emerges among users which supports (or at least doesn't stand in the way of) the optimization goal. Examples educate people. Copy&paste rules even in Lisp. > > (let ((unused-storage (make-string 64))) ; 64 on Linux, 256 > on Solaris... > > (with-cstring (name unused-storage) > > (if (zerop (call-"gethostname" name 64)) > > (convert-from-cstring name)))) Regards, Jorg Hohle