]> git.ozlabs.org Git - ccan/blobdiff - ccan/list/list.h
list: list_pop
[ccan] / ccan / list / list.h
index 8e2f6df0273998764bb89cb69fb4c25d6e5b1070..ab3240871773d90ceee149c3cb30931e228326fb 100644 (file)
@@ -292,6 +292,34 @@ static inline const void *list_top_(const struct list_head *h, size_t off)
        return (const char *)h->n.next - off;
 }
 
        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
  * @h: the list_head
 /**
  * list_tail - get the last entry in a list
  * @h: the list_head