| <head> |
| <title>needstack(3) - Plan 9 from User Space</title> |
| <meta content="text/html; charset=utf-8" http-equiv=Content-Type> |
| </head> |
| <body bgcolor=#ffffff> |
| <table border=0 cellpadding=0 cellspacing=0 width=100%> |
| <tr height=10><td> |
| <tr><td width=20><td> |
| <tr><td width=20><td><b>NEEDSTACK(3)</b><td align=right><b>NEEDSTACK(3)</b> |
| <tr><td width=20><td colspan=2> |
| <br> |
| <p><font size=+1><b>NAME </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| needstack – check for execution stack overflow<br> |
| |
| </table> |
| <p><font size=+1><b>SYNOPSIS </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>#include <u.h> |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>#include <libc.h> |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| </font></tt> |
| <tt><font size=+1>int needstack(int n)<br> |
| </font></tt> |
| </table> |
| <p><font size=+1><b>DESCRIPTION </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| Stack overflow in the thread library leads to bugs that are difficult |
| to diagnose. The Plan 9 libraries are careful about not allocating |
| large structures on the stack, so typically four or eight kilobytes |
| is plenty of stack for a thread. Other libraries are not always |
| as careful. Calling <i>needstack</i> indicates to the thread library |
| that an external routine is about to be called that will require |
| <i>n</i> bytes of stack space. If there is not enough space left on the |
| stack, the thread library prints an error and terminates the program. |
| The call <tt><font size=+1>needstack(0)</font></tt> can be used to check whether the stack is |
| currently overflowed. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Needstack</i> is defined in <tt><font size=+1>libc.h</font></tt> so that library functions used |
| in threaded and non-threaded contexts can call it. The implementation |
| of <i>needstack</i> in <tt><font size=+1>lib9</font></tt> is a no-op. |
| <table border=0 cellpadding=0 cellspacing=0><tr height=5><td></table> |
| |
| <i>Needstack</i> should be thought of as a comment checked at run time, |
| like <a href="../man3/assert.html"><i>assert</i>(3)</a>.<br> |
| |
| </table> |
| <p><font size=+1><b>EXAMPLE </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| The X Window library implementation of <i>XLookupString</i> allocates |
| some very large buffers on the stack, so <tt><font size=+1>/usr/local/plan9/src/libdraw/x11−itrans.c</font></tt> |
| calls <tt><font size=+1>needstack(20*1024)</font></tt> before making calls to <i>XLookupString</i>. |
| If a thread (in this case, the keyboard-reading thread used inside |
| the <a href="../man3/draw.html"><i>draw</i>(3)</a> |
| library) does not allocate a large enough stack, the problem is |
| diagnosed immediately rather than left to corrupt memory.<br> |
| |
| </table> |
| <p><font size=+1><b>SOURCE </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <tt><font size=+1>/usr/local/plan9/src/lib9/needstack.c<br> |
| /usr/local/plan9/src/libthread<br> |
| </font></tt> |
| </table> |
| <p><font size=+1><b>SEE ALSO </b></font><br> |
| |
| <table border=0 cellpadding=0 cellspacing=0><tr height=2><td><tr><td width=20><td> |
| |
| <a href="../man3/thread.html"><i>thread</i>(3)</a><br> |
| |
| </table> |
| |
| <td width=20> |
| <tr height=20><td> |
| </table> |
| <!-- TRAILER --> |
| <table border=0 cellpadding=0 cellspacing=0 width=100%> |
| <tr height=15><td width=10><td><td width=10> |
| <tr><td><td> |
| <center> |
| <a href="../../"><img src="../../dist/spaceglenda100.png" alt="Space Glenda" border=1></a> |
| </center> |
| </table> |
| <!-- TRAILER --> |
| </body></html> |