blob: cf13809d0387f63ebb30a5ffdc38a665cae6472f [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH NEEDSTACK 3
2.SH NAME
3needstack \- check for execution stack overflow
4.SH SYNOPSIS
5.B
6#include <u.h>
7.PP
8.B
9#include <libc.h>
10.PP
11.B
12int needstack(int n)
13.SH DESCRIPTION
14Stack overflow in the thread library leads to bugs that are
15difficult to diagnose.
16The Plan 9 libraries are careful about not allocating
17large structures on the stack, so typically four or eight kilobytes is plenty of stack
18for a thread.
19Other libraries are not always as careful.
20Calling
21.I needstack
22indicates to the thread library that an external routine is about
23to be called that will require
24.I n
25bytes of stack space.
26If there is not enough space left on the stack,
27the thread library prints an error and terminates
28the program.
29The call
30.B needstack(0)
31can be used to check whether the stack is
32currently overflowed.
33.PP
34.I Needstack
35is defined in
36.B libc.h
37so that library functions used in threaded and non-threaded contexts
38can call it.
39The implementation of
40.I needstack
41in
42.B lib9
43is a no-op.
44.PP
45.I Needstack
46should be thought of as a comment checked at run time,
47like
48.IR assert (3).
49.SH EXAMPLE
50The X Window library implementation of
51.I XLookupString
52allocates some very large buffers on the stack, so
rscc3674de2005-01-11 17:37:33 +000053.B \*9/src/libdraw/x11-itrans.c
rsc058b0112005-01-03 06:40:20 +000054calls
55.B needstack(20*1024)
56before making calls to
57.IR XLookupString .
58If a thread (in this case, the keyboard-reading thread used
59inside the
60.IR draw (3)
61library)
62does not allocate a large enough stack, the problem is diagnosed
63immediately rather than left to corrupt memory.
64.SH SOURCE
rscc3674de2005-01-11 17:37:33 +000065.B \*9/src/lib9/needstack.c
rsc058b0112005-01-03 06:40:20 +000066.br
rscc3674de2005-01-11 17:37:33 +000067.B \*9/src/libthread
rsc058b0112005-01-03 06:40:20 +000068.SH SEE ALSO
69.IR thread (3)