標籤:blog code c ext http int
linux中的經典宏定義offsetof
定義:offsetof在linux核心的include/linux/stddef.h中定義。
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
說明:獲得結構體(TYPE)的變數成員(MEMBER)在此結構體中的位移量。
(01) ( (TYPE *)0 ) 將零轉型為TYPE類型指標,即TYPE類型的指標的地址是0。
(02) ((TYPE *)0)->MEMBER 訪問結構中的資料成員。
(03) &( ( (TYPE *)0 )->MEMBER ) 取出資料成員的地址。由於TYPE的地址是0,這裡擷取到的地址就是相對MEMBER在TYPE中的位移。
(04) (size_t)(&(((TYPE*)0)->MEMBER)) 結果轉換類型。對於32位系統而言,size_t是unsigned int類型;對於64位系統而言,size_t是unsigned long類型。
container_of
定義:container_of在linux核心的include/linux/kernel.h中定義。
#define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
說明:根據"結構體(type)變數"中的"域成員變數(member)的指標(ptr)"來擷取指向整個結構體變數的指標。
(01) typeof( ( (type *)0)->member ) 取出member成員的變數類型。
(02) const typeof( ((type *)0)->member ) *__mptr = (ptr) 定義變數__mptr指標,並將ptr賦值給__mptr。經過這一步,__mptr為member資料類型的常量指標,其指向ptr所指向的地址。
(03) (char *)__mptr 將__mptr轉換為位元組型指標。
(04) offsetof(type,member)) 就是擷取"member成員"在"結構體type"中的位置位移。
(05) (char *)__mptr - offsetof(type,member)) 就是用來擷取"結構體type"的指標的起始地址(為char *型指標)。
(06) (type *)( (char *)__mptr - offsetof(type,member) ) 就是將"char *類型的結構體type的指標"轉換為"type *類型的結構體type的指標"。
Linux雙向鏈表實現
Linux雙向鏈表的定義主要涉及到兩個檔案:
include/linux/types.h
include/linux/list.h
(01). 節點定義
struct list_head { struct list_head *next, *prev;};
雖然名稱list_head,但是它既是雙向鏈表的表頭,也代表雙向鏈表的節點。
(02). 初始化節點
#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;
}
LIST_HEAD的作用是定義表頭(節點):建立雙向鏈表表頭name,並設定name的前繼節點和後繼節點都是指向name本身。
LIST_HEAD_INIT的作用是初始化節點:設定name節點的前繼節點和後繼節點都是指向name本身。
INIT_LIST_HEAD和LIST_HEAD_INIT一樣,是初始化節點:將list節點的前繼節點和後繼節點都是指向list本身。
(03). 添加節點
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_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
__list_add(new, prev, next)的作用是添加節點:將new插入到prev和next之間。在linux中,以"__"開頭的函數意味著是核心的內部介面,外部不應該調用該介面。
list_add(new, head)的作用是添加new節點:將new添加到head之後,是new稱為head的後繼節點。
list_add_tail(new, head)的作用是添加new節點:將new添加到head之前,即將new添加到雙鏈表的末尾。
(04). 刪除節點
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->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);
}
__list_del(prev, next) 和__list_del_entry(entry)都是linux核心的內部介面。
__list_del(prev, next) 的作用是從雙鏈表中刪除prev和next之間的節點。
__list_del_entry(entry) 的作用是從雙鏈表中刪除entry節點。
list_del(entry) 和 list_del_init(entry)是linux核心的對外介面。
list_del(entry) 的作用是從雙鏈表中刪除entry節點。
list_del_init(entry) 的作用是從雙鏈表中刪除entry節點,並將entry節點的前繼節點和後繼節點都指向entry本身。
(05). 替換節點
static inline void list_replace(struct list_head *old, struct list_head *new){ new->next = old->next; new->next->prev = new; new->prev = old->prev; new->prev->next = new;}
list_replace(old, new)的作用是用new節點替換old節點。
(06). 判斷雙鏈表是否為空白
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
list_empty(head)的作用是判斷雙鏈表是否為空白。它是通過區分"表頭的後繼節點"是不是"表頭本身"來進行判斷的。
(07). 擷取節點
#define list_entry(ptr, type, member) container_of(ptr, type, member)
list_entry(ptr, type, member) 實際上是調用的container_of宏。
它的作用是:根據"結構體(type)變數"中的"域成員變數(member)的指標(ptr)"來擷取指向整個結構體變數的指標。
(08). 遍曆節點
#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)
list_for_each(pos, head)和list_for_each_safe(pos, n, head)的作用都是遍曆鏈表。但是它們的用途不一樣!
list_for_each(pos, head)通常用於擷取節點,而不能用到刪除節點的情境。
list_for_each_safe(pos, n, head)通常刪除節點的情境。