]> git.ozlabs.org Git - ccan/blobdiff - ccan/list/list.h
list: fix list_prev and list_next on const lists.
[ccan] / ccan / list / list.h
index da2ac73537b6c9d46d67df62c51faa330c927e70..3153d550824b9b8dd7940b02e4bb414cbffb2aa9 100644 (file)
@@ -1,8 +1,10 @@
+/* Licensed under BSD-MIT - see LICENSE file for details */
 #ifndef CCAN_LIST_H
 #define CCAN_LIST_H
 #include <stdbool.h>
 #include <assert.h>
 #include <ccan/container_of/container_of.h>
+#include <ccan/check_type/check_type.h>
 
 /**
  * struct list_node - an entry in a doubly-linked list
@@ -277,9 +279,46 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
  * Example:
  *     struct child *first;
  *     first = list_top(&parent->children, struct child, list);
+ *     if (!first)
+ *             printf("Empty list!\n");
  */
-#define list_top(h, type, member) \
-       (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
+#define list_top(h, type, member)                                      \
+       ((type *)list_top_((h), list_off_(type, member)))
+
+static inline const void *list_top_(const struct list_head *h, size_t off)
+{
+       if (list_empty(h))
+               return NULL;
+       return (const char *)h->n.next - off;
+}
+
+/**
+ * list_pop - remove the first entry in a list
+ * @h: the list_head
+ * @type: the type of the entry
+ * @member: the list_node member of the type
+ *
+ * If the list is empty, returns NULL.
+ *
+ * Example:
+ *     struct child *one;
+ *     one = list_pop(&parent->children, struct child, list);
+ *     if (!one)
+ *             printf("Empty list!\n");
+ */
+#define list_pop(h, type, member)                                      \
+       ((type *)list_pop_((h), list_off_(type, member)))
+
+static inline const void *list_pop_(const struct list_head *h, size_t off)
+{
+       struct list_node *n;
+
+       if (list_empty(h))
+               return NULL;
+       n = h->n.next;
+       list_del(n);
+       return (const char *)n - off;
+}
 
 /**
  * list_tail - get the last entry in a list
@@ -292,13 +331,22 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
  * Example:
  *     struct child *last;
  *     last = list_tail(&parent->children, struct child, list);
+ *     if (!last)
+ *             printf("Empty list!\n");
  */
 #define list_tail(h, type, member) \
-       (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
+       ((type *)list_tail_((h), list_off_(type, member)))
+
+static inline const void *list_tail_(const struct list_head *h, size_t off)
+{
+       if (list_empty(h))
+               return NULL;
+       return (const char *)h->n.prev - off;
+}
 
 /**
  * list_for_each - iterate through a list.
- * @h: the list_head
+ * @h: the list_head (warning: evaluated multiple times!)
  * @i: the structure containing the list_node
  * @member: the list_node member of the structure
  *
@@ -310,9 +358,25 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
  *             printf("Name: %s\n", child->name);
  */
 #define list_for_each(h, i, member)                                    \
-       for (i = container_of_var(list_debug(h)->n.next, i, member);    \
+       list_for_each_off(h, i, list_off_var_(i, member))
+
+/**
+ * list_for_each_rev - iterate through a list backwards.
+ * @h: the list_head
+ * @i: the structure containing the list_node
+ * @member: the list_node member of the structure
+ *
+ * This is a convenient wrapper to iterate @i over the entire list.  It's
+ * a for loop, so you can break and continue as normal.
+ *
+ * Example:
+ *     list_for_each_rev(&parent->children, child, list)
+ *             printf("Name: %s\n", child->name);
+ */
+#define list_for_each_rev(h, i, member)                                        \
+       for (i = container_of_var(list_debug(h)->n.prev, i, member);    \
             &i->member != &(h)->n;                                     \
-            i = container_of_var(i->member.next, i, member))
+            i = container_of_var(i->member.prev, i, member))
 
 /**
  * list_for_each_safe - iterate through a list, maybe during deletion
@@ -333,8 +397,219 @@ static inline void list_del_from(struct list_head *h, struct list_node *n)
  *     }
  */
 #define list_for_each_safe(h, i, nxt, member)                          \
-       for (i = container_of_var(list_debug(h)->n.next, i, member),    \
-               nxt = container_of_var(i->member.next, i, member);      \
-            &i->member != &(h)->n;                                     \
-            i = nxt, nxt = container_of_var(i->member.next, i, member))
+       list_for_each_safe_off(h, i, nxt, list_off_var_(i, member))
+
+/**
+ * list_next - get the next entry in a list
+ * @h: the list_head
+ * @i: a pointer to an entry in the list.
+ * @member: the list_node member of the structure
+ *
+ * If @i was the last entry in the list, returns NULL.
+ *
+ * Example:
+ *     struct child *second;
+ *     second = list_next(&parent->children, first, list);
+ *     if (!second)
+ *             printf("No second child!\n");
+ */
+#define list_next(h, i, member)                                                \
+       ((list_typeof(i))list_entry_or_null(list_debug(h),              \
+                                           (i)->member.next,           \
+                                           list_off_var_((i), member)))
+
+/**
+ * list_prev - get the previous entry in a list
+ * @h: the list_head
+ * @i: a pointer to an entry in the list.
+ * @member: the list_node member of the structure
+ *
+ * If @i was the first entry in the list, returns NULL.
+ *
+ * Example:
+ *     first = list_prev(&parent->children, second, list);
+ *     if (!first)
+ *             printf("Can't go back to first child?!\n");
+ */
+#define list_prev(h, i, member)                                                \
+       ((list_typeof(i))list_entry_or_null(list_debug(h),              \
+                                           (i)->member.prev,           \
+                                           list_off_var_((i), member)))
+
+/**
+ * list_append_list - empty one list onto the end of another.
+ * @to: the list to append into
+ * @from: the list to empty.
+ *
+ * This takes the entire contents of @from and moves it to the end of
+ * @to.  After this @from will be empty.
+ *
+ * Example:
+ *     struct list_head adopter;
+ *
+ *     list_append_list(&adopter, &parent->children);
+ *     assert(list_empty(&parent->children));
+ *     parent->num_children = 0;
+ */
+static inline void list_append_list(struct list_head *to,
+                                   struct list_head *from)
+{
+       struct list_node *from_tail = list_debug(from)->n.prev;
+       struct list_node *to_tail = list_debug(to)->n.prev;
+
+       /* Sew in head and entire list. */
+       to->n.prev = from_tail;
+       from_tail->next = &to->n;
+       to_tail->next = &from->n;
+       from->n.prev = to_tail;
+
+       /* Now remove head. */
+       list_del(&from->n);
+       list_head_init(from);
+}
+
+/**
+ * list_prepend_list - empty one list into the start of another.
+ * @to: the list to prepend into
+ * @from: the list to empty.
+ *
+ * This takes the entire contents of @from and moves it to the start
+ * of @to.  After this @from will be empty.
+ *
+ * Example:
+ *     list_prepend_list(&adopter, &parent->children);
+ *     assert(list_empty(&parent->children));
+ *     parent->num_children = 0;
+ */
+static inline void list_prepend_list(struct list_head *to,
+                                    struct list_head *from)
+{
+       struct list_node *from_tail = list_debug(from)->n.prev;
+       struct list_node *to_head = list_debug(to)->n.next;
+
+       /* Sew in head and entire list. */
+       to->n.next = &from->n;
+       from->n.prev = &to->n;
+       to_head->prev = from_tail;
+       from_tail->next = to_head;
+
+       /* Now remove head. */
+       list_del(&from->n);
+       list_head_init(from);
+}
+
+/**
+ * list_for_each_off - iterate through a list of memory regions.
+ * @h: the list_head
+ * @i: the pointer to a memory region wich contains list node data.
+ * @off: offset(relative to @i) at which list node data resides.
+ *
+ * This is a low-level wrapper to iterate @i over the entire list, used to
+ * implement all oher, more high-level, for-each constructs. It's a for loop,
+ * so you can break and continue as normal.
+ *
+ * WARNING! Being the low-level macro that it is, this wrapper doesn't know
+ * nor care about the type of @i. The only assumtion made is that @i points
+ * to a chunk of memory that at some @offset, relative to @i, contains a
+ * properly filled `struct node_list' which in turn contains pointers to
+ * memory chunks and it's turtles all the way down. Whith all that in mind
+ * remember that given the wrong pointer/offset couple this macro will
+ * happilly churn all you memory untill SEGFAULT stops it, in other words
+ * caveat emptor.
+ *
+ * It is worth mentioning that one of legitimate use-cases for that wrapper
+ * is operation on opaque types with known offset for `struct list_node'
+ * member(preferably 0), because it allows you not to disclose the type of
+ * @i.
+ *
+ * Example:
+ *     list_for_each_off(&parent->children, child,
+ *                             offsetof(struct child, list))
+ *             printf("Name: %s\n", child->name);
+ */
+#define list_for_each_off(h, i, off)                                    \
+  for (i = list_node_to_off_(list_debug(h)->n.next, (off));             \
+       list_node_from_off_((void *)i, (off)) != &(h)->n;                \
+       i = list_node_to_off_(list_node_from_off_((void *)i, (off))->next, \
+                             (off)))
+
+/**
+ * list_for_each_safe_off - iterate through a list of memory regions, maybe
+ * during deletion
+ * @h: the list_head
+ * @i: the pointer to a memory region wich contains list node data.
+ * @nxt: the structure containing the list_node
+ * @off: offset(relative to @i) at which list node data resides.
+ *
+ * For details see `list_for_each_off' and `list_for_each_safe'
+ * descriptions.
+ *
+ * Example:
+ *     list_for_each_safe_off(&parent->children, child,
+ *             next, offsetof(struct child, list))
+ *             printf("Name: %s\n", child->name);
+ */
+#define list_for_each_safe_off(h, i, nxt, off)                          \
+  for (i = list_node_to_off_(list_debug(h)->n.next, (off)),             \
+         nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
+                                 (off));                                \
+       list_node_from_off_(i, (off)) != &(h)->n;                        \
+       i = nxt,                                                         \
+         nxt = list_node_to_off_(list_node_from_off_(i, (off))->next,   \
+                                 (off)))
+
+
+/* Other -off variants. */
+#define list_entry_off(n, type, off)           \
+       ((type *)list_node_from_off_((n), (off)))
+
+#define list_head_off(h, type, off)            \
+       ((type *)list_head_off((h), (off)))
+
+#define list_tail_off(h, type, off)            \
+       ((type *)list_tail_((h), (off)))
+
+#define list_add_off(h, n, off)                 \
+       list_add((h), list_node_from_off_((n), (off)))
+
+#define list_del_off(n, off)                    \
+       list_del(list_node_from_off_((n), (off)))
+
+#define list_del_from_off(h, n, off)                   \
+       list_del_from(h, list_node_from_off_((n), (off)))
+
+/* Offset helper functions so we only single-evaluate. */
+static inline void *list_node_to_off_(struct list_node *node, size_t off)
+{
+       return (void *)((char *)node - off);
+}
+static inline struct list_node *list_node_from_off_(void *ptr, size_t off)
+{
+       return (struct list_node *)((char *)ptr + off);
+}
+
+/* Get the offset of the member, but make sure it's a list_node. */
+#define list_off_(type, member)                                        \
+       (container_off(type, member) +                          \
+        check_type(((type *)0)->member, struct list_node))
+
+#define list_off_var_(var, member)                     \
+       (container_off_var(var, member) +               \
+        check_type(var->member, struct list_node))
+
+#if HAVE_TYPEOF
+#define list_typeof(var) typeof(var)
+#else
+#define list_typeof(var) void *
+#endif
+
+/* Returns member, or NULL if at end of list. */
+static inline void *list_entry_or_null(const struct list_head *h,
+                                      const struct list_node *n,
+                                      size_t off)
+{
+       if (n == &h->n)
+               return NULL;
+       return (char *)n - off;
+}
 #endif /* CCAN_LIST_H */