multi inheritance with list_head.
typedef struct OLNode {int LineNumber, ColumneNumber;ElemType value;struct OLNode *right, *down;}OLNode, *OList;
Find History
Offset
small test
#include <stdio.h>#include <stddef.h>typedef struct list_head {int count;}list_head ;int ref(struct list_head *obj) {return obj->count;}#define CustomTransfer_T(a) ( (_customObject*) ( ((char*)a) - \offsetof(_customObject,obj_T) ) )typedef struct _customObject {int obj;int obj_T;int extra;}_customObject;int sum(_customObject *obj) {return obj->extra+ ref((list_head*)&obj->obj) + ref((list_head*)&obj->obj_T);}void main() {_customObject custom = {10,11,12};printf("%d\n",ref((list_head*)&custom.obj));printf("%d\n",ref((list_head*)&custom));printf("%d\n",ref((list_head*)(&custom.obj_T)));list_head* parent_T = (list_head*)&custom.obj_T;printf("%d\n",sum(CustomTransfer_T(parent_T)));}
Linux 2.4
Linux 2.4 版本中还是最初质朴的定义, 最好研究
https://elixir.bootlin.com/linux/2.4.31/source/include/linux/list.h#L187
/*** list_entry - get the struct for this entry* @ptr: the &struct list_head pointer.* @type: the type of the struct this is embedded in.* @member: the name of the list_struct within the struct.*/#define list_entry(ptr, type, member) \((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
Linux 5.9
Linux 5.9 版本中 从 list 寻找 structure 的函数定义转移到了 /include/linux/kernel
https://elixir.bootlin.com/linux/v5.9-rc8/source/include/linux/kernel.h#L1000
/*** 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) ({ \void *__mptr = (void *)(ptr); \BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \!__same_type(*(ptr), void), \"pointer type mismatch in container_of()"); \((type *)(__mptr - offsetof(type, member))); })
