当前位置: 首页 > news >正文

linux内核中list的实现

 

目录

list_add

list_add_tail

list_del

list_empty

list_splice

list_entry

list_for_each


struct list_head {
	struct list_head *next, *prev;
};

list_add

头插法,将新元素new使用头插法插入到head后面

/*
 * Insert a new entry between two known consecutive entries.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_add(struct list_head *new,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

/**
 * list_add - add a new entry
 * @new: new entry to be added
 * @head: list head to add it after
 *
 * Insert a new entry after the specified head.
 * This is good for implementing stacks.
 */
static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

list_add_tail

尾插法,将新元素插入尾部,因为是循环链表,直接插在head前面也就相当于插在链表

static inline void __list_add(struct list_head *new,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

/**
 * list_add_tail - add a new entry
 * @new: new entry to be added
 * @head: list head to add it before
 *
 * Insert a new entry before the specified head.
 * This is useful for implementing queues.
 */
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

list_del

删除链表中的指定元素

/*
 * These are non-NULL pointers that will result in page faults
 * under normal circumstances, used to verify that nobody uses
 * non-initialized list entries.
 */
#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)

/*
 * Delete a list entry by making the prev/next entries
 * point to each other.
 *
 * This is only for internal list manipulation where we know
 * the prev/next entries already!
 */
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

/**
 * list_del - deletes entry from list.
 * @entry: the element to delete from the list.
 * Note: list_empty() on entry does not return true after this, the entry is
 * in an undefined state.
 */
static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

list_empty

判断链表是否为空

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

list_splice

合并链表,将list链表头插到head链表

/**
 * list_empty - tests whether a list is empty
 * @head: the list to test.
 */
static inline int list_empty(const struct list_head *head)
{
	return head->next == head;
}

static inline void __list_splice(const struct list_head *list,
				 struct list_head *head)
{
	struct list_head *first = list->next;
	struct list_head *last = list->prev;
	struct list_head *at = head->next;

	first->prev = head;
	head->next = first;

	last->next = at;
	at->prev = last;
}

/**
 * list_splice - join two lists
 * @list: the new list to add.
 * @head: the place to add it in the first list.
 */
static inline void list_splice(const struct list_head *list,
				struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head);
}

list_entry

获取结构体实例的首地址

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:	the pointer to the member.
 * @type:	the type of the container struct this is embedded in.
 * @member:	the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - offsetof(type,member) );})

/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) \
	container_of(ptr, type, member)
//最终的宏定义如下:
//ptr:结构体实例中特定成员的地址
//type:结构体类型
//member:结构体实例中特定成员的名称
#define list_entry(ptr, type, member)  ({			\
	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
	(type *)( (char *)__mptr - &((type *)0)->member) );})

/*
&(type *)0)->member: 在0x0地址上面建立sizeof(type)个字节的type结构体, ->member 取出这个0地址上type结构体里的member成员,最后获取到member距离0x0的偏移.
typeof( ((type *)0)->member ): 获取member的类型
const typeof( ((type *)0)->member ) *__mptr = (ptr): 定义一个指针__mptr指向实际地址
(type *)( (char *)__mptr - &((type *)0)->member) ):m_ptr代表的地址减去member在结构体中的偏移,即为结构体实例的首地址
*/

写到这,突然有个感慨:有些自己觉得不合理的东西,可能是因为自己的无知造成的.

list_for_each

遍历链表

//__builtin_prefetch() 是 gcc 的一个内置函数。它通过对数据手工预取的方法,减少了读取延迟,从而提高了性能,但该函数也需要 CPU 的支持。
void __builtin_prefetch (const void *addr, ...);

#define prefetch(x) __builtin_prefetch(x)

/**
 * list_for_each	-	iterate over a list
 * @pos:	the &struct list_head to use as a loop cursor.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head) \
	for (pos = (head)->next; prefetch(pos->next), pos != (head); \
        	pos = pos->next)

相关文章:

  • ZYNQ入门
  • everything-everything使用技巧,过滤文件语法
  • 两军对垒问题及个人的思考
  • HDFS的启动流程和HA
  • ISP算法----AWB总结及源代码
  • 【Java基础(六)】面向对象与类的基础知识
  • Python爬虫——BeautifulSoup的基本使用
  • Acwing 802. 区间和
  • 传统方式连接数据库的弊端和数据库连接池原理
  • 什么叫 “雪碧图”?
  • 公众号网课搜题题库使用方法
  • Excel中身份证号码相关操作详解
  • 如何用4行 C 代码实现一个跨平台的命令行 mp3 播放器
  • ES 查询用法
  • golang泛型
  • 【mysql】环境安装、服务启动、密码设置
  • 2017届校招提前批面试回顾
  • C++类的相互关联
  • ES6系统学习----从Apollo Client看解构赋值
  • go语言学习初探(一)
  • HTTP中GET与POST的区别 99%的错误认识
  • Mysql优化
  • Shadow DOM 内部构造及如何构建独立组件
  • Tornado学习笔记(1)
  • Unix命令
  • 测试如何在敏捷团队中工作?
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 近期前端发展计划
  • 力扣(LeetCode)22
  • 力扣(LeetCode)357
  • 前端技术周刊 2019-01-14:客户端存储
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 算法-图和图算法
  • 网络应用优化——时延与带宽
  • Prometheus VS InfluxDB
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • ​直流电和交流电有什么区别为什么这个时候又要变成直流电呢?交流转换到直流(整流器)直流变交流(逆变器)​
  • # .NET Framework中使用命名管道进行进程间通信
  • #{}和${}的区别?
  • #Lua:Lua调用C++生成的DLL库
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (Java)【深基9.例1】选举学生会
  • (待修改)PyG安装步骤
  • (算法二)滑动窗口
  • (转)大型网站架构演变和知识体系
  • .cfg\.dat\.mak(持续补充)
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .net core 控制台应用程序读取配置文件app.config
  • .Net mvc总结
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • @Conditional注解详解
  • [ajaxupload] - 上传文件同时附件参数值
  • [Angular] 笔记 7:模块
  • [IE编程] WebBrowser控件的多页面浏览(Tabbed Browsing)开发接口