linux内核学习中--“list.h”中 插入add函数 总结

来源:互联网 发布:东亚文化圈知乎 编辑:程序博客网 时间:2024/05/20 18:49

第一  声明和初始化,我在这里不详细说明了,请看我上一篇博文,在这里贴出相应的代码:

#ifndef _LINUX_LIST_H#define _LINUX_LIST_H#define offsetof1(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)#define container_of(ptr, type, member) ( { \        const typeof( ((type *)0)->member ) *__mptr = (ptr); \        (type *)( (char *)__mptr - offsetof1(type,member) ); } )static inline void prefetch(const void *x) {;}static inline void prefetchw(const void *x) {;}//#define LIST_POISON1  ((void *) 0x00100100)//#define LIST_POISON2  ((void *) 0x00200200)#define LIST_POISON1  0#define LIST_POISON2  0struct list_head {        struct list_head *next, *prev;        //双向链表};#define LIST_HEAD_INIT(name) { &(name), &(name) }   //用同一对象初始化next 和prev.#define LIST_HEAD(name) \        struct list_head name = LIST_HEAD_INIT(name)#define INIT_LIST_HEAD(ptr) do { \                  //初始化就是把指针指向自己        (ptr)->next = (ptr); (ptr)->prev = (ptr); \} while (0)


 

下面就是插入函数的具体分析,其实插入函数很简单,只要学过C语言的都能懂,我在这里只是简单解释下:

/** 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 *new1,                //插入新条目,插在prev与next中间                              struct list_head *prev,                //                              struct list_head *next){        next->prev = new1;        new1->next = next;        new1->prev = prev;        prev->next = new1;}/*** 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 *new1, struct list_head *head)   //头插法,调用_list_add()实现{        __list_add(new1, 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 inline void list_add_tail(struct list_head *new1, struct list_head *head)   //尾部插法{        __list_add(new1, head->prev, head);}


 

原创粉丝点击