/* list.h — intrusive circular doubly-linked list (header-only). * * Zero allocation: the node lives inside your struct. Recover the owner with * NAUT_CONTAINER_OF. This is the workhorse list for peer sets, freelists of * objects, timer wheels, etc. */ #ifndef NAUT_LIST_H #define NAUT_LIST_H #include "naut/common.h" typedef struct naut_list { struct naut_list *prev; struct naut_list *next; } naut_list; NAUT_INLINE void naut_list_init(naut_list *l) { l->prev = l; l->next = l; } NAUT_INLINE bool naut_list_empty(const naut_list *l) { return l->next == l; } NAUT_INLINE void naut__link(naut_list *n, naut_list *p, naut_list *x) { n->prev = p; n->next = x; p->next = n; x->prev = n; } /* insert n at head / tail of list l */ NAUT_INLINE void naut_list_push_front(naut_list *l, naut_list *n) { naut__link(n, l, l->next); } NAUT_INLINE void naut_list_push_back(naut_list *l, naut_list *n) { naut__link(n, l->prev, l); } NAUT_INLINE void naut_list_del(naut_list *n) { n->prev->next = n->next; n->next->prev = n->prev; n->prev = n->next = n; /* safe to del again / detect detached */ } NAUT_INLINE naut_list *naut_list_front(const naut_list *l) { return l->next; } NAUT_INLINE naut_list *naut_list_back(const naut_list *l) { return l->prev; } #define naut_list_entry(ptr, type, member) NAUT_CONTAINER_OF(ptr, type, member) #define naut_list_for_each(it, l) \ for ((it) = (l)->next; (it) != (l); (it) = (it)->next) /* safe against deletion of the current node */ #define naut_list_for_each_safe(it, tmp, l) \ for ((it) = (l)->next, (tmp) = (it)->next; \ (it) != (l); (it) = (tmp), (tmp) = (it)->next) #endif /* NAUT_LIST_H */