第三章 表、棧和隊列(二)圖解list.h

分解list.h普通鏈表部分,繪圖鏈表,基本包含了雙向鏈表的各種操作。

定義

struct list_head {
	struct list_head *next, *prev;
};

1、創建一個鏈表基本單元

WRITE_ONCE本身是賦值操作,因爲關係到存儲對齊問題和CPU衝突問題,所以寫法和應用層不同

因爲只考慮算法,可以認爲是普通賦值,不做計較。以下三個代碼功能一致。

#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)
{
	WRITE_ONCE(list->next, list);
	list->prev = list;
}

 

2、插入

把DEBUG代碼刪掉了,對算法理解沒有影響,虛線都是舊指向,實現爲指向,這套API沒什麼說的

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;
	WRITE_ONCE(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);
}

在鏈表尾部插入幾個節點,滿足後進後出,這就是隊列,也是有鏈表衍生出來的。

鏈表頭部的前節點是鏈表的結尾,在鏈表結尾在插入一個元素,就是在隊列尾部插入元素

 

3、刪除,這三個API具有連續性,先把entry的左右節點鏈接起來,把entry左右指針指向空,最後可以做free

static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	WRITE_ONCE(prev->next, next);
}

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(entry);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

static inline void list_del_init(struct list_head *entry)
{
	__list_del_entry(entry);
	INIT_LIST_HEAD(entry);
}

 

4、鏈表置換

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;
}

static inline void list_replace_init(struct list_head *old,
					struct list_head *new)
{
	list_replace(old, new);
	INIT_LIST_HEAD(old);
}

 

5、鏈表移動到頭部或者尾部

static inline void list_move(struct list_head *list, struct list_head *head)
{
	__list_del_entry(list);
	list_add(list, head);
}

static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
	__list_del_entry(list);
	list_add_tail(list, head);
}

把一個鏈表上的節點搬移到另一個鏈表頭部或者尾部。

6、鏈表狀態

static inline int list_is_last(const struct list_head *list,
				const struct list_head *head)
{
	return list->next == head;
}

static inline int list_empty(const struct list_head *head)
{
	return READ_ONCE(head->next) == head;
}

static inline int list_empty_careful(const struct list_head *head)
{
	struct list_head *next = head->next;
	return (next == head) && (next == head->prev);
}

static inline int list_is_singular(const struct list_head *head)
{
	return !list_empty(head) && (head->next == head->prev);
}

鏈表在結尾,鏈表爲空、鏈表只有一個單元。這裏簡要說明一下list_empty只檢測了後面節點指向自身,list_empty_careful也檢測了前面節點

 

7、把隊列頭移動到隊列尾部

static inline void list_rotate_left(struct list_head *head)
{
	struct list_head *first;

	if (!list_empty(head)) {
		first = head->next;
		list_move_tail(first, head);
	}
}

很明顯,first是鏈表頭部,執行list_move_tail移動到了尾部。試想,如果一個線程在時間片內沒有執行完,放到隊列尾,應該是很廣泛的應用。

8、剪切鏈表

static inline void __list_cut_position(struct list_head *list,
		struct list_head *head, struct list_head *entry)
{
	struct list_head *new_first = entry->next;
	list->next = head->next;
	list->next->prev = list;
	list->prev = entry;
	entry->next = list;
	head->next = new_first;
	new_first->prev = head;
}

static inline void list_cut_position(struct list_head *list,
		struct list_head *head, struct list_head *entry)
{
	if (list_empty(head))
		return;
	if (list_is_singular(head) &&
		(head->next != entry && head != entry))
		return;
	if (entry == head)
		INIT_LIST_HEAD(list);
	else
		__list_cut_position(list, head, entry);
}

圖不太好畫,道理很簡單,從head頭部開始,以entry爲界限。entry前面的剪切到list上,後面的放在head上。

第二個判定條件if (list_is_singular(head) &&(head->next != entry && head != entry))不知道何時成立

9、剪切粘貼

static inline void __list_splice(const struct list_head *list,
				 struct list_head *prev,
				 struct list_head *next)
{
	struct list_head *first = list->next;
	struct list_head *last = list->prev;

	first->prev = prev;
	prev->next = first;

	last->next = next;
	next->prev = last;
}

static inline void list_splice(const struct list_head *list,
				struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head, head->next);
}

static inline void list_splice_tail(struct list_head *list,
				struct list_head *head)
{
	if (!list_empty(list))
		__list_splice(list, head->prev, head);
}

static inline void list_splice_init(struct list_head *list,
				    struct list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head, head->next);
		INIT_LIST_HEAD(list);
	}
}

static inline void list_splice_tail_init(struct list_head *list,
					 struct list_head *head)
{
	if (!list_empty(list)) {
		__list_splice(list, head->prev, head);
		INIT_LIST_HEAD(list);
	}
}

這段代碼很明顯,在prev和next之間,插入另一個鏈表從頭到尾(不包含啞結點)。頭部插入、尾部插入、提取未插入單元並指向自己,和插入函數沒有區別。

 

其實下面這部分可以另開一章,暫且放到這。

1、

鏈表的內嵌的對象

因爲這些是鏈表面向對象的基礎,有了這些結構,對象可以直接獲取鏈表屬性,類似於繼承。

#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 container_of(ptr, type, member) ({				\
	void *__mptr = (void *)(ptr);					\
	BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&	\
			 !__same_type(*(ptr), void),			\
			 "pointer type mismatch in container_of()");	\
	((type *)(__mptr - offsetof(type, member))); })

container_of來源於kernel.h文件。BUILD_BUG_ON_MSG是一條assert,是檢測函數,對運行和算法過程沒有影響。

__mptr獲得member的真實地址,然後減去從結構體到成員之間的距離,就是內嵌member結構體的起始位置。

 

這是鏈表面向對象的基礎,如下圖所示:


     

2、嵌入對象的前後單元訪問

#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_next_entry(pos, member) \
	list_entry((pos)->member.next, typeof(*(pos)), member)

#define list_prev_entry(pos, member) \
	list_entry((pos)->member.prev, typeof(*(pos)), member)

#define list_first_entry_or_null(ptr, type, member) ({ \
	struct list_head *head__ = (ptr); \
	struct list_head *pos__ = READ_ONCE(head__->next); \
	pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
})

 

3、鏈表的前後向遍歷和嵌入對象的向前向後遍歷

#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_prev(pos, head) \
	for (pos = (head)->prev; pos != (head); pos = pos->prev)

#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_prev_safe(pos, n, head) \
	for (pos = (head)->prev, n = pos->prev; \
	     pos != (head); \
	     pos = n, n = pos->prev)




#define list_for_each_entry(pos, head, member)				\
	for (pos = list_first_entry(head, typeof(*pos), member);	\
	     &pos->member != (head);					\
	     pos = list_next_entry(pos, member))

#define list_for_each_entry_reverse(pos, head, member)			\
	for (pos = list_last_entry(head, typeof(*pos), member);		\
	     &pos->member != (head); 					\
	     pos = list_prev_entry(pos, member))

#define list_for_each_entry_continue(pos, head, member) 		\
	for (pos = list_next_entry(pos, member);			\
	     &pos->member != (head);					\
	     pos = list_next_entry(pos, member))

#define list_for_each_entry_continue_reverse(pos, head, member)		\
	for (pos = list_prev_entry(pos, member);			\
	     &pos->member != (head);					\
	     pos = list_prev_entry(pos, member))

#define list_for_each_entry_from(pos, head, member) 			\
	for (; &pos->member != (head);					\
	     pos = list_next_entry(pos, member))

#define list_for_each_entry_from_reverse(pos, head, member)		\
	for (; &pos->member != (head);					\
	     pos = list_prev_entry(pos, member))

safe模式就是用兩個變量存儲前後兩個單元,層層推進,大概是不會因爲做什麼操作而丟失單元的安全吧。

list_for_each爲鏈表的前後向遍歷,list_for_each_entry爲嵌入對象的前後向遍歷

continue和form的區別在於從pos開始還是同pos的下一個繼續遍歷

後面幾個safe模式都與上面類似,不做說明了

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章