llist.h revision 9ab7221dac60f0bda81925f5bfc694d5bb2e8bf2
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch#ifndef LLIST_H
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define LLIST_H
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Doubly linked list */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define DLLIST_PREPEND_FULL(list, item, prev, next) STMT_START { \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->prev = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next = *(list); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (*(list) != NULL) (*(list))->prev = (item); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(list) = (item); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } STMT_END
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define DLLIST_PREPEND(list, item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen DLLIST_PREPEND_FULL(list, item, prev, next)
2b9dbb270ad82e58d5f3581436e6f143176d5819Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define DLLIST_REMOVE_FULL(list, item, prev, next) STMT_START { \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if ((item)->prev != NULL) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->prev->next = (item)->next; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen else if ((*list) == item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(list) = (item)->next; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if ((item)->next != NULL) { \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next->prev = (item)->prev; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->prev = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } STMT_END
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
cc0a651962a3e54d5a62231ac5847ae7f9f7de7fTimo Sirainen#define DLLIST_REMOVE(list, item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen DLLIST_REMOVE_FULL(list, item, prev, next)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen/* Doubly linked list with head and tail */
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define DLLIST2_PREPEND_FULL(head, tail, item, prev, next) STMT_START { \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->prev = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next = *(head); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if (*(head) != NULL) (*(head))->prev = (item); else (*tail) = (item); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(head) = (item); \
0001f76bf725c5cf403bade8556f142dd43144eeTimo Sirainen } STMT_END
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen#define DLLIST2_PREPEND(head, tail, item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen DLLIST2_PREPEND_FULL(head, tail, item, prev, next)
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
08a8b3de61139ba02371afc8240ac85be0e8b17cTimo Sirainen#define DLLIST2_APPEND_FULL(head, tail, item, prev, next) STMT_START { \
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen (item)->prev = *(tail); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next = NULL; \
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen if (*(tail) != NULL) (*(tail))->next = (item); else (*head) = (item); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(tail) = (item); \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } STMT_END
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek#define DLLIST2_APPEND(head, tail, item) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen DLLIST2_APPEND_FULL(head, tail, item, prev, next)
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen#define DLLIST2_INSERT_AFTER_FULL(head, tail, after, item, prev, next) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen STMT_START { \
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek (item)->prev = (after); \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen (item)->next = (after)->next; \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen if ((after)->next != NULL) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen (after)->next->prev = (item); \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen (after)->next = (item); \
03010dbaa74ec70f062994dfe3cd39bedc99a28bTimo Sirainen if (*(tail) == (after)) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen *(tail) = (item); \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen } STMT_END
c18ff860dc22960fd37c272d929f889c7939a2c8Timo Sirainen
714c6a150480112eb1a5f309d0cc39b60613a719Timo Sirainen#define DLLIST2_INSERT_AFTER(head, tail, after, item) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen DLLIST2_INSERT_AFTER_FULL(head, tail, after, item, prev, next)
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen#define DLLIST2_REMOVE_FULL(head, tail, item, prev, next) STMT_START { \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen if ((item)->prev != NULL) \
e237ebeb97f42950eef3efd0d3db85590160d5fbTimo Sirainen (item)->prev->next = (item)->next; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen else if (*(head) == item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(head) = (item)->next; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen if ((item)->next != NULL) { \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->next->prev = (item)->prev; \
b67974c4b89ab6950c2694cce8dfb1b6561cc084Josef 'Jeff' Sipek (item)->next = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } else if ((*tail) == item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen *(tail) = (item)->prev; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen (item)->prev = NULL; \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen } STMT_END
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#define DLLIST2_REMOVE(head, tail, item) \
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen DLLIST2_REMOVE_FULL(head, tail, item, prev, next)
9a02317c852face76737763fa6ec43b444688de5Timo Sirainen
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen#endif
1ac7c8e9040e0d0b7e9f849e45b94bfe919595a9Timo Sirainen