X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Flist%2Flist.h;h=ab3240871773d90ceee149c3cb30931e228326fb;hp=8e2f6df0273998764bb89cb69fb4c25d6e5b1070;hb=8216fa0be7d9bf39774a192d52fef955f6bd7cd8;hpb=c35fcafc261eb9c0f5aff1a608568b38ddc01f66 diff --git a/ccan/list/list.h b/ccan/list/list.h index 8e2f6df0..ab324087 100644 --- a/ccan/list/list.h +++ b/ccan/list/list.h @@ -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; } +/** + * 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