#define container_of(ptr, type, member)


#define container_of(ptr, type, member)

/* include/linux/kernel.h */

/**
 * container_of - cast a member of a structure out to the containing structure
 * @ptr:    the pointer to the member.
 * @type:   the type of the container struct this is embedded in.
 * @member: the name of the member within the struct.
 *
 */
#define container_of(ptr, type, member) ({          \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

輸入:
結構體一個成員的member地址ptr,
結構體的類型,
結構體一個成員member的名,

輸出:
得到這個結構體變量的地址;

分析:
結構體類型是確定的,結構體成員 相對 結構體定義的起始地址 偏移是確定的;
知道了一個結構體成員的地址,可以根據偏移得到 結構體定義的起始地址;

Author: Yangkai Wang
wang_yangkai@163.com
Coding in 2021/05/07


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM