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 ...
随机推荐
- Linux Redis集群搭建与集群客户端实现(Python)
硬件环境 本文适用的硬件环境如下 Linux版本:CentOS release 6.7 (Final) Redis版本: Redis已经成功安装,安装路径为/home/idata/yangfan/lo ...
- php中各种定义变量的方法
1.定义常量define("CONSTANT", "Hello world."); 常量只能包含标量数据(boolean,integer,float 和 s ...
- 中秋H5,这篇脑洞开的可以!
案例:嫦娥--寻开心出品:凯迪仕 1.内容:这是一支视频类H5案例.Loading完毕进入页面,首屏提示案例最佳观看方式为先锁屏再横屏.点击开始按钮播放视频,视频讲述"葫芦娃"缠着 ...
- 【转】Sizeof与Strlen的区别与联系
原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- 购物篮算法的理解-基于R的应用
是无监督机器学习方法,用于知识发现,而非预测,无需事先对训练数据进行打标签,因为无监督学习没有训练这个步骤.缺点是很难对关联规则学习器进行模型评估,一般都可以通过肉眼观测结果是否合理. 一,概念术语 ...
- 【ASP.NET MVC 学习笔记】- 20 ASP.NET Web API
本文参考:http://www.cnblogs.com/willick/p/3441432.html 1.ASP.NET Web API(本文简称Web API),是基于ASP.NET平台构建REST ...
- 优先队列(存储结构数组)--Java实现
/*优先队列--是对队列的一种改进 *要存储的数据存在优先级--数值小的优先级高--在队头 *优先队列的实现 *1.数组:适合数据量小的情况(没有用rear+front实现) *优先队列头在items ...
- 树莓派.系统.修改声音输出通道(auto,hdmi,耳机接口)
树莓派平时通过耳机接口一直在正常使用 有一天外接了一个HDMI的屏幕, 结果耳机接口连着的音箱突然就没声音了,在网上查了资料,发现原来树莓派声音输出有3种模式 记录如下: 在命令行终端上输入下面命令将 ...
- vue数据请求
我是vue菜鸟,第一次用vue做项目,写一些自己的理解,可能有些不正确,欢迎纠正. vue开发环境要配置本地代理服务.把config文件加下的index.js里的dev添加一些内容, dev: { e ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...