#include <stdio.h>
#include <stdlib.h>
#define offsetof(type, member) ((size_t) &((type*)0)->member)
/**
* 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) );})
typedef struct list{
struct list *pre;
struct list *next;
}List, *PList;
typedef struct node{
struct list ls;
int ival;
}Node, *PNode;
List gList = {0};
PList gpList = &gList;
int init_plist(PList pLs)
{
if(!pLs)
return -1;
pLs->pre = pLs;
pLs->next = pLs;
return 0;
}
int init_list(List pLs)
{
pLs.pre = &pLs;
pLs.next = &pLs;
return 0;
}
int add_list(PList pLs_hd, PList pLs)
{
pLs->pre = pLs_hd->pre;
pLs_hd->pre->next = pLs;
pLs_hd->pre = pLs;
pLs->next = pLs_hd;
return 0;
}
int delete_list(PList pLs_hd, PList pLs)
{
pLs_hd->pre = pLs->pre;
pLs->pre->next = pLs_hd;
return 0;
}
int main()
{
PList pitr;
Node nd = {1};
nd.ival = 1;
PNode pNd;
printf("Base:[0x%8x], ls:[0x%8x]\n", &nd, &(nd.ls));
init_plist(gpList);
add_list(gpList, &(nd.ls));
for(pitr = gpList->next; pitr != gpList; pitr = pitr->next )
{
pNd = container_of(pitr, Node, ls);
printf("Base:[0x%8x], val:%d\n", pNd, pNd->ival);
}
return 0;
}