- // I'd use a void ** for the argument, and even accept the typecast in all
- // callers as documentation you need the &, except the stupid compiler
- // would then scream about type-punned pointers. Screw it.
- void **llist = (void **)list;
- void **next = (void **)*llist;
- *llist = *next;
+ // I'd use a void ** for the argument, and even accept the typecast in all
+ // callers as documentation you need the &, except the stupid compiler
+ // would then scream about type-punned pointers. Screw it.
+ void **llist = (void **)list;
+ void **next = (void **)*llist;
+ *llist = *next;
+
+ return (void *)next;
+}
+
+void *dlist_pop(void *list)
+{
+ struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist;
+
+ if (dlist->next == dlist) *pdlist = 0;
+ else {
+ dlist->next->prev = dlist->prev;
+ dlist->prev->next = *pdlist = dlist->next;
+ }
+
+ return dlist;
+}
+
+void dlist_add_nomalloc(struct double_list **list, struct double_list *new)
+{
+ if (*list) {
+ new->next = *list;
+ new->prev = (*list)->prev;
+ (*list)->prev->next = new;
+ (*list)->prev = new;
+ } else *list = new->next = new->prev = new;
+}
+
+
+// Add an entry to the end of a doubly linked list
+struct double_list *dlist_add(struct double_list **list, char *data)
+{
+ struct double_list *new = xmalloc(sizeof(struct double_list));
+
+ new->data = data;
+ dlist_add_nomalloc(list, new);
+
+ return new;
+}
+
+// Terminate circular list for traversal in either direction. Returns end *.
+void *dlist_terminate(void *list)
+{
+ struct double_list *end = list;
+
+ if (!list) return 0;
+
+ end = end->prev;
+ end->next->prev = 0;
+ end->next = 0;
+
+ return end;
+}
+
+// Find num in cache
+struct num_cache *get_num_cache(struct num_cache *cache, long long num)
+{
+ while (cache) {
+ if (num==cache->num) return cache;
+ cache = cache->next;
+ }
+
+ return 0;
+}
+
+// Uniquely add num+data to cache. Updates *cache, returns pointer to existing
+// entry if it was already there.
+struct num_cache *add_num_cache(struct num_cache **cache, long long num,
+ void *data, int len)
+{
+ struct num_cache *old = get_num_cache(*cache, num);
+
+ if (old) return old;
+
+ old = xzalloc(sizeof(struct num_cache)+len);
+ old->next = *cache;
+ old->num = num;
+ memcpy(old->data, data, len);
+ *cache = old;