linux内核侵入式链表c实现

常见的链表是将数据塞入到链表中去,linux内核中是将链表塞入到数据中(称之为侵入式链表),内核中还有一些关于链表的操作可以拿出来放到测试代码中测试;测试中用到的数据结构将链表list塞入到只有一个int型数据的结构中去

typedef struct fox {
    int data;
    struct list_head list;
} fox_t;

测试代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)

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

#define LIST_HEAD_INIT(name) { &(name), &(name) }

/*
 *   创建一个新的链表。是新链表头的占位符,并且是一个哑元素。
 *    同时初始化prev和next字段,让它们指向list_name变量本身。
*/
#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)


#define container_of(ptr, type, member) ({                   \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);  \
        (type *)( (char *)__mptr - offsetof(type,member));})

/*
 * 返回链表所在结点
 *
*/
#define list_entry(ptr, type, member) \
        container_of(ptr, type, member)
/*
 * 扫描指定的链表
*/
#define list_for_each(pos, head) \
        for (pos = (head)->next; pos != (head); pos = pos->next)

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;
}

/*
 * 把元素插入特定元素之后
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
        __list_add(new, head, head->next);
}

/*
 * 把元素插到特定元素之前。
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
        __list_add(new, head->prev, head);
}

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

/*
 * 删除特定元素
*/
static inline void list_del(struct list_head *entry)
{
        __list_del(entry->prev, entry->next);
        entry->next = LIST_POISON1;
        entry->prev = LIST_POISON2;
}

/*
 * 检查指定的链表是否为空
*/
static inline int list_empty(const struct list_head *head)
{
        return head->next == head;
}

typedef struct fox {
    int data;
    struct list_head list;
} fox_t;

int main(int argc, char *argv[])
{
    int i;
    fox_t redfox[10];
    fox_t *node;
    struct list_head *pos;

    /*初始化链表*/
    LIST_HEAD(foxlist);
    INIT_LIST_HEAD(&foxlist);

    /*判断链表是否为空*/
    if (list_empty(&foxlist))
       printf("list is empty\n");

    /*插入节点*/
    for (i = 0; i < 10; i++)
    {
        redfox[i].data = i + 1;
        list_add_tail(&redfox[i].list, &foxlist);
    }

    printf("[ ");
    list_for_each(pos, &foxlist) {
        node = list_entry(pos, fox_t, list);
        printf("%d ", node->data);
    }
    printf("]\n");

    /*删除节点*/
    list_del(&redfox[5].list);
    printf("[ ");
    list_for_each(pos, &foxlist) {
        node = list_entry(pos, fox_t, list);
        printf("%d ", node->data);
    }
    printf("]\n");

    /*判断链表是否为空*/
    if (!list_empty(&foxlist))
       printf("list is not empty\n");
    return 0;
}

运行结果:

list is empty
[ 1 2 3 4 5 6 7 8 9 10 ]
[ 1 2 3 4 5 7 8 9 10 ]
list is not empty

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值