linux 内核链表
linux 内核链表
内核链表
#ifndef __C_LIST_H
#define __C_LIST_H#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) (type *)((char *)ptr -offsetof(type,member))/** 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)struct list_head {struct list_head *next, *prev;
};/*** 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)/*** list_first_entry - get the first element from a list* @ptr: the list head to take the element from.* @type: the type of the struct this is embedded in.* @member: the name of the list_struct within the struct.** Note, that list is expected to be not empty.*/
#define list_first_entry(ptr, type, member)\list_entry((ptr)->next, type, member)/*** LIST_HEAD(name) = { &(name), &(name) }* init the struct list_head, next = prev = &name*/#define LIST_HEAD_INIT(name) { &(name), &(name) }#define LIST_HEAD(name)\struct list_head name = LIST_HEAD_INIT(name)/*** INIT_LIST_HEAD is a fun for init list * LIST_HEAD_INIT is macro for init list*/
static void INIT_LIST_HEAD(struct list_head *list)
{list->next = list;list->prev = list;
}/*** list_for_each - iterate over a list* @pos: the &struct list_head to use as a loop counter.* @head: the head for your list.*/
#define list_for_each(pos, head)\for (pos = (head)->next; pos != (head); pos = pos->next)/*** list_for_each_safe - iterate over a list safe against removal of list entry* @pos: the &struct list_head to use as a loop cursor.* @n: another &struct list_head to use as temporary storage* @head: the head for your list.*/
#define list_for_each_safe(pos, n, head)\for (pos = (head)->next, n = pos->next; pos != (head);\pos = n, n = pos->next)/*** list_for_each_r - iterate over a list reversely* @pos: the &struct list_head to use as a loop counter.* @head: the head for your list.*/
#define list_for_each_r(pos, head)\for (pos = (head)->prev; pos != (head); pos = pos->prev)/** 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 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 void list_add(struct list_head *new, struct list_head *head)
{__list_add(new, head, head->next);
}/*** 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 void list_add_tail(struct list_head *new, struct list_head *head)
{__list_add(new, head->prev, head);
}/** 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!* The caller must free the memerry.*/
static 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 void list_del(struct list_head *entry)
{__list_del(entry->prev, entry->next);entry->next = LIST_POISON1;entry->prev = LIST_POISON2;
}/*** list_del_init - deletes entry from list and reinitialize it.* @entry: the element to delete from the list.*/
static void list_del_init(struct list_head *entry)
{__list_del(entry->prev, entry->next);INIT_LIST_HEAD(entry);
}/*** list_replace - replace old entry by new one* @old : the element to be replaced* @new : the new element to insert** If @old was empty, it will be overwritten.*/
static void list_replace(struct list_head *old,struct list_head *new)
{new->next = old->next;new->next->prev = new;new->prev = old->prev;new->prev->next = new;
}static void list_replace_init(struct list_head *old,struct list_head *new)
{list_replace(old, new);INIT_LIST_HEAD(old);
}/*** list_move - delete from one list and add as another's head* @list: the entry to move* @head: the head that will precede our entry*/
static void list_move(struct list_head *list, struct list_head *head)
{__list_del(list->prev, list->next);list_add(list, head);
}/*** list_move_tail - delete from one list and add as another's tail* @list: the entry to move* @head: the head that will follow our entry*/
static void list_move_tail(struct list_head *list,struct list_head *head)
{__list_del(list->prev, list->next);list_add_tail(list, head);
}/*** list_is_last - tests whether @list is the last entry in list @head* @list: the entry to test* @head: the head of the list*/
static int list_is_last(const struct list_head *list,const struct list_head *head)
{return list->next == head;
}/*** list_empty - tests whether a list is empty* @head: the list to test.*/
static int list_empty(const struct list_head *head)
{return head->next == head;
}/*** list_empty_careful - tests whether a list is empty and not being modified* @head: the list to test** Description:* tests whether a list is empty _and_ checks that no other CPU might be* in the process of modifying either member (next or prev)** NOTE: using list_empty_careful() without synchronization* can only be safe if the only activity that can happen* to the list entry is list_del_init(). Eg. it cannot be used* if another CPU could re-list_add() it.*/
static int list_empty_careful(const struct list_head *head)
{struct list_head *next = head->next;return (next == head) && (next == head->prev);
}/*** list_is_singular - tests whether a list has just one entry.* @head: the list to test.*/
static int list_is_singular(const struct list_head *head)
{return !list_empty(head) && (head->next == head->prev);
}static void __list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)
{struct list_head *new_first = entry->next;list->next = head->next;list->next->prev = list;list->prev = entry;entry->next = list;head->next = new_first;new_first->prev = head;
}/*** list_cut_position - cut a list into two* @list: a new list to add all removed entries* @head: a list with entries* @entry: an entry within head, could be the head itself* and if so we won't cut the list** This helper moves the initial part of @head, up to and* including @entry, from @head to @list. You should* pass on @entry an element you know is on @head. @list* should be an empty list or a list you do not care about* losing its data.**/
static void list_cut_position(struct list_head *list,struct list_head *head, struct list_head *entry)
{if (list_empty(head))return;if (list_is_singular(head) &&(head->next != entry && head != entry))return;if (entry == head)INIT_LIST_HEAD(list);else__list_cut_position(list, head, entry);
}/*** add list between prev and next*/
static inline void __list_splice(const struct list_head *list,struct list_head *prev,struct list_head *next)
{struct list_head *first = list->next;struct list_head *last = list->prev;first->prev = prev;prev->next = first;last->next = next;next->prev = last;
}/*** list_splice - join two lists, this is designed for stacks* @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, head->next);
}/*** list_splice_tail - join two lists, each list being a queue* @list: the new list to add.* @head: the place to add it in the first list.*/
static void list_splice_tail(struct list_head *list,struct list_head *head)
{if (!list_empty(list))__list_splice(list, head->prev, head);
}/*** list_splice_init - join two lists and reinitialise the emptied list.* @list: the new list to add.* @head: the place to add it in the first list.** The list at @list is reinitialised*/
static void list_splice_init(struct list_head *list,struct list_head *head)
{if (!list_empty(list)) {__list_splice(list, head, head->next);INIT_LIST_HEAD(list);}
}/*** list_splice_tail_init - join two lists and reinitialise the emptied list* @list: the new list to add.* @head: the place to add it in the first list.** Each of the lists is a queue.* The list at @list is reinitialised*/
static void list_splice_tail_init(struct list_head *list,struct list_head *head)
{if (!list_empty(list)) {__list_splice(list, head->prev, head);INIT_LIST_HEAD(list);}
}#endif // __C_LIST_H
其他开源项目引用,如usblib:
struct list_head {struct list_head *prev, *next;
};/* Get an entry from the list* ptr - the address of this list_head element in "type"* type - the data type that contains "member"* member - the list_head element in "type"*/
#define list_entry(ptr, type, member)\container_of(ptr, type, member)#define list_first_entry(ptr, type, member)\list_entry((ptr)->next, type, member)#define list_next_entry(ptr, type, member)\list_entry((ptr)->member.next, type, member)/* Get each entry from a list* pos - A structure pointer has a "member" element* head - list head* member - the list_head element in "pos"* type - the type of the first parameter*/
#define list_for_each_entry(pos, head, member, type)\for (pos = list_first_entry(head, type, member);\&pos->member != (head);\pos = list_next_entry(pos, type, member))#define list_for_each_entry_safe(pos, n, head, member, type)\for (pos = list_first_entry(head, type, member),\n = list_next_entry(pos, type, member);\&pos->member != (head);\pos = n, n = list_next_entry(n, type, member))/* Helper macros to iterate over a list. The structure pointed* to by "pos" must have a list_head member named "list".*/
#define for_each_helper(pos, head, type)\list_for_each_entry(pos, head, list, type)#define for_each_safe_helper(pos, n, head, type)\list_for_each_entry_safe(pos, n, head, list, type)#define list_empty(entry) ((entry)->next == (entry))static inline void list_init(struct list_head *entry)
{entry->prev = entry->next = entry;
}static inline void list_add(struct list_head *entry, struct list_head *head)
{entry->next = head->next;entry->prev = head;head->next->prev = entry;head->next = entry;
}static inline void list_add_tail(struct list_head *entry,struct list_head *head)
{entry->next = head;entry->prev = head->prev;head->prev->next = entry;head->prev = entry;
}static inline void list_del(struct list_head *entry)
{entry->next->prev = entry->prev;entry->prev->next = entry->next;entry->next = entry->prev = NULL;
}static inline void list_cut(struct list_head *list, struct list_head *head)
{if (list_empty(head)) {list_init(list);return;}list->next = head->next;list->next->prev = list;list->prev = head->prev;list->prev->next = list;list_init(head);
}static inline void list_splice_front(struct list_head *list, struct list_head *head)
{list->next->prev = head;list->prev->next = head->next;head->next->prev = list->prev;head->next = list->next;
}
linux 内核链表相关推荐
- linux内核链表使用例,linux内核链表的使用例子
linux内核链表的使用例子 #include #include #include #include #include #include MODULE_LICENSE("GPL") ...
- linux内核链表以及list_entry--linux内核数据结构(一)
传统的链表实现 之前我们前面提到的链表都是在我们原数据结构的基础上增加指针域next(或者prev),从而使各个节点能否链接在一起, 比如如下的结构信息 typedef struct fox { un ...
- linux内核链表分析
一.常用的链表和内核链表的区别 1.1 常规链表结构 通常链表数据结构至少应包含两个域:数据域和指针域,数据域用于存储数据,指针域用于建立与下一个节点的联系.按照指针域的组织以及各个节 ...
- Linux内核链表交换节点,[笔记]Linux内核链表:结点的插入、删除以及链表的遍历...
Linux内核链表:结点的插入.删除以及链表的遍历 1. Linux内核链表的核心思想是:在用户自定义的结构A中声明list_head类型的成员p,这样每个结构类型为A的变量a中,都拥有同样的成员p, ...
- 深入分析 Linux 内核链表--转
引用地址:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据 ...
- 第六天2017/04/11(2:Linux内核链表Demo、顺序表、链表的开发与设计)
//结构体相关的高级话题#include "stdio.h" #include "stdlib.h" #include "string.h" ...
- linux内核链表的使用
linux内核链表: 链表通常包括两个域:数据域和指针域. struct list_head{ struct list_head *next,*prev; }; include/linux/list. ...
- Linux 内核链表 【转】
原文:http://www.ibm.com/developerworks/cn/linux/kernel/l-chain/index.html 一. 链表数据结构简介 链表是一种常用的组织有序数据的数 ...
- Linux驱动编程 step-by-step (十) Linux 内核链表
终于可以清闲下来打理一下我的blog了,台资企业真的事情很多很烦-- 前几篇文章对字符设备有个简单介绍,并以简单的一个字符设备驱动作结尾,其实linux上大部分驱动程序都是字符设备程序,Linux源码 ...
- Linux 内核链表剖析(二十)
上节博客中,我们讲到了 Linux 中的宏定义 offsetof 与 container_of 宏.那么本节我们的课程目标就是一直 Linux 内核链表,使其适用于非 GNU 编译器,分析 Linux ...
最新文章
- David与Vincent的博弈游戏[树型DP]
- Mrtg搭建流量监控服务器
- ES5原生api(2)
- Microsoft Azure云服务停机!系水泵未知原因关闭导致
- Linux下的sort排序命令详解(二)
- 数字图像处理技术详解程序_大学专业详解系列135——数字媒体技术(工学学士)...
- cuda cudnn tensorflow对应_Ubuntu18.04下安装Tensorflow+cuda+cudnn+pytorch
- 大数据_Hbase-API访问_Java操作Hbase_MR-数据迁移-代码测试---Hbase工作笔记0017
- body下的DIV 高度自适应
- python遍历字典的具体位置_python遍历字典
- EO CAT软件下载数据
- 经常用到的一个分页存储过程
- C和C++的关系, namespace, struct , class
- Hashcat密码破解
- HDU 6080 度度熊保护村庄(计算几何+floyd)
- 用python实现微信定时发送图片
- 【双足轮机器人】Ascento技术详解--(5)实验--(7)总结【翻译】
- NC65在日常开发中常用的代码写法
- Office2013 图标显示不正常的解决办法
- Linux---冯诺依曼体系结构和操作系统