链表代码在<linux/list.h>中声明,其数据结构很简单:
struct list_head {
struct list_head *next;
struct list_head *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;
}
/**
* 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) );})
本文深入探讨了链表在Linux中的实现方式,包括其基本数据结构、初始化方法及使用技巧,同时展示了如何通过container_of宏进行结构转换,提供了一个全面的链表编程指南。
1685

被折叠的 条评论
为什么被折叠?



