Sat, 31 Dec 2011 21:05:59 +0100
Added mempool
universe@4 | 1 | #include "dlist.h" |
universe@4 | 2 | |
universe@8 | 3 | void ucx_dlist_free(UcxDlist *l) { |
universe@8 | 4 | UcxDlist *e = l, *f; |
universe@8 | 5 | while (e != NULL) { |
universe@8 | 6 | f = e; |
universe@8 | 7 | e = e->next; |
universe@8 | 8 | free(f); |
universe@8 | 9 | } |
universe@8 | 10 | } |
universe@8 | 11 | |
universe@7 | 12 | UcxDlist *ucx_dlist_append(UcxDlist *l, void *data) { |
universe@8 | 13 | UcxDlist *nl = (UcxDlist*) malloc(sizeof(UcxDlist)); |
universe@8 | 14 | if (nl == NULL) return NULL; |
universe@7 | 15 | |
universe@8 | 16 | nl->data = data; |
universe@8 | 17 | nl->next = NULL; |
universe@8 | 18 | if (l == NULL) { |
universe@8 | 19 | return nl; |
universe@8 | 20 | } else { |
universe@8 | 21 | UcxDlist *t = ucx_dlist_last(l); |
universe@8 | 22 | t->next = nl; |
universe@8 | 23 | nl->prev = t; |
universe@8 | 24 | return l; |
universe@8 | 25 | } |
universe@7 | 26 | } |
universe@7 | 27 | |
universe@7 | 28 | UcxDlist *ucx_dlist_prepend(UcxDlist *l, void *data) { |
universe@8 | 29 | UcxDlist *nl = ucx_dlist_append(NULL, data); |
universe@8 | 30 | if (nl == NULL) return NULL; |
universe@7 | 31 | |
universe@8 | 32 | if (l != NULL) { |
universe@8 | 33 | nl->next = l; |
universe@8 | 34 | l->prev = nl; |
universe@8 | 35 | } |
universe@8 | 36 | return nl; |
universe@7 | 37 | } |
universe@7 | 38 | |
universe@7 | 39 | UcxDlist *ucx_dlist_concat(UcxDlist *l1, UcxDlist *l2) { |
universe@8 | 40 | if (l1 == NULL) { |
universe@8 | 41 | return l2; |
universe@8 | 42 | } else { |
universe@8 | 43 | UcxDlist *last = ucx_dlist_last(l1); |
universe@8 | 44 | last->next = l2; |
universe@8 | 45 | l2->prev = last; |
universe@8 | 46 | return l1; |
universe@8 | 47 | } |
universe@7 | 48 | } |
universe@7 | 49 | |
universe@7 | 50 | UcxDlist *ucx_dlist_last(UcxDlist *l) { |
universe@7 | 51 | if (l == NULL) return NULL; |
universe@7 | 52 | |
universe@7 | 53 | UcxDlist *e = l; |
universe@7 | 54 | while (e->next != NULL) { |
universe@7 | 55 | e = e->next; |
universe@7 | 56 | } |
universe@7 | 57 | return e; |
universe@7 | 58 | } |
universe@7 | 59 | |
universe@7 | 60 | UcxDlist *ucx_dlist_get(UcxDlist *l, int index) { |
universe@8 | 61 | if (l == NULL) return NULL; |
universe@8 | 62 | |
universe@8 | 63 | UcxDlist *e = l; |
universe@8 | 64 | while (e->next != NULL && index > 0) { |
universe@8 | 65 | e = e->next; |
universe@8 | 66 | index--; |
universe@8 | 67 | } |
universe@7 | 68 | |
universe@8 | 69 | return index == 0 ? e : NULL; |
universe@7 | 70 | } |
universe@7 | 71 | |
universe@7 | 72 | size_t ucx_dlist_size(UcxDlist *l) { |
universe@7 | 73 | if (l == NULL) return 0; |
universe@7 | 74 | |
universe@7 | 75 | UcxDlist *e = l; |
universe@7 | 76 | size_t s = 1; |
universe@7 | 77 | while (e->next != NULL) { |
universe@7 | 78 | e = e->next; |
universe@7 | 79 | s++; |
universe@7 | 80 | } |
universe@7 | 81 | |
universe@7 | 82 | return s; |
universe@7 | 83 | } |
universe@7 | 84 | |
universe@7 | 85 | void ucx_dlist_foreach(UcxDlist *l, ucx_callback fnc, void* data) { |
universe@8 | 86 | UcxDlist *e = l; |
universe@8 | 87 | while (e != NULL) { |
universe@8 | 88 | fnc(e, data); |
universe@8 | 89 | e = e->next; |
universe@8 | 90 | } |
universe@7 | 91 | } |
universe@7 | 92 | |
universe@7 | 93 | /* dlist specific functions */ |
universe@7 | 94 | UcxDlist *ucx_dlist_first(UcxDlist *l) { |
universe@8 | 95 | if (l == NULL) return NULL; |
universe@7 | 96 | |
universe@8 | 97 | UcxDlist *e = l; |
universe@8 | 98 | while (e->prev != NULL) { |
universe@8 | 99 | e = e->prev; |
universe@8 | 100 | } |
universe@8 | 101 | return e; |
olaf@13 | 102 | } |