Common Kernel Datatypes

本文介绍了Linux内核中使用的几种关键数据结构,包括链表、散列表和红黑树等。详细解析了链表和散列表的具体实现方式,并阐述了红黑树的特点及其平衡原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linked Lists : /include/linux/list.h


struct list_head {

  struct list_head *next, *prev;

  };

 

#define list_for_each_safe(pos, n, head) /

  for (pos = (head)->next, n = pos->next; pos != (head); /

  pos = n, n = pos->next)


 #define list_for_each_entry(pos, head, member) /

  for (pos = list_entry((head)->next, typeof(*pos), member); /

  prefetch(pos->member.next), &pos->member != (head); /

  pos = list_entry(pos->member.next, typeof(*pos), member))



  struct hlist_head {

  struct hlist_node *first;

  };


  struct hlist_node {

  struct hlist_node *next, **pprev;

  };


注意:pprev可以理解成向listprev一样,是一个指向hlist_node的指针,又由于hlist_node的第一个元素next是一个指向 hlist_node的指针,pprev也是一个指向next的指针,即pprev是一个指向hlist_node的指针的指针。


 #define HLIST_HEAD_INIT { .first = NULL }

 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }

  

  These list constructs are used throughout the Linux kernel code in work queues, as we've seen in the scheduler, the timer, and the module-handling routines.


Trees

The red black tree used in Linux memory management is similar to an AVL tree. A red black tree is a balanced binary tree in which each node has a red or black color attribute.

Here are the rules for a red black tree:

  • All nodes are either red or black.

  • If a node is red, both its children are black.

  • All leaf nodes are black.

  • When traversing from the root node to a leaf, each path contains the same number of black nodes.

Both AVL and red black trees have a big-O of O log(n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值