steph...@mpeforth.com (Stephen Pelc) writes:
>For example, can you guarantee that any of the stack area below the
>stack pointer (assuming grow-down stacks) is safe to use? Some O/S
>permit this, some don't.
>
>How much stack space does a called C function use? You don't know
>for the most part. The C stack then has to be below the data stack.
>
>Where are the stacks for callbacks? Some O/S use the original caller's
>stack. With nested callbacks we have seen applications in the wild
>need several megabytes of stack space.
Yes, these issues have to be taken into consideration. They don't go
away with reverse parameter order, so they are not an argument for
or against it.
Concerning the individual issues: Neither C nor Forth limits stack
consumption. The OS or application decides at the start how much
address space each stack has; and if it is good, it puts a guard page
in to cause an exception on stack overflow. On Linux you can see the
memory map of process $p with
cat /proc/$p/maps
For a Gforth process, I see:
00400000-0041e000 r-xp 00000000 00:17 233563 .../gforth
0061e000-0061f000 r--p 0001e000 00:17 233563 .../gforth
0061f000-00628000 rw-p 0001f000 00:17 233563 .../gforth
00628000-00669000 rw-p 00000000 00:00 0
01f9e000-02004000 rw-p 00000000 00:00 0 [heap]
7fdf866e1000-7fdf86753000 rwxp 00000000 00:17 233562 .../
gforth.fi
7fdf86753000-7fdf86ee1000 rwxp 00000000 00:00 0
[mappings for various shared libraries]
7fdf87f50000-7fdf87fd0000 rwxp 00000000 00:00 0
7fdf87fd0000-7fdf87fd6000 rw-p 00000000 00:00 0
7fdf87fd6000-7fdf87fd7000 rwxp 00000000 00:00 0
7fdf87fd7000-7fdf87fd8000 ---p 00000000 00:00 0
7fdf87fd8000-7fdf87fdc000 rwxp 00000000 00:00 0
7fdf87fdc000-7fdf87fdd000 ---p 00000000 00:00 0
7fdf87fdd000-7fdf87fe1000 rwxp 00000000 00:00 0
7fdf87fe1000-7fdf87fe2000 ---p 00000000 00:00 0
7fdf87fe2000-7fdf87fe6000 rwxp 00000000 00:00 0
7fdf87fe6000-7fdf87fe7000 ---p 00000000 00:00 0
7fdf87fe7000-7fdf87feb000 rwxp 00000000 00:00 0
7fdf87feb000-7fdf87fec000 ---p 00000000 00:00 0
7fdf87fec000-7fdf87ff0000 rwxp 00000000 00:00 0
7fdf87ff0000-7fdf87ff2000 rw-p 00000000 00:00 0
7fdf87ff2000-7fdf87ff3000 r--p 00022000 00:10 923295 .../
ld-2.15.so
7fdf87ff3000-7fdf87ff5000 rw-p 00023000 00:10 923295 .../
ld-2.15.so
7ffff0e0a000-7ffff0e2b000 rw-p 00000000 00:00 0 [stack]
7ffff0fff000-7ffff1000000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0[vsyscall]
As you can see, the C stack [stack] would first run into a mapping of
the linker; it can consume about 129GB of space before that happens.
You also see the dictionary at 7fdf866e1000-7fdf86ee1000 in two
mappings: one for the image
gforth.fi, and one for the rest. The
Forth stacks (plus user area) are at 7fdf87fd6000-7fdf87ff0000, with a
guard page between each stack.
The mappings for a vfxlin process looks as follows:
08048000-080c0000 rwxp 00000000 00:10 316417 /usr/bin/vfxlin
080c0000-08848000 rwxp 00000000 00:00 0
098ba000-099db000 rwxp 00000000 00:00 0 [heap]
[Mappings of shared libraries]
f77a0000-f77a2000 rwxp 00000000 00:00 0
f77a2000-f77a3000 r-xp 00000000 00:00 0 [vdso]
f77a3000-f77c3000 r-xp 00000000 00:10 923644 .../
ld-2.15.so
f77c3000-f77c4000 r-xp 0001f000 00:10 923644 .../
ld-2.15.so
f77c4000-f77c5000 rwxp 00020000 00:10 923644 .../
ld-2.15.so
ff939000-ff95a000 rwxp 00000000 00:00 0 [stack]
Again, the C stack first runs into a mapping of the linker; this time
there is about 129MB of space before that happens.
A callback is just an ordinary indirect call, so normally it uses the
same stack as the rest. Where does it happen differently? And yes, C
programs may consume lots of stack space, and the OS typically cater
to that.