Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef LIST_H
0003 #define LIST_H
0004 
0005 /*
0006  * Copied from include/linux/...
0007  */
0008 
0009 #undef offsetof
0010 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
0011 
0012 /**
0013  * container_of - cast a member of a structure out to the containing structure
0014  * @ptr:        the pointer to the member.
0015  * @type:       the type of the container struct this is embedded in.
0016  * @member:     the name of the member within the struct.
0017  *
0018  */
0019 #define container_of(ptr, type, member) ({                      \
0020     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
0021     (type *)( (char *)__mptr - offsetof(type,member) );})
0022 
0023 
0024 struct list_head {
0025     struct list_head *next, *prev;
0026 };
0027 
0028 
0029 #define LIST_HEAD_INIT(name) { &(name), &(name) }
0030 
0031 #define LIST_HEAD(name) \
0032     struct list_head name = LIST_HEAD_INIT(name)
0033 
0034 /**
0035  * list_entry - get the struct for this entry
0036  * @ptr:    the &struct list_head pointer.
0037  * @type:   the type of the struct this is embedded in.
0038  * @member: the name of the list_head within the struct.
0039  */
0040 #define list_entry(ptr, type, member) \
0041     container_of(ptr, type, member)
0042 
0043 /**
0044  * list_for_each_entry  -   iterate over list of given type
0045  * @pos:    the type * to use as a loop cursor.
0046  * @head:   the head for your list.
0047  * @member: the name of the list_head within the struct.
0048  */
0049 #define list_for_each_entry(pos, head, member)              \
0050     for (pos = list_entry((head)->next, typeof(*pos), member);  \
0051          &pos->member != (head);    \
0052          pos = list_entry(pos->member.next, typeof(*pos), member))
0053 
0054 /**
0055  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
0056  * @pos:    the type * to use as a loop cursor.
0057  * @n:      another type * to use as temporary storage
0058  * @head:   the head for your list.
0059  * @member: the name of the list_head within the struct.
0060  */
0061 #define list_for_each_entry_safe(pos, n, head, member)          \
0062     for (pos = list_entry((head)->next, typeof(*pos), member),  \
0063         n = list_entry(pos->member.next, typeof(*pos), member); \
0064          &pos->member != (head);                    \
0065          pos = n, n = list_entry(n->member.next, typeof(*n), member))
0066 
0067 /**
0068  * list_empty - tests whether a list is empty
0069  * @head: the list to test.
0070  */
0071 static inline int list_empty(const struct list_head *head)
0072 {
0073     return head->next == head;
0074 }
0075 
0076 /*
0077  * Insert a new entry between two known consecutive entries.
0078  *
0079  * This is only for internal list manipulation where we know
0080  * the prev/next entries already!
0081  */
0082 static inline void __list_add(struct list_head *_new,
0083                   struct list_head *prev,
0084                   struct list_head *next)
0085 {
0086     next->prev = _new;
0087     _new->next = next;
0088     _new->prev = prev;
0089     prev->next = _new;
0090 }
0091 
0092 /**
0093  * list_add_tail - add a new entry
0094  * @new: new entry to be added
0095  * @head: list head to add it before
0096  *
0097  * Insert a new entry before the specified head.
0098  * This is useful for implementing queues.
0099  */
0100 static inline void list_add_tail(struct list_head *_new, struct list_head *head)
0101 {
0102     __list_add(_new, head->prev, head);
0103 }
0104 
0105 /*
0106  * Delete a list entry by making the prev/next entries
0107  * point to each other.
0108  *
0109  * This is only for internal list manipulation where we know
0110  * the prev/next entries already!
0111  */
0112 static inline void __list_del(struct list_head *prev, struct list_head *next)
0113 {
0114     next->prev = prev;
0115     prev->next = next;
0116 }
0117 
0118 #define LIST_POISON1  ((void *) 0x00100100)
0119 #define LIST_POISON2  ((void *) 0x00200200)
0120 /**
0121  * list_del - deletes entry from list.
0122  * @entry: the element to delete from the list.
0123  * Note: list_empty() on entry does not return true after this, the entry is
0124  * in an undefined state.
0125  */
0126 static inline void list_del(struct list_head *entry)
0127 {
0128     __list_del(entry->prev, entry->next);
0129     entry->next = (struct list_head*)LIST_POISON1;
0130     entry->prev = (struct list_head*)LIST_POISON2;
0131 }
0132 #endif