#ifndef _LINUX_LIST_H1#define _LINUX_LIST_H23/*4* Simple doubly linked list implementation.5*6* Some of the internal functions ("__xxx") are useful when7* manipulating whole lists rather than single entries, as8* sometimes we already know the next/prev entries and we can9* generate better code by using them directly rather than10* using the generic single-entry routines.11*/1213struct list_head {14struct list_head *next, *prev;15};1617#define LIST_HEAD_INIT(name) { &(name), &(name) }1819#define LIST_HEAD(name) \20struct list_head name = LIST_HEAD_INIT(name)2122#define INIT_LIST_HEAD(ptr) do { \23(ptr)->next = (ptr); (ptr)->prev = (ptr); \24} while (0)2526/*27* Insert a new entry between two known consecutive entries.28*29* This is only for internal list manipulation where we know30* the prev/next entries already!31*/32static inline void __list_add(struct list_head *nlh,33struct list_head *prev,34struct list_head *next)35{36next->prev = nlh;37nlh->next = next;38nlh->prev = prev;39prev->next = nlh;40}4142/**43* list_add - add a new entry44* @new: new entry to be added45* @head: list head to add it after46*47* Insert a new entry after the specified head.48* This is good for implementing stacks.49*/50static inline void list_add(struct list_head *nlh, struct list_head *head)51{52__list_add(nlh, head, head->next);53}5455/**56* list_add_tail - add a new entry57* @new: new entry to be added58* @head: list head to add it before59*60* Insert a new entry before the specified head.61* This is useful for implementing queues.62*/63static inline void list_add_tail(struct list_head *nlh, struct list_head *head)64{65__list_add(nlh, head->prev, head);66}6768/*69* Delete a list entry by making the prev/next entries70* point to each other.71*72* This is only for internal list manipulation where we know73* the prev/next entries already!74*/75static inline void __list_del(struct list_head *prev, struct list_head *next)76{77next->prev = prev;78prev->next = next;79}8081/**82* list_del - deletes entry from list.83* @entry: the element to delete from the list.84* Note: list_empty on entry does not return true after this, the entry is in an undefined state.85*/86static inline void list_del(struct list_head *entry)87{88__list_del(entry->prev, entry->next);89entry->next = NULL;90entry->prev = NULL;91}9293/**94* list_del_init - deletes entry from list and reinitialize it.95* @entry: the element to delete from the list.96*/97static inline void list_del_init(struct list_head *entry)98{99__list_del(entry->prev, entry->next);100INIT_LIST_HEAD(entry);101}102103/**104* list_move - delete from one list and add as another's head105* @list: the entry to move106* @head: the head that will precede our entry107*/108static inline void list_move(struct list_head *list, struct list_head *head)109{110__list_del(list->prev, list->next);111list_add(list, head);112}113114/**115* list_move_tail - delete from one list and add as another's tail116* @list: the entry to move117* @head: the head that will follow our entry118*/119static inline void list_move_tail(struct list_head *list,120struct list_head *head)121{122__list_del(list->prev, list->next);123list_add_tail(list, head);124}125126/**127* list_empty - tests whether a list is empty128* @head: the list to test.129*/130static inline int list_empty(struct list_head *head)131{132return head->next == head;133}134135static inline void __list_splice(struct list_head *list,136struct list_head *head)137{138struct list_head *first = list->next;139struct list_head *last = list->prev;140struct list_head *at = head->next;141142first->prev = head;143head->next = first;144145last->next = at;146at->prev = last;147}148149/**150* list_splice - join two lists151* @list: the new list to add.152* @head: the place to add it in the first list.153*/154static inline void list_splice(struct list_head *list, struct list_head *head)155{156if (!list_empty(list))157__list_splice(list, head);158}159160/**161* list_splice_init - join two lists and reinitialise the emptied list.162* @list: the new list to add.163* @head: the place to add it in the first list.164*165* The list at @list is reinitialised166*/167static inline void list_splice_init(struct list_head *list,168struct list_head *head)169{170if (!list_empty(list)) {171__list_splice(list, head);172INIT_LIST_HEAD(list);173}174}175176/**177* list_entry - get the struct for this entry178* @ptr: the &struct list_head pointer.179* @type: the type of the struct this is embedded in.180* @member: the name of the list_struct within the struct.181*/182#define list_entry(ptr, type, member) \183((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))184185/**186* list_for_each - iterate over a list187* @pos: the &struct list_head to use as a loop counter.188* @head: the head for your list.189*/190#define list_for_each(pos, head) \191for (pos = (head)->next; pos != (head); \192pos = pos->next)193/**194* list_for_each_prev - iterate over a list backwards195* @pos: the &struct list_head to use as a loop counter.196* @head: the head for your list.197*/198#define list_for_each_prev(pos, head) \199for (pos = (head)->prev; pos != (head); \200pos = pos->prev)201202/**203* list_for_each_safe - iterate over a list safe against removal of list entry204* @pos: the &struct list_head to use as a loop counter.205* @n: another &struct list_head to use as temporary storage206* @head: the head for your list.207*/208#define list_for_each_safe(pos, n, head) \209for (pos = (head)->next, n = pos->next; pos != (head); \210pos = n, n = pos->next)211212/**213* list_for_each_entry - iterate over list of given type214* @pos: the type * to use as a loop counter.215* @head: the head for your list.216* @member: the name of the list_struct within the struct.217* @type: the type of the struct.218*/219#define list_for_each_entry(pos, head, member, type) \220for (pos = list_entry((head)->next, type, member); \221&pos->member != (head); \222pos = list_entry(pos->member.next, type, member))223224/**225* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry226* @pos: the type * to use as a loop counter.227* @n: another type * to use as temporary storage228* @head: the head for your list.229* @member: the name of the list_struct within the struct.230* @type: the type of the struct.231*/232#define list_for_each_entry_safe(pos, n, head, member, type) \233for (pos = list_entry((head)->next, type, member), \234n = list_entry(pos->member.next, type, member); \235&pos->member != (head); \236pos = n, n = list_entry(n->member.next, type, member))237238/**239* list_for_each_entry_continue - iterate over list of given type240* continuing after existing point241* @pos: the type * to use as a loop counter.242* @head: the head for your list.243* @member: the name of the list_struct within the struct.244* @type: the type of the struct.245*/246#define list_for_each_entry_continue(pos, head, member, type) \247for (pos = list_entry(pos->member.next, type, member), \248prefetch(pos->member.next); \249&pos->member != (head); \250pos = list_entry(pos->member.next, type, member), \251prefetch(pos->member.next))252253#endif254255256