Created
April 20, 2015 14:06
-
-
Save synther/6b76678862aca65c8f8e to your computer and use it in GitHub Desktop.
LIST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* List definitions. | |
*/ | |
#define LIST_HEAD(name, type) \ | |
struct name { \ | |
struct type *lh_first; /* first element */ \ | |
} | |
#define LIST_HEAD_INITIALIZER(head) \ | |
{ NULL } | |
#define LIST_ENTRY(type) \ | |
struct { \ | |
struct type *le_next; /* next element */ \ | |
struct type **le_prev; /* address of previous next element */ \ | |
} | |
/* | |
* List functions. | |
*/ | |
#define LIST_EMPTY(head) ((head)->lh_first == NULL) | |
#define LIST_FIRST(head) ((head)->lh_first) | |
#define LIST_FOREACH(var, head, field) \ | |
for((var) = (head)->lh_first; (var); (var) = (var)->field.le_next) | |
#define LIST_INIT(head) do { \ | |
(head)->lh_first = NULL; \ | |
} while (0) | |
#define LIST_INSERT_AFTER(listelm, elm, field) do { \ | |
if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ | |
(listelm)->field.le_next->field.le_prev = \ | |
&(elm)->field.le_next; \ | |
(listelm)->field.le_next = (elm); \ | |
(elm)->field.le_prev = &(listelm)->field.le_next; \ | |
} while (0) | |
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ | |
(elm)->field.le_prev = (listelm)->field.le_prev; \ | |
(elm)->field.le_next = (listelm); \ | |
*(listelm)->field.le_prev = (elm); \ | |
(listelm)->field.le_prev = &(elm)->field.le_next; \ | |
} while (0) | |
#define LIST_INSERT_HEAD(head, elm, field) do { \ | |
if (((elm)->field.le_next = (head)->lh_first) != NULL) \ | |
(head)->lh_first->field.le_prev = &(elm)->field.le_next;\ | |
(head)->lh_first = (elm); \ | |
(elm)->field.le_prev = &(head)->lh_first; \ | |
} while (0) | |
#define LIST_NEXT(elm, field) ((elm)->field.le_next) | |
#define LIST_REMOVE(elm, field) do { \ | |
if ((elm)->field.le_next != NULL) \ | |
(elm)->field.le_next->field.le_prev = \ | |
(elm)->field.le_prev; \ | |
*(elm)->field.le_prev = (elm)->field.le_next; \ | |
} while (0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment