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

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

#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  0
struct 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);
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值