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

Hans Hübner hans at huebner.org
Fri Jun 20 07:26:34 UTC 2008


On Fri, Jun 20, 2008 at 9:13 AM, Luís Oliveira <luismbo at gmail.com> wrote:
> On Fri, Jun 20, 2008 at 5:21 AM, Hans Hübner <hans at huebner.org> wrote:
>> is there a good reason why WITH-FOREIGN-STRING allocates the buffer on
>> the heap instead of using WITH-FOREIGN-OBJECT, which will allocate on
>> the stack on platforms that support it?
>
> 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:

#include <stdlib.h>

main(int argc, char *argv[])
{
        int size = 1;
        while (1) {
                char* s = alloca(size);
                memset(s, 0, size);
                printf("still alive - %d\n", size);
                size *= 10;
        }
}

netzhansa 16_> uname -a
FreeBSD netzhansa.com 6.2-RELEASE FreeBSD 6.2-RELEASE #0: Fri Jan 12
10:40:27 UTC 2007
root at dessler.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
netzhansa 17_> ./alloca
still alive - 1
still alive - 10
still alive - 100
still alive - 1000
still alive - 10000
still alive - 100000
still alive - 1000000
still alive - 10000000
Segmentation fault (core dumped)

deng-hhueb 772_$ uname -a
Linux deng-hhueb 2.6.22.2-42.asl.2.intel.fc3 #1 SMP Thu Sep 20
14:27:32 EDT 2007 x86_64 x86_64 x86_64 GNU/Linux
deng-hhueb 773_$ ./alloca
still alive - 1
still alive - 10
still alive - 100
still alive - 1000
still alive - 10000
still alive - 100000
still alive - 1000000
still alive - 10000000
still alive - 100000000
still alive - 1000000000
still alive - 1410065408
still alive - 1215752192
zsh: 14633 segmentation fault  ./alloca

So, at least ~10 MB for the 32 bit FreeBSD machine, at least ~1 GB for
the amd64 Linux box.

Given this, I'd think that making stack allocation be the default and
heap allocation an option would be beneficial.  If you really don't
like it, making the default a compile-time option would be better than
mandating an allocation policy, I think.

Let me know which way you like it.

Thanks!
Hans



More information about the cffi-devel mailing list