chiark / gitweb /
[PATCH] update volume_id
[elogind.git] / extras / multipath-tools / libdevmapper / list.h
1 /*
2  * Copyright (C) 2001 Sistina Software
3  *
4  * This file is released under the LGPL.
5  */
6
7 #ifndef _LVM_LIST_H
8 #define _LVM_LIST_H
9
10 #include <assert.h>
11
12 struct list {
13         struct list *n, *p;
14 };
15
16 #define LIST_INIT(name) struct list name = { &(name), &(name) }
17
18 static inline void list_init(struct list *head)
19 {
20         head->n = head->p = head;
21 }
22
23 static inline void list_add(struct list *head, struct list *elem)
24 {
25         assert(head->n);
26
27         elem->n = head;
28         elem->p = head->p;
29
30         head->p->n = elem;
31         head->p = elem;
32 }
33
34 static inline void list_add_h(struct list *head, struct list *elem)
35 {
36         assert(head->n);
37
38         elem->n = head->n;
39         elem->p = head;
40
41         head->n->p = elem;
42         head->n = elem;
43 }
44
45 static inline void list_del(struct list *elem)
46 {
47         elem->n->p = elem->p;
48         elem->p->n = elem->n;
49 }
50
51 static inline int list_empty(struct list *head)
52 {
53         return head->n == head;
54 }
55
56 static inline int list_end(struct list *head, struct list *elem)
57 {
58         return elem->n == head;
59 }
60
61 static inline struct list *list_next(struct list *head, struct list *elem)
62 {
63         return (list_end(head, elem) ? NULL : elem->n);
64 }
65
66 #define list_iterate(v, head) \
67         for (v = (head)->n; v != head; v = v->n)
68
69 #define list_uniterate(v, head, start) \
70         for (v = (start)->p; v != head; v = v->p)
71
72 #define list_iterate_safe(v, t, head) \
73         for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
74
75 static inline unsigned int list_size(const struct list *head)
76 {
77         unsigned int s = 0;
78         const struct list *v;
79
80         list_iterate(v, head)
81             s++;
82
83         return s;
84 }
85
86 #define list_item(v, t) \
87     ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->list))
88
89 #define list_struct_base(v, t, h) \
90     ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->h))
91
92 /* Given a known element in a known structure, locate another */
93 #define struct_field(v, t, e, f) \
94     (((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->e))->f)
95
96 /* Given a known element in a known structure, locate the list head */
97 #define list_head(v, t, e) struct_field(v, t, e, list)
98
99 #endif