liblist-c

C type-agnostic linked-list library
git clone https://git.sinitax.com/sinitax/liblist-c
Log | Files | Refs | LICENSE | sfeed.txt

commit a0dd7f53ac1f0ea716d348472bd09cbdf7a3ea6f
parent e9f33be7a929f1cc68d2d0b3d49ca7ec7d8cfa4f
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 13 Mar 2023 22:19:45 +0100

ASSERT vs CHECK semantics + fixed install target

Diffstat:
MMakefile | 16++++++++--------
Minclude/list.h | 20--------------------
Dinclude/test.h | 0
Msrc/list.c | 48++++++++++++++++++++++++++++++++----------------
Msrc/test.c | 6++++--
5 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,11 +1,11 @@ -CFLAGS = -I src -I include +PREFIX ?= /usr/local +LIBDIR ?= /lib +INCLDIR ?= /include -ifeq "$(LIBLIST_DEBUG)" "1" -CFLAGS += -g -endif +CFLAGS = -I src -I include -ifeq "$(LIBLIST_ASSERT)" "1" -CFLAGS += -DLIBLIST_ASSERT_ENABLE=1 +ifeq "$(DEBUG)" "1" +CFLAGS += -g -DLIBLIST_CHECK_ENABLE=1 endif all: build/liblist.so build/liblist.a build/test @@ -28,12 +28,12 @@ build/test: src/test.c build/liblist.a | build $(CC) -o $@ $^ -I include install: - install -m644 include/list.h -t "$(DESTDIR)$(PREFIX)$(INCLUDEDIR)" + install -m644 include/list.h -t "$(DESTDIR)$(PREFIX)$(INCLDIR)" install -m755 build/liblist.a -t "$(DESTDIR)$(PREFIX)$(LIBDIR)" install -m755 build/liblist.so -t "$(DESTDIR)$(PREFIX)$(LIBDIR)" uninstall: - rm -f "$(DESTDIR)$(PREFIX)$(INCLUDEDIR)/list.h" + rm -f "$(DESTDIR)$(PREFIX)$(INCLDIR)/list.h" rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/liblist.a" rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/liblist.so" diff --git a/include/list.h b/include/list.h @@ -3,26 +3,6 @@ #include <stdbool.h> #include <stdlib.h> -#ifdef LIBLIST_ASSERT_ENABLE - -#include <stdio.h> - -#define LIBLIST_ASSERT(x) liblist_assert((x), __FILE__, __LINE__, #x) - -static inline void liblist_assert(int cond, - const char *file, int line, const char *condstr) -{ - if (cond) return; - - fprintf(stderr, "liblist: Assertion failed at %s:%i (%s)\n", - file, line, condstr); - abort(); -} - -#else -#define LIBLIST_ASSERT(x) -#endif - #define LINK_OFFSET(type, member) ((size_t) &((type *)0)->member) #define LINK_UPCAST(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ diff --git a/include/test.h b/include/test.h diff --git a/src/list.c b/src/list.c @@ -4,15 +4,31 @@ #include <stdlib.h> #include <stdio.h> +#ifdef LIBLIST_CHECK_ENABLE +#define LIBLIST_CHECK(x) liblist_assert((x), __FILE__, __LINE__, #x) +#else +#define LIBLIST_CHECK(x) +#endif + static struct link *link_iter_fwd(struct link *link, size_t n); static struct link *link_iter_bwd(struct link *link, size_t n); +static inline void +liblist_assert(int cond, const char *file, int line, const char *condstr) +{ + if (cond) return; + + fprintf(stderr, "liblist: Assertion failed at %s:%i (%s)\n", + file, line, condstr); + abort(); +} + struct link * link_iter_fwd(struct link *link, size_t n) { size_t i; - LIBLIST_ASSERT(link != NULL); + LIBLIST_CHECK(link != NULL); for (i = 0; i < n; i++) { if (!link) return NULL; @@ -27,7 +43,7 @@ link_iter_bwd(struct link *link, size_t n) { size_t i; - LIBLIST_ASSERT(link != NULL); + LIBLIST_CHECK(link != NULL); for (i = 0; i < n; i++) { if (!link) return NULL; @@ -40,7 +56,7 @@ link_iter_bwd(struct link *link, size_t n) void list_init(struct list *list) { - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); list->head.prev = NULL; list->head.next = &list->tail; @@ -53,7 +69,7 @@ list_free(struct list *list, void (*free_item)(void *), int offset) { struct link *item; - LIBLIST_ASSERT(list != NULL && free_item != NULL); + LIBLIST_CHECK(list != NULL && free_item != NULL); while (!list_empty(list)) { item = link_pop(list->head.next); @@ -64,7 +80,7 @@ list_free(struct list *list, void (*free_item)(void *), int offset) void list_clear(struct list *list) { - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); while (!list_empty(list)) list_pop_back(list); @@ -73,7 +89,7 @@ list_clear(struct list *list) bool list_empty(struct list *list) { - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); return list->head.next == &list->tail; } @@ -84,7 +100,7 @@ list_len(struct list *list) struct link *link; size_t len; - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); len = 0; for (LIST_ITER(list, link)) @@ -99,7 +115,7 @@ list_insert_sorted(struct list *list, struct link *insert, bool reverse, { struct link *link; - LIBLIST_ASSERT(list != NULL && insert != NULL && in_order != NULL); + LIBLIST_CHECK(list != NULL && insert != NULL && in_order != NULL); /* Simple Insertion Sort */ /* cmp(a,b) -> (a-b) */ @@ -120,7 +136,7 @@ list_sort(struct list *list, bool reverse, { struct link *link, *cmp, *next; - LIBLIST_ASSERT(list != NULL && in_order != NULL); + LIBLIST_CHECK(list != NULL && in_order != NULL); /* Insertion Sort */ link = list->head.next; @@ -159,7 +175,7 @@ list_at(struct list *list, int n) { struct link * link; - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); if (n >= 0) link = link_iter_fwd(list->head.next, n); @@ -196,7 +212,7 @@ list_push_back(struct list *list, struct link *link) struct link * list_pop_front(struct list *list) { - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); if (list_empty(list)) return NULL; @@ -207,7 +223,7 @@ list_pop_front(struct list *list) struct link * list_pop_back(struct list *list) { - LIBLIST_ASSERT(list != NULL); + LIBLIST_CHECK(list != NULL); if (list_empty(list)) return NULL; @@ -218,7 +234,7 @@ list_pop_back(struct list *list) void link_prepend(struct link *cur, struct link *link) { - LIBLIST_ASSERT(cur != NULL && link != NULL); + LIBLIST_CHECK(cur != NULL && link != NULL); link->prev = cur->prev; link->next = cur; @@ -232,7 +248,7 @@ link_prepend(struct link *cur, struct link *link) void link_append(struct link *cur, struct link *link) { - LIBLIST_ASSERT(cur != NULL && link != NULL); + LIBLIST_CHECK(cur != NULL && link != NULL); link->prev = cur; link->next = cur->next; @@ -246,7 +262,7 @@ link_append(struct link *cur, struct link *link) bool link_inuse(struct link *link) { - LIBLIST_ASSERT(link != NULL); + LIBLIST_CHECK(link != NULL); return link->prev != NULL && link->next != NULL; } @@ -263,7 +279,7 @@ link_iter(struct link *link, int n) struct link * link_pop(struct link *link) { - LIBLIST_ASSERT(link != NULL); + LIBLIST_CHECK(link != NULL); if (link->prev) link->prev->next = link->next; diff --git a/src/test.c b/src/test.c @@ -31,10 +31,12 @@ main(int argc, const char **argv) list_init(&list); - if (argc < 3) + if (argc < 3) { + fprintf(stderr, "Usage: test REVERSE [STR]..\n"); return 1; + } - for (arg = &argv[2]; *arg; arg++) { + for (arg = argv + 2; *arg; arg++) { item = malloc(sizeof(struct arg)); if (!item) return 0; item->str = *arg;