Kernel Design Foundation (eight) core data structure

Source: Internet
Author: User
Tags prev

I personally prefer to learn data structures, and the data structures implemented in the Linux kernel are a great way to learn, understand, and apply data structures. There are four types of data structures widely used in the kernel: linked lists, queues, mappings, and two-fork trees.


Linked list:

The Linux kernel is efficient and streamlined, so we sometimes need to dynamically create and allocate memory, then we have to use the linked list, we allocate memory according to the actual situation, we can only modify the linked list of pointers, still be able to index the memory area just allocated. The list of links is one-way linked list, two-way linked list and circular linked list.

    • One-way linked list
struct List_element {void *data;struct list_element *next;/* Pointer to the next element */};

    • Doubly linked list
struct List_element {void *data;struct list_element *next;/* Pointer to next element */struct list_element *prev;/* Pointer to previous element */};

    • Circular link List
the next pointer to the last element of the list is not NULL, but instead points to the first element of the list. The circular list is also divided into one-way and two-way.

because the circular doubly linked list provides the greatest flexibility (no specific example of the contrast, temporarily still can not see, want to understand the friends leave a comment:), so the Linux kernel standard list is the use of circular doubly linked list. In addition, if you need random access to data, generally do not use linked lists, the ideal use of linked lists is to traverse all the data or need to dynamically join and delete data. However, the list of links in the Linux kernel is not as simple and primitive as the example above, it does not plug the data structure into the linked list, but instead plugs the list nodes into the data structure.
struct List_head {struct list_head *next;struct list_head *prev;};

actually this is the so-called linked list node, and how is it plugged into the data structure? As a member of the data structure, of course:
struct Fox {unsigned long tail_length;unsigned long weight;bool is_fantastic;struct list_head list;  /* All Fox structures form a linked list */};

that is, the list no longer points to a fox structure, but instead uses. Next and. Prev to point to the front and back of the two list_head structures. It's strange that we need to know which two fox structures the two list_head structures are pointing to, which requires the help of macro container_of () so we can find this list_ Any variables contained in the parent structure of the head struct (that is, the FOX structure to which it belongs). Why do you want to construct a linked list, friends who want to know leave a comment:)
Queue:
in kernel programming, we often encounter the producer-consumer model, when the queue is the most ideal data structure, the producer put the data into the queue, the consumer then take out, first-come first-served.
Mapping:
mapping refers to the practice of having a collection of unique keys, each associated with a specific value, which is valued by the key we call mapping. but with what data structure to achieve, generally use hash list (hash) and self-balanced binary search tree. Perhaps the hash table is what we are most familiar with, construct a hash function, enter a unique key, and get the corresponding value. But in fact more time to use a binary tree, the Linux kernel is the case. The Linux kernel uses IDR to maintain a custom mapping
int idr_get_new (struct IDR *idp, void *ptr, int *id);

The mapping of IDs (keys) and PTR (values) is injected into the IDR. And the lookup operation is as follows:
void *idr_find (struct IDR *IDP, int id);

Locate the corresponding pointer ptr based on the ID.
two fork tree:
specific red and black tree introduction see my BlogRed and Black tree (Red-black TREES) basic concepteach i node in the kernel has its own rbtree to correlate page offsets in the file, which is an application of the red-black tree in Linux.




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.