//include/linux/list.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 INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
static inline void __list_add(struct list_head * newnode,
struct list_head * prev,
struct list_head * next)
{
next->prev = newnode;
newnode->next = next;
newnode->prev = prev;
prev->next = newnode;
}
//添加一个node
static inline void list_add(struct list_head *newnode, struct list_head *head)
{
__list_add(newnode, head, head->next);
}
static inline void list_add_tail(struct list_head *newnode, struct list_head *head)
{
__list_add(newnode, head->prev, head);
}
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
static inline void __list_del(struct list_head * prev,
struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
//删除一个node
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = entry->prev = 0;
}
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
#define list_entry(ptr, type, member) \
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
/*******************************
**指针ptr指向结构体type中的成员member;
**通过指针ptr,返回结构体type的起始地址
type
|----------|
| |
| |
|----------|
ptr-->| member --|
|----------|
| |
| |
|----------|
********************************/
//test_list.c
#include <stdio.h>
#include <stdlib.h>
//#include "ilist.h"
struct my_list{
struct list_head list;
char value[10];
};
int main(int argc, char **argv){
struct my_list *tmp;
struct list_head *pos, *q;
unsigned int i;
struct my_list mylist;
INIT_LIST_HEAD(&mylist.list); /*初始化链表头*/
/* 给mylist增加元素 */
for(i=3; i!=0; --i){
tmp= (struct my_list *)malloc(sizeof(struct my_list));
/* 或者INIT_LIST_HEAD(&tmp->list); */
printf("enter value:");
scanf("%s", tmp->value);
list_add(&(tmp->list), &(mylist.list));
/* 也可以用list_add_tail() 在表尾增加元素*/
}
printf("\n");
printf("traversing the list using list_for_each()\n");
list_for_each(pos, &mylist.list){
/* 在这里 pos->next 指向next 节点, pos->prev指向前一个节点.这里的节点是
struct my_list类型. 但是,我们需要访问节点本身,而不是节点中的list字段,
宏list_entry()正是为此目的。*/
tmp= list_entry(pos, struct my_list, list);
printf("%s ", tmp->value);
}
printf("\n");
printf("deleting the list using list_for_each_safe()\n");
list_for_each_safe(pos, q,&mylist.list){
tmp= list_entry(pos, struct my_list, list);
printf("%s ", tmp->value);
list_del(pos);
free(tmp);
}
}
关键点
定义变量:
type a;
type * b;
type1 * ptr;
执行:
ptr=&(a.member);
b=list_entry(ptr,type,member);
则可使b指向a,得到了a的地址。
如何做到的呢?
先看&((type *)0)->member:
把“0”强制转化为指针类型,则该指针一定指向“0”(数据段基址)。因为指针是“type *”型的,所以可取到以“0”为基地址的一个type型变量member域的地址。那么这个地址也就等于member域到结构体基地址的偏移字节数。
/**
* 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)))
再来看 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))):
(char *)(ptr)使得指针的加减操作步长为一字节,(unsigned long)(&((type *)0)->member)等于ptr指向的member到该member所在结构体基地址的偏移字节数。二者一减便得出该结构体的地址。转换为 (type *)型的指针,大功告成。