2 * Copyright (C) 2001 Sistina Software
4 * This file is released under the LGPL.
16 #define LIST_INIT(name) struct list name = { &(name), &(name) }
18 static inline void list_init(struct list *head)
20 head->n = head->p = head;
23 static inline void list_add(struct list *head, struct list *elem)
34 static inline void list_add_h(struct list *head, struct list *elem)
45 static inline void list_del(struct list *elem)
51 static inline int list_empty(struct list *head)
53 return head->n == head;
56 static inline int list_end(struct list *head, struct list *elem)
58 return elem->n == head;
61 static inline struct list *list_next(struct list *head, struct list *elem)
63 return (list_end(head, elem) ? NULL : elem->n);
66 #define list_iterate(v, head) \
67 for (v = (head)->n; v != head; v = v->n)
69 #define list_uniterate(v, head, start) \
70 for (v = (start)->p; v != head; v = v->p)
72 #define list_iterate_safe(v, t, head) \
73 for (v = (head)->n, t = v->n; v != head; v = t, t = v->n)
75 static inline unsigned int list_size(const struct list *head)
86 #define list_item(v, t) \
87 ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->list))
89 #define list_struct_base(v, t, h) \
90 ((t *)((uintptr_t)(v) - (uintptr_t)&((t *) 0)->h))
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)
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)