diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/history.c | 2 | ||||
| -rw-r--r-- | src/list.c | 143 | ||||
| -rw-r--r-- | src/list.h | 46 | ||||
| -rw-r--r-- | src/ref.c | 2 | ||||
| -rw-r--r-- | src/util.h | 3 |
5 files changed, 5 insertions, 191 deletions
diff --git a/src/history.c b/src/history.c index 5bdac39..8337bad 100644 --- a/src/history.c +++ b/src/history.c @@ -41,7 +41,7 @@ history_free(struct history *history) inputln_free(UPCAST(ln, struct inputln)); list_free(&history->list, (link_free_func) inputln_free, - LINK_OFFSET(struct inputln)); + LINK_OFFSET(struct inputln, link)); history->input = NULL; history->sel = NULL; diff --git a/src/list.c b/src/list.c deleted file mode 100644 index 2d1d777..0000000 --- a/src/list.c +++ /dev/null @@ -1,143 +0,0 @@ -#include "list.h" -#include "util.h" - -void -list_init(struct list *list) -{ - list->head.prev = NULL; - list->head.next = &list->tail; - list->tail.prev = &list->head; - list->tail.next = NULL; -} - -void -list_free(struct list *list, void (*free_item)(void *), int offset) -{ - struct link *item; - - ASSERT(list != NULL); - - while (!list_empty(list)) { - item = link_pop(list->head.next); - free_item(((void *) item) - offset); - } -} - -int -list_empty(struct list *list) -{ - ASSERT(list != NULL); - - return list->head.next == &list->tail; -} - -int -list_len(struct list *list) -{ - struct link *iter; - int len; - - ASSERT(list != NULL); - - len = 0; - for (LIST_ITER(list, iter)) - len += 1; - - return len; -} - -struct link * -list_at(struct list *list, int n) -{ - ASSERT(list != NULL); - - return link_iter(list->head.next, n); -} - -void -list_push_front(struct list *list, struct link *link) -{ - link_append(&list->head, link); -} - -void -list_push_back(struct list *list, struct link *link) -{ - link_prepend(&list->tail, link); -} - -struct link * -list_pop_front(struct list *list) -{ - ASSERT(list != NULL); - - if (list_empty(list)) - return NULL; - - return link_pop(list->head.next); -} - -struct link * -list_pop_back(struct list *list) -{ - ASSERT(list != NULL); - - if (list_empty(list)) - return NULL; - - return link_pop(list->tail.prev); -} - -void -link_prepend(struct link *cur, struct link *link) -{ - ASSERT(cur != NULL && link != NULL); - - link->prev = cur->prev; - link->next = cur; - - if (link->prev) - link->prev->next = link; - if (link->next) - link->next->prev = link; -} - -void -link_append(struct link *cur, struct link *link) -{ - ASSERT(cur != NULL && link != NULL); - - link->prev = cur; - link->next = cur->next; - - if (link->prev) - link->prev->next = link; - if (link->next) - link->next->prev = link; -} - -struct link * -link_iter(struct link *link, int n) -{ - int i; - - for (i = 0; i < n; i++) { - if (!link) return NULL; - link = link->next; - } - - return link; -} - -struct link * -link_pop(struct link *link) -{ - ASSERT(link != NULL); - - if (link->prev) - link->prev->next = link->next; - if (link->next) - link->next->prev = link->prev; - - return link; -} diff --git a/src/list.h b/src/list.h deleted file mode 100644 index a18068a..0000000 --- a/src/list.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include <stdlib.h> - -#define LINK_OFFSET(type) ((size_t) &((type *)0)->link) -#define UPCAST(ptr, type) ({ \ - const typeof( ((type *)0)->link ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - LINK_OFFSET(type) ); }) - -#define LINK_EMPTY ((struct link) { 0 }) - -#define LINK(p) (&(p)->link) - -#define LIST_INNER(list, link) \ - (((link) != &(list)->head) && ((link) != &(list)->tail)) - -#define LIST_ITER(list, iter) (iter) = (list)->head.next; \ - (iter) != &(list)->tail; (iter) = (iter)->next - -typedef void (*link_free_func)(void *p); - -struct link { - struct link *prev; - struct link *next; -}; - -struct list { - struct link head; - struct link tail; -}; - -void list_init(struct list *list); -void list_free(struct list *list, void (*free_item)(void *), int offset); -int list_empty(struct list *list); -int list_len(struct list *list); -struct link *list_at(struct list *list, int n); -void list_push_front(struct list *list, struct link *link); -void list_push_back(struct list *list, struct link *link); -struct link *list_pop_front(struct list *list); -struct link *list_pop_back(struct list *list); - -void link_prepend(struct link *list, struct link *link); -void link_append(struct link *list, struct link *link); -struct link *link_iter(struct link *link, int n); -struct link *link_pop(struct link *link); - @@ -22,7 +22,7 @@ ref_free(void *ref) void refs_free(struct list *list) { - list_free(list, ref_free, LINK_OFFSET(struct ref)); + list_free(list, ref_free, LINK_OFFSET(struct ref, link)); } static struct link * @@ -10,6 +10,9 @@ #define PANIC(...) panic(__FILE__, __LINE__, "" __VA_ARGS__) #define ASSERT(x) assert((x), __FILE__, __LINE__, #x) +#define LINK(p) (&(p)->link) +#define UPCAST(iter, type) LINK_UPCAST(iter, type, link) + int strnwidth(const char *s, int n); void panic(const char *file, int line, const char *msg, ...); |
