list_entry in Linux

后端 未结 3 1772
野性不改
野性不改 2020-12-08 04:57
user/include/linux/list.h

this declaration:

#define list_entry(ptr, type, member) \\
((type *)((char *)(ptr) – (unsigned long)(&         


        
3条回答
  •  时光取名叫无心
    2020-12-08 05:45

    Consider two structs like this:

    struct data {
        int something;
    };
    
    struct container {
        int something_before;
        struct data data_item;
        int something_after;
    };
    

    Assume you have a pointer to a struct data value:

    struct data *data_ptr;
    

    The list_entry() macro helps you to convert data_ptr to a pointer to the struct container value that holds the struct data value, pointed to by ptr:

    struct container *cont_ptr = list_entry(data_ptr, struct container, data_item);
    

    The macro works by computing the offset of data_item inside the struct container, and subtracting that many bytes from the data_ptr pointer. This, when cast to struct container *, gives a valid pointer to the struct container that holds this particular struct data "inside".

    The macro can also be simplified a bit by using the builtin offsetof() macro:

    #define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr) – offsetof(type, member)))
    

提交回复
热议问题