src/linked_list.c

changeset 453
bb144d08cd44
parent 451
db06dda7ac4d
child 456
227c2eabbef8
equal deleted inserted replaced
452:a10c3e127050 453:bb144d08cd44
27 */ 27 */
28 28
29 #include "cx/linked_list.h" 29 #include "cx/linked_list.h"
30 #include <stdint.h> 30 #include <stdint.h>
31 #include <string.h> 31 #include <string.h>
32 #include <assert.h>
32 33
33 /* LOW LEVEL LINKED LIST FUNCTIONS */ 34 /* LOW LEVEL LINKED LIST FUNCTIONS */
34 35
35 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off)) 36 #define CX_LL_PTR(cur, off) ((void**)(((char*)cur)+off))
36 37
59 60
60 return last; 61 return last;
61 } 62 }
62 } 63 }
63 64
64 int cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) { 65 void cx_linked_list_add(void **begin, void **end, ptrdiff_t loc_prev, ptrdiff_t loc_next, void *new_node) {
65 void *last = cx_linked_list_last(begin, end, loc_next); 66 void *last = cx_linked_list_last(begin, end, loc_next);
66 if (last == NULL) { 67 if (last == NULL) {
67 if (begin == NULL) { 68 assert(begin != NULL);
68 // no current list and no begin ptr to write to - we don't find something to append to 69 *begin = new_node;
69 return 1;
70 } else {
71 // start fresh list
72 *begin = new_node;
73 }
74 } else { 70 } else {
75 // if there is a last node, update its next pointer 71 // if there is a last node, update its next pointer
76 void **next = CX_LL_PTR(last, loc_next); 72 void **next = CX_LL_PTR(last, loc_next);
77 *next = new_node; 73 *next = new_node;
78 } 74 }
85 // if the nodes use a prev pointer, update it 81 // if the nodes use a prev pointer, update it
86 if (loc_prev >= 0) { 82 if (loc_prev >= 0) {
87 void **prev = CX_LL_PTR(new_node, loc_prev); 83 void **prev = CX_LL_PTR(new_node, loc_prev);
88 *prev = last; 84 *prev = last;
89 } 85 }
90
91 return 0;
92 } 86 }
93 87
94 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */ 88 /* HIGH LEVEL LINKED LIST IMPLEMENTATION */
95 89
96 typedef struct cx_linked_list_node cx_linked_list_node; 90 typedef struct cx_linked_list_node cx_linked_list_node;

mercurial