Download presentation
Presentation is loading. Please wait.
Published byClaude Norris Modified over 8 years ago
1
kernel data structures
2
outline linked list queues maps binary trees algorithmic complexity
3
singly linked lists
4
doubly lined lists
5
circular linked lists
6
the Linux kernel’s implementation The linked-list code is declared in the header file and the data structure is simple
7
usage
8
container_of() Using the macro container_of(), we can easily find the parent structure containing any given member variable. #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
9
container_of() #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) list_head 0x00 0xYZ offset = 0xYZ
10
container_of() #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) list_head member offset = 0xYZ container
11
// Keil 8051 compiler #define offsetof(s,m) (size_t)&(((s *)0)->m) // Microsoft x86 compiler (version 7) #define offsetof(s,m) (size_t)(unsigned long)&(((s *)0)->m) // Diab Coldfire compiler #define offsetof(s,memb) ((size_t)((char *)&((s *)0)->memb-(char *)0) offsetof
12
1.((s *)0) takes the integer zero and casts it as a pointer to s. 2.((s *)0)->m dereferences that pointer to point to structure member m. 3.&(((s *)0)->m) computes the address of m. 4.(size_t)&(((s *)0)->m) casts the result to an appropriate data type. offsetof in Keil’s C reference: http://blog.linux.org.tw/~jserv/archives/001399.html
13
functions list_addlist_add — add a new entry list_dellist_del — deletes entry from list list_movelist_move — delete from one list and add as another's head list_emptylist_empty — tests whether a list is empty list_entrylist_entry — get the struct for this entry list_for_eachlist_for_each — iterate over a list list_for_each_entrylist_for_each_entry — iterate over list of given type
14
#define list_entry(ptr, type, member) \ container_of(ptr, type, member) list_entry
15
QUEUES
16
kfifo Linux’s kfifo works like most other queue abstractions, providing two primary operations: – enqueue (unfortunately named in ) and – dequeue (out ). The kfifo object maintains two offsets into the queue: an in offset and an out offset. The enqueue (in) operation copies data into the queue, starting at the in offset.
17
functions kfifo_reset — removes the entire FIFO contents kfifo_reset kfifo_put — puts some data into the FIFO kfifo_put kfifo_get — gets some data from the FIFO kfifo_get kfifo_len — returns the number of bytes available in the FIFO kfifo_len kfifo_init — allocates a new FIFO using a preallocated buffer kfifo_init kfifo_alloc — allocates a new FIFO and its internal buffer kfifo_alloc kfifo_free — frees the FIFO kfifo_free
18
MAPS
19
definition A map, also known as an associative array, is a collection of unique keys, where each key is associated with a specific value. The relationship between a key and its value is called a mapping. Maps support at least three operations:
20
definition: operator Add (key, value) Remove (key) value = Lookup (key)
21
implementation The Linux kernel provides a simple and efficient map data structure, but it is not a general-purpose map It is designed for one specific use case: mapping a unique identification number (UID) to a pointer.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.