Linux内核数据结构
Chapter 6
Kernel Data Structures
What’s discussed
• Linked lists
• Queues
• Maps
• Binary trees
A singly linked list
A doubly linked list
Circular Linked Lists
The Linux Kernel’s Implementation
• All methods are defined in
The Linked List Structure
Node
• the list_add() method adds a new node to
an existing linked list
• These methods, however, are generic:
They accept only list_head structures
• Using the macro container_of(), we can
easily find the parent structure containing
any given member variable
Defining a Linked List
• initializing the linked list at runtime
• or at compile time
List Heads
• you will generally want a special pointer
that refers to your linked list, without being
a list node itself
• this special node is in fact a normal
list_head:
Manipulating Linked Lists
• Adding a Node to a Linked List
• Deleting a Node from a Linked List
Manipulating Linked Lists
• Moving and Splicing Linked List Nodes
Traversing Linked Lists
• The Basic Approach
• The Usable Approach
• Real example:
Queues
• FIFOs( First in, First Out)
• generic queue implementation is called
kfifo
• implemented in kernel/kfifo.c and declared
in .
kfifo
• Linux’s kfifo works like most other queue
abstractions
• providing two primary operations:
– enqueue (named in)
– dequeue (named out)
Creating a Queue
Enqueuing & Dequeuing Data
Obtaining the Size of a Queue
• total size in bytes
• current size used in bytes
• available size in bytes
Resetting and Destroying the
Queue
Queue Usage Example
Maps
• known as an associative array
• a collection of unique keys
• each key is