From c51bf89a72dcd450b1d455200ac4493da92153d8 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 26 Nov 2023 19:07:49 +0800 Subject: [PATCH 001/119] Release version 1.0 From 60de2e6d459f31c64e971e5929420b1330a43ba2 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 28 Nov 2023 00:00:08 +0800 Subject: [PATCH 002/119] docs align: added api comments Signed-off-by: John Sanpe --- include/bfdev/align.h | 63 ++++++++++++++++++++++++++++++------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/include/bfdev/align.h b/include/bfdev/align.h index 74bd2c58..440e963c 100644 --- a/include/bfdev/align.h +++ b/include/bfdev/align.h @@ -13,33 +13,68 @@ BFDEV_BEGIN_DECLS /** - * align_low/high - aligned value. - * @size: value to aligned. + * bfdev_align_check() - Check if an value is aligned. + * @value: the value being checked for alignment. + * @align: check alignment size. + * + * Alignment must be a power of 2. + */ +#define bfdev_align_check(value, align) ( \ + !((value) & ((align) - 1)) \ +) + +/** + * bfdev_align_ptr_check() - Check if an pointer is aligned. + * @ptr: the pointer being checked for alignment. + * @align: check alignment size. + */ +#define bfdev_align_ptr_check(ptr, align) ( \ + bfdev_align_check((uintptr_t)(ptr), align) \ +) + +/** + * bfdev_align_low() - Return an downward-aligned integer value. + * @value: value to aligned. * @align: alignment size. */ -#define bfdev_align_low(size, align) ({ \ - (size) & ~((align) - 1); \ +#define bfdev_align_low(value, align) ({ \ + (value) & ~((align) - 1); \ }) -#define bfdev_align_high(size, align) ({ \ +/** + * bfdev_align_high() - Return an upward-aligned value. + * @value: value to aligned. + * @align: alignment size. + */ +#define bfdev_align_high(value, align) ({ \ typeof(align) _align = (align); \ - ((size) + (_align - 1)) & ~(_align - 1); \ + ((value) + (_align - 1)) & ~(_align - 1); \ }) +/** + * bfdev_align_ptr_low() - Return an downward-aligned pointer. + * @ptr: pointer to aligned. + * @align: alignment size. + */ #define bfdev_align_ptr_low(ptr, align) ({ \ (typeof(ptr))bfdev_align_low((uintptr_t)(ptr), align); \ }) +/** + * bfdev_align_ptr_high() - Return an upward-aligned pointer. + * @ptr: pointer to aligned. + * @align: alignment size. + */ #define bfdev_align_ptr_high(ptr, align) ({ \ (typeof(ptr))bfdev_align_high((uintptr_t)(ptr), align); \ }) -#define bfdev_align_low_adj(size, align) ({ \ - (size) = bfdev_align_low(size, align); \ +#define bfdev_align_low_adj(value, align) ({ \ + (value) = bfdev_align_low(value, align); \ }) -#define bfdev_align_high_adj(size, align) ({ \ - (size) = bfdev_align_high(size, align); \ +#define bfdev_align_high_adj(value, align) ({ \ + (value) = bfdev_align_high(value, align); \ }) #define bfdev_align_ptr_low_adj(ptr, align) ({ \ @@ -50,14 +85,6 @@ BFDEV_BEGIN_DECLS (ptr) = bfdev_align_ptr_high(ptr, align); \ }) -#define bfdev_align_check(size, align) ( \ - !((size) & ((align) - 1)) \ -) - -#define bfdev_align_ptr_check(ptr, align) ( \ - bfdev_align_check((uintptr_t)(ptr), align) \ -) - BFDEV_END_DECLS #endif /* _BFDEV_ALIGN_H_ */ From 3a4cf131a913d9a8e677f0eca18f735a9f225a61 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 28 Nov 2023 22:46:42 +0800 Subject: [PATCH 003/119] fixup bitfield: fixed incorrect bitops Signed-off-by: John Sanpe --- include/bfdev/bitfield.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/include/bfdev/bitfield.h b/include/bfdev/bitfield.h index 95866e5c..2d68e6c5 100644 --- a/include/bfdev/bitfield.h +++ b/include/bfdev/bitfield.h @@ -17,7 +17,8 @@ BFDEV_BEGIN_DECLS * @reg: value of entire bitfield. */ #define BFDEV_FIELD_GET(mask, reg) ({ \ - (typeof(mask))(((reg) & (mask)) >> bfdev_ffs(mask)); \ + typeof(mask) _mask = (mask); \ + (typeof(mask))(((reg) & (_mask)) >> bfdev_ffsuf(_mask)); \ }) /** @@ -26,24 +27,27 @@ BFDEV_BEGIN_DECLS * @val: value to put in the field. */ #define BFDEV_FIELD_PREP(mask, val) ({ \ - ((typeof(mask))(val) << bfdev_ffs(mask)) & (mask); \ + typeof(mask) _mask = (mask); \ + ((typeof(mask))(val) << bfdev_ffsuf(_mask)) & (_mask); \ }) /** * BFDEV_FIELD_FIT() - check if value fits in the field. - * @_mask: shifted mask defining the field's length and position. - * @_val: value to test against the field. + * @mask: shifted mask defining the field's length and position. + * @val: value to test against the field. */ #define BFDEV_FIELD_FIT(mask, val) ({ \ - !((((typeof(mask))(val)) << bfdev_ffs(mask)) & ~(mask)); \ + typeof(mask) _mask = (mask); \ + !((((typeof(mask))(val)) << bfdev_ffsuf(_mask)) & ~(_mask)); \ }) /** * BFDEV_FIELD_MAX() - produce the maximum value representable by a field. - * @_mask: shifted mask defining the field's length and position. + * @mask: shifted mask defining the field's length and position. */ #define BFDEV_FIELD_MAX(mask) ({ \ - (typeof(mask))((mask) >> bfdev_ffs(mask)); \ + typeof(mask) _mask = (mask); \ + (typeof(mask))((_mask) >> bfdev_ffsuf(_mask)); \ }) BFDEV_END_DECLS From ae379f2768145b2395dfb9edb2afdfab45b83d5c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 29 Nov 2023 23:48:19 +0800 Subject: [PATCH 004/119] docs minmax: fixed some typo Signed-off-by: John Sanpe --- include/bfdev/minmax.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/bfdev/minmax.h b/include/bfdev/minmax.h index 4cd42dca..dd8bb0ce 100644 --- a/include/bfdev/minmax.h +++ b/include/bfdev/minmax.h @@ -11,7 +11,7 @@ BFDEV_BEGIN_DECLS /** - * min - return minimum of two values of the same or compatible types. + * bfdev_min() - return minimum of two values of the same or compatible types. * @a: first value. * @b: second value. */ @@ -23,7 +23,7 @@ BFDEV_BEGIN_DECLS }) /** - * max - return maximum of two values of the same or compatible types. + * bfdev_max() - return maximum of two values of the same or compatible types. * @a: first value. * @b: second value. */ @@ -35,7 +35,7 @@ BFDEV_BEGIN_DECLS }) /** - * clamp - return a value clamped to a given range with strict typechecking. + * bfdev_clamp() - return a value clamped to a given range with strict typechecking. * @val: current value. * @lo: lowest allowable value. * @hi: highest allowable value. @@ -45,7 +45,7 @@ BFDEV_BEGIN_DECLS ) /** - * min_adj - Adjust the minimum value of @val. + * bfdev_min_adj() - Adjust the minimum value of @val. * @val: value to adjust. * @lo: lowest allowable value. */ @@ -54,7 +54,7 @@ BFDEV_BEGIN_DECLS }) /** - * max_adj - Adjust the maximum value of @val. + * bfdev_max_adj() - Adjust the maximum value of @val. * @val: value to adjust. * @hi: highest allowable value. */ @@ -63,7 +63,7 @@ BFDEV_BEGIN_DECLS }) /** - * clamp_adj - Adjust the clamped value of @val. + * bfdev_clamp_adj() - Adjust the clamped value of @val. * @val: value to adjust. * @lo: lowest allowable value. * @hi: highest allowable value. From ef5b340b1084cf884f7f0793ebc9e833c192bae1 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 2 Dec 2023 00:00:14 +0800 Subject: [PATCH 005/119] fixup examples: added conflict prevention prefix to time Signed-off-by: John Sanpe --- examples/skiplist/benchmark.c | 1 + examples/time.h | 71 +++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/examples/skiplist/benchmark.c b/examples/skiplist/benchmark.c index 06babc0b..eb643fc7 100644 --- a/examples/skiplist/benchmark.c +++ b/examples/skiplist/benchmark.c @@ -49,6 +49,7 @@ int main(int argc, const char *argv[]) struct bfdev_skip_node *node; uintptr_t value, *record; unsigned int count; + int retval; record = malloc(TEST_LEN * sizeof(*record)); if (!record) diff --git a/examples/time.h b/examples/time.h index a874f6fd..40c6500e 100644 --- a/examples/time.h +++ b/examples/time.h @@ -3,6 +3,9 @@ * Copyright(c) 2023 John Sanpe */ +#ifndef _EXAMPLES_TIME_H_ +#define _EXAMPLES_TIME_H_ + #include #include #include @@ -19,45 +22,47 @@ time_dump(double ticks, clock_t start, clock_t end, struct tms *stms, struct tms } #define EXAMPLE_TIME_STATISTICAL(codeblock...) ({ \ - struct tms start_tms, stop_tms; \ - clock_t start, stop; \ - unsigned int ticks; \ - int retval; \ + struct tms _start_tms, _stop_tms; \ + clock_t _start, _stop; \ + unsigned int _ticks; \ + int _retval; \ \ - ticks = sysconf(_SC_CLK_TCK); \ - start = times(&start_tms); \ + _ticks = sysconf(_SC_CLK_TCK); \ + _start = times(&_start_tms); \ \ - retval = ({ \ + _retval = ({ \ codeblock \ }); \ \ - stop = times(&stop_tms); \ - time_dump((double)ticks, start, stop, \ - &start_tms, &stop_tms); \ + _stop = times(&_stop_tms); \ + time_dump((double)_ticks, _start, _stop, \ + &_start_tms, &_stop_tms); \ \ - retval; \ + _retval; \ }) -#define EXAMPLE_TIME_LOOP(loop, time, codeblock...) ({ \ - struct timeval curr_timval, stop_timval; \ - int retval; \ - \ - gettimeofday(&stop_timval, NULL); \ - stop_timval.tv_sec += (time) / 1000; \ - stop_timval.tv_usec += ((time) % 1000) * 1000; \ - *(loop) = 0; \ - \ - do { \ - retval = ({ \ - codeblock \ - }); \ - \ - if (retval) \ - break; \ - \ - ++*(loop); \ - gettimeofday(&curr_timval, NULL); \ - } while (timercmp(&curr_timval, &stop_timval, <)); \ - \ - retval; \ +#define EXAMPLE_TIME_LOOP(loop, time, codeblock...) ({ \ + struct timeval _curr_timval, _stop_timval; \ + int _retval; \ + \ + gettimeofday(&_stop_timval, NULL); \ + _stop_timval.tv_sec += (time) / 1000; \ + _stop_timval.tv_usec += ((time) % 1000) * 1000; \ + *(loop) = 0; \ + \ + do { \ + _retval = ({ \ + codeblock \ + }); \ + \ + if (_retval) \ + break; \ + \ + ++*(loop); \ + gettimeofday(&_curr_timval, NULL); \ + } while (timercmp(&_curr_timval, &_stop_timval, <)); \ + \ + _retval; \ }) + +#endif /* _EXAMPLES_TIME_H_ */ From c367e16e71fb6e3f13a26ad4cd6cc1c62566fb3e Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 2 Dec 2023 00:01:27 +0800 Subject: [PATCH 006/119] feat hashmap: added benchmark example Signed-off-by: John Sanpe --- examples/hashmap/CMakeLists.txt | 6 ++ examples/hashmap/benchmark.c | 123 ++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 examples/hashmap/benchmark.c diff --git a/examples/hashmap/CMakeLists.txt b/examples/hashmap/CMakeLists.txt index 29a2d806..3e000db7 100644 --- a/examples/hashmap/CMakeLists.txt +++ b/examples/hashmap/CMakeLists.txt @@ -7,15 +7,21 @@ add_executable(hashmap-simple simple.c) target_link_libraries(hashmap-simple bfdev) add_test(hashmap-simple hashmap-simple) +add_executable(hashmap-benchmark benchmark.c) +target_link_libraries(hashmap-benchmark bfdev) +add_test(hashmap-benchmark hashmap-benchmark) + if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES simple.c + benchmark.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/hashmap ) install(TARGETS hashmap-simple + hashmap-benchmark DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin ) diff --git a/examples/hashmap/benchmark.c b/examples/hashmap/benchmark.c new file mode 100644 index 00000000..5e83ccf3 --- /dev/null +++ b/examples/hashmap/benchmark.c @@ -0,0 +1,123 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "hashmap-benchmark" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include "../time.h" + +#define TEST_LOOP 1000000 + +struct test_node { + struct bfdev_hlist_node node; + unsigned long value; +}; + +#define node_to_test(ptr) \ + bfdev_container_of(ptr, struct test_node, node) + +static inline unsigned long +test_hash_key(const void *key, void *pdata) +{ + return (unsigned long)key; +} + +static inline unsigned long +test_hash_node(const struct bfdev_hlist_node *node, void *pdata) +{ + const struct test_node *tnode = node_to_test(node); + return tnode->value; +} + +static inline long +test_equal(const struct bfdev_hlist_node *node1, + const struct bfdev_hlist_node *nodeb, void *pdata) +{ + const struct test_node *tnode1 = node_to_test(node1); + const struct test_node *tnodeb = node_to_test(nodeb); + return tnode1->value - tnodeb->value; +} + +static inline long +test_find(const struct bfdev_hlist_node *node, const void *key, void *pdata) +{ + const struct test_node *tnode = node_to_test(node); + return tnode->value - (unsigned long)key; +} + +static struct bfdev_hashmap_ops +test_ops = { + .hash_key = test_hash_key, + .hash_node = test_hash_node, + .equal = test_equal, + .find = test_find, +}; + +int main(int argc, const char *argv[]) +{ + struct test_node *nodes; + struct bfdev_hlist_node *hnode; + unsigned long value; + unsigned int count; + void *block; + int retval; + + BFDEV_DEFINE_HASHMAP(test_map, NULL, &test_ops, NULL); + nodes = block = malloc(sizeof(*nodes) * TEST_LOOP); + if (!block) { + bfdev_log_err("Insufficient memory!\n"); + return 1; + } + + srand(time(NULL)); + bfdev_log_info("Generate %u node:\n", TEST_LOOP); + for (count = 0; count < TEST_LOOP; ++count) + nodes[count].value = ((uint64_t)rand() << 32) | rand(); + + bfdev_log_info("Insert nodes:\n"); + EXAMPLE_TIME_STATISTICAL( + for (count = 0; count < TEST_LOOP; ++count) { + value = ((uint64_t)rand() << 32) | rand(); + nodes[count].value = value; + + retval = bfdev_hashmap_add(&test_map, &nodes[count].node); + if (retval) + return retval; + } + 0; + ); + + bfdev_log_info("Find nodes:\n"); + EXAMPLE_TIME_STATISTICAL( + for (count = 0; count < TEST_LOOP; ++count) { + value = nodes[count].value; + hnode = bfdev_hashmap_find(&test_map, (void *)value); + if (!hnode) + return 1; + } + 0; + ); + + bfdev_log_info("Delete nodes:\n"); + EXAMPLE_TIME_STATISTICAL( + for (count = 0; count < TEST_LOOP; ++count) { + value = nodes[count].value; + retval = bfdev_hashmap_del(&test_map, (void *)value, &hnode); + if (retval) + return retval; + } + 0; + ); + + bfdev_log_info("Done.\n"); + bfdev_hashmap_release(&test_map); + free(nodes); + + return 0; +} From 706e4d7dbd970312e9b7361d6d19a0676eba4fca Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 4 Dec 2023 00:00:38 +0800 Subject: [PATCH 007/119] refactor headers: separate platform compatibility layer Signed-off-by: John Sanpe --- examples/matrix/benchmark.c | 1 + include/base.h | 11 +++++------ include/bfdev/action.h | 2 +- include/bfdev/align.h | 2 +- include/bfdev/allocator.h | 1 - include/bfdev/asm-generic/bitops.h | 2 +- include/bfdev/asm-generic/cmpxchg.h | 2 +- include/bfdev/bitops.h | 3 ++- include/bfdev/bitwalk.h | 3 ++- include/bfdev/bloom.h | 3 ++- include/bfdev/ctype.h | 7 +------ include/bfdev/errname.h | 3 ++- include/bfdev/fsm.h | 2 +- include/bfdev/guards.h | 1 + include/bfdev/heap.h | 1 - include/bfdev/hlist.h | 2 +- include/bfdev/limits.h | 8 +------- include/bfdev/list.h | 1 - include/bfdev/log.h | 1 + include/bfdev/once.h | 3 ++- include/bfdev/overflow.h | 2 +- include/bfdev/popcount.h | 2 +- include/bfdev/port/ctype.h | 21 +++++++++++++++++++++ include/bfdev/port/limits.h | 22 ++++++++++++++++++++++ include/bfdev/port/stdarg.h | 21 +++++++++++++++++++++ include/bfdev/{ => port}/stdbool.h | 6 +++--- include/bfdev/port/stddef.h | 21 +++++++++++++++++++++ include/bfdev/{ => port}/stdint.h | 6 +++--- include/bfdev/port/stdio.h | 21 +++++++++++++++++++++ include/bfdev/port/stdlib.h | 21 +++++++++++++++++++++ include/bfdev/port/string.h | 21 +++++++++++++++++++++ include/bfdev/rbtree.h | 1 - include/bfdev/refcount.h | 1 - include/bfdev/scnprintf.h | 3 ++- include/bfdev/slist.h | 2 +- include/bfdev/stdarg.h | 7 +------ include/bfdev/stddef.h | 8 ++------ include/bfdev/stdio.h | 7 +------ include/bfdev/stdlib.h | 7 +------ include/bfdev/string.h | 7 +------ include/bfdev/titer.h | 1 + include/bfdev/types.h | 4 ++-- src/fifo.c | 4 +--- src/ringbuf.c | 4 +--- 44 files changed, 196 insertions(+), 83 deletions(-) create mode 100644 include/bfdev/port/ctype.h create mode 100644 include/bfdev/port/limits.h create mode 100644 include/bfdev/port/stdarg.h rename include/bfdev/{ => port}/stdbool.h (73%) create mode 100644 include/bfdev/port/stddef.h rename include/bfdev/{ => port}/stdint.h (74%) create mode 100644 include/bfdev/port/stdio.h create mode 100644 include/bfdev/port/stdlib.h create mode 100644 include/bfdev/port/string.h diff --git a/examples/matrix/benchmark.c b/examples/matrix/benchmark.c index 86c2964d..5e433bee 100644 --- a/examples/matrix/benchmark.c +++ b/examples/matrix/benchmark.c @@ -7,6 +7,7 @@ #define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt #include +#include #include #include #include "../time.h" diff --git a/include/base.h b/include/base.h index ae139221..39fb5b6c 100644 --- a/include/base.h +++ b/include/base.h @@ -8,24 +8,23 @@ #include #include -#include -#include #include -#include #include #include #include -#include -#include -#include #include +#include #include #include #include +#include +#include +#include #include #include #include +#include #include #endif /* _LOCAL_BASE_H_ */ diff --git a/include/bfdev/action.h b/include/bfdev/action.h index c3210d7d..dcf083a2 100644 --- a/include/bfdev/action.h +++ b/include/bfdev/action.h @@ -7,7 +7,7 @@ #define _BFDEV_ACTION_H_ #include -#include +#include #include #include diff --git a/include/bfdev/align.h b/include/bfdev/align.h index 74bd2c58..e5c5162d 100644 --- a/include/bfdev/align.h +++ b/include/bfdev/align.h @@ -7,7 +7,7 @@ #define _BFDEV_ALIGN_H_ #include -#include +#include #include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index 8243fe48..84f3ed41 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -10,7 +10,6 @@ #include #include #include -#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/asm-generic/bitops.h b/include/bfdev/asm-generic/bitops.h index b4658fe3..d13e0af4 100644 --- a/include/bfdev/asm-generic/bitops.h +++ b/include/bfdev/asm-generic/bitops.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include diff --git a/include/bfdev/asm-generic/cmpxchg.h b/include/bfdev/asm-generic/cmpxchg.h index 10318ab5..5d9a57d6 100644 --- a/include/bfdev/asm-generic/cmpxchg.h +++ b/include/bfdev/asm-generic/cmpxchg.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/bitops.h b/include/bfdev/bitops.h index 8bda4bca..1d501040 100644 --- a/include/bfdev/bitops.h +++ b/include/bfdev/bitops.h @@ -7,7 +7,8 @@ #define _BFDEV_BITOPS_H_ #include -#include +#include +#include #include #include #include diff --git a/include/bfdev/bitwalk.h b/include/bfdev/bitwalk.h index 6f5688b5..be7eaf2b 100644 --- a/include/bfdev/bitwalk.h +++ b/include/bfdev/bitwalk.h @@ -7,7 +7,8 @@ #define _BFDEV_BITWALK_H_ #include -#include +#include +#include #include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/bloom.h b/include/bfdev/bloom.h index 8003fb2d..dd7e707e 100644 --- a/include/bfdev/bloom.h +++ b/include/bfdev/bloom.h @@ -7,7 +7,8 @@ #define _BFDEV_BLOOM_H_ #include -#include +#include +#include #include #include diff --git a/include/bfdev/ctype.h b/include/bfdev/ctype.h index f0540980..f8de5754 100644 --- a/include/bfdev/ctype.h +++ b/include/bfdev/ctype.h @@ -7,12 +7,7 @@ #define _BFDEV_CTYPE_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/errname.h b/include/bfdev/errname.h index 513eec94..886f29f1 100644 --- a/include/bfdev/errname.h +++ b/include/bfdev/errname.h @@ -7,8 +7,9 @@ #define _BFDEV_ERRNAME_H_ #include -#include +#include #include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index 35a8cdd5..d7a0e749 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -7,8 +7,8 @@ #define _BFDEV_FSM_H_ #include +#include #include -#include #include #include diff --git a/include/bfdev/guards.h b/include/bfdev/guards.h index 57188976..613c6220 100644 --- a/include/bfdev/guards.h +++ b/include/bfdev/guards.h @@ -7,6 +7,7 @@ #define _BFDEV_GUARDS_H_ #include +#include #include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index 448a9430..659a18b0 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -9,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/include/bfdev/hlist.h b/include/bfdev/hlist.h index aed7389e..95952963 100644 --- a/include/bfdev/hlist.h +++ b/include/bfdev/hlist.h @@ -7,8 +7,8 @@ #define _BFDEV_HLIST_H_ #include +#include #include -#include #include #include diff --git a/include/bfdev/limits.h b/include/bfdev/limits.h index 70ab8144..08aa7f78 100644 --- a/include/bfdev/limits.h +++ b/include/bfdev/limits.h @@ -7,13 +7,7 @@ #define _BFDEV_LIMITS_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -# include -#else -# include -#endif +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/list.h b/include/bfdev/list.h index b8647ba6..46a6a8f3 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -9,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/include/bfdev/log.h b/include/bfdev/log.h index 8bc89769..58ef401a 100644 --- a/include/bfdev/log.h +++ b/include/bfdev/log.h @@ -7,6 +7,7 @@ #define _BFDEV_LOG_H_ #include +#include #include #include #include diff --git a/include/bfdev/once.h b/include/bfdev/once.h index e5d25666..4da2f0ef 100644 --- a/include/bfdev/once.h +++ b/include/bfdev/once.h @@ -7,7 +7,8 @@ #define _BFDEV_ONCE_H_ #include -#include +#include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/overflow.h b/include/bfdev/overflow.h index 851b1eb7..a3aa4cc3 100644 --- a/include/bfdev/overflow.h +++ b/include/bfdev/overflow.h @@ -8,7 +8,7 @@ #include #include -#include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/popcount.h b/include/bfdev/popcount.h index abdb596d..1a1826f4 100644 --- a/include/bfdev/popcount.h +++ b/include/bfdev/popcount.h @@ -8,7 +8,7 @@ #include #include -#include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/port/ctype.h b/include/bfdev/port/ctype.h new file mode 100644 index 00000000..1d02ad9b --- /dev/null +++ b/include/bfdev/port/ctype.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_CTYPE_H_ +#define _BFDEV_PORT_CTYPE_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_CTYPE_H_ */ diff --git a/include/bfdev/port/limits.h b/include/bfdev/port/limits.h new file mode 100644 index 00000000..e4b0623b --- /dev/null +++ b/include/bfdev/port/limits.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_LIMITS_H_ +#define _BFDEV_PORT_LIMITS_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_LIMITS_H_ */ diff --git a/include/bfdev/port/stdarg.h b/include/bfdev/port/stdarg.h new file mode 100644 index 00000000..ace68773 --- /dev/null +++ b/include/bfdev/port/stdarg.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_STDARG_H_ +#define _BFDEV_PORT_STDARG_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_STDARG_H_ */ diff --git a/include/bfdev/stdbool.h b/include/bfdev/port/stdbool.h similarity index 73% rename from include/bfdev/stdbool.h rename to include/bfdev/port/stdbool.h index fed55745..2c309873 100644 --- a/include/bfdev/stdbool.h +++ b/include/bfdev/port/stdbool.h @@ -3,8 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#ifndef _BFDEV_STDBOOL_H_ -#define _BFDEV_STDBOOL_H_ +#ifndef _BFDEV_PORT_STDBOOL_H_ +#define _BFDEV_PORT_STDBOOL_H_ #include @@ -18,4 +18,4 @@ BFDEV_BEGIN_DECLS BFDEV_END_DECLS -#endif /* _BFDEV_STDBOOL_H_ */ +#endif /* _BFDEV_PORT_STDBOOL_H_ */ diff --git a/include/bfdev/port/stddef.h b/include/bfdev/port/stddef.h new file mode 100644 index 00000000..08fea3e0 --- /dev/null +++ b/include/bfdev/port/stddef.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_STDDEF_H_ +#define _BFDEV_PORT_STDDEF_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_STDDEF_H_ */ diff --git a/include/bfdev/stdint.h b/include/bfdev/port/stdint.h similarity index 74% rename from include/bfdev/stdint.h rename to include/bfdev/port/stdint.h index 9fc26223..aa02ec33 100644 --- a/include/bfdev/stdint.h +++ b/include/bfdev/port/stdint.h @@ -3,8 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#ifndef _BFDEV_STDINT_H_ -#define _BFDEV_STDINT_H_ +#ifndef _BFDEV_PORT_STDINT_H_ +#define _BFDEV_PORT_STDINT_H_ #include @@ -18,4 +18,4 @@ BFDEV_BEGIN_DECLS BFDEV_END_DECLS -#endif /* _BFDEV_STDINT_H_ */ +#endif /* _BFDEV_PORT_STDINT_H_ */ diff --git a/include/bfdev/port/stdio.h b/include/bfdev/port/stdio.h new file mode 100644 index 00000000..bdf2040e --- /dev/null +++ b/include/bfdev/port/stdio.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_STDIO_H_ +#define _BFDEV_PORT_STDIO_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_STDIO_H_ */ diff --git a/include/bfdev/port/stdlib.h b/include/bfdev/port/stdlib.h new file mode 100644 index 00000000..755a2b09 --- /dev/null +++ b/include/bfdev/port/stdlib.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_STDLIB_H_ +#define _BFDEV_PORT_STDLIB_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_STDLIB_H_ */ diff --git a/include/bfdev/port/string.h b/include/bfdev/port/string.h new file mode 100644 index 00000000..0b43ce68 --- /dev/null +++ b/include/bfdev/port/string.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_PORT_STRING_H_ +#define _BFDEV_PORT_STRING_H_ + +#include + +#if defined(__FreeBSD__) && defined(_KERNEL) +# include +#else +# include +#endif + +BFDEV_BEGIN_DECLS + +BFDEV_END_DECLS + +#endif /* _BFDEV_PORT_STRING_H_ */ diff --git a/include/bfdev/rbtree.h b/include/bfdev/rbtree.h index dc6b1fac..17c881e9 100644 --- a/include/bfdev/rbtree.h +++ b/include/bfdev/rbtree.h @@ -10,7 +10,6 @@ #include #include #include -#include #include #include diff --git a/include/bfdev/refcount.h b/include/bfdev/refcount.h index df698c7e..b1b7db75 100644 --- a/include/bfdev/refcount.h +++ b/include/bfdev/refcount.h @@ -8,7 +8,6 @@ #include #include -#include #include #include #include diff --git a/include/bfdev/scnprintf.h b/include/bfdev/scnprintf.h index 199455a8..72370795 100644 --- a/include/bfdev/scnprintf.h +++ b/include/bfdev/scnprintf.h @@ -7,8 +7,9 @@ #define _BFDEV_SCNPRINTF_H_ #include -#include +#include #include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/slist.h b/include/bfdev/slist.h index e2abf488..bd72d2d0 100644 --- a/include/bfdev/slist.h +++ b/include/bfdev/slist.h @@ -7,8 +7,8 @@ #define _BFDEV_SLIST_H_ #include +#include #include -#include #include #include diff --git a/include/bfdev/stdarg.h b/include/bfdev/stdarg.h index 7dec9987..e2272d9a 100644 --- a/include/bfdev/stdarg.h +++ b/include/bfdev/stdarg.h @@ -7,12 +7,7 @@ #define _BFDEV_STDARG_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/stddef.h b/include/bfdev/stddef.h index 94c0908d..dbfc5789 100644 --- a/include/bfdev/stddef.h +++ b/include/bfdev/stddef.h @@ -7,12 +7,8 @@ #define _BFDEV_STDDEF_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif +#include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/stdio.h b/include/bfdev/stdio.h index 9b33cdc4..3bc7fd38 100644 --- a/include/bfdev/stdio.h +++ b/include/bfdev/stdio.h @@ -7,12 +7,7 @@ #define _BFDEV_STDIO_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/stdlib.h b/include/bfdev/stdlib.h index 91a3123d..9ed2fbd9 100644 --- a/include/bfdev/stdlib.h +++ b/include/bfdev/stdlib.h @@ -7,12 +7,7 @@ #define _BFDEV_STDLIB_H_ #include - -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/string.h b/include/bfdev/string.h index 03eacf37..ee1773af 100644 --- a/include/bfdev/string.h +++ b/include/bfdev/string.h @@ -7,14 +7,9 @@ #define _BFDEV_STRING_H_ #include +#include #include -#if defined(__FreeBSD__) && defined(_KERNEL) -# include -#else -# include -#endif - BFDEV_BEGIN_DECLS extern char * diff --git a/include/bfdev/titer.h b/include/bfdev/titer.h index 276fb33b..be18da3a 100644 --- a/include/bfdev/titer.h +++ b/include/bfdev/titer.h @@ -7,6 +7,7 @@ #define _BFDEV_TITER_H_ #include +#include #include #include diff --git a/include/bfdev/types.h b/include/bfdev/types.h index 40280d26..2622ae94 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -7,8 +7,8 @@ #define _BFDEV_TYPES_H_ #include -#include -#include +#include +#include BFDEV_BEGIN_DECLS diff --git a/src/fifo.c b/src/fifo.c index a761d68e..db9afc53 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -3,10 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#include -#include +#include #include -#include #include #include #include diff --git a/src/ringbuf.c b/src/ringbuf.c index c72ffd6c..d3c25e4d 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -3,10 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#include -#include +#include #include -#include #include #include #include From 08b5c78ab1d3e30549bdc1c1ebca8dc95077ea28 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 6 Dec 2023 00:00:44 +0800 Subject: [PATCH 008/119] docs ilist: added api comments Signed-off-by: John Sanpe --- include/bfdev/ilist.h | 24 +++++++++++++++++++----- src/ilist.c | 10 ---------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/bfdev/ilist.h b/include/bfdev/ilist.h index ab66d1e9..c616a4d4 100644 --- a/include/bfdev/ilist.h +++ b/include/bfdev/ilist.h @@ -41,15 +41,25 @@ BFDEV_CALLBACK_CMP( const struct bfdev_ilist_node * ); +/** + * bfdev_ilist_add() - insert a new index list node. + * @ihead: the index head to be insert. + * @inode: the index node to insert. + */ extern void bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, bfdev_ilist_cmp_t cmp, void *pdata); +/** + * bfdev_ilist_del() - delete a node form index head. + * @ihead: the index head to be delete. + * @inode: the index node to delete. + */ extern void bfdev_ilist_del(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode); /** - * bfdev_ilist_head_init - initialize a bfdev_ilist_head structure. + * bfdev_ilist_head_init() - initialize a bfdev_ilist_head structure. * @head: bfdev_ilist_head structure to be initialized. */ static inline void @@ -58,6 +68,10 @@ bfdev_ilist_head_init(struct bfdev_ilist_head *ihead) bfdev_list_head_init(&ihead->node_list); } +/** + * bfdev_ilist_node_init() - initialize a bfdev_ilist_node structure. + * @head: bfdev_ilist_node structure to be initialized. + */ static inline void bfdev_ilist_node_init(struct bfdev_ilist_node *inode) { @@ -66,7 +80,7 @@ bfdev_ilist_node_init(struct bfdev_ilist_node *inode) } /** - * bfdev_ilist_first - return the first node. + * bfdev_ilist_first() - return the first node. * @ihead: the &struct bfdev_ilist_head pointer */ static inline struct bfdev_ilist_node * @@ -76,7 +90,7 @@ bfdev_ilist_first(const struct bfdev_ilist_head *ihead) } /** - * bfdev_ilist_last - return the last node. + * bfdev_ilist_last() - return the last node. * @ihead:the &struct bfdev_ilist_head pointer */ static inline struct bfdev_ilist_node * @@ -86,7 +100,7 @@ bfdev_ilist_last(const struct bfdev_ilist_head *ihead) } /** - * bfdev_list_check_empty - check whether a head is empty. + * bfdev_list_check_empty() - check whether a head is empty. * @ihead: list head to check. */ static inline bool @@ -96,7 +110,7 @@ bfdev_ilist_head_empty(struct bfdev_ilist_head *ihead) } /** - * bfdev_list_check_empty - check whether a node index is empty. + * bfdev_list_check_empty() - check whether a node index is empty. * @inode: list node to check. */ static inline bool diff --git a/src/ilist.c b/src/ilist.c index 53da92ea..c87b71f1 100644 --- a/src/ilist.c +++ b/src/ilist.c @@ -66,11 +66,6 @@ ilist_head_check(struct bfdev_ilist_head *ihead) } #endif -/** - * ilist_add - insert a new index list node. - * @ihead: the index head to be insert. - * @inode: the index node to insert. - */ export void bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, bfdev_ilist_cmp_t cmp, void *pdata) @@ -115,11 +110,6 @@ bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, #endif } -/** - * ilist_del - delete a node form index head. - * @ihead: the index head to be delete. - * @inode: the index node to delete. - */ export void bfdev_ilist_del(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode) { From 8299057c5b6aab8f1cfa2ed56d697283bba93326 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 8 Dec 2023 00:01:03 +0800 Subject: [PATCH 009/119] fixup alloca: fixed 32-bit concurrent issues Signed-off-by: John Sanpe --- include/bfdev/alloca.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bfdev/alloca.h b/include/bfdev/alloca.h index 89718b22..0e8dc0ad 100644 --- a/include/bfdev/alloca.h +++ b/include/bfdev/alloca.h @@ -11,8 +11,8 @@ BFDEV_BEGIN_DECLS -#define bfdev_alloca __builtin_alloca extern __bfdev_malloc void *bfdev_alloca(unsigned long size); +#define bfdev_alloca __builtin_alloca BFDEV_END_DECLS From bea7d71519a2285287a0a97ba409ec9a1c2ebb84 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 9 Dec 2023 01:08:15 +0800 Subject: [PATCH 010/119] build prototypes: added missing prototypes and declarations warns Signed-off-by: John Sanpe --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f5a2895f..a6791c62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,6 +138,8 @@ add_compile_options( -Wno-sign-compare -Wno-pointer-sign -Wno-null-pointer-arithmetic + -Wmissing-prototypes + -Wmissing-declarations -fvisibility=hidden ) From e63a2045318590886d7d3d6c3518337087b2245e Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 9 Dec 2023 01:09:32 +0800 Subject: [PATCH 011/119] hotfix prototypes: fixed api inconsistency issue Signed-off-by: John Sanpe --- include/bfdev/heap.h | 13 +++++++++++++ include/bfdev/respool.h | 8 ++++---- src/bitmap.c | 2 +- src/respool.c | 22 +++++++++++----------- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index 659a18b0..79b64fdf 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -136,6 +136,19 @@ bfdev_heap_parent(struct bfdev_heap_root *root, struct bfdev_heap_node **parentp extern struct bfdev_heap_node * bfdev_heap_find(struct bfdev_heap_root *root, unsigned int index); +/* Base iteration - basic iteration helper */ +extern struct bfdev_heap_node * +bfdev_heap_left_far(const struct bfdev_heap_node *node); + +extern struct bfdev_heap_node * +bfdev_heap_right_far(const struct bfdev_heap_node *node); + +extern struct bfdev_heap_node * +bfdev_heap_left_deep(const struct bfdev_heap_node *node); + +extern struct bfdev_heap_node * +bfdev_heap_right_deep(const struct bfdev_heap_node *node); + /* Level iteration (Sequential) - access in level sequence */ extern struct bfdev_heap_node * bfdev_heap_first(const struct bfdev_heap_root *root, unsigned long *index); diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h index 7c88ddc2..315cb0f3 100755 --- a/include/bfdev/respool.h +++ b/include/bfdev/respool.h @@ -69,12 +69,12 @@ extern void bfdev_respool_release(struct bfdev_respool *pool, struct bfdev_resnode *res); extern struct bfdev_resnode * -respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, - const void *data); +bfdev_respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, + const void *data); extern struct bfdev_resnode * -respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, - const void *data); +bfdev_respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, + const void *data); extern void bfdev_respool_release_all(struct bfdev_respool *pool); diff --git a/src/bitmap.c b/src/bitmap.c index d7aaddfb..536a368b 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -174,7 +174,7 @@ bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, unsigned int bi } export unsigned long * -bfdev_bitmap_malloc(const struct bfdev_alloc *alloc, unsigned int bits) +bfdev_bitmap_alloc(const struct bfdev_alloc *alloc, unsigned int bits) { return bfdev_malloc_array( alloc, BFDEV_BITS_TO_LONG(bits), sizeof(unsigned long) diff --git a/src/respool.c b/src/respool.c index 6332adfd..6c902141 100755 --- a/src/respool.c +++ b/src/respool.c @@ -12,7 +12,7 @@ #include export struct bfdev_resnode * -respool_find(struct bfdev_respool *pool, +bfdev_respool_find(struct bfdev_respool *pool, bfdev_respool_find_t find, const void *data) { struct bfdev_resnode *walk; @@ -26,7 +26,7 @@ respool_find(struct bfdev_respool *pool, } export void -respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *node) { bfdev_list_add_prev(&pool->node, &node->list); bfdev_log_debug("%s: insert %p '%s'\n", @@ -34,7 +34,7 @@ respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *node) } export void -respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *node) { bfdev_list_del(&node->list); bfdev_log_debug("%s: remove %p '%s'\n", @@ -42,7 +42,7 @@ respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *node) } export void -respool_release(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_release(struct bfdev_respool *pool, struct bfdev_resnode *node) { bfdev_list_del(&node->list); node->release(pool, node); @@ -51,12 +51,12 @@ respool_release(struct bfdev_respool *pool, struct bfdev_resnode *node) } export struct bfdev_resnode * -respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, - const void *data) +bfdev_respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, + const void *data) { struct bfdev_resnode *match; - match = respool_find(pool, find, data); + match = bfdev_respool_find(pool, find, data); if (bfdev_likely(match)) bfdev_list_del(&match->list); @@ -69,12 +69,12 @@ respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, } export struct bfdev_resnode * -respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, - const void *data) +bfdev_respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, + const void *data) { struct bfdev_resnode *match; - match = respool_find(pool, find, data); + match = bfdev_respool_find(pool, find, data); if (bfdev_likely(match)) { bfdev_list_del(&match->list); match->release(pool, match); @@ -89,7 +89,7 @@ respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, } export void -respool_release_all(struct bfdev_respool *pool) +bfdev_respool_release_all(struct bfdev_respool *pool) { struct bfdev_resnode *node; From fa0108532a9dbadb70fdd784ab220e42583163aa Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 11 Dec 2023 19:21:21 +0800 Subject: [PATCH 012/119] fixup cmpxchg: fixed arch xchg logic Signed-off-by: John Sanpe --- include/bfdev/asm-generic/cmpxchg.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/bfdev/asm-generic/cmpxchg.h b/include/bfdev/asm-generic/cmpxchg.h index 5d9a57d6..f33e6c66 100644 --- a/include/bfdev/asm-generic/cmpxchg.h +++ b/include/bfdev/asm-generic/cmpxchg.h @@ -27,13 +27,14 @@ bfdev_arch_cmpxchg(bfdev_atomic_t *atomic, bfdev_atomic_t old, bfdev_atomic_t va static __bfdev_always_inline bfdev_atomic_t bfdev_arch_xchg(bfdev_atomic_t *atomic, bfdev_atomic_t value) { - bfdev_atomic_t prev; + bfdev_atomic_t prev, result; do { prev = *atomic; - } while (!bfdev_arch_cmpxchg(atomic, prev, value)); + result = bfdev_arch_cmpxchg(atomic, prev, value); + } while (bfdev_unlikely(result != prev)); - return prev; + return result; } #endif From b3e61df6091402b49bc3bf08a7d14a5480d9fcdd Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 11 Dec 2023 22:09:05 +0800 Subject: [PATCH 013/119] fixup minpool: fixed worst-fit best size Signed-off-by: John Sanpe --- src/minpool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/minpool.c b/src/minpool.c index 02528a9a..d6ea7e93 100644 --- a/src/minpool.c +++ b/src/minpool.c @@ -92,7 +92,7 @@ bfdev_minpool_worst_fit(struct bfdev_minpool_head *head, size_t size) { struct bfdev_minpool_node *best = NULL; struct bfdev_minpool_node *node; - size_t walk, bsize = BFDEV_SIZE_MAX; + size_t walk, bsize = BFDEV_SIZE_MIN; bfdev_list_for_each_entry(node, &head->free_list, free) { if ((walk = minnode_get_size(node)) >= size) { From 30bcebaf4a9a5356dae1b6ddeeadca535197e4ad Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 12 Dec 2023 22:21:04 +0800 Subject: [PATCH 014/119] fixup cache: fixed reset callback Signed-off-by: John Sanpe --- src/cache/cache.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cache/cache.c b/src/cache/cache.c index 3200e9a2..2bcb3ffb 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -246,6 +246,8 @@ bfdev_cache_reset(struct bfdev_cache_head *head) bfdev_list_head_init(&head->using); bfdev_list_head_init(&head->freed); bfdev_list_head_init(&head->changing); + + head->algo->reset(head); memset(head->taghash, 0, sizeof(*head->taghash) * head->size); for (count = 0; count < head->size; ++count) { From b30a09b81ed638b9f6b50a1dbd1bbe2f79ba039e Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 11 Dec 2023 22:10:25 +0800 Subject: [PATCH 015/119] feat minpool: added selftest example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/minpool/.gitignore | 2 + examples/minpool/CMakeLists.txt | 22 +++++++ examples/minpool/selftest.c | 105 ++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 examples/minpool/.gitignore create mode 100644 examples/minpool/CMakeLists.txt create mode 100644 examples/minpool/selftest.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e1aba467..b043b51f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -25,6 +25,7 @@ add_subdirectory(list) add_subdirectory(log) add_subdirectory(log2) add_subdirectory(matrix) +add_subdirectory(minpool) add_subdirectory(once) add_subdirectory(radix) add_subdirectory(rbtree) diff --git a/examples/minpool/.gitignore b/examples/minpool/.gitignore new file mode 100644 index 00000000..4477d53a --- /dev/null +++ b/examples/minpool/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/minpool-selftest diff --git a/examples/minpool/CMakeLists.txt b/examples/minpool/CMakeLists.txt new file mode 100644 index 00000000..83afa145 --- /dev/null +++ b/examples/minpool/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(minpool-selftest selftest.c) +target_link_libraries(minpool-selftest bfdev) +add_test(minpool-selftest minpool-selftest) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + selftest.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/minpool + ) + + install(TARGETS + minpool-selftest + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/minpool/selftest.c b/examples/minpool/selftest.c new file mode 100644 index 00000000..ffd0f0b5 --- /dev/null +++ b/examples/minpool/selftest.c @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "minpool-selftest" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include + +#define POOL_SIZE BFDEV_SZ_32MiB +#define TEST_SIZE BFDEV_SZ_16MiB +#define TEST_LOOP 100 + +static int +test_minpool(struct bfdev_minpool_head *pool) +{ + BFDEV_DEFINE_BITMAP(bitmap, TEST_LOOP); + void *result[TEST_LOOP]; + unsigned int count, index; + size_t size; + + for (count = 0; count < TEST_LOOP; ++count) { + size = (unsigned int)rand() % (TEST_SIZE / TEST_LOOP); + result[count] = bfdev_minpool_alloc(pool, size); + bfdev_log_info("minpool random alloc%02u: %p\n", + count, result[count]); + if (!result[count]) + return 1; + memset(result[count], 0, size); + } + + bfdev_bitmap_zero(bitmap, TEST_LOOP); + for (count = 0; count < TEST_LOOP;) { + index = (unsigned int)rand() % TEST_LOOP; + if (bfdev_bit_test(bitmap, index)) + continue; + + size = (unsigned int)rand() % (TEST_SIZE / TEST_LOOP); + result[index] = bfdev_minpool_realloc(pool, result[index], size); + bfdev_log_info("minpool random realloc%02u: %p\n", + count, result[index]); + if (!result[index]) + return 1; + memset(result[index], 0, size); + + bfdev_bit_set(bitmap, index); + count++; + } + + bfdev_bitmap_zero(bitmap, TEST_LOOP); + for (count = 0; count < TEST_LOOP;) { + index = (unsigned int)rand() % TEST_LOOP; + if (bfdev_bit_test(bitmap, index)) + continue; + + bfdev_log_info("minpool random free%02d: %p %d\n", + count, result[index], index); + bfdev_minpool_free(pool, result[index]); + + bfdev_bit_set(bitmap, index); + count++; + } + + return 0; +} + +int main(int argc, char const *argv[]) +{ + struct bfdev_minpool_head pool; + void *buffer; + int retval; + + buffer = malloc(POOL_SIZE); + if (!buffer) + return 1; + + bfdev_log_info("Setup first-fit minpool...\n"); + bfdev_minpool_setup(&pool, bfdev_minpool_first_fit, buffer, POOL_SIZE); + retval = test_minpool(&pool); + if (retval && pool.avail != POOL_SIZE) + return 1; + + bfdev_log_info("Setup best-fit minpool...\n"); + bfdev_minpool_setup(&pool, bfdev_minpool_best_fit, buffer, POOL_SIZE); + retval = test_minpool(&pool); + if (retval && pool.avail != POOL_SIZE) + return 1; + + bfdev_log_info("Setup worst-fit minpool...\n"); + bfdev_minpool_setup(&pool, bfdev_minpool_worst_fit, buffer, POOL_SIZE); + retval = test_minpool(&pool); + if (retval && pool.avail != POOL_SIZE) + return 1; + + free(buffer); + + return 0; +} From ebbce6d55654842cfa9bf3cfd5ac9e4c4a829a37 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 13 Dec 2023 22:07:41 +0800 Subject: [PATCH 016/119] feat allocpool: added reset api Signed-off-by: John Sanpe --- include/bfdev/allocpool.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/include/bfdev/allocpool.h b/include/bfdev/allocpool.h index 1012f912..2bc37136 100644 --- a/include/bfdev/allocpool.h +++ b/include/bfdev/allocpool.h @@ -30,7 +30,7 @@ struct bfdev_allocpool { /** * bfdev_allocpool_init() - Allocation mempool initialize. - * @pool: minimum mempool to initialize. + * @pool: the allocpool to initialize. * @array: mempool array address. * @size: mempool array size. */ @@ -40,9 +40,20 @@ bfdev_allocpool_init(struct bfdev_allocpool *pool, void *block, size_t size) *pool = BFDEV_ALLOCPOOL_INIT(block, size); } +/** + * bfdev_allocpool_reset() - Allocation mempool reset. + * @pool: the allocpool to reset. + */ +static inline void +bfdev_allocpool_reset(struct bfdev_allocpool *pool) +{ + pool->last = 0; + pool->count = 0; +} + /** * bfdev_allocpool_alloc() - Allocation mempool allocation. - * @pool: minimum mempool to alloc. + * @pool: the allocpool to alloc. * @size: size to allocation. * @align: align to allocation. */ @@ -51,7 +62,7 @@ bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align); /** * bfdev_allocpool_free() - Allocation mempool free. - * @pool: minimum mempool to free. + * @pool: the allocpool to free. * @block: memory block to free. */ extern void From e1a6a025af0555fdb8dd5529126a8dd6fa532bfe Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 15 Dec 2023 00:13:48 +0800 Subject: [PATCH 017/119] refactor base: added log2 header Signed-off-by: John Sanpe --- include/base.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/base.h b/include/base.h index 39fb5b6c..3971012f 100644 --- a/include/base.h +++ b/include/base.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include From 3b6d373d3b46985908f7050a2c308423fecb6b2a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 16 Dec 2023 06:42:34 +0800 Subject: [PATCH 018/119] feat fsm: added state exception Signed-off-by: John Sanpe --- include/bfdev/fsm.h | 11 ++++++--- src/fsm.c | 58 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index d7a0e749..a5b841ef 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -15,14 +15,18 @@ BFDEV_BEGIN_DECLS struct bfdev_fsm_event; +struct bfdev_fsm_transition; struct bfdev_fsm_state; -typedef long (*bfdev_fsm_guard_t) -(struct bfdev_fsm_event *event, const void *cond); - typedef int (*bfdev_fsm_event_t) (struct bfdev_fsm_event *event, void *data); +typedef struct bfdev_fsm_transition *(*bfdev_fsm_exception_t) +(struct bfdev_fsm_event *event, void *data); + +typedef long (*bfdev_fsm_guard_t) +(struct bfdev_fsm_event *event, const void *cond); + typedef int (*bfdev_fsm_active_t) (struct bfdev_fsm_event *event, void *data, void *curr, void *next); @@ -54,6 +58,7 @@ struct bfdev_fsm_transition { struct bfdev_fsm_state { bfdev_fsm_event_t enter; bfdev_fsm_event_t exit; + bfdev_fsm_exception_t exception; void *data; const struct bfdev_fsm_transition *trans; diff --git a/src/fsm.c b/src/fsm.c index 399709d3..ad46e06f 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -23,13 +23,19 @@ fsm_find_transition(const struct bfdev_fsm_state *state, struct bfdev_fsm_event for (count = 0; count < state->tnum; ++count) { find = &state->trans[count]; + /* A transition for the given event has been found. */ if (find->type != event->type) continue; + /* If transition is guarded, ensure that the condition is held. */ if (!find->guard || !find->guard(event, find->cond)) return find; } + /* No transition conditions found, triggering exception */ + if (state->exception) + return state->exception(event, state->data); + return NULL; } @@ -60,53 +66,93 @@ bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) const struct bfdev_fsm_state **pstate; int retval; + /* + * If there were no transitions for the given event for + * the current state, check if there are any transitions + * for any of the parent states (if any). + */ tran = fsm_find_transition(curr, event); if (!tran) { next = curr->parent; continue; } + /* + * Pop the stack as the new state, if popping is required + * and there have no next state. + */ if (!(next = tran->next) && tran->stack < 0) { pstate = bfdev_array_pop(&fsm->stack, -tran->stack); next = *pstate; } + /* + * A transition must have a next state defined. + * If the user has not defined the next state, + * go to error state. + */ if (bfdev_unlikely(!next)) { retval = bfdev_fsm_error(fsm, event); return retval ?: -BFDEV_ENOENT; } + /* + * If the new state is a parent state, enter its entry + * state (if it has one). Step down through the whole family + * tree until a state without an entry state is found. + */ while (next->entry) next = next->entry; + /* + * Call the curr state's exit action, if state + * does not return to itself and the stack + * has not popped. + */ if (curr != next && tran->stack <= 0 && curr->exit) { retval = curr->exit(event, curr->data); if (bfdev_unlikely(retval)) return retval; } + /* Run transition action */ if (tran->action) { retval = tran->action(event, tran->data, curr->data, next->data); if (bfdev_unlikely(retval)) return retval; } + /* + * Call the new state's entry action, if state + * does not return to itself and has not been + * pushed onto the stack. + */ if (curr != next && tran->stack >= 0 && next->enter) { retval = next->enter(event, next->data); if (bfdev_unlikely(retval)) return retval; } + /* Update history state */ fsm_push_state(fsm, next); if (bfdev_unlikely(next == fsm->error)) return -BFDEV_EFAULT; + /* State returned to itself */ if (curr == next) return BFDEV_FSM_SELFLOOP; - if (!next->tnum) + /* + * If the new state is a final state, notify user + * that the state machine has stopped + */ + if (!next->tnum && !next->exception) return BFDEV_FSM_FINISH; + /* + * If it needs to be pushed onto the stack, + * push the curr state. + */ if (tran->stack > 0) { pstate = bfdev_array_push(&fsm->stack, 1); if (bfdev_unlikely(!pstate)) @@ -114,8 +160,14 @@ bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) *pstate = curr; } - if (!tran->cross) - return BFDEV_FSM_CHANGED; + /* + * If it is declared as crossing, Immediately + * change to the next state. + */ + if (tran->cross) + continue; + + return BFDEV_FSM_CHANGED; } return BFDEV_FSM_NOCHANGE; From 28e708629ae4e510cdb651d1998b7478018699da Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 18 Dec 2023 00:50:11 +0800 Subject: [PATCH 019/119] fixup fsm: fixed coredump in state stack leak Signed-off-by: John Sanpe --- include/bfdev/fsm.h | 13 ++++++------- src/fsm.c | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index a5b841ef..7722e234 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -71,10 +71,8 @@ struct bfdev_fsm_state { struct bfdev_fsm { const struct bfdev_fsm_state *state[2]; const struct bfdev_fsm_state *error; - unsigned int count; - struct bfdev_array stack; - unsigned int sindex; + unsigned int count; }; #define BFDEV_FSM_STATIC(ALLOC, INIT, ERROR) { \ @@ -97,17 +95,18 @@ bfdev_fsm_init(struct bfdev_fsm *fsm, const struct bfdev_alloc *alloc, *fsm = BFDEV_FSM_INIT(alloc, init, error); } + static inline const struct bfdev_fsm_state * -bfdev_fsm_prev(const struct bfdev_fsm *fsm) +bfdev_fsm_curr(struct bfdev_fsm *fsm) { - unsigned int count = fsm->count - 1; + unsigned int count = fsm->count; return fsm->state[count & (BFDEV_ARRAY_SIZE(fsm->state) - 1)]; } static inline const struct bfdev_fsm_state * -bfdev_fsm_curr(struct bfdev_fsm *fsm) +bfdev_fsm_prev(const struct bfdev_fsm *fsm) { - unsigned int count = fsm->count - 0; + unsigned int count = fsm->count - 1; return fsm->state[count & (BFDEV_ARRAY_SIZE(fsm->state) - 1)]; } diff --git a/src/fsm.c b/src/fsm.c index ad46e06f..ce2ecd5e 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -83,6 +83,8 @@ bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) */ if (!(next = tran->next) && tran->stack < 0) { pstate = bfdev_array_pop(&fsm->stack, -tran->stack); + if (bfdev_unlikely(!pstate)) + return -BFDEV_EOVERFLOW; next = *pstate; } From cc25352b470588ff6ccc7181090641ef075be213 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 18 Dec 2023 00:56:05 +0800 Subject: [PATCH 020/119] feat fsm: added reset api Signed-off-by: John Sanpe --- include/bfdev/fsm.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index 7722e234..d0e39050 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -95,6 +95,13 @@ bfdev_fsm_init(struct bfdev_fsm *fsm, const struct bfdev_alloc *alloc, *fsm = BFDEV_FSM_INIT(alloc, init, error); } +static inline void +bfdev_fsm_reset(struct bfdev_fsm *fsm, const struct bfdev_fsm_state *init) +{ + *fsm->state = init; + fsm->count = 0; + bfdev_array_reset(&fsm->stack); +} static inline const struct bfdev_fsm_state * bfdev_fsm_curr(struct bfdev_fsm *fsm) From 6e495a8d3cf986a027c538e7ce7b8dd98e8dfff6 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 20 Dec 2023 20:44:22 +0800 Subject: [PATCH 021/119] fixup config: fixed cmake project version Signed-off-by: John Sanpe --- cmake/config.h.in | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmake/config.h.in b/cmake/config.h.in index 5e1d6cdf..25e78bf0 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -13,10 +13,10 @@ BFDEV_BEGIN_DECLS -#define BFDEV_VERSION_MAJOR ${CMAKE_PROJECT_VERSION_MAJOR} -#define BFDEV_VERSION_MINOR ${CMAKE_PROJECT_VERSION_MINOR} -#define BFDEV_VERSION_PATCH ${CMAKE_PROJECT_VERSION_PATCH} -#define BFDEV_VERSION_TWEAK ${CMAKE_PROJECT_VERSION_TWEAK} +#define BFDEV_VERSION_MAJOR ${PROJECT_VERSION_MAJOR} +#define BFDEV_VERSION_MINOR ${PROJECT_VERSION_MINOR} +#define BFDEV_VERSION_PATCH ${PROJECT_VERSION_PATCH} +#define BFDEV_VERSION_TWEAK ${PROJECT_VERSION_TWEAK} #define BFDEV_EXTREVERSION ${BFDEV_EXTREVERSION} #define BFDEV_ARCH ${BFDEV_ARCH} From ce6cf742a03b01a8daae294fba9d94e46574e419 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 24 Dec 2023 22:22:08 +0800 Subject: [PATCH 022/119] feat array: added reserve function Signed-off-by: John Sanpe --- include/bfdev/array.h | 17 +++++---- src/array.c | 82 ++++++++++++++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 2814c304..6dce55bc 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -20,8 +20,8 @@ BFDEV_BEGIN_DECLS struct bfdev_array { const struct bfdev_alloc *alloc; - unsigned int capacity; - unsigned int index; + unsigned long capacity; + unsigned long index; size_t cells; void *data; }; @@ -48,7 +48,7 @@ bfdev_array_reset(struct bfdev_array *array) array->index = 0; } -static inline unsigned int +static inline unsigned long bfdev_array_index(struct bfdev_array *array) { return array->index; @@ -61,13 +61,13 @@ bfdev_array_size(struct bfdev_array *array) } static inline uintptr_t -bfdev_array_offset(struct bfdev_array *array, unsigned int index) +bfdev_array_offset(struct bfdev_array *array, unsigned long index) { return array->cells * index; } static inline void * -bfdev_array_data(struct bfdev_array *array, unsigned int index) +bfdev_array_data(struct bfdev_array *array, unsigned long index) { if (bfdev_unlikely(index >= array->index)) return NULL; @@ -75,10 +75,13 @@ bfdev_array_data(struct bfdev_array *array, unsigned int index) } extern void * -bfdev_array_push(struct bfdev_array *array, unsigned int num); +bfdev_array_push(struct bfdev_array *array, unsigned long num); extern void * -bfdev_array_pop(struct bfdev_array *array, unsigned int num); +bfdev_array_pop(struct bfdev_array *array, unsigned long num); + +extern int +bfdev_array_reserve(struct bfdev_array *array, unsigned long num); extern void bfdev_array_release(struct bfdev_array *array); diff --git a/src/array.c b/src/array.c index ecc6f60d..b88d6b7b 100644 --- a/src/array.c +++ b/src/array.c @@ -9,46 +9,86 @@ #include #include -export void * -bfdev_array_push(struct bfdev_array *array, unsigned int num) +static int +array_resize(struct bfdev_array *array, unsigned long count) { const struct bfdev_alloc *alloc = array->alloc; - unsigned int nalloc, index, count; - bool overflow; + unsigned long nalloc; + size_t size; void *data; + nalloc = bfdev_max(count << 1, BFDEV_ARRAY_MSIZE); + size = nalloc * array->cells; + + data = bfdev_realloc(alloc, array->data, size); + if (bfdev_unlikely(!data)) + return -BFDEV_ENOMEM; + + array->data = data; + array->capacity = nalloc; + + return -BFDEV_ENOERR; +} + +static int +array_apply(struct bfdev_array *array, unsigned long count) +{ + if (count > array->capacity) + return array_resize(array, count); + return -BFDEV_ENOERR; +} + +export void * +bfdev_array_push(struct bfdev_array *array, unsigned long num) +{ + unsigned long index, count; + uintptr_t offset; + bool overflow; + int retval; + overflow = bfdev_overflow_check_add(array->index, num, &count); if (bfdev_unlikely(overflow)) return NULL; - if (count > array->capacity) { - nalloc = bfdev_max(count << 1, BFDEV_ARRAY_MSIZE); - - data = bfdev_realloc(alloc, array->data, nalloc * array->cells); - if (bfdev_unlikely(!data)) - return NULL; - - array->data = data; - array->capacity = nalloc; - } + retval = array_apply(array, count); + if (bfdev_unlikely(retval)) + return NULL; index = array->index; array->index = count; + offset = bfdev_array_offset(array, index); - return bfdev_array_data(array, index); + return array->data + offset; } export void * -bfdev_array_pop(struct bfdev_array *array, unsigned int num) +bfdev_array_pop(struct bfdev_array *array, unsigned long num) { - unsigned int index; - void *data; + unsigned long index; + uintptr_t offset; + bool overflow; + + overflow = bfdev_overflow_check_sub(array->index, num, &index); + if (bfdev_unlikely(overflow)) + return NULL; - index = array->index - num; - data = bfdev_array_data(array, index); array->index = index; + offset = bfdev_array_offset(array, index); + + return array->data + offset; +} + +export int +bfdev_array_reserve(struct bfdev_array *array, unsigned long num) +{ + unsigned long count; + bool overflow; + + overflow = bfdev_overflow_check_add(array->index, num, &count); + if (bfdev_unlikely(overflow)) + return -BFDEV_EOVERFLOW; - return data; + return array_apply(array, count); } export void From 0315de6ca2d4377fe126381a27c5dbd3a9455a20 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 24 Dec 2023 22:26:57 +0800 Subject: [PATCH 023/119] feat circle: added initial circle funcrions Signed-off-by: John Sanpe --- include/bfdev/circle.h | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 include/bfdev/circle.h diff --git a/include/bfdev/circle.h b/include/bfdev/circle.h new file mode 100644 index 00000000..70b5b2d2 --- /dev/null +++ b/include/bfdev/circle.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_CIRCLE_H_ +#define _BFDEV_CIRCLE_H_ + +#include + +BFDEV_BEGIN_DECLS + +struct bfdev_circle { + void *buffer; + unsigned long head; + unsigned long tail; +}; + +/* Return count in buffer */ +#define BFDEV_CIRCLE_CNT(head, tail, size) \ + (((head) - (tail)) & ((size) - 1)) + +/* Return space available */ +#define BFDEV_CIRCLE_SPACE(head, tail, size) \ + (((tail) - (head) + 1) & ((size) - 1)) + +/* Return count up to the end of the buffer */ +#define BFDEV_CIRCLE_CNT_END(head, tail, size) ({ \ + unsigned long __end, __len; \ + __end = (size) - (tail); \ + __len = ((head) + __end) & ((size) - 1); \ + __len < __end ? __len : __end; \ +}) + +/* Return space available up to the end of the buffer */ +#define BFDEV_CIRCLE_SPACE_END(head, tail, size) ({ \ + unsigned long __end, __len; \ + __end = ((size) - 1) - (head); \ + __len = (__end + (tail)) & ((size) - 1); \ + __len <= end ? __len : end + 1; \ +}) + +BFDEV_END_DECLS + +#endif /* _BFDEV_CIRCLE_H_ */ From 0c5f2c04ff67cb4f668bd50b066dd203b0f8cec0 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 24 Dec 2023 22:27:33 +0800 Subject: [PATCH 024/119] feat circle: added simple example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/circle/.gitignore | 2 + examples/circle/CMakeLists.txt | 22 +++++++++++ examples/circle/simple.c | 67 ++++++++++++++++++++++++++++++++++ examples/fifo/simple.c | 33 ++++++++--------- 5 files changed, 108 insertions(+), 17 deletions(-) create mode 100644 examples/circle/.gitignore create mode 100644 examples/circle/CMakeLists.txt create mode 100644 examples/circle/simple.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b043b51f..2eb3f771 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,6 +11,7 @@ add_subdirectory(bfdev) add_subdirectory(bloom) add_subdirectory(btree) add_subdirectory(cache) +add_subdirectory(circle) add_subdirectory(crc) add_subdirectory(fifo) add_subdirectory(fsm) diff --git a/examples/circle/.gitignore b/examples/circle/.gitignore new file mode 100644 index 00000000..4b309451 --- /dev/null +++ b/examples/circle/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/circle-simple diff --git a/examples/circle/CMakeLists.txt b/examples/circle/CMakeLists.txt new file mode 100644 index 00000000..e737b6bc --- /dev/null +++ b/examples/circle/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(circle-simple simple.c) +target_link_libraries(circle-simple bfdev) +add_test(circle-simple circle-simple) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + simple.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/circle + ) + + install(TARGETS + circle-simple + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/circle/simple.c b/examples/circle/simple.c new file mode 100644 index 00000000..e54dbad0 --- /dev/null +++ b/examples/circle/simple.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2021 John Sanpe + */ + +#include +#include +#include +#include +#include + +#define TEST_SIZE 16 +#define TEST_LOOP (TEST_SIZE * 64) + +struct bfdev_circle test_circle = { + .buffer = (char [TEST_SIZE]){}, +}; + +static void * +fifo_production(void *unused) +{ + unsigned int index, count; + + for (count = 0; count < TEST_LOOP; ++count) { + while (!BFDEV_CIRCLE_SPACE(test_circle.head, test_circle.tail, TEST_SIZE)) + sched_yield(); + + index = test_circle.head % TEST_SIZE; + ((char *)test_circle.buffer)[index] = "123456789ABCDEF\n"[index]; + test_circle.head++; + } + + while (!BFDEV_CIRCLE_SPACE(test_circle.head, test_circle.tail, TEST_SIZE)) + sched_yield(); + + index = test_circle.head % TEST_SIZE; + ((char *)test_circle.buffer)[index] = 0; + test_circle.head++; + + return NULL; +} + +int main(int argc, const char *argv[]) +{ + unsigned int index; + pthread_t thread; + char ch; + + pthread_create(&thread, NULL, fifo_production, NULL); + for (;;) { + while (!BFDEV_CIRCLE_CNT(test_circle.head, test_circle.tail, TEST_SIZE)) + sched_yield(); + + index = test_circle.tail % TEST_SIZE; + ch = ((char *)test_circle.buffer)[index]; + test_circle.tail++; + + if (!ch) { + printf("exit\n"); + break; + } + + printf("%c", ch); + } + + return 0; +} diff --git a/examples/fifo/simple.c b/examples/fifo/simple.c index 85073ea3..7dd3fbc3 100644 --- a/examples/fifo/simple.c +++ b/examples/fifo/simple.c @@ -8,13 +8,10 @@ #include #include -static const char test_table[] = { - 'b', 'f', 'd', 'e', 'v', '-', 'f', 'i', - 'f', 'o', '-', 't', 'e', 's', 't', '\n', -}; +#define TEST_SIZE 16 +#define TEST_LOOP (TEST_SIZE * 64) -#define TEST_LOOP 64 -BFDEV_DEFINE_FIFO(normal_bytetest, char, BFDEV_ARRAY_SIZE(test_table)); +BFDEV_DEFINE_FIFO(fifo_test, char, TEST_SIZE); static void * fifo_production(void *unused) @@ -22,18 +19,18 @@ fifo_production(void *unused) unsigned int count, index; char ch; - for (count = 0; count < TEST_LOOP; ++count) { - for (index = 0; index < BFDEV_ARRAY_SIZE(test_table); ++index) { - while (bfdev_fifo_check_full(&normal_bytetest)) - sched_yield(); - ch = test_table[index]; - bfdev_fifo_put(&normal_bytetest, ch); - } + for (count = index = 0; count < TEST_LOOP; ++count) { + while (bfdev_fifo_check_full(&fifo_test)) + sched_yield(); + + index = count % TEST_SIZE; + ch = "123456789ABCDEF\n"[index]; + bfdev_fifo_put(&fifo_test, ch); } - while (bfdev_fifo_check_full(&normal_bytetest)) + while (bfdev_fifo_check_full(&fifo_test)) sched_yield(); - bfdev_fifo_put(&normal_bytetest, 0); + bfdev_fifo_put(&fifo_test, 0); return NULL; } @@ -45,13 +42,15 @@ int main(int argc, const char *argv[]) pthread_create(&thread, NULL, fifo_production, NULL); for (;;) { - while (bfdev_fifo_check_empty(&normal_bytetest)) + while (bfdev_fifo_check_empty(&fifo_test)) sched_yield(); - bfdev_fifo_get(&normal_bytetest, &ch); + + bfdev_fifo_get(&fifo_test, &ch); if (!ch) { printf("exit\n"); break; } + printf("%c", ch); } From dbd81f4a0fb90ded1750b9029f7d7cdf14492e09 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 28 Dec 2023 07:32:18 +0800 Subject: [PATCH 025/119] build packsource: added initial scripts Signed-off-by: John Sanpe --- CMakeLists.txt | 33 ++++++++++++++++++++++++--------- scripts/packed-source.cmake | 29 +++++++++++++++++++++++++++++ src/cache/cache.c | 14 +++++++------- src/cache/lfu.c | 34 +++++++++++++++++----------------- src/cache/lru.c | 20 ++++++++++---------- src/textsearch/bm.c | 6 ++++-- src/textsearch/kmp.c | 6 ++++-- src/textsearch/sunday.c | 6 ++++-- src/textsearch/textsearch.c | 14 +++++++------- 9 files changed, 106 insertions(+), 56 deletions(-) create mode 100644 scripts/packed-source.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a6791c62..02bbc702 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,7 @@ set(BFDEV_CONFIGURE ${BFDEV_GENERATED_PATH}/bfdev-config.cmake) include(scripts/asm-generic.cmake) include(scripts/hostrule.cmake) include(scripts/packed-header.cmake) +include(scripts/packed-source.cmake) include(scripts/commit.cmake) commit_hash(BFDEV_COMMITID) @@ -96,13 +97,6 @@ asm_generic( ${BFDEV_HEADER_PATH}/bfdev/asm-generic ) -packed_header( - bfdev/ - _BFDEV_H_ - ${BFDEV_GENERATED_PATH}/bfdev.h - ${BFDEV_HEADER_PATH}/bfdev -) - configure_file( ${BFDEV_MODULE_PATH}/config.h.in ${BFDEV_GENERATED_PATH}/bfdev/config.h @@ -187,16 +181,23 @@ include_directories(${PROJECT_BINARY_DIR}/generated) add_subdirectory(${PROJECT_SOURCE_DIR}/scripts) include(${PROJECT_SOURCE_DIR}/build.cmake) -set(BFDEV_LIBRARY +set(BFDEV_LIBRARY_HEADER ${BFDEV_HEADER} ${BFDEV_ASM_HEADER} ${BFDEV_ARCH_ASM_HEADER} ${BFDEV_GENERATED_HEADER} - ${BFDEV_INCLUDE} +) + +set(BFDEV_LIBRARY_SOURCE ${BFDEV_SOURCE} ${BFDEV_ARCH_SOURCE} ) +set(BFDEV_LIBRARY + ${BFDEV_LIBRARY_HEADER} + ${BFDEV_LIBRARY_SOURCE} +) + macro(bfdev_dependencies target) add_dependencies( ${target} @@ -210,6 +211,20 @@ macro(bfdev_dependencies target) ) endmacro() +packed_header( + bfdev/ + _BFDEV_H_ + ${BFDEV_GENERATED_PATH}/bfdev.h + ${BFDEV_HEADER_PATH}/bfdev +) + +packed_source( + ${PROJECT_BINARY_DIR}/bfdev.c + "${BFDEV_LIBRARY_SOURCE}" + "#undef MODULE_NAME\n" + "#undef bfdev_log_fmt\n" +) + add_library(bfdev_object OBJECT ${BFDEV_LIBRARY}) bfdev_dependencies(bfdev_object) add_library(bfdev ALIAS bfdev_object) diff --git a/scripts/packed-source.cmake b/scripts/packed-source.cmake new file mode 100644 index 00000000..48028edc --- /dev/null +++ b/scripts/packed-source.cmake @@ -0,0 +1,29 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 John Sanpe +# + +function(packed_source genfile source) + file(REMOVE ${genfile}) + + file(WRITE ${genfile} + "/*\n" + " * Automatically generated file; DO NOT EDIT.\n" + " * " ${PROJECT_NAME} " packed-source\n" + " */\n" + "\n" + ) + + foreach(file ${source}) + file(READ ${file} value) + file(APPEND ${genfile} "/* File: " ${file} ". */\n") + file(APPEND ${PROJECT_BINARY_DIR}/bfdev.c "${value}") + file(APPEND ${genfile} "\n") + + foreach(append ${ARGN}) + file(APPEND ${genfile} ${append}) + endforeach() + + file(APPEND ${genfile} "\n") + endforeach() +endfunction() diff --git a/src/cache/cache.c b/src/cache/cache.c index 2bcb3ffb..fdaeb79f 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -9,14 +9,14 @@ #include #include -static BFDEV_LIST_HEAD(algorithms); +static BFDEV_LIST_HEAD(cache_algorithms); static struct bfdev_cache_algo * -algorithm_find(const char *name) +cache_algorithm_find(const char *name) { struct bfdev_cache_algo *algo; - bfdev_list_for_each_entry(algo, &algorithms, list) { + bfdev_list_for_each_entry(algo, &cache_algorithms, list) { if (!strcmp(algo->name, name)) return algo; } @@ -272,7 +272,7 @@ bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, if (bfdev_unlikely(size < 2)) return NULL; - algo = algorithm_find(name); + algo = cache_algorithm_find(name); if (bfdev_unlikely(!algo)) return NULL; @@ -328,17 +328,17 @@ bfdev_cache_register(struct bfdev_cache_algo *algo) algo->get && algo->put)) return -BFDEV_EINVAL; - if (algorithm_find(algo->name)) + if (cache_algorithm_find(algo->name)) return -BFDEV_EALREADY; - bfdev_list_add(&algorithms, &algo->list); + bfdev_list_add(&cache_algorithms, &algo->list); return -BFDEV_ENOERR; } export void bfdev_cache_unregister(struct bfdev_cache_algo *algo) { - if (algorithm_find(algo->name)) + if (cache_algorithm_find(algo->name)) return; bfdev_list_del(&algo->list); diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 5a5261cd..529c1fef 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -18,14 +18,14 @@ struct lfu_node { unsigned long count; }; -#define cache_to_head(ptr) \ +#define cache_to_lfu_head(ptr) \ bfdev_container_of(ptr, struct lfu_head, cache) -#define cache_to_node(ptr) \ +#define cache_to_lfu_node(ptr) \ bfdev_container_of(ptr, struct lfu_node, cache) -#define heap_to_node(ptr) \ - bfdev_container_of(ptr, struct lfu_node, node) +#define heap_to_lfu_node(ptr) \ + bfdev_heap_entry(ptr, struct lfu_node, node) static long lfu_compare(const struct bfdev_heap_node *node1, @@ -33,8 +33,8 @@ lfu_compare(const struct bfdev_heap_node *node1, { struct lfu_node *lfu1, *lfu2; - lfu1 = heap_to_node(node1); - lfu2 = heap_to_node(node2); + lfu1 = heap_to_lfu_node(node1); + lfu2 = heap_to_lfu_node(node2); if (lfu1->count == lfu2->count) return 0; @@ -46,7 +46,7 @@ static bool lfu_starving(struct bfdev_cache_head *head) { struct lfu_head *lfu_head; - lfu_head = cache_to_head(head); + lfu_head = cache_to_lfu_head(head); return BFDEV_HEAP_EMPTY_ROOT(&lfu_head->lfu); } @@ -56,7 +56,7 @@ lfu_obtain(struct bfdev_cache_head *head) struct lfu_head *lfu_head; struct lfu_node *lfu_node; - lfu_head = cache_to_head(head); + lfu_head = cache_to_lfu_head(head); lfu_node = bfdev_heap_entry(BFDEV_HEAP_ROOT_NODE(&lfu_head->lfu), struct lfu_node, node); bfdev_heap_delete(&lfu_head->lfu, &lfu_node->node, lfu_compare, NULL); @@ -69,8 +69,8 @@ lfu_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) struct lfu_head *lfu_head; struct lfu_node *lfu_node; - lfu_head = cache_to_head(head); - lfu_node = cache_to_node(node); + lfu_head = cache_to_lfu_head(head); + lfu_node = cache_to_lfu_node(node); bfdev_heap_delete(&lfu_head->lfu, &lfu_node->node, lfu_compare, NULL); } @@ -81,8 +81,8 @@ lfu_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) struct lfu_head *lfu_head; struct lfu_node *lfu_node; - lfu_head = cache_to_head(head); - lfu_node = cache_to_node(node); + lfu_head = cache_to_lfu_head(head); + lfu_node = cache_to_lfu_node(node); bfdev_heap_insert(&lfu_head->lfu, &lfu_node->node, lfu_compare, NULL); } @@ -91,7 +91,7 @@ static void lfu_update(struct bfdev_cache_head *head, struct bfdev_cache_node *node) { struct lfu_node *lfu_node; - lfu_node = cache_to_node(node); + lfu_node = cache_to_lfu_node(node); lfu_node->count++; } @@ -99,7 +99,7 @@ static void lfu_clear(struct bfdev_cache_head *head, struct bfdev_cache_node *node) { struct lfu_node *lfu_node; - lfu_node = cache_to_node(node); + lfu_node = cache_to_lfu_node(node); lfu_node->count = 0; } @@ -109,13 +109,13 @@ lfu_reset(struct bfdev_cache_head *head) struct lfu_head *lfu_head; unsigned long count; - lfu_head = cache_to_head(head); + lfu_head = cache_to_lfu_head(head); bfdev_heap_init(&lfu_head->lfu); for (count = 0; count < head->size; ++count) { struct lfu_node *lfu_node; - lfu_node = cache_to_node(head->nodes[count]); + lfu_node = cache_to_lfu_node(head->nodes[count]); lfu_node->count = 0; } } @@ -151,7 +151,7 @@ lfu_create(const struct bfdev_alloc *alloc, unsigned long size) free_element: while (count--) { - lfu_node = cache_to_node(head->nodes[count]); + lfu_node = cache_to_lfu_node(head->nodes[count]); bfdev_free(alloc, lfu_node); } bfdev_free(alloc, head->nodes); diff --git a/src/cache/lru.c b/src/cache/lru.c index 4589e1b4..74295343 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -16,17 +16,17 @@ struct lru_node { struct bfdev_list_head node; }; -#define cache_to_head(ptr) \ +#define cache_to_lru_head(ptr) \ bfdev_container_of(ptr, struct lru_head, cache) -#define cache_to_node(ptr) \ +#define cache_to_lru_node(ptr) \ bfdev_container_of(ptr, struct lru_node, cache) static bool lru_starving(struct bfdev_cache_head *head) { struct lru_head *lru_head; - lru_head = cache_to_head(head); + lru_head = cache_to_lru_head(head); return bfdev_list_check_empty(&lru_head->lru); } @@ -36,7 +36,7 @@ lru_obtain(struct bfdev_cache_head *head) struct lru_head *lru_head; struct lru_node *lru_node; - lru_head = cache_to_head(head); + lru_head = cache_to_lru_head(head); lru_node = bfdev_list_last_entry(&lru_head->lru, struct lru_node, node); bfdev_list_del(&lru_node->node); @@ -47,7 +47,7 @@ static void lru_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) { struct lru_node *lru_node; - lru_node = cache_to_node(node); + lru_node = cache_to_lru_node(node); bfdev_list_del(&lru_node->node); } @@ -57,8 +57,8 @@ lru_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) struct lru_head *lru_head; struct lru_node *lru_node; - lru_head = cache_to_head(head); - lru_node = cache_to_node(node); + lru_head = cache_to_lru_head(head); + lru_node = cache_to_lru_node(node); bfdev_list_add(&lru_head->lru, &lru_node->node); } @@ -67,7 +67,7 @@ static void lru_reset(struct bfdev_cache_head *head) { struct lru_head *lru_head; - lru_head = cache_to_head(head); + lru_head = cache_to_lru_head(head); bfdev_list_head_init(&lru_head->lru); } @@ -102,7 +102,7 @@ lru_create(const struct bfdev_alloc *alloc, unsigned long size) free_element: while (count--) { - lru_node = cache_to_node(head->nodes[count]); + lru_node = cache_to_lru_node(head->nodes[count]); bfdev_free(alloc, lru_node); } bfdev_free(alloc, head->nodes); @@ -115,7 +115,7 @@ lru_create(const struct bfdev_alloc *alloc, unsigned long size) static void lru_destroy(struct bfdev_cache_head *head) { - struct lru_head *lru_head = cache_to_head(head); + struct lru_head *lru_head = cache_to_lru_head(head); const struct bfdev_alloc *alloc; struct bfdev_cache_node *node; unsigned long count; diff --git a/src/textsearch/bm.c b/src/textsearch/bm.c index f667a3ec..ab61e946 100644 --- a/src/textsearch/bm.c +++ b/src/textsearch/bm.c @@ -62,6 +62,8 @@ bm_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) consumed += length; } + + #undef find_pattern } static inline bool @@ -80,7 +82,7 @@ subpattern(const uint8_t *pattern, int index, int j, int g) } static inline void -compute_prefix(struct bm_context *bctx, unsigned long flags) +bm_compute_prefix(struct bm_context *bctx, unsigned long flags) { int index, start, count; @@ -129,7 +131,7 @@ bm_prepare(const struct bfdev_alloc *alloc, const void *pattern, memcpy(bctx->pattern, pattern, len); else for (index = 0; index < len; ++index) bctx->pattern[index] = toupper(((char *)pattern)[index]); - compute_prefix(bctx, flags); + bm_compute_prefix(bctx, flags); return &bctx->tsc; } diff --git a/src/textsearch/kmp.c b/src/textsearch/kmp.c index 21723eb1..7d6503e1 100644 --- a/src/textsearch/kmp.c +++ b/src/textsearch/kmp.c @@ -60,10 +60,12 @@ kmp_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) consumed += length; } + + #undef find_pattern } static inline void -compute_prefix(struct kmp_context *kctx) +kmp_compute_prefix(struct kmp_context *kctx) { unsigned int index, match; @@ -98,7 +100,7 @@ kmp_prepare(const struct bfdev_alloc *alloc, const void *pattern, memcpy(kctx->pattern, pattern, len); else for (index = 0; index < len; ++index) kctx->pattern[index] = toupper(((char *)pattern)[index]); - compute_prefix(kctx); + kmp_compute_prefix(kctx); return &kctx->tsc; } diff --git a/src/textsearch/sunday.c b/src/textsearch/sunday.c index 0c309e48..4c2d8c90 100644 --- a/src/textsearch/sunday.c +++ b/src/textsearch/sunday.c @@ -62,10 +62,12 @@ sunday_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) consumed += length; } + + #undef find_pattern } static inline void -compute_prefix(struct sunday_context *sctx, unsigned int flags) +sunday_compute_prefix(struct sunday_context *sctx, unsigned int flags) { unsigned int index; @@ -99,7 +101,7 @@ sunday_prepare(const struct bfdev_alloc *alloc, const void *pattern, memcpy(sctx->pattern, pattern, len); else for (index = 0; index < len; ++index) sctx->pattern[index] = toupper(((char *)pattern)[index]); - compute_prefix(sctx, flags); + sunday_compute_prefix(sctx, flags); return &sctx->tsc; } diff --git a/src/textsearch/textsearch.c b/src/textsearch/textsearch.c index 38b3b0b6..9fa6a002 100644 --- a/src/textsearch/textsearch.c +++ b/src/textsearch/textsearch.c @@ -7,14 +7,14 @@ #include #include -static BFDEV_LIST_HEAD(algorithms); +static BFDEV_LIST_HEAD(textsearch_algorithms); static struct bfdev_ts_algorithm * -algorithm_find(const char *name) +textsearch_algorithm_find(const char *name) { struct bfdev_ts_algorithm *algo; - bfdev_list_for_each_entry(algo, &algorithms, list) { + bfdev_list_for_each_entry(algo, &textsearch_algorithms, list) { if (!strcmp(algo->name, name)) return algo; } @@ -29,7 +29,7 @@ bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, struct bfdev_ts_algorithm *algo; struct bfdev_ts_context *tsc; - algo = algorithm_find(name); + algo = textsearch_algorithm_find(name); if (!algo) return NULL; @@ -51,17 +51,17 @@ bfdev_textsearch_register(struct bfdev_ts_algorithm *algo) algo->pattern_len)) return -BFDEV_EINVAL; - if (algorithm_find(algo->name)) + if (textsearch_algorithm_find(algo->name)) return -BFDEV_EALREADY; - bfdev_list_add(&algorithms, &algo->list); + bfdev_list_add(&textsearch_algorithms, &algo->list); return -BFDEV_ENOERR; } export void bfdev_textsearch_unregister(struct bfdev_ts_algorithm *algo) { - if (algorithm_find(algo->name)) + if (textsearch_algorithm_find(algo->name)) return; bfdev_list_del(&algo->list); From e809565d5a42840c3a3a694f0fb2a05972abc349 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 28 Dec 2023 07:39:07 +0800 Subject: [PATCH 026/119] fixup respool: fixed log format macro typo Signed-off-by: John Sanpe --- src/respool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/respool.c b/src/respool.c index 6c902141..7d92dcdb 100755 --- a/src/respool.c +++ b/src/respool.c @@ -4,7 +4,7 @@ */ #define MODULE_NAME "bfdev-respool" -#define pr_fmt(fmt) MODULE_NAME ": " fmt +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt #include #include From b7fa16b65d8e0c7ed8f010d1f55410736667e5cb Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 1 Jan 2024 06:58:22 +0800 Subject: [PATCH 027/119] fixup types: fixed some type issue Signed-off-by: John Sanpe --- include/bfdev/bitsperlong.h | 6 ++++-- include/bfdev/fsm.h | 2 +- include/bfdev/types.h | 3 ++- src/errname.c | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/bfdev/bitsperlong.h b/include/bfdev/bitsperlong.h index 5612a3a5..61215d20 100644 --- a/include/bfdev/bitsperlong.h +++ b/include/bfdev/bitsperlong.h @@ -12,9 +12,11 @@ BFDEV_BEGIN_DECLS #if __SIZEOF_POINTER__ == 4 # define BFDEV_BITS_PER_LONG 32 -#else /* __SIZEOF_POINTER__ == 8 */ +#elif __SIZEOF_POINTER__ == 8 # define BFDEV_BITS_PER_LONG 64 -#endif /* __SIZEOF_POINTER__ == 8 */ +#else +# error "Unknown type length" +#endif #define bfdev_const_small_nbits(nbits) ( \ __builtin_constant_p(nbits) && (nbits) <= \ diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index d0e39050..4ee9ba9a 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -125,4 +125,4 @@ bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event); BFDEV_END_DECLS -#endif /* _BFDEV_FSM_H */ +#endif /* _BFDEV_FSM_H_ */ diff --git a/include/bfdev/types.h b/include/bfdev/types.h index 2622ae94..8cc8f163 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -25,7 +25,8 @@ typedef uint16_t __bfdev_bitwise bfdev_be16; typedef uint32_t __bfdev_bitwise bfdev_be32; typedef uint64_t __bfdev_bitwise bfdev_be64; -typedef long bfdev_atomic_t; +typedef int bfdev_state_t; +typedef intptr_t bfdev_atomic_t; #define BFDEV_BYTES_PER_CHAR sizeof(char) #define BFDEV_BYTES_PER_SHORT sizeof(short) diff --git a/src/errname.c b/src/errname.c index 6a884243..4e3d6a39 100644 --- a/src/errname.c +++ b/src/errname.c @@ -133,7 +133,7 @@ static long errname_search(const void *key, void *pdata) { const struct bfdev_errname *entry = key; - return entry->errnum - (long)pdata; + return entry->errnum - (intptr_t)pdata; } export struct bfdev_errname * From dd46b3f0f0d691461c821581df4623acd8334393 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 6 Jan 2024 09:58:19 +0800 Subject: [PATCH 028/119] feat fsm: added initial typedef Signed-off-by: John Sanpe --- examples/fsm/console.c | 24 ++++++++--------- include/bfdev/fsm.h | 59 +++++++++++++++++++++--------------------- src/fsm.c | 22 ++++++++-------- 3 files changed, 53 insertions(+), 52 deletions(-) diff --git a/examples/fsm/console.c b/examples/fsm/console.c index 42e1078a..f5f2526c 100644 --- a/examples/fsm/console.c +++ b/examples/fsm/console.c @@ -18,40 +18,40 @@ enum { }; static int -enter_print(struct bfdev_fsm_event *event, void *data) +enter_print(bfdev_fsm_event_t *event, void *data) { printf( "Enter: %s\n", (char *)data); return 0; } static int -exit_print(struct bfdev_fsm_event *event, void *data) +exit_print(bfdev_fsm_event_t *event, void *data) { printf( "Exit: %s\n", (char *)data); return 0; } static long -trans_guard(struct bfdev_fsm_event *event, const void *cond) +trans_guard(bfdev_fsm_event_t *event, const void *cond) { return event->pdata - cond; } static int -trans_active(struct bfdev_fsm_event *event, void *data, +trans_active(bfdev_fsm_event_t *event, void *data, void *curr, void *next) { printf("Active: %s\n", (char *)data); return 0; } -static struct bfdev_fsm_state +static bfdev_fsm_state_t test_state[] = { [TEST_IDLE] = { .parent = &test_state[TEST_CHECK], .data = "idle", .tnum = 2, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .cond = (void *)(intptr_t)'h', .next = &test_state[TEST_HSTATE], @@ -70,7 +70,7 @@ test_state[] = { .parent = &test_state[TEST_CHECK], .data = "h-state", .tnum = 2, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .cond = (void *)(intptr_t)'i', .next = &test_state[TEST_ISTATE], @@ -89,7 +89,7 @@ test_state[] = { .parent = &test_state[TEST_CHECK], .data = "i-state", .tnum = 2, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .cond = (void *)(intptr_t)'\n', .next = &test_state[TEST_IDLE], @@ -111,7 +111,7 @@ test_state[] = { .parent = &test_state[TEST_CHECK], .data = "a-state", .tnum = 2, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .cond = (void *)(intptr_t)'\n', .next = &test_state[TEST_IDLE], @@ -133,7 +133,7 @@ test_state[] = { .parent = &test_state[TEST_CHECK], .data = "stack", .tnum = 1, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .action = trans_active, .data = "state pop", @@ -148,7 +148,7 @@ test_state[] = { .entry = &test_state[TEST_IDLE], .data = "check", .tnum = 2, - .trans = (struct bfdev_fsm_transition[]) { + .trans = (bfdev_fsm_transition_t[]) { { .cond = (void *)(intptr_t)'!', .next = &test_state[TEST_IDLE], @@ -182,7 +182,7 @@ int main(int argc, const char *argv[]) while ((ch = getc(stdin)) != EOF) { retval = bfdev_fsm_handle(&test_fsm, - &(struct bfdev_fsm_event) { + &(bfdev_fsm_event_t) { .pdata = (void *)(intptr_t)ch, } ); diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index 4ee9ba9a..90932de0 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -14,21 +14,22 @@ BFDEV_BEGIN_DECLS -struct bfdev_fsm_event; -struct bfdev_fsm_transition; -struct bfdev_fsm_state; +typedef struct bfdev_fsm_event bfdev_fsm_event_t; +typedef struct bfdev_fsm_transition bfdev_fsm_transition_t; +typedef struct bfdev_fsm_state bfdev_fsm_state_t; +typedef struct bfdev_fsm bfdev_fsm_t; -typedef int (*bfdev_fsm_event_t) -(struct bfdev_fsm_event *event, void *data); +typedef int (*bfdev_fsm_trigger_t) +(bfdev_fsm_event_t *event, void *data); -typedef struct bfdev_fsm_transition *(*bfdev_fsm_exception_t) -(struct bfdev_fsm_event *event, void *data); +typedef bfdev_fsm_transition_t *(*bfdev_fsm_exception_t) +(bfdev_fsm_event_t *event, void *data); typedef long (*bfdev_fsm_guard_t) -(struct bfdev_fsm_event *event, const void *cond); +(bfdev_fsm_event_t *event, const void *cond); typedef int (*bfdev_fsm_active_t) -(struct bfdev_fsm_event *event, void *data, void *curr, void *next); +(bfdev_fsm_event_t *event, void *data, void *curr, void *next); enum bfdev_fsm_retval { BFDEV_FSM_CHANGED = 0, @@ -46,7 +47,7 @@ struct bfdev_fsm_transition { unsigned int type; const void *cond; - const struct bfdev_fsm_state *next; + const bfdev_fsm_state_t *next; bool cross; int stack; @@ -56,21 +57,21 @@ struct bfdev_fsm_transition { }; struct bfdev_fsm_state { - bfdev_fsm_event_t enter; - bfdev_fsm_event_t exit; + bfdev_fsm_trigger_t enter; + bfdev_fsm_trigger_t exit; bfdev_fsm_exception_t exception; void *data; - const struct bfdev_fsm_transition *trans; + const bfdev_fsm_transition_t *trans; unsigned int tnum; - const struct bfdev_fsm_state *parent; - const struct bfdev_fsm_state *entry; + const bfdev_fsm_state_t *parent; + const bfdev_fsm_state_t *entry; }; struct bfdev_fsm { - const struct bfdev_fsm_state *state[2]; - const struct bfdev_fsm_state *error; + const bfdev_fsm_state_t *state[2]; + const bfdev_fsm_state_t *error; struct bfdev_array stack; unsigned int count; }; @@ -78,50 +79,50 @@ struct bfdev_fsm { #define BFDEV_FSM_STATIC(ALLOC, INIT, ERROR) { \ .state = {(INIT)}, .error = (ERROR), \ .stack = BFDEV_ARRAY_STATIC( \ - ALLOC, sizeof(struct bfdev_fsm_state *) \ + ALLOC, sizeof(bfdev_fsm_state_t *) \ ), \ } #define BFDEV_FSM_INIT(alloc, init, error) \ - (struct bfdev_fsm) BFDEV_FSM_STATIC(alloc, init, error) + (bfdev_fsm_t) BFDEV_FSM_STATIC(alloc, init, error) #define BFDEV_DEFINE_FSM(name, alloc, init, error) \ - struct bfdev_fsm name = BFDEV_FSM_INIT(alloc, init, error) + bfdev_fsm_t name = BFDEV_FSM_INIT(alloc, init, error) static inline void -bfdev_fsm_init(struct bfdev_fsm *fsm, const struct bfdev_alloc *alloc, - const struct bfdev_fsm_state *init, const struct bfdev_fsm_state *error) +bfdev_fsm_init(bfdev_fsm_t *fsm, const struct bfdev_alloc *alloc, + const bfdev_fsm_state_t *init, const bfdev_fsm_state_t *error) { *fsm = BFDEV_FSM_INIT(alloc, init, error); } static inline void -bfdev_fsm_reset(struct bfdev_fsm *fsm, const struct bfdev_fsm_state *init) +bfdev_fsm_reset(bfdev_fsm_t *fsm, const bfdev_fsm_state_t *init) { *fsm->state = init; fsm->count = 0; bfdev_array_reset(&fsm->stack); } -static inline const struct bfdev_fsm_state * -bfdev_fsm_curr(struct bfdev_fsm *fsm) +static inline const bfdev_fsm_state_t * +bfdev_fsm_curr(bfdev_fsm_t *fsm) { unsigned int count = fsm->count; return fsm->state[count & (BFDEV_ARRAY_SIZE(fsm->state) - 1)]; } -static inline const struct bfdev_fsm_state * -bfdev_fsm_prev(const struct bfdev_fsm *fsm) +static inline const bfdev_fsm_state_t * +bfdev_fsm_prev(const bfdev_fsm_t *fsm) { unsigned int count = fsm->count - 1; return fsm->state[count & (BFDEV_ARRAY_SIZE(fsm->state) - 1)]; } extern int -bfdev_fsm_error(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event); +bfdev_fsm_error(bfdev_fsm_t *fsm, bfdev_fsm_event_t *event); extern int -bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event); +bfdev_fsm_handle(bfdev_fsm_t *fsm, bfdev_fsm_event_t *event); BFDEV_END_DECLS diff --git a/src/fsm.c b/src/fsm.c index ce2ecd5e..87098be3 100644 --- a/src/fsm.c +++ b/src/fsm.c @@ -8,16 +8,16 @@ #include static inline void -fsm_push_state(struct bfdev_fsm *fsm, const struct bfdev_fsm_state *state) +fsm_push_state(bfdev_fsm_t *fsm, const bfdev_fsm_state_t *state) { unsigned int count = ++fsm->count; fsm->state[count & (BFDEV_ARRAY_SIZE(fsm->state) - 1)] = state; } -static const struct bfdev_fsm_transition * -fsm_find_transition(const struct bfdev_fsm_state *state, struct bfdev_fsm_event *event) +static const bfdev_fsm_transition_t * +fsm_find_transition(const bfdev_fsm_state_t *state, bfdev_fsm_event_t *event) { - const struct bfdev_fsm_transition *find; + const bfdev_fsm_transition_t *find; unsigned int count; for (count = 0; count < state->tnum; ++count) { @@ -40,9 +40,9 @@ fsm_find_transition(const struct bfdev_fsm_state *state, struct bfdev_fsm_event } export int -bfdev_fsm_error(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) +bfdev_fsm_error(bfdev_fsm_t *fsm, bfdev_fsm_event_t *event) { - const struct bfdev_fsm_state *error = fsm->error; + const bfdev_fsm_state_t *error = fsm->error; int retval = 0; fsm_push_state(fsm, error); @@ -53,17 +53,17 @@ bfdev_fsm_error(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) } export int -bfdev_fsm_handle(struct bfdev_fsm *fsm, struct bfdev_fsm_event *event) +bfdev_fsm_handle(bfdev_fsm_t *fsm, bfdev_fsm_event_t *event) { - const struct bfdev_fsm_state *curr = bfdev_fsm_curr(fsm); - const struct bfdev_fsm_state *next; + const bfdev_fsm_state_t *curr = bfdev_fsm_curr(fsm); + const bfdev_fsm_state_t *next; if (bfdev_unlikely(!curr)) return -BFDEV_EINVAL; for (next = curr; next; curr = next) { - const struct bfdev_fsm_transition *tran; - const struct bfdev_fsm_state **pstate; + const bfdev_fsm_transition_t *tran; + const bfdev_fsm_state_t **pstate; int retval; /* From 3fa59f7ce19f67472522bda1461bfdd73be9b261 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 6 Jan 2024 10:10:41 +0800 Subject: [PATCH 029/119] feat skiplist: added initial typedef Signed-off-by: John Sanpe --- examples/skiplist/benchmark.c | 4 ++-- examples/skiplist/selftest.c | 2 +- include/bfdev/skiplist.h | 24 ++++++++++------------ src/skiplist.c | 38 +++++++++++++++++------------------ 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/examples/skiplist/benchmark.c b/examples/skiplist/benchmark.c index eb643fc7..22d29e95 100644 --- a/examples/skiplist/benchmark.c +++ b/examples/skiplist/benchmark.c @@ -45,8 +45,8 @@ test_find(const void *node, void *pdata) int main(int argc, const char *argv[]) { - struct bfdev_skip_head *head; - struct bfdev_skip_node *node; + bfdev_skip_head_t *head; + bfdev_skip_node_t *node; uintptr_t value, *record; unsigned int count; int retval; diff --git a/examples/skiplist/selftest.c b/examples/skiplist/selftest.c index 866f585f..ac7f91b7 100644 --- a/examples/skiplist/selftest.c +++ b/examples/skiplist/selftest.c @@ -12,7 +12,7 @@ #define TEST_LEVEL 32 struct test_node { - struct bfdev_skip_head *head; + bfdev_skip_head_t *head; uintptr_t values[TEST_LOOP]; }; diff --git a/include/bfdev/skiplist.h b/include/bfdev/skiplist.h index b4595063..1d83de1b 100644 --- a/include/bfdev/skiplist.h +++ b/include/bfdev/skiplist.h @@ -14,6 +14,9 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_skip_head bfdev_skip_head_t; +typedef struct bfdev_skip_node bfdev_skip_node_t; + struct bfdev_skip_node { void *key; struct bfdev_list_head list[0]; @@ -27,28 +30,23 @@ struct bfdev_skip_head { }; extern int -bfdev_skiplist_insert(struct bfdev_skip_head *head, void *key, +bfdev_skiplist_insert(bfdev_skip_head_t *head, void *key, bfdev_cmp_t cmp, void *pdata); extern void -bfdev_skiplist_delete(struct bfdev_skip_head *head, - bfdev_find_t find, void *pdata); +bfdev_skiplist_delete(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata); -extern struct bfdev_skip_node * -bfdev_skiplist_find(struct bfdev_skip_head *head, - bfdev_find_t find, void *pdata); +extern bfdev_skip_node_t * +bfdev_skiplist_find(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata); extern void -bfdev_skiplist_reset(struct bfdev_skip_head *head, - bfdev_release_t relse); +bfdev_skiplist_reset(bfdev_skip_head_t *head, bfdev_release_t relse); extern void -bfdev_skiplist_destroy(struct bfdev_skip_head *head, - bfdev_release_t relse); +bfdev_skiplist_destroy(bfdev_skip_head_t *head, bfdev_release_t relse); -extern struct bfdev_skip_head * -bfdev_skiplist_create(const struct bfdev_alloc *alloc, - unsigned int levels); +extern bfdev_skip_head_t * +bfdev_skiplist_create(const struct bfdev_alloc *alloc, unsigned int levels); /** * bfdev_skiplist_for_each - iterate over list of given type. diff --git a/src/skiplist.c b/src/skiplist.c index ee2ad12c..9517af29 100644 --- a/src/skiplist.c +++ b/src/skiplist.c @@ -8,7 +8,7 @@ #include static unsigned int -random_level(struct bfdev_skip_head *head) +random_level(bfdev_skip_head_t *head) { unsigned int level = 1; @@ -21,13 +21,13 @@ random_level(struct bfdev_skip_head *head) return level; } -static struct bfdev_skip_node * -skipnode_find(struct bfdev_skip_head *head, bfdev_find_t find, +static bfdev_skip_node_t * +skipnode_find(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata, unsigned int *plev) { unsigned int level = head->curr; struct bfdev_list_head *list, *end; - struct bfdev_skip_node *walk; + bfdev_skip_node_t *walk; long retval; if (bfdev_unlikely(!level)) @@ -38,7 +38,7 @@ skipnode_find(struct bfdev_skip_head *head, bfdev_find_t find, for (; level--; --list, --end) { bfdev_list_for_each_continue(list, end) { - walk = bfdev_list_entry(list, struct bfdev_skip_node, list[level]); + walk = bfdev_list_entry(list, bfdev_skip_node_t, list[level]); retval = find(walk->key, pdata); if (retval >= 0) { end = list; @@ -59,12 +59,12 @@ skipnode_find(struct bfdev_skip_head *head, bfdev_find_t find, } export int -bfdev_skiplist_insert(struct bfdev_skip_head *head, void *key, +bfdev_skiplist_insert(bfdev_skip_head_t *head, void *key, bfdev_cmp_t cmp, void *pdata) { const struct bfdev_alloc *alloc = head->alloc; struct bfdev_list_head *list, *end; - struct bfdev_skip_node *walk, *node; + bfdev_skip_node_t *walk, *node; unsigned int level, count; long retval; @@ -81,7 +81,7 @@ bfdev_skiplist_insert(struct bfdev_skip_head *head, void *key, for (count = head->curr; count--; --list, --end) { bfdev_list_for_each_continue(list, end) { - walk = bfdev_list_entry(list, struct bfdev_skip_node, list[count]); + walk = bfdev_list_entry(list, bfdev_skip_node_t, list[count]); retval = cmp(walk->key, key, pdata); if (retval >= 0) { end = list; @@ -98,11 +98,11 @@ bfdev_skiplist_insert(struct bfdev_skip_head *head, void *key, } export void -bfdev_skiplist_delete(struct bfdev_skip_head *head, +bfdev_skiplist_delete(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata) { const struct bfdev_alloc *alloc = head->alloc; - struct bfdev_skip_node *node; + bfdev_skip_node_t *node; unsigned int level; node = skipnode_find(head, find, pdata, &level); @@ -118,21 +118,21 @@ bfdev_skiplist_delete(struct bfdev_skip_head *head, bfdev_free(alloc, node); } -export struct bfdev_skip_node * -bfdev_skiplist_find(struct bfdev_skip_head *head, +export bfdev_skip_node_t * +bfdev_skiplist_find(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata) { - struct bfdev_skip_node *node; + bfdev_skip_node_t *node; node = skipnode_find(head, find, pdata, NULL); return node; } static void -bfdev_skiplist_release(struct bfdev_skip_head *head, +bfdev_skiplist_release(bfdev_skip_head_t *head, bfdev_release_t relse) { const struct bfdev_alloc *alloc = head->alloc; - struct bfdev_skip_node *node, *tmp; + bfdev_skip_node_t *node, *tmp; bfdev_list_for_each_entry_safe(node, tmp, head->nodes, list[0]) { if (relse) @@ -142,7 +142,7 @@ bfdev_skiplist_release(struct bfdev_skip_head *head, } export void -bfdev_skiplist_reset(struct bfdev_skip_head *head, +bfdev_skiplist_reset(bfdev_skip_head_t *head, bfdev_release_t relse) { unsigned int count; @@ -155,7 +155,7 @@ bfdev_skiplist_reset(struct bfdev_skip_head *head, } export void -bfdev_skiplist_destroy(struct bfdev_skip_head *head, +bfdev_skiplist_destroy(bfdev_skip_head_t *head, bfdev_release_t relse) { const struct bfdev_alloc *alloc = head->alloc; @@ -164,11 +164,11 @@ bfdev_skiplist_destroy(struct bfdev_skip_head *head, bfdev_free(alloc, head); } -export struct bfdev_skip_head * +export bfdev_skip_head_t * bfdev_skiplist_create(const struct bfdev_alloc *alloc, unsigned int levels) { - struct bfdev_skip_head *head; + bfdev_skip_head_t *head; unsigned int count; if (bfdev_unlikely(!levels)) From dde6182edcadcaa11f5aafdf840e90a542d09607 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 6 Jan 2024 11:57:02 +0800 Subject: [PATCH 030/119] feat textsearch: added initial typedef Signed-off-by: John Sanpe --- examples/textsearch/simple.c | 63 ++++++++++++------------------------ include/bfdev/textsearch.h | 57 +++++++++++++++++--------------- src/textsearch/bm.c | 14 ++++---- src/textsearch/kmp.c | 14 ++++---- src/textsearch/linear.c | 16 ++++----- src/textsearch/sunday.c | 14 ++++---- src/textsearch/textsearch.c | 14 ++++---- 7 files changed, 87 insertions(+), 105 deletions(-) diff --git a/examples/textsearch/simple.c b/examples/textsearch/simple.c index e49e1fd4..de5ee61a 100644 --- a/examples/textsearch/simple.c +++ b/examples/textsearch/simple.c @@ -10,15 +10,16 @@ #define TEST_PATTERN "abcd" #define TEST_OFFSET 9 -int main(int argc, const char *argv[]) +static int +test_textsearch(const char *algo) { - struct bfdev_ts_context *context; - struct bfdev_ts_linear linear; + bfdev_ts_context_t *context; + bfdev_ts_linear_t linear; unsigned int offset; - printf("test bm algorithm: "); + printf("test %s algorithm: ", algo); context = bfdev_textsearch_create( - NULL, "bm", TEST_PATTERN, + NULL, algo, TEST_PATTERN, sizeof(TEST_PATTERN) - 1, 0 ); if (!context) { @@ -38,48 +39,24 @@ int main(int argc, const char *argv[]) printf("passed\n"); bfdev_textsearch_destroy(context); - printf("test kmp algorithm: "); - context = bfdev_textsearch_create( - NULL, "kmp", TEST_PATTERN, - sizeof(TEST_PATTERN) - 1, 0 - ); - if (!context) { - printf("failed\n"); - return 1; - } + return 0; +} - offset = bfdev_textsearch_linear_find( - context, &linear, TEST_STRING, - sizeof(TEST_STRING) - 1 - ); - if (offset != TEST_OFFSET) { - printf("failed\n"); - return 1; - } +int main(int argc, const char *argv[]) +{ + int retval; - printf("passed\n"); - bfdev_textsearch_destroy(context); + retval = test_textsearch("kmp"); + if (retval) + return retval; - printf("test sunday algorithm: "); - context = bfdev_textsearch_create( - NULL, "sunday", TEST_PATTERN, - sizeof(TEST_PATTERN) - 1, 0 - ); - if (!context) { - printf("failed\n"); - return 1; - } + retval = test_textsearch("bm"); + if (retval) + return retval; - offset = bfdev_textsearch_linear_find( - context, &linear, TEST_STRING, - sizeof(TEST_STRING) - 1 - ); - if (offset != TEST_OFFSET) { - printf("failed\n"); - return 1; - } - printf("passed\n"); - bfdev_textsearch_destroy(context); + retval = test_textsearch("sunday"); + if (retval) + return retval; return 0; } diff --git a/include/bfdev/textsearch.h b/include/bfdev/textsearch.h index 27a75c31..142bdcef 100644 --- a/include/bfdev/textsearch.h +++ b/include/bfdev/textsearch.h @@ -13,6 +13,11 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_ts_state bfdev_ts_state_t; +typedef struct bfdev_ts_linear bfdev_ts_linear_t; +typedef struct bfdev_ts_context bfdev_ts_context_t; +typedef struct bfdev_ts_algorithm bfdev_ts_algorithm_t; + enum bfdev_ts_flags { __BFDEV_TS_IGCASE = 0, BFDEV_TS_IGCASE = BFDEV_BIT(__BFDEV_TS_IGCASE), @@ -29,7 +34,7 @@ struct bfdev_ts_state { }; struct bfdev_ts_linear { - struct bfdev_ts_state tss; + bfdev_ts_state_t tss; const char *data; unsigned int len; }; @@ -44,8 +49,8 @@ struct bfdev_ts_context { const struct bfdev_alloc *alloc; unsigned long flags; - struct bfdev_ts_algorithm *algo; - unsigned int (*next_block)(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss, + bfdev_ts_algorithm_t *algo; + unsigned int (*next_block)(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, unsigned int consumed, const void **dest); }; @@ -62,69 +67,69 @@ struct bfdev_ts_algorithm { struct bfdev_list_head list; const char *name; - struct bfdev_ts_context *(*prepare)(const struct bfdev_alloc *alloc, const void *pattern, - size_t len, unsigned long flags); - void (*destroy)(struct bfdev_ts_context *tsc); - unsigned int (*find)(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss); - const void *(*pattern_get)(struct bfdev_ts_context *tsc); - unsigned int (*pattern_len)(struct bfdev_ts_context *tsc); + bfdev_ts_context_t *(*prepare)(const struct bfdev_alloc *alloc, const void *pattern, + size_t len, unsigned long flags); + void (*destroy)(bfdev_ts_context_t *tsc); + unsigned int (*find)(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss); + const void *(*pattern_get)(bfdev_ts_context_t *tsc); + unsigned int (*pattern_len)(bfdev_ts_context_t *tsc); }; -BFDEV_BITFLAGS_STRUCT(bfdev_ts, struct bfdev_ts_context, flags) -BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_ts, struct bfdev_ts_context, flags, igcase, __BFDEV_TS_IGCASE) +BFDEV_BITFLAGS_STRUCT(bfdev_ts, bfdev_ts_context_t, flags) +BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_ts, bfdev_ts_context_t, flags, igcase, __BFDEV_TS_IGCASE) static inline unsigned int -bfdev_textsearch_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) +bfdev_textsearch_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; tss->offset = 0; return algo->find(tsc, tss); } static inline unsigned int -bfdev_textsearch_next(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) +bfdev_textsearch_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; return algo->find(tsc, tss); } static inline const void * -textsearch_pattern_get(struct bfdev_ts_context *tsc) +textsearch_pattern_get(bfdev_ts_context_t *tsc) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; return algo->pattern_get(tsc); } static inline unsigned int -bfdev_textsearch_pattern_len(struct bfdev_ts_context *tsc) +bfdev_textsearch_pattern_len(bfdev_ts_context_t *tsc) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; return algo->pattern_len(tsc); } static inline void -bfdev_textsearch_destroy(struct bfdev_ts_context *tsc) +bfdev_textsearch_destroy(bfdev_ts_context_t *tsc) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; algo->destroy(tsc); } extern unsigned int -bfdev_textsearch_linear_find(struct bfdev_ts_context *tsc, struct bfdev_ts_linear *linear, +bfdev_textsearch_linear_find(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear, const void *data, unsigned int len); extern unsigned int -bfdev_textsearch_linear_next(struct bfdev_ts_context *tsc, struct bfdev_ts_linear *linear); +bfdev_textsearch_linear_next(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear); -extern struct bfdev_ts_context * +extern bfdev_ts_context_t * bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, const void *pattern, size_t len, unsigned long flags); extern int -bfdev_textsearch_register(struct bfdev_ts_algorithm *algo); +bfdev_textsearch_register(bfdev_ts_algorithm_t *algo); extern void -bfdev_textsearch_unregister(struct bfdev_ts_algorithm *algo); +bfdev_textsearch_unregister(bfdev_ts_algorithm_t *algo); BFDEV_END_DECLS diff --git a/src/textsearch/bm.c b/src/textsearch/bm.c index ab61e946..5e8430aa 100644 --- a/src/textsearch/bm.c +++ b/src/textsearch/bm.c @@ -7,7 +7,7 @@ #include struct bm_context { - struct bfdev_ts_context tsc; + bfdev_ts_context_t tsc; uint8_t *pattern; unsigned int pattern_len; unsigned int bad_shift[UINT8_MAX]; @@ -18,21 +18,21 @@ struct bm_context { bfdev_container_of(ptr, struct bm_context, tsc) static const void * -bm_pattern_get(struct bfdev_ts_context *tsc) +bm_pattern_get(bfdev_ts_context_t *tsc) { struct bm_context *bctx = ts_to_bm(tsc); return bctx->pattern; } static unsigned int -bm_pattern_len(struct bfdev_ts_context *tsc) +bm_pattern_len(bfdev_ts_context_t *tsc) { struct bm_context *bctx = ts_to_bm(tsc); return bctx->pattern_len; } static unsigned int -bm_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) +bm_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { #define find_pattern() (icase ? toupper(text[shift - index]) : text[shift - index]) bool icase = bfdev_ts_test_igcase(tsc); @@ -111,7 +111,7 @@ bm_compute_prefix(struct bm_context *bctx, unsigned long flags) } } -static struct bfdev_ts_context * +static bfdev_ts_context_t * bm_prepare(const struct bfdev_alloc *alloc, const void *pattern, size_t len, unsigned long flags) { @@ -137,13 +137,13 @@ bm_prepare(const struct bfdev_alloc *alloc, const void *pattern, } static void -bm_destroy(struct bfdev_ts_context *tsc) +bm_destroy(bfdev_ts_context_t *tsc) { struct bm_context *bctx = ts_to_bm(tsc); bfdev_free(tsc->alloc, bctx); } -static struct bfdev_ts_algorithm +static bfdev_ts_algorithm_t bm_algorithm = { .name = "bm", .find = bm_find, diff --git a/src/textsearch/kmp.c b/src/textsearch/kmp.c index 7d6503e1..7a13c689 100644 --- a/src/textsearch/kmp.c +++ b/src/textsearch/kmp.c @@ -7,7 +7,7 @@ #include struct kmp_context { - struct bfdev_ts_context tsc; + bfdev_ts_context_t tsc; char *pattern; unsigned int pattern_len; unsigned int prefix_table[0]; @@ -17,21 +17,21 @@ struct kmp_context { bfdev_container_of(ptr, struct kmp_context, tsc) static const void * -kmp_pattern_get(struct bfdev_ts_context *tsc) +kmp_pattern_get(bfdev_ts_context_t *tsc) { struct kmp_context *kctx = ts_to_kmp(tsc); return kctx->pattern; } static unsigned int -kmp_pattern_len(struct bfdev_ts_context *tsc) +kmp_pattern_len(bfdev_ts_context_t *tsc) { struct kmp_context *kctx = ts_to_kmp(tsc); return kctx->pattern_len; } static unsigned int -kmp_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) +kmp_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { #define find_pattern() (icase ? toupper(text[index]) : text[index]) bool icase = bfdev_ts_test_igcase(tsc); @@ -80,7 +80,7 @@ kmp_compute_prefix(struct kmp_context *kctx) } } -static struct bfdev_ts_context * +static bfdev_ts_context_t * kmp_prepare(const struct bfdev_alloc *alloc, const void *pattern, size_t len, unsigned long flags) { @@ -106,13 +106,13 @@ kmp_prepare(const struct bfdev_alloc *alloc, const void *pattern, } static void -kmp_destroy(struct bfdev_ts_context *tsc) +kmp_destroy(bfdev_ts_context_t *tsc) { struct kmp_context *kctx = ts_to_kmp(tsc); bfdev_free(tsc->alloc, kctx); } -struct bfdev_ts_algorithm +static bfdev_ts_algorithm_t kmp_algorithm = { .name = "kmp", .find = kmp_find, diff --git a/src/textsearch/linear.c b/src/textsearch/linear.c index ab05773d..7e9d3e13 100644 --- a/src/textsearch/linear.c +++ b/src/textsearch/linear.c @@ -8,10 +8,10 @@ #include static unsigned int -linear_next(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss, +linear_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, unsigned int consumed, const void **dest) { - struct bfdev_ts_linear *linear = tss->pdata; + bfdev_ts_linear_t *linear = tss->pdata; if (bfdev_likely(consumed < linear->len)) { *dest = linear->data + consumed; @@ -22,11 +22,11 @@ linear_next(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss, } export unsigned int -bfdev_textsearch_linear_find(struct bfdev_ts_context *tsc, - struct bfdev_ts_linear *linear, +bfdev_textsearch_linear_find(bfdev_ts_context_t *tsc, + bfdev_ts_linear_t *linear, const void *data, unsigned int len) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; linear->data = data; linear->len = len; @@ -38,9 +38,9 @@ bfdev_textsearch_linear_find(struct bfdev_ts_context *tsc, } export unsigned int -bfdev_textsearch_linear_next(struct bfdev_ts_context *tsc, - struct bfdev_ts_linear *linear) +bfdev_textsearch_linear_next(bfdev_ts_context_t *tsc, + bfdev_ts_linear_t *linear) { - struct bfdev_ts_algorithm *algo = tsc->algo; + bfdev_ts_algorithm_t *algo = tsc->algo; return algo->find(tsc, &linear->tss); } diff --git a/src/textsearch/sunday.c b/src/textsearch/sunday.c index 4c2d8c90..d7e5718a 100644 --- a/src/textsearch/sunday.c +++ b/src/textsearch/sunday.c @@ -7,7 +7,7 @@ #include struct sunday_context { - struct bfdev_ts_context tsc; + bfdev_ts_context_t tsc; unsigned int pattern_len; unsigned int shift_table[UINT8_MAX]; uint8_t pattern[0]; @@ -17,21 +17,21 @@ struct sunday_context { bfdev_container_of(ptr, struct sunday_context, tsc) static const void * -sunday_pattern_get(struct bfdev_ts_context *tsc) +sunday_pattern_get(bfdev_ts_context_t *tsc) { struct sunday_context *sctx = ts_to_sunday(tsc); return sctx->pattern; } static unsigned int -sunday_pattern_len(struct bfdev_ts_context *tsc) +sunday_pattern_len(bfdev_ts_context_t *tsc) { struct sunday_context *sctx = ts_to_sunday(tsc); return sctx->pattern_len; } static unsigned int -sunday_find(struct bfdev_ts_context *tsc, struct bfdev_ts_state *tss) +sunday_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { #define find_pattern() (icase ? toupper(text[shift + index]) : text[shift + index]) bool icase = bfdev_ts_test_igcase(tsc); @@ -83,7 +83,7 @@ sunday_compute_prefix(struct sunday_context *sctx, unsigned int flags) } } -static struct bfdev_ts_context * +static bfdev_ts_context_t * sunday_prepare(const struct bfdev_alloc *alloc, const void *pattern, size_t len, unsigned long flags) { @@ -107,13 +107,13 @@ sunday_prepare(const struct bfdev_alloc *alloc, const void *pattern, } static void -sunday_destroy(struct bfdev_ts_context *tsc) +sunday_destroy(bfdev_ts_context_t *tsc) { struct sunday_context *sctx = ts_to_sunday(tsc); bfdev_free(tsc->alloc, sctx); } -static struct bfdev_ts_algorithm +static bfdev_ts_algorithm_t sunday_algorithm = { .name = "sunday", .find = sunday_find, diff --git a/src/textsearch/textsearch.c b/src/textsearch/textsearch.c index 9fa6a002..dbd78ebe 100644 --- a/src/textsearch/textsearch.c +++ b/src/textsearch/textsearch.c @@ -9,10 +9,10 @@ static BFDEV_LIST_HEAD(textsearch_algorithms); -static struct bfdev_ts_algorithm * +static bfdev_ts_algorithm_t * textsearch_algorithm_find(const char *name) { - struct bfdev_ts_algorithm *algo; + bfdev_ts_algorithm_t *algo; bfdev_list_for_each_entry(algo, &textsearch_algorithms, list) { if (!strcmp(algo->name, name)) @@ -22,12 +22,12 @@ textsearch_algorithm_find(const char *name) return NULL; } -export struct bfdev_ts_context * +export bfdev_ts_context_t * bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, const void *pattern, size_t len, unsigned long flags) { - struct bfdev_ts_algorithm *algo; - struct bfdev_ts_context *tsc; + bfdev_ts_algorithm_t *algo; + bfdev_ts_context_t *tsc; algo = textsearch_algorithm_find(name); if (!algo) @@ -44,7 +44,7 @@ bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, } export int -bfdev_textsearch_register(struct bfdev_ts_algorithm *algo) +bfdev_textsearch_register(bfdev_ts_algorithm_t *algo) { if (!(algo->name && algo->find && algo->prepare && algo->destroy && algo->pattern_get && @@ -59,7 +59,7 @@ bfdev_textsearch_register(struct bfdev_ts_algorithm *algo) } export void -bfdev_textsearch_unregister(struct bfdev_ts_algorithm *algo) +bfdev_textsearch_unregister(bfdev_ts_algorithm_t *algo) { if (textsearch_algorithm_find(algo->name)) return; From f2607bdc1c5fffbf421ae9a7f83aec99f783d358 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 4 Jan 2024 10:53:19 +0800 Subject: [PATCH 031/119] fixup bcd: make dynamic functions alway inline Signed-off-by: John Sanpe --- include/bfdev/bcd.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bfdev/bcd.h b/include/bfdev/bcd.h index 9ce658fc..d64515af 100644 --- a/include/bfdev/bcd.h +++ b/include/bfdev/bcd.h @@ -34,13 +34,13 @@ bfdev_bcd2bin_table[256]; extern const uint8_t bfdev_bin2bcd_table[256]; -static __bfdev_attribute_const inline +static __bfdev_attribute_const __bfdev_always_inline uint8_t bfdev_bcd2bin_dynamic(uint8_t bcd) { return bfdev_bcd2bin_table[bcd]; } -static inline __bfdev_attribute_const +static __bfdev_attribute_const __bfdev_always_inline uint8_t bfdev_bin2bcd_dynamic(uint8_t bin) { return bfdev_bin2bcd_table[bin]; From 5c6e1cf72133949249b5817e5733d3c5757a563d Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 7 Jan 2024 11:14:38 +0800 Subject: [PATCH 032/119] feat errname: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/errname.h | 6 ++++-- src/errname.c | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/bfdev/errname.h b/include/bfdev/errname.h index 886f29f1..a808239b 100644 --- a/include/bfdev/errname.h +++ b/include/bfdev/errname.h @@ -13,16 +13,18 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_errname bfdev_errname_t; + struct bfdev_errname { int errnum; const char *name; const char *info; }; -extern const struct bfdev_errname +extern const bfdev_errname_t bfdev_errname_table[]; -extern struct bfdev_errname * +extern bfdev_errname_t * bfdev_errname_find(int error); /** diff --git a/src/errname.c b/src/errname.c index 4e3d6a39..7ef60628 100644 --- a/src/errname.c +++ b/src/errname.c @@ -12,7 +12,7 @@ BFDEV_##errnum, "-" #errnum, infos \ } -export const struct bfdev_errname +export const bfdev_errname_t bfdev_errname_table[] = { ERRNAME(ENOERR, "No error"), ERRNAME(EPERM, "Operation not permitted"), @@ -132,14 +132,14 @@ bfdev_errname_table[] = { static long errname_search(const void *key, void *pdata) { - const struct bfdev_errname *entry = key; + const bfdev_errname_t *entry = key; return entry->errnum - (intptr_t)pdata; } -export struct bfdev_errname * +export bfdev_errname_t * bfdev_errname_find(int error) { - struct bfdev_errname *entry; + bfdev_errname_t *entry; if (error < 0) error = -error; @@ -155,7 +155,7 @@ bfdev_errname_find(int error) export const char * bfdev_errname(int error, const char **infop) { - struct bfdev_errname *entry; + bfdev_errname_t *entry; const char *name; entry = bfdev_errname_find(error); From aca254e110c2dd15fa33854036734660884e1a8f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 7 Jan 2024 11:33:17 +0800 Subject: [PATCH 033/119] feat ilist: added initial typedef Signed-off-by: John Sanpe --- examples/ilist/selftest.c | 6 +++--- include/bfdev/ilist.h | 41 +++++++++++++++++++++------------------ include/bfdev/list.h | 3 ++- include/bfdev/notifier.h | 4 ++-- src/ilist.c | 10 +++++----- src/notifier.c | 4 ++-- 6 files changed, 36 insertions(+), 32 deletions(-) diff --git a/examples/ilist/selftest.c b/examples/ilist/selftest.c index 9b9f7bfc..e87db71c 100644 --- a/examples/ilist/selftest.c +++ b/examples/ilist/selftest.c @@ -10,7 +10,7 @@ #define TEST_LOOP 10 struct test_node { - struct bfdev_ilist_node ilist; + bfdev_ilist_node_t ilist; unsigned long num; }; @@ -22,8 +22,8 @@ struct test_pdata { bfdev_ilist_entry(ptr, struct test_node, ilist) static inline long -ilist_cmp(const struct bfdev_ilist_node *node1, - const struct bfdev_ilist_node *node2, void *pdata) +ilist_cmp(const bfdev_ilist_node_t *node1, + const bfdev_ilist_node_t *node2, void *pdata) { struct test_node *tnode1, *tnode2; diff --git a/include/bfdev/ilist.h b/include/bfdev/ilist.h index c616a4d4..71bbdc04 100644 --- a/include/bfdev/ilist.h +++ b/include/bfdev/ilist.h @@ -12,6 +12,9 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_ilist_head bfdev_ilist_head_t; +typedef struct bfdev_ilist_node bfdev_ilist_node_t; + struct bfdev_ilist_head { struct bfdev_list_head node_list; }; @@ -25,20 +28,20 @@ struct bfdev_ilist_node { {BFDEV_LIST_HEAD_STATIC((name).node_list)} #define BFDEV_ILIST_HEAD_INIT(name) \ - (struct bfdev_ilist_head) BFDEV_ILIST_HEAD_STATIC(name) + (bfdev_ilist_head_t) BFDEV_ILIST_HEAD_STATIC(name) #define BFDEV_ILIST_HEAD(name) \ - struct bfdev_ilist_head name = BFDEV_ILIST_HEAD_STATIC(name) + bfdev_ilist_head_t name = BFDEV_ILIST_HEAD_STATIC(name) #define BFDEV_ILISI_NODE_INIT(name, index) \ - (struct bfdev_ilist_node) { \ + (bfdev_ilist_node_t) { \ .node_list = BFDEV_LIST_HEAD_INIT((node).node_list), \ .index_list = BFDEV_LIST_HEAD_INIT((node).index_list), \ } BFDEV_CALLBACK_CMP( bfdev_ilist_cmp_t, - const struct bfdev_ilist_node * + const bfdev_ilist_node_t * ); /** @@ -47,7 +50,7 @@ BFDEV_CALLBACK_CMP( * @inode: the index node to insert. */ extern void -bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, +bfdev_ilist_add(bfdev_ilist_head_t *ihead, bfdev_ilist_node_t *inode, bfdev_ilist_cmp_t cmp, void *pdata); /** @@ -56,14 +59,14 @@ bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, * @inode: the index node to delete. */ extern void -bfdev_ilist_del(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode); +bfdev_ilist_del(bfdev_ilist_head_t *ihead, bfdev_ilist_node_t *inode); /** * bfdev_ilist_head_init() - initialize a bfdev_ilist_head structure. * @head: bfdev_ilist_head structure to be initialized. */ static inline void -bfdev_ilist_head_init(struct bfdev_ilist_head *ihead) +bfdev_ilist_head_init(bfdev_ilist_head_t *ihead) { bfdev_list_head_init(&ihead->node_list); } @@ -73,7 +76,7 @@ bfdev_ilist_head_init(struct bfdev_ilist_head *ihead) * @head: bfdev_ilist_node structure to be initialized. */ static inline void -bfdev_ilist_node_init(struct bfdev_ilist_node *inode) +bfdev_ilist_node_init(bfdev_ilist_node_t *inode) { bfdev_list_head_init(&inode->node_list); bfdev_list_head_init(&inode->index_list); @@ -81,22 +84,22 @@ bfdev_ilist_node_init(struct bfdev_ilist_node *inode) /** * bfdev_ilist_first() - return the first node. - * @ihead: the &struct bfdev_ilist_head pointer + * @ihead: the &bfdev_ilist_head_t pointer */ -static inline struct bfdev_ilist_node * -bfdev_ilist_first(const struct bfdev_ilist_head *ihead) +static inline bfdev_ilist_node_t * +bfdev_ilist_first(const bfdev_ilist_head_t *ihead) { - return bfdev_list_first_entry(&ihead->node_list, struct bfdev_ilist_node, node_list); + return bfdev_list_first_entry(&ihead->node_list, bfdev_ilist_node_t, node_list); } /** * bfdev_ilist_last() - return the last node. - * @ihead:the &struct bfdev_ilist_head pointer + * @ihead:the &bfdev_ilist_head_t pointer */ -static inline struct bfdev_ilist_node * -bfdev_ilist_last(const struct bfdev_ilist_head *ihead) +static inline bfdev_ilist_node_t * +bfdev_ilist_last(const bfdev_ilist_head_t *ihead) { - return bfdev_list_last_entry(&ihead->node_list, struct bfdev_ilist_node, node_list); + return bfdev_list_last_entry(&ihead->node_list, bfdev_ilist_node_t, node_list); } /** @@ -104,7 +107,7 @@ bfdev_ilist_last(const struct bfdev_ilist_head *ihead) * @ihead: list head to check. */ static inline bool -bfdev_ilist_head_empty(struct bfdev_ilist_head *ihead) +bfdev_ilist_head_empty(bfdev_ilist_head_t *ihead) { return bfdev_list_check_empty(&ihead->node_list); } @@ -114,14 +117,14 @@ bfdev_ilist_head_empty(struct bfdev_ilist_head *ihead) * @inode: list node to check. */ static inline bool -bfdev_ilist_node_empty(struct bfdev_ilist_node *inode) +bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) { return bfdev_list_check_empty(&inode->index_list); } /** * bfdev_ilist_entry - get the struct for this entry. - * @ptr: the &struct bfdev_ilist_head pointer. + * @ptr: the &bfdev_ilist_head_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_ilist_head within the struct. */ diff --git a/include/bfdev/list.h b/include/bfdev/list.h index 46a6a8f3..0b1ccf08 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -39,7 +39,8 @@ bfdev_list_sort(struct bfdev_list_head *head, #ifdef BFDEV_DEBUG_LIST extern bool -bfdev_list_check_add(struct bfdev_list_head *prev, struct bfdev_list_head *next, struct bfdev_list_head *node); +bfdev_list_check_add(struct bfdev_list_head *prev, struct bfdev_list_head *next, + struct bfdev_list_head *node); extern bool bfdev_list_check_del(struct bfdev_list_head *node); diff --git a/include/bfdev/notifier.h b/include/bfdev/notifier.h index 38fb0aff..55a74a90 100644 --- a/include/bfdev/notifier.h +++ b/include/bfdev/notifier.h @@ -36,7 +36,7 @@ enum bfdev_notifier_ret { * @pdata: private data of notification chain node. */ struct bfdev_notifier_node { - struct bfdev_ilist_node list; + bfdev_ilist_node_t list; bfdev_notifier_entry_t entry; int priority; void *pdata; @@ -48,7 +48,7 @@ struct bfdev_notifier_node { * @name: name of notification chain. */ struct bfdev_notifier_head { - struct bfdev_ilist_head node; + bfdev_ilist_head_t node; const char *name; }; diff --git a/src/ilist.c b/src/ilist.c index c87b71f1..40b4a446 100644 --- a/src/ilist.c +++ b/src/ilist.c @@ -54,7 +54,7 @@ ilist_list_check(struct bfdev_list_head *head) } static inline bool -ilist_head_check(struct bfdev_ilist_head *ihead) +ilist_head_check(bfdev_ilist_head_t *ihead) { bool success; @@ -67,10 +67,10 @@ ilist_head_check(struct bfdev_ilist_head *ihead) #endif export void -bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, +bfdev_ilist_add(bfdev_ilist_head_t *ihead, bfdev_ilist_node_t *inode, bfdev_ilist_cmp_t cmp, void *pdata) { - struct bfdev_ilist_node *walk, *first, *prev = NULL; + bfdev_ilist_node_t *walk, *first, *prev = NULL; struct bfdev_list_head *next = &ihead->node_list; #ifdef BFDEV_DEBUG_ILIST @@ -111,9 +111,9 @@ bfdev_ilist_add(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode, } export void -bfdev_ilist_del(struct bfdev_ilist_head *ihead, struct bfdev_ilist_node *inode) +bfdev_ilist_del(bfdev_ilist_head_t *ihead, bfdev_ilist_node_t *inode) { - struct bfdev_ilist_node *next; + bfdev_ilist_node_t *next; #ifdef BFDEV_DEBUG_ILIST if (bfdev_unlikely(!ilist_head_check(ihead))) diff --git a/src/notifier.c b/src/notifier.c index 250d8a04..5a37a622 100755 --- a/src/notifier.c +++ b/src/notifier.c @@ -12,8 +12,8 @@ #include static long -notifier_chain_cmp(const struct bfdev_ilist_node *node1, - const struct bfdev_ilist_node *node2, void *pdata) +notifier_chain_cmp(const bfdev_ilist_node_t *node1, + const bfdev_ilist_node_t *node2, void *pdata) { struct bfdev_notifier_node *nnode1, *nnode2; From faed2b19546e78934e5c262a0a90f655a54c1523 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 7 Jan 2024 11:36:18 +0800 Subject: [PATCH 034/119] feat action: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/action.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/include/bfdev/action.h b/include/bfdev/action.h index dcf083a2..faccf388 100644 --- a/include/bfdev/action.h +++ b/include/bfdev/action.h @@ -13,10 +13,12 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_action bfdev_action_t; + typedef int (*bfdev_action_func_t)(void *data); -struct bfdev_action { +struct bfdev_action { bfdev_action_func_t func; const void *data; }; @@ -26,10 +28,10 @@ struct bfdev_action { } #define BFDEV_ACTION_INIT(func, data) \ - (struct bfdev_action) BFDEV_ACTION_STATIC(func, data) + (bfdev_action_t) BFDEV_ACTION_STATIC(func, data) #define BFDEV_DEFINE_ACTION(name, func, data) \ - struct bfdev_action name = BFDEV_ACTION_INIT(func, data) + bfdev_action_t name = BFDEV_ACTION_INIT(func, data) /** * bfdev_action_init - initialize action. @@ -38,7 +40,7 @@ struct bfdev_action { * @data: callback data of @func. */ static inline void -bfdev_action_init(struct bfdev_action *action, +bfdev_action_init(bfdev_action_t *action, bfdev_action_func_t func, const void *data) { *action = BFDEV_ACTION_INIT(func, data); @@ -50,7 +52,7 @@ bfdev_action_init(struct bfdev_action *action, * @data: the data to update. */ static inline void -bfdev_action_update(struct bfdev_action *action, const void *data) +bfdev_action_update(bfdev_action_t *action, const void *data) { action->data = data; } @@ -60,7 +62,7 @@ bfdev_action_update(struct bfdev_action *action, const void *data) * @action: the action pointer. */ static inline void -bfdev_action_clear(struct bfdev_action *action) +bfdev_action_clear(bfdev_action_t *action) { action->func = NULL; } @@ -70,7 +72,7 @@ bfdev_action_clear(struct bfdev_action *action) * @action: the action pointer. */ static inline int -bfdev_action_call(struct bfdev_action *action) +bfdev_action_call(bfdev_action_t *action) { if (bfdev_likely(action->func)) return action->func((void *)action->data); From 24a55b0702a9d13ee3bca094da4e55629f271529 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 7 Jan 2024 12:00:27 +0800 Subject: [PATCH 035/119] feat action: added simple examples Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/action/.gitignore | 2 ++ examples/action/CMakeLists.txt | 22 +++++++++++++++++++++ examples/action/simple.c | 36 ++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 examples/action/.gitignore create mode 100644 examples/action/CMakeLists.txt create mode 100644 examples/action/simple.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2eb3f771..caff636d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -3,6 +3,7 @@ # Copyright(c) 2023 ffashion # +add_subdirectory(action) add_subdirectory(allocator) add_subdirectory(array) add_subdirectory(base32) diff --git a/examples/action/.gitignore b/examples/action/.gitignore new file mode 100644 index 00000000..b5bc0992 --- /dev/null +++ b/examples/action/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/action-simple diff --git a/examples/action/CMakeLists.txt b/examples/action/CMakeLists.txt new file mode 100644 index 00000000..df3f0a36 --- /dev/null +++ b/examples/action/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(action-simple simple.c) +target_link_libraries(action-simple bfdev) +add_test(action-simple action-simple) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + simple.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/action + ) + + install(TARGETS + action-simple + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/action/simple.c b/examples/action/simple.c new file mode 100644 index 00000000..49ad149e --- /dev/null +++ b/examples/action/simple.c @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "action-simple" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include + +static int +test_action(void *pdata) +{ + bfdev_log_info("%s\n", (char *)pdata); + return 0; +} + +int main(int argc, char **argv) +{ + BFDEV_DEFINE_ACTION(action, test_action, NULL); + int retval; + + bfdev_action_update(&action, "hello world"); + retval = bfdev_action_call(&action); + if (retval) + return retval; + + bfdev_action_update(&action, "wow bfdev"); + retval = bfdev_action_call(&action); + if (retval) + return retval; + + return 0; +} From aa738626a25fe5033fd9ec860c9b11c820c13243 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 7 Jan 2024 12:14:18 +0800 Subject: [PATCH 036/119] feat array: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/array.h | 26 ++++++++++++++------------ include/bfdev/fsm.h | 2 +- src/array.c | 12 ++++++------ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 6dce55bc..014a58c6 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -18,6 +18,8 @@ BFDEV_BEGIN_DECLS # define BFDEV_ARRAY_MSIZE 32 #endif +typedef struct bfdev_array bfdev_array_t; + struct bfdev_array { const struct bfdev_alloc *alloc; unsigned long capacity; @@ -30,44 +32,44 @@ struct bfdev_array { {.alloc = (ALLOC), .cells = (CELLS)} #define BFDEV_ARRAY_INIT(alloc, cells) \ - (struct bfdev_array) BFDEV_ARRAY_STATIC(alloc, cells) + (bfdev_array_t) BFDEV_ARRAY_STATIC(alloc, cells) #define BFDEV_DEFINE_ARRAY(name, alloc, cells) \ - struct bfdev_array name = BFDEV_ARRAY_INIT(alloc, cells) + bfdev_array_t name = BFDEV_ARRAY_INIT(alloc, cells) static inline void -bfdev_array_init(struct bfdev_array *array, const struct bfdev_alloc *alloc, +bfdev_array_init(bfdev_array_t *array, const struct bfdev_alloc *alloc, size_t cells) { *array = BFDEV_ARRAY_INIT(alloc, cells); } static inline void -bfdev_array_reset(struct bfdev_array *array) +bfdev_array_reset(bfdev_array_t *array) { array->index = 0; } static inline unsigned long -bfdev_array_index(struct bfdev_array *array) +bfdev_array_index(bfdev_array_t *array) { return array->index; } static inline size_t -bfdev_array_size(struct bfdev_array *array) +bfdev_array_size(bfdev_array_t *array) { return array->cells * array->index; } static inline uintptr_t -bfdev_array_offset(struct bfdev_array *array, unsigned long index) +bfdev_array_offset(bfdev_array_t *array, unsigned long index) { return array->cells * index; } static inline void * -bfdev_array_data(struct bfdev_array *array, unsigned long index) +bfdev_array_data(bfdev_array_t *array, unsigned long index) { if (bfdev_unlikely(index >= array->index)) return NULL; @@ -75,16 +77,16 @@ bfdev_array_data(struct bfdev_array *array, unsigned long index) } extern void * -bfdev_array_push(struct bfdev_array *array, unsigned long num); +bfdev_array_push(bfdev_array_t *array, unsigned long num); extern void * -bfdev_array_pop(struct bfdev_array *array, unsigned long num); +bfdev_array_pop(bfdev_array_t *array, unsigned long num); extern int -bfdev_array_reserve(struct bfdev_array *array, unsigned long num); +bfdev_array_reserve(bfdev_array_t *array, unsigned long num); extern void -bfdev_array_release(struct bfdev_array *array); +bfdev_array_release(bfdev_array_t *array); BFDEV_END_DECLS diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index 90932de0..6ae62ca5 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -72,7 +72,7 @@ struct bfdev_fsm_state { struct bfdev_fsm { const bfdev_fsm_state_t *state[2]; const bfdev_fsm_state_t *error; - struct bfdev_array stack; + bfdev_array_t stack; unsigned int count; }; diff --git a/src/array.c b/src/array.c index b88d6b7b..ea1a6097 100644 --- a/src/array.c +++ b/src/array.c @@ -10,7 +10,7 @@ #include static int -array_resize(struct bfdev_array *array, unsigned long count) +array_resize(bfdev_array_t *array, unsigned long count) { const struct bfdev_alloc *alloc = array->alloc; unsigned long nalloc; @@ -31,7 +31,7 @@ array_resize(struct bfdev_array *array, unsigned long count) } static int -array_apply(struct bfdev_array *array, unsigned long count) +array_apply(bfdev_array_t *array, unsigned long count) { if (count > array->capacity) return array_resize(array, count); @@ -39,7 +39,7 @@ array_apply(struct bfdev_array *array, unsigned long count) } export void * -bfdev_array_push(struct bfdev_array *array, unsigned long num) +bfdev_array_push(bfdev_array_t *array, unsigned long num) { unsigned long index, count; uintptr_t offset; @@ -62,7 +62,7 @@ bfdev_array_push(struct bfdev_array *array, unsigned long num) } export void * -bfdev_array_pop(struct bfdev_array *array, unsigned long num) +bfdev_array_pop(bfdev_array_t *array, unsigned long num) { unsigned long index; uintptr_t offset; @@ -79,7 +79,7 @@ bfdev_array_pop(struct bfdev_array *array, unsigned long num) } export int -bfdev_array_reserve(struct bfdev_array *array, unsigned long num) +bfdev_array_reserve(bfdev_array_t *array, unsigned long num) { unsigned long count; bool overflow; @@ -92,7 +92,7 @@ bfdev_array_reserve(struct bfdev_array *array, unsigned long num) } export void -bfdev_array_release(struct bfdev_array *array) +bfdev_array_release(bfdev_array_t *array) { const struct bfdev_alloc *alloc = array->alloc; From 97748937497bb457f89709384567a773b86e550f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 12:36:45 +0800 Subject: [PATCH 037/119] feat bloom: added initial typedef Signed-off-by: John Sanpe --- examples/bloom/simple.c | 2 +- include/bfdev/bloom.h | 12 +++++++----- src/bloom.c | 14 +++++++------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/bloom/simple.c b/examples/bloom/simple.c index db3a2294..daa60d64 100644 --- a/examples/bloom/simple.c +++ b/examples/bloom/simple.c @@ -37,7 +37,7 @@ bloom_hash(unsigned int func, const void *key, void *pdata) int main(int argc, const char *argv[]) { - struct bfdev_bloom *bloom; + bfdev_bloom_t *bloom; char buffer[TEST_SIZE][32]; unsigned int count; bool retval; diff --git a/include/bfdev/bloom.h b/include/bfdev/bloom.h index dd7e707e..fe907831 100644 --- a/include/bfdev/bloom.h +++ b/include/bfdev/bloom.h @@ -14,6 +14,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_bloom bfdev_bloom_t; + typedef unsigned int (*bfdev_bloom_hash_t) (unsigned int func, const void *key, void *pdata); @@ -35,7 +37,7 @@ struct bfdev_bloom { * @return: object value. */ extern bool -bfdev_bloom_peek(struct bfdev_bloom *bloom, void *key); +bfdev_bloom_peek(bfdev_bloom_t *bloom, void *key); /** * bfdev_bloom_push() - push an object from a bloom filter. @@ -45,14 +47,14 @@ bfdev_bloom_peek(struct bfdev_bloom *bloom, void *key); * @return: object value before push. */ extern bool -bfdev_bloom_push(struct bfdev_bloom *bloom, void *key); +bfdev_bloom_push(bfdev_bloom_t *bloom, void *key); /** * bfdev_bloom_flush() - flush the entire bloom filter. * @bloom: bloom filter pointer. */ extern void -bfdev_bloom_flush(struct bfdev_bloom *bloom); +bfdev_bloom_flush(bfdev_bloom_t *bloom); /** * bfdev_bloom_create() - creat a bloom filter. @@ -61,7 +63,7 @@ bfdev_bloom_flush(struct bfdev_bloom *bloom); * @funcs: number of supported hash algorithms. * @pdata: private data pointer of @hash. */ -extern struct bfdev_bloom * +extern bfdev_bloom_t * bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, bfdev_bloom_hash_t hash, unsigned int funcs, void *pdata); @@ -70,7 +72,7 @@ bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, * @bloom: bloom filter pointer. */ extern void -bfdev_bloom_destory(struct bfdev_bloom *bloom); +bfdev_bloom_destory(bfdev_bloom_t *bloom); BFDEV_END_DECLS diff --git a/src/bloom.c b/src/bloom.c index de0bfb0a..906cb038 100644 --- a/src/bloom.c +++ b/src/bloom.c @@ -10,7 +10,7 @@ #include static unsigned int -bloom_index(struct bfdev_bloom *bloom, unsigned int func, void *key) +bloom_index(bfdev_bloom_t *bloom, unsigned int func, void *key) { unsigned int value, index; @@ -21,7 +21,7 @@ bloom_index(struct bfdev_bloom *bloom, unsigned int func, void *key) } export bool -bfdev_bloom_peek(struct bfdev_bloom *bloom, void *key) +bfdev_bloom_peek(bfdev_bloom_t *bloom, void *key) { unsigned int index, func; bool retval = true; @@ -36,7 +36,7 @@ bfdev_bloom_peek(struct bfdev_bloom *bloom, void *key) } export bool -bfdev_bloom_push(struct bfdev_bloom *bloom, void *key) +bfdev_bloom_push(bfdev_bloom_t *bloom, void *key) { unsigned int index, func; bool retval = true; @@ -51,17 +51,17 @@ bfdev_bloom_push(struct bfdev_bloom *bloom, void *key) } export void -bfdev_bloom_flush(struct bfdev_bloom *bloom) +bfdev_bloom_flush(bfdev_bloom_t *bloom) { size_t size = BFDEV_BITS_WORD(bloom->capacity); memset(bloom->bitmap, 0, size); } -export struct bfdev_bloom * +export bfdev_bloom_t * bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, bfdev_bloom_hash_t hash, unsigned int funcs, void *pdata) { - struct bfdev_bloom *bloom; + bfdev_bloom_t *bloom; size_t size; bfdev_align_high_adj(capacity, BFDEV_BITS_PER_LONG); @@ -81,7 +81,7 @@ bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, } export void -bfdev_bloom_destory(struct bfdev_bloom *bloom) +bfdev_bloom_destory(bfdev_bloom_t *bloom) { const struct bfdev_alloc *alloc = bloom->alloc; bfdev_free(alloc, bloom); From babf54259c7ebb9a87f124b2ccca097e499fa4ad Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 12:40:38 +0800 Subject: [PATCH 038/119] feat hashmap: added initial typedef Signed-off-by: John Sanpe --- examples/hashmap/benchmark.c | 2 +- examples/hashmap/simple.c | 2 +- include/bfdev/hashmap.h | 33 +++++++++++++++------------- src/hashmap.c | 42 ++++++++++++++++++------------------ 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/examples/hashmap/benchmark.c b/examples/hashmap/benchmark.c index 5e83ccf3..4be500ca 100644 --- a/examples/hashmap/benchmark.c +++ b/examples/hashmap/benchmark.c @@ -51,7 +51,7 @@ test_find(const struct bfdev_hlist_node *node, const void *key, void *pdata) return tnode->value - (unsigned long)key; } -static struct bfdev_hashmap_ops +static bfdev_hashmap_ops_t test_ops = { .hash_key = test_hash_key, .hash_node = test_hash_node, diff --git a/examples/hashmap/simple.c b/examples/hashmap/simple.c index 4eb67780..4832672c 100644 --- a/examples/hashmap/simple.c +++ b/examples/hashmap/simple.c @@ -48,7 +48,7 @@ hashmap_find(const struct bfdev_hlist_node *node, const void *key, void *pdata) return tnode->value - (unsigned long)key; } -static struct bfdev_hashmap_ops +static bfdev_hashmap_ops_t test_ops = { .hash_key = hashmap_hash_key, .hash_node = hashmap_hash_node, diff --git a/include/bfdev/hashmap.h b/include/bfdev/hashmap.h index b72b9f9d..e1498127 100644 --- a/include/bfdev/hashmap.h +++ b/include/bfdev/hashmap.h @@ -17,6 +17,9 @@ BFDEV_BEGIN_DECLS # define BFDEV_HASHMAP_MIN_BITS 4 #endif +typedef struct bfdev_hashmap bfdev_hashmap_t; +typedef struct bfdev_hashmap_ops bfdev_hashmap_ops_t; + /** * enum bfdev_hashmap_strategy - Hashmap insertion strategy. * @HASHMAP_ADD: only add key/value if key doesn't exist yet. @@ -38,7 +41,7 @@ struct bfdev_hashmap { unsigned long used; const struct bfdev_alloc *alloc; - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; void *pdata; }; @@ -48,8 +51,8 @@ struct bfdev_hashmap_ops { long (*equal)(const struct bfdev_hlist_node *node1, const struct bfdev_hlist_node *node2, void *pdata); long (*find)(const struct bfdev_hlist_node *node, const void *key, void *pdata); - bool (*extend)(const struct bfdev_hashmap *hashmap, void *pdata); - bool (*shrink)(const struct bfdev_hashmap *hashmap, void *pdata); + bool (*extend)(const bfdev_hashmap_t *hashmap, void *pdata); + bool (*shrink)(const bfdev_hashmap_t *hashmap, void *pdata); }; #define BFDEV_HASHMAP_STATIC(ALLOC, OPS, PDATA) { \ @@ -57,10 +60,10 @@ struct bfdev_hashmap_ops { } #define BFDEV_HASHMAP_INIT(alloc, ops, pdata) \ - (struct bfdev_hashmap) BFDEV_HASHMAP_STATIC(alloc, ops, pdata) + (bfdev_hashmap_t) BFDEV_HASHMAP_STATIC(alloc, ops, pdata) #define BFDEV_DEFINE_HASHMAP(name, alloc, ops, pdata) \ - struct bfdev_hashmap name = BFDEV_HASHMAP_INIT(alloc, ops, pdata) + bfdev_hashmap_t name = BFDEV_HASHMAP_INIT(alloc, ops, pdata) /** * bfdev_hashmap_init() - initialize a hashmap structure. @@ -70,8 +73,8 @@ struct bfdev_hashmap_ops { * @pdata: operations callback data. */ static inline void -bfdev_hashmap_init(struct bfdev_hashmap *hashmap, const struct bfdev_alloc *alloc, - const struct bfdev_hashmap_ops *ops, void *pdata) +bfdev_hashmap_init(bfdev_hashmap_t *hashmap, const struct bfdev_alloc *alloc, + const bfdev_hashmap_ops_t *ops, void *pdata) { *hashmap = BFDEV_HASHMAP_INIT(alloc, ops, pdata); } @@ -84,7 +87,7 @@ bfdev_hashmap_init(struct bfdev_hashmap *hashmap, const struct bfdev_alloc *allo * @strategy: insertion strategy. */ extern int -bfdev_hashmap_insert(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node, +bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, struct bfdev_hlist_node **old, enum bfdev_hashmap_strategy strategy); /** @@ -94,7 +97,7 @@ bfdev_hashmap_insert(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *nod * @node: pointer used to return the deleted node. */ extern int -bfdev_hashmap_del(struct bfdev_hashmap *hashmap, const void *key, +bfdev_hashmap_del(bfdev_hashmap_t *hashmap, const void *key, struct bfdev_hlist_node **node); /** @@ -103,37 +106,37 @@ bfdev_hashmap_del(struct bfdev_hashmap *hashmap, const void *key, * @key: key of the node to be find. */ extern struct bfdev_hlist_node * -bfdev_hashmap_find(struct bfdev_hashmap *hashmap, const void *key); +bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key); /** * bfdev_hashmap_release() - release hash bucket in hashmap. * @hashmap: hashmap structure to be release. */ extern void -bfdev_hashmap_release(struct bfdev_hashmap *hashmap); +bfdev_hashmap_release(bfdev_hashmap_t *hashmap); static __bfdev_always_inline int -bfdev_hashmap_add(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node) +bfdev_hashmap_add(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node) { return bfdev_hashmap_insert(hashmap, node, NULL, BFDEV_HASHMAP_ADD); } static __bfdev_always_inline int -bfdev_hashmap_set(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node, +bfdev_hashmap_set(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, struct bfdev_hlist_node **old) { return bfdev_hashmap_insert(hashmap, node, old, BFDEV_HASHMAP_SET); } static __bfdev_always_inline int -bfdev_hashmap_update(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node, +bfdev_hashmap_update(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, struct bfdev_hlist_node **old) { return bfdev_hashmap_insert(hashmap, node, old, BFDEV_HASHMAP_UPDATE); } static __bfdev_always_inline int -bfdev_hashmap_append(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node) +bfdev_hashmap_append(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node) { return bfdev_hashmap_insert(hashmap, node, NULL, BFDEV_HASHMAP_APPEND); } diff --git a/src/hashmap.c b/src/hashmap.c index dc3dbc52..0a0e84c4 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -8,10 +8,10 @@ #include static __bfdev_always_inline unsigned long -hashmap_hash_node(struct bfdev_hashmap *hashmap, +hashmap_hash_node(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; unsigned long retval; ops = hashmap->ops; @@ -21,10 +21,10 @@ hashmap_hash_node(struct bfdev_hashmap *hashmap, } static __bfdev_always_inline unsigned long -hashmap_hash_key(const struct bfdev_hashmap *hashmap, +hashmap_hash_key(const bfdev_hashmap_t *hashmap, const void *key) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; unsigned long retval; ops = hashmap->ops; @@ -34,10 +34,10 @@ hashmap_hash_key(const struct bfdev_hashmap *hashmap, } static __bfdev_always_inline long -hashmap_equal(struct bfdev_hashmap *hashmap, const struct bfdev_hlist_node *node1, +hashmap_equal(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node1, const struct bfdev_hlist_node *node2) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; long retval; ops = hashmap->ops; @@ -47,10 +47,10 @@ hashmap_equal(struct bfdev_hashmap *hashmap, const struct bfdev_hlist_node *node } static __bfdev_always_inline long -hashmap_find(struct bfdev_hashmap *hashmap, const void *key, +hashmap_find(bfdev_hashmap_t *hashmap, const void *key, const struct bfdev_hlist_node *node) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; long retval; ops = hashmap->ops; @@ -60,9 +60,9 @@ hashmap_find(struct bfdev_hashmap *hashmap, const void *key, } static __bfdev_always_inline bool -hashmap_need_extend(struct bfdev_hashmap *hashmap) +hashmap_need_extend(bfdev_hashmap_t *hashmap) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; if (!hashmap->capacity) return true; @@ -76,9 +76,9 @@ hashmap_need_extend(struct bfdev_hashmap *hashmap) } static __bfdev_always_inline bool -hashmap_need_shrink(struct bfdev_hashmap *hashmap) +hashmap_need_shrink(bfdev_hashmap_t *hashmap) { - const struct bfdev_hashmap_ops *ops; + const bfdev_hashmap_ops_t *ops; ops = hashmap->ops; if (ops->shrink) @@ -89,7 +89,7 @@ hashmap_need_shrink(struct bfdev_hashmap *hashmap) } static inline struct bfdev_hlist_node * -hashmap_find_node(struct bfdev_hashmap *hashmap, const struct bfdev_hlist_node *node, +hashmap_find_node(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node, unsigned long hash) { struct bfdev_hlist_node *walk; @@ -108,7 +108,7 @@ hashmap_find_node(struct bfdev_hashmap *hashmap, const struct bfdev_hlist_node * } static inline struct bfdev_hlist_node * -hashmap_find_key(struct bfdev_hashmap *hashmap, const void *key, +hashmap_find_key(bfdev_hashmap_t *hashmap, const void *key, unsigned long hash) { struct bfdev_hlist_node *walk; @@ -127,7 +127,7 @@ hashmap_find_key(struct bfdev_hashmap *hashmap, const void *key, } static inline int -hashmap_rehash(struct bfdev_hashmap *hashmap, unsigned int nbits) +hashmap_rehash(bfdev_hashmap_t *hashmap, unsigned int nbits) { const struct bfdev_alloc *alloc = hashmap->alloc; struct bfdev_hlist_node *walk, *tmp; @@ -156,7 +156,7 @@ hashmap_rehash(struct bfdev_hashmap *hashmap, unsigned int nbits) } static inline int -hashmap_extend(struct bfdev_hashmap *hashmap) +hashmap_extend(bfdev_hashmap_t *hashmap) { unsigned int nbits; @@ -168,7 +168,7 @@ hashmap_extend(struct bfdev_hashmap *hashmap) } static inline int -hashmap_shrink(struct bfdev_hashmap *hashmap) +hashmap_shrink(bfdev_hashmap_t *hashmap) { unsigned int nbits; @@ -180,7 +180,7 @@ hashmap_shrink(struct bfdev_hashmap *hashmap) } export int -bfdev_hashmap_insert(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *node, +bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, struct bfdev_hlist_node **old, enum bfdev_hashmap_strategy strategy) { struct bfdev_hlist_node *exist; @@ -218,7 +218,7 @@ bfdev_hashmap_insert(struct bfdev_hashmap *hashmap, struct bfdev_hlist_node *nod } export int -bfdev_hashmap_del(struct bfdev_hashmap *hashmap, const void *key, +bfdev_hashmap_del(bfdev_hashmap_t *hashmap, const void *key, struct bfdev_hlist_node **node) { struct bfdev_hlist_node *exist; @@ -241,7 +241,7 @@ bfdev_hashmap_del(struct bfdev_hashmap *hashmap, const void *key, } export struct bfdev_hlist_node * -bfdev_hashmap_find(struct bfdev_hashmap *hashmap, const void *key) +bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key) { struct bfdev_hlist_node *exist; unsigned long value; @@ -254,7 +254,7 @@ bfdev_hashmap_find(struct bfdev_hashmap *hashmap, const void *key) } export void -bfdev_hashmap_release(struct bfdev_hashmap *hashmap) +bfdev_hashmap_release(bfdev_hashmap_t *hashmap) { const struct bfdev_alloc *alloc = hashmap->alloc; From b7bf1e25c5c35a7ace33c25e5c197ca57ca422ae Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 12:45:25 +0800 Subject: [PATCH 039/119] feat rbtree: added initial typedef Signed-off-by: John Sanpe --- examples/rbtree/benchmark.c | 8 +- examples/rbtree/selftest.c | 8 +- examples/rbtree/simple.c | 4 +- include/bfdev/rbtree.h | 357 ++++++++++++++++++------------------ include/bfdev/segtree.h | 20 +- src/rbtree-debug.c | 6 +- src/rbtree.c | 98 +++++----- 7 files changed, 252 insertions(+), 249 deletions(-) diff --git a/examples/rbtree/benchmark.c b/examples/rbtree/benchmark.c index 40f21e5b..ed0a3846 100644 --- a/examples/rbtree/benchmark.c +++ b/examples/rbtree/benchmark.c @@ -17,7 +17,7 @@ #define TEST_LEN 1000000 struct bench_node { - struct bfdev_rb_node node; + bfdev_rb_node_t node; unsigned int num; unsigned long data; }; @@ -60,7 +60,7 @@ static BFDEV_RB_ROOT(bench_root); #endif static unsigned int -test_deepth(struct bfdev_rb_node *node) +test_deepth(bfdev_rb_node_t *node) { unsigned int left_deepth, right_deepth; @@ -73,8 +73,8 @@ test_deepth(struct bfdev_rb_node *node) } static long -demo_cmp(const struct bfdev_rb_node *node1, - const struct bfdev_rb_node *node2, void *pdata) +demo_cmp(const bfdev_rb_node_t *node1, + const bfdev_rb_node_t *node2, void *pdata) { struct bench_node *test1, *test2; diff --git a/examples/rbtree/selftest.c b/examples/rbtree/selftest.c index 4f01d7ff..2c921085 100644 --- a/examples/rbtree/selftest.c +++ b/examples/rbtree/selftest.c @@ -11,7 +11,7 @@ #define TEST_LOOP 100 struct rbtree_test_node { - struct bfdev_rb_node node; + bfdev_rb_node_t node; unsigned long data; }; @@ -26,7 +26,7 @@ struct rbtree_test_pdata { bfdev_rb_entry_safe(ptr, struct rbtree_test_node, node) static long -rbtest_rb_cmp(const struct bfdev_rb_node *rba, const struct bfdev_rb_node *rbb, void *pdata) +rbtest_rb_cmp(const bfdev_rb_node_t *rba, const bfdev_rb_node_t *rbb, void *pdata) { struct rbtree_test_node *node1, *node2; @@ -40,7 +40,7 @@ rbtest_rb_cmp(const struct bfdev_rb_node *rba, const struct bfdev_rb_node *rbb, } static long -rbtest_rb_find(const struct bfdev_rb_node *rb, void *key) +rbtest_rb_find(const bfdev_rb_node_t *rb, void *key) { struct rbtree_test_node *node; @@ -55,7 +55,7 @@ static int rbtree_testing(struct rbtree_test_pdata *sdata) { struct rbtree_test_node *node, *nnode, *tnode; - struct bfdev_rb_node *rbnode, *nrbnode, *trbnode; + bfdev_rb_node_t *rbnode, *nrbnode, *trbnode; unsigned long count; BFDEV_RB_ROOT_CACHED(test_root); diff --git a/examples/rbtree/simple.c b/examples/rbtree/simple.c index 044ef7a7..a13d2ddd 100644 --- a/examples/rbtree/simple.c +++ b/examples/rbtree/simple.c @@ -13,7 +13,7 @@ static BFDEV_RB_ROOT(simple_root); struct simple_node { - struct bfdev_rb_node node; + bfdev_rb_node_t node; unsigned long data; }; @@ -21,7 +21,7 @@ struct simple_node { bfdev_rb_entry_safe(ptr, struct simple_node, node) static long -demo_cmp(const struct bfdev_rb_node *a, const struct bfdev_rb_node *b, void *pdata) +demo_cmp(const bfdev_rb_node_t *a, const bfdev_rb_node_t *b, void *pdata) { struct simple_node *demo_a = rb_to_simple(a); struct simple_node *demo_b = rb_to_simple(b); diff --git a/include/bfdev/rbtree.h b/include/bfdev/rbtree.h index 17c881e9..bed0aac9 100644 --- a/include/bfdev/rbtree.h +++ b/include/bfdev/rbtree.h @@ -19,26 +19,30 @@ BFDEV_BEGIN_DECLS #define BFDEV_RB_BLACK (1) #define BFDEV_RB_NSET (2) +typedef struct bfdev_rb_node bfdev_rb_node_t; +typedef struct bfdev_rb_root bfdev_rb_root_t; +typedef struct bfdev_rb_root_cached bfdev_rb_root_cached_t; + struct bfdev_rb_node { - struct bfdev_rb_node *parent; - struct bfdev_rb_node *left; - struct bfdev_rb_node *right; + bfdev_rb_node_t *parent; + bfdev_rb_node_t *left; + bfdev_rb_node_t *right; bool color; }; struct bfdev_rb_root { - struct bfdev_rb_node *node; + bfdev_rb_node_t *node; }; struct bfdev_rb_root_cached { - struct bfdev_rb_root root; - struct bfdev_rb_node *leftmost; + bfdev_rb_root_t root; + bfdev_rb_node_t *leftmost; }; struct bfdev_rb_callbacks { - void (*rotate)(struct bfdev_rb_node *node, struct bfdev_rb_node *successor); - void (*copy)(struct bfdev_rb_node *node, struct bfdev_rb_node *successor); - void (*propagate)(struct bfdev_rb_node *node, struct bfdev_rb_node *stop); + void (*rotate)(bfdev_rb_node_t *node, bfdev_rb_node_t *successor); + void (*copy)(bfdev_rb_node_t *node, bfdev_rb_node_t *successor); + void (*propagate)(bfdev_rb_node_t *node, bfdev_rb_node_t *stop); }; #define BFDEV_RB_STATIC \ @@ -48,16 +52,16 @@ struct bfdev_rb_callbacks { {{NULL}, NULL} #define BFDEV_RB_INIT \ - (struct bfdev_rb_root) BFDEV_RB_STATIC + (bfdev_rb_root_t) BFDEV_RB_STATIC #define BFDEV_RB_CACHED_INIT \ - (struct bfdev_rb_root_cached) BFDEV_RB_CACHED_STATIC + (bfdev_rb_root_cached_t) BFDEV_RB_CACHED_STATIC #define BFDEV_RB_ROOT(name) \ - struct bfdev_rb_root name = BFDEV_RB_INIT + bfdev_rb_root_t name = BFDEV_RB_INIT #define BFDEV_RB_ROOT_CACHED(name) \ - struct bfdev_rb_root_cached name = BFDEV_RB_CACHED_INIT + bfdev_rb_root_cached_t name = BFDEV_RB_CACHED_INIT #define BFDEV_RB_EMPTY_ROOT(root) \ ((root)->node == NULL) @@ -73,7 +77,7 @@ struct bfdev_rb_callbacks { /** * bfdev_rb_entry - get the struct for this entry. - * @ptr: the &struct bfdev_rb_node pointer. + * @ptr: the &bfdev_rb_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_rb_node within the struct. */ @@ -82,7 +86,7 @@ struct bfdev_rb_callbacks { /** * bfdev_rb_entry_safe - get the struct for this entry or null. - * @ptr: the &struct bfdev_rb_node pointer. + * @ptr: the &bfdev_rb_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_rb_node within the struct. */ @@ -91,25 +95,25 @@ struct bfdev_rb_callbacks { #ifdef BFDEV_DEBUG_RBTREE extern bool -bfdev_rb_check_link(struct bfdev_rb_node *parent, struct bfdev_rb_node **link, - struct bfdev_rb_node *node); +bfdev_rb_check_link(bfdev_rb_node_t *parent, bfdev_rb_node_t **link, + bfdev_rb_node_t *node); extern bool -bfdev_rb_check_delete(struct bfdev_rb_node *node); +bfdev_rb_check_delete(bfdev_rb_node_t *node); #endif BFDEV_CALLBACK_FIND( bfdev_rb_find_t, - const struct bfdev_rb_node * + const bfdev_rb_node_t * ); BFDEV_CALLBACK_CMP( bfdev_rb_cmp_t, - const struct bfdev_rb_node * + const bfdev_rb_node_t * ); static inline void -bfdev_rb_init(struct bfdev_rb_root *root) +bfdev_rb_init(bfdev_rb_root_t *root) { *root = BFDEV_RB_INIT; } @@ -121,7 +125,7 @@ bfdev_rb_init(struct bfdev_rb_root *root) * @callbacks: augmented callback function. */ extern void -bfdev_rb_fixup_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_fixup_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks); /** @@ -131,7 +135,7 @@ bfdev_rb_fixup_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, * @callbacks: augmented callback function. */ extern void -bfdev_rb_erase_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, +bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, const struct bfdev_rb_callbacks *callbacks); /** @@ -140,8 +144,8 @@ bfdev_rb_erase_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *paren * @node: node to remove. * @callbacks: augmented callback function. */ -extern struct bfdev_rb_node * -bfdev_rb_remove_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +extern bfdev_rb_node_t * +bfdev_rb_remove_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks); /** @@ -150,7 +154,7 @@ bfdev_rb_remove_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node * @node: new inserted node. */ extern void -bfdev_rb_fixup(struct bfdev_rb_root *root, struct bfdev_rb_node *node); +bfdev_rb_fixup(bfdev_rb_root_t *root, bfdev_rb_node_t *node); /** * bfdev_rb_erase() - balance after remove node. @@ -158,15 +162,15 @@ bfdev_rb_fixup(struct bfdev_rb_root *root, struct bfdev_rb_node *node); * @parent: parent of removed node. */ extern void -bfdev_rb_erase(struct bfdev_rb_root *root, struct bfdev_rb_node *parent); +bfdev_rb_erase(bfdev_rb_root_t *root, bfdev_rb_node_t *parent); /** * bfdev_rb_remove() - remove node form rbtree. * @root: rbtree root of node. * @node: node to remove. */ -extern struct bfdev_rb_node * -bfdev_rb_remove(struct bfdev_rb_root *root, struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_remove(bfdev_rb_root_t *root, bfdev_rb_node_t *node); /** * bfdev_rb_replace() - replace old node by new one. @@ -175,8 +179,8 @@ bfdev_rb_remove(struct bfdev_rb_root *root, struct bfdev_rb_node *node); * @newn: new node to insert. */ extern void -bfdev_rb_replace(struct bfdev_rb_root *root, struct bfdev_rb_node *oldn, - struct bfdev_rb_node *newn); +bfdev_rb_replace(bfdev_rb_root_t *root, bfdev_rb_node_t *oldn, + bfdev_rb_node_t *newn); /** * bfdev_rb_find() - find @key in tree @root. @@ -184,8 +188,8 @@ bfdev_rb_replace(struct bfdev_rb_root *root, struct bfdev_rb_node *oldn, * @key: key to match. * @cmp: operator defining the node order. */ -extern struct bfdev_rb_node * -bfdev_rb_find(const struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp); +extern bfdev_rb_node_t * +bfdev_rb_find(const bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp); /** * bfdev_rb_find_last() - find @key in tree @root and return parent. @@ -195,9 +199,9 @@ bfdev_rb_find(const struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp); * @parentp: pointer used to modify the parent node pointer. * @linkp: pointer used to modify the point to pointer to child node. */ -extern struct bfdev_rb_node * -bfdev_rb_find_last(struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp, - struct bfdev_rb_node **parentp, struct bfdev_rb_node ***linkp); +extern bfdev_rb_node_t * +bfdev_rb_find_last(bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp, + bfdev_rb_node_t **parentp, bfdev_rb_node_t ***linkp); /** * bfdev_rb_parent() - find the parent node. @@ -207,9 +211,9 @@ bfdev_rb_find_last(struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp, * @cmp: operator defining the node order. * @leftmost: return whether it is the leftmost node. */ -extern struct bfdev_rb_node ** -bfdev_rb_parent(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, - struct bfdev_rb_node *node, bfdev_rb_cmp_t cmp, void *pdata, +extern bfdev_rb_node_t ** +bfdev_rb_parent(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, + bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, bool *leftmost); /** @@ -220,10 +224,9 @@ bfdev_rb_parent(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, * @cmp: operator defining the node order. * @leftmost: return whether it is the leftmost node. */ -extern struct bfdev_rb_node ** -bfdev_rb_parent_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, - struct bfdev_rb_node *node, bfdev_rb_cmp_t cmp, void *pdata, - bool *leftmost); +extern bfdev_rb_node_t ** +bfdev_rb_parent_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, bfdev_rb_node_t *node, + bfdev_rb_cmp_t cmp, void *pdata, bool *leftmost); #define bfdev_rb_cached_erase_augmented(cached, parent, callbacks) \ bfdev_rb_erase_augmented(&(cached)->root, parent, callbacks) @@ -250,44 +253,44 @@ bfdev_rb_parent_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node **pare bfdev_rb_parent_conflict(&(cached)->root, parentp, node, cmp, pdata, leftmost) /* Base iteration - basic iteration helper */ -extern struct bfdev_rb_node * -bfdev_rb_left_far(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_left_far(const bfdev_rb_node_t *node); -extern struct bfdev_rb_node * -bfdev_rb_right_far(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_right_far(const bfdev_rb_node_t *node); -extern struct bfdev_rb_node * -bfdev_rb_left_deep(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_left_deep(const bfdev_rb_node_t *node); -extern struct bfdev_rb_node * -bfdev_rb_right_deep(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_right_deep(const bfdev_rb_node_t *node); /* Inorder iteration (Sequential) - find logical next and previous nodes */ -extern struct bfdev_rb_node * -bfdev_rb_first(const struct bfdev_rb_root *root); +extern bfdev_rb_node_t * +bfdev_rb_first(const bfdev_rb_root_t *root); -extern struct bfdev_rb_node * -bfdev_rb_last(const struct bfdev_rb_root *root); +extern bfdev_rb_node_t * +bfdev_rb_last(const bfdev_rb_root_t *root); -extern struct bfdev_rb_node * -bfdev_rb_prev(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_prev(const bfdev_rb_node_t *node); -extern struct bfdev_rb_node * -bfdev_rb_next(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_next(const bfdev_rb_node_t *node); /* Preorder iteration (Root-first) - always access the left node first */ -extern struct bfdev_rb_node * -bfdev_rb_pre_first(const struct bfdev_rb_root *root); +extern bfdev_rb_node_t * +bfdev_rb_pre_first(const bfdev_rb_root_t *root); -extern struct bfdev_rb_node * -bfdev_rb_pre_next(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_pre_next(const bfdev_rb_node_t *node); /* Postorder iteration (Depth-first) - always visit the parent after its children */ -extern struct bfdev_rb_node * -bfdev_rb_post_first(const struct bfdev_rb_root *root); +extern bfdev_rb_node_t * +bfdev_rb_post_first(const bfdev_rb_root_t *root); -extern struct bfdev_rb_node * -bfdev_rb_post_next(const struct bfdev_rb_node *node); +extern bfdev_rb_node_t * +bfdev_rb_post_next(const bfdev_rb_node_t *node); /** * bfdev_rb_first_entry - get the first element from a rbtree. @@ -325,7 +328,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_for_each - iterate over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @root: the root for your rbtree. */ #define bfdev_rb_for_each(pos, root) \ @@ -333,7 +336,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_for_each_reverse - iterate over a rbtree backwards. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @root: the root for your rbtree. */ #define bfdev_rb_for_each_reverse(pos, root) \ @@ -341,28 +344,28 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_for_each_from - iterate over a rbtree from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_for_each_from(pos) \ for (; pos; pos = bfdev_rb_next(pos)) /** * bfdev_rb_for_each_reverse_from - iterate over a rbtree backwards from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_for_each_reverse_from(pos) \ for (; pos; pos = bfdev_rb_prev(pos)) /** * bfdev_rb_for_each_continue - continue iteration over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_for_each_continue(pos) \ for (pos = bfdev_rb_next(pos); pos; pos = bfdev_rb_next(pos)) /** * bfdev_rb_for_each_reverse_continue - continue iteration over a rbtree backwards. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_for_each_reverse_continue(pos) \ for (pos = bfdev_rb_prev(pos); pos; pos = bfdev_rb_prev(pos)) @@ -440,7 +443,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_pre_for_each - preorder iterate over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @root: the root for your rbtree. */ #define bfdev_rb_pre_for_each(pos, root) \ @@ -448,14 +451,14 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_pre_for_each_from - preorder iterate over a rbtree from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_pre_for_each_from(pos) \ for (; pos; pos = bfdev_rb_pre_next(pos)) /** * bfdev_rb_pre_for_each_continue - continue preorder iteration over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_pre_for_each_continue(pos) \ for (pos = bfdev_rb_pre_next(pos); pos; pos = bfdev_rb_pre_next(pos)) @@ -506,7 +509,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_post_for_each - postorder iterate over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @root: the root for your rbtree. */ #define bfdev_rb_post_for_each(pos, root) \ @@ -514,21 +517,21 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_post_for_each_from - postorder iterate over a rbtree from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_post_for_each_from(pos) \ for (; pos; pos = bfdev_rb_post_next(pos)) /** * bfdev_rb_post_for_each_continue - continue postorder iteration over a rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. */ #define bfdev_rb_post_for_each_continue(pos) \ for (pos = bfdev_rb_post_next(pos); pos; pos = bfdev_rb_post_next(pos)) /** * bfdev_rb_post_for_each_safe - postorder iterate over a rbtree safe against removal of rbtree entry. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @tmp: another bfdev_rb_node to use as temporary storage. * @root: the root for your rbtree. */ @@ -538,7 +541,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_post_for_each_safe_from - postorder iterate over a rbtree safe against removal of rbtree entry from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @tmp: another bfdev_rb_node to use as temporary storage. */ #define bfdev_rb_post_for_each_safe_from(pos, tmp) \ @@ -546,7 +549,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); /** * bfdev_rb_post_for_each_safe_continue - continue rbtree postorder iteration safe against removal. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @tmp: another bfdev_rb_node to use as temporary storage. */ #define bfdev_rb_post_for_each_safe_continue(pos, tmp) \ @@ -620,7 +623,7 @@ bfdev_rb_post_next(const struct bfdev_rb_node *node); * @node: new node to link. */ static inline void -bfdev_rb_link(struct bfdev_rb_node *parent, struct bfdev_rb_node **link, struct bfdev_rb_node *node) +bfdev_rb_link(bfdev_rb_node_t *parent, bfdev_rb_node_t **link, bfdev_rb_node_t *node) { #ifdef BFDEV_DEBUG_RBTREE if (bfdev_unlikely(!bfdev_rb_check_link(parent, link, node))) @@ -642,8 +645,8 @@ bfdev_rb_link(struct bfdev_rb_node *parent, struct bfdev_rb_node **link, struct * @node: new node to link. */ static inline void -bfdev_rb_insert_node(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, - struct bfdev_rb_node **link, struct bfdev_rb_node *node) +bfdev_rb_insert_node(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, + bfdev_rb_node_t **link, bfdev_rb_node_t *node) { bfdev_rb_link(parent, link, node); bfdev_rb_fixup(root, node); @@ -656,10 +659,10 @@ bfdev_rb_insert_node(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, * @cmp: operator defining the node order. */ static inline void -bfdev_rb_insert(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_insert(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; link = bfdev_rb_parent(root, &parent, node, cmp, pdata, NULL); bfdev_rb_insert_node(root, parent, link, node); @@ -672,10 +675,10 @@ bfdev_rb_insert(struct bfdev_rb_root *root, struct bfdev_rb_node *node, * @cmp: operator defining the node order. */ static inline bool -bfdev_rb_insert_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_insert_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; link = bfdev_rb_parent_conflict(root, &parent, node, cmp, pdata, NULL); if (!link) @@ -691,9 +694,9 @@ bfdev_rb_insert_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node *node, * @node: node to delete. */ static inline void -bfdev_rb_delete(struct bfdev_rb_root *root, struct bfdev_rb_node *node) +bfdev_rb_delete(bfdev_rb_root_t *root, bfdev_rb_node_t *node) { - struct bfdev_rb_node *rebalance; + bfdev_rb_node_t *rebalance; #ifdef BFDEV_DEBUG_RBTREE if (bfdev_unlikely(!bfdev_rb_check_delete(node))) @@ -717,8 +720,8 @@ bfdev_rb_delete(struct bfdev_rb_root *root, struct bfdev_rb_node *node) * @callbacks: augmented callback function. */ static inline void -bfdev_rb_insert_node_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, - struct bfdev_rb_node **link, struct bfdev_rb_node *node, +bfdev_rb_insert_node_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, + bfdev_rb_node_t **link, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { bfdev_rb_link(parent, link, node); @@ -733,11 +736,11 @@ bfdev_rb_insert_node_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node * @callbacks: augmented callback function. */ static inline void -bfdev_rb_insert_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_insert_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; link = bfdev_rb_parent(root, &parent, node, cmp, pdata, NULL); bfdev_rb_insert_node_augmented(root, parent, link, node, callbacks); @@ -751,11 +754,11 @@ bfdev_rb_insert_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node * @callbacks: augmented callback function. */ static inline bool -bfdev_rb_insert_conflict_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_insert_conflict_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; link = bfdev_rb_parent_conflict(root, &parent, node, cmp, pdata, NULL); if (!link) @@ -772,10 +775,10 @@ bfdev_rb_insert_conflict_augmented(struct bfdev_rb_root *root, struct bfdev_rb_n * @callbacks: augmented callback function. */ static inline void -bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_delete_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *rebalance; + bfdev_rb_node_t *rebalance; #ifdef BFDEV_DEBUG_RBTREE if (bfdev_unlikely(!bfdev_rb_check_delete(node))) @@ -808,7 +811,7 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node /** * bfdev_rb_cached_for_each - iterate over a cached rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @cached: the cached root for your rbtree. */ #define bfdev_rb_cached_for_each(pos, cached) \ @@ -816,7 +819,7 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node /** * bfdev_rb_cached_for_each_reverse - iterate over a cached rbtree backwards. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @cached: the cached root for your rbtree. */ #define bfdev_rb_cached_for_each_reverse(pos, cached) \ @@ -843,7 +846,7 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node /** * bfdev_rb_cached_pre_for_each - preorder iterate over a cached rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @cached: the cached root for your rbtree. */ #define bfdev_rb_cached_pre_for_each(pos, cached) \ @@ -860,7 +863,7 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node /** * bfdev_rb_cached_post_for_each - postorder iterate over a cached rbtree. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @cached: the cached root for your rbtree. */ #define bfdev_rb_cached_post_for_each(pos, cached) \ @@ -868,7 +871,7 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node /** * bfdev_rb_cached_post_for_each_safe - postorder iterate over a cached rbtree safe against removal of rbtree entry. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @tmp: another bfdev_rb_node to use as temporary storage. * @cached: the cached root for your rbtree. */ @@ -901,8 +904,8 @@ bfdev_rb_delete_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node * @leftmost: is it the leftmost node. */ static inline void -bfdev_rb_cached_fixup(struct bfdev_rb_root_cached *cached, - struct bfdev_rb_node *node, bool leftmost) +bfdev_rb_cached_fixup(bfdev_rb_root_cached_t *cached, + bfdev_rb_node_t *node, bool leftmost) { if (leftmost) cached->leftmost = node; @@ -919,8 +922,8 @@ bfdev_rb_cached_fixup(struct bfdev_rb_root_cached *cached, * @leftmost: is it the leftmost node. */ static inline void -bfdev_rb_cached_insert_node(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *parent, - struct bfdev_rb_node **link, struct bfdev_rb_node *node, bool leftmost) +bfdev_rb_cached_insert_node(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *parent, + bfdev_rb_node_t **link, bfdev_rb_node_t *node, bool leftmost) { bfdev_rb_link(parent, link, node); bfdev_rb_cached_fixup(cached, node, leftmost); @@ -933,10 +936,10 @@ bfdev_rb_cached_insert_node(struct bfdev_rb_root_cached *cached, struct bfdev_rb * @cmp: operator defining the node order. */ static inline void -bfdev_rb_cached_insert(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +bfdev_rb_cached_insert(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; bool leftmost = true; link = bfdev_rb_cached_parent(cached, &parent, node, cmp, pdata, &leftmost); @@ -950,10 +953,10 @@ bfdev_rb_cached_insert(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node * @cmp: operator defining the node order. */ static inline bool -bfdev_rb_cached_insert_conflict(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +bfdev_rb_cached_insert_conflict(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; bool leftmost = true; link = bfdev_rb_cached_parent_conflict(cached, &parent, node, cmp, pdata, &leftmost); @@ -969,10 +972,10 @@ bfdev_rb_cached_insert_conflict(struct bfdev_rb_root_cached *cached, struct bfde * @cached: rbtree cached root of node. * @node: node to delete. */ -static inline struct bfdev_rb_node * -bfdev_rb_cached_delete(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node) +static inline bfdev_rb_node_t * +bfdev_rb_cached_delete(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node) { - struct bfdev_rb_node *leftmost = NULL; + bfdev_rb_node_t *leftmost = NULL; if (cached->leftmost == node) leftmost = cached->leftmost = bfdev_rb_next(node); @@ -989,7 +992,7 @@ bfdev_rb_cached_delete(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node * @callbacks: augmented callback function. */ static inline void -bfdev_rb_cached_fixup_augmented(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +bfdev_rb_cached_fixup_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bool leftmost, const struct bfdev_rb_callbacks *callbacks) { if (leftmost) @@ -1008,8 +1011,8 @@ bfdev_rb_cached_fixup_augmented(struct bfdev_rb_root_cached *cached, struct bfde * @callbacks: augmented callback function. */ static inline void -bfdev_rb_cached_insert_node_augmented(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *parent, - struct bfdev_rb_node **link, struct bfdev_rb_node *node, +bfdev_rb_cached_insert_node_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *parent, + bfdev_rb_node_t **link, bfdev_rb_node_t *node, bool leftmost, const struct bfdev_rb_callbacks *callbacks) { bfdev_rb_link(parent, link, node); @@ -1024,11 +1027,11 @@ bfdev_rb_cached_insert_node_augmented(struct bfdev_rb_root_cached *cached, struc * @callbacks: augmented callback function. */ static inline void -bfdev_rb_cached_insert_augmented(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +bfdev_rb_cached_insert_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; bool leftmost = true; link = bfdev_rb_cached_parent(cached, &parent, node, cmp, pdata, &leftmost); @@ -1043,11 +1046,11 @@ bfdev_rb_cached_insert_augmented(struct bfdev_rb_root_cached *cached, struct bfd * @callbacks: augmented callback function. */ static inline bool -bfdev_rb_cached_insert_conflict_augmented(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +bfdev_rb_cached_insert_conflict_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent, **link; + bfdev_rb_node_t *parent, **link; bool leftmost = true; link = bfdev_rb_cached_parent_conflict(cached, &parent, node, cmp, pdata, &leftmost); @@ -1064,11 +1067,11 @@ bfdev_rb_cached_insert_conflict_augmented(struct bfdev_rb_root_cached *cached, s * @node: node to delete. * @callbacks: augmented callback function. */ -static inline struct bfdev_rb_node * -bfdev_rb_cached_delete_augmented(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *node, +static inline bfdev_rb_node_t * +bfdev_rb_cached_delete_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *leftmost = NULL; + bfdev_rb_node_t *leftmost = NULL; if (cached->leftmost == node) leftmost = cached->leftmost = bfdev_rb_next(node); @@ -1084,8 +1087,8 @@ bfdev_rb_cached_delete_augmented(struct bfdev_rb_root_cached *cached, struct bfd * @newn: new node to insert. */ static inline void -bfdev_rb_cached_replace(struct bfdev_rb_root_cached *cached, struct bfdev_rb_node *oldn, - struct bfdev_rb_node *newn) +bfdev_rb_cached_replace(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *oldn, + bfdev_rb_node_t *newn) { if (cached->leftmost == oldn) cached->leftmost = newn; @@ -1093,58 +1096,58 @@ bfdev_rb_cached_replace(struct bfdev_rb_root_cached *cached, struct bfdev_rb_nod bfdev_rb_replace(&cached->root, oldn, newn); } -#define BFDEV_RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \ -static void RBNAME##_rotate(struct bfdev_rb_node *rb_node, struct bfdev_rb_node *rb_successor) \ -{ \ - RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ - RBSTRUCT *successor = bfdev_rb_entry(rb_successor, RBSTRUCT, RBFIELD); \ - successor->RBAUGMENTED = node->RBAUGMENTED; \ - RBCOMPUTE(node, false); \ -} \ - \ -static void RBNAME##_copy(struct bfdev_rb_node *rb_node, struct bfdev_rb_node *rb_successor) \ -{ \ - RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ - RBSTRUCT *successor = bfdev_rb_entry(rb_successor, RBSTRUCT, RBFIELD); \ - successor->RBAUGMENTED = node->RBAUGMENTED; \ -} \ - \ -static void RBNAME##_propagate(struct bfdev_rb_node *rb_node, struct bfdev_rb_node *rb_stop) \ -{ \ - while (rb_node != rb_stop) { \ - RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ - if (RBCOMPUTE(node, true)) \ - break; \ - rb_node = node->RBFIELD.parent; \ - } \ -} \ - \ -RBSTATIC struct bfdev_rb_callbacks RBNAME = { \ - .rotate = RBNAME##_rotate, \ - .copy = RBNAME##_copy, \ - .propagate = RBNAME##_propagate, \ +#define BFDEV_RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \ +static void RBNAME##_rotate(bfdev_rb_node_t *rb_node, bfdev_rb_node_t *rb_successor) \ +{ \ + RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ + RBSTRUCT *successor = bfdev_rb_entry(rb_successor, RBSTRUCT, RBFIELD); \ + successor->RBAUGMENTED = node->RBAUGMENTED; \ + RBCOMPUTE(node, false); \ +} \ + \ +static void RBNAME##_copy(bfdev_rb_node_t *rb_node, bfdev_rb_node_t *rb_successor) \ +{ \ + RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ + RBSTRUCT *successor = bfdev_rb_entry(rb_successor, RBSTRUCT, RBFIELD); \ + successor->RBAUGMENTED = node->RBAUGMENTED; \ +} \ + \ +static void RBNAME##_propagate(bfdev_rb_node_t *rb_node, bfdev_rb_node_t *rb_stop) \ +{ \ + while (rb_node != rb_stop) { \ + RBSTRUCT *node = bfdev_rb_entry(rb_node, RBSTRUCT, RBFIELD); \ + if (RBCOMPUTE(node, true)) \ + break; \ + rb_node = node->RBFIELD.parent; \ + } \ +} \ + \ +RBSTATIC struct bfdev_rb_callbacks RBNAME = { \ + .rotate = RBNAME##_rotate, \ + .copy = RBNAME##_copy, \ + .propagate = RBNAME##_propagate, \ } -#define BFDEV_RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, RBTYPE, RBAUGMENTED, RBCOMPUTE) \ -static inline bool RBNAME##_compute_max(RBSTRUCT *node, bool exit) \ -{ \ - RBSTRUCT *child; \ - RBTYPE max = RBCOMPUTE(node); \ - if (node->RBFIELD.left) { \ - child = bfdev_rb_entry(node->RBFIELD.left, RBSTRUCT, RBFIELD); \ - if (child->RBAUGMENTED > max) \ - max = child->RBAUGMENTED; \ - } \ - if (node->RBFIELD.right) { \ - child = bfdev_rb_entry(node->RBFIELD.right, RBSTRUCT, RBFIELD); \ - if (child->RBAUGMENTED > max) \ - max = child->RBAUGMENTED; \ - } \ - if (exit && node->RBAUGMENTED == max) \ - return true; \ - node->RBAUGMENTED = max; \ - return false; \ -} \ +#define BFDEV_RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, RBTYPE, RBAUGMENTED, RBCOMPUTE) \ +static inline bool RBNAME##_compute_max(RBSTRUCT *node, bool exit) \ +{ \ + RBSTRUCT *child; \ + RBTYPE max = RBCOMPUTE(node); \ + if (node->RBFIELD.left) { \ + child = bfdev_rb_entry(node->RBFIELD.left, RBSTRUCT, RBFIELD); \ + if (child->RBAUGMENTED > max) \ + max = child->RBAUGMENTED; \ + } \ + if (node->RBFIELD.right) { \ + child = bfdev_rb_entry(node->RBFIELD.right, RBSTRUCT, RBFIELD); \ + if (child->RBAUGMENTED > max) \ + max = child->RBAUGMENTED; \ + } \ + if (exit && node->RBAUGMENTED == max) \ + return true; \ + node->RBAUGMENTED = max; \ + return false; \ +} \ BFDEV_RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME##_compute_max) BFDEV_END_DECLS diff --git a/include/bfdev/segtree.h b/include/bfdev/segtree.h index cfd66445..8920fb86 100644 --- a/include/bfdev/segtree.h +++ b/include/bfdev/segtree.h @@ -12,7 +12,7 @@ BFDEV_BEGIN_DECLS struct bfdev_segtree_node { - struct bfdev_rb_node node; + bfdev_rb_node_t node; unsigned long start, end; unsigned long subtree; }; @@ -35,10 +35,10 @@ struct bfdev_segtree_node { #define bfdev_segtree_entry_safe(ptr, type, member) \ bfdev_container_of_safe(ptr, type, member) -extern void bfdev_segtree_insert(struct bfdev_rb_root_cached *root, struct bfdev_segtree_node *node); -extern void bfdev_segtree_delete(struct bfdev_rb_root_cached *root, struct bfdev_segtree_node *node); +extern void bfdev_segtree_insert(bfdev_rb_root_cached_t *root, struct bfdev_segtree_node *node); +extern void bfdev_segtree_delete(bfdev_rb_root_cached_t *root, struct bfdev_segtree_node *node); extern struct bfdev_segtree_node *bfdev_segtree_search(struct bfdev_segtree_node *node, unsigned long start, unsigned long end); -extern struct bfdev_segtree_node *bfdev_segtree_first(struct bfdev_rb_root_cached *root, unsigned long start, unsigned long end); +extern struct bfdev_segtree_node *bfdev_segtree_first(bfdev_rb_root_cached_t *root, unsigned long start, unsigned long end); extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node *node, unsigned long start, unsigned long end); /** @@ -75,7 +75,7 @@ extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node * /** * bfdev_segtree_for_each_form - iterate over a segtree from the current point. - * @pos: the &struct bfdev_rb_node to use as a loop cursor. + * @pos: the &bfdev_rb_node_t to use as a loop cursor. * @start: start endpoint of segtree element. * @end: end endpoint of segtree element. */ @@ -127,9 +127,9 @@ extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node * #define BFDEV_SEGTREE_DEFINE(STSTATIC, STNAME, STSTRUCT, STRB, STTYPE, STSUBTREE, STSTART, STEND) \ BFDEV_RB_DECLARE_CALLBACKS_MAX(static, STNAME##_callbacks, STSTRUCT, STRB, STTYPE, STSUBTREE, STEND); \ -STSTATIC void STNAME##_insert(struct bfdev_rb_root_cached *cached, STSTRUCT *node) \ +STSTATIC void STNAME##_insert(bfdev_rb_root_cached_t *cached, STSTRUCT *node) \ { \ - struct bfdev_rb_node **link = &cached->root.node; \ + bfdev_rb_node_t **link = &cached->root.node; \ STSTRUCT *parent = NULL; \ STTYPE start, end; \ bool leftmost = true; \ @@ -154,7 +154,7 @@ STSTATIC void STNAME##_insert(struct bfdev_rb_root_cached *cached, STSTRUCT *nod node->STSUBTREE = end; \ } \ \ -STSTATIC void STNAME##_delete(struct bfdev_rb_root_cached *cached, STSTRUCT *node) \ +STSTATIC void STNAME##_delete(bfdev_rb_root_cached_t *cached, STSTRUCT *node) \ { \ bfdev_rb_cached_delete_augmented(cached, &node->STRB, &STNAME##_callbacks); \ } \ @@ -184,7 +184,7 @@ STSTATIC STSTRUCT *STNAME##_search(STSTRUCT *node, STTYPE start, STTYPE end) } \ } \ \ -STSTATIC STSTRUCT *STNAME##_first(struct bfdev_rb_root_cached *cached, STTYPE start, STTYPE end) \ +STSTATIC STSTRUCT *STNAME##_first(bfdev_rb_root_cached_t *cached, STTYPE start, STTYPE end) \ { \ STSTRUCT *node, *leftmost; \ \ @@ -203,7 +203,7 @@ STSTATIC STSTRUCT *STNAME##_first(struct bfdev_rb_root_cached *cached, STTYPE st \ STSTATIC STSTRUCT *STNAME##_next(STSTRUCT *node, STTYPE start, STTYPE end) \ { \ - struct bfdev_rb_node *prev, *walk = node->STRB.right; \ + bfdev_rb_node_t *prev, *walk = node->STRB.right; \ STSTRUCT *right; \ \ for (;;) { \ diff --git a/src/rbtree-debug.c b/src/rbtree-debug.c index d142748c..606371cc 100644 --- a/src/rbtree-debug.c +++ b/src/rbtree-debug.c @@ -12,8 +12,8 @@ #include export bool -bfdev_rb_check_link(struct bfdev_rb_node *parent, struct bfdev_rb_node **link, - struct bfdev_rb_node *node) +bfdev_rb_check_link(bfdev_rb_node_t *parent, bfdev_rb_node_t **link, + bfdev_rb_node_t *node) { if (bfdev_unlikely(*link == node)) { bfdev_log_err( @@ -28,7 +28,7 @@ bfdev_rb_check_link(struct bfdev_rb_node *parent, struct bfdev_rb_node **link, } export bool -bfdev_rb_check_delete(struct bfdev_rb_node *node) +bfdev_rb_check_delete(bfdev_rb_node_t *node) { if (bfdev_unlikely(node->left == BFDEV_POISON_RBNODE1)) { bfdev_log_err( diff --git a/src/rbtree.c b/src/rbtree.c index 0c45e2ee..78313410 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -16,8 +16,8 @@ * @newn: new node to insert. */ static __bfdev_always_inline void -child_change(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, - struct bfdev_rb_node *oldn, struct bfdev_rb_node *newn) +child_change(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, + bfdev_rb_node_t *oldn, bfdev_rb_node_t *newn) { if (!parent) root->node = newn; @@ -38,11 +38,11 @@ child_change(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, * @callbacks: augmented callback function. */ static __bfdev_always_inline void -rotate_set(struct bfdev_rb_root *root, struct bfdev_rb_node *node, struct bfdev_rb_node *newn, - struct bfdev_rb_node *child, unsigned int color, unsigned int ccolor, +rotate_set(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_node_t *newn, + bfdev_rb_node_t *child, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent = node->parent; + bfdev_rb_node_t *parent = node->parent; newn->parent = node->parent; node->parent = newn; @@ -70,12 +70,12 @@ rotate_set(struct bfdev_rb_root *root, struct bfdev_rb_node *node, struct bfdev_ * @ccolor: color of child. * @callbacks: augmented callback function. */ -static __bfdev_always_inline struct bfdev_rb_node * -left_rotate(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +static __bfdev_always_inline bfdev_rb_node_t * +left_rotate(bfdev_rb_root_t *root, bfdev_rb_node_t *node, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *child, *successor = node->right; + bfdev_rb_node_t *child, *successor = node->right; /* change left child */ child = node->right = successor->left; @@ -93,12 +93,12 @@ left_rotate(struct bfdev_rb_root *root, struct bfdev_rb_node *node, * @ccolor: color of child. * @callbacks: augmented callback function. */ -static __bfdev_always_inline struct bfdev_rb_node * -right_rotate(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +static __bfdev_always_inline bfdev_rb_node_t * +right_rotate(bfdev_rb_root_t *root, bfdev_rb_node_t *node, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *child, *successor = node->left; + bfdev_rb_node_t *child, *successor = node->left; /* change right child */ child = node->left = successor->right; @@ -109,10 +109,10 @@ right_rotate(struct bfdev_rb_root *root, struct bfdev_rb_node *node, } export void -bfdev_rb_fixup_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +bfdev_rb_fixup_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent, *gparent, *tmp; + bfdev_rb_node_t *parent, *gparent, *tmp; while (root && node) { parent = node->parent; @@ -219,10 +219,10 @@ bfdev_rb_fixup_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, } export void -bfdev_rb_erase_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *parent, +bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *tmp1, *tmp2, *sibling, *node = NULL; + bfdev_rb_node_t *tmp1, *tmp2, *sibling, *node = NULL; while (root && parent) { /* @@ -367,13 +367,13 @@ bfdev_rb_erase_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *paren } } -export struct bfdev_rb_node * -bfdev_rb_remove_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node, +export bfdev_rb_node_t * +bfdev_rb_remove_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - struct bfdev_rb_node *parent = node->parent, *rebalance = NULL; - struct bfdev_rb_node *child1 = node->left; - struct bfdev_rb_node *child2 = node->right; + bfdev_rb_node_t *parent = node->parent, *rebalance = NULL; + bfdev_rb_node_t *child1 = node->left; + bfdev_rb_node_t *child2 = node->right; if (!child1 && !child2) { /* @@ -418,7 +418,7 @@ bfdev_rb_remove_augmented(struct bfdev_rb_root *root, struct bfdev_rb_node *node child2->parent = node->parent; child_change(root, parent, node, child2); } else { /* child1 && child2 */ - struct bfdev_rb_node *tmp, *successor = child2; + bfdev_rb_node_t *tmp, *successor = child2; child1 = child2->left; if (!child1) { @@ -495,28 +495,28 @@ static const struct bfdev_rb_callbacks dummy_callbacks = { }; export void -bfdev_rb_fixup(struct bfdev_rb_root *root, struct bfdev_rb_node *node) +bfdev_rb_fixup(bfdev_rb_root_t *root, bfdev_rb_node_t *node) { bfdev_rb_fixup_augmented(root, node, &dummy_callbacks); } export void -bfdev_rb_erase(struct bfdev_rb_root *root, struct bfdev_rb_node *parent) +bfdev_rb_erase(bfdev_rb_root_t *root, bfdev_rb_node_t *parent) { bfdev_rb_erase_augmented(root, parent, &dummy_callbacks); } -export struct bfdev_rb_node * -bfdev_rb_remove(struct bfdev_rb_root *root, struct bfdev_rb_node *node) +export bfdev_rb_node_t * +bfdev_rb_remove(bfdev_rb_root_t *root, bfdev_rb_node_t *node) { return bfdev_rb_remove_augmented(root, node, &dummy_callbacks); } export void -bfdev_rb_replace(struct bfdev_rb_root *root, struct bfdev_rb_node *oldn, - struct bfdev_rb_node *newn) +bfdev_rb_replace(bfdev_rb_root_t *root, bfdev_rb_node_t *oldn, + bfdev_rb_node_t *newn) { - struct bfdev_rb_node *parent = oldn->parent; + bfdev_rb_node_t *parent = oldn->parent; *newn = *oldn; @@ -528,11 +528,11 @@ bfdev_rb_replace(struct bfdev_rb_root *root, struct bfdev_rb_node *oldn, child_change(root, parent, oldn, newn); } -export struct bfdev_rb_node * -bfdev_rb_find(const struct bfdev_rb_root *root, void *key, +export bfdev_rb_node_t * +bfdev_rb_find(const bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp) { - struct bfdev_rb_node *node = root->node; + bfdev_rb_node_t *node = root->node; long retval; while (node) { @@ -550,9 +550,9 @@ bfdev_rb_find(const struct bfdev_rb_root *root, void *key, return NULL; } -export struct bfdev_rb_node * -bfdev_rb_find_last(struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp, - struct bfdev_rb_node **parentp, struct bfdev_rb_node ***linkp) +export bfdev_rb_node_t * +bfdev_rb_find_last(bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp, + bfdev_rb_node_t **parentp, bfdev_rb_node_t ***linkp) { long retval; @@ -577,12 +577,12 @@ bfdev_rb_find_last(struct bfdev_rb_root *root, void *key, bfdev_rb_find_t cmp, return NULL; } -export struct bfdev_rb_node ** -bfdev_rb_parent(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, - struct bfdev_rb_node *node, bfdev_rb_cmp_t cmp, void *pdata, +export bfdev_rb_node_t ** +bfdev_rb_parent(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, + bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, bool *leftmost) { - struct bfdev_rb_node **link; + bfdev_rb_node_t **link; bool leftmost_none; long retval; @@ -608,12 +608,12 @@ bfdev_rb_parent(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, return link; } -export struct bfdev_rb_node ** -bfdev_rb_parent_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node **parentp, - struct bfdev_rb_node *node, bfdev_rb_cmp_t cmp, void *pdata, +export bfdev_rb_node_t ** +bfdev_rb_parent_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, + bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, bool *leftmost) { - struct bfdev_rb_node **link; + bfdev_rb_node_t **link; bool leftmost_none; long retval; @@ -642,23 +642,23 @@ bfdev_rb_parent_conflict(struct bfdev_rb_root *root, struct bfdev_rb_node **pare BFDEV_TITER_BASE_DEFINE( export, bfdev_rb, - struct bfdev_rb_node, left, right + bfdev_rb_node_t, left, right ) BFDEV_TITER_INORDER_DEFINE( export, bfdev_rb, bfdev_rb, - struct bfdev_rb_root, node, - struct bfdev_rb_node, parent, left, right + bfdev_rb_root_t, node, + bfdev_rb_node_t, parent, left, right ) BFDEV_TITER_PREORDER_DEFINE( export, bfdev_rb_pre, bfdev_rb, - struct bfdev_rb_root, node, - struct bfdev_rb_node, parent, left, right + bfdev_rb_root_t, node, + bfdev_rb_node_t, parent, left, right ) BFDEV_TITER_POSTORDER_DEFINE( export, bfdev_rb_post, bfdev_rb, - struct bfdev_rb_root, node, - struct bfdev_rb_node, parent, left, right + bfdev_rb_root_t, node, + bfdev_rb_node_t, parent, left, right ) From e90ddc24c26c7a4a1f09fc9b26187d344c4cf888 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 13:00:28 +0800 Subject: [PATCH 040/119] feat radix: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/radix.h | 33 ++++++++++++++++++--------------- src/radix.c | 40 ++++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/include/bfdev/radix.h b/include/bfdev/radix.h index fca291c8..a855f9e7 100644 --- a/include/bfdev/radix.h +++ b/include/bfdev/radix.h @@ -17,21 +17,24 @@ BFDEV_BEGIN_DECLS #define BFDEV_RADIX_SHIFT 8 #define BFDEV_RADIX_BLOCK BFDEV_BIT(BFDEV_RADIX_SHIFT) -#define BFDEV_RADIX_ARY (BFDEV_RADIX_BLOCK / sizeof(struct bfdev_radix_node *)) +#define BFDEV_RADIX_ARY (BFDEV_RADIX_BLOCK / sizeof(bfdev_radix_node_t *)) #define BFDEV_RADIX_ARY_MASK (BFDEV_RADIX_ARY - 1) #define BFDEV_RADIX_ARY_SHIFT bfdev_ilog2(BFDEV_RADIX_ARY) +typedef struct bfdev_radix_root bfdev_radix_root_t; +typedef struct bfdev_radix_node bfdev_radix_node_t; + struct bfdev_radix_root { const struct bfdev_alloc *alloc; - struct bfdev_radix_node *node; + bfdev_radix_node_t *node; unsigned int level; }; struct bfdev_radix_node { - struct bfdev_radix_node *parent; + bfdev_radix_node_t *parent; union { struct { - struct bfdev_radix_node *child[BFDEV_RADIX_ARY]; + bfdev_radix_node_t *child[BFDEV_RADIX_ARY]; unsigned int refcount; }; struct { @@ -46,7 +49,7 @@ struct bfdev_radix_node { #define BFDEV_GENERIC_RADIX(datatype) \ union { \ - struct bfdev_radix_root tree; \ + bfdev_radix_root_t tree; \ datatype data; \ char check[BFDEV_RADIX_CHECK(datatype)]; \ } @@ -84,7 +87,7 @@ bfdev_radix_offset(uintptr_t index, size_t cells) bfdev_radix_offset(index, bfdev_radix_cells(radix)) #define bfdev_radix_find(radix, index) ({ \ - struct bfdev_radix_root *__root; \ + bfdev_radix_root_t *__root; \ __root = &(radix)->tree; \ bfdev_radix_cast(radix) ( \ bfdev_radix_root_find(__root, \ @@ -94,7 +97,7 @@ bfdev_radix_offset(uintptr_t index, size_t cells) }) #define bfdev_radix_alloc(radix, index) ({ \ - struct bfdev_radix_root *__root; \ + bfdev_radix_root_t *__root; \ __root = &(radix)->tree; \ bfdev_radix_cast(radix) ( \ bfdev_radix_root_alloc(__root, \ @@ -104,7 +107,7 @@ bfdev_radix_offset(uintptr_t index, size_t cells) }) #define bfdev_radix_free(radix, index) ({ \ - struct bfdev_radix_root *__root; \ + bfdev_radix_root_t *__root; \ __root = &(radix)->tree; \ bfdev_radix_root_free(__root, \ bfdev_radix_to_offset(radix, index) \ @@ -112,7 +115,7 @@ bfdev_radix_offset(uintptr_t index, size_t cells) }) #define bfdev_radix_charge(radix, index, size) ({ \ - struct bfdev_radix_root *__root; \ + bfdev_radix_root_t *__root; \ __root = &(radix)->tree; \ bfdev_radix_root_charge(__root, \ bfdev_radix_to_offset(radix, index), \ @@ -121,25 +124,25 @@ bfdev_radix_offset(uintptr_t index, size_t cells) }) #define bfdev_radix_destory(radix) ({ \ - struct bfdev_radix_root *__root; \ + bfdev_radix_root_t *__root; \ __root = &(radix)->tree; \ bfdev_radix_root_destory(__root); \ }) extern void * -bfdev_radix_root_find(struct bfdev_radix_root *root, uintptr_t offset); +bfdev_radix_root_find(bfdev_radix_root_t *root, uintptr_t offset); extern void * -bfdev_radix_root_alloc(struct bfdev_radix_root *root, uintptr_t offset); +bfdev_radix_root_alloc(bfdev_radix_root_t *root, uintptr_t offset); extern int -bfdev_radix_root_free(struct bfdev_radix_root *root, uintptr_t offset); +bfdev_radix_root_free(bfdev_radix_root_t *root, uintptr_t offset); extern int -bfdev_radix_root_charge(struct bfdev_radix_root *root, uintptr_t offset, size_t size); +bfdev_radix_root_charge(bfdev_radix_root_t *root, uintptr_t offset, size_t size); extern void -bfdev_radix_root_destory(struct bfdev_radix_root *root); +bfdev_radix_root_destory(bfdev_radix_root_t *root); BFDEV_END_DECLS diff --git a/src/radix.c b/src/radix.c index ba5dbbb1..cc292dae 100644 --- a/src/radix.c +++ b/src/radix.c @@ -20,11 +20,11 @@ radix_depth_index(unsigned int level, uintptr_t offset) return offset >> radix_depth_shift(level) & BFDEV_RADIX_ARY_MASK; } -static struct bfdev_radix_node * -radix_parent(struct bfdev_radix_root *root, uintptr_t offset, uintptr_t *index) +static bfdev_radix_node_t * +radix_parent(bfdev_radix_root_t *root, uintptr_t offset, uintptr_t *index) { unsigned int level = root->level; - struct bfdev_radix_node *node; + bfdev_radix_node_t *node; if (bfdev_ilog2(offset) > radix_depth_shift(level)) return NULL; @@ -39,9 +39,9 @@ radix_parent(struct bfdev_radix_root *root, uintptr_t offset, uintptr_t *index) } export void * -bfdev_radix_root_find(struct bfdev_radix_root *root, uintptr_t offset) +bfdev_radix_root_find(bfdev_radix_root_t *root, uintptr_t offset) { - struct bfdev_radix_node *node; + bfdev_radix_node_t *node; uintptr_t index; node = radix_parent(root, offset, &index); @@ -51,11 +51,11 @@ bfdev_radix_root_find(struct bfdev_radix_root *root, uintptr_t offset) return &node->block[index]; } -static inline struct bfdev_radix_node * -radix_extend(struct bfdev_radix_root *root, uintptr_t offset) +static inline bfdev_radix_node_t * +radix_extend(bfdev_radix_root_t *root, uintptr_t offset) { const struct bfdev_alloc *alloc = root->alloc; - struct bfdev_radix_node *node, *successor; + bfdev_radix_node_t *node, *successor; unsigned int level; for (;;) { @@ -83,10 +83,10 @@ radix_extend(struct bfdev_radix_root *root, uintptr_t offset) } static inline void -radix_shrink(struct bfdev_radix_root *root) +radix_shrink(bfdev_radix_root_t *root) { const struct bfdev_alloc *alloc = root->alloc; - struct bfdev_radix_node *node, *successor; + bfdev_radix_node_t *node, *successor; while (root->level) { node = root->node; @@ -104,10 +104,10 @@ radix_shrink(struct bfdev_radix_root *root) } export void * -bfdev_radix_root_alloc(struct bfdev_radix_root *root, uintptr_t offset) +bfdev_radix_root_alloc(bfdev_radix_root_t *root, uintptr_t offset) { const struct bfdev_alloc *alloc = root->alloc; - struct bfdev_radix_node *node; + bfdev_radix_node_t *node; unsigned int level; node = radix_extend(root, offset); @@ -115,7 +115,7 @@ bfdev_radix_root_alloc(struct bfdev_radix_root *root, uintptr_t offset) return NULL; for (level = root->level; level--;) { - struct bfdev_radix_node **slot, *newn; + bfdev_radix_node_t **slot, *newn; slot = &node->child[radix_depth_index(level, offset)]; offset &= BFDEV_BIT_LOW_MASK(radix_depth_shift(level)); @@ -138,10 +138,10 @@ bfdev_radix_root_alloc(struct bfdev_radix_root *root, uintptr_t offset) } export int -bfdev_radix_root_free(struct bfdev_radix_root *root, uintptr_t offset) +bfdev_radix_root_free(bfdev_radix_root_t *root, uintptr_t offset) { const struct bfdev_alloc *alloc = root->alloc; - struct bfdev_radix_node *node; + bfdev_radix_node_t *node; unsigned int level; uintptr_t index; @@ -154,7 +154,7 @@ bfdev_radix_root_free(struct bfdev_radix_root *root, uintptr_t offset) return -BFDEV_ENOERR; for (level = 0; level <= root->level; ++level) { - struct bfdev_radix_node *parent = node->parent; + bfdev_radix_node_t *parent = node->parent; if (level && --node->refcount) break; @@ -175,7 +175,7 @@ bfdev_radix_root_free(struct bfdev_radix_root *root, uintptr_t offset) } export int -bfdev_radix_root_charge(struct bfdev_radix_root *root, +bfdev_radix_root_charge(bfdev_radix_root_t *root, uintptr_t offset, size_t size) { uintptr_t end; @@ -197,9 +197,9 @@ bfdev_radix_root_charge(struct bfdev_radix_root *root, static void radix_destory_recurse(const struct bfdev_alloc *alloc, - struct bfdev_radix_node *node, unsigned int level) + bfdev_radix_node_t *node, unsigned int level) { - struct bfdev_radix_node *child; + bfdev_radix_node_t *child; unsigned int index; if (level) { @@ -213,7 +213,7 @@ radix_destory_recurse(const struct bfdev_alloc *alloc, } export void -bfdev_radix_root_destory(struct bfdev_radix_root *root) +bfdev_radix_root_destory(bfdev_radix_root_t *root) { const struct bfdev_alloc *alloc = root->alloc; From 1d3eb6037e6e3445b9bc03aa0f183fe230fb48a7 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 13:02:07 +0800 Subject: [PATCH 041/119] feat slist: added initial typedef Signed-off-by: John Sanpe --- examples/slist/selftest.c | 4 ++-- examples/slist/simple.c | 2 +- include/bfdev/llist.h | 14 +++++++------- include/bfdev/slist.h | 38 ++++++++++++++++++++------------------ src/llist.c | 12 ++++++------ src/slist-debug.c | 4 ++-- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/examples/slist/selftest.c b/examples/slist/selftest.c index f32555fe..218a8ea9 100644 --- a/examples/slist/selftest.c +++ b/examples/slist/selftest.c @@ -11,7 +11,7 @@ #define TEST_LOOP 10 struct test_node { - struct bfdev_slist_head list; + bfdev_slist_head_t list; unsigned long num; }; @@ -25,7 +25,7 @@ struct test_pdata { static int bfdev_slist_selftest(struct test_pdata *sdata) { struct test_node *node, *nnode, *tnode; - struct bfdev_slist_head *list, *nlist, *tlist; + bfdev_slist_head_t *list, *nlist, *tlist; unsigned int count; BFDEV_SLIST_HEAD(test_head); diff --git a/examples/slist/simple.c b/examples/slist/simple.c index f3549fb7..1ba3bf16 100644 --- a/examples/slist/simple.c +++ b/examples/slist/simple.c @@ -13,7 +13,7 @@ static BFDEV_SLIST_HEAD(demo_list); struct list_simple { - struct bfdev_slist_head list; + bfdev_slist_head_t list; unsigned int num; unsigned long data; }; diff --git a/include/bfdev/llist.h b/include/bfdev/llist.h index 28963e9e..02d1a1e6 100644 --- a/include/bfdev/llist.h +++ b/include/bfdev/llist.h @@ -22,15 +22,15 @@ BFDEV_BEGIN_DECLS * Return whether list is empty before adding. */ extern bool -bfdev_llist_split(struct bfdev_slist_head *head, struct bfdev_slist_head *node, - struct bfdev_slist_head *end); +bfdev_llist_split(bfdev_slist_head_t *head, bfdev_slist_head_t *node, + bfdev_slist_head_t *end); /** * bfdev_llist_del() - delete the first entry of lock-less list. * @head: the head for your lock-less list. */ -extern struct bfdev_slist_head * -bfdev_llist_del(struct bfdev_slist_head *head); +extern bfdev_slist_head_t * +bfdev_llist_del(bfdev_slist_head_t *head); /** * bfdev_llist_add() - add a new entry. @@ -40,7 +40,7 @@ bfdev_llist_del(struct bfdev_slist_head *head); * Return whether list is empty before adding. */ static inline bool -bfdev_llist_add(struct bfdev_slist_head *head, struct bfdev_slist_head *node) +bfdev_llist_add(bfdev_slist_head_t *head, bfdev_slist_head_t *node) { return bfdev_llist_split(head, node, node); } @@ -49,8 +49,8 @@ bfdev_llist_add(struct bfdev_slist_head *head, struct bfdev_slist_head *node) * bfdev_llist_destroy() - delete all entries from lock-less list. * @head: the head of lock-less list to delete all entries */ -static inline struct bfdev_slist_head * -bfdev_llist_destroy(struct bfdev_slist_head *head) +static inline bfdev_slist_head_t * +bfdev_llist_destroy(bfdev_slist_head_t *head) { return (void *)bfdev_xchg((bfdev_atomic_t *)&head->next, (bfdev_atomic_t)NULL); } diff --git a/include/bfdev/slist.h b/include/bfdev/slist.h index bd72d2d0..b0664a1b 100644 --- a/include/bfdev/slist.h +++ b/include/bfdev/slist.h @@ -14,22 +14,24 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_slist_head bfdev_slist_head_t; + struct bfdev_slist_head { - struct bfdev_slist_head *next; + bfdev_slist_head_t *next; }; #define BFDEV_SLIST_HEAD_STATIC \ {NULL} #define BFDEV_SLIST_HEAD_INIT \ - (struct bfdev_slist_head) BFDEV_SLIST_HEAD_STATIC + (bfdev_slist_head_t) BFDEV_SLIST_HEAD_STATIC #define BFDEV_SLIST_HEAD(name) \ - struct bfdev_slist_head name = BFDEV_SLIST_HEAD_INIT + bfdev_slist_head_t name = BFDEV_SLIST_HEAD_INIT #ifdef BFDEV_DEBUG_SLIST -extern bool bfdev_slist_check_add(struct bfdev_slist_head *node, struct bfdev_slist_head *newn); -extern bool bfdev_slist_check_del(struct bfdev_slist_head *node); +extern bool bfdev_slist_check_add(bfdev_slist_head_t *node, bfdev_slist_head_t *newn); +extern bool bfdev_slist_check_del(bfdev_slist_head_t *node); #endif /** @@ -37,7 +39,7 @@ extern bool bfdev_slist_check_del(struct bfdev_slist_head *node); * @head: slist head structure to be initialized. */ static inline void -bfdev_slist_head_init(struct bfdev_slist_head *head) +bfdev_slist_head_init(bfdev_slist_head_t *head) { head->next = NULL; } @@ -48,7 +50,7 @@ bfdev_slist_head_init(struct bfdev_slist_head *head) * @newn: new entry to be added. */ static inline void -bfdev_slist_add(struct bfdev_slist_head *node, struct bfdev_slist_head *newn) +bfdev_slist_add(bfdev_slist_head_t *node, bfdev_slist_head_t *newn) { #ifdef BFDEV_DEBUG_SLIST if (bfdev_unlikely(!bfdev_slist_check_add(node, newn))) @@ -65,9 +67,9 @@ bfdev_slist_add(struct bfdev_slist_head *node, struct bfdev_slist_head *newn) * @entry: the element to delete from the slist. */ static inline void -bfdev_slist_del(struct bfdev_slist_head *head, struct bfdev_slist_head *node) +bfdev_slist_del(bfdev_slist_head_t *head, bfdev_slist_head_t *node) { - struct bfdev_slist_head *walk = head; + bfdev_slist_head_t *walk = head; #ifdef BFDEV_DEBUG_SLIST if (bfdev_unlikely(!bfdev_slist_check_del(node))) @@ -86,7 +88,7 @@ bfdev_slist_del(struct bfdev_slist_head *head, struct bfdev_slist_head *node) * @head: slist head to check. */ static inline bool -bfdev_slist_check_empty(const struct bfdev_slist_head *head) +bfdev_slist_check_empty(const bfdev_slist_head_t *head) { return !head->next; } @@ -97,8 +99,8 @@ bfdev_slist_check_empty(const struct bfdev_slist_head *head) * @node: the entry to test. */ static inline bool -bfdev_slist_check_first(const struct bfdev_slist_head *head, - const struct bfdev_slist_head *node) +bfdev_slist_check_first(const bfdev_slist_head_t *head, + const bfdev_slist_head_t *node) { return head->next == node; } @@ -108,7 +110,7 @@ bfdev_slist_check_first(const struct bfdev_slist_head *head, * @node: the node to check. */ static inline bool -bfdev_slist_check_end(const struct bfdev_slist_head *node) +bfdev_slist_check_end(const bfdev_slist_head_t *node) { return !node->next; } @@ -119,8 +121,8 @@ bfdev_slist_check_end(const struct bfdev_slist_head *node) * @node: the unique node. */ static inline bool -bfdev_slist_check_another(const struct bfdev_slist_head *head, - const struct bfdev_slist_head *node) +bfdev_slist_check_another(const bfdev_slist_head_t *head, + const bfdev_slist_head_t *node) { return head->next == node && node->next == NULL; } @@ -132,10 +134,10 @@ bfdev_slist_check_another(const struct bfdev_slist_head *head, * @newn: the new element to insert. */ static inline void -bfdev_slist_replace(struct bfdev_slist_head *head, struct bfdev_slist_head *oldn, - struct bfdev_slist_head *newn) +bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, + bfdev_slist_head_t *newn) { - struct bfdev_slist_head *walk = head; + bfdev_slist_head_t *walk = head; while (walk->next != oldn) walk = walk->next; diff --git a/src/llist.c b/src/llist.c index afe76d0e..d59a5ecd 100644 --- a/src/llist.c +++ b/src/llist.c @@ -8,10 +8,10 @@ #include export bool -bfdev_llist_split(struct bfdev_slist_head *head, struct bfdev_slist_head *node, - struct bfdev_slist_head *end) +bfdev_llist_split(bfdev_slist_head_t *head, bfdev_slist_head_t *node, + bfdev_slist_head_t *end) { - struct bfdev_slist_head *first; + bfdev_slist_head_t *first; do first = end->next = BFDEV_READ_ONCE(head->next); while ((void *)bfdev_cmpxchg( @@ -22,10 +22,10 @@ bfdev_llist_split(struct bfdev_slist_head *head, struct bfdev_slist_head *node, return !first; } -export struct bfdev_slist_head * -bfdev_llist_del(struct bfdev_slist_head *head) +export bfdev_slist_head_t * +bfdev_llist_del(bfdev_slist_head_t *head) { - struct bfdev_slist_head *old, *next, *entry; + bfdev_slist_head_t *old, *next, *entry; for (entry = BFDEV_READ_ONCE(head->next); (old = entry);) { next = BFDEV_READ_ONCE(entry->next); diff --git a/src/slist-debug.c b/src/slist-debug.c index ae974ff7..5ff66d04 100644 --- a/src/slist-debug.c +++ b/src/slist-debug.c @@ -12,7 +12,7 @@ #include export bool -bfdev_slist_check_add(struct bfdev_slist_head *node, struct bfdev_slist_head *newn) +bfdev_slist_check_add(bfdev_slist_head_t *node, bfdev_slist_head_t *newn) { if (bfdev_unlikely(newn->next && newn->next == node->next)) { bfdev_log_err( @@ -27,7 +27,7 @@ bfdev_slist_check_add(struct bfdev_slist_head *node, struct bfdev_slist_head *ne } export bool -bfdev_slist_check_del(struct bfdev_slist_head *node) +bfdev_slist_check_del(bfdev_slist_head_t *node) { if (bfdev_unlikely(node->next == BFDEV_POISON_SLIST)) { bfdev_log_err( From 6c1c7b9ccc64c76a2645f55a0a33a9f3f09eee5b Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 13:05:56 +0800 Subject: [PATCH 042/119] feat segtree: added initial typedef Signed-off-by: John Sanpe --- examples/segtree/selftest.c | 4 ++-- include/bfdev/segtree.h | 20 +++++++++++--------- src/segtree.c | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/segtree/selftest.c b/examples/segtree/selftest.c index 8fd1df87..e22e16e8 100644 --- a/examples/segtree/selftest.c +++ b/examples/segtree/selftest.c @@ -12,7 +12,7 @@ #define TEST_LOOP 10 struct test_node { - struct bfdev_segtree_node node; + bfdev_segtree_node_t node; unsigned int num; }; @@ -28,7 +28,7 @@ static int segtree_testing(struct test_pdata *sdata) { struct test_node *node, *tnode; - struct bfdev_segtree_node *snode, *tsnode; + bfdev_segtree_node_t *snode, *tsnode; unsigned int count; BFDEV_RB_ROOT_CACHED(segtree_root); diff --git a/include/bfdev/segtree.h b/include/bfdev/segtree.h index 8920fb86..4fbb77b2 100644 --- a/include/bfdev/segtree.h +++ b/include/bfdev/segtree.h @@ -11,6 +11,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_segtree_node bfdev_segtree_node_t; + struct bfdev_segtree_node { bfdev_rb_node_t node; unsigned long start, end; @@ -19,7 +21,7 @@ struct bfdev_segtree_node { /** * bfdev_segtree_entry - get the struct for this entry. - * @ptr: the &struct bfdev_segtree_node pointer. + * @ptr: the &bfdev_segtree_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_segtree_node within the struct. */ @@ -28,18 +30,18 @@ struct bfdev_segtree_node { /** * bfdev_segtree_entry_safe - get the struct for this entry or null. - * @ptr: the &struct bfdev_segtree_node pointer. + * @ptr: the &bfdev_segtree_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_segtree_node within the struct. */ #define bfdev_segtree_entry_safe(ptr, type, member) \ bfdev_container_of_safe(ptr, type, member) -extern void bfdev_segtree_insert(bfdev_rb_root_cached_t *root, struct bfdev_segtree_node *node); -extern void bfdev_segtree_delete(bfdev_rb_root_cached_t *root, struct bfdev_segtree_node *node); -extern struct bfdev_segtree_node *bfdev_segtree_search(struct bfdev_segtree_node *node, unsigned long start, unsigned long end); -extern struct bfdev_segtree_node *bfdev_segtree_first(bfdev_rb_root_cached_t *root, unsigned long start, unsigned long end); -extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node *node, unsigned long start, unsigned long end); +extern void bfdev_segtree_insert(bfdev_rb_root_cached_t *root, bfdev_segtree_node_t *node); +extern void bfdev_segtree_delete(bfdev_rb_root_cached_t *root, bfdev_segtree_node_t *node); +extern bfdev_segtree_node_t *bfdev_segtree_search(bfdev_segtree_node_t *node, unsigned long start, unsigned long end); +extern bfdev_segtree_node_t *bfdev_segtree_first(bfdev_rb_root_cached_t *root, unsigned long start, unsigned long end); +extern bfdev_segtree_node_t *bfdev_segtree_next(bfdev_segtree_node_t *node, unsigned long start, unsigned long end); /** * bfdev_segtree_first_entry - get the first element from a segtree. @@ -64,7 +66,7 @@ extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node * /** * bfdev_segtree_for_each - iterate over a segtree. - * @pos: the &struct bfdev_segtree_node to use as a loop cursor. + * @pos: the &bfdev_segtree_node_t to use as a loop cursor. * @start: start endpoint of segtree element. * @end: end endpoint of segtree element. * @root: the root for your segtree. @@ -84,7 +86,7 @@ extern struct bfdev_segtree_node *bfdev_segtree_next(struct bfdev_segtree_node * /** * bfdev_segtree_for_each_continue - continue iteration over a segtree. - * @pos: the &struct bfdev_segtree_node to use as a loop cursor. + * @pos: the &bfdev_segtree_node_t to use as a loop cursor. * @start: start endpoint of segtree element. * @end: end endpoint of segtree element. */ diff --git a/src/segtree.c b/src/segtree.c index 8d5eb9f5..73031ad9 100644 --- a/src/segtree.c +++ b/src/segtree.c @@ -10,6 +10,6 @@ #define SEGTREE_END(segnode) ((segnode)->end) BFDEV_SEGTREE_DEFINE( - export, bfdev_segtree, struct bfdev_segtree_node, + export, bfdev_segtree, bfdev_segtree_node_t, node, unsigned long, subtree, SEGTREE_START, SEGTREE_END ); From 292b3ba156dc6adbf1956a73c0b86dd4efb5a737 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 13:42:00 +0800 Subject: [PATCH 043/119] feat list: added initial typedef Signed-off-by: John Sanpe --- examples/btree/selftest.c | 2 +- examples/list/benchmark.c | 6 +-- examples/list/selftest.c | 8 ++-- include/bfdev/cache.h | 10 ++-- include/bfdev/ilist.h | 30 ++++++------ include/bfdev/list.h | 96 +++++++++++++++++++------------------- include/bfdev/minpool.h | 8 ++-- include/bfdev/respool.h | 4 +- include/bfdev/skiplist.h | 4 +- include/bfdev/textsearch.h | 2 +- src/cache/lru.c | 4 +- src/ilist.c | 10 ++-- src/list-debug.c | 6 +-- src/list-sort.c | 89 +++++++++++++++++------------------ src/skiplist.c | 4 +- 15 files changed, 142 insertions(+), 141 deletions(-) diff --git a/examples/btree/selftest.c b/examples/btree/selftest.c index 759f7ce2..51ff8912 100644 --- a/examples/btree/selftest.c +++ b/examples/btree/selftest.c @@ -13,7 +13,7 @@ #define TEST_LOOP 100 struct test_node { - struct bfdev_list_head list; + bfdev_list_head_t list; union { uintptr_t key; char uuid[11]; diff --git a/examples/list/benchmark.c b/examples/list/benchmark.c index 7bb6c87d..7b375e82 100644 --- a/examples/list/benchmark.c +++ b/examples/list/benchmark.c @@ -17,7 +17,7 @@ static BFDEV_LIST_HEAD(demo_list); struct benchmark { - struct bfdev_list_head list; + bfdev_list_head_t list; unsigned int num; unsigned long data; }; @@ -36,8 +36,8 @@ node_dump(struct benchmark *node) #endif static long -demo_cmp(const struct bfdev_list_head *node1, - const struct bfdev_list_head *node2, void *pdata) +demo_cmp(const bfdev_list_head_t *node1, + const bfdev_list_head_t *node2, void *pdata) { struct benchmark *test1, *test2; diff --git a/examples/list/selftest.c b/examples/list/selftest.c index 001777e2..55b5fda6 100644 --- a/examples/list/selftest.c +++ b/examples/list/selftest.c @@ -11,7 +11,7 @@ #define TEST_LOOP 10 struct test_node { - struct bfdev_list_head list; + bfdev_list_head_t list; unsigned long num; }; @@ -23,8 +23,8 @@ struct test_pdata { bfdev_list_entry(ptr, struct test_node, list) static long -list_test_sort(const struct bfdev_list_head *node1, - const struct bfdev_list_head *node2, void *pdata) +list_test_sort(const bfdev_list_head_t *node1, + const bfdev_list_head_t *node2, void *pdata) { struct test_node *tnode1, *tnode2; @@ -41,7 +41,7 @@ static int list_selftest(struct test_pdata *ldata) { struct test_node *node, *nnode, *tnode; - struct bfdev_list_head *list, *nlist, *tlist; + bfdev_list_head_t *list, *nlist, *tlist; unsigned int count; BFDEV_LIST_HEAD(test_head); diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index 0774aa05..4270d754 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -44,7 +44,7 @@ enum bfdev_cache_status { struct bfdev_cache_node { struct bfdev_hlist_node hash; - struct bfdev_list_head list; + bfdev_list_head_t list; enum bfdev_cache_status status; unsigned long index; @@ -59,9 +59,9 @@ struct bfdev_cache_head { struct bfdev_hlist_head *taghash; struct bfdev_cache_node **nodes; - struct bfdev_list_head using; - struct bfdev_list_head freed; - struct bfdev_list_head changing; + bfdev_list_head_t using; + bfdev_list_head_t freed; + bfdev_list_head_t changing; /* const settings */ unsigned long size; @@ -80,7 +80,7 @@ struct bfdev_cache_head { }; struct bfdev_cache_algo { - struct bfdev_list_head list; + bfdev_list_head_t list; const char *name; bool (*starving)(struct bfdev_cache_head *head); diff --git a/include/bfdev/ilist.h b/include/bfdev/ilist.h index 71bbdc04..9496b030 100644 --- a/include/bfdev/ilist.h +++ b/include/bfdev/ilist.h @@ -16,12 +16,12 @@ typedef struct bfdev_ilist_head bfdev_ilist_head_t; typedef struct bfdev_ilist_node bfdev_ilist_node_t; struct bfdev_ilist_head { - struct bfdev_list_head node_list; + bfdev_list_head_t node_list; }; struct bfdev_ilist_node { - struct bfdev_list_head node_list; - struct bfdev_list_head index_list; + bfdev_list_head_t node_list; + bfdev_list_head_t index_list; }; #define BFDEV_ILIST_HEAD_STATIC(name) \ @@ -134,14 +134,14 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each - iterate over a list. * @head: the head for your list. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. */ #define bfdev_ilist_for_each(pos, head) \ bfdev_list_for_each(pos, &(head)->node_list) /** * bfdev_ilist_for_each_reverse - iterate over a list backwards. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @head: the head for your list. */ #define bfdev_ilist_for_each_reverse(pos, head) \ @@ -150,14 +150,14 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_from - iterate over a list from the current point. * @head: the head for your list. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. */ #define bfdev_ilist_for_each_from(pos, head) \ bfdev_list_for_each_from(pos, &(head)->node_list) /** * bfdev_ilist_for_each_reverse_from - iterate over a list backwards from the current point. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @head: the head for your list. */ #define bfdev_ilist_for_each_reverse_from(pos, head) \ @@ -165,7 +165,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_continue - continue iteration over a list. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @head: the head for your list. */ #define bfdev_ilist_for_each_continue(pos, head) \ @@ -173,7 +173,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_reverse_continue - continue iteration over a list backwards. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @head: the head for your list. */ #define bfdev_ilist_for_each_reverse_continue(pos, head) \ @@ -181,7 +181,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_safe - iterate over a list safe against removal of list entry. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ @@ -190,7 +190,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_reverse_safe - iterate backwards over list safe against removal. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ @@ -199,7 +199,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_from_safe - iterate over a list safe against removal of list entry from the current point. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ @@ -208,7 +208,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_reverse_from_safe - iterate backwards over list safe against removal from the current point. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ @@ -217,7 +217,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_continue_safe - continue list iteration safe against removal. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ @@ -226,7 +226,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) /** * bfdev_ilist_for_each_reverse_continue_safe - continue backwards over list iteration safe against removal. - * @pos: the &struct bfdev_list_head to use as a loop cursor. + * @pos: the &bfdev_list_head_t to use as a loop cursor. * @tmp: another bfdev_list_head to use as temporary storage. * @head: the head for your list. */ diff --git a/include/bfdev/list.h b/include/bfdev/list.h index 0b1ccf08..8ea4637b 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -14,41 +14,43 @@ BFDEV_BEGIN_DECLS -struct bfdev_list_head { - struct bfdev_list_head *prev; - struct bfdev_list_head *next; +typedef struct bfdev_list_head bfdev_list_head_t; + +struct bfdev_list_head { + bfdev_list_head_t *prev; + bfdev_list_head_t *next; }; #define BFDEV_LIST_HEAD_STATIC(name) \ {&(name), &(name)} #define BFDEV_LIST_HEAD_INIT(name) \ - (struct bfdev_list_head) BFDEV_LIST_HEAD_STATIC(name) + (bfdev_list_head_t) BFDEV_LIST_HEAD_STATIC(name) #define BFDEV_LIST_HEAD(name) \ - struct bfdev_list_head name = BFDEV_LIST_HEAD_INIT(name) + bfdev_list_head_t name = BFDEV_LIST_HEAD_INIT(name) BFDEV_CALLBACK_CMP( bfdev_list_cmp_t, - const struct bfdev_list_head * + const bfdev_list_head_t * ); extern void -bfdev_list_sort(struct bfdev_list_head *head, +bfdev_list_sort(bfdev_list_head_t *head, bfdev_list_cmp_t cmp, void *pdata); #ifdef BFDEV_DEBUG_LIST extern bool -bfdev_list_check_add(struct bfdev_list_head *prev, struct bfdev_list_head *next, - struct bfdev_list_head *node); +bfdev_list_check_add(bfdev_list_head_t *prev, bfdev_list_head_t *next, + bfdev_list_head_t *node); extern bool -bfdev_list_check_del(struct bfdev_list_head *node); +bfdev_list_check_del(bfdev_list_head_t *node); #endif static inline void -bfdev_list_insert(struct bfdev_list_head *prev, struct bfdev_list_head *next, - struct bfdev_list_head *node) +bfdev_list_insert(bfdev_list_head_t *prev, bfdev_list_head_t *next, + bfdev_list_head_t *node) { #ifdef BFDEV_DEBUG_LIST if (bfdev_unlikely(!bfdev_list_check_add(prev, next, node))) @@ -66,7 +68,7 @@ bfdev_list_insert(struct bfdev_list_head *prev, struct bfdev_list_head *next, * @head: list head structure to be initialized. */ static inline void -bfdev_list_head_init(struct bfdev_list_head *head) +bfdev_list_head_init(bfdev_list_head_t *head) { head->prev = head; head->next = head; @@ -78,7 +80,7 @@ bfdev_list_head_init(struct bfdev_list_head *head) * @newn: new entry to be added. */ static inline void -bfdev_list_add(struct bfdev_list_head *node, struct bfdev_list_head *newn) +bfdev_list_add(bfdev_list_head_t *node, bfdev_list_head_t *newn) { bfdev_list_insert(node, node->next, newn); } @@ -89,7 +91,7 @@ bfdev_list_add(struct bfdev_list_head *node, struct bfdev_list_head *newn) * @newn: new entry to be added. */ static inline void -bfdev_list_add_prev(struct bfdev_list_head *node, struct bfdev_list_head *newn) +bfdev_list_add_prev(bfdev_list_head_t *node, bfdev_list_head_t *newn) { bfdev_list_insert(node->prev, node, newn); } @@ -99,7 +101,7 @@ bfdev_list_add_prev(struct bfdev_list_head *node, struct bfdev_list_head *newn) * @node: the element to delete from the list. */ static inline void -bfdev_list_deluf(struct bfdev_list_head *node) +bfdev_list_deluf(bfdev_list_head_t *node) { node->prev->next = node->next; node->next->prev = node->prev; @@ -110,7 +112,7 @@ bfdev_list_deluf(struct bfdev_list_head *node) * @node: the element to delete from the list. */ static inline void -bfdev_list_del(struct bfdev_list_head *node) +bfdev_list_del(bfdev_list_head_t *node) { #ifdef BFDEV_DEBUG_LIST if (bfdev_unlikely(!bfdev_list_check_del(node))) @@ -118,8 +120,8 @@ bfdev_list_del(struct bfdev_list_head *node) #endif bfdev_list_deluf(node); - node->next = (struct bfdev_list_head *)BFDEV_POISON_LIST1; - node->prev = (struct bfdev_list_head *)BFDEV_POISON_LIST2; + node->next = (bfdev_list_head_t *)BFDEV_POISON_LIST1; + node->prev = (bfdev_list_head_t *)BFDEV_POISON_LIST2; } /** @@ -127,7 +129,7 @@ bfdev_list_del(struct bfdev_list_head *node) * @head: list head to check. */ static inline bool -bfdev_list_check_empty(const struct bfdev_list_head *head) +bfdev_list_check_empty(const bfdev_list_head_t *head) { return head->next == head; } @@ -138,8 +140,8 @@ bfdev_list_check_empty(const struct bfdev_list_head *head) * @list: the entry to test */ static inline bool -bfdev_list_check_head(const struct bfdev_list_head *head, - const struct bfdev_list_head *node) +bfdev_list_check_head(const bfdev_list_head_t *head, + const bfdev_list_head_t *node) { return head == node; } @@ -150,8 +152,8 @@ bfdev_list_check_head(const struct bfdev_list_head *head, * @node: the entry to test. */ static inline bool -bfdev_list_check_first(const struct bfdev_list_head *head, - const struct bfdev_list_head *node) +bfdev_list_check_first(const bfdev_list_head_t *head, + const bfdev_list_head_t *node) { return node->prev == head; } @@ -162,8 +164,8 @@ bfdev_list_check_first(const struct bfdev_list_head *head, * @node: the entry to test. */ static inline bool -bfdev_list_check_end(const struct bfdev_list_head *head, - const struct bfdev_list_head *node) +bfdev_list_check_end(const bfdev_list_head_t *head, + const bfdev_list_head_t *node) { return node->next == head; } @@ -174,18 +176,18 @@ bfdev_list_check_end(const struct bfdev_list_head *head, * @node: the unique node. */ static inline bool -bfdev_list_check_another(const struct bfdev_list_head *head, - const struct bfdev_list_head *node) +bfdev_list_check_another(const bfdev_list_head_t *head, + const bfdev_list_head_t *node) { return head->next == node && head->prev == node; } static inline void -bfdev_list_relocate(struct bfdev_list_head *prev, struct bfdev_list_head *next, - struct bfdev_list_head *list) +bfdev_list_relocate(bfdev_list_head_t *prev, bfdev_list_head_t *next, + bfdev_list_head_t *list) { - struct bfdev_list_head *first = list->next; - struct bfdev_list_head *last = list->prev; + bfdev_list_head_t *first = list->next; + bfdev_list_head_t *last = list->prev; first->prev = prev; prev->next = first; @@ -200,7 +202,7 @@ bfdev_list_relocate(struct bfdev_list_head *prev, struct bfdev_list_head *next, * @list: the new list to add. */ static inline void -bfdev_list_splice(struct bfdev_list_head *head, struct bfdev_list_head *list) +bfdev_list_splice(bfdev_list_head_t *head, bfdev_list_head_t *list) { if (!bfdev_list_check_empty(list)) bfdev_list_relocate(head, head->next, list); @@ -212,7 +214,7 @@ bfdev_list_splice(struct bfdev_list_head *head, struct bfdev_list_head *list) * @list: the new list to add. */ static inline void -bfdev_list_splice_prev(struct bfdev_list_head *head, struct bfdev_list_head *list) +bfdev_list_splice_prev(bfdev_list_head_t *head, bfdev_list_head_t *list) { if (!bfdev_list_check_empty(list)) bfdev_list_relocate(head->prev, head, list); @@ -224,7 +226,7 @@ bfdev_list_splice_prev(struct bfdev_list_head *head, struct bfdev_list_head *lis * @newn: the new element to insert. */ static inline void -bfdev_list_replace(struct bfdev_list_head *oldn, struct bfdev_list_head *newn) +bfdev_list_replace(bfdev_list_head_t *oldn, bfdev_list_head_t *newn) { newn->prev = oldn->prev; newn->next = oldn->next; @@ -238,7 +240,7 @@ bfdev_list_replace(struct bfdev_list_head *oldn, struct bfdev_list_head *newn) * @node: the entry to move. */ static inline void -bfdev_list_move(struct bfdev_list_head *head, struct bfdev_list_head *node) +bfdev_list_move(bfdev_list_head_t *head, bfdev_list_head_t *node) { bfdev_list_del(node); bfdev_list_add(head, node); @@ -250,7 +252,7 @@ bfdev_list_move(struct bfdev_list_head *head, struct bfdev_list_head *node) * @node: the entry to move. */ static inline void -bfdev_list_move_prev(struct bfdev_list_head *head, struct bfdev_list_head *node) +bfdev_list_move_prev(bfdev_list_head_t *head, bfdev_list_head_t *node) { bfdev_list_del(node); bfdev_list_add_prev(head, node); @@ -262,9 +264,9 @@ bfdev_list_move_prev(struct bfdev_list_head *head, struct bfdev_list_head *node) * @node2: the location to place entry1. */ static inline void -bfdev_list_swap(struct bfdev_list_head *node1, struct bfdev_list_head *node2) +bfdev_list_swap(bfdev_list_head_t *node1, bfdev_list_head_t *node2) { - struct bfdev_list_head *prev = node2->prev; + bfdev_list_head_t *prev = node2->prev; bfdev_list_del(node2); bfdev_list_replace(node1, node2); @@ -279,7 +281,7 @@ bfdev_list_swap(struct bfdev_list_head *node1, struct bfdev_list_head *node2) * @node: the element to delete from the list. */ static inline void -bfdev_list_del_init(struct bfdev_list_head *node) +bfdev_list_del_init(bfdev_list_head_t *node) { bfdev_list_deluf(node); bfdev_list_head_init(node); @@ -291,7 +293,7 @@ bfdev_list_del_init(struct bfdev_list_head *node) * @newn: the new element to insert. */ static inline void -bfdev_list_replace_init(struct bfdev_list_head *oldn, struct bfdev_list_head *newn) +bfdev_list_replace_init(bfdev_list_head_t *oldn, bfdev_list_head_t *newn) { bfdev_list_replace(oldn, newn); bfdev_list_head_init(oldn); @@ -303,7 +305,7 @@ bfdev_list_replace_init(struct bfdev_list_head *oldn, struct bfdev_list_head *ne * @list: the new list to add. */ static inline void -bfdev_list_splice_init(struct bfdev_list_head *head, struct bfdev_list_head *list) +bfdev_list_splice_init(bfdev_list_head_t *head, bfdev_list_head_t *list) { if (!bfdev_list_check_empty(list)) { bfdev_list_splice(head, list); @@ -317,7 +319,7 @@ bfdev_list_splice_init(struct bfdev_list_head *head, struct bfdev_list_head *lis * @list: the new list to add. */ static inline void -bfdev_list_splice_tail_init(struct bfdev_list_head *head, struct bfdev_list_head *list) +bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) { if (!bfdev_list_check_empty(list)) { bfdev_list_splice_prev(head, list); @@ -384,8 +386,8 @@ bfdev_list_splice_tail_init(struct bfdev_list_head *head, struct bfdev_list_head * @member: the name of the list head within the struct. */ #define bfdev_list_first_entry_or_null(ptr, type, member) ({ \ - struct bfdev_list_head *head__ = (ptr); \ - struct bfdev_list_head *pos__ = head__->next; \ + bfdev_list_head_t *head__ = (ptr); \ + bfdev_list_head_t *pos__ = head__->next; \ pos__ != head__ ? bfdev_list_entry(pos__, type, member) : NULL; \ }) @@ -396,8 +398,8 @@ bfdev_list_splice_tail_init(struct bfdev_list_head *head, struct bfdev_list_head * @member: the name of the list head within the struct. */ #define bfdev_list_last_entry_or_null(ptr, type, member) ({ \ - struct bfdev_list_head *head__ = (ptr); \ - struct bfdev_list_head *pos__ = head__->prev; \ + bfdev_list_head_t *head__ = (ptr); \ + bfdev_list_head_t *pos__ = head__->prev; \ pos__ != head__ ? bfdev_list_entry(pos__, type, member) : NULL; \ }) diff --git a/include/bfdev/minpool.h b/include/bfdev/minpool.h index e2bef861..d7b6ac64 100644 --- a/include/bfdev/minpool.h +++ b/include/bfdev/minpool.h @@ -22,15 +22,15 @@ typedef struct bfdev_minpool_node * (*bfdev_minpool_find_t)(struct bfdev_minpool_head *head, size_t size); struct bfdev_minpool_head { - struct bfdev_list_head block_list; - struct bfdev_list_head free_list; + bfdev_list_head_t block_list; + bfdev_list_head_t free_list; bfdev_minpool_find_t find; size_t avail; }; struct bfdev_minpool_node { - struct bfdev_list_head block; - struct bfdev_list_head free; + bfdev_list_head_t block; + bfdev_list_head_t free; size_t usize; char data[0]; }; diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h index 315cb0f3..f393ffa5 100755 --- a/include/bfdev/respool.h +++ b/include/bfdev/respool.h @@ -22,13 +22,13 @@ typedef long (*bfdev_respool_find_t) struct bfdev_resnode { const char *name; - struct bfdev_list_head list; + bfdev_list_head_t list; bfdev_respool_release_t release; }; struct bfdev_respool { const char *name; - struct bfdev_list_head node; + bfdev_list_head_t node; }; #define BFDEV_RESPOOL_STATIC(name) { \ diff --git a/include/bfdev/skiplist.h b/include/bfdev/skiplist.h index 1d83de1b..d801a19b 100644 --- a/include/bfdev/skiplist.h +++ b/include/bfdev/skiplist.h @@ -19,14 +19,14 @@ typedef struct bfdev_skip_node bfdev_skip_node_t; struct bfdev_skip_node { void *key; - struct bfdev_list_head list[0]; + bfdev_list_head_t list[0]; }; struct bfdev_skip_head { const struct bfdev_alloc *alloc; unsigned int curr; unsigned int levels; - struct bfdev_list_head nodes[0]; + bfdev_list_head_t nodes[0]; }; extern int diff --git a/include/bfdev/textsearch.h b/include/bfdev/textsearch.h index 142bdcef..8c08d476 100644 --- a/include/bfdev/textsearch.h +++ b/include/bfdev/textsearch.h @@ -64,7 +64,7 @@ struct bfdev_ts_context { * @pattern_len: return length of pattern. */ struct bfdev_ts_algorithm { - struct bfdev_list_head list; + bfdev_list_head_t list; const char *name; bfdev_ts_context_t *(*prepare)(const struct bfdev_alloc *alloc, const void *pattern, diff --git a/src/cache/lru.c b/src/cache/lru.c index 74295343..a31f0a76 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -8,12 +8,12 @@ struct lru_head { struct bfdev_cache_head cache; - struct bfdev_list_head lru; + bfdev_list_head_t lru; }; struct lru_node { struct bfdev_cache_node cache; - struct bfdev_list_head node; + bfdev_list_head_t node; }; #define cache_to_lru_head(ptr) \ diff --git a/src/ilist.c b/src/ilist.c index 40b4a446..9d955519 100644 --- a/src/ilist.c +++ b/src/ilist.c @@ -13,8 +13,8 @@ #ifdef BFDEV_DEBUG_ILIST static inline bool -ilist_integrity_check(struct bfdev_list_head *head, struct bfdev_list_head *prev, - struct bfdev_list_head *next) +ilist_integrity_check(bfdev_list_head_t *head, bfdev_list_head_t *prev, + bfdev_list_head_t *next) { if (bfdev_unlikely(prev->next != next)) { bfdev_log_err( @@ -38,9 +38,9 @@ ilist_integrity_check(struct bfdev_list_head *head, struct bfdev_list_head *prev } static inline bool -ilist_list_check(struct bfdev_list_head *head) +ilist_list_check(bfdev_list_head_t *head) { - struct bfdev_list_head *prev = head, *next = head->next; + bfdev_list_head_t *prev = head, *next = head->next; bool success; success = ilist_integrity_check(head, prev, next); @@ -71,7 +71,7 @@ bfdev_ilist_add(bfdev_ilist_head_t *ihead, bfdev_ilist_node_t *inode, bfdev_ilist_cmp_t cmp, void *pdata) { bfdev_ilist_node_t *walk, *first, *prev = NULL; - struct bfdev_list_head *next = &ihead->node_list; + bfdev_list_head_t *next = &ihead->node_list; #ifdef BFDEV_DEBUG_ILIST if (bfdev_unlikely(!ilist_head_check(ihead))) diff --git a/src/list-debug.c b/src/list-debug.c index 826438d2..875ad05a 100644 --- a/src/list-debug.c +++ b/src/list-debug.c @@ -12,8 +12,8 @@ #include export bool -bfdev_list_check_add(struct bfdev_list_head *prev, struct bfdev_list_head *next, - struct bfdev_list_head *newn) +bfdev_list_check_add(bfdev_list_head_t *prev, bfdev_list_head_t *next, + bfdev_list_head_t *newn) { if (bfdev_unlikely(prev->next != next)) { bfdev_log_err( @@ -46,7 +46,7 @@ bfdev_list_check_add(struct bfdev_list_head *prev, struct bfdev_list_head *next, } export bool -bfdev_list_check_del(struct bfdev_list_head *node) +bfdev_list_check_del(bfdev_list_head_t *node) { if (bfdev_unlikely(node->next == BFDEV_POISON_LIST1)) { bfdev_log_err( diff --git a/src/list-sort.c b/src/list-sort.c index 6d78fbcf..c7401fe8 100644 --- a/src/list-sort.c +++ b/src/list-sort.c @@ -6,27 +6,27 @@ #include #include -static inline struct bfdev_list_head * +static inline bfdev_list_head_t * list_merge(bfdev_list_cmp_t cmp, void *pdata, - struct bfdev_list_head *a, struct bfdev_list_head *b) + bfdev_list_head_t *node1, bfdev_list_head_t *node2) { - struct bfdev_list_head *node, **tail = &node; + bfdev_list_head_t *node, **tail = &node; for (;;) { - if (cmp(a, b, pdata) <= 0) { - *tail = a; - tail = &a->next; - a = a->next; - if (!a) { - *tail = b; + if (cmp(node1, node2, pdata) <= 0) { + *tail = node1; + tail = &node1->next; + node1 = node1->next; + if (!node1) { + *tail = node2; break; } } else { - *tail = b; - tail = &b->next; - b = b->next; - if (!b) { - *tail = a; + *tail = node2; + tail = &node2->next; + node2 = node2->next; + if (!node2) { + *tail = node1; break; } } @@ -36,49 +36,46 @@ list_merge(bfdev_list_cmp_t cmp, void *pdata, } static inline void -list_finish(bfdev_list_cmp_t cmp, void *pdata, struct bfdev_list_head *head, - struct bfdev_list_head *a, struct bfdev_list_head *b) +list_finish(bfdev_list_cmp_t cmp, void *pdata, bfdev_list_head_t *head, + bfdev_list_head_t *node1, bfdev_list_head_t *node2) { - struct bfdev_list_head *tail = head; - unsigned int count = 0; + bfdev_list_head_t *tail = head; for (;;) { - if (cmp(a, b, pdata) <= 0) { - tail->next = a; - a->prev = tail; - tail = a; - a = a->next; - if (!a) + if (cmp(node1, node2, pdata) <= 0) { + tail->next = node1; + node1->prev = tail; + tail = node1; + node1 = node1->next; + if (!node1) break; } else { - tail->next = b; - b->prev = tail; - tail = b; - b = b->next; - if (!b) { - b = a; + tail->next = node2; + node2->prev = tail; + tail = node2; + node2 = node2->next; + if (!node2) { + node2 = node1; break; } } } - tail->next = b; + tail->next = node2; do { - if (bfdev_unlikely(!++count)) - cmp(b, b, pdata); - b->prev = tail; - tail = b; - b = b->next; - } while (b); + node2->prev = tail; + tail = node2; + node2 = node2->next; + } while (node2); tail->next = head; head->prev = tail; } export void -bfdev_list_sort(struct bfdev_list_head *head, bfdev_list_cmp_t cmp, void *pdata) +bfdev_list_sort(bfdev_list_head_t *head, bfdev_list_cmp_t cmp, void *pdata) { - struct bfdev_list_head *pending, *node; + bfdev_list_head_t *pending, *node; unsigned int count; node = head->next; @@ -89,17 +86,17 @@ bfdev_list_sort(struct bfdev_list_head *head, bfdev_list_cmp_t cmp, void *pdata) pending = NULL; for (count = 0; node; ++count) { - struct bfdev_list_head **tail = &pending; + bfdev_list_head_t **tail = &pending; size_t bits; for (bits = count; bits & 1; bits >>= 1) tail = &(*tail)->prev; if (bfdev_likely(bits)) { - struct bfdev_list_head *b = *tail, *a = b->prev; - b = list_merge(cmp, pdata, a, b); - b->prev = a->prev; - *tail = b; + bfdev_list_head_t *node2 = *tail, *node1 = node2->prev; + node2 = list_merge(cmp, pdata, node1, node2); + node2->prev = node1->prev; + *tail = node2; } node->prev = pending; @@ -113,10 +110,12 @@ bfdev_list_sort(struct bfdev_list_head *head, bfdev_list_cmp_t cmp, void *pdata) pending = pending->prev; for (;;) { - struct bfdev_list_head *next = pending->prev; + bfdev_list_head_t *next; + next = pending->prev; if (!next) break; + node = list_merge(cmp, pdata, pending, node); pending = next; } diff --git a/src/skiplist.c b/src/skiplist.c index 9517af29..bc0cf06f 100644 --- a/src/skiplist.c +++ b/src/skiplist.c @@ -26,7 +26,7 @@ skipnode_find(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata, unsigned int *plev) { unsigned int level = head->curr; - struct bfdev_list_head *list, *end; + bfdev_list_head_t *list, *end; bfdev_skip_node_t *walk; long retval; @@ -63,7 +63,7 @@ bfdev_skiplist_insert(bfdev_skip_head_t *head, void *key, bfdev_cmp_t cmp, void *pdata) { const struct bfdev_alloc *alloc = head->alloc; - struct bfdev_list_head *list, *end; + bfdev_list_head_t *list, *end; bfdev_skip_node_t *walk, *node; unsigned int level, count; long retval; From c9eaa6b54ee8533864fcbff47e943b7baa9a6a3e Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 13:58:09 +0800 Subject: [PATCH 044/119] feat heap: added initial typedef Signed-off-by: John Sanpe --- examples/heap/benchmark.c | 8 +-- examples/heap/selftest.c | 8 +-- include/bfdev/heap.h | 113 +++++++++++++++++++------------------- src/cache/lfu.c | 8 +-- src/heap-debug.c | 6 +- src/heap.c | 50 ++++++++--------- 6 files changed, 98 insertions(+), 95 deletions(-) diff --git a/examples/heap/benchmark.c b/examples/heap/benchmark.c index ddf6aeed..83f1b826 100644 --- a/examples/heap/benchmark.c +++ b/examples/heap/benchmark.c @@ -16,7 +16,7 @@ #define TEST_LEN 1000000 struct bench_node { - struct bfdev_heap_node node; + bfdev_heap_node_t node; unsigned int num; unsigned int data; }; @@ -40,7 +40,7 @@ node_dump(struct bench_node *node) #endif static unsigned int -test_deepth(struct bfdev_heap_node *node) +test_deepth(bfdev_heap_node_t *node) { unsigned int left_deepth, right_deepth; @@ -54,8 +54,8 @@ test_deepth(struct bfdev_heap_node *node) } static long -bench_cmp(const struct bfdev_heap_node *hpa, - const struct bfdev_heap_node *hpb, void *pdata) +bench_cmp(const bfdev_heap_node_t *hpa, + const bfdev_heap_node_t *hpb, void *pdata) { struct bench_node *node1, *node2; diff --git a/examples/heap/selftest.c b/examples/heap/selftest.c index 6de4f2b9..049a0e50 100644 --- a/examples/heap/selftest.c +++ b/examples/heap/selftest.c @@ -15,7 +15,7 @@ #define TEST_LOOP 100 struct test_node { - struct bfdev_heap_node node; + bfdev_heap_node_t node; unsigned short num; }; @@ -27,8 +27,8 @@ struct test_pdata { bfdev_heap_entry(ptr, struct test_node, node) static long -bfdev_heap_test_cmp(const struct bfdev_heap_node *node1, - const struct bfdev_heap_node *node2, void *pdata) +bfdev_heap_test_cmp(const bfdev_heap_node_t *node1, + const bfdev_heap_node_t *node2, void *pdata) { struct test_node *tnode1, *tnode2; @@ -45,7 +45,7 @@ static int bfdev_heap_testing(struct test_pdata *hdata) { struct test_node *node, *nnode, *tnode; - struct bfdev_heap_node *hpnode, *nhpnode, *thpnode; + bfdev_heap_node_t *hpnode, *nhpnode, *thpnode; unsigned long count, index, tindex; BFDEV_HEAP_ROOT(root); diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index 79b64fdf..c0fc8e87 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -14,14 +14,17 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_heap_root bfdev_heap_root_t; +typedef struct bfdev_heap_node bfdev_heap_node_t; + struct bfdev_heap_node { - struct bfdev_heap_node *parent; - struct bfdev_heap_node *left; - struct bfdev_heap_node *right; + bfdev_heap_node_t *parent; + bfdev_heap_node_t *left; + bfdev_heap_node_t *right; }; struct bfdev_heap_root { - struct bfdev_heap_node *node; + bfdev_heap_node_t *node; unsigned int count; }; @@ -32,10 +35,10 @@ struct bfdev_heap_root { {{NULL}, NULL} #define BFDEV_HEAP_INIT \ - (struct bfdev_heap_root) BFDEV_HEAP_STATIC + (bfdev_heap_root_t) BFDEV_HEAP_STATIC #define BFDEV_HEAP_ROOT(name) \ - struct bfdev_heap_root name = BFDEV_HEAP_INIT + bfdev_heap_root_t name = BFDEV_HEAP_INIT #define BFDEV_HEAP_EMPTY_ROOT(root) \ ((root)->node == NULL) @@ -54,7 +57,7 @@ struct bfdev_heap_root { /** * bfdev_heap_entry - get the struct for this entry. - * @ptr: the &struct bfdev_heap_node pointer. + * @ptr: the &bfdev_heap_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_heap_node within the struct. */ @@ -63,7 +66,7 @@ struct bfdev_heap_root { /** * bfdev_heap_entry_safe - get the struct for this entry or null. - * @ptr: the &struct bfdev_heap_node pointer. + * @ptr: the &bfdev_heap_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_heap_node within the struct. */ @@ -72,20 +75,20 @@ struct bfdev_heap_root { #ifdef BFDEV_DEBUG_HEAP extern bool -bfdev_heap_check_link(struct bfdev_heap_node *parent, struct bfdev_heap_node **link, - struct bfdev_heap_node *node); +bfdev_heap_check_link(bfdev_heap_node_t *parent, bfdev_heap_node_t **link, + bfdev_heap_node_t *node); extern bool -bfdev_heap_check_delete(struct bfdev_heap_node *node); +bfdev_heap_check_delete(bfdev_heap_node_t *node); #endif BFDEV_CALLBACK_CMP( bfdev_heap_cmp_t, - const struct bfdev_heap_node * + const bfdev_heap_node_t * ); static inline void -bfdev_heap_init(struct bfdev_heap_root *root) +bfdev_heap_init(bfdev_heap_root_t *root) { *root = BFDEV_HEAP_INIT; } @@ -97,7 +100,7 @@ bfdev_heap_init(struct bfdev_heap_root *root) * @cmp: operator defining the node order. */ extern void -bfdev_heap_fixup(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_fixup(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata); /** @@ -107,7 +110,7 @@ bfdev_heap_fixup(struct bfdev_heap_root *root, struct bfdev_heap_node *node, * @cmp: operator defining the node order. */ extern void -bfdev_heap_erase(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_erase(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata); /** @@ -115,8 +118,8 @@ bfdev_heap_erase(struct bfdev_heap_root *root, struct bfdev_heap_node *node, * @root: heap root of node. * @node: node to remove. */ -extern struct bfdev_heap_node * -bfdev_heap_remove(struct bfdev_heap_root *root, struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_remove(bfdev_heap_root_t *root, bfdev_heap_node_t *node); /** * bfdev_heap_parent - find the parent node. @@ -124,44 +127,44 @@ bfdev_heap_remove(struct bfdev_heap_root *root, struct bfdev_heap_node *node); * @parentp: pointer used to modify the parent node pointer. * @node: new node to insert. */ -extern struct bfdev_heap_node ** -bfdev_heap_parent(struct bfdev_heap_root *root, struct bfdev_heap_node **parentp, - struct bfdev_heap_node *node); +extern bfdev_heap_node_t ** +bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, + bfdev_heap_node_t *node); /** * bfdev_heap_find - find @index in tree @root. * @root: heap tree want to search. * @index: index of node. */ -extern struct bfdev_heap_node * -bfdev_heap_find(struct bfdev_heap_root *root, unsigned int index); +extern bfdev_heap_node_t * +bfdev_heap_find(bfdev_heap_root_t *root, unsigned int index); /* Base iteration - basic iteration helper */ -extern struct bfdev_heap_node * -bfdev_heap_left_far(const struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_left_far(const bfdev_heap_node_t *node); -extern struct bfdev_heap_node * -bfdev_heap_right_far(const struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_right_far(const bfdev_heap_node_t *node); -extern struct bfdev_heap_node * -bfdev_heap_left_deep(const struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_left_deep(const bfdev_heap_node_t *node); -extern struct bfdev_heap_node * -bfdev_heap_right_deep(const struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_right_deep(const bfdev_heap_node_t *node); /* Level iteration (Sequential) - access in level sequence */ -extern struct bfdev_heap_node * -bfdev_heap_first(const struct bfdev_heap_root *root, unsigned long *index); +extern bfdev_heap_node_t * +bfdev_heap_first(const bfdev_heap_root_t *root, unsigned long *index); -extern struct bfdev_heap_node * -bfdev_heap_next(const struct bfdev_heap_root *root, unsigned long *index); +extern bfdev_heap_node_t * +bfdev_heap_next(const bfdev_heap_root_t *root, unsigned long *index); /* Postorder iteration (Depth-first) - always visit the parent after its children */ -extern struct bfdev_heap_node * -bfdev_heap_post_first(const struct bfdev_heap_root *root); +extern bfdev_heap_node_t * +bfdev_heap_post_first(const bfdev_heap_root_t *root); -extern struct bfdev_heap_node * -bfdev_heap_post_next(const struct bfdev_heap_node *node); +extern bfdev_heap_node_t * +bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_first_entry - get the preorder first element from a heap. @@ -182,7 +185,7 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_for_each - preorder iterate over a heap. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. * @root: the root for your heap. */ #define bfdev_heap_for_each(pos, index, root) \ @@ -191,14 +194,14 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_for_each_from - preorder iterate over a heap from the current point. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. */ #define bfdev_heap_for_each_from(pos, index, root) \ for (; pos; pos = bfdev_heap_next(root, index)) /** * bfdev_heap_for_each_continue - continue preorder iteration over a heap. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. */ #define bfdev_heap_for_each_continue(pos, index, root) \ for (pos = bfdev_heap_next(root, index); \ @@ -250,7 +253,7 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_post_for_each - postorder iterate over a heap. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. * @root: the root for your heap. */ #define bfdev_heap_post_for_each(pos, root) \ @@ -258,21 +261,21 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_post_for_each_from - postorder iterate over a heap from the current point. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. */ #define bfdev_heap_post_for_each_from(pos) \ for (; pos; pos = bfdev_heap_post_next(pos)) /** * bfdev_heap_post_for_each_continue - continue postorder iteration over a heap. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. */ #define bfdev_heap_post_for_each_continue(pos) \ for (pos = bfdev_heap_post_next(pos); pos; pos = bfdev_heap_post_next(pos)) /** * bfdev_heap_post_for_each_safe - postorder iterate over a heap safe against removal of heap entry. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. * @tmp: another bfdev_heap_node to use as temporary storage. * @root: the root for your heap. */ @@ -282,7 +285,7 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_post_for_each_safe_from - postorder iterate over a heap safe against removal of heap entry from the current point. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. * @tmp: another bfdev_heap_node to use as temporary storage. */ #define bfdev_heap_post_for_each_safe_from(pos, tmp) \ @@ -290,7 +293,7 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); /** * bfdev_heap_post_for_each_safe_continue - continue heap postorder iteration safe against removal. - * @pos: the &struct bfdev_heap_node to use as a loop cursor. + * @pos: the &bfdev_heap_node_t to use as a loop cursor. * @tmp: another bfdev_heap_node to use as temporary storage. */ #define bfdev_heap_post_for_each_safe_continue(pos, tmp) \ @@ -364,8 +367,8 @@ bfdev_heap_post_next(const struct bfdev_heap_node *node); * @link: point to pointer to child node. * @node: new node to link. */ -static inline void bfdev_heap_link(struct bfdev_heap_root *root, struct bfdev_heap_node *parent, - struct bfdev_heap_node **link, struct bfdev_heap_node *node) +static inline void bfdev_heap_link(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t **link, bfdev_heap_node_t *node) { #ifdef BFDEV_DEBUG_HEAP if (bfdev_unlikely(!bfdev_heap_check_link(parent, link, node))) @@ -387,8 +390,8 @@ static inline void bfdev_heap_link(struct bfdev_heap_root *root, struct bfdev_he * @node: new node to link. */ static inline void -bfdev_heap_insert_node(struct bfdev_heap_root *root, struct bfdev_heap_node *parent, - struct bfdev_heap_node **link, struct bfdev_heap_node *node, +bfdev_heap_insert_node(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t **link, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { bfdev_heap_link(root, parent, link, node); @@ -402,10 +405,10 @@ bfdev_heap_insert_node(struct bfdev_heap_root *root, struct bfdev_heap_node *par * @cmp: operator defining the node order. */ static inline void -bfdev_heap_insert(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_insert(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { - struct bfdev_heap_node *parent, **link; + bfdev_heap_node_t *parent, **link; link = bfdev_heap_parent(root, &parent, node); bfdev_heap_insert_node(root, parent, link, node, cmp, pdata); @@ -417,10 +420,10 @@ bfdev_heap_insert(struct bfdev_heap_root *root, struct bfdev_heap_node *node, * @node: node to delete. */ static inline void -bfdev_heap_delete(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_delete(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { - struct bfdev_heap_node *rebalance; + bfdev_heap_node_t *rebalance; #ifdef BFDEV_DEBUG_HEAP if (bfdev_unlikely(!bfdev_heap_check_delete(node))) diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 529c1fef..84c9f3c6 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -9,12 +9,12 @@ struct lfu_head { struct bfdev_cache_head cache; - struct bfdev_heap_root lfu; + bfdev_heap_root_t lfu; }; struct lfu_node { struct bfdev_cache_node cache; - struct bfdev_heap_node node; + bfdev_heap_node_t node; unsigned long count; }; @@ -28,8 +28,8 @@ struct lfu_node { bfdev_heap_entry(ptr, struct lfu_node, node) static long -lfu_compare(const struct bfdev_heap_node *node1, - const struct bfdev_heap_node *node2, void *pdata) +lfu_compare(const bfdev_heap_node_t *node1, + const bfdev_heap_node_t *node2, void *pdata) { struct lfu_node *lfu1, *lfu2; diff --git a/src/heap-debug.c b/src/heap-debug.c index b9025028..366eb1d4 100644 --- a/src/heap-debug.c +++ b/src/heap-debug.c @@ -12,8 +12,8 @@ #include export bool -bfdev_heap_check_link(struct bfdev_heap_node *parent, struct bfdev_heap_node **link, - struct bfdev_heap_node *node) +bfdev_heap_check_link(bfdev_heap_node_t *parent, bfdev_heap_node_t **link, + bfdev_heap_node_t *node) { if (bfdev_unlikely(*link == node)) { bfdev_log_err( @@ -28,7 +28,7 @@ bfdev_heap_check_link(struct bfdev_heap_node *parent, struct bfdev_heap_node **l } export bool -bfdev_heap_check_delete(struct bfdev_heap_node *node) +bfdev_heap_check_delete(bfdev_heap_node_t *node) { if (bfdev_unlikely(node->left == BFDEV_POISON_HPNODE1)) { bfdev_log_err( diff --git a/src/heap.c b/src/heap.c index dd977c65..3743a68e 100644 --- a/src/heap.c +++ b/src/heap.c @@ -9,11 +9,11 @@ #include static __bfdev_always_inline void -parent_swap(struct bfdev_heap_root *root, struct bfdev_heap_node *parent, - struct bfdev_heap_node *node) +parent_swap(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t *node) { - struct bfdev_heap_node *gparent = parent->parent; - struct bfdev_heap_node shadow = *node; + bfdev_heap_node_t *gparent = parent->parent; + bfdev_heap_node_t shadow = *node; if (node->left) node->left->parent = parent; @@ -42,10 +42,10 @@ parent_swap(struct bfdev_heap_root *root, struct bfdev_heap_node *parent, } export void -bfdev_heap_fixup(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_fixup(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { - struct bfdev_heap_node *parent; + bfdev_heap_node_t *parent; while ((parent = node->parent)) { if (cmp(node, parent, pdata) >= 0) @@ -55,11 +55,11 @@ bfdev_heap_fixup(struct bfdev_heap_root *root, struct bfdev_heap_node *node, } export void -bfdev_heap_erase(struct bfdev_heap_root *root, struct bfdev_heap_node *node, +bfdev_heap_erase(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { - struct bfdev_heap_node *successor = node->parent; - struct bfdev_heap_node *child1, *child2; + bfdev_heap_node_t *successor = node->parent; + bfdev_heap_node_t *child1, *child2; if (successor && cmp(node, successor, pdata) < 0) bfdev_heap_fixup(root, node, cmp, pdata); @@ -87,10 +87,10 @@ bfdev_heap_erase(struct bfdev_heap_root *root, struct bfdev_heap_node *node, } } -export struct bfdev_heap_node * -bfdev_heap_remove(struct bfdev_heap_root *root, struct bfdev_heap_node *node) +export bfdev_heap_node_t * +bfdev_heap_remove(bfdev_heap_root_t *root, bfdev_heap_node_t *node) { - struct bfdev_heap_node *successor = bfdev_heap_find(root, root->count); + bfdev_heap_node_t *successor = bfdev_heap_find(root, root->count); if (!successor->parent) { /* @@ -105,7 +105,7 @@ bfdev_heap_remove(struct bfdev_heap_root *root, struct bfdev_heap_node *node) root->node = NULL; successor = NULL; } else { - struct bfdev_heap_node *parent; + bfdev_heap_node_t *parent; parent = successor->parent; if (parent->left == successor) @@ -163,12 +163,12 @@ bfdev_heap_remove(struct bfdev_heap_root *root, struct bfdev_heap_node *node) return successor; } -export struct bfdev_heap_node ** -bfdev_heap_parent(struct bfdev_heap_root *root, struct bfdev_heap_node **parentp, - struct bfdev_heap_node *node) +export bfdev_heap_node_t ** +bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, + bfdev_heap_node_t *node) { unsigned int depth = bfdev_flsuf(root->count + 1); - struct bfdev_heap_node **link; + bfdev_heap_node_t **link; link = &root->node; if (bfdev_unlikely(!*link)) { @@ -187,11 +187,11 @@ bfdev_heap_parent(struct bfdev_heap_root *root, struct bfdev_heap_node **parentp return link; } -export struct bfdev_heap_node * -bfdev_heap_find(struct bfdev_heap_root *root, unsigned int index) +export bfdev_heap_node_t * +bfdev_heap_find(bfdev_heap_root_t *root, unsigned int index) { unsigned int depth = bfdev_flsuf(root->count); - struct bfdev_heap_node *node = root->node; + bfdev_heap_node_t *node = root->node; while (node && depth--) { if ((root->count) & BFDEV_BIT(depth)) @@ -205,17 +205,17 @@ bfdev_heap_find(struct bfdev_heap_root *root, unsigned int index) BFDEV_TITER_BASE_DEFINE( export, bfdev_heap, - struct bfdev_heap_node, left, right + bfdev_heap_node_t, left, right ) BFDEV_TITER_LEVELORDER_DEFINE( export, bfdev_heap, bfdev_heap, - struct bfdev_heap_root, node, - struct bfdev_heap_node, left, right + bfdev_heap_root_t, node, + bfdev_heap_node_t, left, right ) BFDEV_TITER_POSTORDER_DEFINE( export, bfdev_heap_post, bfdev_heap, - struct bfdev_heap_root, node, - struct bfdev_heap_node, parent, left, right + bfdev_heap_root_t, node, + bfdev_heap_node_t, parent, left, right ) From 3dbaa3f6163fec81a39fe48ae7bedb80c17e2e25 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 14:11:18 +0800 Subject: [PATCH 045/119] feat hlist: added initial typedef Signed-off-by: John Sanpe --- examples/hashmap/benchmark.c | 12 +++--- examples/hashmap/simple.c | 12 +++--- examples/hashtbl/simple.c | 2 +- examples/hlist/selftest.c | 4 +- examples/hlist/simple.c | 2 +- include/bfdev/cache.h | 4 +- include/bfdev/hashmap.h | 28 +++++++------- include/bfdev/hashtbl.h | 22 +++++------ include/bfdev/heap.h | 5 ++- include/bfdev/hlist.h | 73 +++++++++++++++++++----------------- src/hashmap.c | 36 +++++++++--------- src/hlist-debug.c | 14 +++---- 12 files changed, 109 insertions(+), 105 deletions(-) diff --git a/examples/hashmap/benchmark.c b/examples/hashmap/benchmark.c index 4be500ca..5887abc5 100644 --- a/examples/hashmap/benchmark.c +++ b/examples/hashmap/benchmark.c @@ -15,7 +15,7 @@ #define TEST_LOOP 1000000 struct test_node { - struct bfdev_hlist_node node; + bfdev_hlist_node_t node; unsigned long value; }; @@ -29,15 +29,15 @@ test_hash_key(const void *key, void *pdata) } static inline unsigned long -test_hash_node(const struct bfdev_hlist_node *node, void *pdata) +test_hash_node(const bfdev_hlist_node_t *node, void *pdata) { const struct test_node *tnode = node_to_test(node); return tnode->value; } static inline long -test_equal(const struct bfdev_hlist_node *node1, - const struct bfdev_hlist_node *nodeb, void *pdata) +test_equal(const bfdev_hlist_node_t *node1, + const bfdev_hlist_node_t *nodeb, void *pdata) { const struct test_node *tnode1 = node_to_test(node1); const struct test_node *tnodeb = node_to_test(nodeb); @@ -45,7 +45,7 @@ test_equal(const struct bfdev_hlist_node *node1, } static inline long -test_find(const struct bfdev_hlist_node *node, const void *key, void *pdata) +test_find(const bfdev_hlist_node_t *node, const void *key, void *pdata) { const struct test_node *tnode = node_to_test(node); return tnode->value - (unsigned long)key; @@ -62,7 +62,7 @@ test_ops = { int main(int argc, const char *argv[]) { struct test_node *nodes; - struct bfdev_hlist_node *hnode; + bfdev_hlist_node_t *hnode; unsigned long value; unsigned int count; void *block; diff --git a/examples/hashmap/simple.c b/examples/hashmap/simple.c index 4832672c..b6225d1a 100644 --- a/examples/hashmap/simple.c +++ b/examples/hashmap/simple.c @@ -12,7 +12,7 @@ #define TEST_LOOP 100 struct test_node { - struct bfdev_hlist_node node; + bfdev_hlist_node_t node; unsigned long value; }; @@ -26,15 +26,15 @@ hashmap_hash_key(const void *key, void *pdata) } static inline unsigned long -hashmap_hash_node(const struct bfdev_hlist_node *node, void *pdata) +hashmap_hash_node(const bfdev_hlist_node_t *node, void *pdata) { const struct test_node *tnode = node_to_test(node); return tnode->value; } static inline long -hashmap_equal(const struct bfdev_hlist_node *node1, - const struct bfdev_hlist_node *nodeb, void *pdata) +hashmap_equal(const bfdev_hlist_node_t *node1, + const bfdev_hlist_node_t *nodeb, void *pdata) { const struct test_node *tnode1 = node_to_test(node1); const struct test_node *tnodeb = node_to_test(nodeb); @@ -42,7 +42,7 @@ hashmap_equal(const struct bfdev_hlist_node *node1, } static inline long -hashmap_find(const struct bfdev_hlist_node *node, const void *key, void *pdata) +hashmap_find(const bfdev_hlist_node_t *node, const void *key, void *pdata) { const struct test_node *tnode = node_to_test(node); return tnode->value - (unsigned long)key; @@ -59,7 +59,7 @@ test_ops = { int main(int argc, const char *argv[]) { struct test_node *nodes, *find; - struct bfdev_hlist_node *hnode; + bfdev_hlist_node_t *hnode; unsigned long value; unsigned int count; int retval; diff --git a/examples/hashtbl/simple.c b/examples/hashtbl/simple.c index fb691b4a..c4d9c7b7 100644 --- a/examples/hashtbl/simple.c +++ b/examples/hashtbl/simple.c @@ -14,7 +14,7 @@ BFDEV_DEFINE_HASHTBL(test_table, TEST_SIZE); struct test_node { - struct bfdev_hlist_node node; + bfdev_hlist_node_t node; unsigned int value; }; diff --git a/examples/hlist/selftest.c b/examples/hlist/selftest.c index fc783503..79ac926d 100644 --- a/examples/hlist/selftest.c +++ b/examples/hlist/selftest.c @@ -11,7 +11,7 @@ #define TEST_LOOP 10 struct test_node { - struct bfdev_hlist_node list; + bfdev_hlist_node_t list; unsigned long num; }; @@ -26,7 +26,7 @@ static int bfdev_hlist_selftest(struct test_pdata *hdata) { struct test_node *node, *nnode, *tnode; - struct bfdev_hlist_node *list, *nlist, *tlist; + bfdev_hlist_node_t *list, *nlist, *tlist; unsigned int count; BFDEV_HLIST_HEAD(test_head); diff --git a/examples/hlist/simple.c b/examples/hlist/simple.c index c9f8e3e6..cbc6a50f 100644 --- a/examples/hlist/simple.c +++ b/examples/hlist/simple.c @@ -13,7 +13,7 @@ static BFDEV_HLIST_HEAD(demo_list); struct hlist_simple { - struct bfdev_hlist_node list; + bfdev_hlist_node_t list; unsigned int num; unsigned long data; }; diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index 4270d754..83ef42fd 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -43,7 +43,7 @@ enum bfdev_cache_status { }; struct bfdev_cache_node { - struct bfdev_hlist_node hash; + bfdev_hlist_node_t hash; bfdev_list_head_t list; enum bfdev_cache_status status; @@ -56,7 +56,7 @@ struct bfdev_cache_node { struct bfdev_cache_head { const struct bfdev_alloc *alloc; const struct bfdev_cache_algo *algo; - struct bfdev_hlist_head *taghash; + bfdev_hlist_head_t *taghash; struct bfdev_cache_node **nodes; bfdev_list_head_t using; diff --git a/include/bfdev/hashmap.h b/include/bfdev/hashmap.h index e1498127..e5eb6ccd 100644 --- a/include/bfdev/hashmap.h +++ b/include/bfdev/hashmap.h @@ -35,7 +35,7 @@ enum bfdev_hashmap_strategy { }; struct bfdev_hashmap { - struct bfdev_hlist_head *buckets; + bfdev_hlist_head_t *buckets; unsigned int bits; unsigned long capacity; unsigned long used; @@ -47,9 +47,9 @@ struct bfdev_hashmap { struct bfdev_hashmap_ops { unsigned long (*hash_key)(const void *key, void *pdata); - unsigned long (*hash_node)(const struct bfdev_hlist_node *node, void *pdata); - long (*equal)(const struct bfdev_hlist_node *node1, const struct bfdev_hlist_node *node2, void *pdata); - long (*find)(const struct bfdev_hlist_node *node, const void *key, void *pdata); + unsigned long (*hash_node)(const bfdev_hlist_node_t *node, void *pdata); + long (*equal)(const bfdev_hlist_node_t *node1, const bfdev_hlist_node_t *node2, void *pdata); + long (*find)(const bfdev_hlist_node_t *node, const void *key, void *pdata); bool (*extend)(const bfdev_hashmap_t *hashmap, void *pdata); bool (*shrink)(const bfdev_hashmap_t *hashmap, void *pdata); @@ -87,8 +87,8 @@ bfdev_hashmap_init(bfdev_hashmap_t *hashmap, const struct bfdev_alloc *alloc, * @strategy: insertion strategy. */ extern int -bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, - struct bfdev_hlist_node **old, enum bfdev_hashmap_strategy strategy); +bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, + bfdev_hlist_node_t **old, enum bfdev_hashmap_strategy strategy); /** * bfdev_hashmap_del() - delete a hashlist node from hashmap. @@ -98,14 +98,14 @@ bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, */ extern int bfdev_hashmap_del(bfdev_hashmap_t *hashmap, const void *key, - struct bfdev_hlist_node **node); + bfdev_hlist_node_t **node); /** * bfdev_hashmap_find() - find a hashlist node in hashmap. * @hashmap: hashmap structure to be find. * @key: key of the node to be find. */ -extern struct bfdev_hlist_node * +extern bfdev_hlist_node_t * bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key); /** @@ -116,27 +116,27 @@ extern void bfdev_hashmap_release(bfdev_hashmap_t *hashmap); static __bfdev_always_inline int -bfdev_hashmap_add(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node) +bfdev_hashmap_add(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node) { return bfdev_hashmap_insert(hashmap, node, NULL, BFDEV_HASHMAP_ADD); } static __bfdev_always_inline int -bfdev_hashmap_set(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, - struct bfdev_hlist_node **old) +bfdev_hashmap_set(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, + bfdev_hlist_node_t **old) { return bfdev_hashmap_insert(hashmap, node, old, BFDEV_HASHMAP_SET); } static __bfdev_always_inline int -bfdev_hashmap_update(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, - struct bfdev_hlist_node **old) +bfdev_hashmap_update(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, + bfdev_hlist_node_t **old) { return bfdev_hashmap_insert(hashmap, node, old, BFDEV_HASHMAP_UPDATE); } static __bfdev_always_inline int -bfdev_hashmap_append(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node) +bfdev_hashmap_append(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node) { return bfdev_hashmap_insert(hashmap, node, NULL, BFDEV_HASHMAP_APPEND); } diff --git a/include/bfdev/hashtbl.h b/include/bfdev/hashtbl.h index 724cfa1d..ba547977 100644 --- a/include/bfdev/hashtbl.h +++ b/include/bfdev/hashtbl.h @@ -18,7 +18,7 @@ BFDEV_BEGIN_DECLS (((size < 2) || (size & (size - 1))) ? -1 : size) #define BFDEV_DECLARE_HASHTBL(name, size) \ - struct bfdev_hlist_head name[BFDEV_HASHTBL_CHECK(size)] + bfdev_hlist_head_t name[BFDEV_HASHTBL_CHECK(size)] #define BFDEV_DEFINE_HASHTBL(name, size) \ BFDEV_DECLARE_HASHTBL(name, size) = { \ @@ -41,7 +41,7 @@ bfdev_hashtbl_index(unsigned long size, unsigned long key) * @hashtable: hashtable to be initialized. */ static inline void -bfdev_hashtbl_init(struct bfdev_hlist_head *head, unsigned long size) +bfdev_hashtbl_init(bfdev_hlist_head_t *head, unsigned long size) { unsigned long count; @@ -54,7 +54,7 @@ bfdev_hashtbl_init(struct bfdev_hlist_head *head, unsigned long size) * @hashtable: hashtable to check. */ static inline bool -bfdev_hashtbl_empty(struct bfdev_hlist_head *head, unsigned long size) +bfdev_hashtbl_empty(bfdev_hlist_head_t *head, unsigned long size) { unsigned long count; @@ -69,12 +69,12 @@ bfdev_hashtbl_empty(struct bfdev_hlist_head *head, unsigned long size) /** * bfdev_hashtbl_add - add an object to a hashtable. * @hashtbl: hashtable to add to. - * @node: the &struct bfdev_hlist_node of the object to be added. + * @node: the &bfdev_hlist_node_t of the object to be added. * @key: the value of the object to be added. */ static inline void -bfdev_hashtbl_add(struct bfdev_hlist_head *head, unsigned long size, - struct bfdev_hlist_node *node, unsigned long key) +bfdev_hashtbl_add(bfdev_hlist_head_t *head, unsigned long size, + bfdev_hlist_node_t *node, unsigned long key) { unsigned long index = bfdev_hashtbl_index(size, key); bfdev_hlist_head_add(&head[index], node); @@ -85,14 +85,14 @@ bfdev_hashtbl_add(struct bfdev_hlist_head *head, unsigned long size, * @node: &struct hlist_node of the object to remove. */ static inline void -bfdev_hashtbl_del(struct bfdev_hlist_node *node) +bfdev_hashtbl_del(bfdev_hlist_node_t *node) { bfdev_hlist_del_init(node); } /** * bfdev_hashtbl_for_each - iterate over a hashtable index. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @head: the head for your hashtable. * @size: the size of your hashtable. * @index: index to for each. @@ -102,7 +102,7 @@ bfdev_hashtbl_del(struct bfdev_hlist_node *node) /** * bfdev_hashtbl_for_each_safe - iterate over a hashtable index safe against removal of hlist entry. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @tmp: another bfdev_hlist_node to use as temporary storage. * @head: the head for your hashtable. * @size: the size of your hashtable. @@ -136,7 +136,7 @@ bfdev_hashtbl_del(struct bfdev_hlist_node *node) /** * bfdev_hashtbl_for_each - iterate over a hashtable. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @head: the head for your hashtable. * @size: the size of your hashtable. * @index: index temporary storage. @@ -147,7 +147,7 @@ bfdev_hashtbl_del(struct bfdev_hlist_node *node) /** * bfdev_hashtbl_for_each_safe - iterate over a hashtable safe against removal of hlist entry. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @tmp: another bfdev_hlist_node to use as temporary storage. * @head: the head for your hashtable. * @size: the size of your hashtable. diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index c0fc8e87..11989915 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -367,8 +367,9 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); * @link: point to pointer to child node. * @node: new node to link. */ -static inline void bfdev_heap_link(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, - bfdev_heap_node_t **link, bfdev_heap_node_t *node) +static inline void +bfdev_heap_link(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t **link, bfdev_heap_node_t *node) { #ifdef BFDEV_DEBUG_HEAP if (bfdev_unlikely(!bfdev_heap_check_link(parent, link, node))) diff --git a/include/bfdev/hlist.h b/include/bfdev/hlist.h index 95952963..aed875fa 100644 --- a/include/bfdev/hlist.h +++ b/include/bfdev/hlist.h @@ -14,29 +14,32 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_hlist_head bfdev_hlist_head_t; +typedef struct bfdev_hlist_node bfdev_hlist_node_t; + struct bfdev_hlist_node { - struct bfdev_hlist_node *next; - struct bfdev_hlist_node **pprev; + bfdev_hlist_node_t *next; + bfdev_hlist_node_t **pprev; }; struct bfdev_hlist_head { - struct bfdev_hlist_node *node; + bfdev_hlist_node_t *node; }; #define BFDEV_HLIST_HEAD_STATIC \ {NULL} #define BFDEV_HLIST_HEAD_INIT \ - (struct bfdev_hlist_head) BFDEV_HLIST_HEAD_STATIC + (bfdev_hlist_head_t) BFDEV_HLIST_HEAD_STATIC #define BFDEV_HLIST_HEAD(name) \ - struct bfdev_hlist_head name = BFDEV_HLIST_HEAD_INIT + bfdev_hlist_head_t name = BFDEV_HLIST_HEAD_INIT #ifdef BFDEV_DEBUG_HLIST -extern bool bfdev_hlist_check_head_add(struct bfdev_hlist_head *head, struct bfdev_hlist_node *newn); -extern bool bfdev_hlist_check_next_add(struct bfdev_hlist_node *next, struct bfdev_hlist_node *newn); -extern bool bfdev_hlist_check_prev_add(struct bfdev_hlist_node *prev, struct bfdev_hlist_node *newn); -extern bool bfdev_hlist_check_del(struct bfdev_hlist_node *node); +extern bool bfdev_hlist_check_head_add(bfdev_hlist_head_t *head, bfdev_hlist_node_t *newn); +extern bool bfdev_hlist_check_next_add(bfdev_hlist_node_t *next, bfdev_hlist_node_t *newn); +extern bool bfdev_hlist_check_prev_add(bfdev_hlist_node_t *prev, bfdev_hlist_node_t *newn); +extern bool bfdev_hlist_check_del(bfdev_hlist_node_t *node); #endif /** @@ -44,7 +47,7 @@ extern bool bfdev_hlist_check_del(struct bfdev_hlist_node *node); * @head: hlist_head structure to be initialized. */ static inline void -bfdev_hlist_head_init(struct bfdev_hlist_head *head) +bfdev_hlist_head_init(bfdev_hlist_head_t *head) { head->node = NULL; } @@ -54,7 +57,7 @@ bfdev_hlist_head_init(struct bfdev_hlist_head *head) * @node: bfdev_hlist_node structure to be initialized. */ static inline void -bfdev_hlist_node_init(struct bfdev_hlist_node *node) +bfdev_hlist_node_init(bfdev_hlist_node_t *node) { node->pprev = NULL; node->next = NULL; @@ -66,7 +69,7 @@ bfdev_hlist_node_init(struct bfdev_hlist_node *node) * @newn: new entry to be added. */ static inline void -bfdev_hlist_head_add(struct bfdev_hlist_head *head, struct bfdev_hlist_node *newn) +bfdev_hlist_head_add(bfdev_hlist_head_t *head, bfdev_hlist_node_t *newn) { #ifdef BFDEV_DEBUG_HLIST if (bfdev_unlikely(!bfdev_hlist_check_head_add(head, newn))) @@ -87,7 +90,7 @@ bfdev_hlist_head_add(struct bfdev_hlist_head *head, struct bfdev_hlist_node *new * @newn: new entry to be added. */ static inline void -bfdev_hlist_next_add(struct bfdev_hlist_node *node, struct bfdev_hlist_node *newn) +bfdev_hlist_next_add(bfdev_hlist_node_t *node, bfdev_hlist_node_t *newn) { #ifdef BFDEV_DEBUG_HLIST if (bfdev_unlikely(!bfdev_hlist_check_next_add(node, newn))) @@ -108,7 +111,7 @@ bfdev_hlist_next_add(struct bfdev_hlist_node *node, struct bfdev_hlist_node *new * @newn: new entry to be added. */ static inline void -bfdev_hlist_prev_add(struct bfdev_hlist_node *node, struct bfdev_hlist_node *newn) +bfdev_hlist_prev_add(bfdev_hlist_node_t *node, bfdev_hlist_node_t *newn) { #ifdef BFDEV_DEBUG_HLIST if (bfdev_unlikely(!bfdev_hlist_check_prev_add(node, newn))) @@ -126,10 +129,10 @@ bfdev_hlist_prev_add(struct bfdev_hlist_node *node, struct bfdev_hlist_node *new * @node: the element to delete from the hlist. */ static inline void -bfdev_hlist_deluf(struct bfdev_hlist_node *node) +bfdev_hlist_deluf(bfdev_hlist_node_t *node) { - struct bfdev_hlist_node **pprev = node->pprev; - struct bfdev_hlist_node *next = node->next; + bfdev_hlist_node_t **pprev = node->pprev; + bfdev_hlist_node_t *next = node->next; if (next) next->pprev = pprev; @@ -141,7 +144,7 @@ bfdev_hlist_deluf(struct bfdev_hlist_node *node) * @node: the element to delete from the hlist. */ static inline void -bfdev_hlist_del(struct bfdev_hlist_node *node) +bfdev_hlist_del(bfdev_hlist_node_t *node) { #ifdef BFDEV_DEBUG_HLIST if (bfdev_unlikely(!bfdev_hlist_check_del(node))) @@ -158,7 +161,7 @@ bfdev_hlist_del(struct bfdev_hlist_node *node) * @head: hlist head to check. */ static inline bool -bfdev_hlist_check_empty(const struct bfdev_hlist_head *head) +bfdev_hlist_check_empty(const bfdev_hlist_head_t *head) { return !head->node; } @@ -169,8 +172,8 @@ bfdev_hlist_check_empty(const struct bfdev_hlist_head *head) * @node: the entry to test. */ static inline bool -bfdev_hlist_check_first(const struct bfdev_hlist_head *head, - const struct bfdev_hlist_node *node) +bfdev_hlist_check_first(const bfdev_hlist_head_t *head, + const bfdev_hlist_node_t *node) { return head->node == node; } @@ -180,7 +183,7 @@ bfdev_hlist_check_first(const struct bfdev_hlist_head *head, * @node: the entry to test. */ static inline bool -bfdev_hlist_check_end(const struct bfdev_hlist_node *node) +bfdev_hlist_check_end(const bfdev_hlist_node_t *node) { return !node->next; } @@ -191,8 +194,8 @@ bfdev_hlist_check_end(const struct bfdev_hlist_node *node) * @node: the unique node. */ static inline bool -bfdev_hlist_check_another(const struct bfdev_hlist_head *head, - const struct bfdev_hlist_node *node) +bfdev_hlist_check_another(const bfdev_hlist_head_t *head, + const bfdev_hlist_node_t *node) { return head->node == node && node->next == NULL; } @@ -202,7 +205,7 @@ bfdev_hlist_check_another(const struct bfdev_hlist_head *head, * @node: hlist node to check. */ static inline bool -bfdev_hlist_check_unhashed(const struct bfdev_hlist_node *node) +bfdev_hlist_check_unhashed(const bfdev_hlist_node_t *node) { return !node->pprev; } @@ -213,7 +216,7 @@ bfdev_hlist_check_unhashed(const struct bfdev_hlist_node *node) * @newn: bfdev_hlist_head for new hlist. */ static inline void -bfdev_hlist_head_replace(struct bfdev_hlist_head *oldn, struct bfdev_hlist_head *newn) +bfdev_hlist_head_replace(bfdev_hlist_head_t *oldn, bfdev_hlist_head_t *newn) { newn->node = oldn->node; oldn->node = NULL; @@ -227,7 +230,7 @@ bfdev_hlist_head_replace(struct bfdev_hlist_head *oldn, struct bfdev_hlist_head * @newn: bfdev_hlist_head for new hlist. */ static inline void -bfdev_hlist_replace(struct bfdev_hlist_node *oldn, struct bfdev_hlist_node *newn) +bfdev_hlist_replace(bfdev_hlist_node_t *oldn, bfdev_hlist_node_t *newn) { newn->next = oldn->next; newn->pprev = oldn->pprev; @@ -241,7 +244,7 @@ bfdev_hlist_replace(struct bfdev_hlist_node *oldn, struct bfdev_hlist_node *newn * @node: the element to delete from the hlist. */ static inline void -bfdev_hlist_del_init(struct bfdev_hlist_node *node) +bfdev_hlist_del_init(bfdev_hlist_node_t *node) { bfdev_hlist_deluf(node); bfdev_hlist_node_init(node); @@ -249,7 +252,7 @@ bfdev_hlist_del_init(struct bfdev_hlist_node *node) /** * bfdev_hlist_entry - get the struct for this entry. - * @ptr: the &struct bfdev_hlist_node pointer. + * @ptr: the &bfdev_hlist_node_t pointer. * @type: the type of the struct this is embedded in. * @member: the name of the hlist_head within the struct. */ @@ -275,7 +278,7 @@ bfdev_hlist_del_init(struct bfdev_hlist_node *node) /** * bfdev_hlist_for_each - iterate over a hlist. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @head: the head for your hlist. */ #define bfdev_hlist_for_each(pos, head) \ @@ -283,21 +286,21 @@ bfdev_hlist_del_init(struct bfdev_hlist_node *node) /** * bfdev_hlist_for_each_from - iterate over a hlist from the current point. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. */ #define bfdev_hlist_for_each_from(pos) \ for (; (pos); (pos) = (pos)->next) /** * bfdev_hlist_for_each_continue - continue iteration over a hlist. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. */ #define bfdev_hlist_for_each_continue(pos) \ for ((void)((pos) && ((pos) = (pos)->next)); (pos); (pos) = (pos)->next) /** * bfdev_hlist_for_each_safe - iterate over a hlist safe against removal of hlist entry. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @tmp: another bfdev_hlist_node to use as temporary storage. * @head: the head for your hlist. */ @@ -307,7 +310,7 @@ bfdev_hlist_del_init(struct bfdev_hlist_node *node) /** * bfdev_hlist_for_each_from_safe - iterate over a hlist safe against removal of hlist entry from the current point. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @tmp: another bfdev_hlist_node to use as temporary storage. */ #define bfdev_hlist_for_each_from_safe(pos, tmp) \ @@ -316,7 +319,7 @@ bfdev_hlist_del_init(struct bfdev_hlist_node *node) /** * bfdev_hlist_for_each_continue_safe - continue hlist iteration safe against removal. - * @pos: the &struct bfdev_hlist_node to use as a loop cursor. + * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @tmp: another bfdev_hlist_node to use as temporary storage. */ #define bfdev_hlist_for_each_continue_safe(pos, tmp) \ diff --git a/src/hashmap.c b/src/hashmap.c index 0a0e84c4..6d98bc8f 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -9,7 +9,7 @@ static __bfdev_always_inline unsigned long hashmap_hash_node(bfdev_hashmap_t *hashmap, - const struct bfdev_hlist_node *node) + const bfdev_hlist_node_t *node) { const bfdev_hashmap_ops_t *ops; unsigned long retval; @@ -34,8 +34,8 @@ hashmap_hash_key(const bfdev_hashmap_t *hashmap, } static __bfdev_always_inline long -hashmap_equal(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node1, - const struct bfdev_hlist_node *node2) +hashmap_equal(bfdev_hashmap_t *hashmap, const bfdev_hlist_node_t *node1, + const bfdev_hlist_node_t *node2) { const bfdev_hashmap_ops_t *ops; long retval; @@ -48,7 +48,7 @@ hashmap_equal(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node1, static __bfdev_always_inline long hashmap_find(bfdev_hashmap_t *hashmap, const void *key, - const struct bfdev_hlist_node *node) + const bfdev_hlist_node_t *node) { const bfdev_hashmap_ops_t *ops; long retval; @@ -88,11 +88,11 @@ hashmap_need_shrink(bfdev_hashmap_t *hashmap) return hashmap->used * 4 < hashmap->capacity; } -static inline struct bfdev_hlist_node * -hashmap_find_node(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node, +static inline bfdev_hlist_node_t * +hashmap_find_node(bfdev_hashmap_t *hashmap, const bfdev_hlist_node_t *node, unsigned long hash) { - struct bfdev_hlist_node *walk; + bfdev_hlist_node_t *walk; unsigned long index; if (!hashmap->buckets) @@ -107,11 +107,11 @@ hashmap_find_node(bfdev_hashmap_t *hashmap, const struct bfdev_hlist_node *node, return NULL; } -static inline struct bfdev_hlist_node * +static inline bfdev_hlist_node_t * hashmap_find_key(bfdev_hashmap_t *hashmap, const void *key, unsigned long hash) { - struct bfdev_hlist_node *walk; + bfdev_hlist_node_t *walk; unsigned long index; if (!hashmap->buckets) @@ -130,8 +130,8 @@ static inline int hashmap_rehash(bfdev_hashmap_t *hashmap, unsigned int nbits) { const struct bfdev_alloc *alloc = hashmap->alloc; - struct bfdev_hlist_node *walk, *tmp; - struct bfdev_hlist_head *nbuckets; + bfdev_hlist_node_t *walk, *tmp; + bfdev_hlist_head_t *nbuckets; unsigned long value, index, ncapacity; ncapacity = BFDEV_BIT(nbits); @@ -180,10 +180,10 @@ hashmap_shrink(bfdev_hashmap_t *hashmap) } export int -bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, - struct bfdev_hlist_node **old, enum bfdev_hashmap_strategy strategy) +bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, + bfdev_hlist_node_t **old, enum bfdev_hashmap_strategy strategy) { - struct bfdev_hlist_node *exist; + bfdev_hlist_node_t *exist; unsigned long value; int retval; @@ -219,9 +219,9 @@ bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, struct bfdev_hlist_node *node, export int bfdev_hashmap_del(bfdev_hashmap_t *hashmap, const void *key, - struct bfdev_hlist_node **node) + bfdev_hlist_node_t **node) { - struct bfdev_hlist_node *exist; + bfdev_hlist_node_t *exist; unsigned long value; value = hashmap_hash_key(hashmap, key); @@ -240,10 +240,10 @@ bfdev_hashmap_del(bfdev_hashmap_t *hashmap, const void *key, return -BFDEV_ENOERR; } -export struct bfdev_hlist_node * +export bfdev_hlist_node_t * bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key) { - struct bfdev_hlist_node *exist; + bfdev_hlist_node_t *exist; unsigned long value; value = hashmap_hash_key(hashmap, key); diff --git a/src/hlist-debug.c b/src/hlist-debug.c index 9867ba1e..6121d26f 100644 --- a/src/hlist-debug.c +++ b/src/hlist-debug.c @@ -12,8 +12,8 @@ #include export bool -bfdev_hlist_check_head_add(struct bfdev_hlist_head *head, - struct bfdev_hlist_node *newn) +bfdev_hlist_check_head_add(bfdev_hlist_head_t *head, + bfdev_hlist_node_t *newn) { if (bfdev_unlikely(head->node == newn)) { bfdev_log_err( @@ -27,8 +27,8 @@ bfdev_hlist_check_head_add(struct bfdev_hlist_head *head, } export bool -bfdev_hlist_check_next_add(struct bfdev_hlist_node *next, - struct bfdev_hlist_node *newn) +bfdev_hlist_check_next_add(bfdev_hlist_node_t *next, + bfdev_hlist_node_t *newn) { if (bfdev_unlikely(next->next == newn)) { bfdev_log_err( @@ -43,8 +43,8 @@ bfdev_hlist_check_next_add(struct bfdev_hlist_node *next, } export bool -bfdev_hlist_check_prev_add(struct bfdev_hlist_node *prev, - struct bfdev_hlist_node *newn) +bfdev_hlist_check_prev_add(bfdev_hlist_node_t *prev, + bfdev_hlist_node_t *newn) { if (bfdev_unlikely(prev->pprev == &newn->next)) { bfdev_log_err( @@ -59,7 +59,7 @@ bfdev_hlist_check_prev_add(struct bfdev_hlist_node *prev, } export bool -bfdev_hlist_check_del(struct bfdev_hlist_node *node) +bfdev_hlist_check_del(bfdev_hlist_node_t *node) { if (bfdev_unlikely(node->next == BFDEV_POISON_HLIST1)) { bfdev_log_err( From 33e57f0a727437e24f59adbaeeaf82e2593f73cd Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 16:55:43 +0800 Subject: [PATCH 046/119] feat cache: added initial typedef Signed-off-by: John Sanpe --- examples/cache/simple.c | 4 +-- include/bfdev/cache.h | 72 ++++++++++++++++++++++------------------- include/bfdev/respool.h | 36 ++++++++++----------- src/cache/cache.c | 68 +++++++++++++++++++------------------- src/cache/lfu.c | 30 ++++++++--------- src/cache/lru.c | 26 +++++++-------- src/respool.c | 28 ++++++++-------- 7 files changed, 134 insertions(+), 130 deletions(-) diff --git a/examples/cache/simple.c b/examples/cache/simple.c index f58971b4..4b995c9c 100644 --- a/examples/cache/simple.c +++ b/examples/cache/simple.c @@ -19,7 +19,7 @@ int main(int argc, const char *argv[]) { - struct bfdev_cache_head *cache; + bfdev_cache_head_t *cache; unsigned int count; cache = bfdev_cache_create("lfu", NULL, TEST_SIZE, 1); @@ -28,7 +28,7 @@ int main(int argc, const char *argv[]) srand(time(NULL)); for (count = 0; count < TEST_LOOP; ++count) { - struct bfdev_cache_node *node; + bfdev_cache_node_t *node; unsigned int value, verify; value = (unsigned int)rand() % TEST_MASK; diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index 83ef42fd..1f0ca569 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -19,6 +19,10 @@ BFDEV_BEGIN_DECLS # define BFDEV_CACHE_FREE_TAG ULONG_MAX #endif +typedef struct bfdev_cache_head bfdev_cache_head_t; +typedef struct bfdev_cache_node bfdev_cache_node_t; +typedef struct bfdev_cache_algo bfdev_cache_algo_t; + enum bfdev_cache_obtain { __BFDEV_CACHE_CHANGE = 0, __BFDEV_CACHE_UNCOMMITTED, @@ -42,7 +46,7 @@ enum bfdev_cache_status { BFDEV_CACHE_MANAGED, }; -struct bfdev_cache_node { +struct bfdev_cache_node { bfdev_hlist_node_t hash; bfdev_list_head_t list; enum bfdev_cache_status status; @@ -55,9 +59,9 @@ struct bfdev_cache_node { struct bfdev_cache_head { const struct bfdev_alloc *alloc; - const struct bfdev_cache_algo *algo; + const bfdev_cache_algo_t *algo; bfdev_hlist_head_t *taghash; - struct bfdev_cache_node **nodes; + bfdev_cache_node_t **nodes; bfdev_list_head_t using; bfdev_list_head_t freed; @@ -83,85 +87,85 @@ struct bfdev_cache_algo { bfdev_list_head_t list; const char *name; - bool (*starving)(struct bfdev_cache_head *head); - struct bfdev_cache_node *(*obtain)(struct bfdev_cache_head *head); - void (*get)(struct bfdev_cache_head *head, struct bfdev_cache_node *node); - void (*put)(struct bfdev_cache_head *head, struct bfdev_cache_node *node); - void (*update)(struct bfdev_cache_head *head, struct bfdev_cache_node *node); - void (*clear)(struct bfdev_cache_head *head, struct bfdev_cache_node *node); - void (*reset)(struct bfdev_cache_head *head); + bool (*starving)(bfdev_cache_head_t *head); + bfdev_cache_node_t *(*obtain)(bfdev_cache_head_t *head); + void (*get)(bfdev_cache_head_t *head, bfdev_cache_node_t *node); + void (*put)(bfdev_cache_head_t *head, bfdev_cache_node_t *node); + void (*update)(bfdev_cache_head_t *head, bfdev_cache_node_t *node); + void (*clear)(bfdev_cache_head_t *head, bfdev_cache_node_t *node); + void (*reset)(bfdev_cache_head_t *head); - struct bfdev_cache_head *(*create)(const struct bfdev_alloc *alloc, unsigned long size); - void (*destroy)(struct bfdev_cache_head *head); + bfdev_cache_head_t *(*create)(const struct bfdev_alloc *alloc, unsigned long size); + void (*destroy)(bfdev_cache_head_t *head); }; BFDEV_BITFLAGS_STRUCT(bfdev_cache, - struct bfdev_cache_head, flags + bfdev_cache_head_t, flags ); BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_cache, - struct bfdev_cache_head, flags, + bfdev_cache_head_t, flags, dirty, __BFDEV_CACHE_DIRTY ); BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_cache, - struct bfdev_cache_head, flags, + bfdev_cache_head_t, flags, starving, __BFDEV_CACHE_STARVING ); -extern struct bfdev_cache_node * -bfdev_cache_find(struct bfdev_cache_head *head, unsigned long tag); +extern bfdev_cache_node_t * +bfdev_cache_find(bfdev_cache_head_t *head, unsigned long tag); -extern struct bfdev_cache_node * -bfdev_cache_obtain(struct bfdev_cache_head *head, unsigned long tag, +extern bfdev_cache_node_t * +bfdev_cache_obtain(bfdev_cache_head_t *head, unsigned long tag, unsigned long flags); extern unsigned long -bfdev_cache_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node); +bfdev_cache_put(bfdev_cache_head_t *head, bfdev_cache_node_t *node); extern int -bfdev_cache_del(struct bfdev_cache_head *head, struct bfdev_cache_node *node); +bfdev_cache_del(bfdev_cache_head_t *head, bfdev_cache_node_t *node); extern int -bfdev_cache_set(struct bfdev_cache_head *head, struct bfdev_cache_node *node, +bfdev_cache_set(bfdev_cache_head_t *head, bfdev_cache_node_t *node, unsigned long tag); extern void -bfdev_cache_committed(struct bfdev_cache_head *head); +bfdev_cache_committed(bfdev_cache_head_t *head); extern void -bfdev_cache_reset(struct bfdev_cache_head *head); +bfdev_cache_reset(bfdev_cache_head_t *head); -extern struct bfdev_cache_head * +extern bfdev_cache_head_t * bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, unsigned long size, unsigned long maxp); extern void -bfdev_cache_destroy(struct bfdev_cache_head *head); +bfdev_cache_destroy(bfdev_cache_head_t *head); -static inline struct bfdev_cache_node * -bfdev_cache_get(struct bfdev_cache_head *head, unsigned long tag) +static inline bfdev_cache_node_t * +bfdev_cache_get(bfdev_cache_head_t *head, unsigned long tag) { return bfdev_cache_obtain(head, tag, BFDEV_CACHE_CHANGE); } -static inline struct bfdev_cache_node * -bfdev_cache_try_get(struct bfdev_cache_head *head, unsigned long tag) +static inline bfdev_cache_node_t * +bfdev_cache_try_get(bfdev_cache_head_t *head, unsigned long tag) { return bfdev_cache_obtain(head, tag, 0); } -static inline struct bfdev_cache_node * -bfdev_cache_cumulative(struct bfdev_cache_head *head, unsigned long tag) +static inline bfdev_cache_node_t * +bfdev_cache_cumulative(bfdev_cache_head_t *head, unsigned long tag) { return bfdev_cache_obtain(head, tag, BFDEV_CACHE_CHANGE | BFDEV_CACHE_UNCOMMITTED); } extern int -bfdev_cache_register(struct bfdev_cache_algo *algo); +bfdev_cache_register(bfdev_cache_algo_t *algo); extern void -bfdev_cache_unregister(struct bfdev_cache_algo *algo); +bfdev_cache_unregister(bfdev_cache_algo_t *algo); BFDEV_END_DECLS diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h index f393ffa5..15bf030a 100755 --- a/include/bfdev/respool.h +++ b/include/bfdev/respool.h @@ -11,14 +11,14 @@ BFDEV_BEGIN_DECLS -struct bfdev_resnode; -struct bfdev_respool; +typedef struct bfdev_resnode bfdev_resnode_t; +typedef struct bfdev_respool bfdev_respool_t; typedef void (*bfdev_respool_release_t) -(struct bfdev_respool *pool, struct bfdev_resnode *res); +(bfdev_respool_t *pool, bfdev_resnode_t *res); typedef long (*bfdev_respool_find_t) -(struct bfdev_respool *pool, struct bfdev_resnode *res, const void *data); +(bfdev_respool_t *pool, bfdev_resnode_t *res, const void *data); struct bfdev_resnode { const char *name; @@ -37,47 +37,47 @@ struct bfdev_respool { } #define BFDEV_RESPOOL_INIT(name) \ - (struct bfdev_respool) BFDEV_RESPOOL_STATIC(name) + (bfdev_respool_t) BFDEV_RESPOOL_STATIC(name) #define BFDEV_DEFINE_RESPOOL(name) \ - struct bfdev_respool name = BFDEV_RESPOOL_INIT(name) + bfdev_respool_t name = BFDEV_RESPOOL_INIT(name) static inline void -bfdev_respool_init(struct bfdev_respool *pool, const char *name) +bfdev_respool_init(bfdev_respool_t *pool, const char *name) { pool->name = name; bfdev_list_head_init(&pool->node); } static inline bool -bfdev_respool_check_empty(struct bfdev_respool *pool) +bfdev_respool_check_empty(bfdev_respool_t *pool) { return bfdev_list_check_empty(&pool->node); } -extern struct bfdev_resnode * -bfdev_respool_find(struct bfdev_respool *pool, +extern bfdev_resnode_t * +bfdev_respool_find(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data); extern void -bfdev_respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *res); +bfdev_respool_insert(bfdev_respool_t *pool, bfdev_resnode_t *res); extern void -bfdev_respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *res); +bfdev_respool_remove(bfdev_respool_t *pool, bfdev_resnode_t *res); extern void -bfdev_respool_release(struct bfdev_respool *pool, struct bfdev_resnode *res); +bfdev_respool_release(bfdev_respool_t *pool, bfdev_resnode_t *res); -extern struct bfdev_resnode * -bfdev_respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, +extern bfdev_resnode_t * +bfdev_respool_find_remove(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data); -extern struct bfdev_resnode * -bfdev_respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, +extern bfdev_resnode_t * +bfdev_respool_find_release(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data); extern void -bfdev_respool_release_all(struct bfdev_respool *pool); +bfdev_respool_release_all(bfdev_respool_t *pool); BFDEV_END_DECLS diff --git a/src/cache/cache.c b/src/cache/cache.c index fdaeb79f..de137f09 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -11,10 +11,10 @@ static BFDEV_LIST_HEAD(cache_algorithms); -static struct bfdev_cache_algo * +static bfdev_cache_algo_t * cache_algorithm_find(const char *name) { - struct bfdev_cache_algo *algo; + bfdev_cache_algo_t *algo; bfdev_list_for_each_entry(algo, &cache_algorithms, list) { if (!strcmp(algo->name, name)) @@ -25,17 +25,17 @@ cache_algorithm_find(const char *name) } static __bfdev_always_inline bool -cache_starving(struct bfdev_cache_head *head) +cache_starving(bfdev_cache_head_t *head) { return bfdev_list_check_empty(&head->freed) && head->algo->starving(head); } /* Find in changing, using or algos */ -static struct bfdev_cache_node * -cache_find(struct bfdev_cache_head *head, unsigned long tag, bool change) +static bfdev_cache_node_t * +cache_find(bfdev_cache_head_t *head, unsigned long tag, bool change) { - struct bfdev_cache_node *walk; + bfdev_cache_node_t *walk; unsigned long index; index = bfdev_hashtbl_index(head->size, tag); @@ -51,11 +51,11 @@ cache_find(struct bfdev_cache_head *head, unsigned long tag, bool change) } /* Obtain in freed or algos */ -static struct bfdev_cache_node * -cache_obtain(struct bfdev_cache_head *head, unsigned long tag) +static bfdev_cache_node_t * +cache_obtain(bfdev_cache_head_t *head, unsigned long tag) { - const struct bfdev_cache_algo *algo; - struct bfdev_cache_node *node; + const bfdev_cache_algo_t *algo; + bfdev_cache_node_t *node; algo = head->algo; if (bfdev_list_check_empty(&head->freed)) { @@ -65,7 +65,7 @@ cache_obtain(struct bfdev_cache_head *head, unsigned long tag) } else { /* Get form freed */ node = bfdev_list_first_entry( - &head->freed, struct bfdev_cache_node, list + &head->freed, bfdev_cache_node_t, list ); } @@ -81,18 +81,18 @@ cache_obtain(struct bfdev_cache_head *head, unsigned long tag) return node; } -export struct bfdev_cache_node * -bfdev_cache_find(struct bfdev_cache_head *head, unsigned long tag) +export bfdev_cache_node_t * +bfdev_cache_find(bfdev_cache_head_t *head, unsigned long tag) { return cache_find(head, tag, false); } -export struct bfdev_cache_node * -bfdev_cache_obtain(struct bfdev_cache_head *head, unsigned long tag, +export bfdev_cache_node_t * +bfdev_cache_obtain(bfdev_cache_head_t *head, unsigned long tag, unsigned long flags) { - const struct bfdev_cache_algo *algo; - struct bfdev_cache_node *node; + const bfdev_cache_algo_t *algo; + bfdev_cache_node_t *node; if (bfdev_unlikely(tag == BFDEV_CACHE_FREE_TAG)) return NULL; @@ -154,7 +154,7 @@ bfdev_cache_obtain(struct bfdev_cache_head *head, unsigned long tag, } export unsigned long -bfdev_cache_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +bfdev_cache_put(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { if (bfdev_unlikely(node->status != BFDEV_CACHE_USING)) return -BFDEV_EINVAL; @@ -172,10 +172,10 @@ bfdev_cache_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } export int -bfdev_cache_set(struct bfdev_cache_head *head, struct bfdev_cache_node *node, +bfdev_cache_set(bfdev_cache_head_t *head, bfdev_cache_node_t *node, unsigned long tag) { - const struct bfdev_cache_algo *algo; + const bfdev_cache_algo_t *algo; if (bfdev_unlikely(tag == BFDEV_CACHE_FREE_TAG)) return -BFDEV_EINVAL; @@ -198,9 +198,9 @@ bfdev_cache_set(struct bfdev_cache_head *head, struct bfdev_cache_node *node, } export int -bfdev_cache_del(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +bfdev_cache_del(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { - const struct bfdev_cache_algo *algo; + const bfdev_cache_algo_t *algo; if (bfdev_unlikely(node->status != BFDEV_CACHE_MANAGED)) return -BFDEV_EBUSY; @@ -217,9 +217,9 @@ bfdev_cache_del(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } export void -bfdev_cache_committed(struct bfdev_cache_head *head) +bfdev_cache_committed(bfdev_cache_head_t *head) { - struct bfdev_cache_node *node, *tmp; + bfdev_cache_node_t *node, *tmp; bfdev_list_for_each_entry_safe(node, tmp, &head->changing, list) { bfdev_list_move(&head->using, &node->list); @@ -231,9 +231,9 @@ bfdev_cache_committed(struct bfdev_cache_head *head) } export void -bfdev_cache_reset(struct bfdev_cache_head *head) +bfdev_cache_reset(bfdev_cache_head_t *head) { - struct bfdev_cache_node *node; + bfdev_cache_node_t *node; unsigned long count; head->flags = 0; @@ -260,12 +260,12 @@ bfdev_cache_reset(struct bfdev_cache_head *head) } } -export struct bfdev_cache_head * +export bfdev_cache_head_t * bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, unsigned long size, unsigned long maxpend) { - const struct bfdev_cache_algo *algo; - struct bfdev_cache_head *head; + const bfdev_cache_algo_t *algo; + bfdev_cache_head_t *head; unsigned long count; size = bfdev_pow2_roundup(size); @@ -296,7 +296,7 @@ bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, bfdev_list_head_init(&head->changing); for (count = 0; count < size; ++count) { - struct bfdev_cache_node *node; + bfdev_cache_node_t *node; node = head->nodes[count]; node->index = count; @@ -308,9 +308,9 @@ bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, } export void -bfdev_cache_destroy(struct bfdev_cache_head *head) +bfdev_cache_destroy(bfdev_cache_head_t *head) { - const struct bfdev_cache_algo *algo; + const bfdev_cache_algo_t *algo; const struct bfdev_alloc *alloc; algo = head->algo; @@ -321,7 +321,7 @@ bfdev_cache_destroy(struct bfdev_cache_head *head) } export int -bfdev_cache_register(struct bfdev_cache_algo *algo) +bfdev_cache_register(bfdev_cache_algo_t *algo) { if (!(algo->name && algo->create && algo->destroy && algo->reset && algo->starving && algo->obtain && @@ -336,7 +336,7 @@ bfdev_cache_register(struct bfdev_cache_algo *algo) } export void -bfdev_cache_unregister(struct bfdev_cache_algo *algo) +bfdev_cache_unregister(bfdev_cache_algo_t *algo) { if (cache_algorithm_find(algo->name)) return; diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 84c9f3c6..16ff45d9 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -8,12 +8,12 @@ #include struct lfu_head { - struct bfdev_cache_head cache; + bfdev_cache_head_t cache; bfdev_heap_root_t lfu; }; struct lfu_node { - struct bfdev_cache_node cache; + bfdev_cache_node_t cache; bfdev_heap_node_t node; unsigned long count; }; @@ -43,15 +43,15 @@ lfu_compare(const bfdev_heap_node_t *node1, } static bool -lfu_starving(struct bfdev_cache_head *head) +lfu_starving(bfdev_cache_head_t *head) { struct lfu_head *lfu_head; lfu_head = cache_to_lfu_head(head); return BFDEV_HEAP_EMPTY_ROOT(&lfu_head->lfu); } -static struct bfdev_cache_node * -lfu_obtain(struct bfdev_cache_head *head) +static bfdev_cache_node_t * +lfu_obtain(bfdev_cache_head_t *head) { struct lfu_head *lfu_head; struct lfu_node *lfu_node; @@ -64,7 +64,7 @@ lfu_obtain(struct bfdev_cache_head *head) } static void -lfu_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lfu_get(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_head *lfu_head; struct lfu_node *lfu_node; @@ -76,7 +76,7 @@ lfu_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lfu_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lfu_put(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_head *lfu_head; struct lfu_node *lfu_node; @@ -88,7 +88,7 @@ lfu_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lfu_update(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lfu_update(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_node *lfu_node; lfu_node = cache_to_lfu_node(node); @@ -96,7 +96,7 @@ lfu_update(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lfu_clear(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lfu_clear(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_node *lfu_node; lfu_node = cache_to_lfu_node(node); @@ -104,7 +104,7 @@ lfu_clear(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lfu_reset(struct bfdev_cache_head *head) +lfu_reset(bfdev_cache_head_t *head) { struct lfu_head *lfu_head; unsigned long count; @@ -120,10 +120,10 @@ lfu_reset(struct bfdev_cache_head *head) } } -static struct bfdev_cache_head * +static bfdev_cache_head_t * lfu_create(const struct bfdev_alloc *alloc, unsigned long size) { - struct bfdev_cache_head *head; + bfdev_cache_head_t *head; struct lfu_head *lfu_head; struct lfu_node *lfu_node; unsigned long count; @@ -162,10 +162,10 @@ lfu_create(const struct bfdev_alloc *alloc, unsigned long size) } static void -lfu_destroy(struct bfdev_cache_head *head) +lfu_destroy(bfdev_cache_head_t *head) { const struct bfdev_alloc *alloc; - struct bfdev_cache_node *node; + bfdev_cache_node_t *node; unsigned long count; alloc = head->alloc; @@ -179,7 +179,7 @@ lfu_destroy(struct bfdev_cache_head *head) bfdev_free(alloc, head); } -static struct bfdev_cache_algo +static bfdev_cache_algo_t lfu_algorithm = { .name = "lfu", .starving = lfu_starving, diff --git a/src/cache/lru.c b/src/cache/lru.c index a31f0a76..350ccc52 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -7,12 +7,12 @@ #include struct lru_head { - struct bfdev_cache_head cache; + bfdev_cache_head_t cache; bfdev_list_head_t lru; }; struct lru_node { - struct bfdev_cache_node cache; + bfdev_cache_node_t cache; bfdev_list_head_t node; }; @@ -23,15 +23,15 @@ struct lru_node { bfdev_container_of(ptr, struct lru_node, cache) static bool -lru_starving(struct bfdev_cache_head *head) +lru_starving(bfdev_cache_head_t *head) { struct lru_head *lru_head; lru_head = cache_to_lru_head(head); return bfdev_list_check_empty(&lru_head->lru); } -static struct bfdev_cache_node * -lru_obtain(struct bfdev_cache_head *head) +static bfdev_cache_node_t * +lru_obtain(bfdev_cache_head_t *head) { struct lru_head *lru_head; struct lru_node *lru_node; @@ -44,7 +44,7 @@ lru_obtain(struct bfdev_cache_head *head) } static void -lru_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lru_get(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lru_node *lru_node; lru_node = cache_to_lru_node(node); @@ -52,7 +52,7 @@ lru_get(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lru_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) +lru_put(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lru_head *lru_head; struct lru_node *lru_node; @@ -64,17 +64,17 @@ lru_put(struct bfdev_cache_head *head, struct bfdev_cache_node *node) } static void -lru_reset(struct bfdev_cache_head *head) +lru_reset(bfdev_cache_head_t *head) { struct lru_head *lru_head; lru_head = cache_to_lru_head(head); bfdev_list_head_init(&lru_head->lru); } -static struct bfdev_cache_head * +static bfdev_cache_head_t * lru_create(const struct bfdev_alloc *alloc, unsigned long size) { - struct bfdev_cache_head *head; + bfdev_cache_head_t *head; struct lru_head *lru_head; struct lru_node *lru_node; unsigned long count; @@ -113,11 +113,11 @@ lru_create(const struct bfdev_alloc *alloc, unsigned long size) } static void -lru_destroy(struct bfdev_cache_head *head) +lru_destroy(bfdev_cache_head_t *head) { struct lru_head *lru_head = cache_to_lru_head(head); const struct bfdev_alloc *alloc; - struct bfdev_cache_node *node; + bfdev_cache_node_t *node; unsigned long count; alloc = head->alloc; @@ -130,7 +130,7 @@ lru_destroy(struct bfdev_cache_head *head) bfdev_free(alloc, lru_head); } -static struct bfdev_cache_algo +static bfdev_cache_algo_t lru_algorithm = { .name = "lru", .starving = lru_starving, diff --git a/src/respool.c b/src/respool.c index 7d92dcdb..901a73a7 100755 --- a/src/respool.c +++ b/src/respool.c @@ -11,11 +11,11 @@ #include #include -export struct bfdev_resnode * -bfdev_respool_find(struct bfdev_respool *pool, +export bfdev_resnode_t * +bfdev_respool_find(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data) { - struct bfdev_resnode *walk; + bfdev_resnode_t *walk; bfdev_list_for_each_entry(walk, &pool->node, list) { if (!find(pool, walk, data)) @@ -26,7 +26,7 @@ bfdev_respool_find(struct bfdev_respool *pool, } export void -bfdev_respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_insert(bfdev_respool_t *pool, bfdev_resnode_t *node) { bfdev_list_add_prev(&pool->node, &node->list); bfdev_log_debug("%s: insert %p '%s'\n", @@ -34,7 +34,7 @@ bfdev_respool_insert(struct bfdev_respool *pool, struct bfdev_resnode *node) } export void -bfdev_respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_remove(bfdev_respool_t *pool, bfdev_resnode_t *node) { bfdev_list_del(&node->list); bfdev_log_debug("%s: remove %p '%s'\n", @@ -42,7 +42,7 @@ bfdev_respool_remove(struct bfdev_respool *pool, struct bfdev_resnode *node) } export void -bfdev_respool_release(struct bfdev_respool *pool, struct bfdev_resnode *node) +bfdev_respool_release(bfdev_respool_t *pool, bfdev_resnode_t *node) { bfdev_list_del(&node->list); node->release(pool, node); @@ -50,11 +50,11 @@ bfdev_respool_release(struct bfdev_respool *pool, struct bfdev_resnode *node) pool->name, node, node->name); } -export struct bfdev_resnode * -bfdev_respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, +export bfdev_resnode_t * +bfdev_respool_find_remove(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data) { - struct bfdev_resnode *match; + bfdev_resnode_t *match; match = bfdev_respool_find(pool, find, data); if (bfdev_likely(match)) @@ -68,11 +68,11 @@ bfdev_respool_find_remove(struct bfdev_respool *pool, bfdev_respool_find_t find, return match; } -export struct bfdev_resnode * -bfdev_respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find, +export bfdev_resnode_t * +bfdev_respool_find_release(bfdev_respool_t *pool, bfdev_respool_find_t find, const void *data) { - struct bfdev_resnode *match; + bfdev_resnode_t *match; match = bfdev_respool_find(pool, find, data); if (bfdev_likely(match)) { @@ -89,9 +89,9 @@ bfdev_respool_find_release(struct bfdev_respool *pool, bfdev_respool_find_t find } export void -bfdev_respool_release_all(struct bfdev_respool *pool) +bfdev_respool_release_all(bfdev_respool_t *pool) { - struct bfdev_resnode *node; + bfdev_resnode_t *node; bfdev_list_for_each_entry(node, &pool->node, list) { node->release(pool, node); From a342a5e45c17b02e0ae685a3e27d2f53e1189a5f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 17:03:59 +0800 Subject: [PATCH 047/119] feat allocator: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/allocator.h | 33 ++++++++++++++++++--------------- include/bfdev/argv.h | 4 ++-- include/bfdev/array.h | 5 ++--- include/bfdev/bitmap.h | 6 +++--- include/bfdev/bloom.h | 4 ++-- include/bfdev/btree.h | 2 +- include/bfdev/cache.h | 6 +++--- include/bfdev/fifo.h | 4 ++-- include/bfdev/fsm.h | 2 +- include/bfdev/hashmap.h | 4 ++-- include/bfdev/levenshtein.h | 4 ++-- include/bfdev/matrix.h | 12 ++++++------ include/bfdev/radix.h | 2 +- include/bfdev/ringbuf.h | 4 ++-- include/bfdev/skiplist.h | 4 ++-- include/bfdev/string.h | 4 ++-- include/bfdev/textsearch.h | 6 +++--- include/bfdev/types.h | 2 +- src/allocator.c | 20 ++++++++++---------- src/argv.c | 4 ++-- src/array.c | 4 ++-- src/bitmap.c | 6 +++--- src/bloom.c | 4 ++-- src/btree-utils.c | 4 ++-- src/cache/cache.c | 4 ++-- src/cache/lfu.c | 4 ++-- src/cache/lru.c | 4 ++-- src/fifo.c | 4 ++-- src/hashmap.c | 4 ++-- src/levenshtein.c | 4 ++-- src/libc/strdup.c | 4 ++-- src/matrix.c | 10 +++++----- src/radix.c | 12 ++++++------ src/ringbuf.c | 4 ++-- src/skiplist.c | 10 +++++----- src/textsearch/bm.c | 2 +- src/textsearch/kmp.c | 2 +- src/textsearch/sunday.c | 2 +- src/textsearch/textsearch.c | 2 +- 39 files changed, 112 insertions(+), 110 deletions(-) diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index 84f3ed41..ee2525dd 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -13,13 +13,16 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_alloc bfdev_alloc_t; +typedef struct bfdev_alloc_ops bfdev_alloc_ops_t; + struct bfdev_alloc { - const struct bfdev_alloc_ops *ops; + const bfdev_alloc_ops_t *ops; void *pdata; }; -struct bfdev_alloc_ops { - bfdev_alloc_t alloc; +struct bfdev_alloc_ops { + bfdev_malloc_t alloc; bfdev_realloc_t realloc; bfdev_free_t free; }; @@ -28,38 +31,38 @@ struct bfdev_alloc_ops { {.alloc = (ALLOC), .realloc = (REALLOC), .free = (FREE)} #define BFDEV_ALLOC_OPS_INIT(alloc, realloc, free) \ - (struct bfdev_alloc_ops) BFDEV_ALLOC_OPS_STATIC(alloc, realloc, free) + (bfdev_alloc_ops_t) BFDEV_ALLOC_OPS_STATIC(alloc, realloc, free) #define BFDEV_DEFINE_ALLOC_OPS(name, alloc, realloc, free) \ - struct bfdev_alloc_ops name = BFDEV_ALLOC_OPS_INIT(alloc, realloc, free) + bfdev_alloc_ops_t name = BFDEV_ALLOC_OPS_INIT(alloc, realloc, free) #define BFDEV_ALLOC_STATIC(ALLOC, REALLOC, FREE, PDATA) \ {.ops = &BFDEV_ALLOC_OPS_INIT(ALLOC, REALLOC, FREE), .pdata = (PDATA)} #define BFDEV_ALLOC_INIT(alloc, realloc, free, pdata) \ - (struct bfdev_alloc) BFDEV_ALLOC_STATIC(alloc, realloc, free, pdata) + (bfdev_alloc_t) BFDEV_ALLOC_STATIC(alloc, realloc, free, pdata) #define BFDEV_DEFINE_ALLOC(name, alloc, realloc, free, pdata) \ - struct bfdev_alloc name = BFDEV_ALLOC_INIT(alloc, realloc, free, pdata) + bfdev_alloc_t name = BFDEV_ALLOC_INIT(alloc, realloc, free, pdata) static inline void -bfdev_alloc_init(struct bfdev_alloc *allocator, bfdev_alloc_t alloc, +bfdev_alloc_init(bfdev_alloc_t *allocator, bfdev_malloc_t alloc, bfdev_realloc_t realloc, bfdev_free_t free, void *pdata) { *allocator = BFDEV_ALLOC_INIT(alloc, realloc, free, pdata); } extern __bfdev_malloc void * -bfdev_malloc(const struct bfdev_alloc *alloc, size_t size); +bfdev_malloc(const bfdev_alloc_t *alloc, size_t size); extern __bfdev_malloc void * -bfdev_zalloc(const struct bfdev_alloc *alloc, size_t size); +bfdev_zalloc(const bfdev_alloc_t *alloc, size_t size); extern __bfdev_malloc void * -bfdev_realloc(const struct bfdev_alloc *alloc, const void *block, size_t resize); +bfdev_realloc(const bfdev_alloc_t *alloc, const void *block, size_t resize); extern void -bfdev_free(const struct bfdev_alloc *alloc, const void *block); +bfdev_free(const bfdev_alloc_t *alloc, const void *block); /** * bfdev_malloc_array - allocate memory for an array. @@ -67,7 +70,7 @@ bfdev_free(const struct bfdev_alloc *alloc, const void *block); * @size: single element size. */ static __bfdev_always_inline __bfdev_malloc void * -bfdev_malloc_array(const struct bfdev_alloc *alloc, +bfdev_malloc_array(const bfdev_alloc_t *alloc, size_t nr, size_t size) { return bfdev_malloc(alloc, size * nr); @@ -79,7 +82,7 @@ bfdev_malloc_array(const struct bfdev_alloc *alloc, * @size: single element size. */ static __bfdev_always_inline __bfdev_malloc void * -bfdev_zalloc_array(const struct bfdev_alloc *alloc, +bfdev_zalloc_array(const bfdev_alloc_t *alloc, size_t nr, size_t size) { return bfdev_zalloc(alloc, size * nr); @@ -92,7 +95,7 @@ bfdev_zalloc_array(const struct bfdev_alloc *alloc, * @size: single element size. */ static __bfdev_always_inline __bfdev_malloc void * -bfdev_realloc_array(const struct bfdev_alloc *alloc, +bfdev_realloc_array(const bfdev_alloc_t *alloc, void *block, size_t nr, size_t size) { return bfdev_realloc(alloc, block, size * nr); diff --git a/include/bfdev/argv.h b/include/bfdev/argv.h index 867eec2a..2e1f5bef 100644 --- a/include/bfdev/argv.h +++ b/include/bfdev/argv.h @@ -21,7 +21,7 @@ bfdev_argv_count(const char *string); * @argcp: returned argument count. */ extern char ** -bfdev_argv_split(const struct bfdev_alloc *alloc, +bfdev_argv_split(const bfdev_alloc_t *alloc, const char *string, unsigned int *argcp); /** @@ -30,7 +30,7 @@ bfdev_argv_split(const struct bfdev_alloc *alloc, * @argv: the argument vector to be freed. */ extern void -bfdev_argv_destory(const struct bfdev_alloc *alloc, +bfdev_argv_destory(const bfdev_alloc_t *alloc, char **argv); BFDEV_END_DECLS diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 014a58c6..f768e872 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -21,7 +21,7 @@ BFDEV_BEGIN_DECLS typedef struct bfdev_array bfdev_array_t; struct bfdev_array { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; unsigned long capacity; unsigned long index; size_t cells; @@ -38,8 +38,7 @@ struct bfdev_array { bfdev_array_t name = BFDEV_ARRAY_INIT(alloc, cells) static inline void -bfdev_array_init(bfdev_array_t *array, const struct bfdev_alloc *alloc, - size_t cells) +bfdev_array_init(bfdev_array_t *array, const bfdev_alloc_t *alloc, size_t cells) { *array = BFDEV_ARRAY_INIT(alloc, cells); } diff --git a/include/bfdev/bitmap.h b/include/bfdev/bitmap.h index 05873176..633a37e7 100644 --- a/include/bfdev/bitmap.h +++ b/include/bfdev/bitmap.h @@ -223,7 +223,7 @@ bfdev_bitmap_copy(unsigned long *dest, unsigned long *src, unsigned int bits) * @flags: allocate flags. */ extern unsigned long * -bfdev_bitmap_alloc(const struct bfdev_alloc *alloc, unsigned int bits); +bfdev_bitmap_alloc(const bfdev_alloc_t *alloc, unsigned int bits); /** * bfdev_bitmap_zalloc - alloc and zeroed a bitmap. @@ -231,14 +231,14 @@ bfdev_bitmap_alloc(const struct bfdev_alloc *alloc, unsigned int bits); * @flags: allocate flags. */ extern unsigned long * -bfdev_bitmap_zalloc(const struct bfdev_alloc *alloc, unsigned int bits); +bfdev_bitmap_zalloc(const bfdev_alloc_t *alloc, unsigned int bits); /** * bfdev_bitmap_free - free a bitmap. * @bitmap: bitmap to free. */ extern void -bfdev_bitmap_free(const struct bfdev_alloc *alloc, const unsigned long *bitmap); +bfdev_bitmap_free(const bfdev_alloc_t *alloc, const unsigned long *bitmap); BFDEV_END_DECLS diff --git a/include/bfdev/bloom.h b/include/bfdev/bloom.h index fe907831..f01050e3 100644 --- a/include/bfdev/bloom.h +++ b/include/bfdev/bloom.h @@ -20,7 +20,7 @@ typedef unsigned int (*bfdev_bloom_hash_t) (unsigned int func, const void *key, void *pdata); struct bfdev_bloom { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; bfdev_bloom_hash_t hash; unsigned int funcs; void *pdata; @@ -64,7 +64,7 @@ bfdev_bloom_flush(bfdev_bloom_t *bloom); * @pdata: private data pointer of @hash. */ extern bfdev_bloom_t * -bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, +bfdev_bloom_create(const bfdev_alloc_t *alloc, unsigned int capacity, bfdev_bloom_hash_t hash, unsigned int funcs, void *pdata); /** diff --git a/include/bfdev/btree.h b/include/bfdev/btree.h index 8d6707e0..8433551c 100644 --- a/include/bfdev/btree.h +++ b/include/bfdev/btree.h @@ -26,7 +26,7 @@ struct bfdev_btree_node { }; struct bfdev_btree_root { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; struct bfdev_btree_layout *layout; struct bfdev_btree_node *node; unsigned int height; diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index 1f0ca569..61451614 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -58,7 +58,7 @@ struct bfdev_cache_node { }; struct bfdev_cache_head { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; const bfdev_cache_algo_t *algo; bfdev_hlist_head_t *taghash; bfdev_cache_node_t **nodes; @@ -95,7 +95,7 @@ struct bfdev_cache_algo { void (*clear)(bfdev_cache_head_t *head, bfdev_cache_node_t *node); void (*reset)(bfdev_cache_head_t *head); - bfdev_cache_head_t *(*create)(const struct bfdev_alloc *alloc, unsigned long size); + bfdev_cache_head_t *(*create)(const bfdev_alloc_t *alloc, unsigned long size); void (*destroy)(bfdev_cache_head_t *head); }; @@ -137,7 +137,7 @@ extern void bfdev_cache_reset(bfdev_cache_head_t *head); extern bfdev_cache_head_t * -bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, +bfdev_cache_create(const char *name, const bfdev_alloc_t *alloc, unsigned long size, unsigned long maxp); extern void diff --git a/include/bfdev/fifo.h b/include/bfdev/fifo.h index bb7ff429..18c7e7af 100755 --- a/include/bfdev/fifo.h +++ b/include/bfdev/fifo.h @@ -12,7 +12,7 @@ #include struct bfdev_fifo { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; unsigned long in; unsigned long out; unsigned long mask; @@ -431,7 +431,7 @@ extern unsigned long bfdev_fifo_in_flat(struct bfdev_fifo *fifo, const void *buf extern unsigned long bfdev_fifo_peek_record(struct bfdev_fifo *fifo, void *buff, unsigned long len, unsigned long record); extern unsigned long bfdev_fifo_out_record(struct bfdev_fifo *fifo, void *buff, unsigned long len, unsigned long record); extern unsigned long bfdev_fifo_in_record(struct bfdev_fifo *fifo, const void *buff, unsigned long len, unsigned long record); -extern int bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const struct bfdev_alloc *alloc, size_t esize, size_t size); +extern int bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const bfdev_alloc_t *alloc, size_t esize, size_t size); extern void bfdev_fifo_dynamic_free(struct bfdev_fifo *fifo); #endif /* _BFDEV_FIFO_H_ */ diff --git a/include/bfdev/fsm.h b/include/bfdev/fsm.h index 6ae62ca5..b5f61554 100644 --- a/include/bfdev/fsm.h +++ b/include/bfdev/fsm.h @@ -90,7 +90,7 @@ struct bfdev_fsm { bfdev_fsm_t name = BFDEV_FSM_INIT(alloc, init, error) static inline void -bfdev_fsm_init(bfdev_fsm_t *fsm, const struct bfdev_alloc *alloc, +bfdev_fsm_init(bfdev_fsm_t *fsm, const bfdev_alloc_t *alloc, const bfdev_fsm_state_t *init, const bfdev_fsm_state_t *error) { *fsm = BFDEV_FSM_INIT(alloc, init, error); diff --git a/include/bfdev/hashmap.h b/include/bfdev/hashmap.h index e5eb6ccd..ecf42d15 100644 --- a/include/bfdev/hashmap.h +++ b/include/bfdev/hashmap.h @@ -40,7 +40,7 @@ struct bfdev_hashmap { unsigned long capacity; unsigned long used; - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; const bfdev_hashmap_ops_t *ops; void *pdata; }; @@ -73,7 +73,7 @@ struct bfdev_hashmap_ops { * @pdata: operations callback data. */ static inline void -bfdev_hashmap_init(bfdev_hashmap_t *hashmap, const struct bfdev_alloc *alloc, +bfdev_hashmap_init(bfdev_hashmap_t *hashmap, const bfdev_alloc_t *alloc, const bfdev_hashmap_ops_t *ops, void *pdata) { *hashmap = BFDEV_HASHMAP_INIT(alloc, ops, pdata); diff --git a/include/bfdev/levenshtein.h b/include/bfdev/levenshtein.h index 7bcbe555..f0f2c462 100644 --- a/include/bfdev/levenshtein.h +++ b/include/bfdev/levenshtein.h @@ -25,7 +25,7 @@ BFDEV_BEGIN_DECLS * @d: deletion distance reward. */ extern unsigned int -bfdev_levenshtein_len(const struct bfdev_alloc *alloc, +bfdev_levenshtein_len(const bfdev_alloc_t *alloc, const char *str1, const char *str2, size_t len1, size_t len2, unsigned int s, unsigned int w, @@ -41,7 +41,7 @@ bfdev_levenshtein_len(const struct bfdev_alloc *alloc, * @d: deletion distance reward. */ extern unsigned int -bfdev_levenshtein(const struct bfdev_alloc *alloc, +bfdev_levenshtein(const bfdev_alloc_t *alloc, const char *str1, const char *str2, unsigned int s, unsigned int w, unsigned int a, unsigned int d); diff --git a/include/bfdev/matrix.h b/include/bfdev/matrix.h index a9a73d4c..ebeea859 100644 --- a/include/bfdev/matrix.h +++ b/include/bfdev/matrix.h @@ -23,7 +23,7 @@ struct bfdev_matrix { * @vb: second addend matrix. */ extern struct bfdev_matrix * -bfdev_matrix_add(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, +bfdev_matrix_add(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, const struct bfdev_matrix *vb); /** @@ -32,7 +32,7 @@ bfdev_matrix_add(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, * @vb: subtrahend; matrix to subtract from @va. */ extern struct bfdev_matrix * -bfdev_matrix_sub(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, +bfdev_matrix_sub(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, const struct bfdev_matrix *vb); /** @@ -41,7 +41,7 @@ bfdev_matrix_sub(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, * @vb: second factor matrix. */ extern struct bfdev_matrix * -bfdev_matrix_mul(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, +bfdev_matrix_mul(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, const struct bfdev_matrix *vb); /** @@ -49,7 +49,7 @@ bfdev_matrix_mul(const struct bfdev_alloc *alloc, const struct bfdev_matrix *va, * @var: variables to copy. */ extern struct bfdev_matrix * -bfdev_matrix_copy(const struct bfdev_alloc *alloc, +bfdev_matrix_copy(const bfdev_alloc_t *alloc, const struct bfdev_matrix *var); /** @@ -58,7 +58,7 @@ bfdev_matrix_copy(const struct bfdev_alloc *alloc, * @col: number of columns in the matrix. */ extern struct bfdev_matrix * -bfdev_matrix_create(const struct bfdev_alloc *alloc, +bfdev_matrix_create(const bfdev_alloc_t *alloc, unsigned int row, unsigned int col); /** @@ -66,7 +66,7 @@ bfdev_matrix_create(const struct bfdev_alloc *alloc, * @var: variables to destory. */ extern void -bfdev_matrix_destory(const struct bfdev_alloc *alloc, +bfdev_matrix_destory(const bfdev_alloc_t *alloc, const struct bfdev_matrix *var); BFDEV_END_DECLS diff --git a/include/bfdev/radix.h b/include/bfdev/radix.h index a855f9e7..b1e9887a 100644 --- a/include/bfdev/radix.h +++ b/include/bfdev/radix.h @@ -25,7 +25,7 @@ typedef struct bfdev_radix_root bfdev_radix_root_t; typedef struct bfdev_radix_node bfdev_radix_node_t; struct bfdev_radix_root { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; bfdev_radix_node_t *node; unsigned int level; }; diff --git a/include/bfdev/ringbuf.h b/include/bfdev/ringbuf.h index ebf56a3d..358770ab 100644 --- a/include/bfdev/ringbuf.h +++ b/include/bfdev/ringbuf.h @@ -12,7 +12,7 @@ #include struct bfdev_ringbuf { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; unsigned long in; unsigned long out; unsigned long mask; @@ -431,7 +431,7 @@ extern unsigned long bfdev_ringbuf_in_flat(struct bfdev_ringbuf *ringbuf, const extern unsigned long bfdev_ringbuf_peek_record(struct bfdev_ringbuf *ringbuf, void *buff, unsigned long len, unsigned long record); extern unsigned long bfdev_ringbuf_out_record(struct bfdev_ringbuf *ringbuf, void *buff, unsigned long len, unsigned long record); extern unsigned long bfdev_ringbuf_in_record(struct bfdev_ringbuf *ringbuf, const void *buff, unsigned long len, unsigned long record); -extern int bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const struct bfdev_alloc *alloc, size_t esize, size_t size); +extern int bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const bfdev_alloc_t *alloc, size_t esize, size_t size); extern void bfdev_ringbuf_dynamic_free(struct bfdev_ringbuf *ringbuf); #endif /* _BFDEV_RINGBUF_H_ */ diff --git a/include/bfdev/skiplist.h b/include/bfdev/skiplist.h index d801a19b..c98c9f87 100644 --- a/include/bfdev/skiplist.h +++ b/include/bfdev/skiplist.h @@ -23,7 +23,7 @@ struct bfdev_skip_node { }; struct bfdev_skip_head { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; unsigned int curr; unsigned int levels; bfdev_list_head_t nodes[0]; @@ -46,7 +46,7 @@ extern void bfdev_skiplist_destroy(bfdev_skip_head_t *head, bfdev_release_t relse); extern bfdev_skip_head_t * -bfdev_skiplist_create(const struct bfdev_alloc *alloc, unsigned int levels); +bfdev_skiplist_create(const bfdev_alloc_t *alloc, unsigned int levels); /** * bfdev_skiplist_for_each - iterate over list of given type. diff --git a/include/bfdev/string.h b/include/bfdev/string.h index ee1773af..5e91f216 100644 --- a/include/bfdev/string.h +++ b/include/bfdev/string.h @@ -19,11 +19,11 @@ extern void * bfdev_memdiff(const void *addr, int c, size_t size); extern char * -bfdev_strdup(const struct bfdev_alloc *alloc, +bfdev_strdup(const bfdev_alloc_t *alloc, const char *string); extern char * -bfdev_strndup(const struct bfdev_alloc *alloc, +bfdev_strndup(const bfdev_alloc_t *alloc, const char *string, size_t len); BFDEV_END_DECLS diff --git a/include/bfdev/textsearch.h b/include/bfdev/textsearch.h index 8c08d476..7db01778 100644 --- a/include/bfdev/textsearch.h +++ b/include/bfdev/textsearch.h @@ -46,7 +46,7 @@ struct bfdev_ts_linear { * @next_block: fetch next block of data. */ struct bfdev_ts_context { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; unsigned long flags; bfdev_ts_algorithm_t *algo; @@ -67,7 +67,7 @@ struct bfdev_ts_algorithm { bfdev_list_head_t list; const char *name; - bfdev_ts_context_t *(*prepare)(const struct bfdev_alloc *alloc, const void *pattern, + bfdev_ts_context_t *(*prepare)(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags); void (*destroy)(bfdev_ts_context_t *tsc); unsigned int (*find)(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss); @@ -122,7 +122,7 @@ extern unsigned int bfdev_textsearch_linear_next(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear); extern bfdev_ts_context_t * -bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, +bfdev_textsearch_create(const bfdev_alloc_t *alloc, const char *name, const void *pattern, size_t len, unsigned long flags); extern int diff --git a/include/bfdev/types.h b/include/bfdev/types.h index 8cc8f163..3e44e6fc 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -55,7 +55,7 @@ BFDEV_CALLBACK_FIND(bfdev_find_t, const void *); BFDEV_CALLBACK_CMP(bfdev_cmp_t, const void *); BFDEV_CALLBACK_RELEASE(bfdev_release_t); -typedef void *(*bfdev_alloc_t)(size_t size, void *pdata); +typedef void *(*bfdev_malloc_t)(size_t size, void *pdata); typedef void *(*bfdev_realloc_t)(void *block, size_t resize, void *pdata); typedef void (*bfdev_free_t)(void *block, void *pdata); diff --git a/src/allocator.c b/src/allocator.c index b55c382c..d1fdb77b 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -25,8 +25,8 @@ generic_free(const void *block) free((void *)block); } -static inline const struct bfdev_alloc_ops * -alloc_check(const struct bfdev_alloc *alloc) +static inline const bfdev_alloc_ops_t * +alloc_check(const bfdev_alloc_t *alloc) { if (!alloc) return NULL; @@ -34,9 +34,9 @@ alloc_check(const struct bfdev_alloc *alloc) } export __bfdev_malloc void * -bfdev_malloc(const struct bfdev_alloc *alloc, size_t size) +bfdev_malloc(const bfdev_alloc_t *alloc, size_t size) { - const struct bfdev_alloc_ops *ops; + const bfdev_alloc_ops_t *ops; void *pdata, *retval; if (!(ops = alloc_check(alloc)) || !ops->alloc) @@ -50,9 +50,9 @@ bfdev_malloc(const struct bfdev_alloc *alloc, size_t size) } export __bfdev_malloc void * -bfdev_zalloc(const struct bfdev_alloc *alloc, size_t size) +bfdev_zalloc(const bfdev_alloc_t *alloc, size_t size) { - const struct bfdev_alloc_ops *ops; + const bfdev_alloc_ops_t *ops; void *pdata, *retval; if (!(ops = alloc_check(alloc)) || !ops->alloc) @@ -69,9 +69,9 @@ bfdev_zalloc(const struct bfdev_alloc *alloc, size_t size) } export __bfdev_malloc void * -bfdev_realloc(const struct bfdev_alloc *alloc, const void *block, size_t resize) +bfdev_realloc(const bfdev_alloc_t *alloc, const void *block, size_t resize) { - const struct bfdev_alloc_ops *ops; + const bfdev_alloc_ops_t *ops; void *pdata, *retval; if (!(ops = alloc_check(alloc)) || !ops->realloc) @@ -85,9 +85,9 @@ bfdev_realloc(const struct bfdev_alloc *alloc, const void *block, size_t resize) } export void -bfdev_free(const struct bfdev_alloc *alloc, const void *block) +bfdev_free(const bfdev_alloc_t *alloc, const void *block) { - const struct bfdev_alloc_ops *ops; + const bfdev_alloc_ops_t *ops; void *pdata; if (!(ops = alloc_check(alloc)) || !ops->free) diff --git a/src/argv.c b/src/argv.c index b9f7c344..a83d0a93 100644 --- a/src/argv.c +++ b/src/argv.c @@ -32,7 +32,7 @@ bfdev_argv_count(const char *args) } export char ** -bfdev_argv_split(const struct bfdev_alloc *alloc, const char *args, +bfdev_argv_split(const bfdev_alloc_t *alloc, const char *args, unsigned int *argcp) { unsigned int count, argc; @@ -64,7 +64,7 @@ bfdev_argv_split(const struct bfdev_alloc *alloc, const char *args, } export void -bfdev_argv_destory(const struct bfdev_alloc *alloc, char **argv) +bfdev_argv_destory(const bfdev_alloc_t *alloc, char **argv) { bfdev_free(alloc, argv); } diff --git a/src/array.c b/src/array.c index ea1a6097..0ee4637a 100644 --- a/src/array.c +++ b/src/array.c @@ -12,7 +12,7 @@ static int array_resize(bfdev_array_t *array, unsigned long count) { - const struct bfdev_alloc *alloc = array->alloc; + const bfdev_alloc_t *alloc = array->alloc; unsigned long nalloc; size_t size; void *data; @@ -94,7 +94,7 @@ bfdev_array_reserve(bfdev_array_t *array, unsigned long num) export void bfdev_array_release(bfdev_array_t *array) { - const struct bfdev_alloc *alloc = array->alloc; + const bfdev_alloc_t *alloc = array->alloc; array->capacity = 0; array->index = 0; diff --git a/src/bitmap.c b/src/bitmap.c index 536a368b..3bec6ce6 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -174,7 +174,7 @@ bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, unsigned int bi } export unsigned long * -bfdev_bitmap_alloc(const struct bfdev_alloc *alloc, unsigned int bits) +bfdev_bitmap_alloc(const bfdev_alloc_t *alloc, unsigned int bits) { return bfdev_malloc_array( alloc, BFDEV_BITS_TO_LONG(bits), sizeof(unsigned long) @@ -182,7 +182,7 @@ bfdev_bitmap_alloc(const struct bfdev_alloc *alloc, unsigned int bits) } export unsigned long * -bfdev_bitmap_zalloc(const struct bfdev_alloc *alloc, unsigned int bits) +bfdev_bitmap_zalloc(const bfdev_alloc_t *alloc, unsigned int bits) { return bfdev_zalloc_array( alloc, BFDEV_BITS_TO_LONG(bits), sizeof(unsigned long) @@ -190,7 +190,7 @@ bfdev_bitmap_zalloc(const struct bfdev_alloc *alloc, unsigned int bits) } export void -bfdev_bitmap_free(const struct bfdev_alloc *alloc, const unsigned long *bitmap) +bfdev_bitmap_free(const bfdev_alloc_t *alloc, const unsigned long *bitmap) { bfdev_free(alloc, bitmap); } diff --git a/src/bloom.c b/src/bloom.c index 906cb038..c31be65e 100644 --- a/src/bloom.c +++ b/src/bloom.c @@ -58,7 +58,7 @@ bfdev_bloom_flush(bfdev_bloom_t *bloom) } export bfdev_bloom_t * -bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, +bfdev_bloom_create(const bfdev_alloc_t *alloc, unsigned int capacity, bfdev_bloom_hash_t hash, unsigned int funcs, void *pdata) { bfdev_bloom_t *bloom; @@ -83,6 +83,6 @@ bfdev_bloom_create(const struct bfdev_alloc *alloc, unsigned int capacity, export void bfdev_bloom_destory(bfdev_bloom_t *bloom) { - const struct bfdev_alloc *alloc = bloom->alloc; + const bfdev_alloc_t *alloc = bloom->alloc; bfdev_free(alloc, bloom); } diff --git a/src/btree-utils.c b/src/btree-utils.c index 9f62a57d..24f07fa3 100644 --- a/src/btree-utils.c +++ b/src/btree-utils.c @@ -56,7 +56,7 @@ bfdev_btree_key_find(struct bfdev_btree_root *root, uintptr_t *node, uintptr_t * export void * bfdev_btree_alloc(struct bfdev_btree_root *root) { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; struct bfdev_btree_layout *layout; alloc = root->alloc; @@ -68,7 +68,7 @@ bfdev_btree_alloc(struct bfdev_btree_root *root) export void bfdev_btree_free(struct bfdev_btree_root *root, void *node) { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; alloc = root->alloc; bfdev_free(alloc, node); } diff --git a/src/cache/cache.c b/src/cache/cache.c index de137f09..f7e649c6 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -261,7 +261,7 @@ bfdev_cache_reset(bfdev_cache_head_t *head) } export bfdev_cache_head_t * -bfdev_cache_create(const char *name, const struct bfdev_alloc *alloc, +bfdev_cache_create(const char *name, const bfdev_alloc_t *alloc, unsigned long size, unsigned long maxpend) { const bfdev_cache_algo_t *algo; @@ -311,7 +311,7 @@ export void bfdev_cache_destroy(bfdev_cache_head_t *head) { const bfdev_cache_algo_t *algo; - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; algo = head->algo; alloc = head->alloc; diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 16ff45d9..1777d7d3 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -121,7 +121,7 @@ lfu_reset(bfdev_cache_head_t *head) } static bfdev_cache_head_t * -lfu_create(const struct bfdev_alloc *alloc, unsigned long size) +lfu_create(const bfdev_alloc_t *alloc, unsigned long size) { bfdev_cache_head_t *head; struct lfu_head *lfu_head; @@ -164,7 +164,7 @@ lfu_create(const struct bfdev_alloc *alloc, unsigned long size) static void lfu_destroy(bfdev_cache_head_t *head) { - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; bfdev_cache_node_t *node; unsigned long count; diff --git a/src/cache/lru.c b/src/cache/lru.c index 350ccc52..9f9470d4 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -72,7 +72,7 @@ lru_reset(bfdev_cache_head_t *head) } static bfdev_cache_head_t * -lru_create(const struct bfdev_alloc *alloc, unsigned long size) +lru_create(const bfdev_alloc_t *alloc, unsigned long size) { bfdev_cache_head_t *head; struct lru_head *lru_head; @@ -116,7 +116,7 @@ static void lru_destroy(bfdev_cache_head_t *head) { struct lru_head *lru_head = cache_to_lru_head(head); - const struct bfdev_alloc *alloc; + const bfdev_alloc_t *alloc; bfdev_cache_node_t *node; unsigned long count; diff --git a/src/fifo.c b/src/fifo.c index db9afc53..7e9a675b 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -193,7 +193,7 @@ bfdev_fifo_in_record(struct bfdev_fifo *fifo, const void *buff, } export int -bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const struct bfdev_alloc *alloc, +bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const bfdev_alloc_t *alloc, size_t esize, size_t size) { size = bfdev_pow2_roundup(size); @@ -216,7 +216,7 @@ bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const struct bfdev_alloc *allo export void bfdev_fifo_dynamic_free(struct bfdev_fifo *fifo) { - const struct bfdev_alloc *alloc = fifo->alloc; + const bfdev_alloc_t *alloc = fifo->alloc; fifo->in = 0; fifo->out = 0; diff --git a/src/hashmap.c b/src/hashmap.c index 6d98bc8f..e2e9a967 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -129,7 +129,7 @@ hashmap_find_key(bfdev_hashmap_t *hashmap, const void *key, static inline int hashmap_rehash(bfdev_hashmap_t *hashmap, unsigned int nbits) { - const struct bfdev_alloc *alloc = hashmap->alloc; + const bfdev_alloc_t *alloc = hashmap->alloc; bfdev_hlist_node_t *walk, *tmp; bfdev_hlist_head_t *nbuckets; unsigned long value, index, ncapacity; @@ -256,7 +256,7 @@ bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key) export void bfdev_hashmap_release(bfdev_hashmap_t *hashmap) { - const struct bfdev_alloc *alloc = hashmap->alloc; + const bfdev_alloc_t *alloc = hashmap->alloc; bfdev_free(alloc, hashmap->buckets); hashmap->buckets = NULL; diff --git a/src/levenshtein.c b/src/levenshtein.c index c108181d..75d4deac 100644 --- a/src/levenshtein.c +++ b/src/levenshtein.c @@ -8,7 +8,7 @@ #include export unsigned int -bfdev_levenshtein_len(const struct bfdev_alloc *alloc, +bfdev_levenshtein_len(const bfdev_alloc_t *alloc, const char *str1, const char *str2, size_t len1, size_t len2, unsigned int s, unsigned int w, @@ -67,7 +67,7 @@ bfdev_levenshtein_len(const struct bfdev_alloc *alloc, } export unsigned int -bfdev_levenshtein(const struct bfdev_alloc *alloc, +bfdev_levenshtein(const bfdev_alloc_t *alloc, const char *str1, const char *str2, unsigned int s, unsigned int w, unsigned int a, unsigned int d) diff --git a/src/libc/strdup.c b/src/libc/strdup.c index 874e7348..4ae7d157 100644 --- a/src/libc/strdup.c +++ b/src/libc/strdup.c @@ -8,7 +8,7 @@ #include export char * -bfdev_strdup(const struct bfdev_alloc *alloc, +bfdev_strdup(const bfdev_alloc_t *alloc, const char *string) { size_t length; @@ -27,7 +27,7 @@ bfdev_strdup(const struct bfdev_alloc *alloc, } export char * -bfdev_strndup(const struct bfdev_alloc *alloc, +bfdev_strndup(const bfdev_alloc_t *alloc, const char *string, size_t len) { size_t length; diff --git a/src/matrix.c b/src/matrix.c index b9ea4405..22d7fe3b 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -9,7 +9,7 @@ #define GENERIC_MATRIX_ADDSUB(name, operate) \ export struct bfdev_matrix * \ -bfdev_matrix_##name(const struct bfdev_alloc *alloc, \ +bfdev_matrix_##name(const bfdev_alloc_t *alloc, \ const struct bfdev_matrix *va, \ const struct bfdev_matrix *vb) \ { \ @@ -41,7 +41,7 @@ GENERIC_MATRIX_ADDSUB(add, +) GENERIC_MATRIX_ADDSUB(sub, -) export struct bfdev_matrix * -bfdev_matrix_mul(const struct bfdev_alloc *alloc, +bfdev_matrix_mul(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, const struct bfdev_matrix *vb) { @@ -74,7 +74,7 @@ bfdev_matrix_mul(const struct bfdev_alloc *alloc, } export struct bfdev_matrix * -bfdev_matrix_copy(const struct bfdev_alloc *alloc, +bfdev_matrix_copy(const bfdev_alloc_t *alloc, const struct bfdev_matrix *var) { struct bfdev_matrix *result; @@ -94,7 +94,7 @@ bfdev_matrix_copy(const struct bfdev_alloc *alloc, } export struct bfdev_matrix * -bfdev_matrix_create(const struct bfdev_alloc *alloc, +bfdev_matrix_create(const bfdev_alloc_t *alloc, unsigned int row, unsigned int col) { struct bfdev_matrix *var; @@ -110,7 +110,7 @@ bfdev_matrix_create(const struct bfdev_alloc *alloc, } export void -bfdev_matrix_destory(const struct bfdev_alloc *alloc, +bfdev_matrix_destory(const bfdev_alloc_t *alloc, const struct bfdev_matrix *var) { bfdev_free(alloc, var); diff --git a/src/radix.c b/src/radix.c index cc292dae..7e5d6e3d 100644 --- a/src/radix.c +++ b/src/radix.c @@ -54,7 +54,7 @@ bfdev_radix_root_find(bfdev_radix_root_t *root, uintptr_t offset) static inline bfdev_radix_node_t * radix_extend(bfdev_radix_root_t *root, uintptr_t offset) { - const struct bfdev_alloc *alloc = root->alloc; + const bfdev_alloc_t *alloc = root->alloc; bfdev_radix_node_t *node, *successor; unsigned int level; @@ -85,7 +85,7 @@ radix_extend(bfdev_radix_root_t *root, uintptr_t offset) static inline void radix_shrink(bfdev_radix_root_t *root) { - const struct bfdev_alloc *alloc = root->alloc; + const bfdev_alloc_t *alloc = root->alloc; bfdev_radix_node_t *node, *successor; while (root->level) { @@ -106,7 +106,7 @@ radix_shrink(bfdev_radix_root_t *root) export void * bfdev_radix_root_alloc(bfdev_radix_root_t *root, uintptr_t offset) { - const struct bfdev_alloc *alloc = root->alloc; + const bfdev_alloc_t *alloc = root->alloc; bfdev_radix_node_t *node; unsigned int level; @@ -140,7 +140,7 @@ bfdev_radix_root_alloc(bfdev_radix_root_t *root, uintptr_t offset) export int bfdev_radix_root_free(bfdev_radix_root_t *root, uintptr_t offset) { - const struct bfdev_alloc *alloc = root->alloc; + const bfdev_alloc_t *alloc = root->alloc; bfdev_radix_node_t *node; unsigned int level; uintptr_t index; @@ -196,7 +196,7 @@ bfdev_radix_root_charge(bfdev_radix_root_t *root, } static void -radix_destory_recurse(const struct bfdev_alloc *alloc, +radix_destory_recurse(const bfdev_alloc_t *alloc, bfdev_radix_node_t *node, unsigned int level) { bfdev_radix_node_t *child; @@ -215,7 +215,7 @@ radix_destory_recurse(const struct bfdev_alloc *alloc, export void bfdev_radix_root_destory(bfdev_radix_root_t *root) { - const struct bfdev_alloc *alloc = root->alloc; + const bfdev_alloc_t *alloc = root->alloc; radix_destory_recurse(alloc, root->node, root->level); root->level = 0; diff --git a/src/ringbuf.c b/src/ringbuf.c index d3c25e4d..234c3b2f 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -216,7 +216,7 @@ bfdev_ringbuf_in_record(struct bfdev_ringbuf *ringbuf, const void *buff, } export int -bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const struct bfdev_alloc *alloc, +bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const bfdev_alloc_t *alloc, size_t esize, size_t size) { size = bfdev_pow2_roundup(size); @@ -239,7 +239,7 @@ bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const struct bfdev_al export void bfdev_ringbuf_dynamic_free(struct bfdev_ringbuf *ringbuf) { - const struct bfdev_alloc *alloc = ringbuf->alloc; + const bfdev_alloc_t *alloc = ringbuf->alloc; ringbuf->in = 0; ringbuf->out = 0; diff --git a/src/skiplist.c b/src/skiplist.c index bc0cf06f..d2cdb7bf 100644 --- a/src/skiplist.c +++ b/src/skiplist.c @@ -62,7 +62,7 @@ export int bfdev_skiplist_insert(bfdev_skip_head_t *head, void *key, bfdev_cmp_t cmp, void *pdata) { - const struct bfdev_alloc *alloc = head->alloc; + const bfdev_alloc_t *alloc = head->alloc; bfdev_list_head_t *list, *end; bfdev_skip_node_t *walk, *node; unsigned int level, count; @@ -101,7 +101,7 @@ export void bfdev_skiplist_delete(bfdev_skip_head_t *head, bfdev_find_t find, void *pdata) { - const struct bfdev_alloc *alloc = head->alloc; + const bfdev_alloc_t *alloc = head->alloc; bfdev_skip_node_t *node; unsigned int level; @@ -131,7 +131,7 @@ static void bfdev_skiplist_release(bfdev_skip_head_t *head, bfdev_release_t relse) { - const struct bfdev_alloc *alloc = head->alloc; + const bfdev_alloc_t *alloc = head->alloc; bfdev_skip_node_t *node, *tmp; bfdev_list_for_each_entry_safe(node, tmp, head->nodes, list[0]) { @@ -158,14 +158,14 @@ export void bfdev_skiplist_destroy(bfdev_skip_head_t *head, bfdev_release_t relse) { - const struct bfdev_alloc *alloc = head->alloc; + const bfdev_alloc_t *alloc = head->alloc; bfdev_skiplist_release(head, relse); bfdev_free(alloc, head); } export bfdev_skip_head_t * -bfdev_skiplist_create(const struct bfdev_alloc *alloc, +bfdev_skiplist_create(const bfdev_alloc_t *alloc, unsigned int levels) { bfdev_skip_head_t *head; diff --git a/src/textsearch/bm.c b/src/textsearch/bm.c index 5e8430aa..c32b63d3 100644 --- a/src/textsearch/bm.c +++ b/src/textsearch/bm.c @@ -112,7 +112,7 @@ bm_compute_prefix(struct bm_context *bctx, unsigned long flags) } static bfdev_ts_context_t * -bm_prepare(const struct bfdev_alloc *alloc, const void *pattern, +bm_prepare(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags) { unsigned int gsize = sizeof(unsigned int) * len; diff --git a/src/textsearch/kmp.c b/src/textsearch/kmp.c index 7a13c689..a31e1d87 100644 --- a/src/textsearch/kmp.c +++ b/src/textsearch/kmp.c @@ -81,7 +81,7 @@ kmp_compute_prefix(struct kmp_context *kctx) } static bfdev_ts_context_t * -kmp_prepare(const struct bfdev_alloc *alloc, const void *pattern, +kmp_prepare(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags) { unsigned int prefix_size = sizeof(unsigned int) * len; diff --git a/src/textsearch/sunday.c b/src/textsearch/sunday.c index d7e5718a..ed40f179 100644 --- a/src/textsearch/sunday.c +++ b/src/textsearch/sunday.c @@ -84,7 +84,7 @@ sunday_compute_prefix(struct sunday_context *sctx, unsigned int flags) } static bfdev_ts_context_t * -sunday_prepare(const struct bfdev_alloc *alloc, const void *pattern, +sunday_prepare(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags) { struct sunday_context *sctx; diff --git a/src/textsearch/textsearch.c b/src/textsearch/textsearch.c index dbd78ebe..9a94ec07 100644 --- a/src/textsearch/textsearch.c +++ b/src/textsearch/textsearch.c @@ -23,7 +23,7 @@ textsearch_algorithm_find(const char *name) } export bfdev_ts_context_t * -bfdev_textsearch_create(const struct bfdev_alloc *alloc, const char *name, +bfdev_textsearch_create(const bfdev_alloc_t *alloc, const char *name, const void *pattern, size_t len, unsigned long flags) { bfdev_ts_algorithm_t *algo; From 55804ccb9b61603278b7f012d2012287ee6b1d9d Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 17:18:03 +0800 Subject: [PATCH 048/119] feat circle: added initial typedef Signed-off-by: John Sanpe --- examples/circle/simple.c | 2 +- include/bfdev/circle.h | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/circle/simple.c b/examples/circle/simple.c index e54dbad0..340323b7 100644 --- a/examples/circle/simple.c +++ b/examples/circle/simple.c @@ -12,7 +12,7 @@ #define TEST_SIZE 16 #define TEST_LOOP (TEST_SIZE * 64) -struct bfdev_circle test_circle = { +static bfdev_circle_t test_circle = { .buffer = (char [TEST_SIZE]){}, }; diff --git a/include/bfdev/circle.h b/include/bfdev/circle.h index 70b5b2d2..37fbb4f2 100644 --- a/include/bfdev/circle.h +++ b/include/bfdev/circle.h @@ -10,6 +10,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_circle bfdev_circle_t; + struct bfdev_circle { void *buffer; unsigned long head; From a258d7973438c7d474f6e368ea04310bafd8a4c5 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 23:50:10 +0800 Subject: [PATCH 049/119] feat matrix: added initial trace Signed-off-by: John Sanpe --- examples/matrix/benchmark.c | 4 +- examples/matrix/fibonacci.c | 14 +++---- include/bfdev/matrix.h | 28 +++++++------- src/matrix.c | 76 ++++++++++++++++++------------------- 4 files changed, 62 insertions(+), 60 deletions(-) diff --git a/examples/matrix/benchmark.c b/examples/matrix/benchmark.c index 5e433bee..eb7db37f 100644 --- a/examples/matrix/benchmark.c +++ b/examples/matrix/benchmark.c @@ -31,8 +31,8 @@ for (count = 0; count < TEST_LOOP; ++count) { \ int main(int argc, char const *argv[]) { unsigned int count, loop; - struct bfdev_matrix *vara, *varb; - struct bfdev_matrix *result; + bfdev_matrix_t *vara, *varb; + bfdev_matrix_t *result; vara = bfdev_matrix_create(NULL, TEST_SIZE, TEST_SIZE); if (!vara) diff --git a/examples/matrix/fibonacci.c b/examples/matrix/fibonacci.c index c763761b..b9be90cb 100644 --- a/examples/matrix/fibonacci.c +++ b/examples/matrix/fibonacci.c @@ -10,7 +10,7 @@ #define TEST_BASE 1 #define TEST_MAXN 46 -static struct bfdev_matrix +static bfdev_matrix_t matrix_power = { .row = 2, .col = 2, .values = { @@ -19,7 +19,7 @@ matrix_power = { }, }; -static struct bfdev_matrix +static bfdev_matrix_t matrix_fibonacci = { .row = 2, .col = 2, .values = { @@ -28,11 +28,11 @@ matrix_fibonacci = { }, }; -static struct bfdev_matrix * -power(const struct bfdev_matrix *var, unsigned int pow) +static bfdev_matrix_t * +power(const bfdev_matrix_t *var, unsigned int pow) { - struct bfdev_matrix *result; - struct bfdev_matrix *tmp; + bfdev_matrix_t *result; + bfdev_matrix_t *tmp; result = bfdev_matrix_copy(NULL, &matrix_power); var = bfdev_matrix_copy(NULL, var); @@ -62,7 +62,7 @@ power(const struct bfdev_matrix *var, unsigned int pow) int main(int argc, const char *argv[]) { - struct bfdev_matrix *result; + bfdev_matrix_t *result; unsigned int count; for (count = TEST_BASE; count <= TEST_MAXN; ++count) { diff --git a/include/bfdev/matrix.h b/include/bfdev/matrix.h index ebeea859..cbb29764 100644 --- a/include/bfdev/matrix.h +++ b/include/bfdev/matrix.h @@ -11,6 +11,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_matrix bfdev_matrix_t; + struct bfdev_matrix { unsigned int row; unsigned int col; @@ -22,42 +24,42 @@ struct bfdev_matrix { * @va: first addend matrix. * @vb: second addend matrix. */ -extern struct bfdev_matrix * -bfdev_matrix_add(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, - const struct bfdev_matrix *vb); +extern bfdev_matrix_t * +bfdev_matrix_add(const bfdev_alloc_t *alloc, const bfdev_matrix_t *va, + const bfdev_matrix_t *vb); /** * bfdev_matrix_sub() - subtracting two matrices. * @va: minuend; matrix to subtract from. * @vb: subtrahend; matrix to subtract from @va. */ -extern struct bfdev_matrix * -bfdev_matrix_sub(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, - const struct bfdev_matrix *vb); +extern bfdev_matrix_t * +bfdev_matrix_sub(const bfdev_alloc_t *alloc, const bfdev_matrix_t *va, + const bfdev_matrix_t *vb); /** * bfdev_matrix_mul() - multiplying two matrices. * @va: first factor matrix. * @vb: second factor matrix. */ -extern struct bfdev_matrix * -bfdev_matrix_mul(const bfdev_alloc_t *alloc, const struct bfdev_matrix *va, - const struct bfdev_matrix *vb); +extern bfdev_matrix_t * +bfdev_matrix_mul(const bfdev_alloc_t *alloc, const bfdev_matrix_t *va, + const bfdev_matrix_t *vb); /** * bfdev_matrix_copy() - copy a new matrix. * @var: variables to copy. */ -extern struct bfdev_matrix * +extern bfdev_matrix_t * bfdev_matrix_copy(const bfdev_alloc_t *alloc, - const struct bfdev_matrix *var); + const bfdev_matrix_t *var); /** * bfdev_matrix_create() - create a new matrix. * @row: number of rows in the matrix. * @col: number of columns in the matrix. */ -extern struct bfdev_matrix * +extern bfdev_matrix_t * bfdev_matrix_create(const bfdev_alloc_t *alloc, unsigned int row, unsigned int col); @@ -67,7 +69,7 @@ bfdev_matrix_create(const bfdev_alloc_t *alloc, */ extern void bfdev_matrix_destory(const bfdev_alloc_t *alloc, - const struct bfdev_matrix *var); + const bfdev_matrix_t *var); BFDEV_END_DECLS diff --git a/src/matrix.c b/src/matrix.c index 22d7fe3b..59804c71 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -7,45 +7,45 @@ #include #include -#define GENERIC_MATRIX_ADDSUB(name, operate) \ -export struct bfdev_matrix * \ -bfdev_matrix_##name(const bfdev_alloc_t *alloc, \ - const struct bfdev_matrix *va, \ - const struct bfdev_matrix *vb) \ -{ \ - struct bfdev_matrix *result; \ - unsigned int row, col; \ - unsigned int size, count; \ - \ - row = va->row; \ - col = va->col; \ - \ - if (row != vb->row || col != vb->col) \ - return NULL; \ - \ - result = bfdev_matrix_create(alloc, row, col); \ - if (bfdev_unlikely(!result)) \ - return NULL; \ - \ - size = row * col; \ - for (count = 0; count < size; ++count) { \ - result->values[count] \ - = va->values[count] operate \ - vb->values[count]; \ - } \ - \ - return result; \ +#define GENERIC_MATRIX_ADDSUB(name, operate) \ +export bfdev_matrix_t * \ +bfdev_matrix_##name(const bfdev_alloc_t *alloc, \ + const bfdev_matrix_t *va, \ + const bfdev_matrix_t *vb) \ +{ \ + bfdev_matrix_t *result; \ + unsigned int row, col; \ + unsigned int size, count; \ + \ + row = va->row; \ + col = va->col; \ + \ + if (row != vb->row || col != vb->col) \ + return NULL; \ + \ + result = bfdev_matrix_create(alloc, row, col); \ + if (bfdev_unlikely(!result)) \ + return NULL; \ + \ + size = row * col; \ + for (count = 0; count < size; ++count) { \ + result->values[count] \ + = va->values[count] operate \ + vb->values[count]; \ + } \ + \ + return result; \ } GENERIC_MATRIX_ADDSUB(add, +) GENERIC_MATRIX_ADDSUB(sub, -) -export struct bfdev_matrix * +export bfdev_matrix_t * bfdev_matrix_mul(const bfdev_alloc_t *alloc, - const struct bfdev_matrix *va, - const struct bfdev_matrix *vb) + const bfdev_matrix_t *va, + const bfdev_matrix_t *vb) { - struct bfdev_matrix *result; + bfdev_matrix_t *result; unsigned int row, col; unsigned int count; long value; @@ -73,11 +73,11 @@ bfdev_matrix_mul(const bfdev_alloc_t *alloc, return result; } -export struct bfdev_matrix * +export bfdev_matrix_t * bfdev_matrix_copy(const bfdev_alloc_t *alloc, - const struct bfdev_matrix *var) + const bfdev_matrix_t *var) { - struct bfdev_matrix *result; + bfdev_matrix_t *result; unsigned int row, col; row = var->row; @@ -93,11 +93,11 @@ bfdev_matrix_copy(const bfdev_alloc_t *alloc, return result; } -export struct bfdev_matrix * +export bfdev_matrix_t * bfdev_matrix_create(const bfdev_alloc_t *alloc, unsigned int row, unsigned int col) { - struct bfdev_matrix *var; + bfdev_matrix_t *var; var = bfdev_zalloc(alloc, sizeof(*var) + sizeof(*var->values) * row * col); if (bfdev_unlikely(!var)) @@ -111,7 +111,7 @@ bfdev_matrix_create(const bfdev_alloc_t *alloc, export void bfdev_matrix_destory(const bfdev_alloc_t *alloc, - const struct bfdev_matrix *var) + const bfdev_matrix_t *var) { bfdev_free(alloc, var); } From 1da6cdaf932c200d926c9ba7de6b84275a2bce12 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 8 Jan 2024 23:53:06 +0800 Subject: [PATCH 050/119] feat notifier: added initial trace Signed-off-by: John Sanpe --- include/bfdev/notifier.h | 13 +++++++------ src/notifier.c | 10 +++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/include/bfdev/notifier.h b/include/bfdev/notifier.h index 55a74a90..4b1078a2 100644 --- a/include/bfdev/notifier.h +++ b/include/bfdev/notifier.h @@ -12,11 +12,12 @@ BFDEV_BEGIN_DECLS -struct bfdev_notifier_node; +typedef struct bfdev_notifier_head bfdev_notifier_head_t; +typedef struct bfdev_notifier_node bfdev_notifier_node_t; typedef enum bfdev_notifier_ret bfdev_notifier_ret_t; typedef bfdev_notifier_ret_t (*bfdev_notifier_entry_t) -(struct bfdev_notifier_node *node, void *args); +(bfdev_notifier_node_t *node, void *args); enum bfdev_notifier_ret { __BFDEV_NOTIFI_RET_DONE = 0, @@ -53,7 +54,7 @@ struct bfdev_notifier_head { }; #define bfdev_ilist_to_notifier(ptr) \ - bfdev_ilist_entry(ptr, struct bfdev_notifier_node, list) + bfdev_ilist_entry(ptr, bfdev_notifier_node_t, list) /** * bfdev_notifier_call - call the callback function of node on notification chain. @@ -63,7 +64,7 @@ struct bfdev_notifier_head { * @called_num: number of nodes actually notified. */ extern bfdev_notifier_ret_t -bfdev_notifier_call(struct bfdev_notifier_head *head, void *args, +bfdev_notifier_call(bfdev_notifier_head_t *head, void *args, unsigned int call_num, unsigned int *called_num); /** @@ -72,7 +73,7 @@ bfdev_notifier_call(struct bfdev_notifier_head *head, void *args, * @node: notification chain node to register. */ extern int -bfdev_notifier_register(struct bfdev_notifier_head *head, struct bfdev_notifier_node *node); +bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node); /** * bfdev_notifier_unregister - unregister a node from the notification chain. @@ -80,7 +81,7 @@ bfdev_notifier_register(struct bfdev_notifier_head *head, struct bfdev_notifier_ * @node: notification chain node to unregister. */ extern void -bfdev_notifier_unregister(struct bfdev_notifier_head *head, struct bfdev_notifier_node *node); +bfdev_notifier_unregister(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node); BFDEV_BEGIN_DECLS diff --git a/src/notifier.c b/src/notifier.c index 5a37a622..b5a98146 100755 --- a/src/notifier.c +++ b/src/notifier.c @@ -15,7 +15,7 @@ static long notifier_chain_cmp(const bfdev_ilist_node_t *node1, const bfdev_ilist_node_t *node2, void *pdata) { - struct bfdev_notifier_node *nnode1, *nnode2; + bfdev_notifier_node_t *nnode1, *nnode2; nnode1 = bfdev_ilist_to_notifier(node1); nnode2 = bfdev_ilist_to_notifier(node2); @@ -27,10 +27,10 @@ notifier_chain_cmp(const bfdev_ilist_node_t *node1, } export bfdev_notifier_ret_t -bfdev_notifier_call(struct bfdev_notifier_head *head, void *arg, +bfdev_notifier_call(bfdev_notifier_head_t *head, void *arg, unsigned int call_num, unsigned int *called_num) { - struct bfdev_notifier_node *node, *tmp; + bfdev_notifier_node_t *node, *tmp; bfdev_notifier_ret_t retval = BFDEV_NOTIFI_RET_DONE; bfdev_ilist_for_each_entry_safe(node, tmp, &head->node, list) { @@ -56,7 +56,7 @@ bfdev_notifier_call(struct bfdev_notifier_head *head, void *arg, } export int -bfdev_notifier_register(struct bfdev_notifier_head *head, struct bfdev_notifier_node *node) +bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node) { if (!node->entry) return -BFDEV_EINVAL; @@ -69,7 +69,7 @@ bfdev_notifier_register(struct bfdev_notifier_head *head, struct bfdev_notifier_ } export void -bfdev_notifier_unregister(struct bfdev_notifier_head *head, struct bfdev_notifier_node *node) +bfdev_notifier_unregister(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node) { bfdev_ilist_del(&head->node, &node->list); bfdev_log_debug("chain '%s' unregister (%p)\n", head->name, node); From 6a972a9c53aff7b709498975c494148de02fe9ef Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 9 Jan 2024 00:58:06 +0800 Subject: [PATCH 051/119] refactor typedef: fixed atomicity issues Signed-off-by: John Sanpe --- examples/circle/.gitignore | 2 +- examples/circle/CMakeLists.txt | 10 +++++----- examples/circle/{simple.c => atomic.c} | 3 ++- examples/fifo/.gitignore | 2 +- examples/fifo/CMakeLists.txt | 10 +++++----- examples/fifo/{simple.c => atomic.c} | 1 + include/bfdev/circle.h | 6 +++--- 7 files changed, 18 insertions(+), 16 deletions(-) rename examples/circle/{simple.c => atomic.c} (96%) rename examples/fifo/{simple.c => atomic.c} (99%) diff --git a/examples/circle/.gitignore b/examples/circle/.gitignore index 4b309451..42d5999c 100644 --- a/examples/circle/.gitignore +++ b/examples/circle/.gitignore @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-or-later -/circle-simple +/circle-atomic diff --git a/examples/circle/CMakeLists.txt b/examples/circle/CMakeLists.txt index e737b6bc..d63c06dd 100644 --- a/examples/circle/CMakeLists.txt +++ b/examples/circle/CMakeLists.txt @@ -3,19 +3,19 @@ # Copyright(c) 2023 ffashion # -add_executable(circle-simple simple.c) -target_link_libraries(circle-simple bfdev) -add_test(circle-simple circle-simple) +add_executable(circle-atomic atomic.c) +target_link_libraries(circle-atomic bfdev) +add_test(circle-atomic circle-atomic) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES - simple.c + atomic.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/circle ) install(TARGETS - circle-simple + circle-atomic DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin ) diff --git a/examples/circle/simple.c b/examples/circle/atomic.c similarity index 96% rename from examples/circle/simple.c rename to examples/circle/atomic.c index 340323b7..2444383b 100644 --- a/examples/circle/simple.c +++ b/examples/circle/atomic.c @@ -12,7 +12,8 @@ #define TEST_SIZE 16 #define TEST_LOOP (TEST_SIZE * 64) -static bfdev_circle_t test_circle = { +static volatile bfdev_circle_t +test_circle = { .buffer = (char [TEST_SIZE]){}, }; diff --git a/examples/fifo/.gitignore b/examples/fifo/.gitignore index ebb431a7..d7f3d7b6 100644 --- a/examples/fifo/.gitignore +++ b/examples/fifo/.gitignore @@ -1,3 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-or-later /fifo-selftest -/fifo-simple +/fifo-atomic diff --git a/examples/fifo/CMakeLists.txt b/examples/fifo/CMakeLists.txt index f6a310d7..3269498b 100644 --- a/examples/fifo/CMakeLists.txt +++ b/examples/fifo/CMakeLists.txt @@ -7,21 +7,21 @@ add_executable(fifo-selftest selftest.c) target_link_libraries(fifo-selftest bfdev) add_test(fifo-selftest fifo-selftest) -add_executable(fifo-simple simple.c) -target_link_libraries(fifo-simple bfdev pthread) -add_test(fifo-simple fifo-simple) +add_executable(fifo-atomic atomic.c) +target_link_libraries(fifo-atomic bfdev pthread) +add_test(fifo-atomic fifo-atomic) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES selftest.c - simple.c + atomic.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/fifo ) install(TARGETS fifo-selftest - fifo-simple + fifo-atomic DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin ) diff --git a/examples/fifo/simple.c b/examples/fifo/atomic.c similarity index 99% rename from examples/fifo/simple.c rename to examples/fifo/atomic.c index 7dd3fbc3..d1dc7317 100644 --- a/examples/fifo/simple.c +++ b/examples/fifo/atomic.c @@ -11,6 +11,7 @@ #define TEST_SIZE 16 #define TEST_LOOP (TEST_SIZE * 64) +static BFDEV_DEFINE_FIFO(fifo_test, char, TEST_SIZE); static void * diff --git a/include/bfdev/circle.h b/include/bfdev/circle.h index 37fbb4f2..1925368e 100644 --- a/include/bfdev/circle.h +++ b/include/bfdev/circle.h @@ -14,8 +14,8 @@ typedef struct bfdev_circle bfdev_circle_t; struct bfdev_circle { void *buffer; - unsigned long head; - unsigned long tail; + volatile unsigned long head; + volatile unsigned long tail; }; /* Return count in buffer */ @@ -24,7 +24,7 @@ struct bfdev_circle { /* Return space available */ #define BFDEV_CIRCLE_SPACE(head, tail, size) \ - (((tail) - (head) + 1) & ((size) - 1)) + (((tail) - ((head) + 1)) & ((size) - 1)) /* Return count up to the end of the buffer */ #define BFDEV_CIRCLE_CNT_END(head, tail, size) ({ \ From 11066a11455bc3a80c39f0f2d4efc4dcf2972d36 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 9 Jan 2024 00:59:24 +0800 Subject: [PATCH 052/119] refactor typedef: fixed some typo Signed-off-by: John Sanpe --- include/bfdev/action.h | 2 +- include/bfdev/allocator.h | 2 +- include/bfdev/cache.h | 2 +- include/bfdev/list.h | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/bfdev/action.h b/include/bfdev/action.h index faccf388..669d07f3 100644 --- a/include/bfdev/action.h +++ b/include/bfdev/action.h @@ -18,7 +18,7 @@ typedef struct bfdev_action bfdev_action_t; typedef int (*bfdev_action_func_t)(void *data); -struct bfdev_action { +struct bfdev_action { bfdev_action_func_t func; const void *data; }; diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index ee2525dd..4afc62ba 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -21,7 +21,7 @@ struct bfdev_alloc { void *pdata; }; -struct bfdev_alloc_ops { +struct bfdev_alloc_ops { bfdev_malloc_t alloc; bfdev_realloc_t realloc; bfdev_free_t free; diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index 61451614..bcbdc29f 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -46,7 +46,7 @@ enum bfdev_cache_status { BFDEV_CACHE_MANAGED, }; -struct bfdev_cache_node { +struct bfdev_cache_node { bfdev_hlist_node_t hash; bfdev_list_head_t list; enum bfdev_cache_status status; diff --git a/include/bfdev/list.h b/include/bfdev/list.h index 8ea4637b..5ad4f31c 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -14,9 +14,9 @@ BFDEV_BEGIN_DECLS -typedef struct bfdev_list_head bfdev_list_head_t; +typedef struct bfdev_list_head bfdev_list_head_t; -struct bfdev_list_head { +struct bfdev_list_head { bfdev_list_head_t *prev; bfdev_list_head_t *next; }; From 80964505c4d1982627220991d7ebdff34307da06 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 10 Jan 2024 20:12:18 +0800 Subject: [PATCH 053/119] feat prandom: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/prandom.h | 10 ++++++---- src/prandom.c | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/bfdev/prandom.h b/include/bfdev/prandom.h index b311d7e2..89e9342c 100755 --- a/include/bfdev/prandom.h +++ b/include/bfdev/prandom.h @@ -13,6 +13,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_prandom_state bfdev_prandom_state_t; + struct bfdev_prandom_state { uint32_t s1, s2; uint32_t s3, s4; @@ -30,24 +32,24 @@ struct bfdev_prandom_state { * @seed: arbitrary 64-bit value to use as a seed. */ extern void -bfdev_prandom_seed(struct bfdev_prandom_state *pstate, uint64_t seed); +bfdev_prandom_seed(bfdev_prandom_state_t *pstate, uint64_t seed); /** * bfdev_prandom_value() - seeded pseudo-random number generator. * @state: pointer to state structure holding seeded state. */ extern uint32_t -bfdev_prandom_value(struct bfdev_prandom_state *pstate); +bfdev_prandom_value(bfdev_prandom_state_t *pstate); static inline uint64_t -bfdev_prandom_u64(struct bfdev_prandom_state *pstate) +bfdev_prandom_u64(bfdev_prandom_state_t *pstate) { uint32_t high = bfdev_prandom_value(pstate); return ((uint64_t)high << 32) + bfdev_prandom_value(pstate); } static inline unsigned long -bfdev_prandom_long(struct bfdev_prandom_state *pstate) +bfdev_prandom_long(bfdev_prandom_state_t *pstate) { #if BFDEV_BITS_PER_LONG == 32 return bfdev_prandom_value(pstate); diff --git a/src/prandom.c b/src/prandom.c index e4794e23..a8820be4 100644 --- a/src/prandom.c +++ b/src/prandom.c @@ -14,7 +14,7 @@ prandom_seed_minimum(uint32_t x, uint32_t m) } export uint32_t -bfdev_prandom_value(struct bfdev_prandom_state *pstate) +bfdev_prandom_value(bfdev_prandom_state_t *pstate) { pstate->s1 = BFDEV_TAUSWORTHE(pstate->s1, 6U, 13U, 4294967294U, 18U); pstate->s2 = BFDEV_TAUSWORTHE(pstate->s2, 2U, 27U, 4294967288U, 2U); @@ -24,7 +24,7 @@ bfdev_prandom_value(struct bfdev_prandom_state *pstate) } static void -bfdev_prandom_setup(struct bfdev_prandom_state *pstate, uint64_t seed) +bfdev_prandom_setup(bfdev_prandom_state_t *pstate, uint64_t seed) { seed = bfdev_lower_32_bits((seed >> 32) ^ (seed << 10) ^ seed); pstate->s1 = prandom_seed_minimum(seed, 2U); @@ -34,7 +34,7 @@ bfdev_prandom_setup(struct bfdev_prandom_state *pstate, uint64_t seed) } static __bfdev_always_inline void -bfdev_prandom_warmup(struct bfdev_prandom_state *pstate) +bfdev_prandom_warmup(bfdev_prandom_state_t *pstate) { unsigned int count; for (count = 0; count < 10; ++count) @@ -42,7 +42,7 @@ bfdev_prandom_warmup(struct bfdev_prandom_state *pstate) } export void -bfdev_prandom_seed(struct bfdev_prandom_state *pstate, uint64_t seed) +bfdev_prandom_seed(bfdev_prandom_state_t *pstate, uint64_t seed) { bfdev_prandom_setup(pstate, seed); bfdev_prandom_warmup(pstate); From 8286003693abf8654e1aafbb7a3970b4291454d8 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 11 Jan 2024 00:12:19 +0800 Subject: [PATCH 054/119] fixup byteorder: fixup some typo Signed-off-by: John Sanpe --- include/bfdev/asm-generic/byteorder.h | 9 ++++++++- .../bfdev/byteorder/{big_endian.h => big-endian.h} | 14 +++++++------- .../byteorder/{little_endian.h => little-endian.h} | 8 ++++---- 3 files changed, 19 insertions(+), 12 deletions(-) rename include/bfdev/byteorder/{big_endian.h => big-endian.h} (96%) rename include/bfdev/byteorder/{little_endian.h => little-endian.h} (100%) diff --git a/include/bfdev/asm-generic/byteorder.h b/include/bfdev/asm-generic/byteorder.h index 0942ae85..b71d65ab 100644 --- a/include/bfdev/asm-generic/byteorder.h +++ b/include/bfdev/asm-generic/byteorder.h @@ -6,6 +6,13 @@ #ifndef _BFDEV_ASM_GENERIC_BYTEORDER_H_ #define _BFDEV_ASM_GENERIC_BYTEORDER_H_ -#include +#include +#include + +BFDEV_BEGIN_DECLS + +/* Nothing */ + +BFDEV_END_DECLS #endif /* _BFDEV_ASM_GENERIC_BYTEORDER_H_ */ diff --git a/include/bfdev/byteorder/big_endian.h b/include/bfdev/byteorder/big-endian.h similarity index 96% rename from include/bfdev/byteorder/big_endian.h rename to include/bfdev/byteorder/big-endian.h index 5111fac3..ee914a51 100644 --- a/include/bfdev/byteorder/big_endian.h +++ b/include/bfdev/byteorder/big-endian.h @@ -3,12 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#ifndef _BFDEV_BYTEORDER_LITTLE_ENDIAN_H_ -#define _BFDEV_BYTEORDER_LITTLE_ENDIAN_H_ - -#ifndef __BFDEV_BIG_ENDIAN__ -# define __BFDEV_BIG_ENDIAN__ -#endif +#ifndef _BFDEV_BYTEORDER_BIG_ENDIAN_H_ +#define _BFDEV_BYTEORDER_BIG_ENDIAN_H_ #include #include @@ -16,6 +12,10 @@ BFDEV_BEGIN_DECLS +#ifndef __BFDEV_BIG_ENDIAN__ +# define __BFDEV_BIG_ENDIAN__ +#endif + #define bfdev_htons_const(x) ((bfdev_be16)(uint16_t)(x)) #define bfdev_htonl_const(x) ((bfdev_be32)(uint32_t)(x)) #define bfdev_ntohl_const(x) ((uint32_t)(bfdev_be32)(x)) @@ -137,4 +137,4 @@ bfdev_be64_to_cpup(const bfdev_be64 *p) BFDEV_END_DECLS -#endif /* _BFDEV_BYTEORDER_LITTLE_ENDIAN_H_ */ +#endif /* _BFDEV_BYTEORDER_BIG_ENDIAN_H_ */ diff --git a/include/bfdev/byteorder/little_endian.h b/include/bfdev/byteorder/little-endian.h similarity index 100% rename from include/bfdev/byteorder/little_endian.h rename to include/bfdev/byteorder/little-endian.h index fbb08712..2abbd1c5 100644 --- a/include/bfdev/byteorder/little_endian.h +++ b/include/bfdev/byteorder/little-endian.h @@ -6,16 +6,16 @@ #ifndef _BFDEV_BYTEORDER_LITTLE_ENDIAN_H_ #define _BFDEV_BYTEORDER_LITTLE_ENDIAN_H_ -#ifndef __BFDEV_LITTLE_ENDIAN__ -# define __BFDEV_LITTLE_ENDIAN__ -#endif - #include #include #include BFDEV_BEGIN_DECLS +#ifndef __BFDEV_LITTLE_ENDIAN__ +# define __BFDEV_LITTLE_ENDIAN__ +#endif + #define bfdev_htons_const(x) ((bfdev_be16)bfdev_swab16_const((x))) #define bfdev_htonl_const(x) ((bfdev_be32)bfdev_swab32_const((x))) #define bfdev_ntohl_const(x) bfdev_swab32_const((bfdev_be32)(x)) From a99413cb666f6ed1508a497f8bdf8675a17c6532 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 12 Jan 2024 22:43:49 +0800 Subject: [PATCH 055/119] feat compiler: added barrier function for data only Signed-off-by: John Sanpe --- include/bfdev/compiler.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/bfdev/compiler.h b/include/bfdev/compiler.h index 14243383..645ac989 100644 --- a/include/bfdev/compiler.h +++ b/include/bfdev/compiler.h @@ -22,8 +22,13 @@ BFDEV_BEGIN_DECLS * Optimization barrier * The "volatile" is due to gcc bugs */ +#ifndef __bfdev_barrier +# define __bfdev_barrier(carrier) __asm__ __volatile__("":carrier:"memory") +#endif + #ifndef bfdev_barrier -# define bfdev_barrier() __asm__ __volatile__("": : :"memory") +# define bfdev_barrier() __bfdev_barrier(:) +# define bfdev_barrier_data(ptr) __bfdev_barrier(:"r"(ptr)) #endif /* Not-quite-unique ID. */ From 79a5ebf2a1f83fa3383901cecac968c7d8cabf91 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 13 Jan 2024 23:33:08 +0800 Subject: [PATCH 056/119] feat compiler: added type signed check Signed-off-by: John Sanpe --- include/bfdev/compiler.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/bfdev/compiler.h b/include/bfdev/compiler.h index 645ac989..8ea54b67 100644 --- a/include/bfdev/compiler.h +++ b/include/bfdev/compiler.h @@ -31,6 +31,13 @@ BFDEV_BEGIN_DECLS # define bfdev_barrier_data(ptr) __bfdev_barrier(:"r"(ptr)) #endif +/* + * Whether 'type' is a signed type or an unsigned type. + * Supports scalar types, bool and also pointer types. + */ +#define bfdev_is_signed(type) (((type)(-1)) < (type)1) +#define bfdev_is_unsigned(type) (!bfdev_is_signed(type)) + /* Not-quite-unique ID. */ #ifndef __BFDEV_UNIQUE_ID # define __BFDEV_UNIQUE_ID(prefix) __BFDEV_PASTE(__BFDEV_PASTE(__UNIQUE_ID_, prefix), __LINE__) From bde2dcbaa4d5e36e9235d9ff6b42acff0f26406d Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 15 Jan 2024 16:07:21 +0800 Subject: [PATCH 057/119] fixup titer: fixed level-order macro typo Signed-off-by: John Sanpe --- include/bfdev/titer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/bfdev/titer.h b/include/bfdev/titer.h index be18da3a..c68185c0 100644 --- a/include/bfdev/titer.h +++ b/include/bfdev/titer.h @@ -223,7 +223,7 @@ TISTATIC TISTRUCT * TINAME##_next(const TIROOT *root, unsigned long *index) \ { \ unsigned int depth = bfdev_flsuf(++*index + 1); \ - TISTRUCT *node = root->node; \ + TISTRUCT *node = root->TINODE; \ \ while (node && depth--) { \ if ((*index + 1) & BFDEV_BIT(depth)) \ From 23628bf1bef343e54b16cdf8dd6035920b1ade0a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 18 Jan 2024 21:37:05 +0800 Subject: [PATCH 058/119] fixup heap: fixed several issues - fixed the issue of find object errors - delete some useless initialization macros Signed-off-by: John Sanpe --- include/bfdev/heap.h | 192 +++++++++++++++++++++---------------------- src/cache/lfu.c | 6 +- src/cache/lru.c | 6 ++ src/heap.c | 30 ++++--- 4 files changed, 126 insertions(+), 108 deletions(-) diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index 11989915..f5e52eeb 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -25,34 +25,22 @@ struct bfdev_heap_node { struct bfdev_heap_root { bfdev_heap_node_t *node; - unsigned int count; + unsigned long count; }; #define BFDEV_HEAP_STATIC \ {NULL, 0} -#define BFDEV_HEAP_CACHED_STATIC \ - {{NULL}, NULL} - #define BFDEV_HEAP_INIT \ (bfdev_heap_root_t) BFDEV_HEAP_STATIC #define BFDEV_HEAP_ROOT(name) \ bfdev_heap_root_t name = BFDEV_HEAP_INIT -#define BFDEV_HEAP_EMPTY_ROOT(root) \ - ((root)->node == NULL) - -#define BFDEV_HEAP_EMPTY_NODE(node) \ - ((node)->parent == (node)) - -#define BFDEV_HEAP_CLEAR_NODE(node) \ - ((node)->parent = (node)) - #define BFDEV_HEAP_ROOT_NODE(root) \ ((root)->node) -#define BFDEV_HEAP_NODE_COUNT(root) \ +#define BFDEV_HEAP_ROOT_COUNT(root) \ ((root)->count) /** @@ -93,6 +81,12 @@ bfdev_heap_init(bfdev_heap_root_t *root) *root = BFDEV_HEAP_INIT; } +static inline bool +bfdev_heap_empty_root(bfdev_heap_root_t *root) +{ + return !root->node; +} + /** * bfdev_heap_fixup - balance after insert node. * @root: heap root of node. @@ -137,7 +131,86 @@ bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, * @index: index of node. */ extern bfdev_heap_node_t * -bfdev_heap_find(bfdev_heap_root_t *root, unsigned int index); +bfdev_heap_find(bfdev_heap_root_t *root, unsigned long index); + +/** + * bfdev_heap_link - link node to parent. + * @root: heap root of node. + * @parent: point to parent node. + * @link: point to pointer to child node. + * @node: new node to link. + */ +static inline void +bfdev_heap_link(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t **link, bfdev_heap_node_t *node) +{ +#ifdef BFDEV_DEBUG_HEAP + if (bfdev_unlikely(!bfdev_heap_check_link(parent, link, node))) + return; +#endif + + /* link = &parent->left/right */ + *link = node; + node->parent = parent; + node->left = node->right = NULL; + root->count++; +} + +/** + * bfdev_heap_insert_node - link node to parent and fixup heap. + * @root: heap root of node. + * @parent: parent node of node. + * @link: point to pointer to child node. + * @node: new node to link. + */ +static inline void +bfdev_heap_insert_node(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, + bfdev_heap_node_t **link, bfdev_heap_node_t *node, + bfdev_heap_cmp_t cmp, void *pdata) +{ + bfdev_heap_link(root, parent, link, node); + bfdev_heap_fixup(root, node, cmp, pdata); +} + +/** + * bfdev_heap_insert - find the parent node and insert new node. + * @root: heap root of node. + * @node: new node to insert. + * @cmp: operator defining the node order. + */ +static inline void +bfdev_heap_insert(bfdev_heap_root_t *root, bfdev_heap_node_t *node, + bfdev_heap_cmp_t cmp, void *pdata) +{ + bfdev_heap_node_t *parent, **link; + + link = bfdev_heap_parent(root, &parent, node); + bfdev_heap_insert_node(root, parent, link, node, cmp, pdata); +} + +/** + * bfdev_heap_delete - delete node and fixup heap. + * @root: heap root of node. + * @node: node to delete. + */ +static inline void +bfdev_heap_delete(bfdev_heap_root_t *root, bfdev_heap_node_t *node, + bfdev_heap_cmp_t cmp, void *pdata) +{ + bfdev_heap_node_t *rebalance; + +#ifdef BFDEV_DEBUG_HEAP + if (bfdev_unlikely(!bfdev_heap_check_delete(node))) + return; +#endif + + if ((rebalance = bfdev_heap_remove(root, node))) + bfdev_heap_erase(root, rebalance, cmp, pdata); + + node->left = BFDEV_POISON_HPNODE1; + node->right = BFDEV_POISON_HPNODE2; + node->parent = BFDEV_POISON_HPNODE3; +} /* Base iteration - basic iteration helper */ extern bfdev_heap_node_t * @@ -168,7 +241,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_first_entry - get the preorder first element from a heap. - * @ptr: the heap root to take the element from. + * @root: the heap root to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_heap_node within the struct. */ @@ -177,6 +250,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_next_entry - get the preorder next element in heap. + * @root: the heap root to take the element from. * @pos: the type * to cursor. * @member: the name of the bfdev_heap_node within the struct. */ @@ -186,7 +260,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each - preorder iterate over a heap. * @pos: the &bfdev_heap_node_t to use as a loop cursor. - * @root: the root for your heap. + * @root: the heap root to take the element from. */ #define bfdev_heap_for_each(pos, index, root) \ for (pos = bfdev_heap_first(root, index); \ @@ -195,6 +269,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each_from - preorder iterate over a heap from the current point. * @pos: the &bfdev_heap_node_t to use as a loop cursor. + * @root: the heap root to take the element from. */ #define bfdev_heap_for_each_from(pos, index, root) \ for (; pos; pos = bfdev_heap_next(root, index)) @@ -202,6 +277,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each_continue - continue preorder iteration over a heap. * @pos: the &bfdev_heap_node_t to use as a loop cursor. + * @root: the heap root to take the element from. */ #define bfdev_heap_for_each_continue(pos, index, root) \ for (pos = bfdev_heap_next(root, index); \ @@ -210,7 +286,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each_entry - preorder iterate over heap of given type. * @pos: the type * to use as a loop cursor. - * @root: the root for your heap. + * @root: the heap root to take the element from. * @member: the name of the bfdev_heap_node within the struct. */ #define bfdev_heap_for_each_entry(pos, index, root, member) \ @@ -220,6 +296,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each_entry_from - preorder iterate over heap of given type from the current point. * @pos: the type * to use as a loop cursor. + * @root: the heap root to take the element from. * @member: the name of the bfdev_heap_node within the struct. */ #define bfdev_heap_for_each_entry_from(pos, index, root, member) \ @@ -228,6 +305,7 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); /** * bfdev_heap_for_each_entry_continue - continue preorder iteration over heap of given type. * @pos: the type * to use as a loop cursor. + * @root: the heap root to take the element from. * @member: the name of the bfdev_heap_node within the struct. */ #define bfdev_heap_for_each_entry_continue(pos, index, root, member) \ @@ -360,84 +438,6 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); pos && ({ tmp = bfdev_heap_post_next_entry(pos, member); \ 1; }); pos = tmp) -/** - * bfdev_heap_link - link node to parent. - * @root: heap root of node. - * @parent: point to parent node. - * @link: point to pointer to child node. - * @node: new node to link. - */ -static inline void -bfdev_heap_link(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, - bfdev_heap_node_t **link, bfdev_heap_node_t *node) -{ -#ifdef BFDEV_DEBUG_HEAP - if (bfdev_unlikely(!bfdev_heap_check_link(parent, link, node))) - return; -#endif - - /* link = &parent->left/right */ - *link = node; - node->parent = parent; - node->left = node->right = NULL; - root->count++; -} - -/** - * bfdev_heap_insert_node - link node to parent and fixup heap. - * @root: heap root of node. - * @parent: parent node of node. - * @link: point to pointer to child node. - * @node: new node to link. - */ -static inline void -bfdev_heap_insert_node(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, - bfdev_heap_node_t **link, bfdev_heap_node_t *node, - bfdev_heap_cmp_t cmp, void *pdata) -{ - bfdev_heap_link(root, parent, link, node); - bfdev_heap_fixup(root, node, cmp, pdata); -} - -/** - * bfdev_heap_insert - find the parent node and insert new node. - * @root: heap root of node. - * @node: new node to insert. - * @cmp: operator defining the node order. - */ -static inline void -bfdev_heap_insert(bfdev_heap_root_t *root, bfdev_heap_node_t *node, - bfdev_heap_cmp_t cmp, void *pdata) -{ - bfdev_heap_node_t *parent, **link; - - link = bfdev_heap_parent(root, &parent, node); - bfdev_heap_insert_node(root, parent, link, node, cmp, pdata); -} - -/** - * bfdev_heap_delete - delete node and fixup heap. - * @root: heap root of node. - * @node: node to delete. - */ -static inline void -bfdev_heap_delete(bfdev_heap_root_t *root, bfdev_heap_node_t *node, - bfdev_heap_cmp_t cmp, void *pdata) -{ - bfdev_heap_node_t *rebalance; - -#ifdef BFDEV_DEBUG_HEAP - if (bfdev_unlikely(!bfdev_heap_check_delete(node))) - return; -#endif - - if ((rebalance = bfdev_heap_remove(root, node))) - bfdev_heap_erase(root, rebalance, cmp, pdata); - - node->left = BFDEV_POISON_HPNODE1; - node->right = BFDEV_POISON_HPNODE2; - node->parent = BFDEV_POISON_HPNODE3; -} BFDEV_END_DECLS diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 1777d7d3..07e57242 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -46,8 +46,10 @@ static bool lfu_starving(bfdev_cache_head_t *head) { struct lfu_head *lfu_head; + lfu_head = cache_to_lfu_head(head); - return BFDEV_HEAP_EMPTY_ROOT(&lfu_head->lfu); + + return bfdev_heap_empty_root(&lfu_head->lfu); } static bfdev_cache_node_t * @@ -91,6 +93,7 @@ static void lfu_update(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_node *lfu_node; + lfu_node = cache_to_lfu_node(node); lfu_node->count++; } @@ -99,6 +102,7 @@ static void lfu_clear(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lfu_node *lfu_node; + lfu_node = cache_to_lfu_node(node); lfu_node->count = 0; } diff --git a/src/cache/lru.c b/src/cache/lru.c index 9f9470d4..9846afa9 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -26,7 +26,9 @@ static bool lru_starving(bfdev_cache_head_t *head) { struct lru_head *lru_head; + lru_head = cache_to_lru_head(head); + return bfdev_list_check_empty(&lru_head->lru); } @@ -47,7 +49,9 @@ static void lru_get(bfdev_cache_head_t *head, bfdev_cache_node_t *node) { struct lru_node *lru_node; + lru_node = cache_to_lru_node(node); + bfdev_list_del(&lru_node->node); } @@ -67,7 +71,9 @@ static void lru_reset(bfdev_cache_head_t *head) { struct lru_head *lru_head; + lru_head = cache_to_lru_head(head); + bfdev_list_head_init(&lru_head->lru); } diff --git a/src/heap.c b/src/heap.c index 3743a68e..f2c91ea1 100644 --- a/src/heap.c +++ b/src/heap.c @@ -12,14 +12,14 @@ static __bfdev_always_inline void parent_swap(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, bfdev_heap_node_t *node) { - bfdev_heap_node_t *gparent = parent->parent; - bfdev_heap_node_t shadow = *node; + bfdev_heap_node_t shadow, *gparent; if (node->left) node->left->parent = parent; if (node->right) node->right->parent = parent; + shadow = *node; if (parent->left == node) { node->left = parent; if ((node->right = parent->right)) @@ -30,7 +30,10 @@ parent_swap(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, parent->left->parent = node; } - if (!(node->parent = gparent)) + gparent = parent->parent; + node->parent = gparent; + + if (!gparent) root->node = node; else if (gparent->left == parent) gparent->left = node; @@ -58,9 +61,9 @@ export void bfdev_heap_erase(bfdev_heap_root_t *root, bfdev_heap_node_t *node, bfdev_heap_cmp_t cmp, void *pdata) { - bfdev_heap_node_t *successor = node->parent; - bfdev_heap_node_t *child1, *child2; + bfdev_heap_node_t *child1, *child2, *successor; + successor = node->parent; if (successor && cmp(node, successor, pdata) < 0) bfdev_heap_fixup(root, node, cmp, pdata); @@ -90,8 +93,9 @@ bfdev_heap_erase(bfdev_heap_root_t *root, bfdev_heap_node_t *node, export bfdev_heap_node_t * bfdev_heap_remove(bfdev_heap_root_t *root, bfdev_heap_node_t *node) { - bfdev_heap_node_t *successor = bfdev_heap_find(root, root->count); + bfdev_heap_node_t *successor; + successor = bfdev_heap_find(root, root->count); if (!successor->parent) { /* * Case 1: node to delete is root. @@ -167,8 +171,8 @@ export bfdev_heap_node_t ** bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, bfdev_heap_node_t *node) { - unsigned int depth = bfdev_flsuf(root->count + 1); bfdev_heap_node_t **link; + unsigned int depth; link = &root->node; if (bfdev_unlikely(!*link)) { @@ -176,6 +180,7 @@ bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, return link; } + depth = bfdev_flsuf(root->count + 1); while (depth--) { *parentp = *link; if ((root->count + 1) & BFDEV_BIT(depth)) @@ -188,13 +193,16 @@ bfdev_heap_parent(bfdev_heap_root_t *root, bfdev_heap_node_t **parentp, } export bfdev_heap_node_t * -bfdev_heap_find(bfdev_heap_root_t *root, unsigned int index) +bfdev_heap_find(bfdev_heap_root_t *root, unsigned long index) { - unsigned int depth = bfdev_flsuf(root->count); - bfdev_heap_node_t *node = root->node; + bfdev_heap_node_t *node; + unsigned int depth; + + depth = bfdev_flsuf(root->count); + node = root->node; while (node && depth--) { - if ((root->count) & BFDEV_BIT(depth)) + if (index & BFDEV_BIT(depth)) node = node->right; else node = node->left; From 98b01fba13438bb317a8af01042b160a139fb651 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 14 Jan 2024 22:29:45 +0800 Subject: [PATCH 059/119] fixup circle: remove inappropriate volatile Signed-off-by: John Sanpe --- include/bfdev/circle.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/bfdev/circle.h b/include/bfdev/circle.h index 1925368e..e64f75db 100644 --- a/include/bfdev/circle.h +++ b/include/bfdev/circle.h @@ -14,8 +14,8 @@ typedef struct bfdev_circle bfdev_circle_t; struct bfdev_circle { void *buffer; - volatile unsigned long head; - volatile unsigned long tail; + unsigned long head; + unsigned long tail; }; /* Return count in buffer */ From adfd8f97195c1b55622db95df5f7ae053803f669 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 19 Jan 2024 22:08:51 +0800 Subject: [PATCH 060/119] refactor rbtree: remove conflict functions Signed-off-by: John Sanpe --- examples/rbtree/benchmark.c | 10 +- examples/rbtree/selftest.c | 314 ++++++++++++++++++------------------ examples/rbtree/simple.c | 12 +- include/bfdev/rbtree.h | 139 +++------------- src/rbtree.c | 105 ++++++------ 5 files changed, 237 insertions(+), 343 deletions(-) diff --git a/examples/rbtree/benchmark.c b/examples/rbtree/benchmark.c index ed0a3846..020601dd 100644 --- a/examples/rbtree/benchmark.c +++ b/examples/rbtree/benchmark.c @@ -69,7 +69,11 @@ test_deepth(bfdev_rb_node_t *node) left_deepth = test_deepth(node->left); right_deepth = test_deepth(node->right); - return left_deepth > right_deepth ? (left_deepth + 1) : (right_deepth + 1); + + if (left_deepth > right_deepth) + left_deepth = right_deepth; + + return right_deepth + 1; } static long @@ -81,9 +85,7 @@ demo_cmp(const bfdev_rb_node_t *node1, test1 = rb_to_bench(node1); test2 = rb_to_bench(node2); - if (test1->data == test2->data) - return 0; - + /* Ignoring conflicts */ return test1->data < test2->data ? -1 : 1; } diff --git a/examples/rbtree/selftest.c b/examples/rbtree/selftest.c index 2c921085..cc5ab8b9 100644 --- a/examples/rbtree/selftest.c +++ b/examples/rbtree/selftest.c @@ -10,52 +10,50 @@ #define TEST_LOOP 100 -struct rbtree_test_node { +struct demo_node { bfdev_rb_node_t node; unsigned long data; }; -struct rbtree_test_pdata { - struct rbtree_test_node nodes[TEST_LOOP]; +struct demo_data { + struct demo_node nodes[TEST_LOOP]; }; #define rbnode_to_test(ptr) \ - bfdev_rb_entry(ptr, struct rbtree_test_node, node) + bfdev_rb_entry(ptr, struct demo_node, node) #define rbnode_to_test_safe(ptr) \ - bfdev_rb_entry_safe(ptr, struct rbtree_test_node, node) + bfdev_rb_entry_safe(ptr, struct demo_node, node) static long -rbtest_rb_cmp(const bfdev_rb_node_t *rba, const bfdev_rb_node_t *rbb, void *pdata) +rbtest_rb_cmp(const bfdev_rb_node_t *node1, const bfdev_rb_node_t *node2, void *pdata) { - struct rbtree_test_node *node1, *node2; + struct demo_node *demo1, *demo2; - node1 = rbnode_to_test(rba); - node2 = rbnode_to_test(rbb); + demo1 = rbnode_to_test(node1); + demo2 = rbnode_to_test(node2); - if (node1->data== node2->data) - return 0; - - return node1->data < node2->data ? -1 : 1; + /* Ignoring conflicts */ + return demo1->data < demo2->data ? -1 : 1; } static long -rbtest_rb_find(const bfdev_rb_node_t *rb, void *key) +rbtest_rb_find(const bfdev_rb_node_t *node, void *key) { - struct rbtree_test_node *node; + struct demo_node *demo; - node = rbnode_to_test(rb); - if (node->data == (unsigned long)key) + demo = rbnode_to_test(node); + if (demo->data == (unsigned long)key) return 0; - return node->data < (unsigned long)key ? -1 : 1; + return demo->data < (unsigned long)key ? -1 : 1; } static int -rbtree_testing(struct rbtree_test_pdata *sdata) +rbtree_testing(struct demo_data *sdata) { - struct rbtree_test_node *node, *nnode, *tnode; - bfdev_rb_node_t *rbnode, *nrbnode, *trbnode; + struct demo_node *demo, *ndemo, *tdemo; + bfdev_rb_node_t *node, *nnode, *tnode; unsigned long count; BFDEV_RB_ROOT_CACHED(test_root); @@ -64,38 +62,38 @@ rbtree_testing(struct rbtree_test_pdata *sdata) bfdev_rb_cached_insert(&test_root, &sdata->nodes[count].node, rbtest_rb_cmp, NULL); for (count = 0; count < TEST_LOOP; ++count) { - rbnode = bfdev_rb_find(&test_root.root, (void *)sdata->nodes[count].data, rbtest_rb_find); - if (!(node = rbnode_to_test_safe(rbnode))) + node = bfdev_rb_find(&test_root.root, (void *)sdata->nodes[count].data, rbtest_rb_find); + if (!(demo = rbnode_to_test_safe(node))) return 1; - printf("rbtree 'rb_cached' test: %lu\n", node->data); + printf("rbtree 'rb_cached' test: %lu\n", demo->data); } for (count = 0; count < TEST_LOOP; ++count) { - rbnode = bfdev_rb_cached_find(&test_root, (void *)sdata->nodes[count].data, rbtest_rb_find); - if (!(node = rbnode_to_test_safe(rbnode))) + node = bfdev_rb_cached_find(&test_root, (void *)sdata->nodes[count].data, rbtest_rb_find); + if (!(demo = rbnode_to_test_safe(node))) return 1; - printf("rbtree 'rb_cached_find' test: %lu\n", node->data); + printf("rbtree 'rb_cached_find' test: %lu\n", demo->data); } count = 0; - bfdev_rb_for_each(rbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each' test: %lu\n", node->data); + bfdev_rb_for_each(node, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - trbnode = rbnode; - bfdev_rb_for_each_continue(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each_continue' test: %lu\n", node->data); + tnode = node; + bfdev_rb_for_each_continue(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each_continue' test: %lu\n", demo->data); count++; } - rbnode = trbnode; - bfdev_rb_for_each_from(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each_from' test: %lu\n", node->data); + node = tnode; + bfdev_rb_for_each_from(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each_from' test: %lu\n", demo->data); count++; } @@ -103,24 +101,24 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_for_each_reverse(rbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each_reverse' test: %lu\n", node->data); + bfdev_rb_for_each_reverse(node, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each_reverse' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - trbnode = rbnode; - bfdev_rb_for_each_reverse_continue(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each_reverse_continue' test: %lu\n", node->data); + tnode = node; + bfdev_rb_for_each_reverse_continue(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each_reverse_continue' test: %lu\n", demo->data); count++; } - rbnode = trbnode; - bfdev_rb_for_each_reverse_from(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_for_each_reverse_from' test: %lu\n", node->data); + node = tnode; + bfdev_rb_for_each_reverse_from(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_for_each_reverse_from' test: %lu\n", demo->data); count++; } @@ -128,24 +126,24 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_post_for_each(rbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each' test: %lu\n", node->data); + bfdev_rb_post_for_each(node, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - trbnode = rbnode; - bfdev_rb_post_for_each_continue(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each_continue' test: %lu\n", node->data); + tnode = node; + bfdev_rb_post_for_each_continue(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each_continue' test: %lu\n", demo->data); count++; } - rbnode = trbnode; - bfdev_rb_post_for_each_from(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each_from' test: %lu\n", node->data); + node = tnode; + bfdev_rb_post_for_each_from(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each_from' test: %lu\n", demo->data); count++; } @@ -153,24 +151,24 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_post_for_each_safe(rbnode, nrbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each_safe' test: %lu\n", node->data); + bfdev_rb_post_for_each_safe(node, nnode, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each_safe' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - trbnode = rbnode; - bfdev_rb_post_for_each_safe_continue(rbnode, nrbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each_safe_continue' test: %lu\n", node->data); + tnode = node; + bfdev_rb_post_for_each_safe_continue(node, nnode) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each_safe_continue' test: %lu\n", demo->data); count++; } - rbnode = trbnode; - bfdev_rb_post_for_each_safe_from(rbnode, nrbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each_safe_from' test: %lu\n", node->data); + node = tnode; + bfdev_rb_post_for_each_safe_from(node, nnode) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each_safe_from' test: %lu\n", demo->data); count++; } @@ -178,21 +176,21 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_for_each_entry(node, &test_root.root, node) { - printf("rbtree 'rb_for_each_entry' test: %lu\n", node->data); + bfdev_rb_for_each_entry(demo, &test_root.root, node) { + printf("rbtree 'rb_for_each_entry' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_for_each_entry_continue(node, node) { - printf("rbtree 'rb_for_each_entry_continue' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_for_each_entry_continue(demo, node) { + printf("rbtree 'rb_for_each_entry_continue' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_for_each_entry_from(node, node) { - printf("rbtree 'rb_for_each_entry_from' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_for_each_entry_from(demo, node) { + printf("rbtree 'rb_for_each_entry_from' test: %lu\n", demo->data); count++; } @@ -200,21 +198,21 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_for_each_entry_reverse(node, &test_root.root, node) { - printf("rbtree 'rb_for_each_entry_reverse' test: %lu\n", node->data); + bfdev_rb_for_each_entry_reverse(demo, &test_root.root, node) { + printf("rbtree 'rb_for_each_entry_reverse' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_for_each_entry_reverse_continue(node, node) { - printf("rbtree 'rb_for_each_entry_reverse_continue' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_for_each_entry_reverse_continue(demo, node) { + printf("rbtree 'rb_for_each_entry_reverse_continue' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_for_each_entry_reverse_from(node, node) { - printf("rbtree 'rb_for_each_entry_reverse_from' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_for_each_entry_reverse_from(demo, node) { + printf("rbtree 'rb_for_each_entry_reverse_from' test: %lu\n", demo->data); count++; } @@ -222,21 +220,21 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_post_for_each_entry(node, &test_root.root, node) { - printf("rbtree 'rb_post_for_each_entry' test: %lu\n", node->data); + bfdev_rb_post_for_each_entry(demo, &test_root.root, node) { + printf("rbtree 'rb_post_for_each_entry' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_post_for_each_entry_continue(node, node) { - printf("rbtree 'rb_post_for_each_entry_continue' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_post_for_each_entry_continue(demo, node) { + printf("rbtree 'rb_post_for_each_entry_continue' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_post_for_each_entry_from(node, node) { - printf("rbtree 'rb_post_for_each_entry_from' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_post_for_each_entry_from(demo, node) { + printf("rbtree 'rb_post_for_each_entry_from' test: %lu\n", demo->data); count++; } @@ -244,24 +242,24 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_pre_for_each(rbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_pre_for_each' test: %lu\n", node->data); + bfdev_rb_pre_for_each(node, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_pre_for_each' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - trbnode = rbnode; - bfdev_rb_pre_for_each_continue(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_pre_for_each_continue' test: %lu\n", node->data); + tnode = node; + bfdev_rb_pre_for_each_continue(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_pre_for_each_continue' test: %lu\n", demo->data); count++; } - rbnode = trbnode; - bfdev_rb_pre_for_each_from(rbnode) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_pre_for_each_from' test: %lu\n", node->data); + node = tnode; + bfdev_rb_pre_for_each_from(node) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_pre_for_each_from' test: %lu\n", demo->data); count++; } @@ -269,21 +267,21 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_pre_for_each_entry(node, &test_root.root, node) { - printf("rbtree 'rb_pre_for_each_entry' test: %lu\n", node->data); + bfdev_rb_pre_for_each_entry(demo, &test_root.root, node) { + printf("rbtree 'rb_pre_for_each_entry' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_pre_for_each_entry_continue(node, node) { - printf("rbtree 'rb_pre_for_each_entry_continue' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_pre_for_each_entry_continue(demo, node) { + printf("rbtree 'rb_pre_for_each_entry_continue' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_pre_for_each_entry_from(node, node) { - printf("rbtree 'rb_pre_for_each_entry_from' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_pre_for_each_entry_from(demo, node) { + printf("rbtree 'rb_pre_for_each_entry_from' test: %lu\n", demo->data); count++; } @@ -291,22 +289,22 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_post_for_each(rbnode, &test_root.root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_post_for_each' test: %lu\n", node->data); + bfdev_rb_post_for_each(node, &test_root.root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_post_for_each' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_post_for_each_entry_continue(node, node) { - printf("rbtree 'rb_post_for_each_entry_continue' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_post_for_each_entry_continue(demo, node) { + printf("rbtree 'rb_post_for_each_entry_continue' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_post_for_each_entry_from(node, node) { - printf("rbtree 'rb_post_for_each_entry_from' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_post_for_each_entry_from(demo, node) { + printf("rbtree 'rb_post_for_each_entry_from' test: %lu\n", demo->data); count++; } @@ -314,21 +312,21 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_post_for_each_entry_safe(node, nnode, &test_root.root, node) { - printf("rbtree 'rb_post_for_each_entry_safe' test: %lu\n", node->data); + bfdev_rb_post_for_each_entry_safe(demo, ndemo, &test_root.root, node) { + printf("rbtree 'rb_post_for_each_entry_safe' test: %lu\n", demo->data); if (count++ == TEST_LOOP / 2) break; } - tnode = node; - bfdev_rb_post_for_each_entry_continue_safe(node, nnode, node) { - printf("rbtree 'rb_post_for_each_entry_continue_safe' test: %lu\n", node->data); + tdemo = demo; + bfdev_rb_post_for_each_entry_continue_safe(demo, ndemo, node) { + printf("rbtree 'rb_post_for_each_entry_continue_safe' test: %lu\n", demo->data); count++; } - node = tnode; - bfdev_rb_post_for_each_entry_from_safe(node, nnode, node) { - printf("rbtree 'rb_post_for_each_entry_from_safe' test: %lu\n", node->data); + demo = tdemo; + bfdev_rb_post_for_each_entry_from_safe(demo, ndemo, node) { + printf("rbtree 'rb_post_for_each_entry_from_safe' test: %lu\n", demo->data); count++; } @@ -336,9 +334,9 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_for_each(rbnode, &test_root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_cached_for_each' test: %lu\n", node->data); + bfdev_rb_cached_for_each(node, &test_root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_cached_for_each' test: %lu\n", demo->data); count++; } @@ -346,9 +344,9 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_for_each_reverse(rbnode, &test_root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_cached_for_each_reverse' test: %lu\n", node->data); + bfdev_rb_cached_for_each_reverse(node, &test_root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_cached_for_each_reverse' test: %lu\n", demo->data); count++; } @@ -356,8 +354,8 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_for_each_entry(node, &test_root, node) { - printf("rbtree 'rb_cached_for_each_entry' test: %lu\n", node->data); + bfdev_rb_cached_for_each_entry(demo, &test_root, node) { + printf("rbtree 'rb_cached_for_each_entry' test: %lu\n", demo->data); count++; } @@ -365,8 +363,8 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_for_each_entry_reverse(node, &test_root, node) { - printf("rbtree 'rb_cached_for_each_entry_reverse' test: %lu\n", node->data); + bfdev_rb_cached_for_each_entry_reverse(demo, &test_root, node) { + printf("rbtree 'rb_cached_for_each_entry_reverse' test: %lu\n", demo->data); count++; } @@ -374,9 +372,9 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_pre_for_each(rbnode, &test_root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_cached_pre_for_each' test: %lu\n", node->data); + bfdev_rb_cached_pre_for_each(node, &test_root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_cached_pre_for_each' test: %lu\n", demo->data); count++; } @@ -384,8 +382,8 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_pre_for_each_entry(node, &test_root, node) { - printf("rbtree 'rb_cached_pre_for_each_entry' test: %lu\n", node->data); + bfdev_rb_cached_pre_for_each_entry(demo, &test_root, node) { + printf("rbtree 'rb_cached_pre_for_each_entry' test: %lu\n", demo->data); count++; } @@ -393,9 +391,9 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_post_for_each(rbnode, &test_root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_cached_post_for_each' test: %lu\n", node->data); + bfdev_rb_cached_post_for_each(node, &test_root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_cached_post_for_each' test: %lu\n", demo->data); count++; } @@ -403,9 +401,9 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_post_for_each_safe(rbnode, nrbnode, &test_root) { - node = rbnode_to_test(rbnode); - printf("rbtree 'rb_cached_post_for_each_safe' test: %lu\n", node->data); + bfdev_rb_cached_post_for_each_safe(node, nnode, &test_root) { + demo = rbnode_to_test(node); + printf("rbtree 'rb_cached_post_for_each_safe' test: %lu\n", demo->data); count++; } @@ -413,8 +411,8 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_post_for_each_entry(node, &test_root, node) { - printf("rbtree 'rb_cached_post_for_each_entry' test: %lu\n", node->data); + bfdev_rb_cached_post_for_each_entry(demo, &test_root, node) { + printf("rbtree 'rb_cached_post_for_each_entry' test: %lu\n", demo->data); count++; } @@ -422,8 +420,8 @@ rbtree_testing(struct rbtree_test_pdata *sdata) return 1; count = 0; - bfdev_rb_cached_post_for_each_entry_safe(node, nnode, &test_root, node) { - printf("rbtree 'rb_cached_post_for_each_entry_safe' test: %lu\n", node->data); + bfdev_rb_cached_post_for_each_entry_safe(demo, ndemo, &test_root, node) { + printf("rbtree 'rb_cached_post_for_each_entry_safe' test: %lu\n", demo->data); count++; } @@ -435,11 +433,11 @@ rbtree_testing(struct rbtree_test_pdata *sdata) int main(int argc, const char *argv[]) { - struct rbtree_test_pdata *rdata; + struct demo_data *rdata; unsigned int count; int retval; - rdata = malloc(sizeof(struct rbtree_test_pdata)); + rdata = malloc(sizeof(struct demo_data)); if (!rdata) return -1; diff --git a/examples/rbtree/simple.c b/examples/rbtree/simple.c index a13d2ddd..04a837a7 100644 --- a/examples/rbtree/simple.c +++ b/examples/rbtree/simple.c @@ -21,11 +21,15 @@ struct simple_node { bfdev_rb_entry_safe(ptr, struct simple_node, node) static long -demo_cmp(const bfdev_rb_node_t *a, const bfdev_rb_node_t *b, void *pdata) +demo_cmp(const bfdev_rb_node_t *node1, const bfdev_rb_node_t *node2, void *pdata) { - struct simple_node *demo_a = rb_to_simple(a); - struct simple_node *demo_b = rb_to_simple(b); - return demo_a->data - demo_b->data; + struct simple_node *demo1, *demo2; + + demo1 = rb_to_simple(node1); + demo2 = rb_to_simple(node2); + + /* Ignoring conflicts */ + return demo1->data < demo2->data ? -1 : 1; } int main(int argc, const char *argv[]) diff --git a/include/bfdev/rbtree.h b/include/bfdev/rbtree.h index bed0aac9..c79d6de1 100644 --- a/include/bfdev/rbtree.h +++ b/include/bfdev/rbtree.h @@ -69,12 +69,6 @@ struct bfdev_rb_callbacks { #define BFDEV_RB_EMPTY_ROOT_CACHED(cached) \ ((cached)->root.node == NULL) -#define BFDEV_RB_EMPTY_NODE(node) \ - ((node)->parent == (node)) - -#define BFDEV_RB_CLEAR_NODE(node) \ - ((node)->parent = (node)) - /** * bfdev_rb_entry - get the struct for this entry. * @ptr: the &bfdev_rb_node_t pointer. @@ -209,24 +203,12 @@ bfdev_rb_find_last(bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp, * @parentp: pointer used to modify the parent node pointer. * @node: new node to insert. * @cmp: operator defining the node order. - * @leftmost: return whether it is the leftmost node. + * @leftmostp: return whether it is the leftmost node. */ extern bfdev_rb_node_t ** bfdev_rb_parent(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, - bool *leftmost); - -/** - * bfdev_rb_parent_conflict() - find the parent node or conflict. - * @root: rbtree want to search. - * @parentp: pointer used to modify the parent node pointer. - * @node: new node to insert. - * @cmp: operator defining the node order. - * @leftmost: return whether it is the leftmost node. - */ -extern bfdev_rb_node_t ** -bfdev_rb_parent_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, bfdev_rb_node_t *node, - bfdev_rb_cmp_t cmp, void *pdata, bool *leftmost); + bool *leftmostp); #define bfdev_rb_cached_erase_augmented(cached, parent, callbacks) \ bfdev_rb_erase_augmented(&(cached)->root, parent, callbacks) @@ -246,11 +228,8 @@ bfdev_rb_parent_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, bfdev #define bfdev_rb_cached_find_last(cached, key, cmp, parentp, linkp) \ bfdev_rb_find_last(&(cached)->root, key, cmp, parentp, linkp) -#define bfdev_rb_cached_parent(cached, parentp, node, cmp, pdata, leftmost) \ - bfdev_rb_parent(&(cached)->root, parentp, node, cmp, pdata, leftmost) - -#define bfdev_rb_cached_parent_conflict(cached, parentp, node, cmp, pdata, leftmost) \ - bfdev_rb_parent_conflict(&(cached)->root, parentp, node, cmp, pdata, leftmost) +#define bfdev_rb_cached_parent(cached, parentp, node, cmp, pdata, leftmostp) \ + bfdev_rb_parent(&(cached)->root, parentp, node, cmp, pdata, leftmostp) /* Base iteration - basic iteration helper */ extern bfdev_rb_node_t * @@ -668,26 +647,6 @@ bfdev_rb_insert(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_insert_node(root, parent, link, node); } -/** - * bfdev_rb_insert_conflict - find the parent node and insert new node or conflict. - * @root: rbtree root of node. - * @node: new node to insert. - * @cmp: operator defining the node order. - */ -static inline bool -bfdev_rb_insert_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t *node, - bfdev_rb_cmp_t cmp, void *pdata) -{ - bfdev_rb_node_t *parent, **link; - - link = bfdev_rb_parent_conflict(root, &parent, node, cmp, pdata, NULL); - if (!link) - return true; - - bfdev_rb_insert_node(root, parent, link, node); - return false; -} - /** * bfdev_rb_delete - delete node and fixup rbtree. * @root: rbtree root of node. @@ -746,28 +705,6 @@ bfdev_rb_insert_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_insert_node_augmented(root, parent, link, node, callbacks); } -/** - * bfdev_rb_insert_conflict_augmented - augmented find the parent node and insert new node or conflict. - * @root: rbtree root of node. - * @node: new node to insert. - * @cmp: operator defining the node order. - * @callbacks: augmented callback function. - */ -static inline bool -bfdev_rb_insert_conflict_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, - bfdev_rb_cmp_t cmp, void *pdata, - const struct bfdev_rb_callbacks *callbacks) -{ - bfdev_rb_node_t *parent, **link; - - link = bfdev_rb_parent_conflict(root, &parent, node, cmp, pdata, NULL); - if (!link) - return true; - - bfdev_rb_insert_node_augmented(root, parent, link, node, callbacks); - return false; -} - /** * bfdev_rb_delete_augmented - augmented delete node and fixup rbtree. * @root: rbtree root of node. @@ -940,33 +877,12 @@ bfdev_rb_cached_insert(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata) { bfdev_rb_node_t *parent, **link; - bool leftmost = true; + bool leftmost; link = bfdev_rb_cached_parent(cached, &parent, node, cmp, pdata, &leftmost); bfdev_rb_cached_insert_node(cached, parent, link, node, leftmost); } -/** - * bfdev_rb_cached_insert_conflict - find the parent node and insert new cached node or conflict. - * @cached: rbtree cached root of node. - * @node: new node to insert. - * @cmp: operator defining the node order. - */ -static inline bool -bfdev_rb_cached_insert_conflict(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, - bfdev_rb_cmp_t cmp, void *pdata) -{ - bfdev_rb_node_t *parent, **link; - bool leftmost = true; - - link = bfdev_rb_cached_parent_conflict(cached, &parent, node, cmp, pdata, &leftmost); - if (!link) - return true; - - bfdev_rb_cached_insert_node(cached, parent, link, node, leftmost); - return false; -} - /** * bfdev_rb_cached_delete - delete cached node and fixup rbtree. * @cached: rbtree cached root of node. @@ -975,10 +891,14 @@ bfdev_rb_cached_insert_conflict(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t static inline bfdev_rb_node_t * bfdev_rb_cached_delete(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node) { - bfdev_rb_node_t *leftmost = NULL; + bfdev_rb_node_t *leftmost; - if (cached->leftmost == node) - leftmost = cached->leftmost = bfdev_rb_next(node); + if (cached->leftmost != node) + leftmost = NULL; + else { + leftmost = bfdev_rb_next(node); + cached->leftmost = leftmost; + } bfdev_rb_delete(&cached->root, node); return leftmost; @@ -1032,35 +952,12 @@ bfdev_rb_cached_insert_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t const struct bfdev_rb_callbacks *callbacks) { bfdev_rb_node_t *parent, **link; - bool leftmost = true; + bool leftmost; link = bfdev_rb_cached_parent(cached, &parent, node, cmp, pdata, &leftmost); bfdev_rb_cached_insert_node_augmented(cached, parent, link, node, leftmost, callbacks); } -/** - * bfdev_rb_cached_insert_conflict - find the parent node and insert new cached node or conflict. - * @cached: rbtree cached root of node. - * @node: new node to insert. - * @cmp: operator defining the node order. - * @callbacks: augmented callback function. - */ -static inline bool -bfdev_rb_cached_insert_conflict_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, - bfdev_rb_cmp_t cmp, void *pdata, - const struct bfdev_rb_callbacks *callbacks) -{ - bfdev_rb_node_t *parent, **link; - bool leftmost = true; - - link = bfdev_rb_cached_parent_conflict(cached, &parent, node, cmp, pdata, &leftmost); - if (!link) - return true; - - bfdev_rb_cached_insert_node_augmented(cached, parent, link, node, leftmost, callbacks); - return false; -} - /** * bfdev_rb_cached_delete - delete cached node and fixup rbtree. * @cached: rbtree cached root of node. @@ -1071,10 +968,14 @@ static inline bfdev_rb_node_t * bfdev_rb_cached_delete_augmented(bfdev_rb_root_cached_t *cached, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *leftmost = NULL; + bfdev_rb_node_t *leftmost; - if (cached->leftmost == node) - leftmost = cached->leftmost = bfdev_rb_next(node); + if (cached->leftmost != node) + leftmost = NULL; + else { + leftmost = bfdev_rb_next(node); + cached->leftmost = leftmost; + } bfdev_rb_delete_augmented(&cached->root, node, callbacks); return leftmost; diff --git a/src/rbtree.c b/src/rbtree.c index 78313410..906d5b95 100644 --- a/src/rbtree.c +++ b/src/rbtree.c @@ -42,8 +42,9 @@ rotate_set(bfdev_rb_root_t *root, bfdev_rb_node_t *node, bfdev_rb_node_t *newn, bfdev_rb_node_t *child, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *parent = node->parent; + bfdev_rb_node_t *parent; + parent = node->parent; newn->parent = node->parent; node->parent = newn; @@ -75,13 +76,16 @@ left_rotate(bfdev_rb_root_t *root, bfdev_rb_node_t *node, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *child, *successor = node->right; + bfdev_rb_node_t *child, *successor; /* change left child */ - child = node->right = successor->left; - successor->left = node; + successor = node->right; + child = successor->left; + node->right = child; + successor->left = node; rotate_set(root, node, successor, child, color, ccolor, callbacks); + return child; } @@ -98,13 +102,16 @@ right_rotate(bfdev_rb_root_t *root, bfdev_rb_node_t *node, unsigned int color, unsigned int ccolor, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *child, *successor = node->left; + bfdev_rb_node_t *child, *successor; /* change right child */ - child = node->left = successor->right; - successor->right = node; + successor = node->left; + child = successor->right; + node->left = child; + successor->right = node; rotate_set(root, node, successor, child, color, ccolor, callbacks); + return child; } @@ -194,7 +201,6 @@ bfdev_rb_fixup_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, */ right_rotate(root, gparent, BFDEV_RB_RED, BFDEV_RB_BLACK, callbacks); - break; } else { /* parent == gparent->right */ tmp = gparent->left; @@ -213,8 +219,9 @@ bfdev_rb_fixup_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, /* Case 3 - left rotate at gparent */ left_rotate(root, gparent, BFDEV_RB_RED, BFDEV_RB_BLACK, callbacks); - break; } + + break; } } @@ -222,7 +229,8 @@ export void bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *tmp1, *tmp2, *sibling, *node = NULL; + bfdev_rb_node_t *sibling, *node = NULL; + bfdev_rb_node_t *tmp1, *tmp2; while (root && parent) { /* @@ -298,7 +306,7 @@ bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, * breaks property 4). This is fixed in * Case 4 (in __rb_rotate_set_parents() * which set sl the color of p - * and set p BFDEV_RB_BLACK) + * and set p black) * * (p) (sl) * / \ / \ @@ -328,7 +336,6 @@ bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, left_rotate(root, parent, BFDEV_RB_BLACK, BFDEV_RB_NSET, callbacks); tmp2->color = BFDEV_RB_BLACK; - break; } else { sibling = parent->left; @@ -362,8 +369,9 @@ bfdev_rb_erase_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *parent, /* Case 4 - right rotate at parent + color flips */ right_rotate(root, parent, BFDEV_RB_BLACK, BFDEV_RB_NSET, callbacks); tmp1->color = BFDEV_RB_BLACK; - break; } + + break; } } @@ -371,9 +379,12 @@ export bfdev_rb_node_t * bfdev_rb_remove_augmented(bfdev_rb_root_t *root, bfdev_rb_node_t *node, const struct bfdev_rb_callbacks *callbacks) { - bfdev_rb_node_t *parent = node->parent, *rebalance = NULL; - bfdev_rb_node_t *child1 = node->left; - bfdev_rb_node_t *child2 = node->right; + bfdev_rb_node_t *parent, *rebalance = NULL; + bfdev_rb_node_t *child1, *child2; + + parent = node->parent; + child1 = node->left; + child2 = node->right; if (!child1 && !child2) { /* @@ -516,8 +527,9 @@ export void bfdev_rb_replace(bfdev_rb_root_t *root, bfdev_rb_node_t *oldn, bfdev_rb_node_t *newn) { - bfdev_rb_node_t *parent = oldn->parent; + bfdev_rb_node_t *parent; + parent = oldn->parent; *newn = *oldn; if (oldn->left) @@ -532,14 +544,16 @@ export bfdev_rb_node_t * bfdev_rb_find(const bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp) { - bfdev_rb_node_t *node = root->node; + bfdev_rb_node_t *node; long retval; + node = root->node; while (node) { retval = cmp(node, key); if (retval == LONG_MIN) return NULL; - else if (retval > 0) + + if (retval > 0) node = node->left; else if (retval < 0) node = node->right; @@ -566,7 +580,8 @@ bfdev_rb_find_last(bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp, retval = cmp((*parentp = **linkp), key); if (retval == LONG_MIN) return NULL; - else if (retval > 0) + + if (retval > 0) *linkp = &(**linkp)->left; else if (retval < 0) *linkp = &(**linkp)->right; @@ -580,62 +595,36 @@ bfdev_rb_find_last(bfdev_rb_root_t *root, void *key, bfdev_rb_find_t cmp, export bfdev_rb_node_t ** bfdev_rb_parent(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, - bool *leftmost) + bool *leftmostp) { bfdev_rb_node_t **link; - bool leftmost_none; + bool leftmost; long retval; - if (!leftmost) - leftmost = &leftmost_none; - link = &root->node; + leftmost = true; + if (bfdev_unlikely(!*link)) { *parentp = NULL; - return link; + goto finish; } do { retval = cmp(node, (*parentp = *link), pdata); + if (bfdev_unlikely(!retval)) + return NULL; + if (retval < 0) link = &(*link)->left; else { link = &(*link)->right; - *leftmost = false; + leftmost = false; } } while (*link); - return link; -} - -export bfdev_rb_node_t ** -bfdev_rb_parent_conflict(bfdev_rb_root_t *root, bfdev_rb_node_t **parentp, - bfdev_rb_node_t *node, bfdev_rb_cmp_t cmp, void *pdata, - bool *leftmost) -{ - bfdev_rb_node_t **link; - bool leftmost_none; - long retval; - - if (!leftmost) - leftmost = &leftmost_none; - - link = &root->node; - if (bfdev_unlikely(!*link)) { - *parentp = NULL; - return link; - } - - do { - retval = cmp(node, (*parentp = *link), pdata); - if (retval < 0) - link = &(*link)->left; - else if (retval > 0) { - link = &(*link)->right; - *leftmost = false; - } else - return NULL; - } while (*link); +finish: + if (leftmostp) + *leftmostp = leftmost; return link; } From 33c112919c02af046dc298bf189000c6b019d911 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 20 Jan 2024 12:19:38 +0800 Subject: [PATCH 061/119] refactor sort: optimize quick-sort logic Signed-off-by: John Sanpe --- src/sort.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/sort.c b/src/sort.c index 10dfc289..46fd97b9 100644 --- a/src/sort.c +++ b/src/sort.c @@ -30,33 +30,38 @@ parent(size_t cells, unsigned int lsbit, size_t index) export int bfdev_qsort(void *base, size_t num, size_t cells, bfdev_cmp_t cmp, void *pdata) { - const unsigned int lsbit = cells & -cells; - size_t size = num * cells; - size_t ida, idb, idc, idd; + size_t size, idx1, idx2, idx3, idx4; + unsigned int lsbit; - ida = (num / 2) * cells; - if (!base || !cmp || !ida) + idx1 = (num >> 1) * cells; + if (bfdev_unlikely(!base || !cmp || !idx1)) return -BFDEV_EINVAL; + size = num * cells; + lsbit = cells & -cells; + for (;;) { - if (ida) - ida -= cells; + if (idx1) + idx1 -= cells; else if (size -= cells) sort_swap(cells, base, base + size); else break; - for (idb = ida; idc = 2 * idb + cells, (idd = idc + cells) < size;) - idb = cmp(base + idc, base + idd, pdata) >= 0 ? idc : idd; - if (idd == size) - idb = idc; + idx2 = idx1; + while (idx3 = 2 * idx2 + cells, (idx4 = idx3 + cells) < size) + idx2 = cmp(base + idx3, base + idx4, pdata) >= 0 ? idx3 : idx4; + + if (idx4 == size) + idx2 = idx3; - while (ida != idb && cmp(base + ida, base + idb, pdata) >= 0) - idb = parent(cells, lsbit, idb); + while (idx1 != idx2 && cmp(base + idx1, base + idx2, pdata) >= 0) + idx2 = parent(cells, lsbit, idx2); - for (idc = idb; ida != idb;) { - idb = parent(cells, lsbit, idb); - sort_swap(cells, base + idb, base + idc); + idx3 = idx2; + while (idx1 != idx2) { + idx2 = parent(cells, lsbit, idx2); + sort_swap(cells, base + idx2, base + idx3); } } From a5c0b2832ae37df19a203e950e5d443e124326a4 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 20 Jan 2024 12:20:00 +0800 Subject: [PATCH 062/119] feat sort: added selftest example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/sort/CMakeLists.txt | 22 ++++++++++++ examples/sort/selftest.c | 70 ++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 examples/sort/CMakeLists.txt create mode 100644 examples/sort/selftest.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index caff636d..647882d5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -35,4 +35,5 @@ add_subdirectory(ringbuf) add_subdirectory(segtree) add_subdirectory(skiplist) add_subdirectory(slist) +add_subdirectory(sort) add_subdirectory(textsearch) diff --git a/examples/sort/CMakeLists.txt b/examples/sort/CMakeLists.txt new file mode 100644 index 00000000..4b9b88d1 --- /dev/null +++ b/examples/sort/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(sort-selftest selftest.c) +target_link_libraries(sort-selftest bfdev) +add_test(sort-selftest sort-selftest) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + selftest.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/sort + ) + + install(TARGETS + sort-selftest + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/sort/selftest.c b/examples/sort/selftest.c new file mode 100644 index 00000000..7e433506 --- /dev/null +++ b/examples/sort/selftest.c @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2022 John Sanpe + */ + +#define MODULE_NAME "sort-benchmark" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include + +#define TEST_LOOP 100 + +struct sort_test { + unsigned int index; + unsigned int value; +}; + +static long +sort_test_cmp(const void *nodea, const void *nodeb, void *pdata) +{ + const struct sort_test *testa = nodea; + const struct sort_test *testb = nodeb; + return testa->value > testb->value ? 1 : -1; +} + +static int +sort_testing(struct sort_test *test) +{ + unsigned int count; + + srand(time(NULL)); + for (count = 0; count < TEST_LOOP; ++count) { + test[count].index = count; + test[count].value = (unsigned int)rand(); + bfdev_log_info("qsort generating: %02d = %u\n", + count, test[count].value); + } + + bfdev_qsort(test, TEST_LOOP, sizeof(*test), sort_test_cmp, NULL); + for (count = 0; count < TEST_LOOP; ++count) { + bfdev_log_info("sort qsort%02u: %02u = %u\n", + count, test[count].index, test[count].value); + + if (count && test[count].value < test[count - 1].value) { + bfdev_log_err("sort failed\n"); + return 1; + } + } + + return 0; +} + +int main(int argc, const char *argv[]) +{ + struct sort_test *test; + int retval; + + test = malloc(sizeof(struct sort_test) * TEST_LOOP); + if (!test) + return 1; + + retval = sort_testing(test); + free(test); + + return retval; +} From d03407507919707a997908116b35c5cde5307076 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 20 Jan 2024 17:11:13 +0800 Subject: [PATCH 063/119] docs readme: added some note and caution Signed-off-by: John Sanpe --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9ed18620..c33e9830 100755 --- a/README.md +++ b/README.md @@ -22,9 +22,8 @@ bfdev is a high-performance, aesthetically pleasing, and portable infrastructure - Offers a rich and well-organized set of APIs for users. - Conforms to the GNUC standard, ensuring strong cross-platform compatibility. -### WARNING - -This project is not yet fully completed, so it is not recommended for use in a production environment. +> [!NOTE] +> We are seeking developers to collaborate with us on improve this project. View supported components: [Docs](docs/components.md) @@ -84,6 +83,9 @@ graph LR Quickly start, API manual, see [Bfdev Documentation Tutorial](https://openbfdev.github.io/bfdev-docs) +> [!CAUTION] +> This project is not yet fully completed, so it is not recommended for use in a production environment. + ## License This is free software: you can redistribute it and/or modify it under the terms of the latest GNU General Public License as published by the Free Software Foundation. From 3c3b5699829f81f1a650a05f11548917f4dce63a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 21 Jan 2024 22:04:59 +0800 Subject: [PATCH 064/119] refactor sort: optimize quick-sort logic Signed-off-by: John Sanpe --- src/sort.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sort.c b/src/sort.c index 46fd97b9..7da7c5f7 100644 --- a/src/sort.c +++ b/src/sort.c @@ -8,15 +8,15 @@ #include static __bfdev_always_inline void -sort_swap(size_t cells, void *cela, void *celb) +sort_swap(size_t cells, void *cel1, void *cel2) { - uint8_t *buff; + void *buff; buff = bfdev_alloca(cells); - memcpy(buff, cela, cells); - memcpy(cela, celb, cells); - memcpy(celb, buff, cells); + memcpy(buff, cel1, cells); + memcpy(cel1, cel2, cells); + memcpy(cel2, buff, cells); } static __bfdev_attribute_const __bfdev_always_inline size_t From 9ca5c3e9dff44e79fe99abcfb97891258a28615b Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 21 Jan 2024 22:05:30 +0800 Subject: [PATCH 065/119] feat sort: added benchmark example Signed-off-by: John Sanpe --- examples/sort/CMakeLists.txt | 6 ++++ examples/sort/benchmark.c | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 examples/sort/benchmark.c diff --git a/examples/sort/CMakeLists.txt b/examples/sort/CMakeLists.txt index 4b9b88d1..74ec54ce 100644 --- a/examples/sort/CMakeLists.txt +++ b/examples/sort/CMakeLists.txt @@ -3,18 +3,24 @@ # Copyright(c) 2023 ffashion # +add_executable(sort-benchmark benchmark.c) +target_link_libraries(sort-benchmark bfdev) +add_test(sort-benchmark sort-benchmark) + add_executable(sort-selftest selftest.c) target_link_libraries(sort-selftest bfdev) add_test(sort-selftest sort-selftest) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES + benchmark.c selftest.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/sort ) install(TARGETS + sort-benchmark sort-selftest DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/sort/benchmark.c b/examples/sort/benchmark.c new file mode 100644 index 00000000..0d598a10 --- /dev/null +++ b/examples/sort/benchmark.c @@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2022 John Sanpe + */ + +#define MODULE_NAME "sort-benchmark" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include "../time.h" + +#define TEST_LOOP 3 +#define TEST_SIZE 10000 + +#define GENERIC_TEST(name, size) \ +for (count = 0; count < TEST_LOOP; ++count) { \ + unsigned int index; \ + \ + for (index = 0; index < size; ++index) \ + buffer[index] = rand(); \ + \ + EXAMPLE_TIME_LOOP(&loop, 1000, \ + bfdev_qsort(buffer, size, sizeof(*buffer), \ + test_cmp, NULL); \ + 0; \ + ); \ + \ + bfdev_log_info("qsort " name " %u: %uops/s\n", \ + count, loop); \ +} + +static long +test_cmp(const void *node1, const void *node2, void *pdata) +{ + const int *test1, *test2; + + test1 = node1; + test2 = node2; + + return *test1 < *test2 ? -1 : 1; +} + +int main(int argc, const char *argv[]) +{ + unsigned int count, loop; + int *buffer; + + buffer = malloc(sizeof(int) * TEST_SIZE); + if (!buffer) + return 1; + + srand(time(NULL)); + GENERIC_TEST("1k", 1000) + GENERIC_TEST("5k", 5000) + GENERIC_TEST("10k", 10000) + free(buffer); + + return 0; +} From dc0a91ade797423710e9346247cc2a8d25d56cba Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 23 Jan 2024 20:05:43 +0800 Subject: [PATCH 066/119] fixup sort: prohibit alloca inline Signed-off-by: John Sanpe --- src/sort.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sort.c b/src/sort.c index 7da7c5f7..cb101818 100644 --- a/src/sort.c +++ b/src/sort.c @@ -7,11 +7,12 @@ #include #include -static __bfdev_always_inline void +static __bfdev_noinline void sort_swap(size_t cells, void *cel1, void *cel2) { void *buff; + /* alloca hates inline */ buff = bfdev_alloca(cells); memcpy(buff, cel1, cells); From 13d11adbd46e2ade439756b45bea58403f848948 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 24 Jan 2024 15:27:13 +0800 Subject: [PATCH 067/119] fixup notifier: fixed call return value Signed-off-by: John Sanpe --- include/bfdev/notifier.h | 5 ++--- src/notifier.c | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/bfdev/notifier.h b/include/bfdev/notifier.h index 4b1078a2..87bcc0d9 100644 --- a/include/bfdev/notifier.h +++ b/include/bfdev/notifier.h @@ -20,11 +20,10 @@ typedef bfdev_notifier_ret_t (*bfdev_notifier_entry_t) (bfdev_notifier_node_t *node, void *args); enum bfdev_notifier_ret { - __BFDEV_NOTIFI_RET_DONE = 0, - __BFDEV_NOTIFI_RET_REMOVE, + __BFDEV_NOTIFI_RET_REMOVE = 0, __BFDEV_NOTIFI_RET_STOP, - BFDEV_NOTIFI_RET_DONE = BFDEV_BIT(__BFDEV_NOTIFI_RET_DONE), + BFDEV_NOTIFI_RET_DONE = 0, BFDEV_NOTIFI_RET_REMOVE = BFDEV_BIT(__BFDEV_NOTIFI_RET_REMOVE), BFDEV_NOTIFI_RET_STOP = BFDEV_BIT(__BFDEV_NOTIFI_RET_STOP), }; diff --git a/src/notifier.c b/src/notifier.c index b5a98146..307de638 100755 --- a/src/notifier.c +++ b/src/notifier.c @@ -31,8 +31,9 @@ bfdev_notifier_call(bfdev_notifier_head_t *head, void *arg, unsigned int call_num, unsigned int *called_num) { bfdev_notifier_node_t *node, *tmp; - bfdev_notifier_ret_t retval = BFDEV_NOTIFI_RET_DONE; + bfdev_notifier_ret_t retval; + retval = BFDEV_NOTIFI_RET_DONE; bfdev_ilist_for_each_entry_safe(node, tmp, &head->node, list) { if (!call_num--) break; From 34b7461b5b56da9554de9095790d3ca9cb8288e1 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 28 Jan 2024 17:23:12 +0800 Subject: [PATCH 068/119] refactor gencrc: declare the use of bash to support windows Signed-off-by: John Sanpe --- scripts/CMakeLists.txt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 68d00be7..4dd42036 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -21,11 +21,12 @@ macro(generate_crctbl proc path table rows poly trans) add_custom_command( TARGET ${proc} POST_BUILD COMMAND - mkdir -p ${BFDEV_GENERATED_PATH}/bfdev/crypto - POST_BUILD COMMAND - ${PROJECT_BINARY_DIR}/scripts/${proc} - ${table} ${rows} ${poly} ${trans} > - ${BFDEV_GENERATED_PATH}/bfdev/crypto/${path} + bash -c \" + mkdir -p ${BFDEV_GENERATED_PATH}/bfdev/crypto && + ${PROJECT_BINARY_DIR}/scripts/${proc} + ${table} ${rows} ${poly} ${trans} > + ${BFDEV_GENERATED_PATH}/bfdev/crypto/${path} + \" ) endmacro() From 71e354aefc1f52f079220ce40267e14ee20ecaab Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 29 Jan 2024 02:39:00 +0800 Subject: [PATCH 069/119] docs authors: update authors file Signed-off-by: John Sanpe --- AUTHORS | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/AUTHORS b/AUTHORS index b1ddf016..babaa038 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,10 +6,10 @@ ----------- Bfdev Designer Group -Name : ffashion -Email : helloworldffashion@gmail.com -Represent : global - Name : John Sanpe Email : sanpeqf@gmail.com -Represent : global +Represent : framework + +Name : ffashion +Email : helloworldffashion@gmail.com +Represent : array, cmake From 40f623eae8ecbdcd2bbddfd2fd730c30d5be7aba Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 29 Jan 2024 16:20:22 +0800 Subject: [PATCH 070/119] fixup bfdev: modify files permissions to 644 Signed-off-by: John Sanpe --- README.md | 0 docs/images/logo.png | Bin examples/allocator/CMakeLists.txt | 0 examples/base32/CMakeLists.txt | 0 examples/base64/CMakeLists.txt | 0 examples/bfdev/CMakeLists.txt | 0 examples/bfdev/version.c | 0 examples/cache/CMakeLists.txt | 0 examples/crc/CMakeLists.txt | 0 examples/ilist/.gitignore | 0 examples/log/.gitignore | 0 examples/log/CMakeLists.txt | 0 examples/log2/.gitignore | 0 examples/log2/CMakeLists.txt | 0 examples/log2/selftest.c | 0 include/bfdev/fifo.h | 0 include/bfdev/log2.h | 0 include/bfdev/math.h | 0 include/bfdev/prandom.h | 0 include/bfdev/respool.h | 0 scripts/gen-crc16be.c | 0 scripts/gen-crc32be.c | 0 scripts/gen-crc64be.c | 0 src/crypto/base32.c | 0 src/crypto/base64.c | 0 src/notifier.c | 0 src/respool.c | 0 27 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 README.md mode change 100755 => 100644 docs/images/logo.png mode change 100755 => 100644 examples/allocator/CMakeLists.txt mode change 100755 => 100644 examples/base32/CMakeLists.txt mode change 100755 => 100644 examples/base64/CMakeLists.txt mode change 100755 => 100644 examples/bfdev/CMakeLists.txt mode change 100755 => 100644 examples/bfdev/version.c mode change 100755 => 100644 examples/cache/CMakeLists.txt mode change 100755 => 100644 examples/crc/CMakeLists.txt mode change 100755 => 100644 examples/ilist/.gitignore mode change 100755 => 100644 examples/log/.gitignore mode change 100755 => 100644 examples/log/CMakeLists.txt mode change 100755 => 100644 examples/log2/.gitignore mode change 100755 => 100644 examples/log2/CMakeLists.txt mode change 100755 => 100644 examples/log2/selftest.c mode change 100755 => 100644 include/bfdev/fifo.h mode change 100755 => 100644 include/bfdev/log2.h mode change 100755 => 100644 include/bfdev/math.h mode change 100755 => 100644 include/bfdev/prandom.h mode change 100755 => 100644 include/bfdev/respool.h mode change 100755 => 100644 scripts/gen-crc16be.c mode change 100755 => 100644 scripts/gen-crc32be.c mode change 100755 => 100644 scripts/gen-crc64be.c mode change 100755 => 100644 src/crypto/base32.c mode change 100755 => 100644 src/crypto/base64.c mode change 100755 => 100644 src/notifier.c mode change 100755 => 100644 src/respool.c diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/docs/images/logo.png b/docs/images/logo.png old mode 100755 new mode 100644 diff --git a/examples/allocator/CMakeLists.txt b/examples/allocator/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/base32/CMakeLists.txt b/examples/base32/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/base64/CMakeLists.txt b/examples/base64/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/bfdev/CMakeLists.txt b/examples/bfdev/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/bfdev/version.c b/examples/bfdev/version.c old mode 100755 new mode 100644 diff --git a/examples/cache/CMakeLists.txt b/examples/cache/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/crc/CMakeLists.txt b/examples/crc/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/ilist/.gitignore b/examples/ilist/.gitignore old mode 100755 new mode 100644 diff --git a/examples/log/.gitignore b/examples/log/.gitignore old mode 100755 new mode 100644 diff --git a/examples/log/CMakeLists.txt b/examples/log/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/log2/.gitignore b/examples/log2/.gitignore old mode 100755 new mode 100644 diff --git a/examples/log2/CMakeLists.txt b/examples/log2/CMakeLists.txt old mode 100755 new mode 100644 diff --git a/examples/log2/selftest.c b/examples/log2/selftest.c old mode 100755 new mode 100644 diff --git a/include/bfdev/fifo.h b/include/bfdev/fifo.h old mode 100755 new mode 100644 diff --git a/include/bfdev/log2.h b/include/bfdev/log2.h old mode 100755 new mode 100644 diff --git a/include/bfdev/math.h b/include/bfdev/math.h old mode 100755 new mode 100644 diff --git a/include/bfdev/prandom.h b/include/bfdev/prandom.h old mode 100755 new mode 100644 diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h old mode 100755 new mode 100644 diff --git a/scripts/gen-crc16be.c b/scripts/gen-crc16be.c old mode 100755 new mode 100644 diff --git a/scripts/gen-crc32be.c b/scripts/gen-crc32be.c old mode 100755 new mode 100644 diff --git a/scripts/gen-crc64be.c b/scripts/gen-crc64be.c old mode 100755 new mode 100644 diff --git a/src/crypto/base32.c b/src/crypto/base32.c old mode 100755 new mode 100644 diff --git a/src/crypto/base64.c b/src/crypto/base64.c old mode 100755 new mode 100644 diff --git a/src/notifier.c b/src/notifier.c old mode 100755 new mode 100644 diff --git a/src/respool.c b/src/respool.c old mode 100755 new mode 100644 From f41534d634a142143a3062f939e7e5fd6baadf98 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 30 Jan 2024 19:26:32 +0800 Subject: [PATCH 071/119] docs sort: added api comments Signed-off-by: John Sanpe --- examples/sort/benchmark.c | 4 ++-- examples/sort/selftest.c | 2 +- include/bfdev/sort.h | 14 +++++++++++++- src/sort.c | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/examples/sort/benchmark.c b/examples/sort/benchmark.c index 0d598a10..0dc8e7bb 100644 --- a/examples/sort/benchmark.c +++ b/examples/sort/benchmark.c @@ -23,12 +23,12 @@ for (count = 0; count < TEST_LOOP; ++count) { \ buffer[index] = rand(); \ \ EXAMPLE_TIME_LOOP(&loop, 1000, \ - bfdev_qsort(buffer, size, sizeof(*buffer), \ + bfdev_sort(buffer, size, sizeof(*buffer), \ test_cmp, NULL); \ 0; \ ); \ \ - bfdev_log_info("qsort " name " %u: %uops/s\n", \ + bfdev_log_info("sort " name " %u: %uops/s\n", \ count, loop); \ } diff --git a/examples/sort/selftest.c b/examples/sort/selftest.c index 7e433506..ff8592b0 100644 --- a/examples/sort/selftest.c +++ b/examples/sort/selftest.c @@ -40,7 +40,7 @@ sort_testing(struct sort_test *test) count, test[count].value); } - bfdev_qsort(test, TEST_LOOP, sizeof(*test), sort_test_cmp, NULL); + bfdev_sort(test, TEST_LOOP, sizeof(*test), sort_test_cmp, NULL); for (count = 0; count < TEST_LOOP; ++count) { bfdev_log_info("sort qsort%02u: %02u = %u\n", count, test[count].index, test[count].value); diff --git a/include/bfdev/sort.h b/include/bfdev/sort.h index 22baf912..997b76ad 100644 --- a/include/bfdev/sort.h +++ b/include/bfdev/sort.h @@ -11,8 +11,20 @@ BFDEV_BEGIN_DECLS +/** + * bfdev_sort() - Heap sort an array of elements. + * @base: pointer to data to sort. + * @num: number of elements. + * @cells: size of each element. + * @cmp: pointer to comparison function. + * @pdata: private data passed to comparison function. + * + * Sorting time is O(n log n) both on average and worst-case. While + * quicksort is slightly faster on average, it suffers from exploitable + * O(n^2) worst-case behavior and extra memory requirements. + */ extern int -bfdev_qsort(void *base, size_t num, size_t cells, bfdev_cmp_t cmp, void *pdata); +bfdev_sort(void *base, size_t num, size_t cells, bfdev_cmp_t cmp, void *pdata); BFDEV_END_DECLS diff --git a/src/sort.c b/src/sort.c index cb101818..9619a54c 100644 --- a/src/sort.c +++ b/src/sort.c @@ -29,7 +29,7 @@ parent(size_t cells, unsigned int lsbit, size_t index) } export int -bfdev_qsort(void *base, size_t num, size_t cells, bfdev_cmp_t cmp, void *pdata) +bfdev_sort(void *base, size_t num, size_t cells, bfdev_cmp_t cmp, void *pdata) { size_t size, idx1, idx2, idx3, idx4; unsigned int lsbit; From 8192519d162f1fbf306449b32bca3dd3ddabc6e4 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 1 Feb 2024 04:34:30 +0800 Subject: [PATCH 072/119] fixup const: fixup some api issue Signed-off-by: John Sanpe --- include/bfdev/const.h | 6 +++--- include/bfdev/poison.h | 2 +- include/bfdev/size.h | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/include/bfdev/const.h b/include/bfdev/const.h index 7cf53185..1b88cee6 100644 --- a/include/bfdev/const.h +++ b/include/bfdev/const.h @@ -10,7 +10,7 @@ BFDEV_BEGIN_DECLS -#ifdef __ASSEMBLY__ +#ifdef __BFDEV_ASSEMBLY__ # define BFDEV_AC(X, Y) X # define BFDEV_AT(T, X) X #else @@ -22,8 +22,8 @@ BFDEV_BEGIN_DECLS #define BFDEV_UL(x) (BFDEV_AC(x, UL)) #define BFDEV_ULL(x) (BFDEV_AC(x, ULL)) -#define BFDEV_BITUL(x) (_BFDEV_UL(1) << (x)) -#define BFDEV_BITULL(x) (_BFDEV_ULL(1) << (x)) +#define BFDEV_BITUL(x) (BFDEV_UL(1) << (x)) +#define BFDEV_BITULL(x) (BFDEV_ULL(1) << (x)) BFDEV_END_DECLS diff --git a/include/bfdev/poison.h b/include/bfdev/poison.h index 1108487f..1435b991 100644 --- a/include/bfdev/poison.h +++ b/include/bfdev/poison.h @@ -12,7 +12,7 @@ BFDEV_BEGIN_DECLS #ifdef BFDEV_POISON_ILLEGAL_ADDRESS -# define BFDEV_POISON_OFFSET _AC(BFDEV_POISON_ILLEGAL_ADDRESS, UL) +# define BFDEV_POISON_OFFSET BFDEV_AC(BFDEV_POISON_ILLEGAL_ADDRESS, UL) #else # define BFDEV_POISON_OFFSET 0 #endif diff --git a/include/bfdev/size.h b/include/bfdev/size.h index 5ce73a02..dec9e235 100644 --- a/include/bfdev/size.h +++ b/include/bfdev/size.h @@ -44,16 +44,16 @@ BFDEV_BEGIN_DECLS #define BFDEV_SZ_256MiB 0x10000000 #define BFDEV_SZ_512MiB 0x20000000 -#define BFDEV_SZ_1GiB _BFDEV_AC(0x0010000000, ULL) -#define BFDEV_SZ_2GiB _BFDEV_AC(0x0020000000, ULL) -#define BFDEV_SZ_4GiB _BFDEV_AC(0x0040000000, ULL) -#define BFDEV_SZ_8GiB _BFDEV_AC(0x0080000000, ULL) -#define BFDEV_SZ_16GiB _BFDEV_AC(0x0100000000, ULL) -#define BFDEV_SZ_32GiB _BFDEV_AC(0x0200000000, ULL) -#define BFDEV_SZ_64GiB _BFDEV_AC(0x0400000000, ULL) -#define BFDEV_SZ_128GiB _BFDEV_AC(0x0800000000, ULL) -#define BFDEV_SZ_256GiB _BFDEV_AC(0x1000000000, ULL) -#define BFDEV_SZ_512GiB _BFDEV_AC(0x2000000000, ULL) +#define BFDEV_SZ_1GiB BFDEV_AC(0x0010000000, ULL) +#define BFDEV_SZ_2GiB BFDEV_AC(0x0020000000, ULL) +#define BFDEV_SZ_4GiB BFDEV_AC(0x0040000000, ULL) +#define BFDEV_SZ_8GiB BFDEV_AC(0x0080000000, ULL) +#define BFDEV_SZ_16GiB BFDEV_AC(0x0100000000, ULL) +#define BFDEV_SZ_32GiB BFDEV_AC(0x0200000000, ULL) +#define BFDEV_SZ_64GiB BFDEV_AC(0x0400000000, ULL) +#define BFDEV_SZ_128GiB BFDEV_AC(0x0800000000, ULL) +#define BFDEV_SZ_256GiB BFDEV_AC(0x1000000000, ULL) +#define BFDEV_SZ_512GiB BFDEV_AC(0x2000000000, ULL) BFDEV_END_DECLS From 11f7376b9c9dcb5bf4164a3787c3ec65d3cf90bc Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 2 Feb 2024 20:46:31 +0800 Subject: [PATCH 073/119] fixup allocpool: fixed free api typo Signed-off-by: John Sanpe --- include/bfdev/allocpool.h | 4 ++-- src/allocpool.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/bfdev/allocpool.h b/include/bfdev/allocpool.h index 2bc37136..ae84f00b 100644 --- a/include/bfdev/allocpool.h +++ b/include/bfdev/allocpool.h @@ -57,7 +57,7 @@ bfdev_allocpool_reset(struct bfdev_allocpool *pool) * @size: size to allocation. * @align: align to allocation. */ -extern void * +extern __bfdev_malloc void * bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align); /** @@ -66,7 +66,7 @@ bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align); * @block: memory block to free. */ extern void -bfdev_allocpool_free(struct bfdev_allocpool *pool, const char *block); +bfdev_allocpool_free(struct bfdev_allocpool *pool, const void *block); BFDEV_END_DECLS diff --git a/src/allocpool.c b/src/allocpool.c index 8b3804ab..0a1cea44 100644 --- a/src/allocpool.c +++ b/src/allocpool.c @@ -7,7 +7,7 @@ #include #include -export void * +export __bfdev_malloc void * bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align) { uintptr_t offset; @@ -30,7 +30,7 @@ bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align) } export void -bfdev_allocpool_free(struct bfdev_allocpool *pool, const char *block) +bfdev_allocpool_free(struct bfdev_allocpool *pool, const void *block) { if (!--pool->count) pool->last = 0; From 603506ae0d983923d3197647ae415073b5cf294c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 2 Feb 2024 22:34:53 +0800 Subject: [PATCH 074/119] feat array: added peek api function Signed-off-by: John Sanpe --- include/bfdev/array.h | 3 +++ src/array.c | 39 ++++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index f768e872..297cfe83 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -81,6 +81,9 @@ bfdev_array_push(bfdev_array_t *array, unsigned long num); extern void * bfdev_array_pop(bfdev_array_t *array, unsigned long num); +extern void * +bfdev_array_peek(bfdev_array_t *array, unsigned long num); + extern int bfdev_array_reserve(bfdev_array_t *array, unsigned long num); diff --git a/src/array.c b/src/array.c index 0ee4637a..6f8abd53 100644 --- a/src/array.c +++ b/src/array.c @@ -9,7 +9,7 @@ #include #include -static int +static inline int array_resize(bfdev_array_t *array, unsigned long count) { const bfdev_alloc_t *alloc = array->alloc; @@ -30,7 +30,7 @@ array_resize(bfdev_array_t *array, unsigned long count) return -BFDEV_ENOERR; } -static int +static inline int array_apply(bfdev_array_t *array, unsigned long count) { if (count > array->capacity) @@ -38,6 +38,24 @@ array_apply(bfdev_array_t *array, unsigned long count) return -BFDEV_ENOERR; } +static inline void * +array_peek(bfdev_array_t *array, unsigned long num, unsigned long *indexp) +{ + unsigned long index; + uintptr_t offset; + bool overflow; + + overflow = bfdev_overflow_check_sub(array->index, num, &index); + if (bfdev_unlikely(overflow)) + return NULL; + + offset = bfdev_array_offset(array, index); + if (indexp) + *indexp = index; + + return array->data + offset; +} + export void * bfdev_array_push(bfdev_array_t *array, unsigned long num) { @@ -64,18 +82,13 @@ bfdev_array_push(bfdev_array_t *array, unsigned long num) export void * bfdev_array_pop(bfdev_array_t *array, unsigned long num) { - unsigned long index; - uintptr_t offset; - bool overflow; - - overflow = bfdev_overflow_check_sub(array->index, num, &index); - if (bfdev_unlikely(overflow)) - return NULL; - - array->index = index; - offset = bfdev_array_offset(array, index); + return array_peek(array, num, &array->index); +} - return array->data + offset; +export void * +bfdev_array_peek(bfdev_array_t *array, unsigned long num) +{ + return array_peek(array, num, NULL); } export int From 1bde48db8757424569e01b396a21ff6fe127e23c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 3 Feb 2024 19:43:29 +0800 Subject: [PATCH 075/119] feat hashmap: added strategy typedef Signed-off-by: John Sanpe --- include/bfdev/hashmap.h | 3 ++- src/hashmap.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/bfdev/hashmap.h b/include/bfdev/hashmap.h index ecf42d15..5f53c398 100644 --- a/include/bfdev/hashmap.h +++ b/include/bfdev/hashmap.h @@ -19,6 +19,7 @@ BFDEV_BEGIN_DECLS typedef struct bfdev_hashmap bfdev_hashmap_t; typedef struct bfdev_hashmap_ops bfdev_hashmap_ops_t; +typedef enum bfdev_hashmap_strategy bfdev_hashmap_strategy_t; /** * enum bfdev_hashmap_strategy - Hashmap insertion strategy. @@ -88,7 +89,7 @@ bfdev_hashmap_init(bfdev_hashmap_t *hashmap, const bfdev_alloc_t *alloc, */ extern int bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, - bfdev_hlist_node_t **old, enum bfdev_hashmap_strategy strategy); + bfdev_hlist_node_t **old, bfdev_hashmap_strategy_t strategy); /** * bfdev_hashmap_del() - delete a hashlist node from hashmap. diff --git a/src/hashmap.c b/src/hashmap.c index e2e9a967..1714717d 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -181,7 +181,7 @@ hashmap_shrink(bfdev_hashmap_t *hashmap) export int bfdev_hashmap_insert(bfdev_hashmap_t *hashmap, bfdev_hlist_node_t *node, - bfdev_hlist_node_t **old, enum bfdev_hashmap_strategy strategy) + bfdev_hlist_node_t **old, bfdev_hashmap_strategy_t strategy) { bfdev_hlist_node_t *exist; unsigned long value; From 9900d92ba7d7d44dcf84e72f474fcfbd211e2cee Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 7 Feb 2024 01:39:50 +0800 Subject: [PATCH 076/119] build cmake: simplify the build script Signed-off-by: John Sanpe --- CMakeLists.txt | 69 +++++++----------------------------------- build.cmake | 16 ++++++++++ scripts/CMakeLists.txt | 2 ++ scripts/hostrule.cmake | 6 ++-- src/build.cmake | 10 +++--- 5 files changed, 37 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 02bbc702..6effe2a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,6 @@ project(bfdev VERSION 1.0.0 LANGUAGES C) include(GNUInstallDirs) include(CheckIncludeFiles) -include(CheckCXXSymbolExists) -include(CheckFunctionExists) -include(CheckCSourceCompiles) set(BFDEV_ARCH dummy) set(BFDEV_NAME sirius) @@ -43,18 +40,18 @@ commit_hash(BFDEV_COMMITID) commit_branch(BFDEV_BRANCH) option(BFDEV_DEVEL "Enable development mode" OFF) -option(BFDEV_EXAMPLES "Build examples" OFF) option(BFDEV_STRICT "Enable strict compilation" ON) +option(BFDEV_EXAMPLES "Build examples" OFF) option(BFDEV_ASAN "Enable Address Sanitizer" OFF) option(BFDEV_UBSAN "Enable Undefined Behaviour Sanitizer" OFF) -option(BFDEV_DBGLIST "Dynamic debug list" ON) -option(BFDEV_DBGSLIST "Dynamic debug slist" ON) -option(BFDEV_DBGHLIST "Dynamic debug hlist" ON) -option(BFDEV_DBGILIST "Dynamic debug ilist" ON) -option(BFDEV_DBGRBTREE "Dynamic debug rbtree" ON) -option(BFDEV_DBGHEAP "Dynamic debug heap" ON) -option(BFDEV_DBGREFCNT "Dynamic debug refcnt" ON) +option(BFDEV_DEBUG_LIST "Dynamic debug list" ON) +option(BFDEV_DEBUG_SLIST "Dynamic debug slist" ON) +option(BFDEV_DEBUG_HLIST "Dynamic debug hlist" ON) +option(BFDEV_DEBUG_ILIST "Dynamic debug ilist" ON) +option(BFDEV_DEBUG_RBTREE "Dynamic debug rbtree" ON) +option(BFDEV_DEBUG_HEAP "Dynamic debug heap" ON) +option(BFDEV_DEBUG_REFCNT "Dynamic debug refcnt" ON) if(BFDEV_DEVEL) set(BFDEV_EXAMPLES ON) @@ -62,34 +59,6 @@ if(BFDEV_DEVEL) set(BFDEV_UBSAN ON) endif() -if(BFDEV_DBGLIST) - set(BFDEV_DEBUG_LIST ON) -endif() - -if(BFDEV_DBGSLIST) - set(BFDEV_DEBUG_SLIST ON) -endif() - -if(BFDEV_DBGHLIST) - set(BFDEV_DEBUG_HLIST ON) -endif() - -if(BFDEV_DBGILIST) - set(BFDEV_DEBUG_ILIST ON) -endif() - -if(BFDEV_DBGRBTREE) - set(BFDEV_DEBUG_RBTREE ON) -endif() - -if(BFDEV_DBGHEAP) - set(BFDEV_DEBUG_HEAP ON) -endif() - -if(BFDEV_DBGREFCNT) - set(BFDEV_DEBUG_REFCNT ON) -endif() - asm_generic( bfdev/asm-generic/ ${BFDEV_GENERATED_PATH}/bfdev/asm @@ -107,22 +76,6 @@ configure_file( ${BFDEV_CONFIGURE} ) -file(GLOB BFDEV_HEADER - ${BFDEV_HEADER_PATH}/bfdev/*.h -) - -file(GLOB BFDEV_ASM_HEADER - ${BFDEV_HEADER_PATH}/bfdev/asm-generic/*.h -) - -file(GLOB BFDEV_ARCH_ASM_HEADER - ${BFDEV_ARCH_HEADER_PATH}/bfdev/asm/*.h -) - -file(GLOB BFDEV_GENERATED_HEADER - ${BFDEV_GENERATED_PATH}/*.h -) - add_compile_options( -std=gnu11 -Wall @@ -262,9 +215,9 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") ) install(FILES - ${CMAKE_SOURCE_DIR}/README.md - ${CMAKE_SOURCE_DIR}/AUTHORS - ${CMAKE_SOURCE_DIR}/COPYING + ${PROJECT_SOURCE_DIR}/README.md + ${PROJECT_SOURCE_DIR}/AUTHORS + ${PROJECT_SOURCE_DIR}/COPYING DESTINATION ${CMAKE_INSTALL_DOCDIR} ) diff --git a/build.cmake b/build.cmake index 2ef3d509..c2ff7da7 100644 --- a/build.cmake +++ b/build.cmake @@ -3,5 +3,21 @@ # Copyright(c) 2023 John Sanpe # +file(GLOB BFDEV_HEADER + ${BFDEV_HEADER_PATH}/bfdev/*.h +) + +file(GLOB BFDEV_ASM_HEADER + ${BFDEV_HEADER_PATH}/bfdev/asm-generic/*.h +) + +file(GLOB BFDEV_ARCH_ASM_HEADER + ${BFDEV_ARCH_HEADER_PATH}/bfdev/asm/*.h +) + +file(GLOB BFDEV_GENERATED_HEADER + ${BFDEV_GENERATED_PATH}/*.h +) + include(${BFDEV_ARCH_PATH}/build.cmake) include(${BFDEV_SOURCE_PATH}/build.cmake) diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 4dd42036..6466229e 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -5,6 +5,8 @@ set(HOST_C_FLAGS ${HOST_C_FLAGS} + -Wall + -Wextra -I ${PROJECT_SOURCE_DIR}/include -I ${PROJECT_BINARY_DIR}/generated ) diff --git a/scripts/hostrule.cmake b/scripts/hostrule.cmake index 6a47589a..6ec0ea1a 100644 --- a/scripts/hostrule.cmake +++ b/scripts/hostrule.cmake @@ -3,14 +3,14 @@ # Copyright(c) 2023 John Sanpe # -function(host_target target source) +function(host_target target) if(NOT HOST_C_COMPILER) - add_executable(${target} ${source}) + add_executable(${target} ${ARGN}) else() add_custom_target(${target} COMMAND ${HOST_C_COMPILER} ${HOST_C_FLAGS} - -o ${CMAKE_CURRENT_BINARY_DIR}/${target} ${source} + -o ${CMAKE_CURRENT_BINARY_DIR}/${target} ${ARGN} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) diff --git a/src/build.cmake b/src/build.cmake index c29ecfae..1d34c4c2 100644 --- a/src/build.cmake +++ b/src/build.cmake @@ -45,35 +45,35 @@ set(BFDEV_SOURCE ${CMAKE_CURRENT_LIST_DIR}/stringhash.c ) -if(BFDEV_DBGLIST) +if(BFDEV_DEBUG_LIST) set(BFDEV_SOURCE ${BFDEV_SOURCE} ${CMAKE_CURRENT_LIST_DIR}/list-debug.c ) endif() -if(BFDEV_DBGSLIST) +if(BFDEV_DEBUG_SLIST) set(BFDEV_SOURCE ${BFDEV_SOURCE} ${CMAKE_CURRENT_LIST_DIR}/slist-debug.c ) endif() -if(BFDEV_DBGHLIST) +if(BFDEV_DEBUG_HLIST) set(BFDEV_SOURCE ${BFDEV_SOURCE} ${CMAKE_CURRENT_LIST_DIR}/hlist-debug.c ) endif() -if(BFDEV_DBGRBTREE) +if(BFDEV_DEBUG_RBTREE) set(BFDEV_SOURCE ${BFDEV_SOURCE} ${CMAKE_CURRENT_LIST_DIR}/rbtree-debug.c ) endif() -if(BFDEV_DBGHEAP) +if(BFDEV_DEBUG_HEAP) set(BFDEV_SOURCE ${BFDEV_SOURCE} ${CMAKE_CURRENT_LIST_DIR}/heap-debug.c From 08f739c1036a1879ebb8b05f4d67ebfc8512c434 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 01:25:57 +0800 Subject: [PATCH 077/119] feat arc4: added initial functions Signed-off-by: John Sanpe --- include/bfdev/arc4.h | 37 +++++++++++++++++++ src/crypto/arc4.c | 81 ++++++++++++++++++++++++++++++++++++++++++ src/crypto/build.cmake | 1 + 3 files changed, 119 insertions(+) create mode 100644 include/bfdev/arc4.h create mode 100644 src/crypto/arc4.c diff --git a/include/bfdev/arc4.h b/include/bfdev/arc4.h new file mode 100644 index 00000000..2db9e8f7 --- /dev/null +++ b/include/bfdev/arc4.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef _BFDEV_ARC4_H_ +#define _BFDEV_ARC4_H_ + +#include +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef BFDEV_ARC4_KSIZE +# define BFDEV_ARC4_KSIZE 256 +#endif + +typedef struct bfdev_arc4_ctx bfdev_arc4_ctx_t; + +struct bfdev_arc4_ctx { + uint8_t box[BFDEV_ARC4_KSIZE]; + uint8_t proa; + uint8_t prob; +}; + +extern void +bfdev_arc4_trans(bfdev_arc4_ctx_t *ctx, void *buff, + const void *data, size_t size); + +extern void +bfdev_arc4_setkey(bfdev_arc4_ctx_t *ctx, + const uint8_t *key, unsigned int klen); + +BFDEV_END_DECLS + +#endif /* _BFDEV_ARC4_H_ */ diff --git a/src/crypto/arc4.c b/src/crypto/arc4.c new file mode 100644 index 00000000..2a041ed3 --- /dev/null +++ b/src/crypto/arc4.c @@ -0,0 +1,81 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#include +#include +#include + +static __bfdev_always_inline void +arc4_transform(bfdev_arc4_ctx_t *ctx, uint8_t *buff, + const uint8_t *data, size_t size) +{ + uint8_t proa, prob; + uint8_t tx, ty, tb; + uint8_t vx, vy; + + proa = ctx->proa; + prob = ctx->prob; + + vx = ctx->box[proa]; + prob += vx; + vy = ctx->box[prob]; + + for (;;) { + ctx->box[prob] = vx; + vx += vy; + ctx->box[proa] = vy; + proa++; + + tx = ctx->box[proa]; + tb = prob + tx; + ty = ctx->box[tb]; + *buff++ = *data++ ^ ctx->box[vx]; + + if (!--size) + break; + + prob = tb; + vx = tx; + vy = ty; + } + + ctx->proa = proa; + ctx->prob = prob; +} + +export void +bfdev_arc4_trans(bfdev_arc4_ctx_t *ctx, void *buff, + const void *data, size_t size) +{ + if (bfdev_unlikely(!size)) + return; + + arc4_transform(ctx, buff, data, size); +} + +export void +bfdev_arc4_setkey(bfdev_arc4_ctx_t *ctx, + const uint8_t *key, unsigned int klen) +{ + uint8_t value, loop; + unsigned int count; + + if (bfdev_unlikely(!klen)) + return; + + ctx->proa = 1; + ctx->prob = 0; + + for (count = 0; count < BFDEV_ARC4_KSIZE; ++count) + ctx->box[count] = count; + loop = 0; + + for (count = 0; count < BFDEV_ARC4_KSIZE; ++count) { + value = ctx->box[count]; + loop += key[count % klen] + value; + ctx->box[count] = ctx->box[loop]; + ctx->box[loop] = value; + } +} diff --git a/src/crypto/build.cmake b/src/crypto/build.cmake index 8824f3ce..63cb66eb 100644 --- a/src/crypto/build.cmake +++ b/src/crypto/build.cmake @@ -5,6 +5,7 @@ set(BFDEV_SOURCE ${BFDEV_SOURCE} + ${CMAKE_CURRENT_LIST_DIR}/arc4.c ${CMAKE_CURRENT_LIST_DIR}/ascii85.c ${CMAKE_CURRENT_LIST_DIR}/base32.c ${CMAKE_CURRENT_LIST_DIR}/base64.c From faf9ae691403807f3c53bbeb4eb3c0af79c4a1cc Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 01:29:53 +0800 Subject: [PATCH 078/119] feat arc4: added bandwidth example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/arc4/.gitignore | 2 ++ examples/arc4/CMakeLists.txt | 22 ++++++++++++ examples/arc4/bandwidth.c | 68 ++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 examples/arc4/.gitignore create mode 100644 examples/arc4/CMakeLists.txt create mode 100644 examples/arc4/bandwidth.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 647882d5..3b71683a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,6 +5,7 @@ add_subdirectory(action) add_subdirectory(allocator) +add_subdirectory(arc4) add_subdirectory(array) add_subdirectory(base32) add_subdirectory(base64) diff --git a/examples/arc4/.gitignore b/examples/arc4/.gitignore new file mode 100644 index 00000000..baff85d8 --- /dev/null +++ b/examples/arc4/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/arc4-bandwidth diff --git a/examples/arc4/CMakeLists.txt b/examples/arc4/CMakeLists.txt new file mode 100644 index 00000000..69a1cf41 --- /dev/null +++ b/examples/arc4/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2024 John Sanpe +# + +add_executable(arc4-bandwidth bandwidth.c) +target_link_libraries(arc4-bandwidth bfdev) +add_test(arc4-bandwidth arc4-bandwidth) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + bandwidth.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/arc4 + ) + + install(TARGETS + arc4-bandwidth + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/arc4/bandwidth.c b/examples/arc4/bandwidth.c new file mode 100644 index 00000000..60b6963f --- /dev/null +++ b/examples/arc4/bandwidth.c @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "arc4-bandwidth" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include "../time.h" + +#define TEST_SIZE BFDEV_SZ_1MiB +#define TEST_LOOP 3 +#define TEST_KEY 64 + +int main(int argc, const char *argv[]) +{ + bfdev_arc4_ctx_t sctx, dctx; + unsigned int count, loop; + uint32_t cksum, check; + uint8_t *dbuff, *sbuff; + + sbuff = malloc(TEST_SIZE); + if (!sbuff) + return 1; + + dbuff = malloc(TEST_SIZE); + if (!dbuff) + return 1; + + srand(time(NULL)); + for (count = 0; count < 64; ++count) + sbuff[count] = (uint8_t)rand(); + bfdev_arc4_setkey(&sctx, sbuff, TEST_KEY); + + for (count = 0; count < TEST_SIZE; ++count) + sbuff[count] = (uint8_t)rand(); + + cksum = bfdev_crc32(sbuff, TEST_SIZE, (uint32_t)~0UL); + bfdev_log_info("start checksum: %#010x\n", cksum); + + for (count = 0; count < TEST_LOOP; ++count) { + EXAMPLE_TIME_LOOP(&loop, 1000, + dctx = sctx; + bfdev_arc4_trans(&sctx, dbuff, sbuff, TEST_SIZE); + bfdev_arc4_trans(&dctx, sbuff, dbuff, TEST_SIZE); + 0; + ); + bfdev_log_info("bandwidth %u: %uMiB/s\n", count, loop * 2); + } + + check = bfdev_crc32(sbuff, TEST_SIZE, (uint32_t)~0UL); + if (cksum != check) { + bfdev_log_err("verification failed\n"); + return 1; + } + bfdev_log_info("verification pass\n"); + + free(sbuff); + free(dbuff); + + return 0; +} From 6c26a595a1a558dd0d73503cbf19c7955665e142 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 05:15:16 +0800 Subject: [PATCH 079/119] refactor bitmap: optimized code style in source file Signed-off-by: John Sanpe --- src/bitmap.c | 161 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 115 insertions(+), 46 deletions(-) diff --git a/src/bitmap.c b/src/bitmap.c index 3bec6ce6..18b17475 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -11,15 +11,20 @@ export bool bfdev_bitmap_comp_equal(const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_DIV_LONG(bits); + unsigned int index, length; + unsigned long value; - for (index = 0; index < len; ++index) + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { if (src1[index] != src2[index]) return false; + } - if (BFDEV_BITS_MOD_LONG(bits)) - if ((src1[index] ^ src2[index]) & BFDEV_BIT_LOW_MASK(bits)) + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] ^ src2[index]; + if (value & BFDEV_BIT_LOW_MASK(bits)) return false; + } return true; } @@ -28,15 +33,20 @@ export bool bfdev_bitmap_comp_or_equal(const unsigned long *src1, const unsigned long *src2, const unsigned long *src3, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_DIV_LONG(bits); + unsigned int index, length; + unsigned long value; - for (index = 0; index < len; ++index) + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { if ((src1[index] | src2[index]) != src3[index]) return false; + } - if (BFDEV_BITS_MOD_LONG(bits)) - if (((src1[index] | src2[index]) ^ src3[index]) & BFDEV_BIT_LOW_MASK(bits)) + if (BFDEV_BITS_MOD_LONG(bits)) { + value = (src1[index] | src2[index]) ^ src3[index]; + if (value & BFDEV_BIT_LOW_MASK(bits)) return false; + } return true; } @@ -45,15 +55,20 @@ export bool bfdev_bitmap_comp_intersects(const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_DIV_LONG(bits); + unsigned int index, length; + unsigned long value; - for (index = 0; index < len; ++index) + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { if ((src1[index] & src2[index])) return true; + } - if (BFDEV_BITS_MOD_LONG(bits)) - if ((src1[index] & src2[index]) & BFDEV_BIT_LOW_MASK(bits)) + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] & src2[index]; + if (value & BFDEV_BIT_LOW_MASK(bits)) return true; + } return false; } @@ -62,14 +77,19 @@ export bool bfdev_bitmap_comp_and(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_DIV_LONG(bits); - unsigned long result = 0; + unsigned int index, length; + unsigned long value, result = 0; - for (index = 0; index < len; ++index) - result |= (dest[index] = src1[index] & src2[index]); + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = src1[index] & src2[index]; + result |= (dest[index] = value); + } - if (BFDEV_BITS_MOD_LONG(bits)) - result |= (dest[index] = src1[index] & src2[index] & BFDEV_BIT_LOW_MASK(bits)); + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] & src2[index]; + result |= (dest[index] = value) & BFDEV_BIT_LOW_MASK(bits); + } return !!result; } @@ -78,14 +98,19 @@ export bool bfdev_bitmap_comp_andnot(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_DIV_LONG(bits); - unsigned long result = 0; + unsigned int index, length; + unsigned long value, result = 0; - for (index = 0; index < len; ++index) - result |= (dest[index] = src1[index] & ~src2[index]); + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = src1[index] & ~src2[index]; + result |= (dest[index] = value); + } - if (BFDEV_BITS_MOD_LONG(bits)) - result |= (dest[index] = src1[index] & ~src2[index] & BFDEV_BIT_LOW_MASK(bits)); + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] & ~src2[index]; + result |= (dest[index] = value) & BFDEV_BIT_LOW_MASK(bits); + } return !!result; } @@ -94,30 +119,57 @@ export void bfdev_bitmap_comp_or(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_TO_LONG(bits); + unsigned int index, length; + unsigned long value; + + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = src1[index] | src2[index]; + dest[index] = value; + } - for (index = 0; index < len; ++index) - dest[index] = src1[index] | src2[index]; + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] | src2[index]; + dest[index] = value & BFDEV_BIT_LOW_MASK(bits); + } } export void bfdev_bitmap_comp_xor(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_TO_LONG(bits); + unsigned int index, length; + unsigned long value; + + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = src1[index] ^ src2[index]; + dest[index] = value; + } - for (index = 0; index < len; ++index) - dest[index] = src1[index] ^ src2[index]; + if (BFDEV_BITS_MOD_LONG(bits)) { + value = src1[index] ^ src2[index]; + dest[index] = value & BFDEV_BIT_LOW_MASK(bits); + } } export void bfdev_bitmap_comp_complement(unsigned long *dest, const unsigned long *src, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_TO_LONG(bits); + unsigned int index, length; + unsigned long value; + + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = ~src[index]; + dest[index] = value; + } - for (index = 0; index < len; ++index) - dest[index] = ~src[index]; + if (BFDEV_BITS_MOD_LONG(bits)) { + value = ~src[index]; + dest[index] = value & BFDEV_BIT_LOW_MASK(bits); + } } export void @@ -125,19 +177,32 @@ bfdev_bitmap_comp_replace(unsigned long *bitmap, const unsigned long *oldp, const unsigned long *newp, const unsigned long *mask, unsigned int bits) { - unsigned int index, len = BFDEV_BITS_TO_LONG(bits); + unsigned int index, length; + unsigned long value; + + length = BFDEV_BITS_DIV_LONG(bits); + for (index = 0; index < length; ++index) { + value = (oldp[index] & ~mask[index]) | (newp[index] & mask[index]); + bitmap[index] = value; + } - for (index = 0; index < len; ++index) - bitmap[index] = (oldp[index] & ~mask[index]) | (newp[index] & mask[index]); + if (BFDEV_BITS_MOD_LONG(bits)) { + value = (oldp[index] & ~mask[index]) | (newp[index] & mask[index]); + bitmap[index] = value & BFDEV_BIT_LOW_MASK(bits); + } } export void -bfdev_bitmap_comp_set(unsigned long *bitmap, unsigned int start, unsigned int bits) +bfdev_bitmap_comp_set(unsigned long *bitmap, unsigned int start, + unsigned int bits) { - unsigned int bits_to_set = BFDEV_BITS_PER_LONG - BFDEV_BITS_MOD_LONG(start); - unsigned long mask_to_set = BFDEV_BIT_HIGH_MASK(start); - unsigned long *curr = bitmap + BFDEV_BITS_DIV_LONG(start); - const unsigned int size = start + bits; + unsigned int bits_to_set, size; + unsigned long mask_to_set, *curr; + + bits_to_set = BFDEV_BITS_PER_LONG - BFDEV_BITS_MOD_LONG(start); + mask_to_set = BFDEV_BIT_HIGH_MASK(start); + curr = bitmap + BFDEV_BITS_DIV_LONG(start); + size = start + bits; while (bits - bits_to_set) { *curr++ |= mask_to_set; @@ -153,12 +218,16 @@ bfdev_bitmap_comp_set(unsigned long *bitmap, unsigned int start, unsigned int bi } export void -bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, unsigned int bits) +bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, + unsigned int bits) { - unsigned int bits_to_clr = BFDEV_BITS_PER_LONG - BFDEV_BITS_MOD_LONG(start); - unsigned long mask_to_clr = BFDEV_BIT_HIGH_MASK(start); - unsigned long *curr = bitmap + BFDEV_BITS_DIV_LONG(start); - const unsigned int size = start + bits; + unsigned int bits_to_clr, size; + unsigned long mask_to_clr, *curr; + + bits_to_clr = BFDEV_BITS_PER_LONG - BFDEV_BITS_MOD_LONG(start); + mask_to_clr = BFDEV_BIT_HIGH_MASK(start); + curr = bitmap + BFDEV_BITS_DIV_LONG(start); + size = start + bits; while (bits - bits_to_clr) { *curr++ &= ~mask_to_clr; From d959afe0667a50dec70f6bc236be59a3bd8577b2 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 06:01:45 +0800 Subject: [PATCH 080/119] refactor bitmap: separation complex functions Signed-off-by: John Sanpe --- include/bfdev/bitmap-comp.h | 84 +++++++++++++ include/bfdev/bitmap.h | 236 +++++++++++++++++------------------- src/bitmap.c | 6 +- 3 files changed, 198 insertions(+), 128 deletions(-) create mode 100644 include/bfdev/bitmap-comp.h diff --git a/include/bfdev/bitmap-comp.h b/include/bfdev/bitmap-comp.h new file mode 100644 index 00000000..52c59aa6 --- /dev/null +++ b/include/bfdev/bitmap-comp.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_BITMAP_COMP_H_ +#define _BFDEV_BITMAP_COMP_H_ + +#include +#include + +BFDEV_BEGIN_DECLS + +extern bool +bfdev_bitmap_comp_equal(const unsigned long *src1, const unsigned long *src2, + unsigned int bits); + +extern bool +bfdev_bitmap_comp_or_equal(const unsigned long *src1, const unsigned long *src2, + const unsigned long *src3, unsigned int bits); + +extern bool +bfdev_bitmap_comp_intersects(const unsigned long *src1, const unsigned long *src2, + unsigned int bits); + +extern bool +bfdev_bitmap_comp_and(unsigned long *dest, const unsigned long *src1, + const unsigned long *src2, unsigned int bits); + +extern bool +bfdev_bitmap_comp_andnot(unsigned long *dest, const unsigned long *src1, + const unsigned long *src2, unsigned int bits); + +extern void +bfdev_bitmap_comp_or(unsigned long *dest, const unsigned long *src1, + const unsigned long *src2, unsigned int bits); + +extern void +bfdev_bitmap_comp_xor(unsigned long *dest, const unsigned long *src1, + const unsigned long *src2, unsigned int bits); + +extern void +bfdev_bitmap_comp_complement(unsigned long *dest, const unsigned long *src, + unsigned int bits); + +extern void +bfdev_bitmap_comp_replace(unsigned long *bitmap, const unsigned long *oldp, + const unsigned long *newp, const unsigned long *mask, + unsigned int bits); + +extern void +bfdev_bitmap_comp_set(unsigned long *bitmap, unsigned int start, + unsigned int bits); + +extern void +bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, + unsigned int bits); + +/** + * bfdev_bitmap_alloc - alloc a bitmap. + * @bits: number of bits in the bitmap. + * @flags: allocate flags. + */ +extern unsigned long * +bfdev_bitmap_alloc(const bfdev_alloc_t *alloc, unsigned int bits); + +/** + * bfdev_bitmap_zalloc - alloc and zeroed a bitmap. + * @bits: number of bits in the bitmap. + * @flags: allocate flags. + */ +extern unsigned long * +bfdev_bitmap_zalloc(const bfdev_alloc_t *alloc, unsigned int bits); + +/** + * bfdev_bitmap_free - free a bitmap. + * @bitmap: bitmap to free. + */ +extern void +bfdev_bitmap_free(const bfdev_alloc_t *alloc, const unsigned long *bitmap); + +BFDEV_END_DECLS + +#endif /* _BFDEV_BITMAP_COMP_H_ */ diff --git a/include/bfdev/bitmap.h b/include/bfdev/bitmap.h index 633a37e7..b5378767 100644 --- a/include/bfdev/bitmap.h +++ b/include/bfdev/bitmap.h @@ -11,76 +11,35 @@ #include #include #include -#include #include +#include BFDEV_BEGIN_DECLS #ifdef CONFIG_ARCH_LITTLE_ENDIAN -# define BFDEV_BITMAP_MEM_ALIGNMENT BFDEV_BITS_PER_BYTE +# define BFDEV_BITMAP_ALIGN BFDEV_BITS_PER_BYTE #else -# define BFDEV_BITMAP_MEM_ALIGNMENT (BFDEV_BITS_PER_BYTE * BFDEV_BYTES_PER_LONG) +# define BFDEV_BITMAP_ALIGN (BFDEV_BITS_PER_BYTE * BFDEV_BYTES_PER_LONG) #endif -#define BFDEV_BITMAP_MEM_MASK (BFDEV_BITMAP_MEM_ALIGNMENT - 1) +#define BFDEV_BITMAP_MASK (BFDEV_BITMAP_ALIGN - 1) #define BFDEV_DEFINE_BITMAP(name, bits) \ unsigned long name[BFDEV_BITS_TO_LONG(bits)]; -extern bool -bfdev_bitmap_comp_equal(const unsigned long *src1, const unsigned long *src2, - unsigned int bits); - -extern bool -bfdev_bitmap_comp_or_equal(const unsigned long *src1, const unsigned long *src2, - const unsigned long *src3, unsigned int bits); - -extern bool -bfdev_bitmap_comp_intersects(const unsigned long *src1, const unsigned long *src2, - unsigned int bits); - -extern bool -bfdev_bitmap_comp_and(unsigned long *dest, const unsigned long *src1, - const unsigned long *src2, unsigned int bits); - -extern bool -bfdev_bitmap_comp_andnot(unsigned long *dest, const unsigned long *src1, - const unsigned long *src2, unsigned int bits); - -extern void -bfdev_bitmap_comp_or(unsigned long *dest, const unsigned long *src1, - const unsigned long *src2, unsigned int bits); - -extern void -bfdev_bitmap_comp_xor(unsigned long *dest, const unsigned long *src1, - const unsigned long *src2, unsigned int bits); - -extern void -bfdev_bitmap_comp_complement(unsigned long *dest, const unsigned long *src, - unsigned int bits); - -extern void -bfdev_bitmap_comp_replace(unsigned long *bitmap, const unsigned long *oldp, - const unsigned long *newp, const unsigned long *mask, - unsigned int bits); - -extern void -bfdev_bitmap_comp_set(unsigned long *bitmap, unsigned int start, - unsigned int bits); - -extern void -bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, - unsigned int bits); +#define bfdev_bitmap_const_aligned(bits) ( \ + __builtin_constant_p((bits) & BFDEV_BITMAP_MASK) && \ + bfdev_align_check(bits, BFDEV_BITMAP_ALIGN) \ +) static __bfdev_always_inline bool bfdev_bitmap_empty(const unsigned long *src, unsigned int bits) { if (bfdev_const_small_nbits(bits)) return !(*src & BFDEV_BIT_LOW_MASK(bits)); - else if (__builtin_constant_p(bits & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(bits, BFDEV_BITMAP_MEM_ALIGNMENT)) - return !bfdev_memdiff(src, BFDEV_UINT_MIN, bits / BFDEV_BITS_PER_BYTE); - else + else if (!bfdev_bitmap_const_aligned(bits)) return bfdev_find_first_bit(src, bits) >= bits; + + return !bfdev_memdiff(src, BFDEV_UINT_MIN, bits / BFDEV_BITS_PER_BYTE); } static __bfdev_always_inline bool @@ -88,11 +47,10 @@ bfdev_bitmap_full(const unsigned long *src, unsigned int bits) { if (bfdev_const_small_nbits(bits)) return !(~(*src) & BFDEV_BIT_LOW_MASK(bits)); - else if (__builtin_constant_p(bits & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(bits, BFDEV_BITMAP_MEM_ALIGNMENT)) - return !bfdev_memdiff(src, BFDEV_UINT_MAX, bits / BFDEV_BITS_PER_BYTE); - else + else if (!bfdev_bitmap_const_aligned(bits)) return bfdev_find_first_zero(src, bits) >= bits; + + return !bfdev_memdiff(src, BFDEV_UINT_MAX, bits / BFDEV_BITS_PER_BYTE); } static __bfdev_always_inline bool @@ -101,144 +59,172 @@ bfdev_bitmap_equal(const unsigned long *src1, const unsigned long *src2, { if (bfdev_const_small_nbits(bits)) return !!((*src1 ^ *src2) & BFDEV_BIT_LOW_MASK(bits)); - else if (__builtin_constant_p(bits & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(bits, BFDEV_BITMAP_MEM_ALIGNMENT)) - return memcmp(src1, src2, bits / BFDEV_BITS_PER_BYTE); - else + else if (!bfdev_bitmap_const_aligned(bits)) return bfdev_bitmap_comp_equal(src1, src2, bits); + + return memcmp(src1, src2, bits / BFDEV_BITS_PER_BYTE); } static __bfdev_always_inline bool bfdev_bitmap_or_equal(const unsigned long *src1, const unsigned long *src2, const unsigned long *src3, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - return !!(((*src1 | *src2) ^ *src3) & BFDEV_BIT_LOW_MASK(bits)); - else + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_or_equal(src1, src2, src3, bits); + + value = (*src1 | *src2) ^ *src3; + return !!(value & BFDEV_BIT_LOW_MASK(bits)); } static __bfdev_always_inline bool bfdev_bitmap_intersects(const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - return !!((*src1 & *src2) & BFDEV_BIT_LOW_MASK(bits)); - else + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_intersects(src1, src2, bits); + + value = *src1 & *src2; + return !!(value & BFDEV_BIT_LOW_MASK(bits)); } static __bfdev_always_inline bool bfdev_bitmap_and(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - return !!(*dest = *src1 & *src2 & BFDEV_BIT_LOW_MASK(bits)); - else + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_and(dest, src1, src2, bits); + + value = *src1 & *src2; + return !!(*dest = (value & BFDEV_BIT_LOW_MASK(bits))); +} + +static __bfdev_always_inline bool +bfdev_bitmap_andnot(unsigned long *dest, const unsigned long *src1, + const unsigned long *src2, unsigned int bits) +{ + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_andnot(dest, src1, src2, bits); + + value = *src1 & ~*src2; + return !!(*dest = (value & BFDEV_BIT_LOW_MASK(bits))); } static __bfdev_always_inline void bfdev_bitmap_or(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - *dest = *src1 | *src2; - else - bfdev_bitmap_comp_or(dest, src1, src2, bits); + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_or(dest, src1, src2, bits); + + value = *src1 | *src2; + *dest = value & BFDEV_BIT_LOW_MASK(bits); } static __bfdev_always_inline void bfdev_bitmap_xor(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - *dest = *src1 ^ *src2; - else - bfdev_bitmap_comp_xor(dest, src1, src2, bits); + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_xor(dest, src1, src2, bits); + + value = *src1 ^ *src2; + *dest = value & BFDEV_BIT_LOW_MASK(bits); } static __bfdev_always_inline void bfdev_bitmap_complement(unsigned long *dest, const unsigned long *src, unsigned int bits) { - if (bfdev_const_small_nbits(bits)) - *dest = ~(*src); - else - bfdev_bitmap_comp_complement(dest, src, bits); + unsigned int value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_complement(dest, src, bits); + + value = ~*src; + *dest = value & BFDEV_BIT_LOW_MASK(bits); } static __bfdev_always_inline void bfdev_bitmap_set(unsigned long *bitmap, unsigned int start, unsigned int bits) { + size_t size, offset; + if (__builtin_constant_p(bits) && bits == 1) - bfdev_bit_set(bitmap, start); - else if (__builtin_constant_p(start & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(start, BFDEV_BITMAP_MEM_ALIGNMENT) && - __builtin_constant_p(bits & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(bits, BFDEV_BITMAP_MEM_ALIGNMENT)) - memset((char *)bitmap + start / BFDEV_BITS_PER_BYTE, 0xff, bits / BFDEV_BITS_PER_BYTE); - else - bfdev_bitmap_comp_set(bitmap, start, bits); + return bfdev_bit_set(bitmap, start); + else if (!bfdev_bitmap_const_aligned(start) || + !bfdev_bitmap_const_aligned(bits)) + return bfdev_bitmap_comp_set(bitmap, start, bits); + + offset = start / BFDEV_BITS_PER_BYTE; + size = bits / BFDEV_BITS_PER_BYTE; + + memset((char *)bitmap + offset, 0xff, size); } static __bfdev_always_inline void bfdev_bitmap_clr(unsigned long *bitmap, unsigned int start, unsigned int bits) { + size_t size, offset; + if (__builtin_constant_p(bits) && bits == 1) - bfdev_bit_clr(bitmap, start); - else if (__builtin_constant_p(start & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(start, BFDEV_BITMAP_MEM_ALIGNMENT) && - __builtin_constant_p(bits & BFDEV_BITMAP_MEM_MASK) && - bfdev_align_check(bits, BFDEV_BITMAP_MEM_ALIGNMENT)) - memset((char *)bitmap + start / BFDEV_BITS_PER_BYTE, 0x00, bits / BFDEV_BITS_PER_BYTE); - else - bfdev_bitmap_comp_clr(bitmap, start, bits); + return bfdev_bit_clr(bitmap, start); + else if (!bfdev_bitmap_const_aligned(start) || + !bfdev_bitmap_const_aligned(bits)) + return bfdev_bitmap_comp_clr(bitmap, start, bits); + + offset = start / BFDEV_BITS_PER_BYTE; + size = bits / BFDEV_BITS_PER_BYTE; + + memset((char *)bitmap + offset, 0, size); } static __bfdev_always_inline void bfdev_bitmap_zero(unsigned long *bitmap, unsigned int bits) { - unsigned int len = BFDEV_BITS_TO_U8(bits); - memset(bitmap, 0, len); + unsigned int length; + + if (bfdev_const_small_nbits(bits)) + *bitmap = 0; + + length = BFDEV_BITS_TO_U8(bits); + memset(bitmap, 0, length); } static __bfdev_always_inline void bfdev_bitmap_fill(unsigned long *bitmap, unsigned int bits) { - unsigned int len = BFDEV_BITS_TO_U8(bits); - memset(bitmap, UINT8_MAX, len); + unsigned int length; + + if (bfdev_const_small_nbits(bits)) + *bitmap = 0xff; + + length = BFDEV_BITS_TO_U8(bits); + memset(bitmap, UINT8_MAX, length); } static __bfdev_always_inline void bfdev_bitmap_copy(unsigned long *dest, unsigned long *src, unsigned int bits) { - unsigned int len = BFDEV_BITS_TO_U8(bits); - memcpy(dest, src, len); -} - -/** - * bfdev_bitmap_alloc - alloc a bitmap. - * @bits: number of bits in the bitmap. - * @flags: allocate flags. - */ -extern unsigned long * -bfdev_bitmap_alloc(const bfdev_alloc_t *alloc, unsigned int bits); + unsigned int length; -/** - * bfdev_bitmap_zalloc - alloc and zeroed a bitmap. - * @bits: number of bits in the bitmap. - * @flags: allocate flags. - */ -extern unsigned long * -bfdev_bitmap_zalloc(const bfdev_alloc_t *alloc, unsigned int bits); + if (bfdev_const_small_nbits(bits)) + *dest = *src; -/** - * bfdev_bitmap_free - free a bitmap. - * @bitmap: bitmap to free. - */ -extern void -bfdev_bitmap_free(const bfdev_alloc_t *alloc, const unsigned long *bitmap); + length = BFDEV_BITS_TO_U8(bits); + memcpy(dest, src, length); +} BFDEV_END_DECLS diff --git a/src/bitmap.c b/src/bitmap.c index 18b17475..f45817a4 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -4,7 +4,7 @@ */ #include -#include +#include #include export bool @@ -88,7 +88,7 @@ bfdev_bitmap_comp_and(unsigned long *dest, const unsigned long *src1, if (BFDEV_BITS_MOD_LONG(bits)) { value = src1[index] & src2[index]; - result |= (dest[index] = value) & BFDEV_BIT_LOW_MASK(bits); + result |= (dest[index] = (value & BFDEV_BIT_LOW_MASK(bits))); } return !!result; @@ -109,7 +109,7 @@ bfdev_bitmap_comp_andnot(unsigned long *dest, const unsigned long *src1, if (BFDEV_BITS_MOD_LONG(bits)) { value = src1[index] & ~src2[index]; - result |= (dest[index] = value) & BFDEV_BIT_LOW_MASK(bits); + result |= (dest[index] = (value & BFDEV_BIT_LOW_MASK(bits))); } return !!result; From ee8d9e942a91c1f412b2b923fd3da626be49df42 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 06:22:11 +0800 Subject: [PATCH 081/119] refactor bitwalk: optimized code style in source file Signed-off-by: John Sanpe --- src/bitwalk.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/bitwalk.c b/src/bitwalk.c index 038a790f..bb96528a 100644 --- a/src/bitwalk.c +++ b/src/bitwalk.c @@ -21,10 +21,9 @@ bfdev_comp_find_first_bit(const unsigned long *block, unsigned int bits, bool sw else value = *block; - if (value == BFDEV_ULONG_MIN) - block++; - else + if (value != BFDEV_ULONG_MIN) return base + bfdev_ffsuf(value); + block++; } return bits; @@ -47,7 +46,6 @@ bfdev_comp_find_last_bit(const unsigned long *block, unsigned int bits, bool swa if ((value ^ BFDEV_ULONG_MIN) & val) return idx * BFDEV_BITS_PER_LONG + bfdev_flsuf(value); - val = BFDEV_ULONG_MAX; } while (idx--); } @@ -68,10 +66,9 @@ bfdev_comp_find_first_zero(const unsigned long *block, unsigned int bits, bool s else value = *block; - if (value == BFDEV_ULONG_MAX) - block++; - else + if (value != BFDEV_ULONG_MAX) return base + bfdev_ffzuf(value); + block++; } return bits; From b9bb2c5d919a4e063537eace3b9bc13c7110e307 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 17:19:35 +0800 Subject: [PATCH 082/119] feat allocpool: added initial typedef Signed-off-by: John Sanpe --- include/bfdev/allocpool.h | 14 ++++++++------ src/allocpool.c | 4 ++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/include/bfdev/allocpool.h b/include/bfdev/allocpool.h index ae84f00b..ed9fee91 100644 --- a/include/bfdev/allocpool.h +++ b/include/bfdev/allocpool.h @@ -12,6 +12,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_allocpool bfdev_allocpool_t; + struct bfdev_allocpool { void *block; size_t size; @@ -23,10 +25,10 @@ struct bfdev_allocpool { {.block = (BLOCK), .size = (SIZE)} #define BFDEV_ALLOCPOOL_INIT(block, size) \ - (struct bfdev_allocpool) BFDEV_ALLOCPOOL_STATIC(block, size) + (bfdev_allocpool_t) BFDEV_ALLOCPOOL_STATIC(block, size) #define BFDEV_DEFINE_ALLOCPOOL(name, block, size) \ - struct bfdev_allocpool name = BFDEV_ALLOCPOOL_INIT(block, size) + bfdev_allocpool_t name = BFDEV_ALLOCPOOL_INIT(block, size) /** * bfdev_allocpool_init() - Allocation mempool initialize. @@ -35,7 +37,7 @@ struct bfdev_allocpool { * @size: mempool array size. */ static inline void -bfdev_allocpool_init(struct bfdev_allocpool *pool, void *block, size_t size) +bfdev_allocpool_init(bfdev_allocpool_t *pool, void *block, size_t size) { *pool = BFDEV_ALLOCPOOL_INIT(block, size); } @@ -45,7 +47,7 @@ bfdev_allocpool_init(struct bfdev_allocpool *pool, void *block, size_t size) * @pool: the allocpool to reset. */ static inline void -bfdev_allocpool_reset(struct bfdev_allocpool *pool) +bfdev_allocpool_reset(bfdev_allocpool_t *pool) { pool->last = 0; pool->count = 0; @@ -58,7 +60,7 @@ bfdev_allocpool_reset(struct bfdev_allocpool *pool) * @align: align to allocation. */ extern __bfdev_malloc void * -bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align); +bfdev_allocpool_alloc(bfdev_allocpool_t *pool, size_t size, size_t align); /** * bfdev_allocpool_free() - Allocation mempool free. @@ -66,7 +68,7 @@ bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align); * @block: memory block to free. */ extern void -bfdev_allocpool_free(struct bfdev_allocpool *pool, const void *block); +bfdev_allocpool_free(bfdev_allocpool_t *pool, const void *block); BFDEV_END_DECLS diff --git a/src/allocpool.c b/src/allocpool.c index 0a1cea44..7580ff8b 100644 --- a/src/allocpool.c +++ b/src/allocpool.c @@ -8,7 +8,7 @@ #include export __bfdev_malloc void * -bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align) +bfdev_allocpool_alloc(bfdev_allocpool_t *pool, size_t size, size_t align) { uintptr_t offset; void *retval; @@ -30,7 +30,7 @@ bfdev_allocpool_alloc(struct bfdev_allocpool *pool, size_t size, size_t align) } export void -bfdev_allocpool_free(struct bfdev_allocpool *pool, const void *block) +bfdev_allocpool_free(bfdev_allocpool_t *pool, const void *block) { if (!--pool->count) pool->last = 0; From b6eddfc3b98ccecf9cb0ea29393b29ddbdb60fb0 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 18:10:44 +0800 Subject: [PATCH 083/119] perf allocator: optimize zalloc with calloa Signed-off-by: John Sanpe --- include/bfdev/allocator.h | 1 + src/allocator.c | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index 4afc62ba..0888c821 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -23,6 +23,7 @@ struct bfdev_alloc { struct bfdev_alloc_ops { bfdev_malloc_t alloc; + bfdev_malloc_t zalloc; bfdev_realloc_t realloc; bfdev_free_t free; }; diff --git a/src/allocator.c b/src/allocator.c index d1fdb77b..6bd9c201 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -8,11 +8,17 @@ #include static __bfdev_always_inline void * -generic_malloc(size_t size) +generic_alloc(size_t size) { return malloc(size); } +static __bfdev_always_inline void * +generic_zalloc(size_t size) +{ + return calloc(1, size); +} + static __bfdev_always_inline void * generic_realloc(const void *block, size_t resize) { @@ -39,8 +45,9 @@ bfdev_malloc(const bfdev_alloc_t *alloc, size_t size) const bfdev_alloc_ops_t *ops; void *pdata, *retval; - if (!(ops = alloc_check(alloc)) || !ops->alloc) - retval = generic_malloc(size); + ops = alloc_check(alloc); + if (!ops || !ops->alloc) + retval = generic_alloc(size); else { pdata = alloc->pdata; retval = ops->alloc(size, pdata); @@ -55,16 +62,14 @@ bfdev_zalloc(const bfdev_alloc_t *alloc, size_t size) const bfdev_alloc_ops_t *ops; void *pdata, *retval; - if (!(ops = alloc_check(alloc)) || !ops->alloc) - retval = generic_malloc(size); + ops = alloc_check(alloc); + if (!ops || !ops->zalloc) + retval = generic_zalloc(size); else { pdata = alloc->pdata; - retval = ops->alloc(size, pdata); + retval = ops->zalloc(size, pdata); } - if (bfdev_likely(retval)) - memset(retval, 0, size); - return retval; } @@ -74,7 +79,8 @@ bfdev_realloc(const bfdev_alloc_t *alloc, const void *block, size_t resize) const bfdev_alloc_ops_t *ops; void *pdata, *retval; - if (!(ops = alloc_check(alloc)) || !ops->realloc) + ops = alloc_check(alloc); + if (!ops || !ops->realloc) retval = generic_realloc((void *)block, resize); else { pdata = alloc->pdata; @@ -90,7 +96,8 @@ bfdev_free(const bfdev_alloc_t *alloc, const void *block) const bfdev_alloc_ops_t *ops; void *pdata; - if (!(ops = alloc_check(alloc)) || !ops->free) + ops = alloc_check(alloc); + if (!ops || !ops->free) generic_free(block); else { pdata = alloc->pdata; From ca915b6f0703367dda1667dc7db8b31037bcf87c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 19:35:11 +0800 Subject: [PATCH 084/119] feat crc: provides cropping options of loop unrolling Signed-off-by: John Sanpe --- CMakeLists.txt | 1 + cmake/config.h.in | 1 + include/bfdev/crypto/crc-inline.h | 32 +++++++++++++++++------ scripts/CMakeLists.txt | 42 ++++++++++++++++++------------- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6effe2a1..2ea6c6b9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ option(BFDEV_DEBUG_ILIST "Dynamic debug ilist" ON) option(BFDEV_DEBUG_RBTREE "Dynamic debug rbtree" ON) option(BFDEV_DEBUG_HEAP "Dynamic debug heap" ON) option(BFDEV_DEBUG_REFCNT "Dynamic debug refcnt" ON) +option(BFDEV_CRC_EXTEND "CRC loop unfolding optimize" ON) if(BFDEV_DEVEL) set(BFDEV_EXAMPLES ON) diff --git a/cmake/config.h.in b/cmake/config.h.in index 25e78bf0..2ab87a4b 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -33,6 +33,7 @@ BFDEV_BEGIN_DECLS #cmakedefine BFDEV_DEBUG_RBTREE #cmakedefine BFDEV_DEBUG_HEAP #cmakedefine BFDEV_DEBUG_REFCNT +#cmakedefine BFDEV_CRC_EXTEND #define BFDEV_VERSION_CHECK(major, minor, patch) ( \ ((major) == BFDEV_VERSION_MAJOR) && \ diff --git a/include/bfdev/crypto/crc-inline.h b/include/bfdev/crypto/crc-inline.h index 1fa37b60..f4665f99 100644 --- a/include/bfdev/crypto/crc-inline.h +++ b/include/bfdev/crypto/crc-inline.h @@ -14,14 +14,29 @@ BFDEV_BEGIN_DECLS -#define BFDEV_CRC_INLINE(name, type, table, bswap) \ -static inline type \ -name##_byte(type crc, const uint8_t data) \ -{ \ - unsigned int index = (crc ^ data) & 0xff; \ - return table[0][index] ^ (crc >> 8); \ -} \ - \ +#define BFDEV_CRC_BYTE(name, type, table) \ +static inline type \ +name##_byte(type crc, const uint8_t data) \ +{ \ + unsigned int index = (crc ^ data) & 0xff; \ + return table[0][index] ^ (crc >> 8); \ +} + +#ifndef BFDEV_CRC_EXTEND +# define BFDEV_CRC_INLINE(name, type, table, bswap) \ +BFDEV_CRC_BYTE(name, type, table) \ +static inline type \ +name##_inline(const uint8_t *src, size_t len, type crc) \ +{ \ + crc = bswap(crc); \ + while (len--) \ + crc = name##_byte(crc, *src++); \ + \ + return bswap(crc); \ +} +#else +# define BFDEV_CRC_INLINE(name, type, table, bswap) \ +BFDEV_CRC_BYTE(name, type, table) \ static inline type \ name##_inline(const uint8_t *src, size_t len, type crc) \ { \ @@ -55,6 +70,7 @@ name##_inline(const uint8_t *src, size_t len, type crc) \ \ return bswap(crc); \ } +#endif BFDEV_END_DECLS diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 6466229e..594f61fc 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -32,56 +32,62 @@ macro(generate_crctbl proc path table rows poly trans) ) endmacro() +if(BFDEV_CRC_EXTEND) + set(CRC_ROWS 8) +else() + set(CRC_ROWS 1) +endif() + generate_crctbl( gen-crc8 crc7-table.h - bfdev_crc7_table 8 0x12 - "" + bfdev_crc7_table ${CRC_ROWS} + 0x12 "" ) generate_crctbl( gen-crc8 crc8-table.h - bfdev_crc8_table 8 0x07 - "" + bfdev_crc8_table ${CRC_ROWS} + 0x07 "" ) generate_crctbl( gen-crc16 crc16-table.h - bfdev_crc16_table 8 0xa001 - "bfdev_cpu_to_le16" + bfdev_crc16_table ${CRC_ROWS} + 0xa001 "bfdev_cpu_to_le16" ) generate_crctbl( gen-crc16 crc-ccitt-table.h - bfdev_crc_ccitt_table 8 0x8408 - "bfdev_cpu_to_le16" + bfdev_crc_ccitt_table ${CRC_ROWS} + 0x8408 "bfdev_cpu_to_le16" ) generate_crctbl( gen-crc16be crc-itut-table.h - bfdev_crc_itut_table 8 0x1021 - "bfdev_cpu_to_be16" + bfdev_crc_itut_table ${CRC_ROWS} + 0x1021 "bfdev_cpu_to_be16" ) generate_crctbl( gen-crc16be crc-t10dif-table.h - bfdev_crc_t10dif_table 8 0x8bb7 - "bfdev_cpu_to_be16" + bfdev_crc_t10dif_table ${CRC_ROWS} + 0x8bb7 "bfdev_cpu_to_be16" ) generate_crctbl( gen-crc32 crc32-table.h - bfdev_crc32_table 8 0xedb88320 - "bfdev_cpu_to_le32" + bfdev_crc32_table ${CRC_ROWS} + 0xedb88320 "bfdev_cpu_to_le32" ) generate_crctbl( gen-crc64be crc64-table.h - bfdev_crc64_table 8 0x42f0e1eba9ea3693 - "bfdev_cpu_to_be64" + bfdev_crc64_table ${CRC_ROWS} + 0x42f0e1eba9ea3693 "bfdev_cpu_to_be64" ) generate_crctbl( gen-crc64 crc-rocksoft-table.h - bfdev_crc_rocksoft_table 8 0x9a6c9329ac4bc9b5 - "bfdev_cpu_to_le64" + bfdev_crc_rocksoft_table ${CRC_ROWS} + 0x9a6c9329ac4bc9b5 "bfdev_cpu_to_le64" ) From 6541a014f3b768ea8a1edcd4631f539aeef4c2cd Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 8 Feb 2024 18:35:35 +0800 Subject: [PATCH 085/119] feat allocator: eliminate implementation UB Signed-off-by: John Sanpe --- src/allocator.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/allocator.c b/src/allocator.c index 6bd9c201..dee3bde8 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -45,6 +45,9 @@ bfdev_malloc(const bfdev_alloc_t *alloc, size_t size) const bfdev_alloc_ops_t *ops; void *pdata, *retval; + if (bfdev_unlikely(!size)) + return NULL; + ops = alloc_check(alloc); if (!ops || !ops->alloc) retval = generic_alloc(size); @@ -62,6 +65,9 @@ bfdev_zalloc(const bfdev_alloc_t *alloc, size_t size) const bfdev_alloc_ops_t *ops; void *pdata, *retval; + if (bfdev_unlikely(!size)) + return NULL; + ops = alloc_check(alloc); if (!ops || !ops->zalloc) retval = generic_zalloc(size); @@ -79,6 +85,14 @@ bfdev_realloc(const bfdev_alloc_t *alloc, const void *block, size_t resize) const bfdev_alloc_ops_t *ops; void *pdata, *retval; + if (!block) + return bfdev_malloc(alloc, resize); + + if (!resize) { + bfdev_free(alloc, block); + return NULL; + } + ops = alloc_check(alloc); if (!ops || !ops->realloc) retval = generic_realloc((void *)block, resize); @@ -96,6 +110,9 @@ bfdev_free(const bfdev_alloc_t *alloc, const void *block) const bfdev_alloc_ops_t *ops; void *pdata; + if (!block) + return; + ops = alloc_check(alloc); if (!ops || !ops->free) generic_free(block); From 16828277908486e80e2e64a40c14dd86ae4076bf Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 9 Feb 2024 22:58:01 +0800 Subject: [PATCH 086/119] feat dword: added initial functions Signed-off-by: John Sanpe --- include/bfdev/asm-generic/dword.h | 29 +++++++ include/bfdev/dword.h | 134 ++++++++++++++++++++++++++++++ include/bfdev/types.h | 10 +++ 3 files changed, 173 insertions(+) create mode 100644 include/bfdev/asm-generic/dword.h create mode 100644 include/bfdev/dword.h diff --git a/include/bfdev/asm-generic/dword.h b/include/bfdev/asm-generic/dword.h new file mode 100644 index 00000000..9e5e2f55 --- /dev/null +++ b/include/bfdev/asm-generic/dword.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef _BFDEV_ASM_GENERIC_DWORD_H_ +#define _BFDEV_ASM_GENERIC_DWORD_H_ + +#include +#include +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef BFDEV_DWORD_BITS +# define BFDEV_DWORD_BITS (BFDEV_BITS_PER_LONG >> 1) +# define BFDEV_DWORD_SIZE (1UL << BFDEV_DWORD_BITS) +#endif + +#define BFDEV_DWORD_LOWER(val) \ + ((bfdev_uw_t)(val) & (BFDEV_DWORD_SIZE - 1)) + +#define BFDEV_DWORD_HIGHER(val) \ + ((bfdev_uw_t)(val) >> BFDEV_DWORD_BITS) + +BFDEV_END_DECLS + +#endif /* _BFDEV_ASM_GENERIC_DWORD_H_ */ diff --git a/include/bfdev/dword.h b/include/bfdev/dword.h new file mode 100644 index 00000000..915a82d6 --- /dev/null +++ b/include/bfdev/dword.h @@ -0,0 +1,134 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_DWORD_H_ +#define _BFDEV_DWORD_H_ + +#include +#include +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef bfdev_dword_addd +# define bfdev_dword_addd(sh, sl, ah, al, bh, bl) do { \ + bfdev_uw_t __ah, __al, __bh, __bl; \ + bfdev_uw_t __x; \ + \ + __ah = (ah); \ + __al = (al); \ + __bh = (bh); \ + __bl = (bl); \ + \ + __x = __al + __bl; \ + (sh) = __ah + __bh + (__x < __al); \ + (sl) = __x; \ +} while (0) +#endif + +#ifndef bfdev_dword_subd +# define bfdev_dword_subd(sh, sl, ah, al, bh, bl) do { \ + bfdev_uw_t __ah, __al, __bh, __bl; \ + bfdev_uw_t __x; \ + \ + __ah = (ah); \ + __al = (al); \ + __bh = (bh); \ + __bl = (bl); \ + \ + __x = __al - __bl; \ + (sh) = __ah - __bh - (__x > __al); \ + (sl) = __x; \ +} while (0) +#endif + +#ifndef bfdev_dword_umul +# define bfdev_dword_umul(dh, dl, va, vb) do { \ + bfdev_uhw_t __ul, __vl, __uh, __vh; \ + bfdev_uw_t __x0, __x1, __x2, __x3; \ + bfdev_uw_t __va, __vb; \ + \ + __va = (va); \ + __vb = (vb); \ + \ + __ul = BFDEV_DWORD_LOWER(__va); \ + __vl = BFDEV_DWORD_LOWER(__vb); \ + __uh = BFDEV_DWORD_HIGHER(__va); \ + __vh = BFDEV_DWORD_HIGHER(__vb); \ + \ + __x0 = (bfdev_uw_t)__ul * __vl; \ + __x1 = (bfdev_uw_t)__ul * __vh; \ + __x2 = (bfdev_uw_t)__uh * __vl; \ + __x3 = (bfdev_uw_t)__uh * __vh; \ + \ + __x1 += BFDEV_DWORD_HIGHER(__x0); \ + __x1 += __x2; \ + if (__x1 < __x2) \ + __x3 += BFDEV_DWORD_SIZE; \ + \ + __x2 = BFDEV_DWORD_LOWER(__x1) << BFDEV_DWORD_BITS; \ + (dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \ + (dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \ +} while (0) +#endif + +#ifndef bfdev_dword_udiv +# define bfdev_dword_udiv(quot, rem, sh, sl, div) do { \ + bfdev_uw_t __d1, __d0, __q1, __q0; \ + bfdev_uw_t __r1, __r0, __m; \ + bfdev_uw_t __sh, __sl, __div; \ + \ + __sh = (sh); \ + __sl = (sl); \ + __div = (div); \ + \ + __d1 = BFDEV_DWORD_HIGHER(__div); \ + __d0 = BFDEV_DWORD_LOWER(__div); \ + \ + __r1 = __sh % __d1; \ + __q1 = __sh / __d1; \ + __m = (bfdev_uw_t)__q1 * __d0; \ + __r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__sl); \ + \ + if (__r1 < __m) { \ + __q1--; \ + __r1 += __div; \ + \ + if (__r1 >= __div) { \ + if (__r1 < __m) { \ + __q1--; \ + __r1 += (__div); \ + } \ + } \ + } \ + \ + __r1 -= __m; \ + __r0 = __r1 % __d1; \ + __q0 = __r1 / __d1; \ + __m = (bfdev_uw_t)__q0 * __d0; \ + __r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__sl); \ + \ + if (__r0 < __m) { \ + __q0--; \ + __r0 += (__div); \ + \ + if (__r0 >= __div) { \ + if (__r0 < __m) { \ + __q0--; \ + __r0 += __div; \ + } \ + } \ + } \ + \ + __r0 -= __m; \ + (quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \ + (rem) = __r0; \ +} while (0) +#endif + +BFDEV_END_DECLS + +#endif /* _BFDEV_DWORD_H_ */ diff --git a/include/bfdev/types.h b/include/bfdev/types.h index 3e44e6fc..be0edc7c 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -25,6 +25,16 @@ typedef uint16_t __bfdev_bitwise bfdev_be16; typedef uint32_t __bfdev_bitwise bfdev_be32; typedef uint64_t __bfdev_bitwise bfdev_be64; +typedef unsigned long bfdev_uw_t; +typedef unsigned int bfdev_uhw_t; + +typedef int bfdev_sw_t __attribute__((mode(SI))); +typedef int bfdev_dw_t __attribute__((mode(DI))); +typedef int bfdev_qw_t __attribute__((mode(QI))); +typedef unsigned bfdev_usw_t __attribute__((mode(SI))); +typedef unsigned bfdev_udw_t __attribute__((mode(DI))); +typedef unsigned bfdev_uqw_t __attribute__((mode(QI))); + typedef int bfdev_state_t; typedef intptr_t bfdev_atomic_t; From d1e4e873ab03e95d4607cf8005d7232285a30bf7 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 9 Feb 2024 23:14:25 +0800 Subject: [PATCH 087/119] fixup array: added const modifier for some functions Signed-off-by: John Sanpe --- include/bfdev/array.h | 10 +++++----- src/array.c | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 297cfe83..6e21c3dd 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -50,25 +50,25 @@ bfdev_array_reset(bfdev_array_t *array) } static inline unsigned long -bfdev_array_index(bfdev_array_t *array) +bfdev_array_index(const bfdev_array_t *array) { return array->index; } static inline size_t -bfdev_array_size(bfdev_array_t *array) +bfdev_array_size(const bfdev_array_t *array) { return array->cells * array->index; } static inline uintptr_t -bfdev_array_offset(bfdev_array_t *array, unsigned long index) +bfdev_array_offset(const bfdev_array_t *array, unsigned long index) { return array->cells * index; } static inline void * -bfdev_array_data(bfdev_array_t *array, unsigned long index) +bfdev_array_data(const bfdev_array_t *array, unsigned long index) { if (bfdev_unlikely(index >= array->index)) return NULL; @@ -82,7 +82,7 @@ extern void * bfdev_array_pop(bfdev_array_t *array, unsigned long num); extern void * -bfdev_array_peek(bfdev_array_t *array, unsigned long num); +bfdev_array_peek(const bfdev_array_t *array, unsigned long num); extern int bfdev_array_reserve(bfdev_array_t *array, unsigned long num); diff --git a/src/array.c b/src/array.c index 6f8abd53..939de1de 100644 --- a/src/array.c +++ b/src/array.c @@ -17,7 +17,7 @@ array_resize(bfdev_array_t *array, unsigned long count) size_t size; void *data; - nalloc = bfdev_max(count << 1, BFDEV_ARRAY_MSIZE); + nalloc = bfdev_max(count, BFDEV_ARRAY_MSIZE); size = nalloc * array->cells; data = bfdev_realloc(alloc, array->data, size); @@ -33,13 +33,15 @@ array_resize(bfdev_array_t *array, unsigned long count) static inline int array_apply(bfdev_array_t *array, unsigned long count) { - if (count > array->capacity) - return array_resize(array, count); - return -BFDEV_ENOERR; + if (count <= array->capacity) + return -BFDEV_ENOERR; + + return array_resize(array, count << 1); } static inline void * -array_peek(bfdev_array_t *array, unsigned long num, unsigned long *indexp) +array_peek(const bfdev_array_t *array, unsigned long num, + unsigned long *indexp) { unsigned long index; uintptr_t offset; @@ -86,7 +88,7 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num) } export void * -bfdev_array_peek(bfdev_array_t *array, unsigned long num) +bfdev_array_peek(const bfdev_array_t *array, unsigned long num) { return array_peek(array, num, NULL); } From fbab737373d310fdcf5f2ea5200f76e30bd041c3 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 10 Feb 2024 20:27:55 +0800 Subject: [PATCH 088/119] feat array: added resize function Signed-off-by: John Sanpe --- include/bfdev/array.h | 3 +++ src/array.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 6e21c3dd..146dd80b 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -84,6 +84,9 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num); extern void * bfdev_array_peek(const bfdev_array_t *array, unsigned long num); +extern int +bfdev_array_resize(bfdev_array_t *array, unsigned long num); + extern int bfdev_array_reserve(bfdev_array_t *array, unsigned long num); diff --git a/src/array.c b/src/array.c index 939de1de..b5f41561 100644 --- a/src/array.c +++ b/src/array.c @@ -93,6 +93,20 @@ bfdev_array_peek(const bfdev_array_t *array, unsigned long num) return array_peek(array, num, NULL); } +export int +bfdev_array_resize(bfdev_array_t *array, unsigned long num) +{ + int retval; + + retval = array_apply(array, num); + if (bfdev_unlikely(retval)) + return retval; + + array->index = num; + + return -BFDEV_ENOERR; +} + export int bfdev_array_reserve(bfdev_array_t *array, unsigned long num) { From 87e2ed38d44c235432f849cd55aa54e2aadace6c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sat, 10 Feb 2024 20:35:43 +0800 Subject: [PATCH 089/119] feat mpi: added initial functions Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 73 +++++ src/build.cmake | 1 + src/mpi.c | 760 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 834 insertions(+) create mode 100644 include/bfdev/mpi.h create mode 100644 src/mpi.c diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h new file mode 100644 index 00000000..2c4636b4 --- /dev/null +++ b/include/bfdev/mpi.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_MPI_H_ +#define _BFDEV_MPI_H_ + +#include +#include +#include +#include +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef BFDEV_MPI_TYPE +# define BFDEV_MPI_TYPE unsigned long +#endif + +typedef struct bfdev_mpi bfdev_mpi_t; + +struct bfdev_mpi { + bfdev_array_t value; + bool plus; +}; + +extern int +bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + +extern int +bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb); + +extern int +bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb); + +extern int +bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb); + +extern int +bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); + +extern int +bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi); + +extern int +bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi); + +extern int +bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi); + +extern int +bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); + +extern int +bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src); + +extern bfdev_mpi_t * +bfdev_mpi_create(const bfdev_alloc_t *alloc); + +extern void +bfdev_mpi_destory(bfdev_mpi_t *var); + +BFDEV_END_DECLS + +#endif /* _BFDEV_MPI_H_ */ diff --git a/src/build.cmake b/src/build.cmake index 1d34c4c2..516f0745 100644 --- a/src/build.cmake +++ b/src/build.cmake @@ -30,6 +30,7 @@ set(BFDEV_SOURCE ${CMAKE_CURRENT_LIST_DIR}/log.c ${CMAKE_CURRENT_LIST_DIR}/matrix.c ${CMAKE_CURRENT_LIST_DIR}/minpool.c + ${CMAKE_CURRENT_LIST_DIR}/mpi.c ${CMAKE_CURRENT_LIST_DIR}/notifier.c ${CMAKE_CURRENT_LIST_DIR}/popcount.c ${CMAKE_CURRENT_LIST_DIR}/prandom.c diff --git a/src/mpi.c b/src/mpi.c new file mode 100644 index 00000000..1e5fe1b6 --- /dev/null +++ b/src/mpi.c @@ -0,0 +1,760 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#include +#include +#include +#include + +#define mpi_len(mpi) bfdev_array_index(&(mpi)->value) +#define mpi_pos(mpi) bfdev_array_data(&(mpi)->value, 0) +#define mpi_neg(mpi) bfdev_array_peek(&(mpi)->value, 1) + +static inline void +mpa_zero(BFDEV_MPI_TYPE *dest, unsigned long length) +{ + memset(dest, 0, length * sizeof(*dest)); +} + +static inline void +mpa_copy(BFDEV_MPI_TYPE *dest, BFDEV_MPI_TYPE *src, unsigned long length) +{ + memcpy(dest, src, length * sizeof(*dest)); +} + +static inline bool +mpa_addi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length, bool carry) +{ + *ptrs = *ptra + vi; + carry = *ptrs++ < *ptra++; + + while (--length) { + *ptrs = *ptra + carry; + carry = *ptrs++ < *ptra++; + } + + return carry; +} + +static inline bool +mpa_add(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, + unsigned long cnta, unsigned long cntb, bool carry) +{ + while (cnta && cntb) { + *ptrs = *ptra + *ptrb++ + carry; + carry = *ptrs++ < *ptra++; + + cnta--; + cntb--; + } + + if (!cnta) { + cnta = cntb; + ptra = ptrb; + } + + while (cnta--) { + *ptrs = *ptra + carry; + carry = *ptrs++ < *ptra++; + } + + return carry; +} + +static inline bool +mpa_subi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length, bool borrow) +{ + *ptrs = *ptra - vi; + borrow = *ptrs++ > *ptra++; + + while (--length) { + *ptrs = *ptra + borrow; + borrow = *ptrs++ > *ptra++; + } + + return borrow; +} + +static inline bool +mpa_sub(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, + unsigned long cnta, unsigned long cntb, bool borrow) +{ + while (cnta && cntb) { + *ptrs = *ptra - *ptrb++ - borrow; + borrow = *ptrs++ > *ptra++; + + cnta--; + cntb--; + } + + if (!cnta) { + cnta = cntb; + ptra = ptrb; + } + + while (cnta--) { + *ptrs = *ptra - borrow; + borrow = *ptrs++ > *ptra++; + } + + return borrow; +} + +static inline BFDEV_MPI_TYPE +mpa_muli(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length, BFDEV_MPI_TYPE carry) +{ + BFDEV_MPI_TYPE vhigh, vlow; + + while (length--) { + bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + vlow += carry; + carry = (vlow < carry) + vhigh; + *ptrs++ = vlow; + } + + return carry; +} + +static inline BFDEV_MPI_TYPE +mpa_maci(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length, BFDEV_MPI_TYPE carry) +{ + BFDEV_MPI_TYPE vhigh, vlow; + + while (length--) { + bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + vlow += carry; + carry = (vlow < carry) + vhigh; + + vlow += *ptrs; + carry += vlow < *ptrs; + *ptrs++ = vlow; + } + + return carry; +} + +static inline void +mpa_mul(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, + unsigned long cnta, unsigned long cntb) +{ + BFDEV_MPI_TYPE value, carry; + unsigned long index; + + /** + * Argument constraints: + * 1. @cnta >= @cntb. + * 2. @ptrs != @ptra and @ptrs != @ptrb, i.e. the destination + * must be distinct from the multiplier and the multiplicand. + */ + + value = *ptrb; + if (value > 1) + carry = mpa_muli(ptrs, ptra, value, cnta, 0); + else { + if (value) + mpa_copy(ptrs, ptra, cnta); + else + mpa_zero(ptrs, cnta); + + carry = 0; + } + + ptrs[cnta] = carry; + ptrs++; + + for (index = 1; index < cntb; ++index) { + value = ptrb[index]; + if (value > 1) + carry = mpa_maci(ptrs, ptra, value, cnta, 0); + else { + carry = 0; + if (value) + carry = mpa_add(ptrs, ptrs, ptra, cnta, cnta, 0); + } + + ptrs[cnta] = carry; + ptrs++; + } +} + +static inline void +mpi_relocation(bfdev_mpi_t *var) +{ + BFDEV_MPI_TYPE *value; + + while (mpi_len(var) > 1) { + value = bfdev_array_peek(&var->value, 1); + if (*value) + break; + + bfdev_array_pop(&var->value, 1); + } +} + +static inline int +mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *value; + + bfdev_array_reset(&dest->value); + value = bfdev_array_push(&dest->value, 1); + if (bfdev_unlikely(!value)) + return -BFDEV_ENOMEM; + + *value = vi; + return -BFDEV_ENOERR; +} + +static inline int +mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi, bool unsign) +{ + unsigned long length; + BFDEV_MPI_TYPE *value; + + if (!unsign && !va->plus) + return -1; + + length = mpi_len(va); + if (length > 1) { + if (unsign || va->plus) + return 1; + else + return -1; + } + + value = mpi_pos(va); + if (*value != vi) { + if (*value > vi) { + if (unsign || va->plus) + return 1; + else + return -1; + } else { + if (unsign || va->plus) + return -1; + else + return 1; + } + } + + return 0; +} + +static inline int +mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb, bool unsign) +{ + BFDEV_MPI_TYPE *ptra, *ptrb; + unsigned long length, lena, lenb; + + if (!unsign && va->plus != vb->plus) { + if (va->plus) + return 1; + else + return -1; + } + + lena = mpi_len(va); + lenb = mpi_len(vb); + + if (lena != lenb) { + if (lena > lenb) { + if (unsign || va->plus) + return 1; + else + return -1; + } else { + if (unsign || va->plus) + return -1; + else + return 1; + } + } + + ptra = mpi_neg(va); + ptrb = mpi_neg(vb); + + for (length = lena; length; --length) { + if (*ptra != *ptrb) + break; + ptra--; + ptrb--; + } + + if (length) { + if (*ptra > *ptrb) { + if (unsign || va->plus) + return 1; + else + return -1; + } else { + if (unsign || va->plus) + return -1; + else + return 1; + } + } + + return 0; +} + +static inline int +mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + bool carry; + int retval; + + length = mpi_len(va); + + retval = bfdev_array_resize(&dest->value, length + 1); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(va); + + carry = mpa_addi(ptrs, ptra, vi, length, false); + *(ptrs + length) = carry; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long length, cnta, cntb; + bool carry; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = bfdev_array_resize(&dest->value, length + 1); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(va); + ptrb = mpi_pos(vb); + + carry = mpa_add(ptrs, ptra, ptrb, cnta, cntb, false); + *(ptrs + length) = carry; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + bool borrow; + int retval; + + length = mpi_len(va); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(va); + + borrow = mpa_subi(ptrs, ptra, vi, length, false); + if (bfdev_unlikely(borrow)) + return -BFDEV_EOVERFLOW; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long length, cnta, cntb; + bool borrow; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(va); + ptrb = mpi_pos(vb); + + borrow = mpa_sub(ptrs, ptra, ptrb, cnta, cntb, false); + if (bfdev_unlikely(borrow)) + return -BFDEV_EOVERFLOW; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + BFDEV_MPI_TYPE carry; + unsigned long length; + int retval; + + length = mpi_len(va); + + retval = bfdev_array_resize(&dest->value, length + 1); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(va); + + carry = mpa_muli(ptrs, ptra, vi, length, 0); + *(ptrs + length) = carry; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long length, cnta, cntb; + bfdev_array_t buffer; + int retval; + bool nval; + + nval = false; + if (dest == va || dest == vb) + buffer = dest->value; + else { + bfdev_array_init(&buffer, dest->value.alloc, sizeof(*ptrs)); + nval = true; + } + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = cnta + cntb; + + retval = bfdev_array_resize(&buffer, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = bfdev_array_data(&buffer, 0); + ptra = mpi_pos(va); + ptrb = mpi_pos(vb); + + mpa_mul(ptrs, ptra, ptrb, cnta, cntb); + if (nval) { + bfdev_array_release(&dest->value); + dest->value = buffer; + } + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + return mpi_cmp(va, vb, false); +} + +export int +bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + return mpi_cmpi(va, vi, false); +} + +export int +bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb) +{ + int diff, retval; + + if (va->plus == vb->plus) { + retval = mpi_add(dest, va, vb); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = va->plus; + return -BFDEV_ENOERR; + } + + diff = mpi_cmp(va, vb, true); + switch (diff) { + case 0: default: + retval = mpi_set(dest, 0); + if (bfdev_unlikely(retval)) + break; + + dest->plus = true; + break; + + case 1: + retval = mpi_sub(dest, va, vb); + if (bfdev_unlikely(retval)) + break; + + dest->plus = va->plus; + break; + + case -1: + retval = mpi_sub(dest, vb, va); + if (bfdev_unlikely(retval)) + break; + + dest->plus = vb->plus; + break; + } + + return retval; +} + +export int +bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *value; + int diff, retval; + + if (va->plus) { + retval = mpi_addi(dest, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = true; + return -BFDEV_ENOERR; + } + + diff = bfdev_mpi_cmpi(va, vi); + switch (diff) { + case 0: default: + retval = mpi_set(dest, 0); + if (bfdev_unlikely(retval)) + break; + + dest->plus = true; + break; + + case -1: + value = mpi_pos(va); + retval = mpi_set(dest, vi - *value); + dest->plus = true; + break; + + case 1: + retval = mpi_subi(dest, va, vi); + if (bfdev_unlikely(retval)) + break; + + dest->plus = false; + break; + } + + return retval; +} + +export int +bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb) +{ + int diff, retval; + + if (va->plus != vb->plus) { + retval = mpi_add(dest, va, vb); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = va->plus; + return -BFDEV_ENOERR; + } + + diff = mpi_cmp(va, vb, true); + switch (diff) { + case 0: default: + retval = mpi_set(dest, 0); + if (bfdev_unlikely(retval)) + break; + + dest->plus = true; + break; + + case 1: + retval = mpi_sub(dest, va, vb); + if (bfdev_unlikely(retval)) + break; + + dest->plus = va->plus; + break; + + case -1: + retval = mpi_sub(dest, vb, va); + if (bfdev_unlikely(retval)) + break; + + dest->plus = !vb->plus; + break; + } + + return retval; +} + +export int +bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *value; + int diff, retval; + + if (!va->plus) { + retval = mpi_addi(dest, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = false; + return -BFDEV_ENOERR; + } + + diff = mpi_cmpi(va, vi, true); + switch (diff) { + case 0: default: + retval = mpi_set(dest, 0); + if (bfdev_unlikely(retval)) + break; + + dest->plus = true; + break; + + case -1: + value = mpi_pos(va); + retval = mpi_set(dest, vi - *value); + dest->plus = false; + break; + + case 1: + retval = mpi_subi(dest, va, vi); + if (bfdev_unlikely(retval)) + break; + + dest->plus = va->plus; + break; + } + + return retval; +} + +export int +bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + const bfdev_mpi_t *vb) +{ + unsigned long cnta, cntb; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + + if (cnta > cntb) { + bfdev_swap(vb ,va); + bfdev_swap(cntb, cnta); + } + + retval = mpi_mul(dest, va, vb); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = !(va->plus ^ vb->plus); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, + BFDEV_MPI_TYPE vi) +{ + int retval; + + retval = mpi_muli(dest, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = va->plus; + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) +{ + return mpi_set(dest, val); +} + +export int +bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + int retval; + + length = mpi_len(src); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_pos(dest); + ptra = mpi_pos(src); + + mpa_copy(ptrs, ptra, length); + dest->plus = src->plus; + + return -BFDEV_ENOERR; +} + +export bfdev_mpi_t * +bfdev_mpi_create(const bfdev_alloc_t *alloc) +{ + bfdev_mpi_t *result; + BFDEV_MPI_TYPE *array; + + result = bfdev_malloc(alloc, sizeof(*result)); + if (bfdev_unlikely(!result)) + return NULL; + + bfdev_array_init(&result->value, alloc, sizeof(*array)); + array = bfdev_array_push(&result->value, 1); + if (bfdev_unlikely(!array)) + return NULL; + + result->plus = true; + *array = 0; + + return result; +} + +export void +bfdev_mpi_destory(bfdev_mpi_t *var) +{ + const bfdev_alloc_t *alloc; + + alloc = var->value.alloc; + bfdev_array_release(&var->value); + + bfdev_free(alloc, var); +} From a8ae9d8e47e083a62813d60a70590d8f7205915c Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 11 Feb 2024 07:43:02 +0800 Subject: [PATCH 090/119] feat mpi: added divi function Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 32 ++-- src/mpi.c | 407 +++++++++++++++++++++++++++++++++----------- 2 files changed, 321 insertions(+), 118 deletions(-) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index 2c4636b4..3114b0a3 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -16,7 +16,7 @@ BFDEV_BEGIN_DECLS #ifndef BFDEV_MPI_TYPE -# define BFDEV_MPI_TYPE unsigned long +# define BFDEV_MPI_TYPE bfdev_uw_t #endif typedef struct bfdev_mpi bfdev_mpi_t; @@ -30,31 +30,35 @@ extern int bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb); extern int -bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb); +bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int -bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb); +bfdev_mpi_add(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); extern int -bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb); +bfdev_mpi_sub(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); extern int -bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); +bfdev_mpi_mul(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + +extern int +bfdev_mpi_addi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int -bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi); +bfdev_mpi_subi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int -bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi); +bfdev_mpi_muli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int -bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi); +bfdev_mpi_divi(bfdev_mpi_t *dest, BFDEV_MPI_TYPE *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); diff --git a/src/mpi.c b/src/mpi.c index 1e5fe1b6..a9ff65a6 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -9,8 +9,14 @@ #include #define mpi_len(mpi) bfdev_array_index(&(mpi)->value) -#define mpi_pos(mpi) bfdev_array_data(&(mpi)->value, 0) -#define mpi_neg(mpi) bfdev_array_peek(&(mpi)->value, 1) +#define mpi_val(mpi) bfdev_array_data(&(mpi)->value, 0) + +/** + * Multi Precision Array Computing. + * + * The following functions are used to + * calculating raw data within an array. + */ static inline void mpa_zero(BFDEV_MPI_TYPE *dest, unsigned long length) @@ -19,13 +25,62 @@ mpa_zero(BFDEV_MPI_TYPE *dest, unsigned long length) } static inline void -mpa_copy(BFDEV_MPI_TYPE *dest, BFDEV_MPI_TYPE *src, unsigned long length) +mpa_copy(BFDEV_MPI_TYPE *dest, const BFDEV_MPI_TYPE *src, + unsigned long length) { memcpy(dest, src, length * sizeof(*dest)); } +static inline int +mpa_cmpi(const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length) +{ + if (length > 1) + return 1; + + if (*ptra != vi) { + if (*ptra > vi) + return 1; + return -1; + } + + return 0; +} + +static inline int +mpa_cmp(const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, + unsigned long lena, unsigned long lenb) +{ + unsigned long length; + + if (lena != lenb) { + if (lena > lenb) + return 1; + return -1; + } + + ptra += lena - 1; + ptrb += lenb - 1; + + for (length = lena; length; --length) { + if (*ptra != *ptrb) + break; + ptra--; + ptrb--; + } + + if (length) { + if (*ptra > *ptrb) + return 1; + return -1; + } + + return 0; +} + static inline bool -mpa_addi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, +mpa_addi(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, bool carry) { *ptrs = *ptra + vi; @@ -40,7 +95,8 @@ mpa_addi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, } static inline bool -mpa_add(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, +mpa_add(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, unsigned long cnta, unsigned long cntb, bool carry) { while (cnta && cntb) { @@ -65,7 +121,8 @@ mpa_add(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, } static inline bool -mpa_subi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, +mpa_subi(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, bool borrow) { *ptrs = *ptra - vi; @@ -80,7 +137,8 @@ mpa_subi(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, } static inline bool -mpa_sub(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, +mpa_sub(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, unsigned long cnta, unsigned long cntb, bool borrow) { while (cnta && cntb) { @@ -105,7 +163,8 @@ mpa_sub(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, } static inline BFDEV_MPI_TYPE -mpa_muli(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, +mpa_muli(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, BFDEV_MPI_TYPE carry) { BFDEV_MPI_TYPE vhigh, vlow; @@ -121,7 +180,8 @@ mpa_muli(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, } static inline BFDEV_MPI_TYPE -mpa_maci(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, +mpa_maci(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, BFDEV_MPI_TYPE carry) { BFDEV_MPI_TYPE vhigh, vlow; @@ -131,7 +191,7 @@ mpa_maci(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, vlow += carry; carry = (vlow < carry) + vhigh; - vlow += *ptrs; + vlow = *ptrs + vlow; carry += vlow < *ptrs; *ptrs++ = vlow; } @@ -139,8 +199,29 @@ mpa_maci(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, return carry; } +static inline BFDEV_MPI_TYPE +mpa_msui(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length, BFDEV_MPI_TYPE carry) +{ + BFDEV_MPI_TYPE vhigh, vlow; + + while (length--) { + bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + vlow += carry; + carry = (vlow < carry) + vhigh; + + vlow = *ptrs - vlow; + carry += vlow > *ptrs; /* WTF? */ + *ptrs++ = vlow; + } + + return carry; +} + static inline void -mpa_mul(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, +mpa_mul(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, unsigned long cnta, unsigned long cntb) { BFDEV_MPI_TYPE value, carry; @@ -183,6 +264,69 @@ mpa_mul(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE *ptrb, } } +static inline BFDEV_MPI_TYPE +mpa_divmodi(BFDEV_MPI_TYPE *ptrs, + const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length) +{ + BFDEV_MPI_TYPE value, rem; + unsigned long index; + + index = length - 1; + ptrs += index; + ptra += index; + rem = *ptra; + + if (rem >= vi) + rem = 0; + else { + *ptrs-- = 0; + ptra--; + length--; + } + + while (length--) { + value = *ptra--; + bfdev_dword_udiv(*ptrs--, rem, rem, value, vi); + } + + return rem; +} + +static inline BFDEV_MPI_TYPE +mpa_modi(const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, + unsigned long length) +{ + BFDEV_MPI_TYPE dummy __bfdev_maybe_unused; + BFDEV_MPI_TYPE value, rem; + unsigned long index; + + index = length - 1; + ptra += index; + rem = *ptra; + + if (rem >= vi) + rem = 0; + else { + ptra--; + length--; + } + + while (length--) { + value = *ptra--; + bfdev_dword_udiv(dummy, rem, rem, value, vi); + } + + return rem; +} + +/** + * Multi Precision Integers (Unsigned). + * + * The following functions are used to + * calculating unsigned mpi. + */ + static inline void mpi_relocation(bfdev_mpi_t *var) { @@ -216,95 +360,68 @@ mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi, bool unsign) { unsigned long length; BFDEV_MPI_TYPE *value; + int diff; if (!unsign && !va->plus) return -1; length = mpi_len(va); - if (length > 1) { + value = mpi_val(va); + + diff = mpa_cmpi(value, vi, length); + if (!diff) + return 0; + + if (diff > 0) { if (unsign || va->plus) return 1; - else - return -1; - } - - value = mpi_pos(va); - if (*value != vi) { - if (*value > vi) { - if (unsign || va->plus) - return 1; - else - return -1; - } else { - if (unsign || va->plus) - return -1; - else - return 1; - } + return -1; } - return 0; + /* diff < 0 */ + if (unsign || va->plus) + return -1; + return 1; } static inline int mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb, bool unsign) { BFDEV_MPI_TYPE *ptra, *ptrb; - unsigned long length, lena, lenb; + unsigned long lena, lenb; + int diff; if (!unsign && va->plus != vb->plus) { if (va->plus) return 1; - else - return -1; + return -1; } lena = mpi_len(va); lenb = mpi_len(vb); - if (lena != lenb) { - if (lena > lenb) { - if (unsign || va->plus) - return 1; - else - return -1; - } else { - if (unsign || va->plus) - return -1; - else - return 1; - } - } - - ptra = mpi_neg(va); - ptrb = mpi_neg(vb); + ptra = mpi_val(va); + ptrb = mpi_val(vb); - for (length = lena; length; --length) { - if (*ptra != *ptrb) - break; - ptra--; - ptrb--; - } + diff = mpa_cmp(ptra, ptrb, lena, lenb); + if (!diff) + return 0; - if (length) { - if (*ptra > *ptrb) { - if (unsign || va->plus) - return 1; - else - return -1; - } else { - if (unsign || va->plus) - return -1; - else - return 1; - } + if (diff > 0) { + if (unsign || va->plus) + return 1; + return -1; } - return 0; + /* diff < 0 */ + if (unsign || va->plus) + return -1; + return 1; } static inline int -mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +mpi_addi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *ptrs, *ptra; unsigned long length; @@ -317,8 +434,8 @@ mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(va); + ptrs = mpi_val(dest); + ptra = mpi_val(va); carry = mpa_addi(ptrs, ptra, vi, length, false); *(ptrs + length) = carry; @@ -328,7 +445,8 @@ mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) } static inline int -mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +mpi_add(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; unsigned long length, cnta, cntb; @@ -343,9 +461,9 @@ mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(va); - ptrb = mpi_pos(vb); + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); carry = mpa_add(ptrs, ptra, ptrb, cnta, cntb, false); *(ptrs + length) = carry; @@ -355,7 +473,8 @@ mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) } static inline int -mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +mpi_subi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *ptrs, *ptra; unsigned long length; @@ -368,8 +487,8 @@ mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(va); + ptrs = mpi_val(dest); + ptra = mpi_val(va); borrow = mpa_subi(ptrs, ptra, vi, length, false); if (bfdev_unlikely(borrow)) @@ -380,7 +499,8 @@ mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) } static inline int -mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +mpi_sub(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; unsigned long length, cnta, cntb; @@ -395,9 +515,9 @@ mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(va); - ptrb = mpi_pos(vb); + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); borrow = mpa_sub(ptrs, ptra, ptrb, cnta, cntb, false); if (bfdev_unlikely(borrow)) @@ -408,7 +528,8 @@ mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) } static inline int -mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +mpi_muli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *ptrs, *ptra; BFDEV_MPI_TYPE carry; @@ -421,8 +542,8 @@ mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(va); + ptrs = mpi_val(dest); + ptra = mpi_val(va); carry = mpa_muli(ptrs, ptra, vi, length, 0); *(ptrs + length) = carry; @@ -432,7 +553,33 @@ mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) } static inline int -mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +mpi_maci(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + BFDEV_MPI_TYPE carry; + unsigned long length; + int retval; + + length = mpi_len(va); + + retval = bfdev_array_resize(&dest->value, length + 1); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + + carry = mpa_maci(ptrs, ptra, vi, length, 0); + *(ptrs + length) = carry; + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_mul(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; unsigned long length, cnta, cntb; @@ -457,8 +604,8 @@ mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) return retval; ptrs = bfdev_array_data(&buffer, 0); - ptra = mpi_pos(va); - ptrb = mpi_pos(vb); + ptra = mpi_val(va); + ptrb = mpi_val(vb); mpa_mul(ptrs, ptra, ptrb, cnta, cntb); if (nval) { @@ -470,6 +617,42 @@ mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) return -BFDEV_ENOERR; } +static inline int +mpi_divi(bfdev_mpi_t *quot, BFDEV_MPI_TYPE *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, value; + unsigned long length; + int retval; + + /* divide by zero */ + if (bfdev_unlikely(!vi)) + return -BFDEV_EOVERFLOW; + + length = mpi_len(va); + + if (quot) { + retval = bfdev_array_resize("->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(quot); + } + + ptra = mpi_val(va); + + if (!quot) + value = mpa_modi(ptra, vi, length); + else + value = mpa_divmodi(ptrs, ptra, vi, length); + + if (rem) + *rem = value; + mpi_relocation(quot); + + return -BFDEV_ENOERR; +} + export int bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { @@ -483,8 +666,8 @@ bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) } export int -bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb) +bfdev_mpi_add(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { int diff, retval; @@ -528,8 +711,8 @@ bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, } export int -bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi) +bfdev_mpi_addi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *value; int diff, retval; @@ -554,7 +737,7 @@ bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, break; case -1: - value = mpi_pos(va); + value = mpi_val(va); retval = mpi_set(dest, vi - *value); dest->plus = true; break; @@ -572,8 +755,8 @@ bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, } export int -bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb) +bfdev_mpi_sub(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { int diff, retval; @@ -617,8 +800,8 @@ bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, } export int -bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi) +bfdev_mpi_subi(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *value; int diff, retval; @@ -643,7 +826,7 @@ bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, break; case -1: - value = mpi_pos(va); + value = mpi_val(va); retval = mpi_set(dest, vi - *value); dest->plus = false; break; @@ -661,8 +844,8 @@ bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, } export int -bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - const bfdev_mpi_t *vb) +bfdev_mpi_mul(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { unsigned long cnta, cntb; int retval; @@ -685,8 +868,8 @@ bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, } export int -bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, - BFDEV_MPI_TYPE vi) +bfdev_mpi_muli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { int retval; @@ -699,6 +882,22 @@ bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, return -BFDEV_ENOERR; } +export int +bfdev_mpi_divi(bfdev_mpi_t *dest, BFDEV_MPI_TYPE *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + int retval; + + retval = mpi_divi(dest, rem, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + if (dest) + dest->plus = va->plus; + + return -BFDEV_ENOERR; +} + export int bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) { @@ -718,8 +917,8 @@ bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_pos(dest); - ptra = mpi_pos(src); + ptrs = mpi_val(dest); + ptra = mpi_val(src); mpa_copy(ptrs, ptra, length); dest->plus = src->plus; From f8ea84d60387d98f5a25c611eebd9046d3527243 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 11 Feb 2024 23:28:18 +0800 Subject: [PATCH 091/119] feat mpi: added div function Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 6 +- src/mpi.c | 461 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 375 insertions(+), 92 deletions(-) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index 3114b0a3..f4c7bd8d 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -44,6 +44,10 @@ extern int bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); +extern int +bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + extern int bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); @@ -57,7 +61,7 @@ bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int -bfdev_mpi_divi(bfdev_mpi_t *dest, BFDEV_MPI_TYPE *rem, +bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int diff --git a/src/mpi.c b/src/mpi.c index a9ff65a6..02cb34f6 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -212,7 +212,7 @@ mpa_msui(BFDEV_MPI_TYPE *ptrs, carry = (vlow < carry) + vhigh; vlow = *ptrs - vlow; - carry += vlow > *ptrs; /* WTF? */ + carry += vlow > *ptrs; *ptrs++ = vlow; } @@ -320,8 +320,83 @@ mpa_modi(const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, return rem; } +static inline bool +mpa_divrem(BFDEV_MPI_TYPE *ptrs, + BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, + unsigned long cnta, unsigned long cntb) +{ + BFDEV_MPI_TYPE dhigh, dlow, value; + unsigned long index; + bool limb; + + /** + * Argument constraints: + * 0. @cnta >= @cntb. + * 1. The most significant bit of the divisor must be set. + * 2. @ptrs must either not overlap with the input operands at all, or + * @ptrs + @cntb >= @ptra must hold true. (This means that it's + * possible to put the quotient in the high part of NUM, right after the + * remainder in NUM. + */ + + index = cnta - cntb; + ptra += index; + + dlow = ptra[cntb - 2]; + dhigh = ptra[cntb - 1]; + value = ptrb[cntb - 1]; + + limb = false; + if (value >= dhigh) { + if (value > dhigh || mpa_cmp(ptra, ptrb, cntb - 1, cntb - 1) >= 0) { + mpa_sub(ptra, ptra, ptrb, cntb, cntb, false); + value = ptra[cntb - 1]; + limb = true; + } + } + + while (index--) { + BFDEV_MPI_TYPE quot, carry; + BFDEV_MPI_TYPE v1, v2; + + ptra--; + v2 = ptra[cntb]; + + if (value == dhigh) + quot = ~(BFDEV_MPI_TYPE)0UL; + else { + BFDEV_MPI_TYPE rem; + + bfdev_dword_udiv(quot, rem, value, ptra[cntb - 1], dhigh); + bfdev_dword_umul(v1, value, dlow, quot); + + while (v1 > rem || (v1 == rem && value > ptra[cntb - 2])) { + quot--; + rem += dhigh; + + if (rem < dhigh) + break; + + v1 -= value < dlow; + value -= dlow; + } + } + + carry = mpa_msui(ptra, ptrb, quot, cntb, 0); + if (v2 != carry) { + mpa_add(ptra, ptra, ptrb, cntb, cntb, 0); + quot--; + } + + ptrs[index] = quot; + value = ptra[cntb - 1]; + } + + return limb; +} + /** - * Multi Precision Integers (Unsigned). + * Multi Precision Integers (unsigned). * * The following functions are used to * calculating unsigned mpi. @@ -352,50 +427,27 @@ mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE vi) return -BFDEV_ENOMEM; *value = vi; + return -BFDEV_ENOERR; } static inline int -mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi, bool unsign) +mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { - unsigned long length; BFDEV_MPI_TYPE *value; - int diff; - - if (!unsign && !va->plus) - return -1; + unsigned long length; length = mpi_len(va); value = mpi_val(va); - diff = mpa_cmpi(value, vi, length); - if (!diff) - return 0; - - if (diff > 0) { - if (unsign || va->plus) - return 1; - return -1; - } - - /* diff < 0 */ - if (unsign || va->plus) - return -1; - return 1; + return mpa_cmpi(value, vi, length); } static inline int -mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb, bool unsign) +mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { BFDEV_MPI_TYPE *ptra, *ptrb; unsigned long lena, lenb; - int diff; - - if (!unsign && va->plus != vb->plus) { - if (va->plus) - return 1; - return -1; - } lena = mpi_len(va); lenb = mpi_len(vb); @@ -403,20 +455,7 @@ mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb, bool unsign) ptra = mpi_val(va); ptrb = mpi_val(vb); - diff = mpa_cmp(ptra, ptrb, lena, lenb); - if (!diff) - return 0; - - if (diff > 0) { - if (unsign || va->plus) - return 1; - return -1; - } - - /* diff < 0 */ - if (unsign || va->plus) - return -1; - return 1; + return mpa_cmp(ptra, ptrb, lena, lenb); } static inline int @@ -453,8 +492,13 @@ mpi_add(bfdev_mpi_t *dest, bool carry; int retval; - cnta = mpi_len(va); cntb = mpi_len(vb); + ptrb = mpi_val(vb); + + if (cntb == 1) + return mpi_addi(dest, va, *ptrb); + + cnta = mpi_len(va); length = bfdev_max(cnta, cntb); retval = bfdev_array_resize(&dest->value, length + 1); @@ -463,7 +507,6 @@ mpi_add(bfdev_mpi_t *dest, ptrs = mpi_val(dest); ptra = mpi_val(va); - ptrb = mpi_val(vb); carry = mpa_add(ptrs, ptra, ptrb, cnta, cntb, false); *(ptrs + length) = carry; @@ -507,8 +550,13 @@ mpi_sub(bfdev_mpi_t *dest, bool borrow; int retval; - cnta = mpi_len(va); cntb = mpi_len(vb); + ptrb = mpi_val(vb); + + if (cntb == 1) + return mpi_subi(dest, va, *ptrb); + + cnta = mpi_len(va); length = bfdev_max(cnta, cntb); retval = bfdev_array_resize(&dest->value, length); @@ -517,7 +565,6 @@ mpi_sub(bfdev_mpi_t *dest, ptrs = mpi_val(dest); ptra = mpi_val(va); - ptrb = mpi_val(vb); borrow = mpa_sub(ptrs, ptra, ptrb, cnta, cntb, false); if (bfdev_unlikely(borrow)) @@ -583,34 +630,39 @@ mpi_mul(bfdev_mpi_t *dest, { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; unsigned long length, cnta, cntb; - bfdev_array_t buffer; + bfdev_array_t *buffer, array; int retval; bool nval; + cntb = mpi_len(vb); + ptrb = mpi_val(vb); + + if (cntb == 1) + return mpi_muli(dest, va, *ptrb); + nval = false; - if (dest == va || dest == vb) - buffer = dest->value; + if (dest != va && dest != vb) + buffer = &dest->value; else { - bfdev_array_init(&buffer, dest->value.alloc, sizeof(*ptrs)); + bfdev_array_init(&array, dest->value.alloc, sizeof(*ptrs)); + buffer = &array; nval = true; } cnta = mpi_len(va); - cntb = mpi_len(vb); length = cnta + cntb; - retval = bfdev_array_resize(&buffer, length); + retval = bfdev_array_resize(buffer, length); if (bfdev_unlikely(retval)) return retval; - ptrs = bfdev_array_data(&buffer, 0); + ptrs = bfdev_array_data(buffer, 0); ptra = mpi_val(va); - ptrb = mpi_val(vb); mpa_mul(ptrs, ptra, ptrb, cnta, cntb); if (nval) { bfdev_array_release(&dest->value); - dest->value = buffer; + dest->value = array; } mpi_relocation(dest); @@ -618,7 +670,7 @@ mpi_mul(bfdev_mpi_t *dest, } static inline int -mpi_divi(bfdev_mpi_t *quot, BFDEV_MPI_TYPE *rem, +mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { BFDEV_MPI_TYPE *ptrs, *ptra, value; @@ -647,22 +699,132 @@ mpi_divi(bfdev_mpi_t *quot, BFDEV_MPI_TYPE *rem, value = mpa_divmodi(ptrs, ptra, vi, length); if (rem) - *rem = value; - mpi_relocation(quot); + mpi_set(rem, value); + + if (quot) + mpi_relocation(quot); return -BFDEV_ENOERR; } +static inline int +mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + bool limb; + int retval; + + cntb = mpi_len(vb); + ptrb = mpi_val(vb); + + if (cntb == 1) + return mpi_divi(quot, rem, va, *ptrb); + + /* divide by zero */ + if (bfdev_unlikely(!mpi_cmpi(vb, 0))) + return -BFDEV_EOVERFLOW; + + cnta = mpi_len(va); + length = cnta - cntb; + + + + + + + if (quot) { + retval = bfdev_array_resize("->value, length); + if (bfdev_unlikely(retval)) + return retval; + } + + retval = bfdev_mpi_copy(rem, va); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(quot); + ptra = mpi_val(rem); + + limb = mpa_divrem(ptrs, ptra, ptrb, cnta, cntb); + if (rem) { + retval = bfdev_array_resize(&rem->value, cntb); + if (bfdev_unlikely(retval)) + return retval; + + mpi_relocation(rem); + } + + if (quot) { + length = cnta - cntb; + if (limb) { + ptrs[length] = limb; + length += 1; + } + + mpi_relocation(quot); + } + + return -BFDEV_ENOERR; +} + +/** + * Multi Precision Integers (signed). + * + * The following function is used to calculate + * signed mpi and provide export api. + */ + export int bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { - return mpi_cmp(va, vb, false); + int diff; + + if (va->plus != vb->plus) { + if (va->plus) + return 1; + return -1; + } + + diff = mpi_cmp(va, vb); + if (!diff) + return 0; + + if (diff > 0) { + if (va->plus) + return 1; + return -1; + } + + /* diff < 0 */ + if (va->plus) + return -1; + return 1; } export int bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { - return mpi_cmpi(va, vi, false); + int diff; + + if (!va->plus) + return -1; + + diff = mpi_cmpi(va, vi); + if (!diff) + return 0; + + if (diff > 0) { + if (va->plus) + return 1; + return -1; + } + + /* diff < 0 */ + if (va->plus) + return -1; + return 1; } export int @@ -680,12 +842,12 @@ bfdev_mpi_add(bfdev_mpi_t *dest, return -BFDEV_ENOERR; } - diff = mpi_cmp(va, vb, true); + diff = mpi_cmp(va, vb); switch (diff) { case 0: default: retval = mpi_set(dest, 0); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = true; break; @@ -693,7 +855,7 @@ bfdev_mpi_add(bfdev_mpi_t *dest, case 1: retval = mpi_sub(dest, va, vb); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = va->plus; break; @@ -701,13 +863,13 @@ bfdev_mpi_add(bfdev_mpi_t *dest, case -1: retval = mpi_sub(dest, vb, va); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = vb->plus; break; } - return retval; + return -BFDEV_ENOERR; } export int @@ -731,7 +893,7 @@ bfdev_mpi_addi(bfdev_mpi_t *dest, case 0: default: retval = mpi_set(dest, 0); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = true; break; @@ -739,19 +901,22 @@ bfdev_mpi_addi(bfdev_mpi_t *dest, case -1: value = mpi_val(va); retval = mpi_set(dest, vi - *value); + if (bfdev_unlikely(retval)) + return retval; + dest->plus = true; break; case 1: retval = mpi_subi(dest, va, vi); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = false; break; } - return retval; + return -BFDEV_ENOERR; } export int @@ -769,12 +934,12 @@ bfdev_mpi_sub(bfdev_mpi_t *dest, return -BFDEV_ENOERR; } - diff = mpi_cmp(va, vb, true); + diff = mpi_cmp(va, vb); switch (diff) { case 0: default: retval = mpi_set(dest, 0); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = true; break; @@ -782,7 +947,7 @@ bfdev_mpi_sub(bfdev_mpi_t *dest, case 1: retval = mpi_sub(dest, va, vb); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = va->plus; break; @@ -790,13 +955,13 @@ bfdev_mpi_sub(bfdev_mpi_t *dest, case -1: retval = mpi_sub(dest, vb, va); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = !vb->plus; break; } - return retval; + return -BFDEV_ENOERR; } export int @@ -815,12 +980,12 @@ bfdev_mpi_subi(bfdev_mpi_t *dest, return -BFDEV_ENOERR; } - diff = mpi_cmpi(va, vi, true); + diff = mpi_cmpi(va, vi); switch (diff) { case 0: default: retval = mpi_set(dest, 0); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = true; break; @@ -828,19 +993,22 @@ bfdev_mpi_subi(bfdev_mpi_t *dest, case -1: value = mpi_val(va); retval = mpi_set(dest, vi - *value); + if (bfdev_unlikely(retval)) + return retval; + dest->plus = false; break; case 1: retval = mpi_subi(dest, va, vi); if (bfdev_unlikely(retval)) - break; + return retval; dest->plus = va->plus; break; } - return retval; + return -BFDEV_ENOERR; } export int @@ -853,9 +1021,9 @@ bfdev_mpi_mul(bfdev_mpi_t *dest, cnta = mpi_len(va); cntb = mpi_len(vb); - if (cnta > cntb) { - bfdev_swap(vb ,va); - bfdev_swap(cntb, cnta); + if (cnta < cntb) { + bfdev_swap(va, vb); + bfdev_swap(cnta, cntb); } retval = mpi_mul(dest, va, vb); @@ -883,17 +1051,118 @@ bfdev_mpi_muli(bfdev_mpi_t *dest, } export int -bfdev_mpi_divi(bfdev_mpi_t *dest, BFDEV_MPI_TYPE *rem, +bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + int retval, diff; + + diff = mpi_cmp(va, vb); + switch (diff) { + case 0: default: + if (quot) { + retval = mpi_set(quot, 1); + if (bfdev_unlikely(retval)) + return retval; + + quot->plus = !(va->plus ^ vb->plus); + } + + if (rem) { + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; + } + break; + + case -1: + if (quot) { + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; + } + + if (rem) { + retval = bfdev_mpi_copy(rem, va); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = va->plus; + } + break; + + case 1: + retval = mpi_div(quot, rem, va, vb); + if (bfdev_unlikely(retval)) + return retval; + + if (quot) + quot->plus = !(va->plus ^ vb->plus); + + if (rem) + rem->plus = va->plus; + break; + } + + return retval; +} + +export int +bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { - int retval; + int retval, diff; - retval = mpi_divi(dest, rem, va, vi); - if (bfdev_unlikely(retval)) - return retval; + diff = mpi_cmpi(va, vi); + switch (diff) { + case 0: default: + if (quot) { + retval = mpi_set(quot, 1); + if (bfdev_unlikely(retval)) + return retval; - if (dest) - dest->plus = va->plus; + quot->plus = va->plus; + } + + if (rem) { + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; + } + break; + + case -1: + if (quot) { + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; + } + + if (rem) { + retval = bfdev_mpi_copy(rem, va); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = va->plus; + } + break; + + case 1: + retval = mpi_divi(quot, rem, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + if (quot) + quot->plus = va->plus; + break; + } return -BFDEV_ENOERR; } @@ -901,7 +1170,15 @@ bfdev_mpi_divi(bfdev_mpi_t *dest, BFDEV_MPI_TYPE *rem, export int bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) { - return mpi_set(dest, val); + int retval; + + retval = mpi_set(dest, val); + if (bfdev_unlikely(retval)) + return retval; + + dest->plus = true; + + return -BFDEV_ENOERR; } export int @@ -911,8 +1188,10 @@ bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) unsigned long length; int retval; - length = mpi_len(src); + if (dest == src) + return -BFDEV_EINVAL; + length = mpi_len(src); retval = bfdev_array_resize(&dest->value, length); if (bfdev_unlikely(retval)) return retval; From e6db237f43fdd49c9b9db4295cca467bb7ba3ce1 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 12 Feb 2024 02:07:59 +0800 Subject: [PATCH 092/119] fixup mpi: fixed division typo Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 8 + src/mpi.c | 363 ++++++++++++++++++++++++++++---------------- 2 files changed, 241 insertions(+), 130 deletions(-) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index f4c7bd8d..b6d4c1f2 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -48,6 +48,10 @@ extern int bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); +extern int +bfdev_mpi_mod(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + extern int bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); @@ -64,6 +68,10 @@ extern int bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); +extern int +bfdev_mpi_modi(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); + extern int bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); diff --git a/src/mpi.c b/src/mpi.c index 02cb34f6..e4db1c11 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -342,9 +342,9 @@ mpa_divrem(BFDEV_MPI_TYPE *ptrs, index = cnta - cntb; ptra += index; - dlow = ptra[cntb - 2]; - dhigh = ptra[cntb - 1]; - value = ptrb[cntb - 1]; + value = ptra[cntb - 1]; + dhigh = ptrb[cntb - 1]; + dlow = ptrb[cntb - 2]; limb = false; if (value >= dhigh) { @@ -388,7 +388,8 @@ mpa_divrem(BFDEV_MPI_TYPE *ptrs, quot--; } - ptrs[index] = quot; + if (ptrs) + ptrs[index] = quot; value = ptra[cntb - 1]; } @@ -431,6 +432,29 @@ mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE vi) return -BFDEV_ENOERR; } +static inline int +mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + int retval; + + if (dest == src) + return -BFDEV_EINVAL; + + length = mpi_len(src); + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(src); + + mpa_copy(ptrs, ptra, length); + + return -BFDEV_ENOERR; +} + static inline int mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { @@ -495,6 +519,7 @@ mpi_add(bfdev_mpi_t *dest, cntb = mpi_len(vb); ptrb = mpi_val(vb); + /* degrade to addi */ if (cntb == 1) return mpi_addi(dest, va, *ptrb); @@ -553,6 +578,7 @@ mpi_sub(bfdev_mpi_t *dest, cntb = mpi_len(vb); ptrb = mpi_val(vb); + /* degrade to subi */ if (cntb == 1) return mpi_subi(dest, va, *ptrb); @@ -599,31 +625,6 @@ mpi_muli(bfdev_mpi_t *dest, return -BFDEV_ENOERR; } -static inline int -mpi_maci(bfdev_mpi_t *dest, - const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) -{ - BFDEV_MPI_TYPE *ptrs, *ptra; - BFDEV_MPI_TYPE carry; - unsigned long length; - int retval; - - length = mpi_len(va); - - retval = bfdev_array_resize(&dest->value, length + 1); - if (bfdev_unlikely(retval)) - return retval; - - ptrs = mpi_val(dest); - ptra = mpi_val(va); - - carry = mpa_maci(ptrs, ptra, vi, length, 0); - *(ptrs + length) = carry; - mpi_relocation(dest); - - return -BFDEV_ENOERR; -} - static inline int mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) @@ -637,6 +638,7 @@ mpi_mul(bfdev_mpi_t *dest, cntb = mpi_len(vb); ptrb = mpi_val(vb); + /* degrade to muli */ if (cntb == 1) return mpi_muli(dest, va, *ptrb); @@ -683,26 +685,36 @@ mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, length = mpi_len(va); - if (quot) { - retval = bfdev_array_resize("->value, length); - if (bfdev_unlikely(retval)) - return retval; - - ptrs = mpi_val(quot); - } + retval = bfdev_array_resize("->value, length); + if (bfdev_unlikely(retval)) + return retval; + ptrs = mpi_val(quot); ptra = mpi_val(va); - if (!quot) - value = mpa_modi(ptra, vi, length); - else - value = mpa_divmodi(ptrs, ptra, vi, length); + value = mpa_divmodi(ptrs, ptra, vi, length); + mpi_set(rem, value); + mpi_relocation(quot); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_modi(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + BFDEV_MPI_TYPE *ptra, value; + unsigned long length; + + /* divide by zero */ + if (bfdev_unlikely(!vi)) + return -BFDEV_EOVERFLOW; - if (rem) - mpi_set(rem, value); + length = mpi_len(va); + ptra = mpi_val(va); - if (quot) - mpi_relocation(quot); + value = mpa_modi(ptra, vi, length); + mpi_set(rem, value); return -BFDEV_ENOERR; } @@ -713,12 +725,14 @@ mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; unsigned long cnta, cntb, length; - bool limb; + bfdev_array_t *buffer, array; + bool limb, nval; int retval; cntb = mpi_len(vb); ptrb = mpi_val(vb); + /* degrade to divi */ if (cntb == 1) return mpi_divi(quot, rem, va, *ptrb); @@ -726,46 +740,85 @@ mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, if (bfdev_unlikely(!mpi_cmpi(vb, 0))) return -BFDEV_EOVERFLOW; - cnta = mpi_len(va); - length = cnta - cntb; - - - - - - - if (quot) { - retval = bfdev_array_resize("->value, length); + if (rem != va) { + retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) return retval; } - retval = bfdev_mpi_copy(rem, va); + nval = false; + if (quot != va) + buffer = "->value; + else { + bfdev_array_init(&array, va->value.alloc, sizeof(*ptrs)); + buffer = &array; + nval = true; + } + + cnta = mpi_len(va); + length = cnta + cntb; + + retval = bfdev_array_resize(buffer, length); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(quot); + ptrs = bfdev_array_data(buffer, 0); ptra = mpi_val(rem); limb = mpa_divrem(ptrs, ptra, ptrb, cnta, cntb); - if (rem) { - retval = bfdev_array_resize(&rem->value, cntb); - if (bfdev_unlikely(retval)) - return retval; + length = cnta - cntb; + + if (limb) { + ptrs[length] = limb; + length += 1; + } - mpi_relocation(rem); + if (nval) { + bfdev_array_release("->value); + quot->value = array; } - if (quot) { - length = cnta - cntb; - if (limb) { - ptrs[length] = limb; - length += 1; - } + bfdev_array_resize("->value, length); + bfdev_array_resize(&rem->value, cntb); + + mpi_relocation(quot); + mpi_relocation(rem); - mpi_relocation(quot); + return -BFDEV_ENOERR; +} + +static inline int +mpi_mod(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptra, *ptrb; + unsigned long cnta, cntb; + int retval; + + cntb = mpi_len(vb); + ptrb = mpi_val(vb); + + /* degrade to divi */ + if (cntb == 1) + return mpi_modi(rem, va, *ptrb); + + /* divide by zero */ + if (bfdev_unlikely(!mpi_cmpi(vb, 0))) + return -BFDEV_EOVERFLOW; + + if (rem != va) { + retval = mpi_copy(rem, va); + if (bfdev_unlikely(retval)) + return retval; } + cnta = mpi_len(va); + ptra = mpi_val(rem); + + mpa_divrem(NULL, ptra, ptrb, cnta, cntb); + bfdev_array_resize(&rem->value, cntb); + mpi_relocation(rem); + return -BFDEV_ENOERR; } @@ -1059,39 +1112,32 @@ bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, diff = mpi_cmp(va, vb); switch (diff) { case 0: default: - if (quot) { - retval = mpi_set(quot, 1); - if (bfdev_unlikely(retval)) - return retval; - - quot->plus = !(va->plus ^ vb->plus); - } + retval = mpi_set(quot, 1); + if (bfdev_unlikely(retval)) + return retval; - if (rem) { - retval = mpi_set(rem, 0); - if (bfdev_unlikely(retval)) - return retval; + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; - rem->plus = true; - } + quot->plus = !(va->plus ^ vb->plus); + rem->plus = true; break; case -1: - if (quot) { - retval = mpi_set(quot, 0); - if (bfdev_unlikely(retval)) - return retval; - - rem->plus = true; - } + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; - if (rem) { - retval = bfdev_mpi_copy(rem, va); + if (rem != va) { + retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) return retval; rem->plus = va->plus; } + + quot->plus = true; break; case 1: @@ -1099,15 +1145,12 @@ bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, if (bfdev_unlikely(retval)) return retval; - if (quot) - quot->plus = !(va->plus ^ vb->plus); - - if (rem) - rem->plus = va->plus; + quot->plus = !(va->plus ^ vb->plus); + rem->plus = va->plus; break; } - return retval; + return -BFDEV_ENOERR; } export int @@ -1119,34 +1162,104 @@ bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, diff = mpi_cmpi(va, vi); switch (diff) { case 0: default: - if (quot) { - retval = mpi_set(quot, 1); - if (bfdev_unlikely(retval)) - return retval; + retval = mpi_set(quot, 1); + if (bfdev_unlikely(retval)) + return retval; - quot->plus = va->plus; - } + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; - if (rem) { - retval = mpi_set(rem, 0); + quot->plus = va->plus; + rem->plus = true; + break; + + case -1: + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; + + if (rem != va) { + retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) return retval; - rem->plus = true; + rem->plus = va->plus; } + + quot->plus = true; + break; + + case 1: + retval = mpi_divi(quot, rem, va, vi); + if (bfdev_unlikely(retval)) + return retval; + + quot->plus = va->plus; + rem->plus = va->plus; + break; + } + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_mod(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + int retval, diff; + + diff = mpi_cmp(va, vb); + switch (diff) { + case 0: default: + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; break; case -1: - if (quot) { - retval = mpi_set(quot, 0); + if (rem != va) { + retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) return retval; - rem->plus = true; + rem->plus = va->plus; } + break; + + case 1: + retval = mpi_mod(rem, va, vb); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = va->plus; + break; + } + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_modi(bfdev_mpi_t *rem, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) +{ + int retval, diff; + + diff = mpi_cmpi(va, vi); + switch (diff) { + case 0: default: + retval = mpi_set(rem, 0); + if (bfdev_unlikely(retval)) + return retval; + + rem->plus = true; + break; - if (rem) { - retval = bfdev_mpi_copy(rem, va); + case -1: + if (rem != va) { + retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) return retval; @@ -1155,12 +1268,11 @@ bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, break; case 1: - retval = mpi_divi(quot, rem, va, vi); + retval = mpi_modi(rem, va, vi); if (bfdev_unlikely(retval)) return retval; - if (quot) - quot->plus = va->plus; + rem->plus = va->plus; break; } @@ -1184,22 +1296,12 @@ bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) export int bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) { - BFDEV_MPI_TYPE *ptrs, *ptra; - unsigned long length; int retval; - if (dest == src) - return -BFDEV_EINVAL; - - length = mpi_len(src); - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_copy(dest, src); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - ptra = mpi_val(src); - - mpa_copy(ptrs, ptra, length); dest->plus = src->plus; return -BFDEV_ENOERR; @@ -1210,18 +1312,19 @@ bfdev_mpi_create(const bfdev_alloc_t *alloc) { bfdev_mpi_t *result; BFDEV_MPI_TYPE *array; + int retval; result = bfdev_malloc(alloc, sizeof(*result)); if (bfdev_unlikely(!result)) return NULL; bfdev_array_init(&result->value, alloc, sizeof(*array)); - array = bfdev_array_push(&result->value, 1); - if (bfdev_unlikely(!array)) - return NULL; + retval = bfdev_mpi_set(result, 0); - result->plus = true; - *array = 0; + if (bfdev_unlikely(retval)) { + bfdev_free(alloc, result); + return NULL; + } return result; } From 49e346ce0fec2edc598af7e95a6f954dfb602b75 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 12 Feb 2024 16:34:08 +0800 Subject: [PATCH 093/119] refactor bitsperlong: move to asm-generic Signed-off-by: John Sanpe --- include/bfdev/{ => asm-generic}/bitsperlong.h | 6 ++--- include/bfdev/bits.h | 2 +- include/bfdev/types.h | 26 ++++++++++++------- 3 files changed, 21 insertions(+), 13 deletions(-) rename include/bfdev/{ => asm-generic}/bitsperlong.h (78%) diff --git a/include/bfdev/bitsperlong.h b/include/bfdev/asm-generic/bitsperlong.h similarity index 78% rename from include/bfdev/bitsperlong.h rename to include/bfdev/asm-generic/bitsperlong.h index 61215d20..dda63670 100644 --- a/include/bfdev/bitsperlong.h +++ b/include/bfdev/asm-generic/bitsperlong.h @@ -3,8 +3,8 @@ * Copyright(c) 2023 John Sanpe */ -#ifndef _BFDEV_BITSPERLONG_H_ -#define _BFDEV_BITSPERLONG_H_ +#ifndef _BFDEV_ASM_GENERIC_BITSPERLONG_H_ +#define _BFDEV_ASM_GENERIC_BITSPERLONG_H_ #include @@ -25,4 +25,4 @@ BFDEV_BEGIN_DECLS BFDEV_END_DECLS -#endif /* _BFDEV_BITSPERLONG_H_ */ +#endif /* _BFDEV_ASM_GENERIC_BITSPERLONG_H_ */ diff --git a/include/bfdev/bits.h b/include/bfdev/bits.h index 53433843..cf6fa46b 100644 --- a/include/bfdev/bits.h +++ b/include/bfdev/bits.h @@ -9,7 +9,7 @@ #include #include #include -#include +#include BFDEV_BEGIN_DECLS diff --git a/include/bfdev/types.h b/include/bfdev/types.h index be0edc7c..a3ecff23 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -9,6 +9,7 @@ #include #include #include +#include BFDEV_BEGIN_DECLS @@ -25,15 +26,22 @@ typedef uint16_t __bfdev_bitwise bfdev_be16; typedef uint32_t __bfdev_bitwise bfdev_be32; typedef uint64_t __bfdev_bitwise bfdev_be64; -typedef unsigned long bfdev_uw_t; -typedef unsigned int bfdev_uhw_t; - -typedef int bfdev_sw_t __attribute__((mode(SI))); -typedef int bfdev_dw_t __attribute__((mode(DI))); -typedef int bfdev_qw_t __attribute__((mode(QI))); -typedef unsigned bfdev_usw_t __attribute__((mode(SI))); -typedef unsigned bfdev_udw_t __attribute__((mode(DI))); -typedef unsigned bfdev_uqw_t __attribute__((mode(QI))); +typedef int bfdev_qi_t __bfdev_mode(QI); +typedef int bfdev_hi_t __bfdev_mode(HI); +typedef int bfdev_si_t __bfdev_mode(SI); +typedef int bfdev_di_t __bfdev_mode(DI); +typedef unsigned bfdev_uqi_t __bfdev_mode(QI); +typedef unsigned bfdev_uhi_t __bfdev_mode(HI); +typedef unsigned bfdev_usi_t __bfdev_mode(SI); +typedef unsigned bfdev_udi_t __bfdev_mode(DI); + +#if BFDEV_BITS_PER_LONG == 32 +typedef bfdev_uhi_t bfdev_uhw_t; +typedef bfdev_usi_t bfdev_uw_t; +#else /* BFDEV_BITS_PER_LONG == 64 */ +typedef bfdev_usi_t bfdev_uhw_t; +typedef bfdev_udi_t bfdev_uw_t; +#endif typedef int bfdev_state_t; typedef intptr_t bfdev_atomic_t; From 6d7e8f9bdc43e44ef6653c6ac93298e0aad55d06 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 12 Feb 2024 17:30:39 +0800 Subject: [PATCH 094/119] feat dword: added common double-word division Signed-off-by: John Sanpe --- include/bfdev/asm-generic/dword.h | 116 +++++++++++++++++++++++++++ include/bfdev/dword.h | 122 +++-------------------------- src/build.cmake | 1 + src/dword.c | 126 ++++++++++++++++++++++++++++++ 4 files changed, 255 insertions(+), 110 deletions(-) create mode 100644 src/dword.c diff --git a/include/bfdev/asm-generic/dword.h b/include/bfdev/asm-generic/dword.h index 9e5e2f55..7ec4edd1 100644 --- a/include/bfdev/asm-generic/dword.h +++ b/include/bfdev/asm-generic/dword.h @@ -24,6 +24,122 @@ BFDEV_BEGIN_DECLS #define BFDEV_DWORD_HIGHER(val) \ ((bfdev_uw_t)(val) >> BFDEV_DWORD_BITS) +#ifndef bfdev_dword_addd +# define bfdev_dword_addd(sh, sl, ah, al, bh, bl) do { \ + bfdev_uw_t __ah, __al, __bh, __bl; \ + bfdev_uw_t __x; \ + \ + __ah = (ah); \ + __al = (al); \ + __bh = (bh); \ + __bl = (bl); \ + \ + __x = __al + __bl; \ + (sh) = __ah + __bh + (__x < __al); \ + (sl) = __x; \ +} while (0) +#endif + +#ifndef bfdev_sub_ddmmss +# define bfdev_sub_ddmmss(sh, sl, ah, al, bh, bl) do { \ + bfdev_uw_t __ah, __al, __bh, __bl; \ + bfdev_uw_t __x; \ + \ + __ah = (ah); \ + __al = (al); \ + __bh = (bh); \ + __bl = (bl); \ + \ + __x = __al - __bl; \ + (sh) = __ah - __bh - (__x > __al); \ + (sl) = __x; \ +} while (0) +#endif + +#ifndef bfdev_umul_ppmm +# define bfdev_umul_ppmm(dh, dl, va, vb) do { \ + bfdev_uhw_t __ul, __vl, __uh, __vh; \ + bfdev_uw_t __x0, __x1, __x2, __x3; \ + bfdev_uw_t __va, __vb; \ + \ + __va = (va); \ + __vb = (vb); \ + \ + __ul = BFDEV_DWORD_LOWER(__va); \ + __vl = BFDEV_DWORD_LOWER(__vb); \ + __uh = BFDEV_DWORD_HIGHER(__va); \ + __vh = BFDEV_DWORD_HIGHER(__vb); \ + \ + __x0 = (bfdev_uw_t)__ul * __vl; \ + __x1 = (bfdev_uw_t)__ul * __vh; \ + __x2 = (bfdev_uw_t)__uh * __vl; \ + __x3 = (bfdev_uw_t)__uh * __vh; \ + \ + __x1 += BFDEV_DWORD_HIGHER(__x0); \ + __x1 += __x2; \ + if (__x1 < __x2) \ + __x3 += BFDEV_DWORD_SIZE; \ + \ + __x2 = BFDEV_DWORD_LOWER(__x1) << BFDEV_DWORD_BITS; \ + (dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \ + (dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \ +} while (0) +#endif + +#ifndef bfdev_udiv_qrnnd +# define bfdev_udiv_qrnnd(quot, rem, sh, sl, div) do { \ + bfdev_uw_t __d1, __d0, __q1, __q0; \ + bfdev_uw_t __r1, __r0, __m; \ + bfdev_uw_t __sh, __sl, __div; \ + \ + __sh = (sh); \ + __sl = (sl); \ + __div = (div); \ + \ + __d1 = BFDEV_DWORD_HIGHER(__div); \ + __d0 = BFDEV_DWORD_LOWER(__div); \ + \ + __r1 = __sh % __d1; \ + __q1 = __sh / __d1; \ + __m = (bfdev_uw_t)__q1 * __d0; \ + __r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__sl); \ + \ + if (__r1 < __m) { \ + __q1--; \ + __r1 += __div; \ + \ + if (__r1 >= __div) { \ + if (__r1 < __m) { \ + __q1--; \ + __r1 += (__div); \ + } \ + } \ + } \ + \ + __r1 -= __m; \ + __r0 = __r1 % __d1; \ + __q0 = __r1 / __d1; \ + __m = (bfdev_uw_t)__q0 * __d0; \ + __r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__sl); \ + \ + if (__r0 < __m) { \ + __q0--; \ + __r0 += (__div); \ + \ + if (__r0 >= __div) { \ + if (__r0 < __m) { \ + __q0--; \ + __r0 += __div; \ + } \ + } \ + } \ + \ + __r0 -= __m; \ + (quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \ + (rem) = __r0; \ +} while (0) +#endif + BFDEV_END_DECLS #endif /* _BFDEV_ASM_GENERIC_DWORD_H_ */ diff --git a/include/bfdev/dword.h b/include/bfdev/dword.h index 915a82d6..5f8a94c5 100644 --- a/include/bfdev/dword.h +++ b/include/bfdev/dword.h @@ -13,121 +13,23 @@ BFDEV_BEGIN_DECLS -#ifndef bfdev_dword_addd -# define bfdev_dword_addd(sh, sl, ah, al, bh, bl) do { \ - bfdev_uw_t __ah, __al, __bh, __bl; \ - bfdev_uw_t __x; \ - \ - __ah = (ah); \ - __al = (al); \ - __bh = (bh); \ - __bl = (bl); \ - \ - __x = __al + __bl; \ - (sh) = __ah + __bh + (__x < __al); \ - (sl) = __x; \ -} while (0) +#ifndef bfdev_dword_udiv +# define bfdev_dword_udiv(quot, rem, src, div) \ + bfdev_dword_generic_udiv(quot, rem, src, div) #endif -#ifndef bfdev_dword_subd -# define bfdev_dword_subd(sh, sl, ah, al, bh, bl) do { \ - bfdev_uw_t __ah, __al, __bh, __bl; \ - bfdev_uw_t __x; \ - \ - __ah = (ah); \ - __al = (al); \ - __bh = (bh); \ - __bl = (bl); \ - \ - __x = __al - __bl; \ - (sh) = __ah - __bh - (__x > __al); \ - (sl) = __x; \ -} while (0) +#ifndef bfdev_dword_udivd +# define bfdev_dword_udivd(quot, rem, src, div) \ + bfdev_dword_generic_udivd(quot, rem, src, div) #endif -#ifndef bfdev_dword_umul -# define bfdev_dword_umul(dh, dl, va, vb) do { \ - bfdev_uhw_t __ul, __vl, __uh, __vh; \ - bfdev_uw_t __x0, __x1, __x2, __x3; \ - bfdev_uw_t __va, __vb; \ - \ - __va = (va); \ - __vb = (vb); \ - \ - __ul = BFDEV_DWORD_LOWER(__va); \ - __vl = BFDEV_DWORD_LOWER(__vb); \ - __uh = BFDEV_DWORD_HIGHER(__va); \ - __vh = BFDEV_DWORD_HIGHER(__vb); \ - \ - __x0 = (bfdev_uw_t)__ul * __vl; \ - __x1 = (bfdev_uw_t)__ul * __vh; \ - __x2 = (bfdev_uw_t)__uh * __vl; \ - __x3 = (bfdev_uw_t)__uh * __vh; \ - \ - __x1 += BFDEV_DWORD_HIGHER(__x0); \ - __x1 += __x2; \ - if (__x1 < __x2) \ - __x3 += BFDEV_DWORD_SIZE; \ - \ - __x2 = BFDEV_DWORD_LOWER(__x1) << BFDEV_DWORD_BITS; \ - (dh) = __x3 + BFDEV_DWORD_HIGHER(__x1); \ - (dl) = __x2 + BFDEV_DWORD_LOWER(__x0); \ -} while (0) -#endif +extern void +bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem, + const bfdev_uw_t *src, bfdev_uw_t div); -#ifndef bfdev_dword_udiv -# define bfdev_dword_udiv(quot, rem, sh, sl, div) do { \ - bfdev_uw_t __d1, __d0, __q1, __q0; \ - bfdev_uw_t __r1, __r0, __m; \ - bfdev_uw_t __sh, __sl, __div; \ - \ - __sh = (sh); \ - __sl = (sl); \ - __div = (div); \ - \ - __d1 = BFDEV_DWORD_HIGHER(__div); \ - __d0 = BFDEV_DWORD_LOWER(__div); \ - \ - __r1 = __sh % __d1; \ - __q1 = __sh / __d1; \ - __m = (bfdev_uw_t)__q1 * __d0; \ - __r1 = __r1 * BFDEV_DWORD_SIZE | BFDEV_DWORD_HIGHER(__sl); \ - \ - if (__r1 < __m) { \ - __q1--; \ - __r1 += __div; \ - \ - if (__r1 >= __div) { \ - if (__r1 < __m) { \ - __q1--; \ - __r1 += (__div); \ - } \ - } \ - } \ - \ - __r1 -= __m; \ - __r0 = __r1 % __d1; \ - __q0 = __r1 / __d1; \ - __m = (bfdev_uw_t)__q0 * __d0; \ - __r0 = __r0 * BFDEV_DWORD_SIZE | BFDEV_DWORD_LOWER(__sl); \ - \ - if (__r0 < __m) { \ - __q0--; \ - __r0 += (__div); \ - \ - if (__r0 >= __div) { \ - if (__r0 < __m) { \ - __q0--; \ - __r0 += __div; \ - } \ - } \ - } \ - \ - __r0 -= __m; \ - (quot) = (bfdev_uw_t)__q1 * BFDEV_DWORD_SIZE | __q0; \ - (rem) = __r0; \ -} while (0) -#endif +extern void +bfdev_dword_generic_udivd(bfdev_uw_t *quot, bfdev_uw_t *rem, + const bfdev_uw_t *src, const bfdev_uw_t *div); BFDEV_END_DECLS diff --git a/src/build.cmake b/src/build.cmake index 1d34c4c2..8c549d64 100644 --- a/src/build.cmake +++ b/src/build.cmake @@ -17,6 +17,7 @@ set(BFDEV_SOURCE ${CMAKE_CURRENT_LIST_DIR}/bsearch.c ${CMAKE_CURRENT_LIST_DIR}/btree.c ${CMAKE_CURRENT_LIST_DIR}/btree-utils.c + ${CMAKE_CURRENT_LIST_DIR}/dword.c ${CMAKE_CURRENT_LIST_DIR}/callback.c ${CMAKE_CURRENT_LIST_DIR}/errname.c ${CMAKE_CURRENT_LIST_DIR}/fifo.c diff --git a/src/dword.c b/src/dword.c new file mode 100644 index 00000000..74e6868e --- /dev/null +++ b/src/dword.c @@ -0,0 +1,126 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#include +#include +#include +#include + +export void +bfdev_dword_generic_udiv(bfdev_uw_t *quot, bfdev_uw_t *rem, + const bfdev_uw_t *src, bfdev_uw_t div) +{ + bfdev_uw_t q0, q1, b, bm; + bfdev_uw_t n0, n1, n2; + + if (!quot && !rem) + return; + + n0 = src[0]; + n1 = src[1]; + + if (div > n1) { + bm = bfdev_clz(div); + if (bm) { + div = div << bm; + n1 = (n1 << bm) | (n0 >> (BFDEV_BITS_PER_LONG - bm)); + n0 = n0 << bm; + } + + bfdev_udiv_qrnnd(q0, n0, n1, n0, div); + q1 = 0; + } else { + /* divide by zero */ + if (!div) + div = 1 / div; + + bm = bfdev_clz(div); + if (bm == 0) { + n1 -= div; + q1 = 1; + } else { + b = BFDEV_BITS_PER_LONG - bm; + div = div << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + bfdev_udiv_qrnnd(q1, n1, n2, n1, div); + } + + bfdev_udiv_qrnnd(q0, n0, n1, n0, div); + } + + if (quot) { + quot[0] = q0; + quot[1] = q1; + } + + if (rem) + *rem = n0 >> bm; +} + +export void +bfdev_dword_generic_udivd(bfdev_uw_t *quot, bfdev_uw_t *rem, + const bfdev_uw_t *src, const bfdev_uw_t *div) +{ + bfdev_uw_t q0, q1, b, bm; + bfdev_uw_t d0, d1, n0, n1, n2; + bfdev_uw_t m1, m0; + + if (!quot && !rem) + return; + + n0 = src[0]; + n1 = src[1]; + d0 = div[0]; + d1 = div[1]; + + /* degrade to udiv */ + if (!d1) + return bfdev_dword_udiv(quot, rem, src, d0); + + bm = bfdev_clz(d1); + if (bm == 0) { + if (n1 <= d1 && n0 < d0) + q0 = 0; + else { + q0 = 1; + bfdev_sub_ddmmss(n1, n0, n1, n0, d1, d0); + } + + q1 = 0; + if (rem) { + rem[0] = n0; + rem[1] = n1; + } + } else { + b = BFDEV_BITS_PER_LONG - bm; + d1 = (d1 << bm) | (d0 >> b); + d0 = d0 << bm; + n2 = n1 >> b; + n1 = (n1 << bm) | (n0 >> b); + n0 = n0 << bm; + + bfdev_udiv_qrnnd(q0, n1, n2, n1, d1); + bfdev_umul_ppmm(m1, m0, q0, d0); + + if (m1 > n1 || (m1 == n1 && m0 > n0)) { + q0--; + bfdev_sub_ddmmss(m1, m0, m1, m0, d1, d0); + } + + q1 = 0; + if (rem) { + bfdev_sub_ddmmss(n1, n0, n1, n0, m1, m0); + rem[0] = (n1 << b) | (n0 >> bm); + rem[1] = n1 >> bm; + } + } + + if (quot) { + quot[0] = q0; + quot[1] = q1; + } +} From d92da545bc4d24d994c9b8a5242add96754eedb7 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 12 Feb 2024 23:27:48 +0800 Subject: [PATCH 095/119] feat mpi: using the refactored dword Signed-off-by: John Sanpe --- src/mpi.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/mpi.c b/src/mpi.c index e4db1c11..69db03c1 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -170,7 +170,7 @@ mpa_muli(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE vhigh, vlow; while (length--) { - bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi); vlow += carry; carry = (vlow < carry) + vhigh; *ptrs++ = vlow; @@ -187,7 +187,7 @@ mpa_maci(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE vhigh, vlow; while (length--) { - bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi); vlow += carry; carry = (vlow < carry) + vhigh; @@ -207,7 +207,7 @@ mpa_msui(BFDEV_MPI_TYPE *ptrs, BFDEV_MPI_TYPE vhigh, vlow; while (length--) { - bfdev_dword_umul(vhigh, vlow, *ptra++, vi); + bfdev_umul_ppmm(vhigh, vlow, *ptra++, vi); vlow += carry; carry = (vlow < carry) + vhigh; @@ -269,16 +269,16 @@ mpa_divmodi(BFDEV_MPI_TYPE *ptrs, const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length) { - BFDEV_MPI_TYPE value, rem; + BFDEV_MPI_TYPE quot[2], dword[2]; unsigned long index; index = length - 1; ptrs += index; ptra += index; - rem = *ptra; + dword[1] = *ptra; - if (rem >= vi) - rem = 0; + if (dword[1] >= vi) + dword[1] = 0; else { *ptrs-- = 0; ptra--; @@ -286,38 +286,38 @@ mpa_divmodi(BFDEV_MPI_TYPE *ptrs, } while (length--) { - value = *ptra--; - bfdev_dword_udiv(*ptrs--, rem, rem, value, vi); + dword[0] = *ptra--; + bfdev_dword_udiv(quot, dword + 1, dword, vi); + *ptrs-- = quot[0]; } - return rem; + return dword[1]; } static inline BFDEV_MPI_TYPE mpa_modi(const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length) { - BFDEV_MPI_TYPE dummy __bfdev_maybe_unused; - BFDEV_MPI_TYPE value, rem; + BFDEV_MPI_TYPE dword[2]; unsigned long index; index = length - 1; ptra += index; - rem = *ptra; + dword[1] = *ptra; - if (rem >= vi) - rem = 0; + if (dword[1] >= vi) + dword[1] = 0; else { ptra--; length--; } while (length--) { - value = *ptra--; - bfdev_dword_udiv(dummy, rem, rem, value, vi); + dword[0] = *ptra--; + bfdev_dword_udiv(NULL, dword + 1, dword, vi); } - return rem; + return dword[1]; } static inline bool @@ -365,10 +365,15 @@ mpa_divrem(BFDEV_MPI_TYPE *ptrs, if (value == dhigh) quot = ~(BFDEV_MPI_TYPE)0UL; else { + BFDEV_MPI_TYPE result[2], dword[2]; BFDEV_MPI_TYPE rem; - bfdev_dword_udiv(quot, rem, value, ptra[cntb - 1], dhigh); - bfdev_dword_umul(v1, value, dlow, quot); + dword[0] = ptra[cntb - 1]; + dword[1] = value; + bfdev_dword_udiv(result, &rem, dword, dhigh); + + quot = result[0]; + bfdev_umul_ppmm(v1, value, dlow, quot); while (v1 > rem || (v1 == rem && value > ptra[cntb - 2])) { quot--; @@ -693,7 +698,8 @@ mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, ptra = mpi_val(va); value = mpa_divmodi(ptrs, ptra, vi, length); - mpi_set(rem, value); + if (quot != rem) + mpi_set(rem, value); mpi_relocation(quot); return -BFDEV_ENOERR; From 6bff18b081a86a3932f7e6a6afdf6291553e305a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 13 Feb 2024 11:42:44 +0800 Subject: [PATCH 096/119] fixup mpi: fixed mpa subi borrow Signed-off-by: John Sanpe --- src/mpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mpi.c b/src/mpi.c index 69db03c1..12acfc88 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -129,7 +129,7 @@ mpa_subi(BFDEV_MPI_TYPE *ptrs, borrow = *ptrs++ > *ptra++; while (--length) { - *ptrs = *ptra + borrow; + *ptrs = *ptra - borrow; borrow = *ptrs++ > *ptra++; } From 0492d57f938e18dbacde717912b381e424689df8 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 13 Feb 2024 12:10:53 +0800 Subject: [PATCH 097/119] feat mpi: added pi example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/mpi/.gitignore | 2 ++ examples/mpi/CMakeLists.txt | 22 ++++++++++++ examples/mpi/pi.c | 67 +++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 examples/mpi/.gitignore create mode 100644 examples/mpi/CMakeLists.txt create mode 100644 examples/mpi/pi.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 3b71683a..a0cf58c1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -29,6 +29,7 @@ add_subdirectory(log) add_subdirectory(log2) add_subdirectory(matrix) add_subdirectory(minpool) +add_subdirectory(mpi) add_subdirectory(once) add_subdirectory(radix) add_subdirectory(rbtree) diff --git a/examples/mpi/.gitignore b/examples/mpi/.gitignore new file mode 100644 index 00000000..ebe8118a --- /dev/null +++ b/examples/mpi/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/mpi-pi diff --git a/examples/mpi/CMakeLists.txt b/examples/mpi/CMakeLists.txt new file mode 100644 index 00000000..f72db0ca --- /dev/null +++ b/examples/mpi/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(mpi-pi pi.c) +target_link_libraries(mpi-pi bfdev) +add_test(mpi-pi mpi-pi) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + pi.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/mpi + ) + + install(TARGETS + mpi-pi + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/mpi/pi.c b/examples/mpi/pi.c new file mode 100644 index 00000000..ecc9430c --- /dev/null +++ b/examples/mpi/pi.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "mpi-pi" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include "../time.h" + +#define TEST_LEN 10000 +#define TEST_SIZE (TEST_LEN / 4 + 1) +#define TEST_LOOP (TEST_LEN / 1.39793 + 1) + +int main(int argc, const char *argv[]) +{ + bfdev_mpi_t *vw, *vs, *vv, *vq; + unsigned int k; + int retval; + + if (!((vw = bfdev_mpi_create(NULL)) && + (vs = bfdev_mpi_create(NULL)) && + (vv = bfdev_mpi_create(NULL)) && + (vq = bfdev_mpi_create(NULL)))) + return 1; + + if ((retval = bfdev_mpi_set(vw, 16 * 5)) || + (retval = bfdev_mpi_set(vv, 239 * 4)) || + (retval = bfdev_mpi_set(vq, 10000))) + return retval; + + for (k = 0; k < TEST_SIZE; ++k) { + if ((retval = bfdev_mpi_mul(vw, vw, vq)) || + (retval = bfdev_mpi_mul(vv, vv, vq))) + return retval; + } + + bfdev_log_info("Calculate PI %d:\n", TEST_LEN); + EXAMPLE_TIME_STATISTICAL( + for (k = 1; k <= TEST_LOOP; ++k) { + if ((retval = bfdev_mpi_divi(vw, vw, vw, 25)) || + (retval = bfdev_mpi_divi(vv, vv, vv, 239 * 239)) || + (retval = bfdev_mpi_sub(vq, vw, vv)) || + (retval = bfdev_mpi_divi(vq, vq, vq, 2 * k - 1))) + return retval; + + if (k & 1) + retval = bfdev_mpi_add(vs, vs, vq); + else + retval = bfdev_mpi_sub(vs, vs, vq); + + if (retval) + return retval; + } + 0; + ); + + bfdev_mpi_destory(vw); + bfdev_mpi_destory(vs); + bfdev_mpi_destory(vv); + bfdev_mpi_destory(vq); + + return 0; +} From fb06a9fb77a47694f075eb77aab37de67b2c5f83 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 01:32:56 +0800 Subject: [PATCH 098/119] feat mpi: added read/write functions Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 50 +++++++++++++++++++++++++++++++-------------- src/mpi.c | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index b6d4c1f2..e6871abe 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -26,62 +26,82 @@ struct bfdev_mpi { bool plus; }; -extern int +static inline unsigned long __bfdev_nonnull(1) +bfdev_mpi_length(const bfdev_mpi_t *mpi) +{ + return bfdev_array_index(&mpi->value); +} + +static inline size_t __bfdev_nonnull(1) +bfdev_mpi_size(const bfdev_mpi_t *mpi) +{ + return bfdev_array_size(&mpi->value); +} + +extern int __bfdev_nonnull(1, 2) bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1) bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1, 2, 3) bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1, 2, 3) bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1, 2, 3) bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1, 2, 3, 4) bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1, 2, 3) bfdev_mpi_mod(bfdev_mpi_t *rem, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_modi(bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int +extern int __bfdev_nonnull(1) bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); -extern int +extern int __bfdev_nonnull(1, 2) bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src); +extern int __bfdev_nonnull(1, 2, 4) +bfdev_mpi_read(const bfdev_mpi_t *var, BFDEV_MPI_TYPE *buffer, + unsigned long length, bool *sign); + +extern int __bfdev_nonnull(1, 2) +bfdev_mpi_write(bfdev_mpi_t *var, const BFDEV_MPI_TYPE *buffer, + unsigned long length, bool sign); + extern bfdev_mpi_t * bfdev_mpi_create(const bfdev_alloc_t *alloc); -extern void +extern void __bfdev_nonnull(1) bfdev_mpi_destory(bfdev_mpi_t *var); BFDEV_END_DECLS diff --git a/src/mpi.c b/src/mpi.c index 12acfc88..a0f2ed0e 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -1313,6 +1313,42 @@ bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) return -BFDEV_ENOERR; } +export int +bfdev_mpi_read(const bfdev_mpi_t *var, BFDEV_MPI_TYPE *buffer, + unsigned long length, bool *sign) +{ + BFDEV_MPI_TYPE *data; + unsigned long index; + + index = mpi_len(var); + if (bfdev_unlikely(length > index)) + return -BFDEV_EFBIG; + + data = mpi_val(var); + mpa_copy(buffer, data, length); + *sign = var->plus; + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_write(bfdev_mpi_t *var, const BFDEV_MPI_TYPE *buffer, + unsigned long length, bool sign) +{ + BFDEV_MPI_TYPE *data; + int retval; + + retval = bfdev_array_resize(&var->value, length); + if (bfdev_unlikely(retval)) + return retval; + + data = mpi_val(var); + mpa_copy(data, buffer, length); + var->plus = sign; + + return -BFDEV_ENOERR; +} + export bfdev_mpi_t * bfdev_mpi_create(const bfdev_alloc_t *alloc) { From 6a935a3722f00a9f8dc1d777e3265ab20e913962 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 10:59:47 +0800 Subject: [PATCH 099/119] fixup mpi: fixed some calculation errors Signed-off-by: John Sanpe --- examples/mpi/pi.c | 6 +-- include/bfdev/mpi.h | 10 ++--- src/mpi.c | 91 ++++++++++++++++++++++----------------------- 3 files changed, 52 insertions(+), 55 deletions(-) diff --git a/examples/mpi/pi.c b/examples/mpi/pi.c index ecc9430c..53bf583e 100644 --- a/examples/mpi/pi.c +++ b/examples/mpi/pi.c @@ -27,9 +27,9 @@ int main(int argc, const char *argv[]) (vq = bfdev_mpi_create(NULL)))) return 1; - if ((retval = bfdev_mpi_set(vw, 16 * 5)) || - (retval = bfdev_mpi_set(vv, 239 * 4)) || - (retval = bfdev_mpi_set(vq, 10000))) + if ((retval = bfdev_mpi_seti(vw, 16 * 5)) || + (retval = bfdev_mpi_seti(vv, 239 * 4)) || + (retval = bfdev_mpi_seti(vq, 10000))) return retval; for (k = 0; k < TEST_SIZE; ++k) { diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index e6871abe..ec2338b6 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -15,9 +15,9 @@ BFDEV_BEGIN_DECLS -#ifndef BFDEV_MPI_TYPE -# define BFDEV_MPI_TYPE bfdev_uw_t -#endif +#define BFDEV_MPI_TYPE bfdev_uw_t +#define BFDEV_MPI_BITS BFDEV_BITS_PER_LONG +#define BFDEV_MPI_SIZE BFDEV_BYTES_PER_LONG typedef struct bfdev_mpi bfdev_mpi_t; @@ -85,10 +85,10 @@ bfdev_mpi_modi(bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); extern int __bfdev_nonnull(1) -bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); +bfdev_mpi_seti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); extern int __bfdev_nonnull(1, 2) -bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src); +bfdev_mpi_set(bfdev_mpi_t *dest, const bfdev_mpi_t *src); extern int __bfdev_nonnull(1, 2, 4) bfdev_mpi_read(const bfdev_mpi_t *var, BFDEV_MPI_TYPE *buffer, diff --git a/src/mpi.c b/src/mpi.c index a0f2ed0e..5ac50637 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -83,8 +83,11 @@ mpa_addi(BFDEV_MPI_TYPE *ptrs, const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, bool carry) { - *ptrs = *ptra + vi; - carry = *ptrs++ < *ptra++; + BFDEV_MPI_TYPE value; + + value = *ptra + vi; + carry = value < *ptra++; + *ptrs++ = value; while (--length) { *ptrs = *ptra + carry; @@ -99,9 +102,12 @@ mpa_add(BFDEV_MPI_TYPE *ptrs, const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, unsigned long cnta, unsigned long cntb, bool carry) { + BFDEV_MPI_TYPE value; + while (cnta && cntb) { - *ptrs = *ptra + *ptrb++ + carry; - carry = *ptrs++ < *ptra++; + value = *ptra + *ptrb++ + carry; + carry = value < *ptra++; + *ptrs++ = value; cnta--; cntb--; @@ -113,8 +119,9 @@ mpa_add(BFDEV_MPI_TYPE *ptrs, } while (cnta--) { - *ptrs = *ptra + carry; - carry = *ptrs++ < *ptra++; + value = *ptra + carry; + carry = value < *ptra++; + *ptrs++ = value; } return carry; @@ -125,8 +132,11 @@ mpa_subi(BFDEV_MPI_TYPE *ptrs, const BFDEV_MPI_TYPE *ptra, BFDEV_MPI_TYPE vi, unsigned long length, bool borrow) { - *ptrs = *ptra - vi; - borrow = *ptrs++ > *ptra++; + BFDEV_MPI_TYPE value; + + value = *ptra - vi; + borrow = value > *ptra++; + *ptrs++ = value; while (--length) { *ptrs = *ptra - borrow; @@ -141,22 +151,21 @@ mpa_sub(BFDEV_MPI_TYPE *ptrs, const BFDEV_MPI_TYPE *ptra, const BFDEV_MPI_TYPE *ptrb, unsigned long cnta, unsigned long cntb, bool borrow) { + BFDEV_MPI_TYPE value; + while (cnta && cntb) { - *ptrs = *ptra - *ptrb++ - borrow; - borrow = *ptrs++ > *ptra++; + value = *ptra - *ptrb++ - borrow; + borrow = value > *ptra++; + *ptrs++ = value; cnta--; cntb--; } - if (!cnta) { - cnta = cntb; - ptra = ptrb; - } - while (cnta--) { - *ptrs = *ptra - borrow; - borrow = *ptrs++ > *ptra++; + value = *ptra - borrow; + borrow = value > *ptra++; + *ptrs++ = value; } return borrow; @@ -576,7 +585,7 @@ mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; - unsigned long length, cnta, cntb; + unsigned long cnta, cntb; bool borrow; int retval; @@ -588,9 +597,10 @@ mpi_sub(bfdev_mpi_t *dest, return mpi_subi(dest, va, *ptrb); cnta = mpi_len(va); - length = bfdev_max(cnta, cntb); + if (bfdev_unlikely(cnta < cntb)) + return -BFDEV_EOVERFLOW; - retval = bfdev_array_resize(&dest->value, length); + retval = bfdev_array_resize(&dest->value, cnta); if (bfdev_unlikely(retval)) return retval; @@ -865,25 +875,10 @@ bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb) export int bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi) { - int diff; - if (!va->plus) return -1; - diff = mpi_cmpi(va, vi); - if (!diff) - return 0; - - if (diff > 0) { - if (va->plus) - return 1; - return -1; - } - - /* diff < 0 */ - if (va->plus) - return -1; - return 1; + return mpi_cmpi(va, vi); } export int @@ -1131,10 +1126,6 @@ bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, break; case -1: - retval = mpi_set(quot, 0); - if (bfdev_unlikely(retval)) - return retval; - if (rem != va) { retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) @@ -1143,6 +1134,11 @@ bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, rem->plus = va->plus; } + /* quot can be equal to va */ + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; + quot->plus = true; break; @@ -1181,10 +1177,6 @@ bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, break; case -1: - retval = mpi_set(quot, 0); - if (bfdev_unlikely(retval)) - return retval; - if (rem != va) { retval = mpi_copy(rem, va); if (bfdev_unlikely(retval)) @@ -1193,6 +1185,11 @@ bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, rem->plus = va->plus; } + /* quot can be equal to va */ + retval = mpi_set(quot, 0); + if (bfdev_unlikely(retval)) + return retval; + quot->plus = true; break; @@ -1286,7 +1283,7 @@ bfdev_mpi_modi(bfdev_mpi_t *rem, } export int -bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) +bfdev_mpi_seti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) { int retval; @@ -1300,7 +1297,7 @@ bfdev_mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) } export int -bfdev_mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) +bfdev_mpi_set(bfdev_mpi_t *dest, const bfdev_mpi_t *src) { int retval; @@ -1361,7 +1358,7 @@ bfdev_mpi_create(const bfdev_alloc_t *alloc) return NULL; bfdev_array_init(&result->value, alloc, sizeof(*array)); - retval = bfdev_mpi_set(result, 0); + retval = bfdev_mpi_seti(result, 0); if (bfdev_unlikely(retval)) { bfdev_free(alloc, result); From ce29e71bb77b3b60d16446e754583065dd5a37d2 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 12:18:41 +0800 Subject: [PATCH 100/119] refactor mpi: move pi to marchin Signed-off-by: John Sanpe --- examples/mpi/CMakeLists.txt | 10 +++++----- examples/mpi/{pi.c => marchin.c} | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) rename examples/mpi/{pi.c => marchin.c} (94%) diff --git a/examples/mpi/CMakeLists.txt b/examples/mpi/CMakeLists.txt index f72db0ca..1504ebe4 100644 --- a/examples/mpi/CMakeLists.txt +++ b/examples/mpi/CMakeLists.txt @@ -3,19 +3,19 @@ # Copyright(c) 2023 ffashion # -add_executable(mpi-pi pi.c) -target_link_libraries(mpi-pi bfdev) -add_test(mpi-pi mpi-pi) +add_executable(mpi-marchin marchin.c) +target_link_libraries(mpi-marchin bfdev) +add_test(mpi-marchin mpi-marchin) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES - pi.c + marchin.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/mpi ) install(TARGETS - mpi-pi + mpi-marchin DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin ) diff --git a/examples/mpi/pi.c b/examples/mpi/marchin.c similarity index 94% rename from examples/mpi/pi.c rename to examples/mpi/marchin.c index 53bf583e..e9121e47 100644 --- a/examples/mpi/pi.c +++ b/examples/mpi/marchin.c @@ -3,7 +3,7 @@ * Copyright(c) 2023 John Sanpe */ -#define MODULE_NAME "mpi-pi" +#define MODULE_NAME "mpi-marchin" #define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt #include @@ -38,7 +38,7 @@ int main(int argc, const char *argv[]) return retval; } - bfdev_log_info("Calculate PI %d:\n", TEST_LEN); + bfdev_log_info("Calculate Marchin %d:\n", TEST_LEN); EXAMPLE_TIME_STATISTICAL( for (k = 1; k <= TEST_LOOP; ++k) { if ((retval = bfdev_mpi_divi(vw, vw, vw, 25)) || From 86f1dbb136cdd7fbf48652f5da3cb6c2730be451 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 12:24:42 +0800 Subject: [PATCH 101/119] feat mpi: added bseti/bclri functions Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 6 +++++ src/mpi.c | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index ec2338b6..95e3c7a5 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -84,6 +84,12 @@ extern int __bfdev_nonnull(1, 2) bfdev_mpi_modi(bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); +extern int __bfdev_nonnull(1) +bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); + +extern int __bfdev_nonnull(1) +bfdev_mpi_bclri(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); + extern int __bfdev_nonnull(1) bfdev_mpi_seti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); diff --git a/src/mpi.c b/src/mpi.c index 5ac50637..45e5e9f2 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #define mpi_len(mpi) bfdev_array_index(&(mpi)->value) @@ -446,6 +447,27 @@ mpi_set(bfdev_mpi_t *dest, BFDEV_MPI_TYPE vi) return -BFDEV_ENOERR; } +static inline int +mpi_extend(bfdev_mpi_t *var, unsigned long length) +{ + BFDEV_MPI_TYPE *base; + unsigned long index; + int retval; + + index = mpi_len(var); + if (index >= length) + return -BFDEV_ENOERR; + + retval = bfdev_array_resize(&var->value, length); + if (bfdev_unlikely(retval)) + return retval; + + base = bfdev_array_data(&var->value, index); + mpa_zero(base, length - index); + + return -BFDEV_ENOERR; +} + static inline int mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) { @@ -1282,6 +1304,43 @@ bfdev_mpi_modi(bfdev_mpi_t *rem, return -BFDEV_ENOERR; } +export int +bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) +{ + BFDEV_MPI_TYPE *base; + unsigned long offset; + int retval; + + offset = BFDEV_BITS_WORD(bit); + retval = mpi_extend(dest, offset + 1); + if (bfdev_unlikely(retval)) + return retval; + + base = mpi_val(dest); + bfdev_bit_set(base, bit); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_bclri(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) +{ + BFDEV_MPI_TYPE *base; + unsigned long offset, length; + + offset = BFDEV_BITS_WORD(bit); + length = mpi_len(dest); + + if (offset > length) + return -BFDEV_ENOERR; + + base = mpi_val(dest); + bfdev_bit_clr(base, bit); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + export int bfdev_mpi_seti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val) { From bdf0a4d099d6253c575127feca3b3cdf55fc7573 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 12:25:53 +0800 Subject: [PATCH 102/119] feat mpi: added bbp example Signed-off-by: John Sanpe --- examples/mpi/CMakeLists.txt | 6 +++ examples/mpi/bbp.c | 100 ++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 examples/mpi/bbp.c diff --git a/examples/mpi/CMakeLists.txt b/examples/mpi/CMakeLists.txt index 1504ebe4..275ed3f4 100644 --- a/examples/mpi/CMakeLists.txt +++ b/examples/mpi/CMakeLists.txt @@ -3,18 +3,24 @@ # Copyright(c) 2023 ffashion # +add_executable(mpi-bbp bbp.c) +target_link_libraries(mpi-bbp bfdev) +add_test(mpi-bbp mpi-bbp) + add_executable(mpi-marchin marchin.c) target_link_libraries(mpi-marchin bfdev) add_test(mpi-marchin mpi-marchin) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES + bbp.c marchin.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/mpi ) install(TARGETS + mpi-bbp mpi-marchin DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/mpi/bbp.c b/examples/mpi/bbp.c new file mode 100644 index 00000000..4ea1da38 --- /dev/null +++ b/examples/mpi/bbp.c @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "mpi-bbp" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include "../time.h" + +#define TEST_LEN 10000 + +static BFDEV_MPI_TYPE +factor1[] = { + 2, 1, 0, 0, +}; + +static BFDEV_MPI_TYPE +factor2[] = { + 1, 4, 5, 6, +}; + +int main(int argc, const char *argv[]) +{ + bfdev_mpi_t *vv[4], *vq; + unsigned int k, i, v; + int retval; + + if (!((vv[0] = bfdev_mpi_create(NULL)) && + (vv[1] = bfdev_mpi_create(NULL)) && + (vv[2] = bfdev_mpi_create(NULL)) && + (vv[3] = bfdev_mpi_create(NULL)) && + (vq = bfdev_mpi_create(NULL)))) + return 1; + + bfdev_log_info("Calculate BBP %d:\n", TEST_LEN); + EXAMPLE_TIME_STATISTICAL( + for (i = 0; i < 4; ++i) { + retval = bfdev_mpi_seti(vv[i], 0); + if (retval) + return retval; + + v = factor1[i] + (TEST_LEN << 2); + retval = bfdev_mpi_bseti(vv[i], v); + if (retval) + return retval; + + v = factor2[i]; + retval = bfdev_mpi_divi(vv[i], vv[i], vv[i], v); + if (retval) + return retval; + + if (i) + retval = bfdev_mpi_sub(vq, vq, vv[i]); + else + retval = bfdev_mpi_add(vq, vq, *vv); + + if (retval) + return retval; + } + + for (k = 1; k < TEST_LEN; ++k) { + for (i = 0; i < 4; ++i) { + retval = bfdev_mpi_seti(vv[i], 0); + if (retval) + return retval; + + v = factor1[i] + ((TEST_LEN - k) << 2); + retval = bfdev_mpi_bseti(vv[i], v); + if (retval) + return retval; + + v = factor2[i] | (k << 3); + retval = bfdev_mpi_divi(vv[i], vv[i], vv[i], v); + if (retval) + return retval; + + if (i) + retval = bfdev_mpi_sub(vq, vq, vv[i]); + else + retval = bfdev_mpi_add(vq, vq, *vv); + + if (retval) + return retval; + } + } + 0; + ); + + bfdev_mpi_destory(vv[0]); + bfdev_mpi_destory(vv[1]); + bfdev_mpi_destory(vv[2]); + bfdev_mpi_destory(vv[3]); + bfdev_mpi_destory(vq); + + return 0; +} From f09bafcec19a6bc6f50e011bb5b73776b336cca2 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 12:36:52 +0800 Subject: [PATCH 103/119] feat mpi: added fibonacci example Signed-off-by: John Sanpe --- examples/mpi/CMakeLists.txt | 6 +++ examples/mpi/fibonacci.c | 48 ++++++++++++++++++++ examples/mpi/helper.h | 88 +++++++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 examples/mpi/fibonacci.c create mode 100644 examples/mpi/helper.h diff --git a/examples/mpi/CMakeLists.txt b/examples/mpi/CMakeLists.txt index 275ed3f4..d5369385 100644 --- a/examples/mpi/CMakeLists.txt +++ b/examples/mpi/CMakeLists.txt @@ -11,10 +11,15 @@ add_executable(mpi-marchin marchin.c) target_link_libraries(mpi-marchin bfdev) add_test(mpi-marchin mpi-marchin) +add_executable(mpi-fibonacci fibonacci.c) +target_link_libraries(mpi-fibonacci bfdev) +add_test(mpi-fibonacci mpi-fibonacci) + if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES bbp.c marchin.c + fibonacci.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/mpi ) @@ -22,6 +27,7 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(TARGETS mpi-bbp mpi-marchin + mpi-fibonacci DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin ) diff --git a/examples/mpi/fibonacci.c b/examples/mpi/fibonacci.c new file mode 100644 index 00000000..63480a5e --- /dev/null +++ b/examples/mpi/fibonacci.c @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#include +#include +#include "helper.h" + +#define TEST_LOOP 10000 + +int main(int argc, const char *argv[]) +{ + bfdev_mpi_t *va, *vb, *vc; + unsigned int count; + char *result; + int retval; + + if (!((va = bfdev_mpi_create(NULL)) && + (vb = bfdev_mpi_create(NULL)) && + (vc = bfdev_mpi_create(NULL)))) + return 1; + + if ((retval = bfdev_mpi_seti(va, 1)) || + (retval = bfdev_mpi_seti(vb, 0)) || + (retval = bfdev_mpi_seti(vc, 0))) + return retval; + + for (count = 0; count < TEST_LOOP - 1; ++count) { + if ((retval = bfdev_mpi_add(vc, va, vb)) || + (retval = bfdev_mpi_set(vb, va)) || + (retval = bfdev_mpi_set(va, vc))) + return retval; + } + + result = print_num(va, 10); + if (!result) + return 1; + + puts(result); + free(result); + + bfdev_mpi_destory(va); + bfdev_mpi_destory(vb); + bfdev_mpi_destory(vc); + + return 0; +} diff --git a/examples/mpi/helper.h b/examples/mpi/helper.h new file mode 100644 index 00000000..91ae9c44 --- /dev/null +++ b/examples/mpi/helper.h @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef _MPI_PRINT_H_ +#define _MPI_PRINT_H_ + +#include +#include +#include +#include + +static inline char * +print_num(const bfdev_mpi_t *var, unsigned int base) +{ + BFDEV_DEFINE_ARRAY(stack, NULL, sizeof(char)); + bfdev_mpi_t *taken, *value; + unsigned long count; + char *buffer, *str; + size_t size; + int retval; + + if (!((value = bfdev_mpi_create(NULL)) && + (taken = bfdev_mpi_create(NULL)))) + return NULL; + + retval = bfdev_mpi_set(value, var); + if (retval) + return NULL; + + while (bfdev_mpi_cmpi(value, 0)) { + BFDEV_MPI_TYPE walk; + bool dummy; + + retval = bfdev_mpi_divi(value, taken, value, base); + if (retval) + return NULL; + + retval = bfdev_mpi_read(taken, &walk, 1, &dummy); + if (retval) + return NULL; + + buffer = bfdev_array_push(&stack, 1); + if (!buffer) + return NULL; + + if (walk < 10) + *buffer = '0' + walk; + else + *buffer = 'A' + walk - 10; + } + + size = bfdev_array_size(&stack); + if (!size) { + buffer = bfdev_array_push(&stack, 1); + if (!buffer) + return NULL; + + *buffer = '0'; + } + + if (!var->plus) { + buffer = bfdev_array_push(&stack, 1); + if (!buffer) + return NULL; + + *buffer = '-'; + } + + size = bfdev_array_size(&stack); + buffer = malloc(size + 1); + + for (count = 0; count < size; ++count) { + str = bfdev_array_pop(&stack, 1); + buffer[count] = *str; + } + + buffer[count] = '\0'; + bfdev_array_release(&stack); + + bfdev_mpi_destory(taken); + bfdev_mpi_destory(value); + + return buffer; +} + +#endif /* _MPI_PRINT_H_ */ From c7b09efd6757ca0ae9db4d7ddff4a37e1ba28de3 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Wed, 14 Feb 2024 12:53:02 +0800 Subject: [PATCH 104/119] feat mpi: added initial bitops functions Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 12 ++++++ src/mpi.c | 95 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index 95e3c7a5..71579c0a 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -84,6 +84,18 @@ extern int __bfdev_nonnull(1, 2) bfdev_mpi_modi(bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); +extern int __bfdev_nonnull(1, 2, 3) +bfdev_mpi_and(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + +extern int __bfdev_nonnull(1, 2, 3) +bfdev_mpi_or(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + +extern int __bfdev_nonnull(1, 2, 3) +bfdev_mpi_xor(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb); + extern int __bfdev_nonnull(1) bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); diff --git a/src/mpi.c b/src/mpi.c index 45e5e9f2..5c959499 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -1304,6 +1304,101 @@ bfdev_mpi_modi(bfdev_mpi_t *rem, return -BFDEV_ENOERR; } +export int +bfdev_mpi_and(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_min(cnta, cntb); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + bfdev_bitmap_and(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_or(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + length = bfdev_min(cnta, cntb); + bfdev_bitmap_or(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + + if (cnta < cntb) { + bfdev_swap(ptra, ptrb); + bfdev_swap(cnta, cntb); + } + + length = cnta - cntb; + mpa_copy(ptrs + cntb, ptra + cntb, length); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_xor(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + length = bfdev_min(cnta, cntb); + bfdev_bitmap_xor(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + + if (cnta < cntb) { + bfdev_swap(ptra, ptrb); + bfdev_swap(cnta, cntb); + } + + length = cnta - cntb; + mpa_copy(ptrs + cntb, ptra + cntb, length); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + export int bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) { From c4426b4f42de058fd8fc079841ee20aed6de695a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 15 Feb 2024 04:51:08 +0800 Subject: [PATCH 105/119] fixup style: fixed several style issues Signed-off-by: John Sanpe --- examples/mpi/.gitignore | 4 +- examples/mpi/CMakeLists.txt | 10 +- examples/mpi/{marchin.c => machin.c} | 17 ++- include/bfdev/asm-generic/builtin-clz.h | 2 +- include/bfdev/asm-generic/builtin-ctz.h | 2 +- include/bfdev/asm-generic/builtin-ffs.h | 2 +- include/bfdev/asm-generic/builtin-ffsuf.h | 2 +- include/bfdev/asm-generic/builtin-fls.h | 2 +- include/bfdev/asm-generic/builtin-flsuf.h | 2 +- include/bfdev/asm-generic/cmpxchg.h | 4 +- include/bfdev/hashtbl.h | 8 +- include/bfdev/heap.h | 2 +- include/bfdev/hlist.h | 8 +- include/bfdev/ilist.h | 10 +- include/bfdev/list.h | 14 +-- include/bfdev/log2.h | 124 +++++++++++----------- include/bfdev/refcount.h | 2 +- include/bfdev/respool.h | 4 +- include/bfdev/segtree.h | 2 +- include/bfdev/slist.h | 6 +- include/bfdev/struct.h | 2 +- src/btree.c | 22 +++- src/crypto/arc4.c | 12 +-- src/fifo.c | 2 +- src/mpi.c | 15 +-- src/radix.c | 6 +- src/respool.c | 4 +- src/ringbuf.c | 4 +- src/textsearch/bm.c | 2 +- src/textsearch/linear.c | 8 +- 30 files changed, 164 insertions(+), 140 deletions(-) rename examples/mpi/{marchin.c => machin.c} (76%) diff --git a/examples/mpi/.gitignore b/examples/mpi/.gitignore index ebe8118a..7724d4fb 100644 --- a/examples/mpi/.gitignore +++ b/examples/mpi/.gitignore @@ -1,2 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-or-later -/mpi-pi +/mpi-bbp +/mpi-fibonacci +/mpi-machin diff --git a/examples/mpi/CMakeLists.txt b/examples/mpi/CMakeLists.txt index d5369385..f962b521 100644 --- a/examples/mpi/CMakeLists.txt +++ b/examples/mpi/CMakeLists.txt @@ -7,9 +7,9 @@ add_executable(mpi-bbp bbp.c) target_link_libraries(mpi-bbp bfdev) add_test(mpi-bbp mpi-bbp) -add_executable(mpi-marchin marchin.c) -target_link_libraries(mpi-marchin bfdev) -add_test(mpi-marchin mpi-marchin) +add_executable(mpi-machin machin.c) +target_link_libraries(mpi-machin bfdev) +add_test(mpi-machin mpi-machin) add_executable(mpi-fibonacci fibonacci.c) target_link_libraries(mpi-fibonacci bfdev) @@ -18,7 +18,7 @@ add_test(mpi-fibonacci mpi-fibonacci) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES bbp.c - marchin.c + machin.c fibonacci.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/mpi @@ -26,7 +26,7 @@ if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(TARGETS mpi-bbp - mpi-marchin + mpi-machin mpi-fibonacci DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/mpi/marchin.c b/examples/mpi/machin.c similarity index 76% rename from examples/mpi/marchin.c rename to examples/mpi/machin.c index e9121e47..792dce15 100644 --- a/examples/mpi/marchin.c +++ b/examples/mpi/machin.c @@ -3,7 +3,7 @@ * Copyright(c) 2023 John Sanpe */ -#define MODULE_NAME "mpi-marchin" +#define MODULE_NAME "mpi-machin" #define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt #include @@ -27,8 +27,17 @@ int main(int argc, const char *argv[]) (vq = bfdev_mpi_create(NULL)))) return 1; + /** + * Machin-like formula: + * PI = 16arctan(1/5) - 4arctan(1/239) + * + * These formulas are used in conjunction with Gregory's + * series, the Taylor series expansion for arctangent: + * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + ... + */ + if ((retval = bfdev_mpi_seti(vw, 16 * 5)) || - (retval = bfdev_mpi_seti(vv, 239 * 4)) || + (retval = bfdev_mpi_seti(vv, 4 * 239)) || (retval = bfdev_mpi_seti(vq, 10000))) return retval; @@ -38,10 +47,10 @@ int main(int argc, const char *argv[]) return retval; } - bfdev_log_info("Calculate Marchin %d:\n", TEST_LEN); + bfdev_log_info("Calculate Machin %d:\n", TEST_LEN); EXAMPLE_TIME_STATISTICAL( for (k = 1; k <= TEST_LOOP; ++k) { - if ((retval = bfdev_mpi_divi(vw, vw, vw, 25)) || + if ((retval = bfdev_mpi_divi(vw, vw, vw, 5 * 5)) || (retval = bfdev_mpi_divi(vv, vv, vv, 239 * 239)) || (retval = bfdev_mpi_sub(vq, vw, vv)) || (retval = bfdev_mpi_divi(vq, vq, vq, 2 * k - 1))) diff --git a/include/bfdev/asm-generic/builtin-clz.h b/include/bfdev/asm-generic/builtin-clz.h index 3125e7c3..e3948220 100644 --- a/include/bfdev/asm-generic/builtin-clz.h +++ b/include/bfdev/asm-generic/builtin-clz.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_clz(unsigned long value) { - return __builtin_clzl(value); + return __builtin_clzl(value); } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/builtin-ctz.h b/include/bfdev/asm-generic/builtin-ctz.h index e01d8772..ce21a28b 100644 --- a/include/bfdev/asm-generic/builtin-ctz.h +++ b/include/bfdev/asm-generic/builtin-ctz.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_ctz(unsigned long value) { - return __builtin_ctzl(value); + return __builtin_ctzl(value); } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/builtin-ffs.h b/include/bfdev/asm-generic/builtin-ffs.h index 626239f1..fc2698dc 100644 --- a/include/bfdev/asm-generic/builtin-ffs.h +++ b/include/bfdev/asm-generic/builtin-ffs.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_ffs(unsigned long value) { - return __builtin_ffsl(value); + return __builtin_ffsl(value); } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/builtin-ffsuf.h b/include/bfdev/asm-generic/builtin-ffsuf.h index 11b7f36e..f46762af 100644 --- a/include/bfdev/asm-generic/builtin-ffsuf.h +++ b/include/bfdev/asm-generic/builtin-ffsuf.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_ffsuf(unsigned long value) { - return __builtin_ctzl(value); + return __builtin_ctzl(value); } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/builtin-fls.h b/include/bfdev/asm-generic/builtin-fls.h index 67cc4ae6..505bbf3e 100644 --- a/include/bfdev/asm-generic/builtin-fls.h +++ b/include/bfdev/asm-generic/builtin-fls.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_fls(unsigned long value) { - return value ? BFDEV_BITS_PER_LONG - __builtin_clzl(value) : 0; + return value ? BFDEV_BITS_PER_LONG - __builtin_clzl(value) : 0; } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/builtin-flsuf.h b/include/bfdev/asm-generic/builtin-flsuf.h index 8ff10077..0aba2ba6 100644 --- a/include/bfdev/asm-generic/builtin-flsuf.h +++ b/include/bfdev/asm-generic/builtin-flsuf.h @@ -15,7 +15,7 @@ BFDEV_BEGIN_DECLS static __bfdev_always_inline unsigned int bfdev_arch_flsuf(unsigned long value) { - return BFDEV_BITS_PER_LONG - 1 - __builtin_clzl(value); + return BFDEV_BITS_PER_LONG - 1 - __builtin_clzl(value); } BFDEV_END_DECLS diff --git a/include/bfdev/asm-generic/cmpxchg.h b/include/bfdev/asm-generic/cmpxchg.h index f33e6c66..586d1ccb 100644 --- a/include/bfdev/asm-generic/cmpxchg.h +++ b/include/bfdev/asm-generic/cmpxchg.h @@ -47,8 +47,8 @@ bfdev_arch_try_cmpxchg(bfdev_atomic_t *atomic, bfdev_atomic_t *old, bfdev_atomic prev = *old; result = bfdev_arch_cmpxchg(atomic, prev, value); - if (bfdev_unlikely(result != prev)) - *old = result; + if (bfdev_unlikely(result != prev)) + *old = result; return bfdev_likely(result == prev); } diff --git a/include/bfdev/hashtbl.h b/include/bfdev/hashtbl.h index ba547977..43ceed02 100644 --- a/include/bfdev/hashtbl.h +++ b/include/bfdev/hashtbl.h @@ -112,11 +112,11 @@ bfdev_hashtbl_del(bfdev_hlist_node_t *node) bfdev_hlist_for_each_safe(pos, tmp, &head[index & BFDEV_HASHTBL_MASK(size)]) /** - * bfdev_hashtbl_for_each_entry - iterate over hashtable index of given type. + * bfdev_hashtbl_for_each_entry - iterate over hashtable index of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your hashtable. * @size: the size of your hashtable. - * @member: the name of the bfdev_hlist_node within the struct. + * @member: the name of the bfdev_hlist_node within the struct. * @index: index to for each. */ #define bfdev_hashtbl_for_each_idx_entry(pos, head, size, member, index) \ @@ -158,11 +158,11 @@ bfdev_hashtbl_del(bfdev_hlist_node_t *node) bfdev_hashtbl_for_each_idx_safe(pos, tmp, head, size, index) /** - * bfdev_hashtbl_for_each_entry - iterate over hashtable of given type. + * bfdev_hashtbl_for_each_entry - iterate over hashtable of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your hashtable. * @size: the size of your hashtable. - * @member: the name of the bfdev_hlist_node within the struct. + * @member: the name of the bfdev_hlist_node within the struct. * @index: index temporary storage. */ #define bfdev_hashtbl_for_each_entry(pos, head, size, member, index) \ diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index f5e52eeb..d028745a 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -49,7 +49,7 @@ struct bfdev_heap_root { * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_heap_node within the struct. */ -#define bfdev_heap_entry(ptr, type, member) \ +#define bfdev_heap_entry(ptr, type, member) \ bfdev_container_of(ptr, type, member) /** diff --git a/include/bfdev/hlist.h b/include/bfdev/hlist.h index aed875fa..727f3a0f 100644 --- a/include/bfdev/hlist.h +++ b/include/bfdev/hlist.h @@ -328,10 +328,10 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) /** - * bfdev_hlist_for_each_entry - iterate over hlist of given type. + * bfdev_hlist_for_each_entry - iterate over hlist of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your hlist. - * @member: the name of the bfdev_hlist_node within the struct. + * @member: the name of the bfdev_hlist_node within the struct. */ #define bfdev_hlist_for_each_entry(pos, head, member) \ for ((pos) = bfdev_hlist_first_entry(head, typeof(*(pos)), member); \ @@ -370,7 +370,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) * bfdev_hlist_for_each_entry_from_safe - iterate over hlist from current point safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. - * @member: the name of the bfdev_hlist_node within the struct. + * @member: the name of the bfdev_hlist_node within the struct. */ #define bfdev_hlist_for_each_entry_from_safe(pos, tmp, member) \ for (; (pos) && ({(tmp) = bfdev_hlist_next_entry(pos, member); 1;}); \ @@ -380,7 +380,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) * bfdev_hlist_for_each_entry_continue_safe - continue hlist iteration safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. - * @member: the name of the bfdev_hlist_node within the struct. + * @member: the name of the bfdev_hlist_node within the struct. */ #define bfdev_hlist_for_each_entry_continue_safe(pos, tmp, member) \ for ((void)((pos) && ((pos) = bfdev_hlist_next_entry(pos, member))); \ diff --git a/include/bfdev/ilist.h b/include/bfdev/ilist.h index 9496b030..b5151fb7 100644 --- a/include/bfdev/ilist.h +++ b/include/bfdev/ilist.h @@ -302,7 +302,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the bfdev_list_head within the struct. + * @member: the name of the bfdev_list_head within the struct. */ #define bfdev_ilist_for_each_entry_reverse_safe(pos, tmp, head, member) \ bfdev_list_for_each_entry_reverse_safe(pos, tmp, &(head)->node_list, member.node_list) @@ -312,7 +312,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the bfdev_list_head within the struct. + * @member: the name of the bfdev_list_head within the struct. */ #define bfdev_ilist_for_each_entry_from_safe(pos, tmp, head, member) \ bfdev_list_for_each_entry_from_safe(pos, tmp, &(head)->node_list, member.node_list) @@ -322,7 +322,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the bfdev_list_head within the struct. + * @member: the name of the bfdev_list_head within the struct. */ #define bfdev_ilist_for_each_entry_reverse_from_safe(pos, tmp, head, member) \ bfdev_list_for_each_entry_reverse_from_safe(pos, tmp, &(head)->node_list, member.node_list) @@ -332,7 +332,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the bfdev_list_head within the struct. + * @member: the name of the bfdev_list_head within the struct. */ #define bfdev_ilist_for_each_entry_continue_safe(pos, tmp, head, member) \ bfdev_list_for_each_entry_continue_safe(pos, tmp, &(head)->node_list, member.node_list) @@ -342,7 +342,7 @@ bfdev_ilist_node_empty(bfdev_ilist_node_t *inode) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the bfdev_list_head within the struct. + * @member: the name of the bfdev_list_head within the struct. */ #define bfdev_ilist_for_each_entry_reverse_continue_safe(pos, tmp, head, member) \ bfdev_list_for_each_entry_reverse_continue_safe(pos, tmp, &(head)->node_list, member.node_list) diff --git a/include/bfdev/list.h b/include/bfdev/list.h index 5ad4f31c..754e25e2 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -693,7 +693,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_reverse_safe(pos, tmp, head, member) \ for ((pos) = bfdev_list_last_entry(head, typeof(*pos), member), \ @@ -706,7 +706,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_from_safe(pos, tmp, head, member) \ for ((tmp) = bfdev_list_next_entry(pos, member); \ @@ -718,7 +718,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_reverse_from_safe(pos, tmp, head, member) \ for ((tmp) = bfdev_list_prev_entry(pos, member); \ @@ -730,7 +730,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_continue_safe(pos, tmp, head, member) \ for ((pos) = bfdev_list_next_entry(pos, member), \ @@ -743,7 +743,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_reverse_continue_safe(pos, tmp, head, member) \ for ((pos) = bfdev_list_prev_entry(pos, member), \ @@ -756,7 +756,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_continue_check_safe(pos, tmp, head, member) \ for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ @@ -770,7 +770,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. - * @member: the name of the list head within the struct. + * @member: the name of the list head within the struct. */ #define bfdev_list_for_each_entry_reverse_continue_check_safe(pos, tmp, head, member) \ for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ diff --git a/include/bfdev/log2.h b/include/bfdev/log2.h index e338adc9..989d122d 100644 --- a/include/bfdev/log2.h +++ b/include/bfdev/log2.h @@ -16,68 +16,68 @@ #define bfdev_ilog2_const(value) ( \ __builtin_constant_p(value) ? ( \ (value) < 2 ? 0 : \ - (value) & (1ULL << 63) ? 63 : \ - (value) & (1ULL << 62) ? 62 : \ - (value) & (1ULL << 61) ? 61 : \ - (value) & (1ULL << 60) ? 60 : \ - (value) & (1ULL << 59) ? 59 : \ - (value) & (1ULL << 58) ? 58 : \ - (value) & (1ULL << 57) ? 57 : \ - (value) & (1ULL << 56) ? 56 : \ - (value) & (1ULL << 55) ? 55 : \ - (value) & (1ULL << 54) ? 54 : \ - (value) & (1ULL << 53) ? 53 : \ - (value) & (1ULL << 52) ? 52 : \ - (value) & (1ULL << 51) ? 51 : \ - (value) & (1ULL << 50) ? 50 : \ - (value) & (1ULL << 49) ? 49 : \ - (value) & (1ULL << 48) ? 48 : \ - (value) & (1ULL << 47) ? 47 : \ - (value) & (1ULL << 46) ? 46 : \ - (value) & (1ULL << 45) ? 45 : \ - (value) & (1ULL << 44) ? 44 : \ - (value) & (1ULL << 43) ? 43 : \ - (value) & (1ULL << 42) ? 42 : \ - (value) & (1ULL << 41) ? 41 : \ - (value) & (1ULL << 40) ? 40 : \ - (value) & (1ULL << 39) ? 39 : \ - (value) & (1ULL << 38) ? 38 : \ - (value) & (1ULL << 37) ? 37 : \ - (value) & (1ULL << 36) ? 36 : \ - (value) & (1ULL << 35) ? 35 : \ - (value) & (1ULL << 34) ? 34 : \ - (value) & (1ULL << 33) ? 33 : \ - (value) & (1ULL << 32) ? 32 : \ - (value) & (1ULL << 31) ? 31 : \ - (value) & (1ULL << 30) ? 30 : \ - (value) & (1ULL << 29) ? 29 : \ - (value) & (1ULL << 28) ? 28 : \ - (value) & (1ULL << 27) ? 27 : \ - (value) & (1ULL << 26) ? 26 : \ - (value) & (1ULL << 25) ? 25 : \ - (value) & (1ULL << 24) ? 24 : \ - (value) & (1ULL << 23) ? 23 : \ - (value) & (1ULL << 22) ? 22 : \ - (value) & (1ULL << 21) ? 21 : \ - (value) & (1ULL << 20) ? 20 : \ - (value) & (1ULL << 19) ? 19 : \ - (value) & (1ULL << 18) ? 18 : \ - (value) & (1ULL << 17) ? 17 : \ - (value) & (1ULL << 16) ? 16 : \ - (value) & (1ULL << 15) ? 15 : \ - (value) & (1ULL << 14) ? 14 : \ - (value) & (1ULL << 13) ? 13 : \ - (value) & (1ULL << 12) ? 12 : \ - (value) & (1ULL << 11) ? 11 : \ - (value) & (1ULL << 10) ? 10 : \ - (value) & (1ULL << 9) ? 9 : \ - (value) & (1ULL << 8) ? 8 : \ - (value) & (1ULL << 7) ? 7 : \ - (value) & (1ULL << 6) ? 6 : \ - (value) & (1ULL << 5) ? 5 : \ - (value) & (1ULL << 4) ? 4 : \ - (value) & (1ULL << 3) ? 3 : \ - (value) & (1ULL << 2) ? 2 : \ + (value) & (1ULL << 63) ? 63 : \ + (value) & (1ULL << 62) ? 62 : \ + (value) & (1ULL << 61) ? 61 : \ + (value) & (1ULL << 60) ? 60 : \ + (value) & (1ULL << 59) ? 59 : \ + (value) & (1ULL << 58) ? 58 : \ + (value) & (1ULL << 57) ? 57 : \ + (value) & (1ULL << 56) ? 56 : \ + (value) & (1ULL << 55) ? 55 : \ + (value) & (1ULL << 54) ? 54 : \ + (value) & (1ULL << 53) ? 53 : \ + (value) & (1ULL << 52) ? 52 : \ + (value) & (1ULL << 51) ? 51 : \ + (value) & (1ULL << 50) ? 50 : \ + (value) & (1ULL << 49) ? 49 : \ + (value) & (1ULL << 48) ? 48 : \ + (value) & (1ULL << 47) ? 47 : \ + (value) & (1ULL << 46) ? 46 : \ + (value) & (1ULL << 45) ? 45 : \ + (value) & (1ULL << 44) ? 44 : \ + (value) & (1ULL << 43) ? 43 : \ + (value) & (1ULL << 42) ? 42 : \ + (value) & (1ULL << 41) ? 41 : \ + (value) & (1ULL << 40) ? 40 : \ + (value) & (1ULL << 39) ? 39 : \ + (value) & (1ULL << 38) ? 38 : \ + (value) & (1ULL << 37) ? 37 : \ + (value) & (1ULL << 36) ? 36 : \ + (value) & (1ULL << 35) ? 35 : \ + (value) & (1ULL << 34) ? 34 : \ + (value) & (1ULL << 33) ? 33 : \ + (value) & (1ULL << 32) ? 32 : \ + (value) & (1ULL << 31) ? 31 : \ + (value) & (1ULL << 30) ? 30 : \ + (value) & (1ULL << 29) ? 29 : \ + (value) & (1ULL << 28) ? 28 : \ + (value) & (1ULL << 27) ? 27 : \ + (value) & (1ULL << 26) ? 26 : \ + (value) & (1ULL << 25) ? 25 : \ + (value) & (1ULL << 24) ? 24 : \ + (value) & (1ULL << 23) ? 23 : \ + (value) & (1ULL << 22) ? 22 : \ + (value) & (1ULL << 21) ? 21 : \ + (value) & (1ULL << 20) ? 20 : \ + (value) & (1ULL << 19) ? 19 : \ + (value) & (1ULL << 18) ? 18 : \ + (value) & (1ULL << 17) ? 17 : \ + (value) & (1ULL << 16) ? 16 : \ + (value) & (1ULL << 15) ? 15 : \ + (value) & (1ULL << 14) ? 14 : \ + (value) & (1ULL << 13) ? 13 : \ + (value) & (1ULL << 12) ? 12 : \ + (value) & (1ULL << 11) ? 11 : \ + (value) & (1ULL << 10) ? 10 : \ + (value) & (1ULL << 9) ? 9 : \ + (value) & (1ULL << 8) ? 8 : \ + (value) & (1ULL << 7) ? 7 : \ + (value) & (1ULL << 6) ? 6 : \ + (value) & (1ULL << 5) ? 5 : \ + (value) & (1ULL << 4) ? 4 : \ + (value) & (1ULL << 3) ? 3 : \ + (value) & (1ULL << 2) ? 2 : \ 1) : -1 \ ) diff --git a/include/bfdev/refcount.h b/include/bfdev/refcount.h index b1b7db75..1e232a28 100644 --- a/include/bfdev/refcount.h +++ b/include/bfdev/refcount.h @@ -79,7 +79,7 @@ static inline bool bfdev_refcnt_fetch_sub_test(bfdev_refcnt_t *ref, bfdev_atomic_t nr, bfdev_atomic_t *oldp) { - bfdev_atomic_t prev; + bfdev_atomic_t prev; prev = bfdev_atomic_fetch_sub(&ref->count, nr); if (oldp) diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h index 15bf030a..17e34226 100644 --- a/include/bfdev/respool.h +++ b/include/bfdev/respool.h @@ -56,8 +56,8 @@ bfdev_respool_check_empty(bfdev_respool_t *pool) } extern bfdev_resnode_t * -bfdev_respool_find(bfdev_respool_t *pool, - bfdev_respool_find_t find, const void *data); +bfdev_respool_find(bfdev_respool_t *pool, bfdev_respool_find_t find, + const void *data); extern void bfdev_respool_insert(bfdev_respool_t *pool, bfdev_resnode_t *res); diff --git a/include/bfdev/segtree.h b/include/bfdev/segtree.h index 4fbb77b2..468f4e14 100644 --- a/include/bfdev/segtree.h +++ b/include/bfdev/segtree.h @@ -25,7 +25,7 @@ struct bfdev_segtree_node { * @type: the type of the struct this is embedded in. * @member: the name of the bfdev_segtree_node within the struct. */ -#define bfdev_segtree_entry(ptr, type, member) \ +#define bfdev_segtree_entry(ptr, type, member) \ bfdev_container_of(ptr, type, member) /** diff --git a/include/bfdev/slist.h b/include/bfdev/slist.h index b0664a1b..c5a87bbc 100644 --- a/include/bfdev/slist.h +++ b/include/bfdev/slist.h @@ -159,7 +159,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, * bfdev_slist_first_entry - get the first element from a slist. * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. - * @member: the name of the slist head within the struct. + * @member: the name of the slist head within the struct. */ #define bfdev_slist_first_entry(ptr, type, member) \ bfdev_slist_entry((ptr)->next, type, member) @@ -265,7 +265,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, * bfdev_slist_for_each_entry_from_safe - iterate over slist from current point safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. - * @member: the name of the slist head within the struct. + * @member: the name of the slist head within the struct. */ #define bfdev_slist_for_each_entry_from_safe(pos, tmp, member) \ for ((tmp) = bfdev_slist_next_entry(pos, member); (pos); (pos) = (tmp), \ @@ -275,7 +275,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, * bfdev_slist_for_each_entry_continue_safe - continue slist iteration safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. - * @member: the name of the slist head within the struct. + * @member: the name of the slist head within the struct. */ #define bfdev_slist_for_each_entry_continue_safe(pos, tmp, member) \ for ((pos) = bfdev_slist_next_entry(pos, member), \ diff --git a/include/bfdev/struct.h b/include/bfdev/struct.h index da9ea6b5..935ac500 100644 --- a/include/bfdev/struct.h +++ b/include/bfdev/struct.h @@ -36,7 +36,7 @@ BFDEV_BEGIN_DECLS * @name: The identifier name of the mirrored sub-struct. * @members: The member declarations for the mirrored structs. */ -#define bfdev_struct_group(NAME, MEMBERS...) \ +#define bfdev_struct_group(NAME, MEMBERS...) \ __bfdev_struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS) /** diff --git a/src/btree.c b/src/btree.c index 7a078f62..dcc85a49 100644 --- a/src/btree.c +++ b/src/btree.c @@ -12,8 +12,12 @@ bnode_get_value(struct bfdev_btree_root *root, struct bfdev_btree_node *node, unsigned int index) { struct bfdev_btree_layout *layout; + unsigned int offset; + layout = root->layout; - return (void *)node->block[layout->ptrindex + index]; + offset = layout->ptrindex + index; + + return (void *)node->block[offset]; } static __bfdev_always_inline void @@ -21,8 +25,12 @@ bnode_set_value(struct bfdev_btree_root *root, struct bfdev_btree_node *node, unsigned int index, void *value) { struct bfdev_btree_layout *layout; + unsigned int offset; + layout = root->layout; - node->block[layout->ptrindex + index] = (uintptr_t)value; + offset = layout->ptrindex + index; + + node->block[offset] = (uintptr_t)value; } static __bfdev_always_inline uintptr_t * @@ -30,8 +38,12 @@ bnode_get_key(struct bfdev_btree_root *root, struct bfdev_btree_node *node, unsigned int index) { struct bfdev_btree_layout *layout; + unsigned int offset; + layout = root->layout; - return &node->block[layout->keylen * index]; + offset = layout->keylen * index; + + return &node->block[offset]; } static __bfdev_always_inline void @@ -450,7 +462,7 @@ remove_rebalance(struct bfdev_btree_root *root, unsigned int level, if (index) { node = bnode_get_value(root, parent, index - 1); nfill = bnode_fill_index(root, node, 0); - if (fill + nfill <= layout->keynum) { + if (fill + nfill <= layout->keynum) { rebalance_merge( root, level, node, nfill, child, fill, parent, index - 1 @@ -462,7 +474,7 @@ remove_rebalance(struct bfdev_btree_root *root, unsigned int level, if (index + 1 < bnode_fill_index(root, parent, index)) { node = bnode_get_value(root, parent, index + 1); nfill = bnode_fill_index(root, node, 0); - if (fill + nfill <= layout->keynum) { + if (fill + nfill <= layout->keynum) { rebalance_merge( root, level, child, fill, node, nfill, parent, index diff --git a/src/crypto/arc4.c b/src/crypto/arc4.c index 2a041ed3..a0b5c39a 100644 --- a/src/crypto/arc4.c +++ b/src/crypto/arc4.c @@ -18,9 +18,9 @@ arc4_transform(bfdev_arc4_ctx_t *ctx, uint8_t *buff, proa = ctx->proa; prob = ctx->prob; - vx = ctx->box[proa]; - prob += vx; - vy = ctx->box[prob]; + vx = ctx->box[proa]; + prob += vx; + vy = ctx->box[prob]; for (;;) { ctx->box[prob] = vx; @@ -36,9 +36,9 @@ arc4_transform(bfdev_arc4_ctx_t *ctx, uint8_t *buff, if (!--size) break; - prob = tb; - vx = tx; - vy = ty; + prob = tb; + vx = tx; + vy = ty; } ctx->proa = proa; diff --git a/src/fifo.c b/src/fifo.c index 7e9a675b..6d254e64 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -173,7 +173,7 @@ bfdev_fifo_out_record(struct bfdev_fifo *fifo, void *buff, datalen = fifo_record_peek(fifo, record); bfdev_min_adj(len, datalen); fifo_out_copy(fifo, buff, len, fifo->out + record); - fifo->out += datalen + record; + fifo->out += datalen + record; return len; } diff --git a/src/mpi.c b/src/mpi.c index 5c959499..56645e15 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -140,8 +140,9 @@ mpa_subi(BFDEV_MPI_TYPE *ptrs, *ptrs++ = value; while (--length) { - *ptrs = *ptra - borrow; - borrow = *ptrs++ > *ptra++; + value = *ptra - borrow; + borrow = value > *ptra++; + *ptrs++ = value; } return borrow; @@ -804,12 +805,12 @@ mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, ptra = mpi_val(rem); limb = mpa_divrem(ptrs, ptra, ptrb, cnta, cntb); - length = cnta - cntb; + length = cnta - cntb; - if (limb) { - ptrs[length] = limb; - length += 1; - } + if (limb) { + ptrs[length] = limb; + length += 1; + } if (nval) { bfdev_array_release("->value); diff --git a/src/radix.c b/src/radix.c index 7e5d6e3d..0f278625 100644 --- a/src/radix.c +++ b/src/radix.c @@ -115,7 +115,7 @@ bfdev_radix_root_alloc(bfdev_radix_root_t *root, uintptr_t offset) return NULL; for (level = root->level; level--;) { - bfdev_radix_node_t **slot, *newn; + bfdev_radix_node_t **slot, *newn; slot = &node->child[radix_depth_index(level, offset)]; offset &= BFDEV_BIT_LOW_MASK(radix_depth_shift(level)); @@ -186,8 +186,8 @@ bfdev_radix_root_charge(bfdev_radix_root_t *root, return -BFDEV_EOVERFLOW; while (offset < end) { - if (!bfdev_radix_root_alloc(root, offset)) - return -BFDEV_ENOMEM; + if (!bfdev_radix_root_alloc(root, offset)) + return -BFDEV_ENOMEM; offset += BFDEV_RADIX_BLOCK; } diff --git a/src/respool.c b/src/respool.c index 901a73a7..1148ea42 100644 --- a/src/respool.c +++ b/src/respool.c @@ -12,8 +12,8 @@ #include export bfdev_resnode_t * -bfdev_respool_find(bfdev_respool_t *pool, - bfdev_respool_find_t find, const void *data) +bfdev_respool_find(bfdev_respool_t *pool, bfdev_respool_find_t find, + const void *data) { bfdev_resnode_t *walk; diff --git a/src/ringbuf.c b/src/ringbuf.c index 234c3b2f..81ee0740 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -186,7 +186,7 @@ bfdev_ringbuf_out_record(struct bfdev_ringbuf *ringbuf, void *buff, datalen = ringbuf_record_peek(ringbuf, record); bfdev_min_adj(len, datalen); ringbuf_out_copy(ringbuf, buff, len, ringbuf->out + record); - ringbuf->out += datalen + record; + ringbuf->out += datalen + record; return len; } @@ -205,7 +205,7 @@ bfdev_ringbuf_in_record(struct bfdev_ringbuf *ringbuf, const void *buff, overflow = ringbuf_overflow(ringbuf); while (overflow) { datalen = record + ringbuf_record_peek(ringbuf, record); - ringbuf->out += datalen; + ringbuf->out += datalen; overflow -= bfdev_min(datalen, overflow); } diff --git a/src/textsearch/bm.c b/src/textsearch/bm.c index c32b63d3..6041fa4a 100644 --- a/src/textsearch/bm.c +++ b/src/textsearch/bm.c @@ -91,7 +91,7 @@ bm_compute_prefix(struct bm_context *bctx, unsigned long flags) for (index = 0; index < bctx->pattern_len - 1; ++index) { bctx->bad_shift[bctx->pattern[index]] = bctx->pattern_len - index - 1; - if (flags & BFDEV_TS_IGCASE) { + if (flags & BFDEV_TS_IGCASE) { bctx->bad_shift[tolower(bctx->pattern[index])] = bctx->pattern_len - index - 1; } diff --git a/src/textsearch/linear.c b/src/textsearch/linear.c index 7e9d3e13..5c6b7f41 100644 --- a/src/textsearch/linear.c +++ b/src/textsearch/linear.c @@ -13,10 +13,10 @@ linear_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, { bfdev_ts_linear_t *linear = tss->pdata; - if (bfdev_likely(consumed < linear->len)) { - *dest = linear->data + consumed; - return linear->len - consumed; - } + if (bfdev_likely(consumed < linear->len)) { + *dest = linear->data + consumed; + return linear->len - consumed; + } return 0; } From e289c1b2a66068502c3dd7f2d9655526e24551b0 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 16 Feb 2024 17:17:42 +0800 Subject: [PATCH 106/119] feat bitmap: added left/right logic shift functions Signed-off-by: John Sanpe --- include/bfdev/bitmap-comp.h | 8 ++++ include/bfdev/bitmap.h | 40 +++++++++++++--- src/bitmap.c | 94 +++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 7 deletions(-) diff --git a/include/bfdev/bitmap-comp.h b/include/bfdev/bitmap-comp.h index 52c59aa6..8c8845ba 100644 --- a/include/bfdev/bitmap-comp.h +++ b/include/bfdev/bitmap-comp.h @@ -39,6 +39,14 @@ extern void bfdev_bitmap_comp_xor(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits); +extern void +bfdev_bitmap_comp_shl(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits); + +extern void +bfdev_bitmap_comp_shr(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits); + extern void bfdev_bitmap_comp_complement(unsigned long *dest, const unsigned long *src, unsigned int bits); diff --git a/include/bfdev/bitmap.h b/include/bfdev/bitmap.h index b5378767..e88f99f5 100644 --- a/include/bfdev/bitmap.h +++ b/include/bfdev/bitmap.h @@ -69,7 +69,7 @@ static __bfdev_always_inline bool bfdev_bitmap_or_equal(const unsigned long *src1, const unsigned long *src2, const unsigned long *src3, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_or_equal(src1, src2, src3, bits); @@ -82,7 +82,7 @@ static __bfdev_always_inline bool bfdev_bitmap_intersects(const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_intersects(src1, src2, bits); @@ -95,7 +95,7 @@ static __bfdev_always_inline bool bfdev_bitmap_and(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_and(dest, src1, src2, bits); @@ -108,7 +108,7 @@ static __bfdev_always_inline bool bfdev_bitmap_andnot(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_andnot(dest, src1, src2, bits); @@ -121,7 +121,7 @@ static __bfdev_always_inline void bfdev_bitmap_or(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_or(dest, src1, src2, bits); @@ -134,7 +134,7 @@ static __bfdev_always_inline void bfdev_bitmap_xor(unsigned long *dest, const unsigned long *src1, const unsigned long *src2, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_xor(dest, src1, src2, bits); @@ -143,11 +143,37 @@ bfdev_bitmap_xor(unsigned long *dest, const unsigned long *src1, *dest = value & BFDEV_BIT_LOW_MASK(bits); } +static __bfdev_always_inline void +bfdev_bitmap_shl(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits) +{ + unsigned long value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_shl(dest, src, shift, bits); + + value = *src << shift; + *dest = value & BFDEV_BIT_LOW_MASK(bits); +} + +static __bfdev_always_inline void +bfdev_bitmap_shr(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits) +{ + unsigned long value; + + if (!bfdev_const_small_nbits(bits)) + return bfdev_bitmap_comp_shr(dest, src, shift, bits); + + value = *src & BFDEV_BIT_LOW_MASK(bits); + *dest = value >> shift; +} + static __bfdev_always_inline void bfdev_bitmap_complement(unsigned long *dest, const unsigned long *src, unsigned int bits) { - unsigned int value; + unsigned long value; if (!bfdev_const_small_nbits(bits)) return bfdev_bitmap_comp_complement(dest, src, bits); diff --git a/src/bitmap.c b/src/bitmap.c index f45817a4..fb0c3260 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -153,6 +153,100 @@ bfdev_bitmap_comp_xor(unsigned long *dest, const unsigned long *src1, } } +export void +bfdev_bitmap_comp_shl(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits) +{ + unsigned int length, offset, rem, index; + unsigned long value, vlow, vhigh; + + length = BFDEV_BITS_TO_LONG(bits); + offset = BFDEV_BITS_DIV_LONG(shift); + + if (length <= offset) { + memset(dest, 0, length * sizeof(*dest)); + return; + } + + /* length > offset */ + rem = BFDEV_BITS_MOD_LONG(shift); + index = length - offset - 1; + + if (BFDEV_BITS_MOD_LONG(bits)) { + vhigh = src[index] << rem; + + if (rem && index) + vlow = src[--index] >> (BFDEV_BITS_PER_LONG - rem); + else + vlow = 0; + + value = vhigh | vlow; + dest[--length] = value & BFDEV_BIT_LOW_MASK(bits); + } + + while (length) { + vhigh = src[index] << rem; + + if (rem && index) + vlow = src[--index] >> (BFDEV_BITS_PER_LONG - rem); + else + vlow = 0; + + value = vhigh | vlow; + dest[--length] = value; + } + + if (offset) + memset(dest, 0, offset * sizeof(*dest)); +} + +export void +bfdev_bitmap_comp_shr(unsigned long *dest, const unsigned long *src, + unsigned int shift, unsigned int bits) +{ + unsigned int index, length, offset, rem; + unsigned long value, vlow, vhigh; + + length = BFDEV_BITS_TO_LONG(bits); + offset = BFDEV_BITS_DIV_LONG(shift); + + if (length <= offset) { + memset(dest, 0, length * sizeof(*dest)); + return; + } + + /* length > offset */ + rem = BFDEV_BITS_MOD_LONG(shift); + index = 0; + + while (index < BFDEV_BITS_DIV_LONG(bits) - offset) { + vlow = src[index + offset] >> rem; + + if (rem && index + offset + 1 < length) + vhigh = src[index + offset + 1] << (BFDEV_BITS_PER_LONG - rem); + else + vhigh = 0; + + value = vhigh | vlow; + dest[index++] = value; + } + + if (BFDEV_BITS_MOD_LONG(bits)) { + vlow = src[index] << rem; + + if (rem && index + offset + 1 < length) + vhigh = src[index + offset + 1] << (BFDEV_BITS_PER_LONG - rem); + else + vhigh = 0; + + value = vhigh | vlow; + dest[index + offset] = value & BFDEV_BIT_LOW_MASK(bits); + } + + if (offset) + memset(dest + length - offset, 0, offset * sizeof(*dest)); +} + export void bfdev_bitmap_comp_complement(unsigned long *dest, const unsigned long *src, unsigned int bits) From 177bc078693231ccdaf6ca2e2f958060a4cfd66e Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 16 Feb 2024 17:18:05 +0800 Subject: [PATCH 107/119] feat mpi: added left/right logic shift functions Signed-off-by: John Sanpe --- include/bfdev/mpi.h | 11 +++++++ src/mpi.c | 75 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index 71579c0a..f9062630 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -96,6 +96,17 @@ extern int __bfdev_nonnull(1, 2, 3) bfdev_mpi_xor(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); +extern int __bfdev_nonnull(1, 2) +bfdev_mpi_shli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift); + +extern int __bfdev_nonnull(1, 2) +bfdev_mpi_shri(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift); + +extern bool __bfdev_nonnull(1) +bfdev_mpi_btesti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); + extern int __bfdev_nonnull(1) bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); diff --git a/src/mpi.c b/src/mpi.c index 56645e15..21e4b3cc 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -1400,6 +1400,79 @@ bfdev_mpi_xor(bfdev_mpi_t *dest, return -BFDEV_ENOERR; } +export int +bfdev_mpi_shli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length, cnta; + int retval; + + if (dest == va && !shift) + return -BFDEV_ENOERR; + + cnta = mpi_len(va); + length = cnta + BFDEV_BITS_TO_LONG(shift); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + + if (dest != va) { + ptra = mpi_val(va); + mpa_copy(ptrs, ptra, cnta); + } + + bfdev_bitmap_shl(ptrs, ptrs, shift, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +export int +bfdev_mpi_shri(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + int retval; + + if (dest == va && !shift) + return -BFDEV_ENOERR; + + length = mpi_len(va); + + retval = bfdev_array_resize(&dest->value, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + + bfdev_bitmap_shr(ptrs, ptra, shift, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +export bool +bfdev_mpi_btesti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) +{ + BFDEV_MPI_TYPE *base; + unsigned long offset, length; + + offset = BFDEV_BITS_WORD(bit); + length = mpi_len(dest); + + if (offset >= length) + return false; + + base = mpi_val(dest); + return bfdev_bit_test(base, bit); +} + export int bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) { @@ -1427,7 +1500,7 @@ bfdev_mpi_bclri(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit) offset = BFDEV_BITS_WORD(bit); length = mpi_len(dest); - if (offset > length) + if (offset >= length) return -BFDEV_ENOERR; base = mpi_val(dest); From ddd96347de3edd1224913d801415d9524c272c1b Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 18 Feb 2024 09:55:29 +0800 Subject: [PATCH 108/119] feat allocator: added default hooks Signed-off-by: John Sanpe --- include/bfdev/allocator.h | 3 ++ src/allocator.c | 71 ++++++++++++++++++++++----------------- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/include/bfdev/allocator.h b/include/bfdev/allocator.h index 0888c821..9f13aa16 100644 --- a/include/bfdev/allocator.h +++ b/include/bfdev/allocator.h @@ -46,6 +46,9 @@ struct bfdev_alloc_ops { #define BFDEV_DEFINE_ALLOC(name, alloc, realloc, free, pdata) \ bfdev_alloc_t name = BFDEV_ALLOC_INIT(alloc, realloc, free, pdata) +extern bfdev_alloc_ops_t +bfdev_alloc_default; + static inline void bfdev_alloc_init(bfdev_alloc_t *allocator, bfdev_malloc_t alloc, bfdev_realloc_t realloc, bfdev_free_t free, void *pdata) diff --git a/src/allocator.c b/src/allocator.c index dee3bde8..dc7b7cfa 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -7,35 +7,54 @@ #include #include +export bfdev_alloc_ops_t +bfdev_alloc_default; + static __bfdev_always_inline void * -generic_alloc(size_t size) +generic_alloc(size_t size, void *pdata) { - return malloc(size); + if (!bfdev_alloc_default.alloc) + return malloc(size); + + return bfdev_alloc_default.alloc(size, pdata); } static __bfdev_always_inline void * -generic_zalloc(size_t size) +generic_zalloc(size_t size, void *pdata) { - return calloc(1, size); + if (!bfdev_alloc_default.zalloc) + return calloc(1, size); + + return bfdev_alloc_default.zalloc(size, pdata); } static __bfdev_always_inline void * -generic_realloc(const void *block, size_t resize) +generic_realloc(void *block, size_t resize, void *pdata) { - return realloc((void *)block, resize); + if (!bfdev_alloc_default.realloc) + return realloc(block, resize); + + return bfdev_alloc_default.realloc(block, resize, pdata); } static __bfdev_always_inline void -generic_free(const void *block) +generic_free(void *block, void *pdata) { - free((void *)block); + if (!bfdev_alloc_default.free) + return free((void *)block); + + return bfdev_alloc_default.free(block, pdata); } static inline const bfdev_alloc_ops_t * -alloc_check(const bfdev_alloc_t *alloc) +alloc_check(const bfdev_alloc_t *alloc, void **pdata) { - if (!alloc) + if (!alloc) { + *pdata = NULL; return NULL; + } + + *pdata = alloc->pdata; return alloc->ops; } @@ -48,13 +67,11 @@ bfdev_malloc(const bfdev_alloc_t *alloc, size_t size) if (bfdev_unlikely(!size)) return NULL; - ops = alloc_check(alloc); + ops = alloc_check(alloc, &pdata); if (!ops || !ops->alloc) - retval = generic_alloc(size); - else { - pdata = alloc->pdata; + retval = generic_alloc(size, pdata); + else retval = ops->alloc(size, pdata); - } return retval; } @@ -68,13 +85,11 @@ bfdev_zalloc(const bfdev_alloc_t *alloc, size_t size) if (bfdev_unlikely(!size)) return NULL; - ops = alloc_check(alloc); + ops = alloc_check(alloc, &pdata); if (!ops || !ops->zalloc) - retval = generic_zalloc(size); - else { - pdata = alloc->pdata; + return generic_zalloc(size, pdata); + else retval = ops->zalloc(size, pdata); - } return retval; } @@ -93,13 +108,11 @@ bfdev_realloc(const bfdev_alloc_t *alloc, const void *block, size_t resize) return NULL; } - ops = alloc_check(alloc); + ops = alloc_check(alloc, &pdata); if (!ops || !ops->realloc) - retval = generic_realloc((void *)block, resize); - else { - pdata = alloc->pdata; + retval = generic_realloc((void *)block, resize, pdata); + else retval = ops->realloc((void *)block, resize, pdata); - } return retval; } @@ -113,11 +126,9 @@ bfdev_free(const bfdev_alloc_t *alloc, const void *block) if (!block) return; - ops = alloc_check(alloc); + ops = alloc_check(alloc, &pdata); if (!ops || !ops->free) - generic_free(block); - else { - pdata = alloc->pdata; + generic_free((void *)block, pdata); + else ops->free((void *)block, pdata); - } } From dff4d4aff4e55084a0d97e1ee822d674ab4a957d Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Sun, 18 Feb 2024 12:28:42 +0800 Subject: [PATCH 109/119] feat mpi: added result output function for bbp/machine examples Signed-off-by: John Sanpe --- examples/mpi/bbp.c | 16 +++++++++++++++- examples/mpi/machin.c | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/examples/mpi/bbp.c b/examples/mpi/bbp.c index 4ea1da38..7c1f4fd8 100644 --- a/examples/mpi/bbp.c +++ b/examples/mpi/bbp.c @@ -10,8 +10,10 @@ #include #include #include "../time.h" +#include "helper.h" #define TEST_LEN 10000 +#define PRINT_RESULT 0 static BFDEV_MPI_TYPE factor1[] = { @@ -36,7 +38,7 @@ int main(int argc, const char *argv[]) (vq = bfdev_mpi_create(NULL)))) return 1; - bfdev_log_info("Calculate BBP %d:\n", TEST_LEN); + bfdev_log_info("Convergence BBP %d:\n", TEST_LEN); EXAMPLE_TIME_STATISTICAL( for (i = 0; i < 4; ++i) { retval = bfdev_mpi_seti(vv[i], 0); @@ -90,6 +92,18 @@ int main(int argc, const char *argv[]) 0; ); +#if PRINT_RESULT + char *result; + + result = print_num(vq, 16); + if (!result) + return 1; + + printf("%c.", *result); + puts(result + 1); + free(result); +#endif + bfdev_mpi_destory(vv[0]); bfdev_mpi_destory(vv[1]); bfdev_mpi_destory(vv[2]); diff --git a/examples/mpi/machin.c b/examples/mpi/machin.c index 792dce15..25262a4c 100644 --- a/examples/mpi/machin.c +++ b/examples/mpi/machin.c @@ -10,10 +10,12 @@ #include #include #include "../time.h" +#include "helper.h" #define TEST_LEN 10000 #define TEST_SIZE (TEST_LEN / 4 + 1) #define TEST_LOOP (TEST_LEN / 1.39793 + 1) +#define PRINT_RESULT 0 int main(int argc, const char *argv[]) { @@ -47,7 +49,7 @@ int main(int argc, const char *argv[]) return retval; } - bfdev_log_info("Calculate Machin %d:\n", TEST_LEN); + bfdev_log_info("Convergence Machin %d:\n", TEST_LEN); EXAMPLE_TIME_STATISTICAL( for (k = 1; k <= TEST_LOOP; ++k) { if ((retval = bfdev_mpi_divi(vw, vw, vw, 5 * 5)) || @@ -67,6 +69,18 @@ int main(int argc, const char *argv[]) 0; ); +#if PRINT_RESULT + char *result; + + result = print_num(vs, 10); + if (!result) + return 1; + + printf("%c.", *result); + puts(result + 1); + free(result); +#endif + bfdev_mpi_destory(vw); bfdev_mpi_destory(vs); bfdev_mpi_destory(vv); From 72ff77ed127bfcde614511e3839c2a57d28327e4 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 19 Feb 2024 08:57:10 +0800 Subject: [PATCH 110/119] fixup llist: used try-cmpxchg function Signed-off-by: John Sanpe --- src/llist.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/llist.c b/src/llist.c index d59a5ecd..1f309485 100644 --- a/src/llist.c +++ b/src/llist.c @@ -11,31 +11,33 @@ export bool bfdev_llist_split(bfdev_slist_head_t *head, bfdev_slist_head_t *node, bfdev_slist_head_t *end) { - bfdev_slist_head_t *first; + bfdev_slist_head_t *entry; - do first = end->next = BFDEV_READ_ONCE(head->next); - while ((void *)bfdev_cmpxchg( - (bfdev_atomic_t *)&head->next, (bfdev_atomic_t)first, - (bfdev_atomic_t)node) != first + entry = BFDEV_READ_ONCE(head->next); + do { + end->next = entry; + } while (!bfdev_try_cmpxchg( + (bfdev_atomic_t *)&head->next, (bfdev_atomic_t *)&entry, + (bfdev_atomic_t)node) ); - return !first; + return !entry; } export bfdev_slist_head_t * bfdev_llist_del(bfdev_slist_head_t *head) { - bfdev_slist_head_t *old, *next, *entry; + bfdev_slist_head_t *next, *entry; - for (entry = BFDEV_READ_ONCE(head->next); (old = entry);) { + entry = BFDEV_READ_ONCE(head->next); + do { + if (!entry) + return NULL; next = BFDEV_READ_ONCE(entry->next); - entry = (void *)bfdev_cmpxchg( - (bfdev_atomic_t *)&head->next, (bfdev_atomic_t)old, - (bfdev_atomic_t)next - ); - if (old == entry) - break; - } + } while (!bfdev_try_cmpxchg( + (bfdev_atomic_t *)&head->next, (bfdev_atomic_t *)&entry, + (bfdev_atomic_t)next) + ); return entry; } From e0b8eda30f5d3ea2e7acc2a6b22b29495e3cc4a2 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 19 Feb 2024 13:40:45 +0800 Subject: [PATCH 111/119] feat bug: added initial functions Signed-off-by: John Sanpe --- include/bfdev/bug.h | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 include/bfdev/bug.h diff --git a/include/bfdev/bug.h b/include/bfdev/bug.h new file mode 100644 index 00000000..a0cf4ec5 --- /dev/null +++ b/include/bfdev/bug.h @@ -0,0 +1,64 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef _BFDEV_BUG_H_ +#define _BFDEV_BUG_H_ + +#include +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef BFDEV_BUG_MSG +# define BFDEV_BUG_MSG(msg...) do { \ + bfdev_log_alert(msg); \ + abort(); \ +} while (0) +#endif + +#ifndef BFDEV_WARN_MSG +# define BFDEV_WARN_MSG(msg...) do { \ + bfdev_log_alert(msg); \ +} while (0) +#endif + +#ifndef BFDEV_BUG +# define BFDEV_BUG() \ +BFDEV_BUG_MSG( \ + "Generic BUG: failure at %s:%d, in func '%s'\n", \ + __FILE__, __LINE__, __FUNCTION__ \ +) +#endif + +#ifndef BFDEV_WARN +# define BFDEV_WARN() \ +BFDEV_WARN_MSG( \ + "Generic WARN: failure at %s:%d, in func '%s'\n", \ + __FILE__, __LINE__, __FUNCTION__ \ +) +#endif + +#ifndef BFDEV_BUG_ON +# define BFDEV_BUG_ON(condition) ({ \ + bool __cond = !!(condition); \ + if (bfdev_unlikely(__cond)) \ + BFDEV_BUG(); \ + bfdev_unlikely(__cond); \ +}) +#endif + +#ifndef BFDEV_WARN_ON +# define BFDEV_WARN_ON(condition) ({ \ + bool __cond = !!(condition); \ + if (bfdev_unlikely(__cond)) \ + BFDEV_WARN(); \ + bfdev_unlikely(__cond); \ +}) +#endif + +BFDEV_END_DECLS + +#endif /* _BFDEV_BUG_H_ */ From 46e49e3ae71f1fabfe580dfde9d0a7bba9aee8ac Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Mon, 19 Feb 2024 13:59:02 +0800 Subject: [PATCH 112/119] refactor mpi: separation bit operation framewark Signed-off-by: John Sanpe --- src/mpi.c | 278 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 180 insertions(+), 98 deletions(-) diff --git a/src/mpi.c b/src/mpi.c index 21e4b3cc..856c748c 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -7,10 +7,12 @@ #include #include #include +#include #include #define mpi_len(mpi) bfdev_array_index(&(mpi)->value) #define mpi_val(mpi) bfdev_array_data(&(mpi)->value, 0) +#define mpi_resize(mpi, size) bfdev_array_resize(&(mpi)->value, size) /** * Multi Precision Array Computing. @@ -459,7 +461,7 @@ mpi_extend(bfdev_mpi_t *var, unsigned long length) if (index >= length) return -BFDEV_ENOERR; - retval = bfdev_array_resize(&var->value, length); + retval = mpi_resize(var, length); if (bfdev_unlikely(retval)) return retval; @@ -480,7 +482,7 @@ mpi_copy(bfdev_mpi_t *dest, const bfdev_mpi_t *src) return -BFDEV_EINVAL; length = mpi_len(src); - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_resize(dest, length); if (bfdev_unlikely(retval)) return retval; @@ -530,7 +532,7 @@ mpi_addi(bfdev_mpi_t *dest, length = mpi_len(va); - retval = bfdev_array_resize(&dest->value, length + 1); + retval = mpi_resize(dest, length + 1); if (bfdev_unlikely(retval)) return retval; @@ -563,7 +565,7 @@ mpi_add(bfdev_mpi_t *dest, cnta = mpi_len(va); length = bfdev_max(cnta, cntb); - retval = bfdev_array_resize(&dest->value, length + 1); + retval = mpi_resize(dest, length + 1); if (bfdev_unlikely(retval)) return retval; @@ -588,7 +590,7 @@ mpi_subi(bfdev_mpi_t *dest, length = mpi_len(va); - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_resize(dest, length); if (bfdev_unlikely(retval)) return retval; @@ -623,7 +625,7 @@ mpi_sub(bfdev_mpi_t *dest, if (bfdev_unlikely(cnta < cntb)) return -BFDEV_EOVERFLOW; - retval = bfdev_array_resize(&dest->value, cnta); + retval = mpi_resize(dest, cnta); if (bfdev_unlikely(retval)) return retval; @@ -649,7 +651,7 @@ mpi_muli(bfdev_mpi_t *dest, length = mpi_len(va); - retval = bfdev_array_resize(&dest->value, length + 1); + retval = mpi_resize(dest, length + 1); if (bfdev_unlikely(retval)) return retval; @@ -723,7 +725,7 @@ mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, length = mpi_len(va); - retval = bfdev_array_resize("->value, length); + retval = mpi_resize(quot, length); if (bfdev_unlikely(retval)) return retval; @@ -817,9 +819,7 @@ mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, quot->value = array; } - bfdev_array_resize("->value, length); - bfdev_array_resize(&rem->value, cntb); - + BFDEV_BUG_ON(mpi_resize(rem, cntb)); mpi_relocation(quot); mpi_relocation(rem); @@ -855,12 +855,165 @@ mpi_mod(bfdev_mpi_t *rem, ptra = mpi_val(rem); mpa_divrem(NULL, ptra, ptrb, cnta, cntb); - bfdev_array_resize(&rem->value, cntb); + + BFDEV_BUG_ON(mpi_resize(rem, cntb)); mpi_relocation(rem); return -BFDEV_ENOERR; } +static inline int +mpi_and(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_min(cnta, cntb); + + retval = mpi_resize(dest, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + bfdev_bitmap_and(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_or(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = mpi_resize(dest, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + length = bfdev_min(cnta, cntb); + bfdev_bitmap_or(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + + if (cnta < cntb) { + bfdev_swap(ptra, ptrb); + bfdev_swap(cnta, cntb); + } + + length = cnta - cntb; + mpa_copy(ptrs + cntb, ptra + cntb, length); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_xor(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, const bfdev_mpi_t *vb) +{ + BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; + unsigned long cnta, cntb, length; + int retval; + + cnta = mpi_len(va); + cntb = mpi_len(vb); + length = bfdev_max(cnta, cntb); + + retval = mpi_resize(dest, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + ptrb = mpi_val(vb); + + length = bfdev_min(cnta, cntb); + bfdev_bitmap_xor(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); + + if (cnta < cntb) { + bfdev_swap(ptra, ptrb); + bfdev_swap(cnta, cntb); + } + + length = cnta - cntb; + mpa_copy(ptrs + cntb, ptra + cntb, length); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_shli(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length, cnta; + int retval; + + if (dest == va && !shift) + return -BFDEV_ENOERR; + + cnta = mpi_len(va); + length = cnta + BFDEV_BITS_TO_LONG(shift); + + retval = mpi_resize(dest, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + + if (dest != va) { + ptra = mpi_val(va); + mpa_copy(ptrs, ptra, cnta); + } + + bfdev_bitmap_shl(ptrs, ptrs, shift, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + +static inline int +mpi_shri(bfdev_mpi_t *dest, + const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) +{ + BFDEV_MPI_TYPE *ptrs, *ptra; + unsigned long length; + int retval; + + if (dest == va && !shift) + return -BFDEV_ENOERR; + + length = mpi_len(va); + + retval = mpi_resize(dest, length); + if (bfdev_unlikely(retval)) + return retval; + + ptrs = mpi_val(dest); + ptra = mpi_val(va); + + bfdev_bitmap_shr(ptrs, ptra, shift, length * BFDEV_MPI_BITS); + mpi_relocation(dest); + + return -BFDEV_ENOERR; +} + /** * Multi Precision Integers (signed). * @@ -1309,24 +1462,13 @@ export int bfdev_mpi_and(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { - BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; - unsigned long cnta, cntb, length; int retval; - cnta = mpi_len(va); - cntb = mpi_len(vb); - length = bfdev_min(cnta, cntb); - - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_and(dest, va, vb); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - ptra = mpi_val(va); - ptrb = mpi_val(vb); - - bfdev_bitmap_and(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); - mpi_relocation(dest); + dest->plus = true; return -BFDEV_ENOERR; } @@ -1335,32 +1477,13 @@ export int bfdev_mpi_or(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { - BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; - unsigned long cnta, cntb, length; int retval; - cnta = mpi_len(va); - cntb = mpi_len(vb); - length = bfdev_max(cnta, cntb); - - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_or(dest, va, vb); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - ptra = mpi_val(va); - ptrb = mpi_val(vb); - - length = bfdev_min(cnta, cntb); - bfdev_bitmap_or(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); - - if (cnta < cntb) { - bfdev_swap(ptra, ptrb); - bfdev_swap(cnta, cntb); - } - - length = cnta - cntb; - mpa_copy(ptrs + cntb, ptra + cntb, length); + dest->plus = true; return -BFDEV_ENOERR; } @@ -1369,33 +1492,13 @@ export int bfdev_mpi_xor(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb) { - BFDEV_MPI_TYPE *ptrs, *ptra, *ptrb; - unsigned long cnta, cntb, length; int retval; - cnta = mpi_len(va); - cntb = mpi_len(vb); - length = bfdev_max(cnta, cntb); - - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_xor(dest, va, vb); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - ptra = mpi_val(va); - ptrb = mpi_val(vb); - - length = bfdev_min(cnta, cntb); - bfdev_bitmap_xor(ptrs, ptra, ptrb, length * BFDEV_MPI_BITS); - - if (cnta < cntb) { - bfdev_swap(ptra, ptrb); - bfdev_swap(cnta, cntb); - } - - length = cnta - cntb; - mpa_copy(ptrs + cntb, ptra + cntb, length); - mpi_relocation(dest); + dest->plus = true; return -BFDEV_ENOERR; } @@ -1404,29 +1507,16 @@ export int bfdev_mpi_shli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) { - BFDEV_MPI_TYPE *ptrs, *ptra; - unsigned long length, cnta; int retval; - if (dest == va && !shift) - return -BFDEV_ENOERR; - - cnta = mpi_len(va); - length = cnta + BFDEV_BITS_TO_LONG(shift); + if (dest == va && !shift) + return -BFDEV_ENOERR; - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_shli(dest, va, shift); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - - if (dest != va) { - ptra = mpi_val(va); - mpa_copy(ptrs, ptra, cnta); - } - - bfdev_bitmap_shl(ptrs, ptrs, shift, length * BFDEV_MPI_BITS); - mpi_relocation(dest); + dest->plus = va->plus; return -BFDEV_ENOERR; } @@ -1435,24 +1525,16 @@ export int bfdev_mpi_shri(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift) { - BFDEV_MPI_TYPE *ptrs, *ptra; - unsigned long length; int retval; - if (dest == va && !shift) - return -BFDEV_ENOERR; - - length = mpi_len(va); + if (dest == va && !shift) + return -BFDEV_ENOERR; - retval = bfdev_array_resize(&dest->value, length); + retval = mpi_shri(dest, va, shift); if (bfdev_unlikely(retval)) return retval; - ptrs = mpi_val(dest); - ptra = mpi_val(va); - - bfdev_bitmap_shr(ptrs, ptra, shift, length * BFDEV_MPI_BITS); - mpi_relocation(dest); + dest->plus = va->plus; return -BFDEV_ENOERR; } @@ -1563,7 +1645,7 @@ bfdev_mpi_write(bfdev_mpi_t *var, const BFDEV_MPI_TYPE *buffer, BFDEV_MPI_TYPE *data; int retval; - retval = bfdev_array_resize(&var->value, length); + retval = mpi_resize(var, length); if (bfdev_unlikely(retval)) return retval; From f31f662552192638295f1aedcf108310b991599f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 20 Feb 2024 21:07:27 +0800 Subject: [PATCH 113/119] refactor notifier: optimize functions style Signed-off-by: John Sanpe --- include/bfdev/notifier.h | 32 ++++++++++++++++++++++++-------- src/notifier.c | 11 ++++++++--- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/bfdev/notifier.h b/include/bfdev/notifier.h index 87bcc0d9..f9689bf4 100644 --- a/include/bfdev/notifier.h +++ b/include/bfdev/notifier.h @@ -16,8 +16,8 @@ typedef struct bfdev_notifier_head bfdev_notifier_head_t; typedef struct bfdev_notifier_node bfdev_notifier_node_t; typedef enum bfdev_notifier_ret bfdev_notifier_ret_t; -typedef bfdev_notifier_ret_t (*bfdev_notifier_entry_t) -(bfdev_notifier_node_t *node, void *args); +typedef bfdev_notifier_ret_t +(*bfdev_notifier_entry_t)(void *args, void *pdata); enum bfdev_notifier_ret { __BFDEV_NOTIFI_RET_REMOVE = 0, @@ -31,14 +31,15 @@ enum bfdev_notifier_ret { /** * struct bfdev_notifier_node - describe a notification chain node. * @list: node for hanging notification chain. - * @entry: callback function of notification chain. * @priority: priority of notification chain node. + * @entry: callback function of notification chain. * @pdata: private data of notification chain node. */ struct bfdev_notifier_node { bfdev_ilist_node_t list; - bfdev_notifier_entry_t entry; int priority; + + bfdev_notifier_entry_t entry; void *pdata; }; @@ -52,8 +53,21 @@ struct bfdev_notifier_head { const char *name; }; -#define bfdev_ilist_to_notifier(ptr) \ - bfdev_ilist_entry(ptr, bfdev_notifier_node_t, list) +#define BFDEV_NOTIFIER_STATIC(HEAD, NAME) { \ + BFDEV_ILIST_HEAD_STATIC((HEAD).node), .name = (NAME) \ +} + +#define BFDEV_NOTIFIER_INIT(head, name) \ + (bfdev_notifier_head_t) BFDEV_NOTIFIER_STATIC(head, name) + +#define BFDEV_DEFINE_NOTIFIER(head, name) \ + bfdev_notifier_head_t head = BFDEV_NOTIFIER_INIT(head, name) + +static inline void +bfdev_notifier_init(bfdev_notifier_head_t *head, const char *name) +{ + *head = BFDEV_NOTIFIER_INIT(*head, name); +} /** * bfdev_notifier_call - call the callback function of node on notification chain. @@ -72,7 +86,8 @@ bfdev_notifier_call(bfdev_notifier_head_t *head, void *args, * @node: notification chain node to register. */ extern int -bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node); +bfdev_notifier_register(bfdev_notifier_head_t *head, + bfdev_notifier_node_t *node); /** * bfdev_notifier_unregister - unregister a node from the notification chain. @@ -80,7 +95,8 @@ bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node * @node: notification chain node to unregister. */ extern void -bfdev_notifier_unregister(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node); +bfdev_notifier_unregister(bfdev_notifier_head_t *head, + bfdev_notifier_node_t *node); BFDEV_BEGIN_DECLS diff --git a/src/notifier.c b/src/notifier.c index 307de638..25f4e462 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -11,6 +11,9 @@ #include #include +#define bfdev_ilist_to_notifier(ptr) \ + bfdev_ilist_entry(ptr, bfdev_notifier_node_t, list) + static long notifier_chain_cmp(const bfdev_ilist_node_t *node1, const bfdev_ilist_node_t *node2, void *pdata) @@ -39,7 +42,7 @@ bfdev_notifier_call(bfdev_notifier_head_t *head, void *arg, break; bfdev_log_debug("chain '%s' calling (%p)\n", head->name, node); - retval = node->entry(node, arg); + retval = node->entry(arg, node->pdata); if (called_num) (*called_num)++; @@ -57,7 +60,8 @@ bfdev_notifier_call(bfdev_notifier_head_t *head, void *arg, } export int -bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node) +bfdev_notifier_register(bfdev_notifier_head_t *head, + bfdev_notifier_node_t *node) { if (!node->entry) return -BFDEV_EINVAL; @@ -70,7 +74,8 @@ bfdev_notifier_register(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node } export void -bfdev_notifier_unregister(bfdev_notifier_head_t *head, bfdev_notifier_node_t *node) +bfdev_notifier_unregister(bfdev_notifier_head_t *head, + bfdev_notifier_node_t *node) { bfdev_ilist_del(&head->node, &node->list); bfdev_log_debug("chain '%s' unregister (%p)\n", head->name, node); From 7afeb0e55a4c88d0f67e9507de7326f944ad4820 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 20 Feb 2024 21:08:02 +0800 Subject: [PATCH 114/119] feat notifier: added simple example Signed-off-by: John Sanpe --- examples/CMakeLists.txt | 1 + examples/notifier/.gitignore | 2 ++ examples/notifier/CMakeLists.txt | 22 +++++++++++++++ examples/notifier/simple.c | 48 ++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 examples/notifier/.gitignore create mode 100644 examples/notifier/CMakeLists.txt create mode 100644 examples/notifier/simple.c diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a0cf58c1..a4f1c612 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,6 +30,7 @@ add_subdirectory(log2) add_subdirectory(matrix) add_subdirectory(minpool) add_subdirectory(mpi) +add_subdirectory(notifier) add_subdirectory(once) add_subdirectory(radix) add_subdirectory(rbtree) diff --git a/examples/notifier/.gitignore b/examples/notifier/.gitignore new file mode 100644 index 00000000..2bd8cbf9 --- /dev/null +++ b/examples/notifier/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/notifier-simple diff --git a/examples/notifier/CMakeLists.txt b/examples/notifier/CMakeLists.txt new file mode 100644 index 00000000..e8cc2218 --- /dev/null +++ b/examples/notifier/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later */ +# +# Copyright(c) 2024 John Sanpe +# + +add_executable(notifier-simple simple.c) +target_link_libraries(notifier-simple bfdev) +add_test(notifier-simple notifier-simple) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + simple.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/notifier + ) + + install(TARGETS + notifier-simple + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/notifier/simple.c b/examples/notifier/simple.c new file mode 100644 index 00000000..0bdc4df6 --- /dev/null +++ b/examples/notifier/simple.c @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#include +#include + +BFDEV_DEFINE_NOTIFIER(notifer, "test"); + +static bfdev_notifier_ret_t +func(void *arg, void *pdata) +{ + printf("%s: %s\n", (char *)arg, (char *)pdata); + return BFDEV_NOTIFI_RET_DONE; +} + +int main(int argc, const char *argv[]) +{ + bfdev_notifier_node_t node[3]; + int retval; + + node[0].priority = 3; + node[0].entry = func; + node[0].pdata = "priority=3"; + + retval = bfdev_notifier_register(¬ifer, &node[0]); + if (retval) + return retval; + + node[1].priority = 2; + node[1].entry = func; + node[1].pdata = "priority=2"; + + retval = bfdev_notifier_register(¬ifer, &node[1]); + if (retval) + return retval; + + node[2].priority = 1; + node[2].entry = func; + node[2].pdata = "priority=1"; + + retval = bfdev_notifier_register(¬ifer, &node[2]); + if (retval) + return retval; + + return bfdev_notifier_call(¬ifer, "helloworld", -1, NULL); +} From 6f7346bb14c0d192a04768795f31c90206af9fc6 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 22 Feb 2024 17:37:53 +0800 Subject: [PATCH 115/119] fixup bfdev: corrected some global style issues Signed-off-by: John Sanpe --- examples/time.h | 20 +++++++++++++---- include/bfdev/mpi.h | 54 ++++++++++++++++++++++----------------------- src/hashmap.c | 5 ++--- src/heap.c | 6 +++-- src/minpool.c | 6 +++-- src/prandom.c | 46 ++++++++++++++++++++++---------------- src/radix.c | 3 ++- 7 files changed, 82 insertions(+), 58 deletions(-) diff --git a/examples/time.h b/examples/time.h index 40c6500e..2bd02f21 100644 --- a/examples/time.h +++ b/examples/time.h @@ -14,11 +14,23 @@ #include static inline void -time_dump(double ticks, clock_t start, clock_t end, struct tms *stms, struct tms *etms) +time_dump(double ticks, clock_t start, clock_t end, + struct tms *stms, struct tms *etms) { - bfdev_log_info("\treal time: %lf\n", (end - start) / ticks); - bfdev_log_info("\tuser time: %lf\n", (etms->tms_utime - stms->tms_utime) / ticks); - bfdev_log_info("\tkern time: %lf\n", (etms->tms_stime - stms->tms_stime) / ticks); + bfdev_log_info( + "\treal time: %lf\n", + (end - start) / ticks + ); + + bfdev_log_info( + "\tuser time: %lf\n", + (etms->tms_utime - stms->tms_utime) / ticks + ); + + bfdev_log_info( + "\tkern time: %lf\n", + (etms->tms_stime - stms->tms_stime) / ticks + ); } #define EXAMPLE_TIME_STATISTICAL(codeblock...) ({ \ diff --git a/include/bfdev/mpi.h b/include/bfdev/mpi.h index f9062630..00a61767 100644 --- a/include/bfdev/mpi.h +++ b/include/bfdev/mpi.h @@ -26,111 +26,111 @@ struct bfdev_mpi { bool plus; }; -static inline unsigned long __bfdev_nonnull(1) +static inline unsigned long bfdev_mpi_length(const bfdev_mpi_t *mpi) { return bfdev_array_index(&mpi->value); } -static inline size_t __bfdev_nonnull(1) +static inline size_t bfdev_mpi_size(const bfdev_mpi_t *mpi) { return bfdev_array_size(&mpi->value); } -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_cmp(const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1) +extern int bfdev_mpi_cmpi(const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_add(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_sub(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_mul(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3, 4) +extern int bfdev_mpi_div(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_mod(bfdev_mpi_t *rem, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_addi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_subi(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_muli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_divi(bfdev_mpi_t *quot, bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_modi(bfdev_mpi_t *rem, const bfdev_mpi_t *va, BFDEV_MPI_TYPE vi); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_and(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_or(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2, 3) +extern int bfdev_mpi_xor(bfdev_mpi_t *dest, const bfdev_mpi_t *va, const bfdev_mpi_t *vb); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_shli(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_shri(bfdev_mpi_t *dest, const bfdev_mpi_t *va, BFDEV_MPI_TYPE shift); -extern bool __bfdev_nonnull(1) +extern bool bfdev_mpi_btesti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); -extern int __bfdev_nonnull(1) +extern int bfdev_mpi_bseti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); -extern int __bfdev_nonnull(1) +extern int bfdev_mpi_bclri(bfdev_mpi_t *dest, BFDEV_MPI_TYPE bit); -extern int __bfdev_nonnull(1) +extern int bfdev_mpi_seti(bfdev_mpi_t *dest, BFDEV_MPI_TYPE val); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_set(bfdev_mpi_t *dest, const bfdev_mpi_t *src); -extern int __bfdev_nonnull(1, 2, 4) +extern int bfdev_mpi_read(const bfdev_mpi_t *var, BFDEV_MPI_TYPE *buffer, unsigned long length, bool *sign); -extern int __bfdev_nonnull(1, 2) +extern int bfdev_mpi_write(bfdev_mpi_t *var, const BFDEV_MPI_TYPE *buffer, unsigned long length, bool sign); extern bfdev_mpi_t * bfdev_mpi_create(const bfdev_alloc_t *alloc); -extern void __bfdev_nonnull(1) +extern void bfdev_mpi_destory(bfdev_mpi_t *var); BFDEV_END_DECLS diff --git a/src/hashmap.c b/src/hashmap.c index 1714717d..b503f062 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -247,10 +247,9 @@ bfdev_hashmap_find(bfdev_hashmap_t *hashmap, const void *key) unsigned long value; value = hashmap_hash_key(hashmap, key); - if ((exist = hashmap_find_key(hashmap, key, value))) - return exist; + exist = hashmap_find_key(hashmap, key, value); - return NULL; + return exist; } export void diff --git a/src/heap.c b/src/heap.c index f2c91ea1..0cc10baf 100644 --- a/src/heap.c +++ b/src/heap.c @@ -22,11 +22,13 @@ parent_swap(bfdev_heap_root_t *root, bfdev_heap_node_t *parent, shadow = *node; if (parent->left == node) { node->left = parent; - if ((node->right = parent->right)) + node->right = parent->right; + if (node->right) parent->right->parent = node; } else { /* parent->right == node */ node->right = parent; - if ((node->left = parent->left)) + node->left = parent->left; + if (node->left) parent->left->parent = node; } diff --git a/src/minpool.c b/src/minpool.c index d6ea7e93..8dda296e 100644 --- a/src/minpool.c +++ b/src/minpool.c @@ -74,7 +74,8 @@ bfdev_minpool_best_fit(struct bfdev_minpool_head *head, size_t size) size_t walk, bsize = BFDEV_SIZE_MAX; bfdev_list_for_each_entry(node, &head->free_list, free) { - if ((walk = minnode_get_size(node)) >= size) { + walk = minnode_get_size(node); + if (walk >= size) { if (walk == size) return node; else if (walk >= bsize) @@ -95,7 +96,8 @@ bfdev_minpool_worst_fit(struct bfdev_minpool_head *head, size_t size) size_t walk, bsize = BFDEV_SIZE_MIN; bfdev_list_for_each_entry(node, &head->free_list, free) { - if ((walk = minnode_get_size(node)) >= size) { + walk = minnode_get_size(node); + if (walk >= size) { if (walk == head->avail) return node; else if (walk <= bsize) diff --git a/src/prandom.c b/src/prandom.c index a8820be4..33879cbe 100644 --- a/src/prandom.c +++ b/src/prandom.c @@ -8,42 +8,50 @@ #include static __bfdev_always_inline uint32_t -prandom_seed_minimum(uint32_t x, uint32_t m) +seed_minimum(uint32_t x, uint32_t m) { return (x < m) ? x + m : x; } -export uint32_t -bfdev_prandom_value(bfdev_prandom_state_t *pstate) -{ - pstate->s1 = BFDEV_TAUSWORTHE(pstate->s1, 6U, 13U, 4294967294U, 18U); - pstate->s2 = BFDEV_TAUSWORTHE(pstate->s2, 2U, 27U, 4294967288U, 2U); - pstate->s3 = BFDEV_TAUSWORTHE(pstate->s3, 13U, 21U, 4294967280U, 7U); - pstate->s4 = BFDEV_TAUSWORTHE(pstate->s4, 3U, 12U, 4294967168U, 13U); - return pstate->s1 ^ pstate->s2 ^ pstate->s3 ^ pstate->s4; -} - static void -bfdev_prandom_setup(bfdev_prandom_state_t *pstate, uint64_t seed) +prandom_setup(bfdev_prandom_state_t *pstate, uint64_t seed) { seed = bfdev_lower_32_bits((seed >> 32) ^ (seed << 10) ^ seed); - pstate->s1 = prandom_seed_minimum(seed, 2U); - pstate->s2 = prandom_seed_minimum(seed, 8U); - pstate->s3 = prandom_seed_minimum(seed, 16U); - pstate->s4 = prandom_seed_minimum(seed, 128U); + pstate->s1 = seed_minimum(seed, 2U); + pstate->s2 = seed_minimum(seed, 8U); + pstate->s3 = seed_minimum(seed, 16U); + pstate->s4 = seed_minimum(seed, 128U); } static __bfdev_always_inline void -bfdev_prandom_warmup(bfdev_prandom_state_t *pstate) +prandom_warmup(bfdev_prandom_state_t *pstate) { unsigned int count; for (count = 0; count < 10; ++count) bfdev_prandom_value(pstate); } +export uint32_t +bfdev_prandom_value(bfdev_prandom_state_t *pstate) +{ + uint32_t s1, s2, s3, s4; + + s1 = BFDEV_TAUSWORTHE(pstate->s1, 6U, 13U, 4294967294U, 18U); + s2 = BFDEV_TAUSWORTHE(pstate->s2, 2U, 27U, 4294967288U, 2U); + s3 = BFDEV_TAUSWORTHE(pstate->s3, 13U, 21U, 4294967280U, 7U); + s4 = BFDEV_TAUSWORTHE(pstate->s4, 3U, 12U, 4294967168U, 13U); + + pstate->s1 = s1; + pstate->s2 = s2; + pstate->s3 = s3; + pstate->s4 = s4; + + return s1 ^ s2 ^ s3 ^ s4; +} + export void bfdev_prandom_seed(bfdev_prandom_state_t *pstate, uint64_t seed) { - bfdev_prandom_setup(pstate, seed); - bfdev_prandom_warmup(pstate); + prandom_setup(pstate, seed); + prandom_warmup(pstate); } diff --git a/src/radix.c b/src/radix.c index 0f278625..7b6d3416 100644 --- a/src/radix.c +++ b/src/radix.c @@ -204,7 +204,8 @@ radix_destory_recurse(const bfdev_alloc_t *alloc, if (level) { for (index = 0; index < BFDEV_RADIX_ARY; ++index) { - if ((child = node->child[index])) + child = node->child[index]; + if (child) radix_destory_recurse(alloc, child, level - 1); } } From c9e903075526caf3c05e3bdaecea9e2340750606 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 23 Feb 2024 12:49:49 +0800 Subject: [PATCH 116/119] feat log: added level parameter for write function Signed-off-by: John Sanpe --- include/bfdev/log.h | 14 ++++++++++++-- include/port/log.h | 29 +++++++++++++++++++++++++++++ src/log.c | 32 ++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 include/port/log.h diff --git a/include/bfdev/log.h b/include/bfdev/log.h index 58ef401a..5289bb60 100644 --- a/include/bfdev/log.h +++ b/include/bfdev/log.h @@ -24,8 +24,11 @@ BFDEV_BEGIN_DECLS # define BFDEV_LOGLEVEL_MAX BFDEV_LEVEL_DEBUG #endif +typedef struct bfdev_log bfdev_log_t; +typedef struct bfdev_log_message bfdev_log_message_t; + typedef int (*bfdev_log_write_t) -(const char *buff, int size, void *pdata); +(bfdev_log_message_t *msg, void *pdata); enum bfdev_log_flags { __BFDEV_LOG_COLOR = 0, @@ -44,6 +47,12 @@ struct bfdev_log { void *pdata; }; +struct bfdev_log_message { + unsigned int level; + const char *data; + size_t length; +}; + BFDEV_BITFLAGS_STRUCT(bfdev_log, struct bfdev_log, flags ) @@ -58,7 +67,8 @@ BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_log, commit, __BFDEV_LOG_COMMIT ) -extern struct bfdev_log bfdev_log_default; +extern struct bfdev_log +bfdev_log_default; extern unsigned int bfdev_log_level(const char *str, const char **endptr); diff --git a/include/port/log.h b/include/port/log.h new file mode 100644 index 00000000..630c94fa --- /dev/null +++ b/include/port/log.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _LOCAL_PORT_LOG_H_ +#define _LOCAL_PORT_LOG_H_ + +#include +#include + +#ifndef __INSIDE_LOG__ +# error "please don't include this file directly" +#endif + +static inline void +generic_log_write(bfdev_log_message_t *msg) +{ + FILE *file; + + if (msg->level > BFDEV_LEVEL_WARNING) + file = stdout; + else + file = stderr; + + fwrite(msg->data, msg->length, 1, file); +} + +#endif /* _LOCAL_PORT_LOG_H_ */ diff --git a/src/log.c b/src/log.c index 4ec211ed..012304a1 100644 --- a/src/log.c +++ b/src/log.c @@ -6,10 +6,11 @@ #include #include #include +#include #include static const unsigned int -bfdev_level_color[] = { +level_color[] = { [BFDEV_LEVEL_EMERG ] = BFDEV_COLR_RED, [BFDEV_LEVEL_ALERT ] = BFDEV_COLR_DARK_MAGENTA, [BFDEV_LEVEL_CRIT ] = BFDEV_COLR_MAGENTA, @@ -22,7 +23,7 @@ bfdev_level_color[] = { }; static const char * const -bfdev_level_name[] = { +level_name[] = { [BFDEV_LEVEL_EMERG ] = "emerg", [BFDEV_LEVEL_ALERT ] = "alert", [BFDEV_LEVEL_CRIT ] = "crit", @@ -41,11 +42,8 @@ bfdev_log_default = { .flags = BFDEV_LOG_COLOR | BFDEV_LOG_COMMIT, }; -static void -generic_write(const char *buff) -{ - printf("%s", buff); -} +#define __INSIDE_LOG__ +#include static inline char log_get_level(const char *str) @@ -85,8 +83,10 @@ export int bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) { char buff[BFDEV_LOG_BUFF_SIZE]; - int retval, offset = 0; + bfdev_log_message_t msg; unsigned int level; + size_t offset = 0; + int retval; level = bfdev_log_level(fmt, &fmt); if (level >= BFDEV_LEVEL_DEFAULT) @@ -98,7 +98,7 @@ bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) if (bfdev_log_test_commit(log)) { retval = bfdev_scnprintf( buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - "[%s] ", bfdev_level_name[level] + "[%s] ", level_name[level] ); offset += retval; } @@ -106,7 +106,7 @@ bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) if (bfdev_log_test_color(log)) { retval = bfdev_scnprintf( buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - "\e[%dm", bfdev_level_color[level] + "\e[%dm", level_color[level] ); offset += retval; } @@ -115,6 +115,9 @@ bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) buff + offset, BFDEV_LOG_BUFF_SIZE - offset, fmt, args ); + + if (retval < 0) + return retval; offset += retval; if (bfdev_log_test_color(log)) { @@ -125,9 +128,14 @@ bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) offset += retval; } + msg.data = buff; + msg.length = offset; + msg.level = level; + if (log->write) - return log->write(buff, offset, log->pdata); - generic_write(buff); + offset = log->write(&msg, log->pdata); + else + generic_log_write(&msg); return offset; } From 7e40e5f905c8f677dd5dcb7dfb9ac0edb8ab001b Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 23 Feb 2024 19:30:46 +0800 Subject: [PATCH 117/119] fixup types: using unsigned long to express uw On 32-bit computer, the mode will define uw as unsigned int, which will not be able to integrate with bitops. Signed-off-by: John Sanpe --- include/bfdev/types.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/bfdev/types.h b/include/bfdev/types.h index a3ecff23..ac71a647 100644 --- a/include/bfdev/types.h +++ b/include/bfdev/types.h @@ -34,13 +34,12 @@ typedef unsigned bfdev_uqi_t __bfdev_mode(QI); typedef unsigned bfdev_uhi_t __bfdev_mode(HI); typedef unsigned bfdev_usi_t __bfdev_mode(SI); typedef unsigned bfdev_udi_t __bfdev_mode(DI); +typedef unsigned long bfdev_uw_t; #if BFDEV_BITS_PER_LONG == 32 typedef bfdev_uhi_t bfdev_uhw_t; -typedef bfdev_usi_t bfdev_uw_t; #else /* BFDEV_BITS_PER_LONG == 64 */ typedef bfdev_usi_t bfdev_uhw_t; -typedef bfdev_udi_t bfdev_uw_t; #endif typedef int bfdev_state_t; From 8747793ef3fe6266933904e62b2ca13b4f73bde8 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 23 Feb 2024 19:41:54 +0800 Subject: [PATCH 118/119] fixup mpi: added log format prefix Signed-off-by: John Sanpe --- src/mpi.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mpi.c b/src/mpi.c index 856c748c..5424bd41 100644 --- a/src/mpi.c +++ b/src/mpi.c @@ -3,6 +3,9 @@ * Copyright(c) 2024 John Sanpe */ +#define MODULE_NAME "bfdev-mpi" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + #include #include #include From 391541f7f7c8d2e1b947f335c8f5e0e949d4758f Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Fri, 23 Feb 2024 20:10:23 +0800 Subject: [PATCH 119/119] build cmake: update version Signed-off-by: John Sanpe --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ea6c6b9..63ea509b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ # cmake_minimum_required(VERSION 3.12) -project(bfdev VERSION 1.0.0 LANGUAGES C) +project(bfdev VERSION 1.0.1 LANGUAGES C) include(GNUInstallDirs) include(CheckIncludeFiles)