#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
//#define ossfetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
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);
}
struct stu {
int n;
int m;
struct list_head entry;
};
void test()
{
struct list_head entry;
struct list_head entry1;
struct list_head entry2;
struct stu student;
struct stu student1;
struct list_head *ptr;
LIST_HEAD(dev_list);
INIT_LIST_HEAD(&student.entry);
INIT_LIST_HEAD(&student1.entry);
student.m = 5;
student.n = 10;
student1.m = 8;
student1.n = 240;
//INIT_LIST_HEAD(&entry1);
//INIT_LIST_HEAD(&entry2);
list_add(&student.entry, &dev_list);
list_add(&student1.entry, &dev_list);
const typeof(((struct stu *)0)->entry) *p = dev_list.next;
struct stu *q;
q = ((char *)p - ((size_t) & ((struct stu *)0)->entry));
//for (ptr = dev_list.next; ptr != &dev_list; ptr = ptr->next)
{
//ptr = list_entry(ptr, struct stu, entry);
//}
//}
//list_add(&entry1, &dev_list);
//list_add(&entry2, &dev_list);
}
int main()
{
test();
return 0;
}
|