CeDOS - common/list.h

common/list.h (view raw)

#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