这一章我们研究四种主要的数据结构: 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. GooglePlay - 文件上传限制的扩展

    前言 Google Play应用商店在上传限制100MB大小,超过该大小的应用必须将超过部分以扩展文件的形式进行上传处理. 总共可上传2个扩展文件,每个最大文件可为2GB,同时obb文件格式可自选. ...

  2. CSS之 relative 特性

    1. 自身特性: 如left,right,top,bottom定位都是相对于自身位置定位. 当left与right同时存在,lfet生效. 当top与bottom同时存在,top生效. 无侵入,保留原 ...

  3. C#同步方法转异步

    public async Task DelayAsync() { await Task.Run(()=>Delay()); } private void Delay() { } 本作品采用知识共 ...

  4. git 合并两个仓库

    我有两个仓库,一个是gitbook在写一本 一个是放在github的垃圾,这个是我想要开个人网站,但是做的还是不行https://github.com/lindexi/lindexi.github.i ...

  5. ELK简介

    什么是ELK ELK是ElasticSearch,LogStash以及Kibana三个产品的首字母缩写.是可以和商业产品 Splunk 相媲美开源项目. 2013 年,Logstash 被 Elast ...

  6. Linux入门(11)——Ubuntu16.04安装texlive2016并配置texmaker和sublime text3

    安装过程中需要调用 Perl 的模块 Digest::MD5 来检测 ISO 文件的完整性:升级过程中界面需要调用 Perl 的模块 Tk: sudo apt-get install libdiges ...

  7. 【ASP.NET MVC 学习笔记】- 15 Unobtrusive Ajax

    本文参考:http://www.cnblogs.com/willick/p/3418517.html 1.Unobtrusive Ajax允许我们通过 MVC 的 Help mothod 来定义 Aj ...

  8. PHP程序员的技术成长之路规划

    按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...

  9. 聊聊Java中的反射(一)

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 反射reflection主要为了动态操作Java代码,它的主要功能体现在Java提供的refl ...

  10. eclipse禁用svg文件Validation

    1.打开window>preferences>validation找到xml validator 2.点击xml validator最右侧的按钮打开xml校验规则窗口,选中exclude ...