summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/inline.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/inline.html
new repository
Diffstat (limited to 'devdocs/gcc~13/inline.html')
-rw-r--r--devdocs/gcc~13/inline.html19
1 files changed, 19 insertions, 0 deletions
diff --git a/devdocs/gcc~13/inline.html b/devdocs/gcc~13/inline.html
new file mode 100644
index 00000000..39d462a5
--- /dev/null
+++ b/devdocs/gcc~13/inline.html
@@ -0,0 +1,19 @@
+<div class="section-level-extent" id="Inline"> <div class="nav-panel"> <p> Next: <a href="volatiles" accesskey="n" rel="next">When is a Volatile Object Accessed?</a>, Previous: <a href="alignment" accesskey="p" rel="prev">Determining the Alignment of Functions, Types or Variables</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="An-Inline-Function-is-As-Fast-As-a-Macro"><span>6.45 An Inline Function is As Fast As a Macro<a class="copiable-link" href="#An-Inline-Function-is-As-Fast-As-a-Macro"> ¶</a></span></h1> <p>By declaring a function inline, you can direct GCC to make calls to that function faster. One way GCC can achieve this is to integrate that function’s code into the code for its callers. This makes execution faster by eliminating the function-call overhead; in addition, if any of the actual argument values are constant, their known values may permit simplifications at compile time so that not all of the inline function’s code needs to be included. The effect on code size is less predictable; object code may be larger or smaller with function inlining, depending on the particular case. You can also direct GCC to try to integrate all “simple enough” functions into their callers with the option <samp class="option">-finline-functions</samp>. </p> <p>GCC implements three different semantics of declaring a function inline. One is available with <samp class="option">-std=gnu89</samp> or <samp class="option">-fgnu89-inline</samp> or when <code class="code">gnu_inline</code> attribute is present on all inline declarations, another when <samp class="option">-std=c99</samp>, <samp class="option">-std=gnu99</samp> or an option for a later C version is used (without <samp class="option">-fgnu89-inline</samp>), and the third is used when compiling C++. </p> <p>To declare a function inline, use the <code class="code">inline</code> keyword in its declaration, like this: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">static inline int
+inc (int *a)
+{
+ return (*a)++;
+}</pre>
+</div> <p>If you are writing a header file to be included in ISO C90 programs, write <code class="code">__inline__</code> instead of <code class="code">inline</code>. See <a class="xref" href="alternate-keywords">Alternate Keywords</a>. </p> <p>The three types of inlining behave similarly in two important cases: when the <code class="code">inline</code> keyword is used on a <code class="code">static</code> function, like the example above, and when a function is first declared without using the <code class="code">inline</code> keyword and then is defined with <code class="code">inline</code>, like this: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">extern int inc (int *a);
+inline int
+inc (int *a)
+{
+ return (*a)++;
+}</pre>
+</div> <p>In both of these common cases, the program behaves the same as if you had not used the <code class="code">inline</code> keyword, except for its speed. </p> <p>When a function is both inline and <code class="code">static</code>, if all calls to the function are integrated into the caller, and the function’s address is never used, then the function’s own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option <samp class="option">-fkeep-inline-functions</samp>. If there is a nonintegrated call, then the function is compiled to assembler code as usual. The function must also be compiled as usual if the program refers to its address, because that cannot be inlined. </p> <p>Note that certain usages in a function definition can make it unsuitable for inline substitution. Among these usages are: variadic functions, use of <code class="code">alloca</code>, use of computed goto (see <a class="pxref" href="labels-as-values">Labels as Values</a>), use of nonlocal goto, use of nested functions, use of <code class="code">setjmp</code>, use of <code class="code">__builtin_longjmp</code> and use of <code class="code">__builtin_return</code> or <code class="code">__builtin_apply_args</code>. Using <samp class="option">-Winline</samp> warns when a function marked <code class="code">inline</code> could not be substituted, and gives the reason for the failure. </p> <p>As required by ISO C++, GCC considers member functions defined within the body of a class to be marked inline even if they are not explicitly declared with the <code class="code">inline</code> keyword. You can override this with <samp class="option">-fno-default-inline</samp>; see <a class="pxref" href="c_002b_002b-dialect-options">Options Controlling C++ Dialect</a>. </p> <p>GCC does not inline any functions when not optimizing unless you specify the ‘<samp class="samp">always_inline</samp>’ attribute for the function, like this: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">/* <span class="r">Prototype.</span> */
+inline void foo (const char) __attribute__((always_inline));</pre>
+</div> <p>The remainder of this section is specific to GNU C90 inlining. </p> <p>When an inline function is not <code class="code">static</code>, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files, so the calls therein cannot be integrated. Therefore, a non-<code class="code">static</code> inline function is always compiled on its own in the usual fashion. </p> <p>If you specify both <code class="code">inline</code> and <code class="code">extern</code> in the function definition, then the definition is used only for inlining. In no case is the function compiled on its own, not even if you refer to its address explicitly. Such an address becomes an external reference, as if you had only declared the function, and had not defined it. </p> <p>This combination of <code class="code">inline</code> and <code class="code">extern</code> has almost the effect of a macro. The way to use it is to put a function definition in a header file with these keywords, and put another copy of the definition (lacking <code class="code">inline</code> and <code class="code">extern</code>) in a library file. The definition in the header file causes most calls to the function to be inlined. If any uses of the function remain, they refer to the single copy in the library. </p> </div> <div class="nav-panel"> <p> Next: <a href="volatiles">When is a Volatile Object Accessed?</a>, Previous: <a href="alignment">Determining the Alignment of Functions, Types or Variables</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">
+ &copy; 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/Inline.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Inline.html</a>
+ </p>
+</div>