当前位置: 技术问答>linux和unix
linux 内核相关的链表代码,有地方不懂????
来源: 互联网 发布时间:2017-05-29
本文导语: /********main.c********************/] #include #include #include "list.h" #define NAMESIZE 32 struct score { int id; char name[NAMESIZE]; int math; struct list_head node; }; void print_s(void *data) { struct score ...
/********main.c********************/]
#include
#include
#include "list.h"
#define NAMESIZE 32
struct score {
int id;
char name[NAMESIZE];
int math;
struct list_head node;
};
void print_s(void *data)
{
struct score *d = data;
printf("%d %s %dn", d->id, d->name, d->math);
}
int main(void)
{
struct score *datap;
int i;
struct list_head *cur;
LIST_HEAD(list);
for (i = 0; i id = i;
datap->math = 100 - i;
snprintf(datap->name, NAMESIZE, "stu%d", i);
list_add(&datap->node, &list);
}
__list_for_each(cur, &list) {
datap = list_entry(cur, struct score, node);
print_s(datap);
}
#if 0
__list_for_each(cur, &list) {
datap = list_entry(cur, struct score, node);
if (datap->id == 9) {
__list_del_entry(&datap->node);
free(datap);
}
}
printf("n");
__list_for_each(cur, &list) {
datap = list_entry(cur, struct score, node);
print_s(datap);
}
#endif
__list_for_each(cur, &list) {
datap = list_entry(cur, struct score, node);
if (datap->id == 5) {
break;
}
}
printf("n");
if (cur == &list) {
printf("Can not find.n");
} else {
print_s(datap);
}
return 0;
}
/********头文件 list.h*****************/
#ifndef HS_KERNEL_LIST_H
#define HS_KERNEL_LIST_H
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member)
(type *)( (char *)ptr - offsetof(type,member) )
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)
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);
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
#define list_entry(ptr, type, member)
container_of(ptr, type, member)
#define __list_for_each(pos, head)
for (pos = (head)->next; pos != (head); pos = pos->next)
#endif
其中的main.c 中
一段代码
__list_for_each(cur, &list) {
datap = list_entry(cur, struct score, node);
print_s(datap);
}
根据他作的宏定义逆推
产生这样的一段代码
for (cur = (&list)->next; cur != (&list); cur = cur->next)
{
datap=(struct score *)( (char *)cur - ((size_t) &((struct score *)0)->node) //这一句是怎样执行的过程????????????????????
print_s(datap);
}
|
这一句的作用是,从一个struct成员的地址,找到整个struct的地址。
cur是一个struct list_head类型的指针,指向某个score结构的node成员。&((struct score *)0)->node)是node成员在score结构里面的偏移量。
cur是一个struct list_head类型的指针,指向某个score结构的node成员。&((struct score *)0)->node)是node成员在score结构里面的偏移量。