Next:When is a Volatile Object Accessed?, Previous:OpenACC, Up:Extensions to the C Language Family [Contents][Index]
By declaring a function inline, you can direct GCC to makecalls to that function faster. One way GCC can achieve this is tointegrate that function’s code into the code for its callers. Thismakes execution faster by eliminating the function-call overhead; inaddition, if any of the actual argument values are constant, theirknown values may permit simplifications at compile time so that notall of the inline function’s code needs to be included. The effect oncode size is less predictable; object code may be larger or smallerwith function inlining, depending on the particular case. You canalso direct GCC to try to integrate all “simple enough” functionsinto their callers with the option-finline-functions.
GCC implements three different semantics of declaring a functioninline. One is available with-std=gnu89 or-fgnu89-inline or whengnu_inline attribute is presenton all inline declarations, another when-std=c99,-std=gnu99 or an option for a later C version is used(without-fgnu89-inline), and the thirdis used when compiling C++.
To declare a function inline, use theinline keyword in itsdeclaration, like this:
static inline intinc (int *a){ return (*a)++;}If you are writing a header file to be included in ISO C90 programs, write__inline__ instead ofinline. SeeAlternate Keywords.
The three types of inlining behave similarly in two important cases:when theinline keyword is used on astatic function,like the example above, and when a function is first declared withoutusing theinline keyword and then is defined withinline, like this:
extern int inc (int *a);inline intinc (int *a){ return (*a)++;}In both of these common cases, the program behaves the same as if youhad not used theinline keyword, except for its speed.
When a function is both inline andstatic, if all calls to thefunction are integrated into the caller, and the function’s address isnever used, then the function’s own assembler code is never referenced.In this case, GCC does not actually output assembler code for thefunction, unless you specify the option-fkeep-inline-functions.If there is a nonintegrated call, then the function is compiled toassembler code as usual. The function must also be compiled as usual ifthe program refers to its address, because that cannot be inlined.
Note that certain usages in a function definition can make it unsuitablefor inline substitution. Among these usages are: variadic functions,use ofalloca, use of computed goto (seeLabels as Values),use of nonlocal goto, use of nested functions, use ofsetjmp, useof__builtin_longjmp and use of__builtin_return or__builtin_apply_args. Using-Winline warns when afunction markedinline could not be substituted, and gives thereason for the failure.
As required by ISO C++, GCC considers member functions defined withinthe body of a class to be marked inline even if they arenot explicitly declared with theinline keyword. You canoverride this with-fno-default-inline; seeOptions Controlling C++ Dialect.
GCC does not inline any functions when not optimizing unless you specifythe ‘always_inline’ attribute for the function, like this:
/*Prototype. */inline void foo (const char) __attribute__((always_inline));The remainder of this section is specific to GNU C90 inlining.
When an inline function is notstatic, then the compiler must assumethat there may be calls from other source files; since a global symbol canbe defined only once in any program, the function must not be defined inthe other source files, so the calls therein cannot be integrated.Therefore, a non-static inline function is always compiled on itsown in the usual fashion.
If you specify bothinline andextern in the functiondefinition, then the definition is used only for inlining. In no caseis the function compiled on its own, not even if you refer to itsaddress explicitly. Such an address becomes an external reference, asif you had only declared the function, and had not defined it.
This combination ofinline andextern has almost theeffect of a macro. The way to use it is to put a function definition ina header file with these keywords, and put another copy of thedefinition (lackinginline andextern) in a library file.The definition in the header file causes most calls to the functionto be inlined. If any uses of the function remain, they refer tothe single copy in the library.
Next:When is a Volatile Object Accessed?, Previous:OpenACC, Up:Extensions to the C Language Family [Contents][Index]