这一章我们研究四种主要的数据结构: 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的更多相关文章

  1. [轉]Linux Data Structures

    Table of Contents, Show Frames, No Frames Chapter 15 Linux Data Structures This appendix lists the m ...

  2. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  3. 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 ...

  4. 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 ...

  5. 20182320《Program Design and Data Structures》Learning Summary Week9

    20182320<Program Design and Data Structures>Learning Summary Week9 1.Summary of Textbook's Con ...

  6. A library of generic data structures

    A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...

  7. 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 ...

  8. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  9. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

随机推荐

  1. iOS音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  2. 创建静态库Static Library(Framework库原理相似)

    在项目开发的过程中,经常使用静态库文件.例如两个公司之间业务交流,不可能把源代码都发送给另一个公司,这时候将私密内容打包成静态库,别人只能调用接口,而不能知道其中实现的细节. 简介: 库是一些没有ma ...

  3. TableView的性能优化

    现在市场上的iOS应用程序界面中使用最多的UI控件是什么? 答案肯定是UITableView,几乎每一款App都有很多的界面是由UITableView实现的,所以为了做出一款优秀的App,让用户有更好 ...

  4. 【转载】兼容所有浏览器的JQuery zClip插件实现复制到剪贴板功能

    文章转载自 代码家园 http://www.daimajiayuan.com/ 原文链接:http://www.daimajiayuan.com/sitejs-17973-1.html原文摘要: 相信 ...

  5. Appium python自动化测试系列之Android知识讲解(三)

    ​3.1 ADB工具讲解 3.1.1 什么是ADB呢? 我们不去解释官方语言的翻译,给大家说一个通熟易懂的说法,ADB我理解为他就是电脑和手机连接的桥梁.此连接不是充电的连接,大家不要混淆,说他是一个 ...

  6. 一起来学linux:网络命令

    首先介绍最基本也是经常用到的命令ifconfig,对应windows中的ipconfig.执行ifconfig会将所有的端口信息都显示出来,包括IP地址,MTU 接收和发送的报文还有HWaddr也就是 ...

  7. JS高级. 05 词法作用域、变量名提升、作用域链、闭包

    作用域 域,表示的是一个范围,作用域,就是作用范围. 作用域说明的是一个变量可以在什么地方被使用,什么地方不能被使用. 块级作用域 JavaScript中没有块级作用域 { var num = 123 ...

  8. struts2使用模型传值

    用户bean package userBeans; public class User { private String username; public String getUsername() { ...

  9. Vuex state 状态浅解

    对于Vuex中的state里面的理解总是有些欠缺,机制似乎理解了.但是还有很多的不足,在这就先浅谈下自己的理解. vuex 机制中,定义了全局Store,在各个vue组件面的this.$store指向 ...

  10. C# post提交

    WebForm 前台 <asp:Button ID="Button1" runat="server" Text="Button" On ...