10a9064fbSMasahiro Yamada #ifndef LIST_H
20a9064fbSMasahiro Yamada #define LIST_H
30a9064fbSMasahiro Yamada
40a9064fbSMasahiro Yamada /*
50a9064fbSMasahiro Yamada * Copied from include/linux/...
60a9064fbSMasahiro Yamada */
70a9064fbSMasahiro Yamada
80a9064fbSMasahiro Yamada #undef offsetof
90a9064fbSMasahiro Yamada #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
100a9064fbSMasahiro Yamada
110a9064fbSMasahiro Yamada /**
120a9064fbSMasahiro Yamada * container_of - cast a member of a structure out to the containing structure
130a9064fbSMasahiro Yamada * @ptr: the pointer to the member.
140a9064fbSMasahiro Yamada * @type: the type of the container struct this is embedded in.
150a9064fbSMasahiro Yamada * @member: the name of the member within the struct.
160a9064fbSMasahiro Yamada *
170a9064fbSMasahiro Yamada */
180a9064fbSMasahiro Yamada #define container_of(ptr, type, member) ({ \
190a9064fbSMasahiro Yamada const typeof( ((type *)0)->member ) *__mptr = (ptr); \
200a9064fbSMasahiro Yamada (type *)( (char *)__mptr - offsetof(type,member) );})
210a9064fbSMasahiro Yamada
220a9064fbSMasahiro Yamada
230a9064fbSMasahiro Yamada struct list_head {
240a9064fbSMasahiro Yamada struct list_head *next, *prev;
250a9064fbSMasahiro Yamada };
260a9064fbSMasahiro Yamada
270a9064fbSMasahiro Yamada
280a9064fbSMasahiro Yamada #define LIST_HEAD_INIT(name) { &(name), &(name) }
290a9064fbSMasahiro Yamada
300a9064fbSMasahiro Yamada #define LIST_HEAD(name) \
310a9064fbSMasahiro Yamada struct list_head name = LIST_HEAD_INIT(name)
320a9064fbSMasahiro Yamada
330a9064fbSMasahiro Yamada /**
340a9064fbSMasahiro Yamada * list_entry - get the struct for this entry
350a9064fbSMasahiro Yamada * @ptr: the &struct list_head pointer.
360a9064fbSMasahiro Yamada * @type: the type of the struct this is embedded in.
37*9b5f0b1dSMasahiro Yamada * @member: the name of the list_head within the struct.
380a9064fbSMasahiro Yamada */
390a9064fbSMasahiro Yamada #define list_entry(ptr, type, member) \
400a9064fbSMasahiro Yamada container_of(ptr, type, member)
410a9064fbSMasahiro Yamada
420a9064fbSMasahiro Yamada /**
430a9064fbSMasahiro Yamada * list_for_each_entry - iterate over list of given type
440a9064fbSMasahiro Yamada * @pos: the type * to use as a loop cursor.
450a9064fbSMasahiro Yamada * @head: the head for your list.
46*9b5f0b1dSMasahiro Yamada * @member: the name of the list_head within the struct.
470a9064fbSMasahiro Yamada */
480a9064fbSMasahiro Yamada #define list_for_each_entry(pos, head, member) \
490a9064fbSMasahiro Yamada for (pos = list_entry((head)->next, typeof(*pos), member); \
500a9064fbSMasahiro Yamada &pos->member != (head); \
510a9064fbSMasahiro Yamada pos = list_entry(pos->member.next, typeof(*pos), member))
520a9064fbSMasahiro Yamada
530a9064fbSMasahiro Yamada /**
540a9064fbSMasahiro Yamada * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
550a9064fbSMasahiro Yamada * @pos: the type * to use as a loop cursor.
560a9064fbSMasahiro Yamada * @n: another type * to use as temporary storage
570a9064fbSMasahiro Yamada * @head: the head for your list.
58*9b5f0b1dSMasahiro Yamada * @member: the name of the list_head within the struct.
590a9064fbSMasahiro Yamada */
600a9064fbSMasahiro Yamada #define list_for_each_entry_safe(pos, n, head, member) \
610a9064fbSMasahiro Yamada for (pos = list_entry((head)->next, typeof(*pos), member), \
620a9064fbSMasahiro Yamada n = list_entry(pos->member.next, typeof(*pos), member); \
630a9064fbSMasahiro Yamada &pos->member != (head); \
640a9064fbSMasahiro Yamada pos = n, n = list_entry(n->member.next, typeof(*n), member))
650a9064fbSMasahiro Yamada
660a9064fbSMasahiro Yamada /**
670a9064fbSMasahiro Yamada * list_empty - tests whether a list is empty
680a9064fbSMasahiro Yamada * @head: the list to test.
690a9064fbSMasahiro Yamada */
list_empty(const struct list_head * head)700a9064fbSMasahiro Yamada static inline int list_empty(const struct list_head *head)
710a9064fbSMasahiro Yamada {
720a9064fbSMasahiro Yamada return head->next == head;
730a9064fbSMasahiro Yamada }
740a9064fbSMasahiro Yamada
750a9064fbSMasahiro Yamada /*
760a9064fbSMasahiro Yamada * Insert a new entry between two known consecutive entries.
770a9064fbSMasahiro Yamada *
780a9064fbSMasahiro Yamada * This is only for internal list manipulation where we know
790a9064fbSMasahiro Yamada * the prev/next entries already!
800a9064fbSMasahiro Yamada */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)810a9064fbSMasahiro Yamada static inline void __list_add(struct list_head *_new,
820a9064fbSMasahiro Yamada struct list_head *prev,
830a9064fbSMasahiro Yamada struct list_head *next)
840a9064fbSMasahiro Yamada {
850a9064fbSMasahiro Yamada next->prev = _new;
860a9064fbSMasahiro Yamada _new->next = next;
870a9064fbSMasahiro Yamada _new->prev = prev;
880a9064fbSMasahiro Yamada prev->next = _new;
890a9064fbSMasahiro Yamada }
900a9064fbSMasahiro Yamada
910a9064fbSMasahiro Yamada /**
920a9064fbSMasahiro Yamada * list_add_tail - add a new entry
930a9064fbSMasahiro Yamada * @new: new entry to be added
940a9064fbSMasahiro Yamada * @head: list head to add it before
950a9064fbSMasahiro Yamada *
960a9064fbSMasahiro Yamada * Insert a new entry before the specified head.
970a9064fbSMasahiro Yamada * This is useful for implementing queues.
980a9064fbSMasahiro Yamada */
list_add_tail(struct list_head * _new,struct list_head * head)990a9064fbSMasahiro Yamada static inline void list_add_tail(struct list_head *_new, struct list_head *head)
1000a9064fbSMasahiro Yamada {
1010a9064fbSMasahiro Yamada __list_add(_new, head->prev, head);
1020a9064fbSMasahiro Yamada }
1030a9064fbSMasahiro Yamada
1040a9064fbSMasahiro Yamada /*
1050a9064fbSMasahiro Yamada * Delete a list entry by making the prev/next entries
1060a9064fbSMasahiro Yamada * point to each other.
1070a9064fbSMasahiro Yamada *
1080a9064fbSMasahiro Yamada * This is only for internal list manipulation where we know
1090a9064fbSMasahiro Yamada * the prev/next entries already!
1100a9064fbSMasahiro Yamada */
__list_del(struct list_head * prev,struct list_head * next)1110a9064fbSMasahiro Yamada static inline void __list_del(struct list_head *prev, struct list_head *next)
1120a9064fbSMasahiro Yamada {
1130a9064fbSMasahiro Yamada next->prev = prev;
1140a9064fbSMasahiro Yamada prev->next = next;
1150a9064fbSMasahiro Yamada }
1160a9064fbSMasahiro Yamada
1170a9064fbSMasahiro Yamada #define LIST_POISON1 ((void *) 0x00100100)
1180a9064fbSMasahiro Yamada #define LIST_POISON2 ((void *) 0x00200200)
1190a9064fbSMasahiro Yamada /**
1200a9064fbSMasahiro Yamada * list_del - deletes entry from list.
1210a9064fbSMasahiro Yamada * @entry: the element to delete from the list.
1220a9064fbSMasahiro Yamada * Note: list_empty() on entry does not return true after this, the entry is
1230a9064fbSMasahiro Yamada * in an undefined state.
1240a9064fbSMasahiro Yamada */
list_del(struct list_head * entry)1250a9064fbSMasahiro Yamada static inline void list_del(struct list_head *entry)
1260a9064fbSMasahiro Yamada {
1270a9064fbSMasahiro Yamada __list_del(entry->prev, entry->next);
1280a9064fbSMasahiro Yamada entry->next = (struct list_head*)LIST_POISON1;
1290a9064fbSMasahiro Yamada entry->prev = (struct list_head*)LIST_POISON2;
1300a9064fbSMasahiro Yamada }
1310a9064fbSMasahiro Yamada #endif
132