Linux Kernel Architecture

(Jacob Rumans) #1
Mauerer app03.tex V1 - 09/04/2008 6:11pm Page 1192

Appendix C: Notes on C


Once you understand this optimization feature, the use ofscanfto read thexvariable in the previous
example becomes clearer. If the value ofxcannot change prior to theifquery, dead code elimination
is also applied to this program. Alternatively, it would have been possible to declare the variable as
volatile. This informs the compiler that the value of the variable can be modified by uncontrollable
side effects (such as interrupts), and this suppresses some kinds of optimization — including dead code
elimination.

C.1.5 Inline Functions


Compared with other programming languages, function calls in C carry relatively little overhead but
still require a certain amount of CPU time that may be crucial in the case ofveryfrequently used code
sections (or extremely time-critical segments such as interrupt handlers). To avoid having to split the
code into small sections and having to work with long functions, an earlier, widely adopted solution to
the problem is to employ macros. A function is replaced by a macro that the pre-processor automatically
copies into the ‘‘calling‘‘ function. The aesthetics of this approach are, of course, dubious, and the absence
of type checking for the procedure arguments (apart from a few unpleasant characteristics of the pre-
processor that must be noted when writing code) means that macros are not necessarily the method
of choice.

Inline functions implemented by the GCC offer an elegant alternative. The keywordinlineis added
to a function. This causes the compiler to copy the code — such as a macro — to the position at which
the function is called. Type checking at compilation time is retained, as is done in regular function calls.
Functions can be transformed into inline functions simply by prefixing them with the keywordinline
like this:

inline int add (int a, int b) {
...
}

If the arguments are constant when an inline function is called, the compiler may be able to apply other
optimization options (e.g., dead code elimination or CSE) — these would not be possible with normal
function calls.

Of course, there is always a reverse side to the coin. If longer, frequently used chunks of code are declared
inline, the size of the generated binary code grows enormously, and this can give rise to problems, par-
ticularly in low-resource embedded systems.

In the meantime,inlinefunctions have been included in the C99 standard, so they can now be translated
by other compilers. However, there are a number of small differences between the standard implemen-
tation and the GCC implementation, which are described in the GCC manual.

C.1.6 Attributes


Attributes supply compilers with more detailed information on the use of functions or variables. This
enables them to apply more precise optimization options in order to generate better code, or permits for-
mulations that would not be possible in normal C. A range of code output details can also be influenced.

GCC supports dozens of attributes for all possible purposes. These are described in the GCC documen-
tation. This section describes the attributes used by the kernel.
Free download pdf