1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Inline Function Definitions (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Inline Function Definitions (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Inline Function Definitions (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Advanced-Definitions.html" rel="up" title="Advanced Definitions">
|
---|
33 | <link href="Obsolete-Definitions.html" rel="next" title="Obsolete Definitions">
|
---|
34 | <link href="Nested-Functions.html" rel="prev" title="Nested Functions">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Inline-Function-Definitions"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Nested-Functions.html" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Inline-Function-Definitions-1"></span><h4 class="subsection">22.7.4 Inline Function Definitions</h4>
|
---|
64 | <span id="index-inline-function-definitions"></span>
|
---|
65 | <span id="index-function-definitions_002c-inline"></span>
|
---|
66 | <span id="index-inline"></span>
|
---|
67 |
|
---|
68 | <p>To declare a function inline, use the <code>inline</code> keyword in its
|
---|
69 | definition. Here’s a simple function that takes a pointer-to-<code>int</code>
|
---|
70 | and increments the integer stored there—declared inline.
|
---|
71 | </p>
|
---|
72 | <div class="example">
|
---|
73 | <pre class="example">struct list
|
---|
74 | {
|
---|
75 | struct list *first, *second;
|
---|
76 | };
|
---|
77 |
|
---|
78 | inline struct list *
|
---|
79 | list_first (struct list *p)
|
---|
80 | {
|
---|
81 | return p->first;
|
---|
82 | }
|
---|
83 |
|
---|
84 | inline struct list *
|
---|
85 | list_second (struct list *p)
|
---|
86 | {
|
---|
87 | return p->second;
|
---|
88 | }
|
---|
89 | </pre></div>
|
---|
90 |
|
---|
91 | <p>optimized compilation can substitute the inline function’s body for
|
---|
92 | any call to it. This is called <em>inlining</em> the function. It
|
---|
93 | makes the code that contains the call run faster, significantly so if
|
---|
94 | the inline function is small.
|
---|
95 | </p>
|
---|
96 | <p>Here’s a function that uses <code>pair_second</code>:
|
---|
97 | </p>
|
---|
98 | <div class="example">
|
---|
99 | <pre class="example">int
|
---|
100 | pairlist_length (struct list *l)
|
---|
101 | {
|
---|
102 | int length = 0;
|
---|
103 | while (l)
|
---|
104 | {
|
---|
105 | length++;
|
---|
106 | l = pair_second (l);
|
---|
107 | }
|
---|
108 | return length;
|
---|
109 | }
|
---|
110 | </pre></div>
|
---|
111 |
|
---|
112 | <p>Substituting the code of <code>pair_second</code> into the definition of
|
---|
113 | <code>pairlist_length</code> results in this code, in effect:
|
---|
114 | </p>
|
---|
115 | <div class="example">
|
---|
116 | <pre class="example">int
|
---|
117 | pairlist_length (struct list *l)
|
---|
118 | {
|
---|
119 | int length = 0;
|
---|
120 | while (l)
|
---|
121 | {
|
---|
122 | length++;
|
---|
123 | l = l->second;
|
---|
124 | }
|
---|
125 | return length;
|
---|
126 | }
|
---|
127 | </pre></div>
|
---|
128 |
|
---|
129 | <p>Since the definition of <code>pair_second</code> does not say <code>extern</code>
|
---|
130 | or <code>static</code>, that definition is used only for inlining. It
|
---|
131 | doesn’t generate code that can be called at run time. If not all the
|
---|
132 | calls to the function are inlined, there must be a definition of the
|
---|
133 | same function name in another module for them to call.
|
---|
134 | </p>
|
---|
135 | <span id="index-inline-functions_002c-omission-of"></span>
|
---|
136 | <p>Adding <code>static</code> to an inline function definition means the
|
---|
137 | function definition is limited to this compilation module. Also, it
|
---|
138 | generates run-time code if necessary for the sake of any calls that
|
---|
139 | were not inlined. If all calls are inlined then the function
|
---|
140 | definition does not generate run-time code, but you can force
|
---|
141 | generation of run-time code with the option
|
---|
142 | <samp>-fkeep-inline-functions</samp>.
|
---|
143 | </p>
|
---|
144 | <span id="index-extern-inline-function"></span>
|
---|
145 | <p>Specifying <code>extern</code> along with <code>inline</code> means the function is
|
---|
146 | external and generates run-time code to be called from other
|
---|
147 | separately compiled modules, as well as inlined. You can define the
|
---|
148 | function as <code>inline</code> without <code>extern</code> in other modules so as
|
---|
149 | to inline calls to the same function in those modules.
|
---|
150 | </p>
|
---|
151 | <p>Why are some calls not inlined? First of all, inlining is an
|
---|
152 | optimization, so non-optimized compilation does not inline.
|
---|
153 | </p>
|
---|
154 | <p>Some calls cannot be inlined for technical reasons. Also, certain
|
---|
155 | usages in a function definition can make it unsuitable for inline
|
---|
156 | substitution. Among these usages are: variadic functions, use of
|
---|
157 | <code>alloca</code>, use of computed goto (see <a href="Labels-as-Values.html">Labels as Values</a>), and
|
---|
158 | use of nonlocal goto. The option <samp>-Winline</samp> requests a warning
|
---|
159 | when a function marked <code>inline</code> is unsuitable to be inlined. The
|
---|
160 | warning explains what obstacle makes it unsuitable.
|
---|
161 | </p>
|
---|
162 | <p>Just because a call <em>can</em> be inlined does not mean it
|
---|
163 | <em>should</em> be inlined. The GNU C compiler weighs costs and
|
---|
164 | benefits to decide whether inlining a particular call is advantageous.
|
---|
165 | </p>
|
---|
166 | <p>You can force inlining of all calls to a given function that can be
|
---|
167 | inlined, even in a non-optimized compilation. by specifying the
|
---|
168 | ‘<samp>always_inline</samp>’ attribute for the function, like this:
|
---|
169 | </p>
|
---|
170 | <div class="example">
|
---|
171 | <pre class="example">/* <span class="roman">Prototype.</span> */
|
---|
172 | inline void foo (const char) __attribute__((always_inline));
|
---|
173 | </pre></div>
|
---|
174 |
|
---|
175 | <p>This is a GNU C extension. See <a href="Attributes.html">Attributes</a>.
|
---|
176 | </p>
|
---|
177 | <p>A function call may be inlined even if not declared <code>inline</code> in
|
---|
178 | special cases where the compiler can determine this is correct and
|
---|
179 | desirable. For instance, when a static function is called only once,
|
---|
180 | it will very likely be inlined. With <samp>-flto</samp>, link-time
|
---|
181 | optimization, any function might be inlined. To absolutely prevent
|
---|
182 | inlining of a specific function, specify
|
---|
183 | <code>__attribute__((__noinline__))</code> in the function’s definition.
|
---|
184 | </p>
|
---|
185 | <hr>
|
---|
186 | <div class="header">
|
---|
187 | <p>
|
---|
188 | Previous: <a href="Nested-Functions.html" accesskey="p" rel="prev">Nested Functions</a>, Up: <a href="Advanced-Definitions.html" accesskey="u" rel="up">Advanced Definitions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
189 | </div>
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 | </body>
|
---|
194 | </html>
|
---|