From 193a7be520502f24e995b71768dd89c37b3776eb Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Tue, 13 Jun 2023 22:19:05 +0000 Subject: Fix iter_func type; add tain_earliest Signed-off-by: Laurent Bercot --- src/include/skalibs/functypes.h | 2 +- src/include/skalibs/tai.h | 6 ++++++ src/libdatastruct/genset_deepfree.c | 4 ++-- src/libdatastruct/genset_iter_nocancel.c | 2 ++ src/libdatastruct/genset_iter_withcancel.c | 1 + src/libdatastruct/gensetdyn_deepfree.c | 4 ++-- src/libdatastruct/gensetdyn_iter.c | 27 ------------------------- src/libdatastruct/gensetdyn_iter_nocancel.c | 29 +++++++++++++++++++++++++++ src/libdatastruct/gensetdyn_iter_withcancel.c | 1 + src/libstddjb/tain_earliest1.c | 8 ++++++++ src/libstddjb/tain_earliestv.c | 9 +++++++++ 11 files changed, 61 insertions(+), 32 deletions(-) delete mode 100644 src/libdatastruct/gensetdyn_iter.c create mode 100644 src/libdatastruct/gensetdyn_iter_nocancel.c create mode 100644 src/libstddjb/tain_earliest1.c create mode 100644 src/libstddjb/tain_earliestv.c (limited to 'src') diff --git a/src/include/skalibs/functypes.h b/src/include/skalibs/functypes.h index 958b16f..b015f53 100644 --- a/src/include/skalibs/functypes.h +++ b/src/include/skalibs/functypes.h @@ -19,7 +19,7 @@ typedef cmp_func *cmp_func_ref ; typedef void *dtok_func (uint32_t, void *) ; typedef dtok_func *dtok_func_ref ; -typedef int iter_func (char *, void *) ; +typedef int iter_func (void *, void *) ; typedef iter_func *iter_func_ref ; typedef void free_func (void *) ; diff --git a/src/include/skalibs/tai.h b/src/include/skalibs/tai.h index 8bf2386..0e38b8c 100644 --- a/src/include/skalibs/tai.h +++ b/src/include/skalibs/tai.h @@ -125,6 +125,12 @@ extern int tain_sub (tain *, tain const *, tain const *) ; extern int tain_less (tain const *, tain const *) gccattr_pure ; #define tain_future(deadline) tain_less(&STAMP, (deadline)) +extern void tain_earliest1 (tain *, tain const *) ; +extern void tain_earliestv (tain *, tain const *const *, unsigned int) ; +#define tain_array(...) ((tain const *const[]){__VA_ARGS__}) +#define tain_earliestn(t, n, ...) tain_earliestv(t, tain_array(__VA_ARGS__), (n)) +#define tain_earliest(t, ...) tain_earliestn(t, sizeof(tain_array(__VA_ARGS__))/sizeof(tain const *), __VA_ARGS__) + #define TAIN_PACK 12 extern void tain_pack (char *, tain const *) ; extern void tain_unpack (char const *, tain *) ; diff --git a/src/libdatastruct/genset_deepfree.c b/src/libdatastruct/genset_deepfree.c index c58a5f9..7b4a728 100644 --- a/src/libdatastruct/genset_deepfree.c +++ b/src/libdatastruct/genset_deepfree.c @@ -3,10 +3,10 @@ #include #include -static int freeiter (char *s, void *aux) +static int freeiter (void *s, void *aux) { free_func_ref f = aux ; - (*f)((void *)s) ; + (*f)(s) ; return 1 ; } diff --git a/src/libdatastruct/genset_iter_nocancel.c b/src/libdatastruct/genset_iter_nocancel.c index 339f4b6..28da323 100644 --- a/src/libdatastruct/genset_iter_nocancel.c +++ b/src/libdatastruct/genset_iter_nocancel.c @@ -1,5 +1,7 @@ /* ISC license. */ +#include + #include #include diff --git a/src/libdatastruct/genset_iter_withcancel.c b/src/libdatastruct/genset_iter_withcancel.c index 1220dbc..6b70c68 100644 --- a/src/libdatastruct/genset_iter_withcancel.c +++ b/src/libdatastruct/genset_iter_withcancel.c @@ -2,6 +2,7 @@ #include #include + #include int genset_iter_withcancel (genset *g, iter_func_ref f, iter_func_ref cancelf, void *stuff) diff --git a/src/libdatastruct/gensetdyn_deepfree.c b/src/libdatastruct/gensetdyn_deepfree.c index 2b0c87c..623120f 100644 --- a/src/libdatastruct/gensetdyn_deepfree.c +++ b/src/libdatastruct/gensetdyn_deepfree.c @@ -3,10 +3,10 @@ #include #include -static int freeiter (char *s, void *aux) +static int freeiter (void *s, void *aux) { free_func_ref f = aux ; - (*f)((void *)s) ; + (*f)(s) ; return 1 ; } diff --git a/src/libdatastruct/gensetdyn_iter.c b/src/libdatastruct/gensetdyn_iter.c deleted file mode 100644 index 8470adf..0000000 --- a/src/libdatastruct/gensetdyn_iter.c +++ /dev/null @@ -1,27 +0,0 @@ -/* ISC license. */ - -#include -#include -#include - -uint32_t gensetdyn_iter_nocancel (gensetdyn *g, uint32_t n, iter_func_ref f, void *stuff) -{ - /* - XXX: we may be called by a freeing function, so we cannot alloc - - XXX: so pray that the bitarray fits in the stack. - */ - unsigned char bits[bitarray_div8(g->storage.len) ? bitarray_div8(g->storage.len) : 1] ; - size_t i = 0 ; - uint32_t j = 0 ; - uint32_t *fl = genalloc_s(uint32_t, &g->freelist) ; - size_t sp = genalloc_len(uint32_t, &g->freelist) ; - bitarray_setn(bits, 0, g->storage.len) ; - - for (; i < sp ; i++) if (fl[i] < g->storage.len) bitarray_clear(bits, fl[i]) ; - for (i = 0 ; (i < g->storage.len) && (j < n) ; i++) if (bitarray_peek(bits, i)) - { - j++ ; - if (!(*f)(gensetdyn_p(g, i), stuff)) break ; - } - return j ; -} diff --git a/src/libdatastruct/gensetdyn_iter_nocancel.c b/src/libdatastruct/gensetdyn_iter_nocancel.c new file mode 100644 index 0000000..a06b6b5 --- /dev/null +++ b/src/libdatastruct/gensetdyn_iter_nocancel.c @@ -0,0 +1,29 @@ +/* ISC license. */ + +#include +#include + +#include +#include + +uint32_t gensetdyn_iter_nocancel (gensetdyn *g, uint32_t n, iter_func_ref f, void *stuff) +{ + /* + XXX: we may be called by a freeing function, so we cannot alloc - + XXX: so pray that the bitarray fits in the stack. + */ + unsigned char bits[bitarray_div8(g->storage.len) ? bitarray_div8(g->storage.len) : 1] ; + size_t i = 0 ; + uint32_t j = 0 ; + uint32_t *fl = genalloc_s(uint32_t, &g->freelist) ; + size_t sp = genalloc_len(uint32_t, &g->freelist) ; + bitarray_setn(bits, 0, g->storage.len) ; + + for (; i < sp ; i++) if (fl[i] < g->storage.len) bitarray_clear(bits, fl[i]) ; + for (i = 0 ; (i < g->storage.len) && (j < n) ; i++) if (bitarray_peek(bits, i)) + { + j++ ; + if (!(*f)(gensetdyn_p(g, i), stuff)) break ; + } + return j ; +} diff --git a/src/libdatastruct/gensetdyn_iter_withcancel.c b/src/libdatastruct/gensetdyn_iter_withcancel.c index 0a250ce..b883e58 100644 --- a/src/libdatastruct/gensetdyn_iter_withcancel.c +++ b/src/libdatastruct/gensetdyn_iter_withcancel.c @@ -2,6 +2,7 @@ #include #include + #include int gensetdyn_iter_withcancel (gensetdyn *g, iter_func_ref f, iter_func_ref cancelf, void *stuff) diff --git a/src/libstddjb/tain_earliest1.c b/src/libstddjb/tain_earliest1.c new file mode 100644 index 0000000..af6ba3a --- /dev/null +++ b/src/libstddjb/tain_earliest1.c @@ -0,0 +1,8 @@ +/* ISC license. */ + +#include + +void tain_earliest1 (tain *t, tain const *u) +{ + if (tain_less(u, t)) *t = *u ; +} diff --git a/src/libstddjb/tain_earliestv.c b/src/libstddjb/tain_earliestv.c new file mode 100644 index 0000000..f4fd36b --- /dev/null +++ b/src/libstddjb/tain_earliestv.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include + +void tain_earliestv (tain *t, tain const *const *v, unsigned int n) +{ + for (unsigned int i = 0 ; i < n ; i++) + if (tain_less(v[i], t)) *t = *v[i] ; +} -- cgit v1.2.3