1 / 21

kernel data structures

kernel data structures. outline. linked list queues maps binary trees algorithmic complexity. singly linked lists. doubly lined lists. circular linked lists. the Linux kernel’s implementation.

jerryj
Download Presentation

kernel data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 <linux/list.h> 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) ({ \ consttypeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})

  9. container_of() #define container_of(ptr, type, member) ({ \ consttypeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) list_head 0xYZ offset = 0xYZ 0x00

  10. container_of() #define container_of(ptr, type, member) ({ \ consttypeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) list_head member offset = 0xYZ container

  11. offsetof // 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)// DiabColdfire compiler#define offsetof(s,memb) ((size_t)((char *)&((s *)0)->memb-(char *)0)

  12. offsetof in Keil’s C • ((s *)0) takes the integer zero and casts it as a pointer to s. • ((s *)0)->m dereferences that pointer to point to structure member m. • &(((s *)0)->m) computes the address of m. • (size_t)&(((s *)0)->m) casts the result to an appropriate data type. reference: http://blog.linux.org.tw/~jserv/archives/001399.html

  13. functions list_add — add a new entry list_del— deletes entry from list list_move— delete from one list and add as another's head list_empty— tests whether a list is empty list_entry— get the struct for this entry list_for_each— iterate over a list list_for_each_entry— iterate over list of given type

  14. list_entry #define list_entry(ptr, type, member) \ container_of(ptr, type, member)

  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_put— puts some data into the FIFO • kfifo_get— gets some data from the FIFO • kfifo_len— returns the number of bytes available in the FIFO • kfifo_init— allocates a new FIFO using a preallocated buffer • kfifo_alloc— allocates a new FIFO and its internal buffer • kfifo_free— frees the FIFO

  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.

More Related