LKD: Chapter 6 Kernel Data Structures
这一章我们研究四种主要的数据结构: linked lists, queues, maps, binary trees.
Linked Lists:(<linux/list.h>)
在linux中,并不是直接将某个结构体作为链表的节点,而是在该结构中插入一个链表的节点。借助container_of()这个宏,我们可以轻松的找到包含给定成员变量的父结构。
Linux kernel中一般使用的是循环双链表。
正常遍历使用的是list_for_each()宏,但是当遍历过程中删除某个表项时就有可能出错。解决办法是使用
list_for_each_entry_safe(pos, next, head, member)
与原来的不同在于需要多提供一个next指针,这样就可以在遍历的过程中安全移除当前表项了。
当我们能提供next和prev指针时,我们就直接使用内部函数而不是包装函数。比如删除某个节点,用__list_del(prev, next)而不是list_del(list),可以节约资源。
Queues:
内核中的队列也称作kfifo,在<linux/kfifo.h>中声明,在kernel/kfifo.c中实现。因为先进先出的特性,所以常用于生产与消费的模型中。初始化中的size须是2的n次方。
Maps:
Maps至少要能满足三种操作:
Add(key, value)
Remove(key)
value=Lookup (key)
Hash table是常见的map,但并不是所有maps都是hash table。
在linux内核中,map也被称作idr。其中UID可以理解为key。
Binary Trees:
某个节点的深度是指从根节点到该节点中有多少个父节点。而balanced binary tree就是指所有叶子节点的深度相差不超过1的二叉树。
在linux kernel中,主要使用的是red-black树,它有6个属性:
1、所有节点非黑即红;
2、叶子节点全为黑;
3、叶子节点不含数据;
4、所有非叶子节点都有两个孩子节点;
5、如果某节点是红色的,则它的两个孩子都是黑色的;
6、从一个节点开始到它的某个叶子节点的路径上含有的黑节点数目与到它其他的叶子节点的数目相同。
rbtree的查找和插入需要自己实现。
What Data Structure to Use, When:
linked list: iterating over all your data;
queue: producer/consumer pattern;
map: map a UID to an object;
red-black tree: store a large amount of data and look it up efficiently.
LKD: Chapter 6 Kernel Data Structures的更多相关文章
- [轉]Linux Data Structures
Table of Contents, Show Frames, No Frames Chapter 15 Linux Data Structures This appendix lists the m ...
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...
- Operating system management of address-translation-related data structures and hardware lookasides
An approach is provided in a hypervised computer system where a page table request is at an operatin ...
- 20182320《Program Design and Data Structures》Learning Summary Week9
20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
随机推荐
- web自动化测试从入门到持续集成(selenium webdriver)
在很多刚学习自动化的可能会认为我只需要会运用selenium,我只需要在一个编辑器中实用selenium +java编写了一些脚本那么就会自动化了,是真的吗?答案肯定是假的.自动化肯定是需要做到真的完 ...
- Python 初学者 入门 应该学习 python 2 还是 python 3?
许多刚入门 Python 的朋友都在纠结的的问题是:我应该选择学习 python2 还是 python3? 对此,咪博士的回答是:果断 Python3 ! 可是,还有许多小白朋友仍然犹豫:那为什么还是 ...
- Servlet 笔记-读取表单数据
Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析: getParameter():您可以调用 request.getParameter() 方法来获取表单参数的值. get ...
- 用linux文件处理三剑客将微信群成员导出的方法
工具: Mac/Linux 系统 Chrome Linux命令:vi.cat. wc. grep. awk. sed.sort. uniq 步骤: 1.微信网页版登陆: https://wx.qq.c ...
- angularJs 个人初探笔记
1.环境搭建与angular - phoneCat 安装可以通过git clone来下载源代码: git clone --depth=14 https://github.com/angular/ang ...
- 推荐一款不错的反编译软件:Reflector
只需要把要反编译的dll拖放到程序窗口就可以看到code了.是不是很简单,快来试试吧.不只是可以反编译个人写的code,.Net库一样可以查看代码.想学习.Net核心代码的可以试试看.
- ajax请求发送和页面跳转的冲突
http://blog.csdn.net/allenliu6/article/details/77341538?locationNum=7&fps=1 在A页面中,有一个click事件,点击时 ...
- 使用js获取数组中最大、最小的数字
1.查询最大值 var maxValue=Math.max.apply(Math,array); 2.查询最小值 var minValue=Math.min.apply(Math,array);
- linux终端自定义命令的别名
alias : 给某个命令定义别名. 如:alias gpush='Git push origin HEAD:refs/for/master'这样在终端中,只需要输入 gpush 就ok了.但是只是这 ...
- pgjdbc源码分析
一. 源代码目录结构 pgjdbc的源码结构如下图: 那么我们来一一看看各个模块都是做什么的吧. 1 core 该目录是程序的核心模块目录. 这里实现了大部分pgjdbc的基类和接口,例如statem ...