common/list.h (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifndef INCLUDE_LIST_H
#define INCLUDE_LIST_H
#include <stddef.h>
#include "utils.h"
struct list_node {
struct list_node *next;
struct list_node *prev;
};
struct list {
struct list_node *first;
struct list_node *last;
};
#define LIST_INIT() { NULL, NULL }
void list_append(struct list *list, struct list_node *node);
void list_prepend(struct list *list, struct list_node *node);
static inline struct list_node *list_next(struct list_node *node) {
if (node == NULL) {
return NULL;
}
return node->next;
}
#define LIST_FOR_EACH(listptr, type, nodeptr) \
for ((nodeptr) = (listptr)->first; (nodeptr) != NULL; (nodeptr) = (nodeptr)->next)
#define LIST_FOR_EACH_ENTRY(listptr, type, member, entry) \
for (struct list_node *nodeptr = (listptr)->first; \
nodeptr != NULL \
&& ((entry) = containerof(nodeptr, type, member)) != NULL; \
nodeptr = nodeptr->next)
#endif