1 #ifndef LIBXMP_LIST_H
2 #define LIBXMP_LIST_H
3
4 #ifdef _MSC_VER
5 #define __inline__ __inline
6 #endif
7 #ifdef __WATCOMC__
8 #define __inline__ inline
9 #endif
10
11 /*
12 * Simple doubly linked list implementation.
13 *
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
19 */
20
21 struct list_head {
22 struct list_head *next, *prev;
23 };
24
25 #define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27 #define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
29
30 #define INIT_LIST_HEAD(ptr) do { \
31 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
32 } while (0)
33
34 /*
35 * Insert a new entry between two known consecutive entries.
36 *
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
39 */
__list_add(struct list_head * _new,struct list_head * prev,struct list_head * next)40 static __inline__ void __list_add(struct list_head *_new,
41 struct list_head * prev,
42 struct list_head * next)
43 {
44 next->prev = _new;
45 _new->next = next;
46 _new->prev = prev;
47 prev->next = _new;
48 }
49
50 /**
51 * list_add - add a new entry
52 * @_new: new entry to be added
53 * @head: list head to add it after
54 *
55 * Insert a new entry after the specified head.
56 * This is good for implementing stacks.
57 */
list_add(struct list_head * _new,struct list_head * head)58 static __inline__ void list_add(struct list_head *_new, struct list_head *head)
59 {
60 __list_add(_new, head, head->next);
61 }
62
63 /**
64 * list_add_tail - add a new entry
65 * @_new: new entry to be added
66 * @head: list head to add it before
67 *
68 * Insert a new entry before the specified head.
69 * This is useful for implementing queues.
70 */
list_add_tail(struct list_head * _new,struct list_head * head)71 static __inline__ void list_add_tail(struct list_head *_new, struct list_head *head)
72 {
73 __list_add(_new, head->prev, head);
74 }
75
76 /*
77 * Delete a list entry by making the prev/next entries
78 * point to each other.
79 *
80 * This is only for internal list manipulation where we know
81 * the prev/next entries already!
82 */
__list_del(struct list_head * prev,struct list_head * next)83 static __inline__ void __list_del(struct list_head * prev,
84 struct list_head * next)
85 {
86 next->prev = prev;
87 prev->next = next;
88 }
89
90 /**
91 * list_del - deletes entry from list.
92 * @entry: the element to delete from the list.
93 */
list_del(struct list_head * entry)94 static __inline__ void list_del(struct list_head *entry)
95 {
96 __list_del(entry->prev, entry->next);
97 }
98
99 /**
100 * list_empty - tests whether a list is empty
101 * @head: the list to test.
102 */
list_empty(struct list_head * head)103 static __inline__ int list_empty(struct list_head *head)
104 {
105 return head->next == head;
106 }
107
108 /**
109 * list_splice - join two lists
110 * @list: the new list to add.
111 * @head: the place to add it in the first list.
112 */
list_splice(struct list_head * list,struct list_head * head)113 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
114 {
115 struct list_head *first = list->next;
116
117 if (first != list) {
118 struct list_head *last = list->prev;
119 struct list_head *at = head->next;
120
121 first->prev = head;
122 head->next = first;
123
124 last->next = at;
125 at->prev = last;
126 }
127 }
128
129 /**
130 * list_entry - get the struct for this entry
131 * @ptr: the &struct list_head pointer.
132 * @type: the type of the struct this is embedded in.
133 * @member: the name of the list_struct within the struct.
134 */
135 #define list_entry(ptr, type, member) \
136 ((type *)((char *)(ptr)-(size_t)(&((type *)0)->member)))
137
138 /**
139 * list_for_each - iterate over a list
140 * @pos: the &struct list_head to use as a loop counter.
141 * @head: the head for your list.
142 */
143 #define list_for_each(pos, head) \
144 for (pos = (head)->next; pos != (head); pos = pos->next)
145
146 #endif
147