liblist-c

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

commit d8a1937d0f759517cf06c87e6866c8cfd30d162d
parent a1eba38afd00c5c8be0a6c66684c7ace10afb3e5
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 14 Feb 2022 15:39:45 +0100

Switched to more stable / sane types

Diffstat:
Minclude/list.h | 5+++--
Msrc/list.c | 18++++++++++--------
Msrc/test.c | 2+-
3 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/include/list.h b/include/list.h @@ -1,5 +1,6 @@ #pragma once +#include <stdbool.h> #include <stdlib.h> #define LINK_OFFSET(type, member) ((size_t) &((type *)0)->member) @@ -30,8 +31,8 @@ struct list { 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); +bool list_empty(struct list *list); +size_t list_len(struct list *list); struct link *list_at(struct list *list, int n); struct link *list_front(struct list *list); diff --git a/src/list.c b/src/list.c @@ -1,28 +1,30 @@ #include "list.h" +#include <stdbool.h> #include <stdlib.h> #include <stdio.h> #define ASSERT(x) assert((x), __FILE__, __LINE__, #x) -static void assert(int cond, const char *file, int line, const char *condstr); +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(int cond, const char *file, int line, const char *condstr) +assert(bool cond, const char *file, size_t line, const char *condstr) { if (cond) return; - fprintf(stderr, "CLIST: Assertion failed at %s:%i (%s)\n", + 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) { - int i; + size_t i; ASSERT(link != NULL); @@ -37,7 +39,7 @@ link_iter_fwd(struct link *link, size_t n) struct link * link_iter_bwd(struct link *link, size_t n) { - int i; + size_t i; ASSERT(link != NULL); @@ -74,7 +76,7 @@ list_free(struct list *list, void (*free_item)(void *), int offset) } } -int +bool list_empty(struct list *list) { ASSERT(list != NULL); @@ -82,11 +84,11 @@ list_empty(struct list *list) return list->head.next == &list->tail; } -int +size_t list_len(struct list *list) { struct link *iter; - int len; + size_t len; ASSERT(list != NULL); diff --git a/src/test.c b/src/test.c @@ -24,7 +24,7 @@ main(int argc, const char **argv) if (!item) return 0; item->str = *arg; item->link = LINK_EMPTY; - list_push_back(&list, LINK(item)); + list_push_back(&list, &item->link); } for (LIST_ITER(&list, iter)) {