Re: gethostname example: trying to avoid superfluous alloc+free

John DeSoi <[email protected]> Thu, 21 Mar 2002 08:12:27 -0500
Newsgroups gmane.lisp.uffi.general
Message-ID <p04330101b8bf890ec747@[192.168.1.6]>
>
>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))))
>


I would like to see this approach also, and not just with strings. 
For simple types, MCL does not even provide an alloc method (I have a 
work-around using non-exported MCL functions). The approach (which 
the docs say is highly optimized compared to alloc+free) look 
something like:

(with-allocation ((my-long :long) (my-string :string 64)
  ...forms...

Lispworks also has something like this (with-dynamic-foreign-objects).

Best,

John DeSoi, Ph.D.