Donnerstag, 3. Juni 2010

kernel list handling function cont'd

kernel list handling function cont'd

quoted from : professional linux kernel architecture by Wolfgang Mauerer , page 21-22

4. list_empty(head) checks if a list is empty, that is, if it does not contain any elements.

5. list_splice(list, head) combines TWO lists by inserting the list in list after the head element of an existing list.

6. list_entry must be used to find a list element; at first glance, its call syntax appears to be quite complicated: list_entry(ptr, type, member).

ptr is a pointer to the list_head instance of the data structure, type is its type, and member is the element name used for the list element.

The following sample call would be needed to find a task_struct instance of a list:

struct task_struct = list_entry(ptr, struct task_struct, run_list)

Explicit type specification is required because list implementation is NOT type-safe.

The list element must be specified to find the CORRECT element if there are data structures that are included in several lists.

( Even if there is only one list element in the structure, this entry is used to find the CORRECT start address of the instance by means of pointer arithmetic; the address is translated into the required data type by means of type conversion.

7. list_for_each(pos, head) must be used to iterate through ALL elements of a list. pos indicated the current position in the list, while head specifies the list head.

eg.

structure list_head *p;

list_for_each(p, &list)
if (condition)
return list_entry(p, struct task_struct, run_list);

return NULL;

Keine Kommentare: