commit e9f33be7a929f1cc68d2d0b3d49ca7ec7d8cfa4f
parent acb63a984ad49a86daeb444696494ad3461fcfc4
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 17 Feb 2023 20:41:16 +0100
Refactor to use optional assert
Diffstat:
M | Makefile | | | 43 | +++++++++++++++++++++---------------------- |
M | include/list.h | | | 20 | ++++++++++++++++++++ |
M | src/list.c | | | 49 | ++++++++++++++++--------------------------------- |
3 files changed, 57 insertions(+), 55 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,14 +1,12 @@
CFLAGS = -I src -I include
-LDLIBS =
-DEPFLAGS = -MT $@ -MMD -MP -MF build/$*.d
-_SRCS = list.c
-SRCS = $(_SRCS:%=src/%)
-OBJS = $(_SRCS:%.c=build/%.o)
-DEPS = $(_SRCS:%.c=build/%.d)
-PI_OBJS = $(_SRCS:%.c=build/%.pi.o)
+ifeq "$(LIBLIST_DEBUG)" "1"
+CFLAGS += -g
+endif
-.PHONY: all clean
+ifeq "$(LIBLIST_ASSERT)" "1"
+CFLAGS += -DLIBLIST_ASSERT_ENABLE=1
+endif
all: build/liblist.so build/liblist.a build/test
@@ -18,24 +16,25 @@ clean:
build:
mkdir build
-build/%.o: src/%.c build/%.d | build
- $(CC) -c -o $@ $< $(DEPFLAGS) $(CFLAGS)
-
-build/%.pi.o: src/%.c build/%.d | build
- $(CC) -c -o $@ $< $(DEPFLAGS) $(CFLAGS) -fPIC
-
-build/%.d: | build;
-
-include $(DEPS)
-
-build/liblist.a: $(OBJS) | build
- $(CC) -o build/tmp.o $(OBJS) $(CFLAGS) -r
+build/liblist.a: src/list.c include/list.h | build
+ $(CC) -o build/tmp.o src/list.c $(CFLAGS) -r
objcopy --keep-global-symbols=liblist.api build/tmp.o build/fixed.o
ar rcs $@ build/fixed.o
-build/liblist.so: $(PI_OBJS) | build
- $(CC) -o $@ $(PI_OBJS) $(CFLAGS) -shared -Wl,-version-script liblist.lds
+build/liblist.so: src/list.c include/list.h | build
+ $(CC) -o $@ src/list.c $(CFLAGS) -shared -Wl,-version-script liblist.lds
build/test: src/test.c build/liblist.a | build
$(CC) -o $@ $^ -I include
+install:
+ install -m644 include/list.h -t "$(DESTDIR)$(PREFIX)$(INCLUDEDIR)"
+ 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)$(LIBDIR)/liblist.a"
+ rm -f "$(DESTDIR)$(PREFIX)$(LIBDIR)/liblist.so"
+
+.PHONY: all clean install uninstall
diff --git a/include/list.h b/include/list.h
@@ -3,6 +3,26 @@
#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/src/list.c b/src/list.c
@@ -4,31 +4,15 @@
#include <stdlib.h>
#include <stdio.h>
-#define ASSERT(x) assert((x), __FILE__, __LINE__, #x)
-
-static void assert(bool cond, const char *file,
- size_t line, const char *condstr);
-
static struct link *link_iter_fwd(struct link *link, size_t n);
static struct link *link_iter_bwd(struct link *link, size_t n);
-void
-assert(bool cond, const char *file, size_t line, const char *condstr)
-{
- if (cond) return;
-
- fprintf(stderr, "CLIST: Assertion failed at %s:%li (%s)\n",
- file, line, condstr);
-
- exit(1);
-}
-
struct link *
link_iter_fwd(struct link *link, size_t n)
{
size_t i;
- ASSERT(link != NULL);
+ LIBLIST_ASSERT(link != NULL);
for (i = 0; i < n; i++) {
if (!link) return NULL;
@@ -43,7 +27,7 @@ link_iter_bwd(struct link *link, size_t n)
{
size_t i;
- ASSERT(link != NULL);
+ LIBLIST_ASSERT(link != NULL);
for (i = 0; i < n; i++) {
if (!link) return NULL;
@@ -53,11 +37,10 @@ link_iter_bwd(struct link *link, size_t n)
return link;
}
-
void
list_init(struct list *list)
{
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
list->head.prev = NULL;
list->head.next = &list->tail;
@@ -70,7 +53,7 @@ list_free(struct list *list, void (*free_item)(void *), int offset)
{
struct link *item;
- ASSERT(list != NULL && free_item != NULL);
+ LIBLIST_ASSERT(list != NULL && free_item != NULL);
while (!list_empty(list)) {
item = link_pop(list->head.next);
@@ -81,7 +64,7 @@ list_free(struct list *list, void (*free_item)(void *), int offset)
void
list_clear(struct list *list)
{
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
while (!list_empty(list))
list_pop_back(list);
@@ -90,7 +73,7 @@ list_clear(struct list *list)
bool
list_empty(struct list *list)
{
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
return list->head.next == &list->tail;
}
@@ -101,7 +84,7 @@ list_len(struct list *list)
struct link *link;
size_t len;
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
len = 0;
for (LIST_ITER(list, link))
@@ -116,7 +99,7 @@ list_insert_sorted(struct list *list, struct link *insert, bool reverse,
{
struct link *link;
- ASSERT(list != NULL && insert != NULL && in_order != NULL);
+ LIBLIST_ASSERT(list != NULL && insert != NULL && in_order != NULL);
/* Simple Insertion Sort */
/* cmp(a,b) -> (a-b) */
@@ -137,7 +120,7 @@ list_sort(struct list *list, bool reverse,
{
struct link *link, *cmp, *next;
- ASSERT(list != NULL && in_order != NULL);
+ LIBLIST_ASSERT(list != NULL && in_order != NULL);
/* Insertion Sort */
link = list->head.next;
@@ -176,7 +159,7 @@ list_at(struct list *list, int n)
{
struct link * link;
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
if (n >= 0)
link = link_iter_fwd(list->head.next, n);
@@ -213,7 +196,7 @@ list_push_back(struct list *list, struct link *link)
struct link *
list_pop_front(struct list *list)
{
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
if (list_empty(list))
return NULL;
@@ -224,7 +207,7 @@ list_pop_front(struct list *list)
struct link *
list_pop_back(struct list *list)
{
- ASSERT(list != NULL);
+ LIBLIST_ASSERT(list != NULL);
if (list_empty(list))
return NULL;
@@ -235,7 +218,7 @@ list_pop_back(struct list *list)
void
link_prepend(struct link *cur, struct link *link)
{
- ASSERT(cur != NULL && link != NULL);
+ LIBLIST_ASSERT(cur != NULL && link != NULL);
link->prev = cur->prev;
link->next = cur;
@@ -249,7 +232,7 @@ link_prepend(struct link *cur, struct link *link)
void
link_append(struct link *cur, struct link *link)
{
- ASSERT(cur != NULL && link != NULL);
+ LIBLIST_ASSERT(cur != NULL && link != NULL);
link->prev = cur;
link->next = cur->next;
@@ -263,7 +246,7 @@ link_append(struct link *cur, struct link *link)
bool
link_inuse(struct link *link)
{
- ASSERT(link != NULL);
+ LIBLIST_ASSERT(link != NULL);
return link->prev != NULL && link->next != NULL;
}
@@ -280,7 +263,7 @@ link_iter(struct link *link, int n)
struct link *
link_pop(struct link *link)
{
- ASSERT(link != NULL);
+ LIBLIST_ASSERT(link != NULL);
if (link->prev)
link->prev->next = link->next;