Kernel-driven Development fourth day Linux kernel list __linux

Source: Internet
Author: User
Tags prev contact form
Basic concepts
1. A linked list is a commonly used data structure that passes through pointers
Connect a series of data nodes into a single data chain.
Phase
For the array, the linked list has a better dynamic nature, and establishes
Linked list without prior knowledge of the total amount of data, you can randomly
Allocate space that can be efficiently in any bit of the list
Inserts or deletes data in real time.


2. The cost of the linked list is mainly
is the order of access and the loss of space in the organization chain


Typically, a linked list data structure contains at least two domains:
Data
Domain and pointer fields, data fields for storing data, pointers
Domain is used to establish a connection with the next node.
According to the point
The organization of the needle domain and the contact form between the nodes
Type, linked list can be divided into single linked list, double linked list, follow
Ring linked list and many other types.








A large number of linked lists are used in the Linux kernel
structure to organize the data. Most of these linked lists use
One of the implementations in [Include/linux/list.h]
Set of wonderful linked list data structures.
Realize:





1.
Definition of linked list data structure:


struct List_head


{
struct List_head *next, *prev;
};


List_head structure contains two pointers to List_head structure
Prev and Next,
This shows that the core of the linked list with dual-chain Biaogong
Energy, in fact, it is usually organized into a two-way cyclic list.


2.


Linked list operations
The list operations provided in the kernel include:


1. Initialize the header of the linked table


Init_list_head (List_head *head)
2. Insert Node


List_add (struct list_head *new, struct list_head)


List_add_tail (struct list_head *new, struct list_head)






3. Delete node


List_del (struct list_head *entry)
4. Extract data structure


List_entry (PTR, type, member)


Node pointer ptr in a known data structure to find the data knot
Frame
Cases:
List_entry (AUP, struct autofs, list)






5. Traverse


List_for_each (Struc list_head *pos, Struc list_head)


Cases:


struct List_head *entry;


struct List_head Cs46xx_devs; Linked table Header


List_for_each (entry, &cs46xx_devs)
{


Card = list_entry (entry, struct cs_card, list);


if (Card->dev_midi = = minor)
Break


}










And also
List_for_each_entry
Reverse traversal
List_for_each_entry_reverse ()
Traversal while deleting
List_for_each_entry_safe ()

6. Release
Cannot use List_for_each () and List_del () to release a linked list
Use the For loop and List_del, and then use Kfree to allocate memory using Kmalloc
7. Moving and merging linked lists
Move a node from one linked list to another
List_move ()
List_move_tail ()
Charine
List_empty ()
Combine two linked lists
List_splice ()
Merge two linked lists and reinitialize the original list
List_splice_init ()

Kernel data type
List, queue Kfifo, mapping, Binary tree (red-black tree) from Linux kernel design and implementation Third edition
Data structure and selection
If the main operation of the data collection is traversing the data, the linked list is used. In fact, no data structures can provide more complex algorithms than linear algorithms to traverse the elements, so you should use the simplest data structures to do simple work. In addition, when performance is not a primary consideration, or when you need to store less data first, or when you need to interact with other code in the kernel that uses the linked list, select the list
If your code conforms to the producer/consumer model, use queues, especially if you want a fixed-length buffer. Queues make adding and deleting work easier and more efficient. The queue also provides first-in-first-out (FIFO) semantics, which is a common requirement for producer/consumer use cases. On the other hand, if you need to store a data set of unknown size, the linked list may be more appropriate because you can add any number of data items
If you need to map a UID to an object, use the mapping. mapping constructs make mapping work simple and efficient, and mapping helps you maintain and distribute UID. Linux's mapping interface is a mapping of UID to pointers, which is not suitable for other scenarios. But if you're dealing with a descriptor for a user space, consider mapping it.
If you need to store a lot of data and retrieve it quickly, then the red-black tree is the best. The red-black tree ensures that the complexity of search time is logarithmic, and it can ensure that sequential traversal time complexity is a linear relationship. Although he is more complex than any data structure, it is not too bad for memory overhead. But if you don't perform too many time-pressing search operations, the red-black tree may not be the best choice, and it's best to use a linked list.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.