练习一下linux中的list函数。

所有的list函数见 include/linux/list.h

自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下。

没有别的目的,就是想熟练一下。毕竟linux内核代码中试用了大量的list函数。

list的函数太方便使用了。

文件:list.c

 #include <stdio.h>
// #include <linux/list.h> struct list_head {
struct list_head *next, *prev;
}; #define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name) static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next) //ËüÖ»Êǽ«newÌí¼Óµ½prevºÍnextÖ®¼ä
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
} static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
} static inline int list_empty(const struct list_head *head)
{
return head->next == head;
} #undef offsetof
#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 *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) #define list_entry(ptr, type, member) \
container_of(ptr, type, member) /**
* * list_for_each_entry - iterate over list of given type
* * @pos: the type * to use as a loop cursor.
* * @head: the head for your list.
* * @member: the name of the list_struct within the struct.
* */
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member)) #define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) struct devlist {
struct list_head list;
char * name;
}; struct devlist dev0 = {
.list = LIST_HEAD_INIT(dev0.list),
.name = NULL,
}; /* static initiate head list*/
// LIST_HEAD(hl);
struct list_head hl = LIST_HEAD_INIT(hl); void main(void) {
/* dynamic initiate head list. */
INIT_LIST_HEAD(&hl);
struct list_head *l;
struct devlist * pdev = NULL;
int empty = list_empty(&hl);
if(empty)
printf("the device list is empty!\n");
struct devlist dev1;
dev1.name = "device1";
list_add_tail(&dev1.list, &hl);
struct devlist edev1 = {{}, "entry device1"};
list_add_tail(&edev1.list, &dev0.list);
struct devlist dev2 = {{}, "device2"};
list_add_tail(&dev2.list, &hl);
struct devlist edev2 = {{}, "entry device2"};
list_add_tail(&edev2.list, &dev0.list); empty = list_empty(&hl);
if(!empty)
printf("the device list is not empty!\n"); list_for_each_entry(pdev, &dev0.list, list){
printf("the device name is %s\n", pdev->name);
} list_for_each(l, &hl) {
// for (l = (&hl)->next; l != (&hl); l = l->next){
// printf("l is: %p, head is: %p \n", l, &hl);
pdev = list_entry(l, typeof(dev0), list);
printf("the device name is %s\n", pdev->name);
}
}

输出结果:

 $ ./list
the device list is empty!
the device list is not empty!
the device name is entry device1
the device name is entry device2
the device name is device1
the device name is device2
上一篇:[LeetCode-53] 最大子序和


下一篇:Security+ 学习笔记53 安全政策