Libubox/list
【Libubox/list】list是创建双向循环链表的实用程序。只需向结构中添加struct list_head。它附带了很多迭代器宏。下面逐个看一下。
1.container_of宏:知道结构体类型,成员地址求结构体地址。其中ptr:成员地址,typ:结构体类型,member:成员指示器
#ifndef container_of
#define container_of(ptr, type, member)\
({\
const __typeof__(((type *) NULL)->member) *__mptr = (ptr);
\
(type *) ((char *) __mptr - offsetof(type, member));
\
})
#endif
2.结构体和创建变量宏,#define LIST_HEAD_INIT(name) { &(name), &(name) }这句非常经典
struct list_head {
struct list_head *next;
struct list_head *prev;
};
//创建变量宏
#define LIST_HEAD_INIT(name) { &(name), &(name) }
3.创建链表
#define LIST_HEAD_INIT(name) { &(name), &(name) }
4.判断链表是否为空
static inline bool
list_empty(const struct list_head *head)
{
return (head->next == head);
}
5.判断list节点是否为首节点
static inline bool
list_is_first(const struct list_head *list,
const struct list_head *head)
{
return list->prev == head;
}
6.判断list是否为尾节点
static inline void
list_add_tail(struct list_head *_new, struct list_head *head)
{
_list_add(_new, head->prev, head);
}
7.删除节点
static inline void
_list_del(struct list_head *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}static inline void
list_del(struct list_head *entry)
{
_list_del(entry);
entry->next = entry->prev = NULL;
}
8.添加节点
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;
}
9.将节点entry从链表中分出来(entry未释放)
static inline void
list_del_init(struct list_head *entry)
{
_list_del(entry);
INIT_LIST_HEAD(entry);
}
10.链表遍历宏
//遍历链表
#define list_for_each(p, head)\
for (p = (head)->next;
p != (head);
p = p->next)
11.链表遍历的实现
#define list_entry(ptr, type, field) container_of(ptr, type, field)
#define list_first_entry(ptr, type, field) list_entry((ptr)->next, type, field)
#define list_last_entry(ptr, type, field) list_entry((ptr)->prev, type, field)
//迭代
#define list_for_each_safe(p, n, head)\
for (p = (head)->next, n = p->next;
p != (head);
p = n, n = p->next)#define list_for_each_entry(p, h, field)\
for (p = list_first_entry(h, __typeof__(*p), field);
&p->field != (h);
\
p = list_entry(p->field.next, __typeof__(*p), field))#define list_for_each_entry_safe(p, n, h, field)\
for (p = list_first_entry(h, __typeof__(*p), field),\
n = list_entry(p->field.next, __typeof__(*p), field);
&p->field != (h);
\
p = n, n = list_entry(n->field.next, __typeof__(*n), field))#define list_for_each_entry_reverse(p, h, field)\
for (p = list_last_entry(h, __typeof__(*p), field);
&p->field != (h);
\
p = list_entry(p->field.prev, __typeof__(*p), field))#define list_for_each_prev(p, h) for (p = (h)->prev;
p != (h);
p = p->prev)
#define list_for_each_prev_safe(p, n, h) for (p = (h)->prev, n = p->prev;
p != (h);
p = n, n = p->prev)
12.链表插入和拼接
//从头插入节点
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到头部
static inline void
list_move(struct list_head *list, struct list_head *head)
{
_list_del(list);
list_add(list, head);
}
//移动list到尾部
static inline void
list_move_tail(struct list_head *entry, struct list_head *head)
{
_list_del(entry);
list_add_tail(entry, head);
}
//链表拼接
static inline void
_list_splice(const struct list_head *list, struct list_head *prev,
struct list_head *next)
{
struct list_head *first;
struct list_head *last;
if (list_empty(list))
return;
first = list->next;
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)
{
_list_splice(list, head, head->next);
}static inline void
list_splice_tail(struct list_head *list, struct list_head *head)
{
_list_splice(list, head->prev, head);
}static inline void
list_splice_init(struct list_head *list, struct list_head *head)
{
_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)
{
_list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
推荐阅读
- 宽容谁
- 我要做大厨
- 增长黑客的海盗法则
- 画画吗()
- 2019-02-13——今天谈梦想()
- 远去的风筝
- 三十年后的广场舞大爷
- 叙述作文
- 20190302|20190302 复盘翻盘
- 学无止境,人生还很长