源码+调试+注释 = 学会!
#include<Windows.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
/* stl */
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<map>
using namespace std;
struct list_head {
struct list_head* next;
struct list_head* prev;
};
struct fox {
unsigned long tail_length; // 尾巴长度,以厘米为单位
unsigned long weight; // 重量,以千克为单位
bool is_fantastic; // 这只狐狸奇妙吗?
struct list_head list;
};
#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;
}
/**
* Insert a new entry between two known consecutive entries.
*/
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;
}
/**
* Insert a new entry after the specified head.
*/
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;
}
#define POISON_POINTER_DELTA 0
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
static inline void __list_del_entry(struct list_head* entry)
{
__list_del(entry->prev, entry->next);
}
static inline void list_del(struct list_head* entry)
{
__list_del(entry->prev, entry->next);
//entry->next = LIST_POISON1;
//entry->prev = LIST_POISON2;
}
// 结构体成员的偏移地址(偏移量)
#define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/**
* 分析:
* @ptr为成员的地址
* @type为容器结构体类型
* @member为容器结构体的成员
* typeof( ((type *)0)->member )得到结构体type的member成员的类型
* const typeof( ((type *)0)->member ) *__mptr = (ptr) 指向容器结构体list成员的指针(地址)
* (type *)( (char *)__mptr - offsetof(type, member) 计算指向容器结构体实例的指针
*/
//#define container_of(ptr, type, member) (\
// { \
// const typeof( ((type *)0)->member ) *__mptr = (ptr); \
// (type *)( (char *)__mptr - _offsetof(type, member) );\
// }\
// )
#ifndef container_of
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *) &((type *)0)->member)
#endif
/**
* list_entry - get the struct for this entry
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/* 深刻掌握内核链表数据结构的设计思想 */
int main(int argc, char* argv[])
{
fox* red_fox = new fox;
red_fox->tail_length = 40;
red_fox->weight = 6;
red_fox->is_fantastic = false;
INIT_LIST_HEAD(&red_fox->list);
list_head* head = &red_fox->list;
fox* red_fox_1 = new fox;
red_fox_1->tail_length = 41;
red_fox_1->weight = 7;
red_fox_1->is_fantastic = true;
__list_add(&red_fox_1->list, head, head->next);
fox* tmp = list_entry(head, fox, list);
return 0;
}