libliftoff/include/list.h

35 lines
1.1 KiB
C
Raw Permalink Normal View History

2019-08-21 22:07:37 +02:00
#ifndef LIST_H
#define LIST_H
#include <stdbool.h>
#include <stddef.h>
struct liftoff_list {
struct liftoff_list *prev;
struct liftoff_list *next;
2019-08-21 22:07:37 +02:00
};
void liftoff_list_init(struct liftoff_list *list);
void liftoff_list_insert(struct liftoff_list *list, struct liftoff_list *elm);
void liftoff_list_remove(struct liftoff_list *elm);
size_t liftoff_list_length(const struct liftoff_list *list);
bool liftoff_list_empty(const struct liftoff_list *list);
2019-08-21 22:07:37 +02:00
#define liftoff_container_of(ptr, sample, member) \
2019-08-21 22:07:37 +02:00
(__typeof__(sample))((char *)(ptr) - \
offsetof(__typeof__(*sample), member))
#define liftoff_list_for_each(pos, head, member) \
for (pos = liftoff_container_of((head)->next, pos, member); \
2019-08-21 22:07:37 +02:00
&pos->member != (head); \
pos = liftoff_container_of(pos->member.next, pos, member))
2019-08-21 22:07:37 +02:00
#define liftoff_list_for_each_safe(pos, tmp, head, member) \
for (pos = liftoff_container_of((head)->next, pos, member), \
tmp = liftoff_container_of(pos->member.next, tmp, member); \
2019-08-21 22:07:37 +02:00
&pos->member != (head); \
pos = tmp, \
tmp = liftoff_container_of(pos->member.next, tmp, member))
2019-08-21 22:07:37 +02:00
#endif