1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<div class="section-level-extent" id="Nonlocal-Gotos"> <div class="nav-panel"> <p> Next: <a href="constructing-calls" accesskey="n" rel="next">Constructing Function Calls</a>, Previous: <a href="nested-functions" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Nonlocal-Gotos-1"><span>6.5 Nonlocal Gotos<a class="copiable-link" href="#Nonlocal-Gotos-1"> ¶</a></span></h1> <p>GCC provides the built-in functions <code class="code">__builtin_setjmp</code> and <code class="code">__builtin_longjmp</code> which are similar to, but not interchangeable with, the C library functions <code class="code">setjmp</code> and <code class="code">longjmp</code>. The built-in versions are used internally by GCC’s libraries to implement exception handling on some targets. You should use the standard C library functions declared in <code class="code"><setjmp.h></code> in user code instead of the builtins. </p> <p>The built-in versions of these functions use GCC’s normal mechanisms to save and restore registers using the stack on function entry and exit. The jump buffer argument <var class="var">buf</var> holds only the information needed to restore the stack frame, rather than the entire set of saved register values. </p> <p>An important caveat is that GCC arranges to save and restore only those registers known to the specific architecture variant being compiled for. This can make <code class="code">__builtin_setjmp</code> and <code class="code">__builtin_longjmp</code> more efficient than their library counterparts in some cases, but it can also cause incorrect and mysterious behavior when mixing with code that uses the full register set. </p> <p>You should declare the jump buffer argument <var class="var">buf</var> to the built-in functions as: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#include <stdint.h>
intptr_t <var class="var">buf</var>[5];</pre>
</div> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005fsetjmp">
<span class="category-def">Built-in Function: </span><span><code class="def-type">int</code> <strong class="def-name">__builtin_setjmp</strong> <code class="def-code-arguments">(intptr_t *<var class="var">buf</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005fsetjmp"> ¶</a></span>
</dt> <dd><p>This function saves the current stack context in <var class="var">buf</var>. <code class="code">__builtin_setjmp</code> returns 0 when returning directly, and 1 when returning from <code class="code">__builtin_longjmp</code> using the same <var class="var">buf</var>. </p></dd>
</dl> <dl class="first-deftypefn"> <dt class="deftypefn" id="index-_005f_005fbuiltin_005flongjmp">
<span class="category-def">Built-in Function: </span><span><code class="def-type">void</code> <strong class="def-name">__builtin_longjmp</strong> <code class="def-code-arguments">(intptr_t *<var class="var">buf</var>, int <var class="var">val</var>)</code><a class="copiable-link" href="#index-_005f_005fbuiltin_005flongjmp"> ¶</a></span>
</dt> <dd>
<p>This function restores the stack context in <var class="var">buf</var>, saved by a previous call to <code class="code">__builtin_setjmp</code>. After <code class="code">__builtin_longjmp</code> is finished, the program resumes execution as if the matching <code class="code">__builtin_setjmp</code> returns the value <var class="var">val</var>, which must be 1. </p> <p>Because <code class="code">__builtin_longjmp</code> depends on the function return mechanism to restore the stack context, it cannot be called from the same function calling <code class="code">__builtin_setjmp</code> to initialize <var class="var">buf</var>. It can only be called from a function called (directly or indirectly) from the function calling <code class="code">__builtin_setjmp</code>. </p>
</dd>
</dl> </div> <div class="nav-panel"> <p> Next: <a href="constructing-calls">Constructing Function Calls</a>, Previous: <a href="nested-functions">Nested Functions</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Nonlocal-Gotos.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Nonlocal-Gotos.html</a>
</p>
</div>
|