summaryrefslogtreecommitdiff
path: root/list.h
diff options
context:
space:
mode:
authorAndrew O. Shadoura <bugzilla@tut.by>2010-12-27 18:02:28 +0200
committerAndrew O. Shadoura <bugzilla@tut.by>2010-12-27 18:02:28 +0200
commit201d2025c2ff664f8df84c5131f80018ee5a3049 (patch)
treebf71741750627b1f380c37ea9599130f0aaf7a55 /list.h
parent967e4cfab94521dbf673805cf29fa7a807ecaf7f (diff)
mercurial-importorig imported tayga version 0.9
--HG-- branch : upstream
Diffstat (limited to 'list.h')
-rw-r--r--list.h80
1 files changed, 80 insertions, 0 deletions
diff --git a/list.h b/list.h
new file mode 100644
index 0000000..13c8f29
--- /dev/null
+++ b/list.h
@@ -0,0 +1,80 @@
+/* Linux-style linked list implementation */
+
+#ifndef __LIST_H__
+#define __LIST_H__
+
+#include <stddef.h> /* for offsetof() macro */
+
+struct list_head {
+ struct list_head *next;
+ struct list_head *prev;
+};
+
+/* Declare an empty list */
+#define LIST_HEAD(x) struct list_head x = { &x, &x }
+
+/* Initialize an empty list (required for all malloc'd list_heads) */
+static inline void INIT_LIST_HEAD(struct list_head *x)
+{
+ x->next = x;
+ x->prev = x;
+}
+
+/* Remove an entry from its current list (if any) and insert it into the
+ * beginning of the given list */
+static inline void list_add(struct list_head *x, struct list_head *head)
+{
+ x->next->prev = x->prev;
+ x->prev->next = x->next;
+ x->next = head->next;
+ x->prev = head;
+ head->next = x;
+ x->next->prev = x;
+}
+
+/* Remove an entry from its current list (if any) and insert it into the
+ * end of the given list */
+static inline void list_add_tail(struct list_head *x, struct list_head *head)
+{
+ x->next->prev = x->prev;
+ x->prev->next = x->next;
+ x->prev = head->prev;
+ x->next = head;
+ head->prev = x;
+ x->prev->next = x;
+}
+
+/* Remove an entry from its current list and reinitialize it */
+static inline void list_del(struct list_head *x)
+{
+ x->next->prev = x->prev;
+ x->prev->next = x->next;
+ x->next = x;
+ x->prev = x;
+}
+
+#define list_del_init list_del
+#define list_move list_add
+#define list_move_tail list_add_tail
+
+/* Test if list is empty (contains no other entries) */
+static inline int list_empty(const struct list_head *x)
+{
+ return x->next == x;
+}
+
+/* Get a pointer to the object containing x, which is of type "type" and
+ * embeds x as a field called "field" */
+#define list_entry(x, type, field) ({ \
+ const typeof( ((type *)0)->field ) *__mptr = (x); \
+ (type *)( (char *)__mptr - offsetof(type, field) );})
+
+/* Iterate over all items in the list */
+#define list_for_each(x, head) \
+ for (x = (head)->next; x != (head); x = x->next)
+
+/* Iterate over all items in the list, possibly deleting some */
+#define list_for_each_safe(x, n, head) \
+ for (x = (head)->next, n = x->next; x != (head); x = n, n = x->next)
+
+#endif /* __LIST_H__ */