[cffi-devel] Re: WITH-FOREIGN-STRING allocates on heap?

Martin Simmons martin at lispworks.com
Fri Jun 20 15:22:24 UTC 2008


>>>>> On Fri, 20 Jun 2008 14:28:02 +0100, Luis Oliveira said:
> 
> "Hans Hübner" <hans at huebner.org> writes:
> 
> > On Fri, Jun 20, 2008 at 9:13 AM, Luís Oliveira <luismbo at gmail.com> wrote:
> >> I believe we were worried about big strings and the possibility of
> >> overflowing the stack. I suppose we could add an argument to
> >> WITH-FOREIGN-STRING and :STRING to force stack allocation.
> >
> > Is this really a realistic issue with modern systems?  Some sampling:
> [...]
> > So, at least ~10 MB for the 32 bit FreeBSD machine, at least ~1 GB for
> > the amd64 Linux box.
> 
> Many Lisps seem to operate with much smaller stacks.  Here's the test I
> used:
> 
>   (defconstant +size+ (* 50 (expt 2 10)))        ; 50 KB
> 
>   (defun test ()
>     (labels ((ek (n)
>                (cffi:with-foreign-pointer (p #.+size+)
>                  (loop for i below +size+
>                        do (setf (cffi:mem-ref p :char i) 0))
>                  (format t "[~A] ~A: total allocated ~:D~%"
>                          n p (* +size+ n))
>                  (ek (1+ n)))))
>       (ek 1)))
> 
>   (compile 'test)
> 
> 
> SBCL: ~2 MB followed by "the party is over."
> CCL: ~2 MB followed by a segfault.
> CLISP: 8 MB followed by a graceful stack overflow.
> Allegro: ~4 MB followed by a graceful stack overflow.
> (used 40 KB chunks otherwise Allegro refused to do stack allocation)
> 
> This is on linux/amd64 (but I think my copy of Allegro is a 32-bit
> version).  Hopefully my test is not (too) bogus.

There is an interaction with threads that Hans's C test code did not trigger.
You can't expect to get 1 GB per stack for every thread!

-- 
Martin Simmons
LispWorks Ltd
http://www.lispworks.com/



More information about the cffi-devel mailing list