1 /* llist.c - Linked list functions
3 * Linked list structures have a next pointer as their first element.
8 // Callback function to free data pointer of double_list or arg_list
10 void llist_free_arg(void *node)
12 struct arg_list *d = node;
18 void llist_free_double(void *node)
20 struct double_list *d = node;
26 // Call a function (such as free()) on each element of a linked list.
27 void llist_traverse(void *list, void (*using)(void *node))
32 void *pop = llist_pop(&list);
35 // End doubly linked list too.
36 if (old == list) break;
40 // Return the first item from the list, advancing the list (which must be called
42 void *llist_pop(void *list)
44 // I'd use a void ** for the argument, and even accept the typecast in all
45 // callers as documentation you need the &, except the stupid compiler
46 // would then scream about type-punned pointers. Screw it.
47 void **llist = (void **)list;
48 void **next = (void **)*llist;
54 void *dlist_pop(void *list)
56 struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist;
58 if (dlist->next == dlist) *pdlist = 0;
60 dlist->next->prev = dlist->prev;
61 dlist->prev->next = *pdlist = dlist->next;
67 void dlist_add_nomalloc(struct double_list **list, struct double_list *new)
71 new->prev = (*list)->prev;
72 (*list)->prev->next = new;
74 } else *list = new->next = new->prev = new;
78 // Add an entry to the end of a doubly linked list
79 struct double_list *dlist_add(struct double_list **list, char *data)
81 struct double_list *new = xmalloc(sizeof(struct double_list));
84 dlist_add_nomalloc(list, new);
89 // Terminate circular list for traversal in either direction. Returns end *.
90 void *dlist_terminate(void *list)
92 struct double_list *end = list;
104 struct num_cache *get_num_cache(struct num_cache *cache, long long num)
107 if (num==cache->num) return cache;
114 // Uniquely add num+data to cache. Updates *cache, returns pointer to existing
115 // entry if it was already there.
116 struct num_cache *add_num_cache(struct num_cache **cache, long long num,
119 struct num_cache *old = get_num_cache(*cache, num);
123 old = xzalloc(sizeof(struct num_cache)+len);
126 memcpy(old->data, data, len);