#ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif struct sl { struct sl *next; }; #define SL_INIT(x) { .next = x, } #define SL_DECLARE(x) struct sl x = SL_INIT(&x) #define sl_init(pos) \ (pos)->next = (pos) #define sl_next(pos) \ ((pos)->next) #define sl_empty(pos) \ (sl_next(pos) == (pos)) #define sl_entry(ptr, type, member) ({ \ const struct sl * __mptr = (const struct sl *)(ptr); \ (type *)((char *)__mptr - offsetof(type, member)); }) #define sl_for_each(pos, head) \ for (pos = sl_next(head); pos != (head); pos = sl_next(pos)) #define sl_for_each_safe(pos, npos, head) \ for (pos = sl_next(head), npos = sl_next(pos); \ pos != (head); pos = npos, npos = sl_next(pos)) static inline void sl_link(struct sl *entry, struct sl *prev) { entry->next = prev->next; prev->next = entry; } static inline void sl_unlink(struct sl *entry, struct sl *prev) { prev->next = entry->next; entry->next = (void*)0xdeadbeef; /* poisoning */ }