linux kernel list

kernel-list

需要学些的知识点

offsetof

offsetof的作用是返回结构体中某个成员在该结构体中的偏移量

0是一个具体的常量值,指一个地址(struct list_head *)0是一个指针

指针本身是常量,但它指向的地址里的内容可以改变。不对一个空指针进行读写,就不会存在非法访问内存的问题

  • offsetof.c
#include <stdio.h>

#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

struct person {
    int height;
    int weight;
};

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


int main(void) 
{
    printf("%ld\n", sizeof(struct list_head*));                  /* 8 */
    printf("%lu\n", offsetof(struct person, height));            /* 0 */
    printf("%lu\n", offsetof(struct person, weight));            // 4
    printf("%lu\n", offsetof(struct list_head, next));           /* 0 */
    printf("%lu\n", offsetof(struct list_head, prev));           /* 8 */
    printf("%lu\n", (size_t)&((struct list_head*)0)->next);      /* 8 */
    printf("%lu\n", (size_t)&((struct list_head*)0)->prev);      /* 8 */
    /* 表示一个指向struct list_head结构的指针  */
    struct list_head *head = (struct list_head *)0;
    /* next和prev 成员的地址,减去它所在结构的地址,
     * 就可以得出next和prev在struct list_head结构中的偏移量, 
     * 但是此时struct list_head的地址为0,
     * 所以成员的地址就是其在struct list_head中的偏移量 */
    /* &((struct list_head *)0)->next 
     * 表示struct list_head结构的成员地址  */
    printf("%lu\n", (size_t)&head->next);                        /* 0 */
    /* &((struct list_head *)0)->prev 
     * 表示struct list_head结构的成员地址  */
    printf("%lu\n", (size_t)&head->prev);                        /* 8 */
    return 0;
}

typeof

typeof可以根据表达式或者变量,推断出其类型

gnu-c特有的

  • typeof.c
#include <stdio.h>

int main(void) 
{
    int ia = 0;
    int ib = 0;
    typeof(int *) pa, pb;                       /* int *pa, *pb */
    pa = &ia;
    pb = &ib;
    printf("ia = %d, ib = %d\n", *pa, *pb);     /* ia = 0, ib = 0 */
    *pa = 1;
    *pb = 2;
    printf("ia = %d, ib = %d\n", *pa, *pb);     /* ia = 1, ib = 2 */

    typeof(int) *p3, p4;                        /* int *p3, p4 */
    return 0;
}

container_of

container_of的作用是根据一个结构体变量中的一个域成员变量的指针,来获取指向整个结构体变量的指针。

注意ptr为结构体,某个成员变量的指针

type为struct 结构体

member为成员字段,例如:prev

  • container_of.c

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

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

struct person
{
    int height;
    int weight;
};

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

int main(void)
{
    struct person p = {180, 60};
    struct person *pp = container_of(&p.weight, struct person, weight);
    printf("%d\n", pp->height);

    struct person *pp2 = (struct person *)( (char*)(&p.weight)  - 
            offsetof(struct person, weight) );
    printf("%d\n", pp2->height);

    struct person *pp3 = (struct person *)( (char*)(&p.weight)  - 
            (size_t)&((struct person *)0)->weight );
    printf("%d\n", pp3->height);
    printf("-------------------------\n");

    struct list_head node = {(struct list_head *)9, (struct list_head *)8};
    struct list_head *pl = container_of(&node.prev, struct list_head, prev);
    printf("%p\n", pl->next);

    struct list_head *pl2 = (struct list_head *)( (char*)&node.prev - 
            offsetof(struct list_head, prev));
    printf("%p\n", pl2->next);

    struct list_head *pl3 = (struct list_head *)( (char*)&node.prev - 
            (size_t)&((struct list_head *)0)->prev );
    printf("%p\n", pl3->next);
    printf("-------------------------\n");

    printf("%p\n", &node);
    printf("%p\n", &node.prev);
    printf("%p\n", pl3);
    return 0;
}

双向链表

  • 头节点和数据节点都是struct list_head结构

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

  • 内核list是一个双向环形list,数据结构如下图:

kernel-list的使用

  • kernel_list.h
#ifndef __LIST_H__
#define __LIST_H__
#include <stddef.h>

/* taken from linux kernel */

#undef offsetof
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif

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

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

#define list_first_entry(ptr, type, member) \
    list_entry((ptr)->next, type, member)

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

#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)

#define list_for_each_prev(pos, head) \
    for (pos = (head)->prev; pos != (head); pos = pos->prev)

#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_entry_safe(pos, n, head, member)          \
    for (pos = list_entry((head)->next, typeof(*pos), member),  \
            n = list_entry(pos->member.next, typeof(*pos), member); \
            &pos->member != (head);                    \
            pos = n, n = list_entry(n->member.next, typeof(*n), member))

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 = entry->prev = NULL;
}

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

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

static inline 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);
    }
}

#endif
  • test_list.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kernel_list.h"

struct emp {
    int no;
    char name[254];
    struct list_head emp_list;
};

int main(void)
{
    struct list_head head;
    struct list_head *pos;
    struct emp *emps, *tmp;
    INIT_LIST_HEAD(&head);
    unsigned int i;
    for (i = 0; i < 10; ++i) {
        tmp = (struct emp *)malloc(sizeof(struct emp));
        tmp->no = i;
        sprintf(tmp->name, "zhangfei%d", i);
        list_add_tail(&(tmp->emp_list), &head);
    }

    printf("traversing the list using list_for_each()\n");
    printf("--------------------------------------\n");
    printf("head=%p head.next=%p head.prev=%p\n", 
            (void*)&head, (void*)head.next, (void*)head.prev);
    list_for_each(pos, &head) {
        tmp = list_entry(pos, struct emp, emp_list);
        printf("&tmp->emp_list=%p tmp->emp_list.next=%p tmp->emp_list.prev=%p\n", 
                (void*)&tmp->emp_list, 
                (void*)tmp->emp_list.next, 
                (void*)tmp->emp_list.prev);
        printf("id=%d, name=%s\n", tmp->no, tmp->name);
    }

    printf("--------------------------------------\n");

    for (pos = (&head)->next; pos != (&head); pos = pos->next) {
        tmp = ({ 
                const typeof( ((struct emp *)0)->emp_list ) *__mptr = (pos); 
                (struct emp *)( (char *)__mptr - ((size_t) &((struct emp *)0)->emp_list) );
              });
        printf("id=%d, name=%s\n", tmp->no, tmp->name);
    }

    return 0;
}

  • 双向链表的地址输出,如下
  • 从输出的地址,可以印证内核list是一个双向环形list
--------------------------------------
head=0x7ffd5de5a720 head.next=0x56018ea3c3a8 head.prev=0x56018ea3cdc8
&tmp->emp_list=0x56018ea3c3a8 tmp->emp_list.next=0x56018ea3c4c8 tmp->emp_list.prev=0x7ffd5de5a720
id=0, name=zhangfei0
&tmp->emp_list=0x56018ea3c4c8 tmp->emp_list.next=0x56018ea3c5e8 tmp->emp_list.prev=0x56018ea3c3a8
id=1, name=zhangfei1
&tmp->emp_list=0x56018ea3c5e8 tmp->emp_list.next=0x56018ea3c708 tmp->emp_list.prev=0x56018ea3c4c8
id=2, name=zhangfei2
&tmp->emp_list=0x56018ea3c708 tmp->emp_list.next=0x56018ea3c828 tmp->emp_list.prev=0x56018ea3c5e8
id=3, name=zhangfei3
&tmp->emp_list=0x56018ea3c828 tmp->emp_list.next=0x56018ea3c948 tmp->emp_list.prev=0x56018ea3c708
id=4, name=zhangfei4
&tmp->emp_list=0x56018ea3c948 tmp->emp_list.next=0x56018ea3ca68 tmp->emp_list.prev=0x56018ea3c828
id=5, name=zhangfei5
&tmp->emp_list=0x56018ea3ca68 tmp->emp_list.next=0x56018ea3cb88 tmp->emp_list.prev=0x56018ea3c948
id=6, name=zhangfei6
&tmp->emp_list=0x56018ea3cb88 tmp->emp_list.next=0x56018ea3cca8 tmp->emp_list.prev=0x56018ea3ca68
id=7, name=zhangfei7
&tmp->emp_list=0x56018ea3cca8 tmp->emp_list.next=0x56018ea3cdc8 tmp->emp_list.prev=0x56018ea3cb88
id=8, name=zhangfei8
&tmp->emp_list=0x56018ea3cdc8 tmp->emp_list.next=0x7ffd5de5a720 tmp->emp_list.prev=0x56018ea3cca8
id=9, name=zhangfei9
--------------------------------------

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值