linux内核的链循环双链表简单使用
linux内核的循环双链表是非常经典的代码实现方式,个人总结如下:API多,像nginx(二者原理一致)、redis的链表在api数目上还差一点扩展性强,比如可以实现一个LRU、和线程池结合使用都可以,而redis的链表还差了点
demo代码如下:#include #include #include #include "list.h" #define MAX_ELE_NUMS (5) struct Stu { unsigned int ulId; struct list_head stNode; }; struct StuList { struct list_head stLi; }; // 打印单个Node信息 static DL_PrintNode(char *pcPrefix, struct Stu *pstStu) { assert((NULL != pcPrefix) && (NULL != pstStu)); printf("%s, id: %u, addr: %p, node: %p ", pcPrefix, pstStu->ulId, pstStu, &(pstStu->stNode)); } // 释放链表的所有数据 void DL_ClearStuLi(struct list_head *pstHead) { assert(NULL != pstHead); struct Stu *pstStu = NULL; struct Stu *pstNextStu = NULL; list_for_each_entry_safe(pstStu, pstNextStu, pstHead, stNode) { list_del(&(pstStu->stNode)); DL_PrintNode("free", pstStu); free((void *) pstStu); } } // 打印链表数据 void DL_PrintStuLi(struct list_head *pstHead, int lReverse) { assert(NULL != pstHead); struct Stu *pstStu = NULL; struct Stu *pstNextStu = NULL; if (lReverse) { list_for_each_entry_safe_reverse(pstStu, pstNextStu, pstHead, stNode) { DL_PrintNode("show backward", pstStu); } } else { list_for_each_entry_safe(pstStu, pstNextStu, pstHead, stNode) { DL_PrintNode("show forward", pstStu); } } } // 批量添加至链表中 void DL_BatchAddStuLiNodes(struct list_head *pstHead, struct Stu **ppstStus, unsigned int ulNodeNum, int lAddTail) { assert((NULL != pstHead) && (NULL != ppstStus)); struct Stu *pstStu = NULL; if (lAddTail) { for (unsigned int i = 0; i < ulNodeNum; ++i) { pstStu = ppstStus[i]; list_add_tail(&(pstStu->stNode), pstHead); } } else { for (unsigned int i = 0; i < ulNodeNum; ++i) { pstStu = ppstStus[i]; list_add(&(pstStu->stNode), pstHead); } } } void DL_BatchCreateNodes(struct Stu **ppstStus, unsigned int ulNodeNum, unsigned int ulStartId) { assert(NULL != ppstStus); for (unsigned int i = 0; i < ulNodeNum; ++i) { ppstStus[i] = (struct Stu *) malloc(sizeof(struct Stu)); assert(NULL != ppstStus[i]); ppstStus[i]->ulId = ulStartId; DL_PrintNode("create", ppstStus[i]); ulStartId++; } } // 从给定的元素开始遍历,包括给定的元素 void DL_IterateIncludeGivenEntry(struct list_head *pstHead, struct Stu *pstStu, int lReverse) { assert((NULL != pstHead) && (NULL != pstStu)); struct Stu *pstNextStu = NULL; if (lReverse) { list_for_each_entry_from_reverse(pstStu, pstHead, stNode) { DL_PrintNode("show backward", pstStu); } } else { list_for_each_entry_safe_from(pstStu, pstNextStu, pstHead, stNode) { DL_PrintNode("show forward", pstStu); } } } // 从给定的元素开始遍历, void DL_IterateExcludeGivenEntry(struct list_head *pstHead, struct Stu *pstStu, int lReverse) { assert((NULL != pstHead) && (NULL != pstStu)); struct Stu *pstNextStu = NULL; if (lReverse) { list_for_each_entry_continue_reverse(pstStu, pstHead, stNode) { DL_PrintNode("show backward", pstStu); } } else { list_for_each_entry_safe_continue(pstStu, pstNextStu, pstHead, stNode) { DL_PrintNode("show forward", pstStu); } } } void DL_CreateNoEmptyList(struct list_head *pstHead, unsigned int ulNodeNum) { assert((NULL != pstHead) && (0 != ulNodeNum)); struct Stu **ppstStus = NULL; ppstStus = (struct Stu **) malloc(sizeof(struct Stu *) * ulNodeNum); assert(NULL != ppstStus); INIT_LIST_HEAD(pstHead); DL_BatchCreateNodes(ppstStus, ulNodeNum, 0); DL_BatchAddStuLiNodes(pstHead, ppstStus, ulNodeNum, 1); free((void *) ppstStus); } /**********************************************************************************************************************/ void DL_IterateGivenPoint() { // 分配空间 struct Stu *apstStus[MAX_ELE_NUMS] = {0}; DL_BatchCreateNodes(apstStus, MAX_ELE_NUMS, 0); // 初始化链表 struct StuList stList = {0}; INIT_LIST_HEAD(&(stList.stLi)); // 链表尾部添加 DL_BatchAddStuLiNodes(&(stList.stLi), apstStus, MAX_ELE_NUMS, 1); //DL_IterateIncludeGivenEntry(&(stList.stLi), apstStus[2], 1); DL_IterateExcludeGivenEntry(&(stList.stLi), apstStus[2], 1); // 释放链表所有数据 DL_ClearStuLi(&(stList.stLi)); } // 简单使用 void DL_Basic() { // 分配空间 struct Stu *apstStus[MAX_ELE_NUMS] = {0}; DL_BatchCreateNodes(apstStus, MAX_ELE_NUMS, 0); // 初始化链表 struct StuList stList = {0}; INIT_LIST_HEAD(&(stList.stLi)); // 无数据,为空 printf("init, " "list emtpy: %s" " ", list_empty(&(stList.stLi)) ? "yes" : "no"); // 链表尾部添加 DL_BatchAddStuLiNodes(&(stList.stLi), apstStus, MAX_ELE_NUMS, 1); // 有数据不为空 printf("After Add, " "list emtpy: %s" " ", list_empty(&(stList.stLi)) ? "yes" : "no"); struct Stu *pstStu = NULL; struct list_head *pstNode = NULL; struct list_head *pstNextNode = NULL; // 打印所有的链表数据 DL_PrintStuLi(&(stList.stLi), 0); // 释放链表所有数据 DL_ClearStuLi(&(stList.stLi)); } void showBasicListInfo() { struct Stu *apstStus[MAX_ELE_NUMS] = {0}; DL_BatchCreateNodes(apstStus, MAX_ELE_NUMS, 0); // init list struct StuList stList = {0}; INIT_LIST_HEAD(&(stList.stLi)); // no data, is empty printf("init, " "list emtpy: %s" " ", list_empty(&(stList.stLi)) ? "yes" : "no"); // add node to list tail for (unsigned int i = 0; i < MAX_ELE_NUMS; ++i) { list_add_tail(&(apstStus[i]->stNode), &(stList.stLi)); printf("list empty: %s, " "list singular: %s, " "list first: %s, " "list last: %s, " "entry is head: %s " " ", list_empty(&(stList.stLi)) ? "yes" : "no", list_is_singular(&(stList.stLi)) ? "yes" : "no", list_is_first(&(apstStus[i]->stNode), &(stList.stLi)) ? "yes" : "no", list_is_last(&(apstStus[i]->stNode), &(stList.stLi)) ? "yes" : "no", list_entry_is_head(apstStus[i], &(stList.stLi), stNode) ? "yes" : "no" ); } printf("list first: %u" "list last: %u" " ", list_first_entry_or_null(&(stList.stLi), struct Stu, stNode)->ulId, list_last_entry(&(stList.stLi), struct Stu, stNode)->ulId ); DL_PrintStuLi(&(stList.stLi), 0); DL_ClearStuLi(&(stList.stLi)); } struct InvalidStruct { int ulInteger; unsigned long long dulDoubleInteger; char szName[20]; float fDataFloat; }; int main() { showBasicListInfo(); return 0; }
为此,提取的代码如下,可直接复制粘贴供以后使用,由于代码中使用了new作为变量名,在C++中会报错,可以自行将new替换掉其他变量名即可。/* SPDX-License-Identifier: GPL-2.0 */ #ifndef _R2M_LIST_H_ #define _R2M_LIST_H_ #include #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER) #endif /** * 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. * */ #ifndef container_of #define container_of(ptr, type, member) ({ void *__mptr = (void *)(ptr); ((type *)(__mptr - offsetof(type, member))); }) #endif #define POISON_POINTER_DELTA 0 /* * These are non-NULL pointers that will result in page faults * under normal circumstances, used to verify that nobody uses * non-initialized list entries. */ #define LIST_POISON1 ((void *) 0x100 + POISON_POINTER_DELTA) #define LIST_POISON2 ((void *) 0x122 + POISON_POINTER_DELTA) /* * Circular doubly linked list implementation. * * Some of the internal functions ("__xxx") are useful when * manipulating whole lists rather than single entries, as * sometimes we already know the next/prev entries and we can * generate better code by using them directly rather than * using the generic single-entry routines. */ struct list_head { struct list_head *next, *prev; }; #define LIST_HEAD_INIT(name) { &(name), &(name) } #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name) /** * INIT_LIST_HEAD - Initialize a list_head structure * @list: list_head structure to be initialized. * * Initializes the list_head to point to itself. If it is a list header, * the result is an empty list. */ static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list; list->prev = list; } #ifdef CONFIG_DEBUG_LIST extern bool __list_add_valid(struct list_head *new, struct list_head *prev, struct list_head *next); extern bool __list_del_entry_valid(struct list_head *entry); #else static inline bool __list_add_valid(struct list_head *new, struct list_head *prev, struct list_head *next) { return true; } static inline bool __list_del_entry_valid(struct list_head *entry) { return true; } #endif /* * Insert a new entry between two known consecutive entries. * * This is only for internal list manipulation where we know * the prev/next entries already! */ static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { if (!__list_add_valid(new, prev, next)) return; next->prev = new; new->next = next; new->prev = prev; prev->next = new; } /** * list_add - add a new entry * @new: new entry to be added * @head: list head to add it after * * Insert a new entry after the specified head. * This is good for implementing stacks. */ static inline void list_add(struct list_head *new, struct list_head *head) { __list_add(new, head, head->next); } /** * list_add_tail - add a new entry * @new: new entry to be added * @head: list head to add it before * * Insert a new entry before the specified head. * This is useful for implementing queues. */ static inline void list_add_tail(struct list_head *new, struct list_head *head) { __list_add(new, head->prev, head); } /* * Delete a list entry by making the prev/next entries * point to each other. * * This is only for internal list manipulation where we know * the prev/next entries already! */ static inline void __list_del(struct list_head * prev, struct list_head * next) { next->prev = prev; prev->next = next; } /* * Delete a list entry and clear the "prev" pointer. * * This is a special-purpose list clearing method used in the networking code * for lists allocated as per-cpu, where we don"t want to incur the extra * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this * needs to check the node "prev" pointer instead of calling list_empty(). */ static inline void __list_del_clearprev(struct list_head *entry) { __list_del(entry->prev, entry->next); entry->prev = NULL; } static inline void __list_del_entry(struct list_head *entry) { if (!__list_del_entry_valid(entry)) return; __list_del(entry->prev, entry->next); } /** * list_del - deletes entry from list. * @entry: the element to delete from the list. * Note: list_empty() on entry does not return true after this, the entry is * in an undefined state. */ static inline void list_del(struct list_head *entry) { __list_del_entry(entry); entry->next = LIST_POISON1; entry->prev = LIST_POISON2; } /** * list_replace - replace old entry by new one * @old : the element to be replaced * @new : the new element to insert * * If @old was empty, it will be overwritten. */ 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_init - replace old entry by new one and initialize the old one * @old : the element to be replaced * @new : the new element to insert * * If @old was empty, it will be overwritten. */ static inline void list_replace_init(struct list_head *old, struct list_head *new) { list_replace(old, new); INIT_LIST_HEAD(old); } /** * list_swap - replace entry1 with entry2 and re-add entry1 at entry2"s position * @entry1: the location to place entry2 * @entry2: the location to place entry1 */ static inline void list_swap(struct list_head *entry1, struct list_head *entry2) { struct list_head *pos = entry2->prev; list_del(entry2); list_replace(entry1, entry2); if (pos == entry1) pos = entry2; list_add(entry1, pos); } /** * list_del_init - deletes entry from list and reinitialize it. * @entry: the element to delete from the list. */ static inline void list_del_init(struct list_head *entry) { __list_del_entry(entry); INIT_LIST_HEAD(entry); } /** * list_move - delete from one list and add as another"s head * @list: the entry to move * @head: the head that will precede our entry */ static inline void list_move(struct list_head *list, struct list_head *head) { __list_del_entry(list); list_add(list, head); } /** * list_move_tail - delete from one list and add as another"s tail * @list: the entry to move * @head: the head that will follow our entry */ static inline void list_move_tail(struct list_head *list, struct list_head *head) { __list_del_entry(list); list_add_tail(list, head); } /** * list_bulk_move_tail - move a subsection of a list to its tail * @head: the head that will follow our entry * @first: first entry to move * @last: last entry to move, can be the same as first * * Move all entries between @first and including @last before @head. * All three entries must belong to the same linked list. */ static inline void list_bulk_move_tail(struct list_head *head, struct list_head *first, struct list_head *last) { first->prev->next = last->next; last->next->prev = first->prev; head->prev->next = first; first->prev = head->prev; last->next = head; head->prev = last; } /** * list_is_first -- tests whether @list is the first entry in list @head * @list: the entry to test * @head: the head of the list */ static inline int list_is_first(const struct list_head *list, const struct list_head *head) { return list->prev == head; } /** * list_is_last - tests whether @list is the last entry in list @head * @list: the entry to test * @head: the head of the list */ static inline int list_is_last(const struct list_head *list, const struct list_head *head) { return list->next == head; } /** * list_empty - tests whether a list is empty * @head: the list to test. */ static inline int list_empty(const struct list_head *head) { return head->next == head; } /** * list_del_init_careful - deletes entry from list and reinitialize it. * @entry: the element to delete from the list. * * This is the same as list_del_init(), except designed to be used * together with list_empty_careful() in a way to guarantee ordering * of other memory operations. * * Any memory operations done before a list_del_init_careful() are * guaranteed to be visible after a list_empty_careful() test. */ static inline void list_del_init_careful(struct list_head *entry) { /* unsupported */ #if 0 __list_del_entry(entry); entry->prev = entry; smp_store_release(&entry->next, entry); #endif } /** * list_empty_careful - tests whether a list is empty and not being modified * @head: the list to test * * Description: * tests whether a list is empty _and_ checks that no other CPU might be * in the process of modifying either member (next or prev) * * NOTE: using list_empty_careful() without synchronization * can only be safe if the only activity that can happen * to the list entry is list_del_init(). Eg. it cannot be used * if another CPU could re-list_add() it. */ static inline int list_empty_careful(const struct list_head *head) { /* unsupported */ #if 0 struct list_head *next = smp_load_acquire(&head->next); return (next == head) && (next == head->prev); #endif return 0; } /** * list_rotate_left - rotate the list to the left * @head: the head of the list */ 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); } } /** * list_rotate_to_front() - Rotate list to specific item. * @list: The desired new front of the list. * @head: The head of the list. * * Rotates list so that @list becomes the new front of the list. */ static inline void list_rotate_to_front(struct list_head *list, struct list_head *head) { /* * Deletes the list head from the list denoted by @head and * places it as the tail of @list, this effectively rotates the * list so that @list is at the front. */ list_move_tail(head, list); } /** * list_is_singular - tests whether a list has just one entry. * @head: the list to test. */ static inline int list_is_singular(const struct list_head *head) { return !list_empty(head) && (head->next == head->prev); } 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; } /** * list_cut_position - cut a list into two * @list: a new list to add all removed entries * @head: a list with entries * @entry: an entry within head, could be the head itself * and if so we won"t cut the list * * This helper moves the initial part of @head, up to and * including @entry, from @head to @list. You should * pass on @entry an element you know is on @head. @list * should be an empty list or a list you do not care about * losing its data. * */ 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); } /** * list_cut_before - cut a list into two, before given entry * @list: a new list to add all removed entries * @head: a list with entries * @entry: an entry within head, could be the head itself * * This helper moves the initial part of @head, up to but * excluding @entry, from @head to @list. You should pass * in @entry an element you know is on @head. @list should * be an empty list or a list you do not care about losing * its data. * If @entry == @head, all entries on @head are moved to * @list. */ static inline void list_cut_before(struct list_head *list, struct list_head *head, struct list_head *entry) { if (head->next == entry) { INIT_LIST_HEAD(list); return; } list->next = head->next; list->next->prev = list; list->prev = entry->prev; list->prev->next = list; head->next = entry; entry->prev = head; } 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; } /** * list_splice - join two lists, this is designed for stacks * @list: the new list to add. * @head: the place to add it in the first list. */ static inline void list_splice(const struct list_head *list, struct list_head *head) { if (!list_empty(list)) __list_splice(list, head, head->next); } /** * list_splice_tail - join two lists, each list being a queue * @list: the new list to add. * @head: the place to add it in the first list. */ static inline void list_splice_tail(struct list_head *list, struct list_head *head) { if (!list_empty(list)) __list_splice(list, head->prev, head); } /** * list_splice_init - join two lists and reinitialise the emptied list. * @list: the new list to add. * @head: the place to add it in the first list. * * The list at @list is reinitialised */ 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); } } /** * list_splice_tail_init - join two lists and reinitialise the emptied list * @list: the new list to add. * @head: the place to add it in the first list. * * Each of the lists is a queue. * The list at @list is reinitialised */ 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); } } /** * list_entry - get the struct for this entry * @ptr: the &struct list_head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. */ #define list_entry(ptr, type, member) container_of(ptr, type, member) /** * list_first_entry - get the first element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. * * Note, that list is expected to be not empty. */ #define list_first_entry(ptr, type, member) list_entry((ptr)->next, type, member) /** * list_last_entry - get the last element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. * * Note, that list is expected to be not empty. */ #define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member) /** * list_first_entry_or_null - get the first element from a list * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. * * Note that if the list is empty, it returns NULL. */ #define list_first_entry_or_null(ptr, type, member) ({ struct list_head *head__ = (ptr); struct list_head *pos__ = head__->next; pos__ != head__ ? list_entry(pos__, type, member) : NULL; }) /** * list_next_entry - get the next element in list * @pos: the type * to cursor * @member: the name of the list_head within the struct. */ #define list_next_entry(pos, member) list_entry((pos)->member.next, typeof(*(pos)), member) /** * list_prev_entry - get the prev element in list * @pos: the type * to cursor * @member: the name of the list_head within the struct. */ #define list_prev_entry(pos, member) list_entry((pos)->member.prev, typeof(*(pos)), member) /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) /** * list_for_each_continue - continue iteration over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. * * Continue to iterate over a list, continuing after the current position. */ #define list_for_each_continue(pos, head) for (pos = pos->next; pos != (head); pos = pos->next) /** * list_for_each_prev - iterate over a list backwards * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each_prev(pos, head) for (pos = (head)->prev; pos != (head); pos = pos->prev) /** * list_for_each_safe - iterate over a list safe against removal of list entry * @pos: the &struct list_head to use as a loop cursor. * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ #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_prev_safe - iterate over a list backwards safe against removal of list entry * @pos: the &struct list_head to use as a loop cursor. * @n: another &struct list_head to use as temporary storage * @head: the head for your list. */ #define list_for_each_prev_safe(pos, n, head) for (pos = (head)->prev, n = pos->prev; pos != (head); pos = n, n = pos->prev) /** * list_entry_is_head - test if the entry points to the head of the list * @pos: the type * to cursor * @head: the head for your list. * @member: the name of the list_head within the struct. */ #define list_entry_is_head(pos, head, member) (&pos->member == (head)) /** * list_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. */ #define list_for_each_entry(pos, head, member) for (pos = list_first_entry(head, typeof(*pos), member); !list_entry_is_head(pos, head, member); pos = list_next_entry(pos, member)) /** * list_for_each_entry_reverse - iterate backwards over list of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. */ #define list_for_each_entry_reverse(pos, head, member) for (pos = list_last_entry(head, typeof(*pos), member); !list_entry_is_head(pos, head, member); pos = list_prev_entry(pos, member)) /** * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() * @pos: the type * to use as a start point * @head: the head of the list * @member: the name of the list_head within the struct. * * Prepares a pos entry for use as a start point in list_for_each_entry_continue(). */ #define list_prepare_entry(pos, head, member) ((pos) ? : list_entry(head, typeof(*pos), member)) /** * list_for_each_entry_continue - continue iteration over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. * * Continue to iterate over list of given type, continuing after * the current position. */ #define list_for_each_entry_continue(pos, head, member) for (pos = list_next_entry(pos, member); !list_entry_is_head(pos, head, member); pos = list_next_entry(pos, member)) /** * list_for_each_entry_continue_reverse - iterate backwards from the given point * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. * * Start to iterate over list of given type backwards, continuing after * the current position. */ #define list_for_each_entry_continue_reverse(pos, head, member) for (pos = list_prev_entry(pos, member); !list_entry_is_head(pos, head, member); pos = list_prev_entry(pos, member)) /** * list_for_each_entry_from - iterate over list of given type from the current point * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate over list of given type, continuing from current position. */ #define list_for_each_entry_from(pos, head, member) for (; !list_entry_is_head(pos, head, member); pos = list_next_entry(pos, member)) /** * list_for_each_entry_from_reverse - iterate backwards over list of given type * from the current point * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate backwards over list of given type, continuing from current position. */ #define list_for_each_entry_from_reverse(pos, head, member) for (; !list_entry_is_head(pos, head, member); pos = list_prev_entry(pos, member)) /** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. * @member: the name of the list_head within the struct. */ #define list_for_each_entry_safe(pos, n, head, member) for (pos = list_first_entry(head, typeof(*pos), member), n = list_next_entry(pos, member); !list_entry_is_head(pos, head, member); pos = n, n = list_next_entry(n, member)) /** * list_for_each_entry_safe_continue - continue list iteration safe against removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate over list of given type, continuing after current point, * safe against removal of list entry. */ #define list_for_each_entry_safe_continue(pos, n, head, member) for (pos = list_next_entry(pos, member), n = list_next_entry(pos, member); !list_entry_is_head(pos, head, member); pos = n, n = list_next_entry(n, member)) /** * list_for_each_entry_safe_from - iterate over list from current point safe against removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate over list of given type from current point, safe against * removal of list entry. */ #define list_for_each_entry_safe_from(pos, n, head, member) for (n = list_next_entry(pos, member); !list_entry_is_head(pos, head, member); pos = n, n = list_next_entry(n, member)) /** * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal * @pos: the type * to use as a loop cursor. * @n: another type * to use as temporary storage * @head: the head for your list. * @member: the name of the list_head within the struct. * * Iterate backwards over list of given type, safe against removal * of list entry. */ #define list_for_each_entry_safe_reverse(pos, n, head, member) for (pos = list_last_entry(head, typeof(*pos), member), n = list_prev_entry(pos, member); !list_entry_is_head(pos, head, member); pos = n, n = list_prev_entry(n, member)) /** * list_safe_reset_next - reset a stale list_for_each_entry_safe loop * @pos: the loop cursor used in the list_for_each_entry_safe loop * @n: temporary storage used in list_for_each_entry_safe * @member: the name of the list_head within the struct. * * list_safe_reset_next is not safe to use in general if the list may be * modified concurrently (eg. the lock is dropped in the loop body). An * exception to this is if the cursor element (pos) is pinned in the list, * and list_safe_reset_next is called after re-taking the lock and before * completing the current iteration of the loop body. */ #define list_safe_reset_next(pos, n, member) n = list_next_entry(pos, member) /* * Double linked lists with a single pointer list head. * Mostly useful for hash tables where the two pointer list head is * too wasteful. * You lose the ability to access the tail in O(1). */ struct hlist_head { struct hlist_node *first; }; struct hlist_node { struct hlist_node *next, **pprev; }; #define HLIST_HEAD_INIT { .first = NULL } #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) static inline void INIT_HLIST_NODE(struct hlist_node *h) { h->next = NULL; h->pprev = NULL; } /** * hlist_unhashed - Has node been removed from list and reinitialized? * @h: Node to be checked * * Not that not all removal functions will leave a node in unhashed * state. For example, hlist_nulls_del_init_rcu() does leave the * node in unhashed state, but hlist_nulls_del() does not. */ static inline int hlist_unhashed(const struct hlist_node *h) { return !h->pprev; } /** * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use * @h: Node to be checked * * This variant of hlist_unhashed() must be used in lockless contexts * to avoid potential load-tearing. The READ_ONCE() is paired with the * various WRITE_ONCE() in hlist helpers that are defined below. */ static inline int hlist_unhashed_lockless(const struct hlist_node *h) { return !h->pprev; } /** * hlist_empty - Is the specified hlist_head structure an empty hlist? * @h: Structure to check. */ static inline int hlist_empty(const struct hlist_head *h) { return !h->first; } static inline void __hlist_del(struct hlist_node *n) { struct hlist_node *next = n->next; struct hlist_node **pprev = n->pprev; *pprev = next; if (next) next->pprev = pprev; } /** * hlist_del - Delete the specified hlist_node from its list * @n: Node to delete. * * Note that this function leaves the node in hashed state. Use * hlist_del_init() or similar instead to unhash @n. */ static inline void hlist_del(struct hlist_node *n) { __hlist_del(n); n->next = LIST_POISON1; n->pprev = LIST_POISON2; } /** * hlist_del_init - Delete the specified hlist_node from its list and initialize * @n: Node to delete. * * Note that this function leaves the node in unhashed state. */ static inline void hlist_del_init(struct hlist_node *n) { if (!hlist_unhashed(n)) { __hlist_del(n); INIT_HLIST_NODE(n); } } /** * hlist_add_head - add a new entry at the beginning of the hlist * @n: new entry to be added * @h: hlist head to add it after * * Insert a new entry after the specified head. * This is good for implementing stacks. */ static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) { struct hlist_node *first = h->first; n->next = first; if (first) first->pprev = &n->next; h->first = n; n->pprev = &h->first; } /** * hlist_add_before - add a new entry before the one specified * @n: new entry to be added * @next: hlist node to add it before, which must be non-NULL */ static inline void hlist_add_before(struct hlist_node *n, struct hlist_node *next) { n->pprev = next->pprev; n->next = next; next->pprev = &n->next; *(n->pprev) = n; } /** * hlist_add_behind - add a new entry after the one specified * @n: new entry to be added * @prev: hlist node to add it after, which must be non-NULL */ static inline void hlist_add_behind(struct hlist_node *n, struct hlist_node *prev) { n->next = prev->next; prev->next = n; n->pprev = &prev->next; if (n->next) n->next->pprev = &n->next; } /** * hlist_add_fake - create a fake hlist consisting of a single headless node * @n: Node to make a fake list out of * * This makes @n appear to be its own predecessor on a headless hlist. * The point of this is to allow things like hlist_del() to work correctly * in cases where there is no list. */ static inline void hlist_add_fake(struct hlist_node *n) { n->pprev = &n->next; } /** * hlist_fake: Is this node a fake hlist? * @h: Node to check for being a self-referential fake hlist. */ static inline bool hlist_fake(struct hlist_node *h) { return h->pprev == &h->next; } /** * hlist_is_singular_node - is node the only element of the specified hlist? * @n: Node to check for singularity. * @h: Header for potentially singular list. * * Check whether the node is the only node of the head without * accessing head, thus avoiding unnecessary cache misses. */ static inline bool hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h) { return !n->next && n->pprev == &h->first; } /** * hlist_move_list - Move an hlist * @old: hlist_head for old list. * @new: hlist_head for new list. * * Move a list from one list head to another. Fixup the pprev * reference of the first entry if it exists. */ static inline void hlist_move_list(struct hlist_head *old, struct hlist_head *new) { new->first = old->first; if (new->first) new->first->pprev = &new->first; old->first = NULL; } #define hlist_entry(ptr, type, member) container_of(ptr,type,member) #define hlist_for_each(pos, head) for (pos = (head)->first; pos ; pos = pos->next) #define hlist_for_each_safe(pos, n, head) for (pos = (head)->first; pos && ({ n = pos->next; 1; }); pos = n) #define hlist_entry_safe(ptr, type, member) ({ typeof(ptr) ____ptr = (ptr); ____ptr ? hlist_entry(____ptr, type, member) : NULL; }) /** * hlist_for_each_entry - iterate over list of given type * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the hlist_node within the struct. */ #define hlist_for_each_entry(pos, head, member) for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); pos; pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) /** * hlist_for_each_entry_continue - iterate over a hlist continuing after current point * @pos: the type * to use as a loop cursor. * @member: the name of the hlist_node within the struct. */ #define hlist_for_each_entry_continue(pos, member) for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); pos; pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) /** * hlist_for_each_entry_from - iterate over a hlist continuing from current point * @pos: the type * to use as a loop cursor. * @member: the name of the hlist_node within the struct. */ #define hlist_for_each_entry_from(pos, member) for (; pos; pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) /** * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @pos: the type * to use as a loop cursor. * @n: a &struct hlist_node to use as temporary storage * @head: the head for your list. * @member: the name of the hlist_node within the struct. */ #define hlist_for_each_entry_safe(pos, n, head, member) for (pos = hlist_entry_safe((head)->first, typeof(*pos), member); pos && ({ n = pos->member.next; 1; }); pos = hlist_entry_safe(n, typeof(*pos), member)) #endif
实体店的小米手机比官网便宜,是真的吗?关注今日头条找靓机,每天分享各种数码资讯,互动还送奖品哦!这个问题应该说,部分是真的。什么意思呢?如果你去的是小米直营门店,就是那种一看装修就是小米风的,正儿八经的大店,手机售价和
洗碗机为什么没有在日常生活中得到普及?你可能没用过洗碗机,但你一定听说过洗碗机因为洗碗这件事,着实让人讨厌。2016年,洗碗机如同一匹黑马,跃马扬鞭跨进了数十亿市场。根据中怡康监测的数据显示,去年中国洗碗机市场零售额达
请问哪家平台,投资基金更好?支付时一直用的都是支付宝,余额宝里放点零钱,有一次不知道怎么操作的买了一支债基,天天看着没啥大变化,基本没有收益,慢慢就开始了解基金,用支付宝开始买基金。后来也听人推荐下载过天天基
比特币怎么买,新人该选什么平台?比特币作为一款没有国界的投资品,它的交易平台种类也是比较多的,从大的属性上来看,基本上分为中心化的交易平台以及去中心化的交易平台。在2019年以前,中心化的交易平台基本上占据到了比
为什么现在人宁愿把钱放网上支付宝平台,也不放在银行?原因肯定是多方面的,有的认为安全,有的认为利息高,有的认为支付方便感谢邀请!的确现在很多人宁可把钱放在支付宝也不愿意放在银行。就比如说我个人,虽然没什么钱,但是有点一块两块的毛票也
有哪些闷声发大财的行业?我堂姐就是闷声发大财的人。堂姐以前是农村的赤脚医生,今年60岁。她再婚的老公是个转业军人,复员到了山西某地,在当地娶妻结婚。女方家是世代中医,堂姐夫在帮忙时,也知道了很多中药配方。
创业,如果条件允许,你最想开一个什么店?我的想法还是开超市或大卖场。线上消费红火一阵后,开始逐渐回归,线下曾经惨淡经营的状况也开始有了好转。很多东西在线上看不见摸不着,买到手不是大了就是小了,不是颜色不对就是其他原因,各
微信号怎么改回自己手机号呢?微信是当今你我日常生活聊天中必不可少的一款软件,有的人在申请微信号是可能用的是别人的手机号,也有的是更换了手机号,需要将新手机号绑定微信。这里值得注意的是微信号一旦设置是更改不了的
准备换手机,要求屏幕好,拍照真实,没道德绑架的好手机,请推荐?自从手机不再成为奢侈品以来,其功能外观跑分价格就成为人们要考虑的四大指标。如果题主仅是拍照和屏幕要求较高,建议买小米系列足矣!1,苹果手机虽然流畅性好,但操作习惯我确实不敢恭维。外
如何让手机来电闪光?手机来电时让手机闪光,设置方法其实是很简单的,具体操作如下这里以小米手机为例,首先打开电话,接着点击图标,新界面中选择电话设置,接着选择来电时状态,之后把来电闪光灯打开就可以了,是
vivoX50Pro有骁龙865和ufs3。1,性能怎么样?感谢您的阅读!vivoX50Pro有骁龙865处理器和UFS3。1闪存规格,到底性能怎么样呢?我们知道一款手机如果有了骁龙865处理器,说明这款手机的性能确实不差。你得知道骁龙86