Linux Kernel Architecture

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

Appendix C: Notes on C


printf("one\n"); break;
case 2:
printf("two\n"); break;
case 3:
printf("three\n");
default:
printf("default\n");
}

The code generates the following output:

wolfgang@meitner>./switch
three
default

Because thecasestatement for 3 doesnotinclude abreakstatement, code flow descends to thedefault
label, which under normal circumstances would not have been selected. Generally,switchstatements
canbeexitedonlybymeansofbreakstatements (or at the end of the statement itself). Once a suitable
statement is found, the code descends until it reaches a corresponding statement — regardless of whether
it comes across further labels or not.

C.2.7 Doubly Linked Lists


Doubly linked lists appear in practically every larger data structure of the kernel. A number of general
functions and structures are therefore provided to implement such lists for a wide range of purposes.
Chapter 1 discussed the API needed to work with lists. This section describes its implementation, which
involves some interesting aspects of generic programming in C.

The starting point for linked lists is the following data structure that can be embedded in other data
structures:

<list.h>
struct list_head {
struct list_head *next, *prev;
};

The meaning of the elements is clear.nextpoints to the next element, andprevpoints to the previous
element. The list is also organized cyclically — in other words, the predecessor of the first list element is
the last entry, and the successor of thelast list element is the first entry.

Implementation of list functions is made more difficult by the following conditions:

❑ The list elements need not be at the beginning ofa structure, but may be located anywhere in
the structure. Because list processing is supposed to function with any data types, this causes
problems if a selected element is to be typecast into the target data type.

❑ Several list elements can be used jointly in a structure so that they can be held in various lists.
Free download pdf