libbitvec-c

C bit vector library
git clone https://git.sinitax.com/sinitax/libbitvec-c
Log | Files | Refs | LICENSE | sfeed.txt

commit fc0aa2c9c84fc09027f23d104917f4dd9b67b354
parent 0f9d382d4a5e3377381146274186dd86408526bb
Author: Louis Burda <quent.burda@gmail.com>
Date:   Mon, 13 Mar 2023 21:10:03 +0100

Return errno directly for thread-safety

Diffstat:
Minclude/bitvec.h | 4+---
Mlibbitvec.api | 2--
Mlibbitvec.lds | 4+---
Msrc/bitvec.c | 45++++++++++++++++-----------------------------
Msrc/test.c | 22+++++++++++-----------
5 files changed, 29 insertions(+), 48 deletions(-)

diff --git a/include/bitvec.h b/include/bitvec.h @@ -15,12 +15,10 @@ struct bitvec { bitvec_slot_t *data; }; -extern int libbitvec_errno; - int bitvec_init(struct bitvec *vec, size_t cap); void bitvec_deinit(struct bitvec *vec); -struct bitvec *bitvec_alloc(size_t cap); +int bitvec_alloc(struct bitvec **vec, size_t cap); void bitvec_free(struct bitvec *vec); int bitvec_reserve(struct bitvec *vec, size_t cnt); diff --git a/libbitvec.api b/libbitvec.api @@ -1,5 +1,3 @@ -libbitvec_errno - bitvec_init bitvec_deinit diff --git a/libbitvec.lds b/libbitvec.lds @@ -1,7 +1,5 @@ -LIBBITVEC_1.1 { +LIBBITVEC_1.2 { global: - libbitvec_errno; - bitvec_init; bitvec_deinit; diff --git a/src/bitvec.c b/src/bitvec.c @@ -7,14 +7,14 @@ #define SLOT_BYTES sizeof(bitvec_slot_t) #define SLOT_BITS (SLOT_BYTES * 8) -#define SLOT_MAX (~(bitvec_slot_t)0) +#define SLOT_MAX (~((bitvec_slot_t) 0)) #define CEILDIV(n, d) ((n) / (d) + ((n) % (d) == 0 ? 0 : 1)) #define BITCEIL(n) ((n) + SLOT_BITS - 1 - (SLOT_BITS - 1 + n) % SLOT_BITS) #define SLOT(n) ((n) / SLOT_BITS) #define SLOTCNT(n) CEILDIV(n, SLOT_BITS) -#define SLOT_BIT(n) (((bitvec_slot_t)1) << n) +#define SLOT_BIT(n) (((bitvec_slot_t) 1) << n) #define APPLY_MASK(x,s,m) ((s) ? ((x) | (m)) : ((x) & ~(m))) @@ -24,8 +24,6 @@ #define LIBBITVEC_CHECK(x) #endif -int libbitvec_errno = 0; - static inline void libbitvec_assert(int cond, const char *file, int line, const char *condstr) { @@ -43,10 +41,7 @@ bitvec_init(struct bitvec *vec, size_t cap) if (cap) { vec->data = calloc(SLOTCNT(cap), SLOT_BYTES); - if (!vec->data) { - libbitvec_errno = errno; - return libbitvec_errno; - } + if (!vec->data) return errno; } else { vec->data = NULL; } @@ -65,23 +60,21 @@ bitvec_deinit(struct bitvec *vec) free(vec->data); } -struct bitvec * -bitvec_alloc(size_t cap) +int +bitvec_alloc(struct bitvec **bitvec, size_t cap) { - struct bitvec *bitvec; + int rc; - bitvec = malloc(sizeof(struct bitvec)); - if (!bitvec) { - libbitvec_errno = errno; - return NULL; - } + *bitvec = malloc(sizeof(struct bitvec)); + if (!*bitvec) return errno; - if (!bitvec_init(bitvec, cap)) { + rc = bitvec_init(*bitvec, cap); + if (rc) { free(bitvec); - return NULL; + return rc; } - return bitvec; + return 0; } void @@ -99,13 +92,10 @@ bitvec_reserve(struct bitvec *vec, size_t cnt) LIBBITVEC_CHECK(vec != NULL); cnt = BITCEIL(cnt); - if (vec->cap >= cnt) return true; + if (vec->cap >= cnt) return 0; alloc = realloc(vec->data, SLOTCNT(cnt) * SLOT_BYTES); - if (!alloc) { - libbitvec_errno = errno; - return libbitvec_errno; - } + if (!alloc) return errno; alloc = vec->data; memset(vec->data + SLOT(vec->cap), 0, SLOT(cnt) - SLOT(vec->cap)); vec->cap = cnt; @@ -121,13 +111,10 @@ bitvec_shrink(struct bitvec *vec, size_t cnt) LIBBITVEC_CHECK(vec != NULL); cnt = BITCEIL(cnt); - if (vec->cap <= cnt) return true; + if (vec->cap <= cnt) return 0; alloc = realloc(vec->data, SLOTCNT(cnt)); - if (!alloc) { - libbitvec_errno = errno; - return libbitvec_errno; - } + if (!alloc) return errno; vec->data = alloc; vec->cap = cnt; diff --git a/src/test.c b/src/test.c @@ -5,25 +5,25 @@ #include <stdio.h> #include <string.h> -#define LIBBITVEC_ERR() errx(1, "test: libbitvec: %s", strerror(libbitvec_errno)) +#define LIBBITVEC_ERR(rc) errx(1, "libbitvec: %s", strerror(rc)) int main(int argc, const char **argv) { struct bitvec bitvec; - int i, ret; - int *val; + int i, rc; + int val; - ret = bitvec_init(&bitvec, 10); - if (ret) LIBBITVEC_ERR(); + rc = bitvec_init(&bitvec, 10); + if (rc) LIBBITVEC_ERR(rc); for (i = 1; i < argc; i++) { - *val = atoi(argv[i]); - if (bitvec_get(&bitvec, *val)) - printf("%i -> dup!\n", *val); - ret = bitvec_reserve(&bitvec, *val); - if (ret) LIBBITVEC_ERR(); - bitvec_set(&bitvec, *val); + val = atoi(argv[i]); + if (bitvec_get(&bitvec, val)) + printf("%i -> dup!\n", val); + rc = bitvec_reserve(&bitvec, val); + if (rc) LIBBITVEC_ERR(rc); + bitvec_set(&bitvec, val); } for (i = 0; i < 10; i++)