Linux Kernel Architecture

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

Appendix C: Notes on C


list_for_each_entry(f, &sb->s_files, f_list) {
/* Code for processing the elements in f */
}

To illustrate the work oflist_for_each_entry, an overview of the essential elements of thefileand
super_blockstructures involved is needed. Their important elements are as follows:

<fs.h>
struct file {
struct list_head f_list;
...
};

<fs.h>
struct super_block {
...
struct list_head s_files;
...

super_block->s_filesserves as the starting point of a list in which elements of thefiletype are stored.
file->f_listis used as a list element to establish the link between the individual entries.

Iteration over the elements is split into the following two phases:


  1. Finding thelist_headinstance of the next entry. This is not dependent on the concrete data
    structure in the list. The kernel performs this task by de-referencing thenextelement of the
    current entry and thus finding the position of the next list element.
    The insertedprefetchstatements supply information to the compiler on which elements
    are to be transferred by preference from memory into one of the processor caches. When
    iterating over a list, this is particularly useful for thenextelements.

  2. Finding the container element of the list elements. This contains the useful data and is found
    by means of thelist_entrymacro, discussed below.


As a result of the cyclic nature of the list, the kernel easily detects when it has iterated over all elements.
Thenextelement of the current entry then points to the start of the list specified byhead.

list_entryis defined as follows:

<list.h>
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)

ptris a pointer to the list element,typespecifies the type of the container element (struct filein the
example), andmemberdefines which element of the container accepts the list elements (this element is
f_listin the example, because the list elements are stored infile->f_list).
Free download pdf