diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index 1bc794c6..2528fd50 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -35,7 +35,6 @@ jobs: cmake -B ${{github.workspace}}/build \ -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install \ -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ - -D CMAKE_C_COMPILER=/usr/local/opt/ccache/libexec/clang \ -D BFDEV_DEVEL=ON - name: make diff --git a/CMakeLists.txt b/CMakeLists.txt index eb50b25b..a64578aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ # cmake_minimum_required(VERSION 3.12) -project(bfdev VERSION 1.0.3 LANGUAGES C) +project(bfdev VERSION 1.0.4 LANGUAGES C) include(GNUInstallDirs) include(CheckIncludeFiles) @@ -32,7 +32,6 @@ set(BFDEV_CONFIGURE ${BFDEV_GENERATED_PATH}/bfdev-config.cmake) include(scripts/hostrule.cmake) include(scripts/packed-header.cmake) -include(scripts/packed-source.cmake) include(scripts/commit.cmake) commit_hash(BFDEV_COMMITID) @@ -54,6 +53,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_DEBUG_MEMALLOC "Dynamic debug memalloc" ON) option(BFDEV_CRC_EXTEND "CRC loop unfolding optimize" ON) if(BFDEV_DEVEL) @@ -74,13 +74,6 @@ packed_header( ${BFDEV_HEADER_PATH}/bfdev ) -packed_source( - ${PROJECT_BINARY_DIR}/bfdev.c - "${BFDEV_LIBRARY_SOURCE}" - "#undef MODULE_NAME\n" - "#undef bfdev_log_fmt\n" -) - macro(bfdev_dependencies target) add_dependencies( ${target} diff --git a/cmake/config.h.in b/cmake/config.h.in index 2ab87a4b..e4e32884 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_DEBUG_MEMALLOC #cmakedefine BFDEV_CRC_EXTEND #define BFDEV_VERSION_CHECK(major, minor, patch) ( \ diff --git a/docs/components.md b/docs/components.md index 8ec0b378..e9691194 100644 --- a/docs/components.md +++ b/docs/components.md @@ -56,7 +56,7 @@ - allocator: Allocation compatibility layer - allocpool: Mempool optimized for allocation performance -- minpool: Simple memory allocator +- memalloc: Memory allocator algorithm ## String Process diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 5e0a745f..a014c358 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,6 +7,7 @@ add_subdirectory(action) add_subdirectory(allocator) add_subdirectory(arc4) add_subdirectory(array) +add_subdirectory(ascii85) add_subdirectory(base32) add_subdirectory(base64) add_subdirectory(bfdev) @@ -28,13 +29,13 @@ add_subdirectory(list) add_subdirectory(log) add_subdirectory(log2) add_subdirectory(matrix) -add_subdirectory(minpool) add_subdirectory(mpi) add_subdirectory(notifier) add_subdirectory(once) add_subdirectory(prandom) add_subdirectory(radix) add_subdirectory(rbtree) +add_subdirectory(respool) add_subdirectory(ringbuf) add_subdirectory(segtree) add_subdirectory(skiplist) diff --git a/examples/ascii85/.gitignore b/examples/ascii85/.gitignore new file mode 100644 index 00000000..22ef9c6f --- /dev/null +++ b/examples/ascii85/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/ascii85-bandwidth diff --git a/examples/ascii85/CMakeLists.txt b/examples/ascii85/CMakeLists.txt new file mode 100644 index 00000000..9b258bbd --- /dev/null +++ b/examples/ascii85/CMakeLists.txt @@ -0,0 +1,28 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2024 John Sanpe +# + +add_executable(ascii85-bandwidth bandwidth.c) +target_link_libraries(ascii85-bandwidth bfdev) +add_test(ascii85-bandwidth ascii85-bandwidth) + +add_executable(ascii85 utils.c) +target_link_libraries(ascii85 bfdev) +add_test(ascii85 ascii85 "helloworld") + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + bandwidth.c + utils.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/ascii85 + ) + + install(TARGETS + ascii85 + ascii85-bandwidth + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/ascii85/bandwidth.c b/examples/ascii85/bandwidth.c new file mode 100644 index 00000000..ed426282 --- /dev/null +++ b/examples/ascii85/bandwidth.c @@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "ascii85-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 + +int main(int argc, char const *argv[]) +{ + unsigned int count, loop; + uint8_t *dbuff, *sbuff; + size_t dlen, slen, index; + uint32_t cksum, check; + + slen = bfdev_ascii85_encode_length(TEST_SIZE); + sbuff = malloc(slen); + if (!sbuff) + return 1; + + dlen = bfdev_ascii85_decode_length(slen); + dbuff = malloc(dlen); + if (!dbuff) + return 1; + + srand(time(NULL)); + for (index = 0; index < dlen; ++index) + dbuff[index] = (uint8_t)rand(); + + cksum = bfdev_crc32(dbuff, dlen, (uint32_t)~0UL); + bfdev_log_info("start checksum: %#010x\n", cksum); + + for (count = 0; count < TEST_LOOP; ++count) { + EXAMPLE_TIME_LOOP(&loop, 1000, + bfdev_ascii85_encode(sbuff, dbuff, &slen, dlen); + 0; + ); + bfdev_log_info("encode bandwidth %u: %uMiB/s\n", count, loop); + + EXAMPLE_TIME_LOOP(&loop, 1000, + bfdev_ascii85_decode(dbuff, sbuff, &dlen, slen); + 0; + ); + bfdev_log_info("decode bandwidth %u: %uMiB/s\n", count, loop); + } + + check = bfdev_crc32(dbuff, dlen, (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; +} diff --git a/examples/ascii85/utils.c b/examples/ascii85/utils.c new file mode 100644 index 00000000..0ad9fa1e --- /dev/null +++ b/examples/ascii85/utils.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "bfdev-ascii85" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include + +int +main(int argc, const char *argv[]) +{ + unsigned int index; + bool decode; + int retval; + + decode = false; + for (index = 1; index < argc; ++index) { + const char *para; + + para = argv[index]; + if (para[0] != '-') + break; + + switch (para[1]) { + case 'd': + decode = true; + break; + + case 'h': default: + goto usage; + } + } + + if (index == argc) + goto usage; + + do { + size_t len1, len2, len3; + const char *data; + char *buff; + + data = argv[index]; + len1 = strlen(data); + + if (decode) + len2 = bfdev_ascii85_decode_length(len1); + else + len2 = bfdev_ascii85_encode_length(len1); + + buff = malloc(len2); + if (!buff) { + bfdev_log_err("Out of memory.\n"); + return 1; + } + + if (!decode) + bfdev_ascii85_encode(buff, data, &len3, len1); + else { + retval = bfdev_ascii85_decode(buff, data, &len3, len1); + if (retval) { + const char *ername, *infop; + + ername = bfdev_errname(retval, &infop); + bfdev_log_err("Decode error: %s (%s).\n", ername, infop); + + return retval; + } + } + + fwrite(buff, 1, len3, stdout); + fputc('\n', stdout); + free(buff); + } while (++index < argc); + + return 0; + +usage: + bfdev_log_err("Usage: %s [-d] data ...\n", argv[0]); + return 1; +} diff --git a/examples/base32/CMakeLists.txt b/examples/base32/CMakeLists.txt index fa0ac4fd..45b2084c 100644 --- a/examples/base32/CMakeLists.txt +++ b/examples/base32/CMakeLists.txt @@ -7,14 +7,20 @@ add_executable(base32-bandwidth bandwidth.c) target_link_libraries(base32-bandwidth bfdev) add_test(base32-bandwidth base32-bandwidth) +add_executable(base32 utils.c) +target_link_libraries(base32 bfdev) +add_test(base32 base32 "helloworld") + if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES bandwidth.c + utils.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/base32 ) install(TARGETS + base32 base32-bandwidth DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/base32/bandwidth.c b/examples/base32/bandwidth.c index ad80a9e3..883f9e45 100644 --- a/examples/base32/bandwidth.c +++ b/examples/base32/bandwidth.c @@ -35,7 +35,7 @@ int main(int argc, char const *argv[]) return 1; srand(time(NULL)); - for (index = 0; index < TEST_SIZE; ++index) + for (index = 0; index < dlen; ++index) dbuff[index] = (uint8_t)rand(); cksum = bfdev_crc32(dbuff, dlen, (uint32_t)~0UL); @@ -43,7 +43,7 @@ int main(int argc, char const *argv[]) for (count = 0; count < TEST_LOOP; ++count) { EXAMPLE_TIME_LOOP(&loop, 1000, - bfdev_base32_encode(sbuff, dbuff, TEST_SIZE); + bfdev_base32_encode(sbuff, dbuff, dlen); 0; ); bfdev_log_info("encode bandwidth %u: %uMiB/s\n", count, loop); diff --git a/examples/base32/utils.c b/examples/base32/utils.c new file mode 100644 index 00000000..1f59a919 --- /dev/null +++ b/examples/base32/utils.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "bfdev-base32" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include + +int +main(int argc, const char *argv[]) +{ + unsigned int index; + bool decode; + int retval; + + decode = false; + for (index = 1; index < argc; ++index) { + const char *para; + + para = argv[index]; + if (para[0] != '-') + break; + + switch (para[1]) { + case 'd': + decode = true; + break; + + case 'h': default: + goto usage; + } + } + + if (index == argc) + goto usage; + + do { + size_t len1, len2; + const char *data; + char *buff; + + data = argv[index]; + len1 = strlen(data); + + if (decode) + len2 = bfdev_base32_decode_length(len1); + else + len2 = bfdev_base32_encode_length(len1); + + buff = malloc(len2); + if (!buff) { + bfdev_log_err("Out of memory.\n"); + return 1; + } + + if (!decode) + bfdev_base32_encode(buff, data, len1); + else { + retval = bfdev_base32_decode(buff, data, len1); + if (retval) { + const char *ername, *infop; + + ername = bfdev_errname(retval, &infop); + bfdev_log_err("Decode error: %s (%s).\n", ername, infop); + + return retval; + } + } + + fwrite(buff, 1, len2, stdout); + fputc('\n', stdout); + free(buff); + } while (++index < argc); + + return 0; + +usage: + bfdev_log_err("Usage: %s [-d] data ...\n", argv[0]); + return 1; +} diff --git a/examples/base64/CMakeLists.txt b/examples/base64/CMakeLists.txt index 2aff05e3..58eaa203 100644 --- a/examples/base64/CMakeLists.txt +++ b/examples/base64/CMakeLists.txt @@ -7,14 +7,20 @@ add_executable(base64-bandwidth bandwidth.c) target_link_libraries(base64-bandwidth bfdev) add_test(base64-bandwidth base64-bandwidth) +add_executable(base64 utils.c) +target_link_libraries(base64 bfdev) +add_test(base64 base64 "helloworld") + if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES bandwidth.c + utils.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/base64 ) install(TARGETS + base64 base64-bandwidth DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/base64/bandwidth.c b/examples/base64/bandwidth.c index 607e43a8..09d21d09 100644 --- a/examples/base64/bandwidth.c +++ b/examples/base64/bandwidth.c @@ -35,7 +35,7 @@ int main(int argc, char const *argv[]) return 1; srand(time(NULL)); - for (index = 0; index < TEST_SIZE; ++index) + for (index = 0; index < dlen; ++index) dbuff[index] = (uint8_t)rand(); cksum = bfdev_crc32(dbuff, dlen, (uint32_t)~0UL); @@ -43,7 +43,7 @@ int main(int argc, char const *argv[]) for (count = 0; count < TEST_LOOP; ++count) { EXAMPLE_TIME_LOOP(&loop, 1000, - bfdev_base64_encode(sbuff, dbuff, TEST_SIZE); + bfdev_base64_encode(sbuff, dbuff, dlen); 0; ); bfdev_log_info("encode bandwidth %u: %uMiB/s\n", count, loop); diff --git a/examples/base64/utils.c b/examples/base64/utils.c new file mode 100644 index 00000000..aed82414 --- /dev/null +++ b/examples/base64/utils.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "bfdev-base64" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include + +int +main(int argc, const char *argv[]) +{ + unsigned int index; + bool decode; + int retval; + + decode = false; + for (index = 1; index < argc; ++index) { + const char *para; + + para = argv[index]; + if (para[0] != '-') + break; + + switch (para[1]) { + case 'd': + decode = true; + break; + + case 'h': default: + goto usage; + } + } + + if (index == argc) + goto usage; + + do { + size_t len1, len2; + const char *data; + char *buff; + + data = argv[index]; + len1 = strlen(data); + + if (decode) + len2 = bfdev_base64_decode_length(len1); + else + len2 = bfdev_base64_encode_length(len1); + + buff = malloc(len2); + if (!buff) { + bfdev_log_err("Out of memory.\n"); + return 1; + } + + if (!decode) + bfdev_base64_encode(buff, data, len1); + else { + retval = bfdev_base64_decode(buff, data, len1); + if (retval) { + const char *ername, *infop; + + ername = bfdev_errname(retval, &infop); + bfdev_log_err("Decode error: %s (%s).\n", ername, infop); + + return retval; + } + } + + fwrite(buff, 1, len2, stdout); + fputc('\n', stdout); + free(buff); + } while (++index < argc); + + return 0; + +usage: + bfdev_log_err("Usage: %s [-d] data ...\n", argv[0]); + return 1; +} diff --git a/examples/fifo/.gitignore b/examples/fifo/.gitignore index d7f3d7b6..7f75332b 100644 --- a/examples/fifo/.gitignore +++ b/examples/fifo/.gitignore @@ -1,3 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-or-later -/fifo-selftest /fifo-atomic diff --git a/examples/fifo/CMakeLists.txt b/examples/fifo/CMakeLists.txt index 3269498b..f2a2e1c1 100644 --- a/examples/fifo/CMakeLists.txt +++ b/examples/fifo/CMakeLists.txt @@ -3,24 +3,18 @@ # Copyright(c) 2023 ffashion # -add_executable(fifo-selftest selftest.c) -target_link_libraries(fifo-selftest bfdev) -add_test(fifo-selftest fifo-selftest) - 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 atomic.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/fifo ) install(TARGETS - fifo-selftest fifo-atomic DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/fifo/selftest.c b/examples/fifo/selftest.c deleted file mode 100644 index 9b1fd5dd..00000000 --- a/examples/fifo/selftest.c +++ /dev/null @@ -1,474 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2022 John Sanpe - */ - -#include -#include -#include -#include - -#define TEST_LOOP 16 - -struct test_pdata { - BFDEV_DECLARE_FIFO(normal_bytetest, char, TEST_LOOP); - BFDEV_DECLARE_FIFO(normal_longtest, long, TEST_LOOP); - BFDEV_DECLARE_FIFO_RECORD(record_bytetest, char, TEST_LOOP, 1); - BFDEV_DECLARE_FIFO_RECORD(record_longtest, long, TEST_LOOP, 1); - BFDEV_DECLARE_FIFO_DYNAMIC(dynamic_bytetest, char); - BFDEV_DECLARE_FIFO_DYNAMIC(dynamic_longtest, long); - BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(dynamic_record_bytetest, char, 1); - BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(dynamic_record_longtest, long, 1); -}; - -static const char -bytetest_table[TEST_LOOP] = { - 'o', 'p', 'e', 'n', 'b', 'f', 'd', 'e', - 'v', ',', 'h', 'e', 'l', 'l', 'l', 'o', -}; - -static const long -longtest_table[TEST_LOOP] = { - (long)0x0000000000000000ULL, (long)0x1111111111111111ULL, - (long)0x2222222222222222ULL, (long)0x3333333333333333ULL, - (long)0x4444444444444444ULL, (long)0x5555555555555555ULL, - (long)0x6666666666666666ULL, (long)0x7777777777777777ULL, - (long)0x8888888888888888ULL, (long)0x9999999999999999ULL, - (long)0xaaaaaaaaaaaaaaaaULL, (long)0xbbbbbbbbbbbbbbbbULL, - (long)0xccccccccccccccccULL, (long)0xddddddddddddddddULL, - (long)0xeeeeeeeeeeeeeeeeULL, (long)0xffffffffffffffffULL, -}; - -static int -fifo_testing(struct test_pdata *pdata) -{ - char bytevalue[TEST_LOOP]; - long longvalue[TEST_LOOP]; - unsigned int count; - unsigned long retval; - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_put(&pdata->normal_bytetest, bytetest_table[count]); - printf("fifo normal_bytetest %u put '%c': ", count, bytetest_table[count]); - if (!retval || bfdev_fifo_len(&pdata->normal_bytetest) != count + 1) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo normal_bytetest check full: "); - if (!bfdev_fifo_check_full(&pdata->normal_bytetest)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_peek(&pdata->normal_bytetest, bytevalue); - printf("fifo normal_bytetest %u peek '%c': ", count, *bytevalue); - if (!retval || *bytevalue != bytetest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - retval = bfdev_fifo_get(&pdata->normal_bytetest, bytevalue); - printf("fifo normal_bytetest %u get '%c': ", count, *bytevalue); - if (!retval || *bytevalue != bytetest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo normal_bytetest copy in: "); - retval = bfdev_fifo_in(&pdata->normal_bytetest, bytetest_table, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_bytetest copy peek out: "); - retval = bfdev_fifo_out_peek(&pdata->normal_bytetest, bytevalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_bytetest check peek out: "); - if (memcmp(bytevalue, bytetest_table, sizeof(bytetest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_bytetest copy out: "); - retval = bfdev_fifo_out(&pdata->normal_bytetest, bytevalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_bytetest check out: "); - if (memcmp(bytevalue, bytetest_table, sizeof(bytetest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - bfdev_fifo_put(&pdata->normal_longtest, longtest_table[count]); - printf("fifo normal_longtest %u put %#lx: ", count, longtest_table[count]); - if (bfdev_fifo_len(&pdata->normal_longtest) != count + 1) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo normal_longtest check full: "); - if (!bfdev_fifo_check_full(&pdata->normal_longtest)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_peek(&pdata->normal_longtest, longvalue); - printf("fifo normal_longtest %u peek %#lx: ", count, *longvalue); - if (!retval || *longvalue != longtest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - retval = bfdev_fifo_get(&pdata->normal_longtest, longvalue); - printf("fifo normal_longtest %u get %#lx: ", count, *longvalue); - if (!retval || *longvalue != longtest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo normal_longtest copy in: "); - retval = bfdev_fifo_in(&pdata->normal_longtest, longtest_table, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_longtest copy peek out: "); - retval = bfdev_fifo_out_peek(&pdata->normal_longtest, longvalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_longtest check peek out: "); - if (memcmp(longvalue, longtest_table, sizeof(longtest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_longtest copy out: "); - retval = bfdev_fifo_out(&pdata->normal_longtest, longvalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_longtest check out: "); - if (memcmp(longvalue, longtest_table, sizeof(longtest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_put(&pdata->dynamic_bytetest, bytetest_table[count]); - printf("fifo dynamic_bytetest %u put '%c': ", count, bytetest_table[count]); - if (!retval || bfdev_fifo_len(&pdata->dynamic_bytetest) != count + 1) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo dynamic_bytetest check full: "); - if (!bfdev_fifo_check_full(&pdata->dynamic_bytetest)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_peek(&pdata->dynamic_bytetest, bytevalue); - printf("fifo dynamic_bytetest %u peek '%c': ", count, *bytevalue); - if (!retval || *bytevalue != bytetest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - retval = bfdev_fifo_get(&pdata->dynamic_bytetest, bytevalue); - printf("fifo dynamic_bytetest %u get '%c': ", count, *bytevalue); - if (!retval || *bytevalue != bytetest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo dynamic_bytetest copy in: "); - retval = bfdev_fifo_in(&pdata->dynamic_bytetest, bytetest_table, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_bytetest copy peek out: "); - retval = bfdev_fifo_out_peek(&pdata->dynamic_bytetest, bytevalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_bytetest check peek out: "); - if (memcmp(bytetest_table, bytetest_table, sizeof(bytetest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_bytetest copy out: "); - retval = bfdev_fifo_out(&pdata->dynamic_bytetest, bytevalue, TEST_LOOP); - if (retval != TEST_LOOP) - return 1; - printf("pass\n"); - - printf("fifo dynamic_bytetest check out: "); - if (memcmp(bytevalue, bytetest_table, sizeof(bytetest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - bfdev_fifo_put(&pdata->dynamic_longtest, longtest_table[count]); - printf("fifo dynamic_longtest %u put %#lx: ", count, longtest_table[count]); - if (bfdev_fifo_len(&pdata->dynamic_longtest) != count + 1) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo dynamic_longtest check full: "); - if (!bfdev_fifo_check_full(&pdata->dynamic_longtest)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 0; count < TEST_LOOP; ++count) { - retval = bfdev_fifo_peek(&pdata->dynamic_longtest, longvalue); - printf("fifo dynamic_longtest %u peek %#lx: ", count, *longvalue); - if (!retval || *longvalue != longtest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - retval = bfdev_fifo_get(&pdata->dynamic_longtest, longvalue); - printf("fifo dynamic_longtest %u get %#lx: ", count, *longvalue); - if (!retval || *longvalue != longtest_table[count]) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - printf("fifo dynamic_longtest copy in: "); - retval = bfdev_fifo_in(&pdata->dynamic_longtest, longtest_table, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_longtest copy peek out: "); - retval = bfdev_fifo_out_peek(&pdata->dynamic_longtest, longvalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_longtest check peek out: "); - if (memcmp(longvalue, longtest_table, sizeof(longtest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_longtest copy out: "); - retval = bfdev_fifo_out(&pdata->dynamic_longtest, longvalue, TEST_LOOP); - if (retval != TEST_LOOP) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo normal_longtest check out: "); - if (memcmp(longvalue, longtest_table, sizeof(longtest_table))) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - for (count = 1; count < TEST_LOOP; ++count) { - printf("fifo record_bytetest copy %u in: ", count); - retval = bfdev_fifo_in(&pdata->record_bytetest, bytetest_table, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo record_bytetest copy %u out: ", count); - retval = bfdev_fifo_out(&pdata->record_bytetest, bytevalue, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo record_bytetest check %u copy: ", count); - if (memcmp(bytevalue, bytetest_table, count)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - for (count = 1; count < TEST_LOOP; ++count) { - printf("fifo record_longtest copy %u in: ", count); - retval = bfdev_fifo_in(&pdata->record_longtest, longtest_table, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo record_longtest copy %u out: ", count); - retval = bfdev_fifo_out(&pdata->record_longtest, longvalue, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo record_longtest check %u copy: ", count); - if (memcmp(longvalue, longtest_table, count)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - for (count = 1; count < TEST_LOOP; ++count) { - printf("fifo dynamic_record_bytetest copy %u in: ", count); - retval = bfdev_fifo_in(&pdata->dynamic_record_bytetest, bytetest_table, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_record_bytetest copy %u out: ", count); - retval = bfdev_fifo_out(&pdata->dynamic_record_bytetest, bytevalue, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_record_bytetest check %u copy: ", count); - if (memcmp(bytevalue, bytetest_table, count)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - for (count = 1; count < TEST_LOOP; ++count) { - printf("fifo dynamic_record_longtest copy %u in: ", count); - retval = bfdev_fifo_in(&pdata->dynamic_record_longtest, longtest_table, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_record_longtest copy %u out: ", count); - retval = bfdev_fifo_out(&pdata->dynamic_record_longtest, longvalue, count); - if (retval != count) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - - printf("fifo dynamic_record_longtest check %u copy: ", count); - if (memcmp(longvalue, longtest_table, count)) { - printf("failed\n"); - return 1; - } - printf("pass\n"); - } - - return 0; -} - -int main(int argc, const char *argv[]) -{ - struct test_pdata *pdata; - int retval; - - pdata = malloc(sizeof(struct test_pdata)); - if (!pdata) - return 1; - - pdata->normal_bytetest = BFDEV_FIFO_INIT(pdata->normal_bytetest); - pdata->normal_longtest = BFDEV_FIFO_INIT(pdata->normal_longtest); - pdata->record_bytetest = BFDEV_FIFO_INIT(pdata->record_bytetest); - pdata->record_longtest = BFDEV_FIFO_INIT(pdata->record_longtest); - - if ((retval = bfdev_fifo_alloc(&pdata->dynamic_bytetest, NULL, TEST_LOOP)) || - (retval = bfdev_fifo_alloc(&pdata->dynamic_longtest, NULL, TEST_LOOP)) || - (retval = bfdev_fifo_alloc(&pdata->dynamic_record_bytetest, NULL, TEST_LOOP)) || - (retval = bfdev_fifo_alloc(&pdata->dynamic_record_longtest, NULL, TEST_LOOP))) - return 1; - - retval = fifo_testing(pdata); - if (retval) - return retval; - - bfdev_fifo_free(&pdata->dynamic_bytetest); - bfdev_fifo_free(&pdata->dynamic_longtest); - bfdev_fifo_free(&pdata->dynamic_record_bytetest); - bfdev_fifo_free(&pdata->dynamic_record_longtest); - free(pdata); - - return 0; -} diff --git a/examples/hlist/.gitignore b/examples/hlist/.gitignore index 7f7e4e0f..35cd147a 100644 --- a/examples/hlist/.gitignore +++ b/examples/hlist/.gitignore @@ -1,3 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-or-later -/hlist-selftest /hlist-simple diff --git a/examples/hlist/CMakeLists.txt b/examples/hlist/CMakeLists.txt index 5e2ef655..d7ebd899 100644 --- a/examples/hlist/CMakeLists.txt +++ b/examples/hlist/CMakeLists.txt @@ -3,24 +3,18 @@ # Copyright(c) 2023 ffashion # -add_executable(hlist-selftest selftest.c) -target_link_libraries(hlist-selftest bfdev) -add_test(hlist-selftest hlist-selftest) - add_executable(hlist-simple simple.c) target_link_libraries(hlist-simple bfdev) add_test(hlist-simple hlist-simple) if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") install(FILES - selftest.c simple.c DESTINATION ${CMAKE_INSTALL_DOCDIR}/examples/hlist ) install(TARGETS - hlist-selftest hlist-simple DESTINATION ${CMAKE_INSTALL_DOCDIR}/bin diff --git a/examples/hlist/selftest.c b/examples/hlist/selftest.c deleted file mode 100644 index 79ac926d..00000000 --- a/examples/hlist/selftest.c +++ /dev/null @@ -1,130 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2021 John Sanpe - */ - -#include -#include -#include -#include - -#define TEST_LOOP 10 - -struct test_node { - bfdev_hlist_node_t list; - unsigned long num; -}; - -struct test_pdata { - struct test_node nodes[TEST_LOOP]; -}; - -#define bfdev_hlist_to_test(ptr) \ - bfdev_hlist_entry(ptr, struct test_node, list) - -static int -bfdev_hlist_selftest(struct test_pdata *hdata) -{ - struct test_node *node, *nnode, *tnode; - bfdev_hlist_node_t *list, *nlist, *tlist; - unsigned int count; - - BFDEV_HLIST_HEAD(test_head); - - for (count = 0; count < BFDEV_ARRAY_SIZE(hdata->nodes); ++count) { - bfdev_hlist_head_add(&test_head, &hdata->nodes[count].list); - } - - bfdev_hlist_for_each(list, &test_head) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each' test: %lu\n", node->num); - if (node->num == TEST_LOOP / 2) - break; - } - - tlist = list; - bfdev_hlist_for_each_continue(list) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each_continue' test: %lu\n", node->num); - } - - list = tlist; - bfdev_hlist_for_each_from(list) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each_from' test: %lu\n", node->num); - } - - bfdev_hlist_for_each_safe(list, nlist, &test_head) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each_safe' test: %lu\n", node->num); - if (node->num == TEST_LOOP / 2) - break; - } - - tlist = list; - bfdev_hlist_for_each_continue_safe(list, nlist) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each_continue_safe' test: %lu\n", node->num); - } - - list = tlist; - bfdev_hlist_for_each_from_safe(list, nlist) { - node = bfdev_hlist_to_test(list); - printf("hlist 'bfdev_hlist_for_each_from_safe' test: %lu\n", node->num); - } - - bfdev_hlist_for_each_entry(node, &test_head, list) { - printf("hlist 'bfdev_hlist_for_each_entry' test: %lu\n", node->num); - if (node->num == TEST_LOOP / 2) - break; - } - - tnode = node; - bfdev_hlist_for_each_entry_continue(node, list) { - printf("hlist 'bfdev_hlist_for_each_entry_continue' test: %lu\n", node->num); - } - - node = tnode; - bfdev_hlist_for_each_entry_from(node, list) { - printf("hlist 'bfdev_hlist_for_each_entry_from' test: %lu\n", node->num); - } - - bfdev_hlist_for_each_entry_safe(node, nnode, &test_head, list) { - printf("hlist 'bfdev_hlist_for_each_entry_safe' test: %lu\n", node->num); - if (node->num == TEST_LOOP / 2) - break; - bfdev_hlist_del(&node->list); - } - - tnode = node; - bfdev_hlist_for_each_entry_continue_safe(node, nnode, list) { - printf("hlist 'bfdev_hlist_for_each_entry_continue_safe' test: %lu\n", node->num); - } - - node = tnode; - bfdev_hlist_for_each_entry_from_safe(node, nnode, list) { - printf("hlist 'bfdev_hlist_for_each_entry_from_safe' test: %lu\n", node->num); - bfdev_hlist_del(&node->list); - } - - return 0; -} - -int main(int argc, const char *argv[]) -{ - struct test_pdata *hdata; - unsigned int count; - int retval; - - hdata = malloc(sizeof(struct test_pdata)); - if ((retval = !hdata)) - return retval; - - for (count = 0; count < BFDEV_ARRAY_SIZE(hdata->nodes); ++count) - hdata->nodes[count].num = TEST_LOOP - count - 1; - - retval = bfdev_hlist_selftest(hdata); - free(hdata); - - return retval; -} diff --git a/examples/minpool/CMakeLists.txt b/examples/minpool/CMakeLists.txt deleted file mode 100644 index 83afa145..00000000 --- a/examples/minpool/CMakeLists.txt +++ /dev/null @@ -1,22 +0,0 @@ -# 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 deleted file mode 100644 index ffd0f0b5..00000000 --- a/examples/minpool/selftest.c +++ /dev/null @@ -1,105 +0,0 @@ -/* 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; -} diff --git a/examples/notifier/simple.c b/examples/notifier/simple.c index 0bdc4df6..4b246ed8 100644 --- a/examples/notifier/simple.c +++ b/examples/notifier/simple.c @@ -3,15 +3,20 @@ * Copyright(c) 2023 John Sanpe */ +#define MODULE_NAME "notifier-simple" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + #include +#include #include -BFDEV_DEFINE_NOTIFIER(notifer, "test"); +static +BFDEV_DEFINE_NOTIFIER(notifer); static bfdev_notifier_ret_t func(void *arg, void *pdata) { - printf("%s: %s\n", (char *)arg, (char *)pdata); + bfdev_log_info("%s: %s\n", (char *)arg, (char *)pdata); return BFDEV_NOTIFI_RET_DONE; } diff --git a/examples/minpool/.gitignore b/examples/respool/.gitignore similarity index 70% rename from examples/minpool/.gitignore rename to examples/respool/.gitignore index 4477d53a..d983f891 100644 --- a/examples/minpool/.gitignore +++ b/examples/respool/.gitignore @@ -1,2 +1,2 @@ # SPDX-License-Identifier: GPL-2.0-or-later -/minpool-selftest +/respool-simple diff --git a/examples/respool/CMakeLists.txt b/examples/respool/CMakeLists.txt new file mode 100644 index 00000000..677ce78e --- /dev/null +++ b/examples/respool/CMakeLists.txt @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 John Sanpe +# + +add_executable(respool-simple simple.c) +target_link_libraries(respool-simple bfdev) +add_test(respool-simple respool-simple) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(FILES + simple.c + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/examples/respool + ) + + install(TARGETS + respool-simple + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/bin + ) +endif() diff --git a/examples/respool/simple.c b/examples/respool/simple.c new file mode 100644 index 00000000..78f9bd50 --- /dev/null +++ b/examples/respool/simple.c @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define MODULE_NAME "respool-simple" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include + +#define TEST_LOOP 16 +#define TEST_SIZE 4096 + +struct test_struct { + bfdev_respool_node_t node; + unsigned int count; + void *data; +}; + +static void +cleanup(bfdev_respool_node_t *node, void *pdata) +{ + struct test_struct *test; + + test = bfdev_container_of(node, struct test_struct, node); + bfdev_log_info("release %d: %p\n", test->count, test->data); + + free(test->data); + free(test); +} + +int main(int argc, char *argv[]) +{ + BFDEV_DEFINE_RESPOOL(respool); + struct test_struct *test; + unsigned int count; + + for (count = 0; count < TEST_LOOP; ++count) { + test = malloc(sizeof(*test)); + if (!test) + return 1; + + test->data = malloc(TEST_SIZE); + if (!test->data) + return 1; + + test->count = count; + test->node.release = cleanup; + + bfdev_log_info("insert %d: %p\n", count, test->data); + bfdev_respool_insert(&respool, &test->node); + } + + bfdev_respool_release_all(&respool, NULL); + + return 0; +} diff --git a/examples/textsearch/simple.c b/examples/textsearch/simple.c index de5ee61a..95a7fd00 100644 --- a/examples/textsearch/simple.c +++ b/examples/textsearch/simple.c @@ -27,7 +27,7 @@ test_textsearch(const char *algo) return 1; } - offset = bfdev_textsearch_linear_find( + offset = bfdev_textsearch_linear( context, &linear, TEST_STRING, sizeof(TEST_STRING) - 1 ); diff --git a/include/bfdev/array.h b/include/bfdev/array.h index 1095cdaa..e9cb1793 100644 --- a/include/bfdev/array.h +++ b/include/bfdev/array.h @@ -14,6 +14,14 @@ BFDEV_BEGIN_DECLS +/** + * Array: + * + * The Array container provides a simple dynamically resizing + * array, with separate tracking of capacity and usage. This + * is typically useful when buffering I/O or processing data. + */ + #ifndef BFDEV_ARRAY_MSIZE # define BFDEV_ARRAY_MSIZE 32 #endif @@ -38,44 +46,100 @@ struct bfdev_array { #define BFDEV_DEFINE_ARRAY(name, alloc, cells) \ bfdev_array_t name = BFDEV_ARRAY_INIT(alloc, cells) +/** + * bfdev_array_init() - initialize array. + * @array: the array object. + * @alloc: allocator used to allocate memory. + * @cells: the size per elements. + */ static inline void bfdev_array_init(bfdev_array_t *array, const bfdev_alloc_t *alloc, size_t cells) { *array = BFDEV_ARRAY_INIT(alloc, cells); } +/** + * bfdev_array_reset() - reset array. + * @array: the array object. + * + * Reset the length of the stored data to zero and also + * free the internal memory buffer + */ static inline void bfdev_array_reset(bfdev_array_t *array) { array->index = 0; } +/** + * bfdev_array_index() - get index in array. + * @array: the array object. + * + * Returns the number of elements stored in + * the array container. + */ static inline unsigned long bfdev_array_index(const bfdev_array_t *array) { return array->index; } +/** + * bfdev_array_size() - get total size in array. + * @array: the array object. + * + * Returns the total size of elements stored in + * the array container. + */ static inline size_t bfdev_array_size(const bfdev_array_t *array) { return array->cells * array->index; } +/** + * bfdev_array_offset() - get elements offset in array. + * @array: the array object. + * @index: elements index. + * + * Return the address offset of the object indexed + * by @index in the array. + */ static inline uintptr_t bfdev_array_offset(const bfdev_array_t *array, unsigned long index) { return array->cells * index; } +/** + * bfdev_array_data() - get elements pointer in array. + * @array: the array object. + * @index: elements index. + * + * Return the offset value of the object indexed + * by @index in the array. + */ static inline void * bfdev_array_data(const bfdev_array_t *array, unsigned long index) { if (bfdev_unlikely(index >= array->index)) return NULL; + return array->data + bfdev_array_offset(array, index); } +/** + * bfdev_array_push() - push elements into the array. + * @array: the array object. + * @num: the data length to push. + * + * Creates a number of new elements on the array and + * returns a pointer to the first of these elements. + * + * this may cause a re-allocation of the array depending on + * the current state of the pool which in-turn could + * cause a small performance hit. + */ extern void * bfdev_array_push(bfdev_array_t *array, unsigned long num); @@ -85,12 +149,33 @@ bfdev_array_pop(bfdev_array_t *array, unsigned long num); extern void * bfdev_array_peek(const bfdev_array_t *array, unsigned long num); +/** + * bfdev_array_resize() - directly set the number of elements in array. + * @array: the array object. + * @num: the number required resize. + */ extern int bfdev_array_resize(bfdev_array_t *array, unsigned long num); +/** + * bfdev_array_reserve() - reserve array buffer. + * @array: the array object. + * @num: the minimum required free space. + * + * Ensure that the buffer has space allocated for at least + * @num bytes. If the current buffer is too small, it will + * be reallocated, possibly to a larger size than requested. + */ extern int bfdev_array_reserve(bfdev_array_t *array, unsigned long num); +/** + * bfdev_array_release() - release array. + * @array: the array object + * + * Reset the length of the stored data to zero and also + * free the internal memory buffer. + */ extern void bfdev_array_release(bfdev_array_t *array); diff --git a/include/bfdev/ascii85.h b/include/bfdev/ascii85.h index e79a014a..c2a947c7 100644 --- a/include/bfdev/ascii85.h +++ b/include/bfdev/ascii85.h @@ -8,22 +8,56 @@ #include #include -#include #include +#include BFDEV_BEGIN_DECLS -extern void -bfdev_ascii85_encode(void *buff, const void *data, size_t size); +/** + * bfdev_ascii85_encode_length() - ascii85 encode buffer length. + * @size: length to encode. + * + * Get the maximum length a block of + * data will encode to. + */ +static inline size_t +bfdev_ascii85_encode_length(size_t size) +{ + return BFDEV_DIV_ROUND_UP(size, 4) * 5; +} -extern int -bfdev_ascii85_decode(void *buff, const void *data, size_t size); +/** + * bfdev_ascii85_decode_length() - ascii85 decode buffer length. + * @size: length to decode. + * + * Get the maximum length a block of + * data will decode to. + */ +static inline size_t +bfdev_ascii85_decode_length(size_t size) +{ + return BFDEV_DIV_ROUND_UP(size, 5) * 4; +} -extern size_t -bfdev_ascii85_encode_length(const void *data, size_t size); +/** + * bfdev_base32_encode() - ascii85 encoder. + * @buff: encode output buffer. + * @data: data to encode. + * @plen: save output length. + * @size: length of @data. + */ +extern void +bfdev_ascii85_encode(void *buff, const void *data, size_t *plen, size_t size); -extern size_t -bfdev_ascii85_decode_length(const void *data, size_t size); +/** + * bfdev_ascii85_decode() - ascii85 decoder. + * @buff: decode output buffer. + * @data: data to decode. + * @plen: save output length. + * @size: length of @data. + */ +extern int +bfdev_ascii85_decode(void *buff, const void *data, size_t *plen, size_t size); BFDEV_END_DECLS diff --git a/include/bfdev/base32.h b/include/bfdev/base32.h index dee84f16..b55d2274 100644 --- a/include/bfdev/base32.h +++ b/include/bfdev/base32.h @@ -13,22 +13,42 @@ BFDEV_BEGIN_DECLS +/** + * bfdev_base32_encode_length() - base32 encode buffer length. + * @size: length to encode. + */ static inline size_t bfdev_base32_encode_length(size_t size) { - return BFDEV_DIV_ROUND_UP(size, 5) * 8 + 1; + return BFDEV_DIV_ROUND_UP(size, 5) * 8; } +/** + * bfdev_base32_decode_length() - base32 decoder buffer length. + * @size: length to decode. + */ static inline size_t bfdev_base32_decode_length(size_t size) { return BFDEV_DIV_ROUND_UP(size, 8) * 5; } -extern void +/** + * bfdev_base32_encode() - base32 encoder. + * @buff: encoder output buffer. + * @data: data to encode. + * @size: length of @data. + */ +extern void __bfdev_nonnull(1, 2) bfdev_base32_encode(void *buff, const void *data, size_t size); -extern int +/** + * bfdev_base32_decode() - base32 decoder. + * @buff: decode output buffer. + * @data: data to decode. + * @size: length of @data. + */ +extern int __bfdev_nonnull(1, 2) bfdev_base32_decode(void *buff, const void *data, size_t size); BFDEV_END_DECLS diff --git a/include/bfdev/base64.h b/include/bfdev/base64.h index 51adf168..2543ee6a 100644 --- a/include/bfdev/base64.h +++ b/include/bfdev/base64.h @@ -13,21 +13,41 @@ BFDEV_BEGIN_DECLS +/** + * bfdev_base64_encode_length() - base64 encode buffer length. + * @size: length to encode. + */ static inline size_t bfdev_base64_encode_length(size_t size) { - return BFDEV_DIV_ROUND_UP(size, 3) * 4 + 1; + return BFDEV_DIV_ROUND_UP(size, 3) * 4; } +/** + * bfdev_base64_decode_length() - base64 decoder buffer length. + * @size: length to decode. + */ static inline size_t bfdev_base64_decode_length(size_t size) { return BFDEV_DIV_ROUND_UP(size, 4) * 3; } +/** + * bfdev_base64_encode() - base64 encoder. + * @buff: encoder output buffer. + * @data: data to encode. + * @size: length of @data. + */ extern void bfdev_base64_encode(void *buff, const void *data, size_t size); +/** + * bfdev_base64_decode() - base64 decoder. + * @buff: decode output buffer. + * @data: data to decode. + * @size: length of @data. + */ extern int bfdev_base64_decode(void *buff, const void *data, size_t size); diff --git a/include/bfdev/cache.h b/include/bfdev/cache.h index bcbdc29f..ea394f5e 100644 --- a/include/bfdev/cache.h +++ b/include/bfdev/cache.h @@ -99,16 +99,19 @@ struct bfdev_cache_algo { void (*destroy)(bfdev_cache_head_t *head); }; -BFDEV_BITFLAGS_STRUCT(bfdev_cache, +BFDEV_BITFLAGS_STRUCT( + bfdev_cache, bfdev_cache_head_t, flags ); -BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_cache, +BFDEV_BITFLAGS_STRUCT_FLAG( + bfdev_cache, bfdev_cache_head_t, flags, dirty, __BFDEV_CACHE_DIRTY ); -BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_cache, +BFDEV_BITFLAGS_STRUCT_FLAG( + bfdev_cache, bfdev_cache_head_t, flags, starving, __BFDEV_CACHE_STARVING ); @@ -164,7 +167,7 @@ bfdev_cache_cumulative(bfdev_cache_head_t *head, unsigned long tag) extern int bfdev_cache_register(bfdev_cache_algo_t *algo); -extern void +extern int bfdev_cache_unregister(bfdev_cache_algo_t *algo); BFDEV_END_DECLS diff --git a/include/bfdev/fifo.h b/include/bfdev/fifo.h index a9fa1762..8a654b3b 100644 --- a/include/bfdev/fifo.h +++ b/include/bfdev/fifo.h @@ -13,6 +13,8 @@ BFDEV_BEGIN_DECLS +typedef struct bfdev_fifo bfdev_fifo_t; + struct bfdev_fifo { const bfdev_alloc_t *alloc; unsigned long in; @@ -23,14 +25,14 @@ struct bfdev_fifo { }; /** - * BFDEV_GENERIC_FIFO - define a generic fifo structure. + * BFDEV_GENERIC_FIFO() - define a generic fifo structure. * @datatype: fifo data type. * @ptrtype: fifo pointer containing data. * @rsize: fifo record size. */ #define BFDEV_GENERIC_FIFO(datatype, ptrtype, rsize) \ union { \ - struct bfdev_fifo fifo; \ + bfdev_fifo_t fifo; \ datatype *data; \ const datatype *cdata; \ ptrtype *ptr; \ @@ -39,7 +41,7 @@ struct bfdev_fifo { } /** - * BFDEV_BODY_FIFO - generate the body of normal fifo. + * BFDEV_BODY_FIFO() - generate the body of normal fifo. * @type: fifo contains the type of data. * @ptype: fifo pointer containing data. * @size: fifo buffer size. @@ -52,7 +54,7 @@ struct bfdev_fifo { } /** - * BFDEV_BODY_FIFO_DYNAMIC - generate the body of dynamic fifo. + * BFDEV_BODY_FIFO_DYNAMIC() - generate the body of dynamic fifo. * @type: fifo contains the type of data. * @ptype: fifo pointer containing data. * @rsize: fifo record size. @@ -63,33 +65,33 @@ struct bfdev_fifo { } /** - * BFDEV_FIFO_INIT - initialize normal fifo in compound literals. - * @name: the name of fifo to init. + * BFDEV_FIFO_INIT() - initialize normal fifo in compound literals. + * @ptr: the pointer of fifo to init. */ -#define BFDEV_FIFO_INIT(name) \ -(typeof(name)) { \ +#define BFDEV_FIFO_INIT(ptr) \ +(typeof(*(ptr))) { \ .fifo = { \ .in = 0, .out = 0, \ - .esize = sizeof(*(name).buff), \ - .mask = BFDEV_ARRAY_SIZE((name).buff) - 1, \ - .data = (name).buff, \ + .esize = sizeof(*(ptr)->buff), \ + .mask = BFDEV_ARRAY_SIZE((ptr)->buff) - 1, \ + .data = &(ptr)->buff, \ }, \ } /** - * BFDEV_FIFO_DYNAMIC_INIT - initialize dynamic fifo in compound literals. - * @name: the name of fifo to init. + * BFDEV_FIFO_DYNAMIC_INIT() - initialize dynamic fifo in compound literals. + * @ptr: the pointer of fifo to init. */ -#define BFDEV_FIFO_DYNAMIC_INIT(name) \ -(typeof(name)) {{ \ +#define BFDEV_FIFO_DYNAMIC_INIT(ptr) \ +(typeof(*(ptr))) { \ .fifo = { \ .in = 0, .out = 0, .mask = 0, .data = NULL, \ - .esize = sizeof(*(name).buff), \ + .esize = sizeof(*(ptr)->buff), \ }, \ -}} +} /** - * BFDEV_STRUCT_FIFO - generate a normal fifo structure. + * BFDEV_STRUCT_FIFO() - generate a normal fifo structure. * @type: fifo contains the type of data. * @size: fifo buffer size. */ @@ -97,7 +99,7 @@ struct bfdev_fifo { struct BFDEV_BODY_FIFO(type, type, size, 0) /** - * BFDEV_STRUCT_FIFO_RECORD - generate a record fifo structure. + * BFDEV_STRUCT_FIFO_RECORD() - generate a record fifo structure. * @type: fifo contains the type of data. * @size: fifo buffer size. * @record: fifo record size. @@ -106,14 +108,14 @@ struct bfdev_fifo { struct BFDEV_BODY_FIFO(type, type, size, record) /** - * BFDEV_DECLARE_STRUCT_FIFO_DYNAMIC - generate a dynamic fifo structure. + * BFDEV_DECLARE_STRUCT_FIFO_DYNAMIC() - generate a dynamic fifo structure. * @type: fifo contains the type of data. */ #define BFDEV_DECLARE_STRUCT_FIFO_DYNAMIC(type) \ struct BFDEV_BODY_FIFO_DYNAMIC(type, type, 0) /** - * BFDEV_STRUCT_FIFO_DYNAMIC_RECORD - generate a dynamic record fifo structure. + * BFDEV_STRUCT_FIFO_DYNAMIC_RECORD() - generate a dynamic record fifo structure. * @type: fifo contains the type of data. * @record: fifo record size. */ @@ -121,7 +123,7 @@ struct bfdev_fifo { struct BFDEV_BODY_FIFO_DYNAMIC(type, type, record) /** - * BFDEV_DECLARE_FIFO - declare a normal fifo structure. + * BFDEV_DECLARE_FIFO() - declare a normal fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @size: fifo buffer size. @@ -130,7 +132,7 @@ struct bfdev_fifo { BFDEV_STRUCT_FIFO(type, size) name /** - * BFDEV_DECLARE_FIFO_RECORD - declare a record fifo structure. + * BFDEV_DECLARE_FIFO_RECORD() - declare a record fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @size: fifo buffer size. @@ -140,7 +142,7 @@ struct bfdev_fifo { BFDEV_STRUCT_FIFO_RECORD(type, size, record) name /** - * BFDEV_DECLARE_FIFO_DYNAMIC - declare a dynamic fifo structure. + * BFDEV_DECLARE_FIFO_DYNAMIC() - declare a dynamic fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. */ @@ -148,7 +150,7 @@ struct bfdev_fifo { BFDEV_DECLARE_STRUCT_FIFO_DYNAMIC(type) name /** - * BFDEV_DECLARE_FIFO_DYNAMIC_RECORD - declare a dynamic record fifo structure. + * BFDEV_DECLARE_FIFO_DYNAMIC_RECORD() - declare a dynamic record fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @record: fifo record size. @@ -157,67 +159,67 @@ struct bfdev_fifo { BFDEV_STRUCT_FIFO_DYNAMIC_RECORD(type, record) name /** - * BFDEV_DEFINE_FIFO - define a normal fifo structure. + * BFDEV_DEFINE_FIFO() - define a normal fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @size: fifo buffer size. */ #define BFDEV_DEFINE_FIFO(name, type, size) \ - BFDEV_DECLARE_FIFO(name, type, size) = BFDEV_FIFO_INIT(name) + BFDEV_DECLARE_FIFO(name, type, size) = BFDEV_FIFO_INIT(&name) /** - * BFDEV_DEFINE_FIFO_RECORD - define a record fifo structure. + * BFDEV_DEFINE_FIFO_RECORD() - define a record fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @size: fifo buffer size. * @record: fifo record size. */ #define BFDEV_DEFINE_FIFO_RECORD(name, type, size, record) \ - BFDEV_DECLARE_FIFO_RECORD(name, type, size, record) = BFDEV_FIFO_INIT(name) + BFDEV_DECLARE_FIFO_RECORD(name, type, size, record) = BFDEV_FIFO_INIT(&name) /** - * BFDEV_DEFINE_FIFO_DYNAMIC - define a dynamic fifo structure. + * BFDEV_DEFINE_FIFO_DYNAMIC() - define a dynamic fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. */ #define BFDEV_DEFINE_FIFO_DYNAMIC(name, type) \ - BFDEV_DECLARE_FIFO_DYNAMIC(name, type) = BFDEV_FIFO_DYNAMIC_INIT(name) + BFDEV_DECLARE_FIFO_DYNAMIC(name, type) = BFDEV_FIFO_DYNAMIC_INIT(&name) /** - * BFDEV_DEFINE_FIFO_DYNAMIC_RECORD - declare define dynamic record fifo structure. + * BFDEV_DEFINE_FIFO_DYNAMIC_RECORD() - declare define dynamic record fifo structure. * @name: name of fifo structure to declare. * @type: fifo contains the type of data. * @record: fifo record size. */ #define BFDEV_DEFINE_FIFO_DYNAMIC_RECORD(name, type, record) \ - BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(name, type, record) = BFDEV_FIFO_DYNAMIC_INIT(name) + BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(name, type, record) = BFDEV_FIFO_DYNAMIC_INIT(&name) /** - * bfdev_fifo_initialized - check if the fifo is initialized. + * bfdev_fifo_initialized() - check if the fifo is initialized. * @ptr: pointer of the fifo to check. */ #define bfdev_fifo_initialized(ptr) ((ptr)->fifo.mask) /** - * bfdev_fifo_recsize - get the size of the record length field. + * bfdev_fifo_recsize() - get the size of the record length field. * @ptr: pointer of the fifo to get field length. */ #define bfdev_fifo_recsize(ptr) (sizeof(*(ptr)->rectype)) /** - * bfdev_fifo_size - get the size of the element managed by the fifo. + * bfdev_fifo_size() - get the size of the element managed by the fifo. * @ptr: pointer of the fifo to get size. */ #define bfdev_fifo_size(ptr) ((ptr)->fifo.mask + 1) /** - * bfdev_fifo_esize - get the size of the fifo in elements. + * bfdev_fifo_esize() - get the size of the fifo in elements. * @ptr: pointer of the fifo to get size. */ #define bfdev_fifo_esize(ptr) ((ptr)->fifo.esize) /** - * bfdev_fifo_reset - reset fifo state. + * bfdev_fifo_reset() - reset fifo state. * @ptr: the fifo to reset. */ #define bfdev_fifo_reset(ptr) do { \ @@ -226,7 +228,7 @@ struct bfdev_fifo { } while (0) /** - * bfdev_fifo_homing - homing unread valid data length. + * bfdev_fifo_homing() - homing unread valid data length. * @ptr: the fifo to homing. */ #define bfdev_fifo_homing(ptr) do { \ @@ -235,7 +237,7 @@ struct bfdev_fifo { } while (0) /** - * bfdev_fifo_len - get the valid data length in fifo. + * bfdev_fifo_len() - get the valid data length in fifo. * @ptr: the fifo to get. */ #define bfdev_fifo_len(ptr) ({ \ @@ -244,7 +246,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_check_empty - check w fifo is empty. + * bfdev_fifo_check_empty() - check w fifo is empty. * @ptr: the fifo to check. */ #define bfdev_fifo_check_empty(ptr) ({ \ @@ -253,7 +255,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_check_full - check whether fifo is full. + * bfdev_fifo_check_full() - check whether fifo is full. * @ptr: the fifo to check. */ #define bfdev_fifo_check_full(ptr) ({ \ @@ -262,7 +264,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_check_dynamic - check whether fifo is dynamic. + * bfdev_fifo_check_dynamic() - check whether fifo is dynamic. * @ptr: the fifo to check. */ #define bfdev_fifo_check_dynamic(ptr) ( \ @@ -270,7 +272,7 @@ struct bfdev_fifo { ) /** - * bfdev_fifo_alloc - dynamically allocate buffer to fifo. + * bfdev_fifo_alloc() - dynamically allocate buffer to fifo. * @ptr: the fifo to allocate buffer. * @size: size of buffer. */ @@ -283,7 +285,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_free - dynamically free buffer to fifo. + * bfdev_fifo_free() - dynamically free buffer to fifo. * @ptr: the fifo to free buffer. */ #define bfdev_fifo_free(ptr) ({ \ @@ -294,90 +296,90 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_peek - peek an object from fifo. + * bfdev_fifo_peek() - peek an object from fifo. * @struct: the fifo to peek object out. * @value: object to peek. */ -#define bfdev_fifo_peek(pfifo, value) ({ \ - typeof((pfifo) + 1) __tmp = (pfifo); \ - typeof(__tmp->ptr) __tvalue = (value); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ - unsigned long __recsize = sizeof(*__tmp->rectype); \ - unsigned long __retval; \ - if (__recsize) { \ - __retval = bfdev_fifo_peek_record(__fifo, \ - __tvalue, sizeof(*__tvalue), __recsize); \ - } else { \ - __retval = !bfdev_fifo_check_empty(__tmp); \ - if (__retval) { \ - *(typeof(__tmp->data)) __tvalue = \ - (bfdev_fifo_check_dynamic(__tmp) ? \ - ((typeof(__tmp->data)) __fifo->data) : \ - (__tmp->buff)) \ - [__fifo->out & __tmp->fifo.mask]; \ - } \ - } \ - __retval; \ +#define bfdev_fifo_peek(pfifo, value) ({ \ + typeof((pfifo) + 1) __tmp = (pfifo); \ + typeof(__tmp->ptr) __tvalue = (value); \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ + unsigned long __recsize = sizeof(*__tmp->rectype); \ + unsigned long __retval; \ + if (__recsize) { \ + __retval = bfdev_fifo_peek_record(__fifo, \ + __tvalue, sizeof(*__tvalue), __recsize); \ + } else { \ + __retval = !bfdev_fifo_check_empty(__tmp); \ + if (__retval) { \ + *(typeof(__tmp->data)) __tvalue = \ + (bfdev_fifo_check_dynamic(__tmp) ? \ + ((typeof(__tmp->data)) __fifo->data) : \ + (__tmp->buff)) \ + [__fifo->out & __tmp->fifo.mask]; \ + } \ + } \ + __retval; \ }) /** - * bfdev_fifo_get - get an object from fifo. + * bfdev_fifo_get() - get an object from fifo. * @struct: the fifo to get object out. * @value: object to get. */ -#define bfdev_fifo_get(pfifo, value) ({ \ - typeof((pfifo) + 1) __tmp = (pfifo); \ - typeof(__tmp->ptr) __tvalue = (value); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ - unsigned long __recsize = sizeof(*__tmp->rectype); \ - unsigned long __retval; \ - if (__recsize) { \ - __retval = bfdev_fifo_out_record(__fifo, \ - __tvalue, sizeof(*__tvalue), __recsize); \ - } else { \ - __retval = !bfdev_fifo_check_empty(__tmp); \ - if (__retval) { \ - *(typeof(__tmp->data)) __tvalue = \ - (bfdev_fifo_check_dynamic(__tmp) ? \ - ((typeof(__tmp->data)) __fifo->data) : \ - (__tmp->buff)) \ - [__fifo->out & __tmp->fifo.mask]; \ - ++__fifo->out; \ - } \ - } \ - __retval; \ +#define bfdev_fifo_get(pfifo, value) ({ \ + typeof((pfifo) + 1) __tmp = (pfifo); \ + typeof(__tmp->ptr) __tvalue = (value); \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ + unsigned long __recsize = sizeof(*__tmp->rectype); \ + unsigned long __retval; \ + if (__recsize) { \ + __retval = bfdev_fifo_out_record(__fifo, \ + __tvalue, sizeof(*__tvalue), __recsize); \ + } else { \ + __retval = !bfdev_fifo_check_empty(__tmp); \ + if (__retval) { \ + *(typeof(__tmp->data)) __tvalue = \ + (bfdev_fifo_check_dynamic(__tmp) ? \ + ((typeof(__tmp->data)) __fifo->data) : \ + (__tmp->buff)) \ + [__fifo->out & __tmp->fifo.mask]; \ + ++__fifo->out; \ + } \ + } \ + __retval; \ }) /** - * bfdev_fifo_put - put an object into fifo. + * bfdev_fifo_put() - put an object into fifo. * @pfifo: the fifo to put object in. * @value: object to put. */ -#define bfdev_fifo_put(pfifo, value) ({ \ - typeof((pfifo) + 1) __tmp = (pfifo); \ - typeof(*__tmp->cdata) __tvalue = (value); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ - unsigned long __recsize = sizeof(*__tmp->rectype); \ - unsigned long __retval; \ - if (__recsize) { \ - __retval = bfdev_fifo_in_record(__fifo, \ - &__tvalue, sizeof(__tvalue), __recsize); \ - } else { \ - __retval = !bfdev_fifo_check_full(__tmp); \ - if (__retval) { \ - (bfdev_fifo_check_dynamic(__tmp) ? \ - ((typeof(__tmp->data)) __fifo->data) : \ - (__tmp->buff)) \ - [__fifo->in & __tmp->fifo.mask] = \ - *(typeof(__tmp->data)) &__tvalue; \ - ++__fifo->in; \ - } \ - } \ - __retval; \ +#define bfdev_fifo_put(pfifo, value) ({ \ + typeof((pfifo) + 1) __tmp = (pfifo); \ + typeof(*__tmp->cdata) __tvalue = (value); \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ + unsigned long __recsize = sizeof(*__tmp->rectype); \ + unsigned long __retval; \ + if (__recsize) { \ + __retval = bfdev_fifo_in_record(__fifo, \ + &__tvalue, sizeof(__tvalue), __recsize); \ + } else { \ + __retval = !bfdev_fifo_check_full(__tmp); \ + if (__retval) { \ + (bfdev_fifo_check_dynamic(__tmp) ? \ + ((typeof(__tmp->data)) __fifo->data) : \ + (__tmp->buff)) \ + [__fifo->in & __tmp->fifo.mask] = \ + *(typeof(__tmp->data)) &__tvalue; \ + ++__fifo->in; \ + } \ + } \ + __retval; \ }) /** - * bfdev_fifo_out_peek - peek continuous data from fifo. + * bfdev_fifo_out_peek() - peek continuous data from fifo. * @pfifo: the fifo to peek data out. * @buff: the buffer to peek data in. * @len: number of continuously peeked objects. @@ -385,7 +387,7 @@ struct bfdev_fifo { #define bfdev_fifo_out_peek(pfifo, buff, len) ({ \ typeof((pfifo) + 1) __tmp = (pfifo); \ typeof(__tmp->ptr) __tbuff = (buff); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -394,7 +396,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_out - copy continuous data from fifo. + * bfdev_fifo_out() - copy continuous data from fifo. * @pfifo: the fifo to copy data out. * @buff: the buffer to copy data in. * @len: number of continuously copied objects. @@ -402,7 +404,7 @@ struct bfdev_fifo { #define bfdev_fifo_out(pfifo, buff, len) ({ \ typeof((pfifo) + 1) __tmp = (pfifo); \ typeof(__tmp->ptr) __tbuff = (buff); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -411,7 +413,7 @@ struct bfdev_fifo { }) /** - * bfdev_fifo_in - copy continuous data into fifo. + * bfdev_fifo_in() - copy continuous data into fifo. * @pfifo: the fifo to copy data in. * @buff: the buffer to copy data out. * @len: number of continuously copied objects. @@ -419,7 +421,7 @@ struct bfdev_fifo { #define bfdev_fifo_in(pfifo, buff, len) ({ \ typeof((pfifo) + 1) __tmp = (pfifo); \ typeof(__tmp->cptr) __tbuff = (buff); \ - struct bfdev_fifo *__fifo = &__tmp->fifo; \ + bfdev_fifo_t *__fifo = &__tmp->fifo; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -428,35 +430,32 @@ struct bfdev_fifo { }) extern unsigned long -bfdev_fifo_peek_flat(struct bfdev_fifo *fifo, void *buff, - unsigned long len); +bfdev_fifo_peek_flat(bfdev_fifo_t *fifo, void *buff, unsigned long len); extern unsigned long -bfdev_fifo_out_flat(struct bfdev_fifo *fifo, void *buff, - unsigned long len); +bfdev_fifo_out_flat(bfdev_fifo_t *fifo, void *buff, unsigned long len); extern unsigned long -bfdev_fifo_in_flat(struct bfdev_fifo *fifo, const void *buff, - unsigned long len); +bfdev_fifo_in_flat(bfdev_fifo_t *fifo, const void *buff, unsigned long len); extern unsigned long -bfdev_fifo_peek_record(struct bfdev_fifo *fifo, void *buff, - unsigned long len, unsigned long record); +bfdev_fifo_peek_record(bfdev_fifo_t *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); +bfdev_fifo_out_record(bfdev_fifo_t *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); +bfdev_fifo_in_record(bfdev_fifo_t *fifo, const void *buff, unsigned long len, + unsigned long record); extern int -bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const bfdev_alloc_t *alloc, +bfdev_fifo_dynamic_alloc(bfdev_fifo_t *fifo, const bfdev_alloc_t *alloc, size_t esize, size_t size); extern void -bfdev_fifo_dynamic_free(struct bfdev_fifo *fifo); +bfdev_fifo_dynamic_free(bfdev_fifo_t *fifo); BFDEV_END_DECLS diff --git a/include/bfdev/heap.h b/include/bfdev/heap.h index 95172cb5..add1f19b 100644 --- a/include/bfdev/heap.h +++ b/include/bfdev/heap.h @@ -439,7 +439,6 @@ bfdev_heap_post_next(const bfdev_heap_node_t *node); pos && ({ tmp = bfdev_heap_post_next_entry(pos, member); \ 1; }); pos = tmp) - BFDEV_END_DECLS #endif /* _BFDEV_HEAP_H_ */ diff --git a/include/bfdev/hlist.h b/include/bfdev/hlist.h index fd7b83f5..7585d9b1 100644 --- a/include/bfdev/hlist.h +++ b/include/bfdev/hlist.h @@ -51,7 +51,7 @@ bfdev_hlist_check_del(bfdev_hlist_node_t *node); #endif /** - * bfdev_hlist_head_init - initialize a bfdev_hlist_head structure. + * bfdev_hlist_head_init() - initialize a bfdev_hlist_head structure. * @head: hlist_head structure to be initialized. */ static inline void @@ -61,7 +61,7 @@ bfdev_hlist_head_init(bfdev_hlist_head_t *head) } /** - * bfdev_hlist_node_init - initialize a bfdev_hlist_node structure. + * bfdev_hlist_node_init() - initialize a bfdev_hlist_node structure. * @node: bfdev_hlist_node structure to be initialized. */ static inline void @@ -72,7 +72,7 @@ bfdev_hlist_node_init(bfdev_hlist_node_t *node) } /** - * bfdev_hlist_head_add - add a new entry at the beginning of the hlist. + * bfdev_hlist_head_add() - add a new entry at the beginning of the hlist. * @head: hlist head to add it after. * @newn: new entry to be added. */ @@ -93,7 +93,7 @@ bfdev_hlist_head_add(bfdev_hlist_head_t *head, bfdev_hlist_node_t *newn) } /** - * bfdev_hlist_next_add - add a new entry before the one specified. + * bfdev_hlist_next_add() - add a new entry before the one specified. * @node: hlist node to add it after, which must be non-NULL. * @newn: new entry to be added. */ @@ -114,7 +114,7 @@ bfdev_hlist_next_add(bfdev_hlist_node_t *node, bfdev_hlist_node_t *newn) } /** - * bfdev_hlist_prev_add - add a new entry before the one specified. + * bfdev_hlist_prev_add() - add a new entry before the one specified. * @node: hlist node to add it before, which must be non-NULL. * @newn: new entry to be added. */ @@ -133,14 +133,16 @@ bfdev_hlist_prev_add(bfdev_hlist_node_t *node, bfdev_hlist_node_t *newn) } /** - * bfdev_hlist_deluf - delete the specified bfdev_hlist_node from its hlist (unsafe). + * bfdev_hlist_deluf() - delete the specified bfdev_hlist_node from its hlist (unsafe). * @node: the element to delete from the hlist. */ static inline void bfdev_hlist_deluf(bfdev_hlist_node_t *node) { - bfdev_hlist_node_t **pprev = node->pprev; - bfdev_hlist_node_t *next = node->next; + bfdev_hlist_node_t **pprev, *next; + + pprev = node->pprev; + next = node->next; if (next) next->pprev = pprev; @@ -148,7 +150,7 @@ bfdev_hlist_deluf(bfdev_hlist_node_t *node) } /** - * bfdev_hlist_del - delete the specified bfdev_hlist_node from its hlist. + * bfdev_hlist_del() - delete the specified bfdev_hlist_node from its hlist. * @node: the element to delete from the hlist. */ static inline void @@ -165,7 +167,7 @@ bfdev_hlist_del(bfdev_hlist_node_t *node) } /** - * bfdev_hlist_check_empty - check whether the node is head. + * bfdev_hlist_check_empty() - check whether the node is head. * @head: hlist head to check. */ static inline bool @@ -175,7 +177,7 @@ bfdev_hlist_check_empty(const bfdev_hlist_head_t *head) } /** - * bfdev_hlist_check_first - check whether the node is a header. + * bfdev_hlist_check_first() - check whether the node is a header. * @head: the head of the hlist. * @node: the entry to test. */ @@ -187,7 +189,7 @@ bfdev_hlist_check_first(const bfdev_hlist_head_t *head, } /** - * bfdev_hlist_check_end - check whether the node is a ending. + * bfdev_hlist_check_end() - check whether the node is a ending. * @node: the entry to test. */ static inline bool @@ -197,7 +199,7 @@ bfdev_hlist_check_end(const bfdev_hlist_node_t *node) } /** - * bfdev_hlist_check_another - check whether has another node. + * bfdev_hlist_check_another() - check whether has another node. * @head: hlist head to check. * @node: the unique node. */ @@ -209,7 +211,7 @@ bfdev_hlist_check_another(const bfdev_hlist_head_t *head, } /** - * bfdev_hlist_check_unhashed - check whether the node is reinitialized. + * bfdev_hlist_check_unhashed() - check whether the node is reinitialized. * @node: hlist node to check. */ static inline bool @@ -219,7 +221,7 @@ bfdev_hlist_check_unhashed(const bfdev_hlist_node_t *node) } /** - * bfdev_hlist_head_replace - replace a hlist head with an external head. + * bfdev_hlist_head_replace() - replace a hlist head with an external head. * @oldn: bfdev_hlist_head for old hlist. * @newn: bfdev_hlist_head for new hlist. */ @@ -233,7 +235,7 @@ bfdev_hlist_head_replace(bfdev_hlist_head_t *oldn, bfdev_hlist_head_t *newn) } /** - * bfdev_hlist_replace - replace a hlist node with an external node. + * bfdev_hlist_replace() - replace a hlist node with an external node. * @old: bfdev_hlist_head for old hlist. * @newn: bfdev_hlist_head for new hlist. */ @@ -248,7 +250,7 @@ bfdev_hlist_replace(bfdev_hlist_node_t *oldn, bfdev_hlist_node_t *newn) } /** - * bfdev_hlist_del_init - deletes entry from hlist and reinitialize it. + * bfdev_hlist_del_init() - deletes entry from hlist and reinitialize it. * @node: the element to delete from the hlist. */ static inline void @@ -259,7 +261,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) } /** - * bfdev_hlist_entry - get the struct for this entry. + * bfdev_hlist_entry() - get the struct for this entry. * @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. @@ -268,7 +270,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) bfdev_container_of_safe(ptr, type, member) /** - * bfdev_hlist_first_entry - get the first element from a hlist. + * bfdev_hlist_first_entry() - get the first element from a hlist. * @ptr: the hlist head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the hlist_head within the struct. @@ -277,7 +279,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) bfdev_hlist_entry((ptr)->node, type, member) /** - * bfdev_hlist_next_entry - get the next element in hlist. + * bfdev_hlist_next_entry() - get the next element in hlist. * @pos: the type * to cursor. * @member: the name of the bfdev_hlist_node within the struct. */ @@ -285,7 +287,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) bfdev_hlist_entry((pos)->member.next, typeof(*(pos)), member) /** - * bfdev_hlist_for_each - iterate over a hlist. + * bfdev_hlist_for_each() - iterate over a hlist. * @pos: the &bfdev_hlist_node_t to use as a loop cursor. * @head: the head for your hlist. */ @@ -293,50 +295,50 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) for ((pos) = (head)->node; (pos); (pos) = (pos)->next) /** - * bfdev_hlist_for_each_from - iterate over a hlist from the current point. + * bfdev_hlist_for_each_from() - iterate over a hlist from the current point. * @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. + * bfdev_hlist_for_each_continue() - continue iteration over a hlist. * @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) + 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. + * bfdev_hlist_for_each_safe() - iterate over a hlist safe against removal of hlist entry. * @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. */ #define bfdev_hlist_for_each_safe(pos, tmp, head) \ - for ((pos) = (head)->node; (pos) && ({(tmp) = (pos)->next; 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)(((pos) = (head)->node) && ((tmp) = (pos)->next)); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = (pos)->next))) /** - * bfdev_hlist_for_each_from_safe - iterate over a hlist safe against removal of hlist entry from the current point. + * bfdev_hlist_for_each_from_safe() - iterate over a hlist safe against removal of hlist entry from the current point. * @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) \ - for (; (pos) && ({(tmp) = (pos)->next; 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)((pos) && ((tmp) = (pos)->next)); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = (pos)->next))) /** - * bfdev_hlist_for_each_continue_safe - continue hlist iteration safe against removal. + * bfdev_hlist_for_each_continue_safe() - continue hlist iteration safe against removal. * @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) \ - for ((void)((pos) && ((pos) = (pos)->next)); \ - (pos) && ({(tmp) = (pos)->next; 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)((pos) && ((pos) = (pos)->next) && ((tmp) = (pos)->next)); \ + (pos); (void)(((pos) = (tmp)) && ((tmp) = (pos)->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. @@ -346,7 +348,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) (pos); (pos) = bfdev_hlist_next_entry(pos, member)) /** - * bfdev_hlist_for_each_entry_from - continue iteration over hlist of given type from the current point. + * bfdev_hlist_for_each_entry_from() - continue iteration over hlist of given type from the current point. * @pos: the type * to use as a loop cursor. * @member: the name of the bfdev_hlist_node within the struct. */ @@ -354,7 +356,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) for (; (pos); (pos) = bfdev_hlist_next_entry(pos, member)) /** - * bfdev_hlist_for_each_entry_continue - continue iteration over hlist of given type. + * bfdev_hlist_for_each_entry_continue() - continue iteration over hlist of given type. * @pos: the type * to use as a loop cursor. * @member: the name of the bfdev_hlist_node within the struct. */ @@ -363,7 +365,7 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) (pos); (pos) = bfdev_hlist_next_entry(pos, member)) /** - * bfdev_hlist_for_each_entry_safe - iterate over hlist of given type safe against removal of hlist entry + * bfdev_hlist_for_each_entry_safe() - iterate over hlist of given type safe against removal of hlist entry * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage * @head: the head for your hlist. @@ -372,28 +374,28 @@ bfdev_hlist_del_init(bfdev_hlist_node_t *node) #define bfdev_hlist_for_each_entry_safe(pos, tmp, head, member) \ for ((pos) = bfdev_hlist_first_entry(head, typeof(*(pos)), member); \ (pos) && ({(tmp) = bfdev_hlist_next_entry(pos, member); 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = bfdev_hlist_next_entry(tmp, member)))) + (void)(((pos) = (tmp)) && ((tmp) = bfdev_hlist_next_entry(pos, member)))) /** - * bfdev_hlist_for_each_entry_from_safe - iterate over hlist from current point safe against removal. + * 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. */ #define bfdev_hlist_for_each_entry_from_safe(pos, tmp, member) \ - for (; (pos) && ({(tmp) = bfdev_hlist_next_entry(pos, member); 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = bfdev_hlist_next_entry(pos, member)))) + for ((void)((pos) && ((tmp) = bfdev_hlist_next_entry(pos, member))); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = bfdev_hlist_next_entry(pos, member)))) /** - * bfdev_hlist_for_each_entry_continue_safe - continue hlist iteration safe against removal. + * 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. */ #define bfdev_hlist_for_each_entry_continue_safe(pos, tmp, member) \ - for ((void)((pos) && ((pos) = bfdev_hlist_next_entry(pos, member))); \ - (pos) && ({(tmp) = bfdev_hlist_next_entry(pos, member); 1;}); \ - (pos) = (tmp), ((tmp) && ((tmp) = bfdev_hlist_next_entry(pos, member)))) + for ((void)(((pos) && ((pos) = bfdev_hlist_next_entry(pos, member))) && \ + ((tmp) = bfdev_hlist_next_entry(pos, member))); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = bfdev_hlist_next_entry(pos, member)))) BFDEV_END_DECLS diff --git a/include/bfdev/level.h b/include/bfdev/level.h index d3c08712..961b0d34 100644 --- a/include/bfdev/level.h +++ b/include/bfdev/level.h @@ -22,35 +22,17 @@ BFDEV_BEGIN_DECLS #define BFDEV_NOTICE BFDEV_SOH "5" /* normal but significant condition */ #define BFDEV_INFO BFDEV_SOH "6" /* informational */ #define BFDEV_DEBUG BFDEV_SOH "7" /* debug-level messages */ -#define BFDEV_DEFAULT "" /* the default kernel loglevel */ - -#define BFDEV_LEVEL_EMERG 0 -#define BFDEV_LEVEL_ALERT 1 -#define BFDEV_LEVEL_CRIT 2 -#define BFDEV_LEVEL_ERR 3 -#define BFDEV_LEVEL_WARNING 4 -#define BFDEV_LEVEL_NOTICE 5 -#define BFDEV_LEVEL_INFO 6 -#define BFDEV_LEVEL_DEBUG 7 -#define BFDEV_LEVEL_DEFAULT 8 - -#define BFDEV_COLR_DEFAULT 39 -#define BFDEV_COLR_BLACK 30 -#define BFDEV_COLR_DARK_RED 31 -#define BFDEV_COLR_DARK_GREEN 32 -#define BFDEV_COLR_DARK_YELLOW 33 -#define BFDEV_COLR_DARK_BLUE 34 -#define BFDEV_COLR_DARK_MAGENTA 35 -#define BFDEV_COLR_DARK_CYAN 36 -#define BFDEV_COLR_LIGHT_GRAY 37 -#define BFDEV_COLR_DARK_GRAY 90 -#define BFDEV_COLR_RED 91 -#define BFDEV_COLR_GREEN 92 -#define BFDEV_COLR_YELLOW 93 -#define BFDEV_COLR_BLUE 94 -#define BFDEV_COLR_MAGENTA 95 -#define BFDEV_COLR_CYAN 96 -#define BFDEV_COLR_WHITE 97 +#define BFDEV_DEFAULT "" /* the default loglevel */ + +#define BFDEV_LEVEL_EMERG 0 +#define BFDEV_LEVEL_ALERT 1 +#define BFDEV_LEVEL_CRIT 2 +#define BFDEV_LEVEL_ERR 3 +#define BFDEV_LEVEL_WARNING 4 +#define BFDEV_LEVEL_NOTICE 5 +#define BFDEV_LEVEL_INFO 6 +#define BFDEV_LEVEL_DEBUG 7 +#define BFDEV_LEVEL_DEFAULT 8 BFDEV_END_DECLS diff --git a/include/bfdev/list.h b/include/bfdev/list.h index db48857e..20763458 100644 --- a/include/bfdev/list.h +++ b/include/bfdev/list.h @@ -65,7 +65,7 @@ bfdev_list_insert(bfdev_list_head_t *prev, bfdev_list_head_t *next, } /** - * bfdev_list_head_init - initialize a list head structure. + * bfdev_list_head_init() - initialize a list head structure. * @head: list head structure to be initialized. */ static inline void @@ -75,7 +75,7 @@ bfdev_list_head_init(bfdev_list_head_t *head) } /** - * bfdev_list_add - add a new node next old node. + * bfdev_list_add() - add a new node next old node. * @node: list head to add it next. * @newn: new entry to be added. */ @@ -86,7 +86,7 @@ bfdev_list_add(bfdev_list_head_t *node, bfdev_list_head_t *newn) } /** - * bfdev_list_add_prev - add a new node after old node. + * bfdev_list_add_prev() - add a new node after old node. * @node: list head to add it prev. * @newn: new entry to be added. */ @@ -97,7 +97,7 @@ bfdev_list_add_prev(bfdev_list_head_t *node, bfdev_list_head_t *newn) } /** - * bfdev_list_deluf - deletes entry from list (unsafe). + * bfdev_list_deluf() - deletes entry from list (unsafe). * @node: the element to delete from the list. */ static inline void @@ -108,7 +108,7 @@ bfdev_list_deluf(bfdev_list_head_t *node) } /** - * bfdev_list_del - deletes entry from list. + * bfdev_list_del() - deletes entry from list. * @node: the element to delete from the list. */ static inline void @@ -125,7 +125,7 @@ bfdev_list_del(bfdev_list_head_t *node) } /** - * bfdev_list_check_empty - check whether a list is empty. + * bfdev_list_check_empty() - check whether a list is empty. * @head: list head to check. */ static inline bool @@ -135,7 +135,7 @@ bfdev_list_check_empty(const bfdev_list_head_t *head) } /** - * bfdev_list_check_head - check whether the node is head. + * bfdev_list_check_head() - check whether the node is head. * @head: the head of the list * @list: the entry to test */ @@ -147,7 +147,7 @@ bfdev_list_check_head(const bfdev_list_head_t *head, } /** - * bfdev_list_check_first - check whether the node is a header. + * bfdev_list_check_first() - check whether the node is a header. * @head: the head of the list. * @node: the entry to test. */ @@ -159,7 +159,7 @@ bfdev_list_check_first(const bfdev_list_head_t *head, } /** - * bfdev_list_check_end - check whether the node is a ending. + * bfdev_list_check_end() - check whether the node is a ending. * @head: the head of the list. * @node: the entry to test. */ @@ -171,7 +171,7 @@ bfdev_list_check_end(const bfdev_list_head_t *head, } /** - * bfdev_list_check_another - check whether has another node. + * bfdev_list_check_another() - check whether has another node. * @head: list head to check. * @node: the unique node. */ @@ -186,8 +186,11 @@ static inline void bfdev_list_relocate(bfdev_list_head_t *prev, bfdev_list_head_t *next, bfdev_list_head_t *list) { - bfdev_list_head_t *first = list->next; - bfdev_list_head_t *last = list->prev; + bfdev_list_head_t *first; + bfdev_list_head_t *last; + + first = list->next; + last = list->prev; first->prev = prev; prev->next = first; @@ -197,7 +200,7 @@ bfdev_list_relocate(bfdev_list_head_t *prev, bfdev_list_head_t *next, } /** - * bfdev_list_splice - join two lists, this is designed for stacks. + * bfdev_list_splice() - join two lists, this is designed for stacks. * @head: the place to add it in the first list. * @list: the new list to add. */ @@ -209,7 +212,7 @@ bfdev_list_splice(bfdev_list_head_t *head, bfdev_list_head_t *list) } /** - * bfdev_list_splice_prev - join two lists, each list being a queue. + * bfdev_list_splice_prev() - join two lists, each list being a queue. * @head: the place to add it in the first list. * @list: the new list to add. */ @@ -221,7 +224,7 @@ bfdev_list_splice_prev(bfdev_list_head_t *head, bfdev_list_head_t *list) } /** - * bfdev_list_replace - replace a list node with an external node. + * bfdev_list_replace() - replace a list node with an external node. * @oldn: the element to be replaced. * @newn: the new element to insert. */ @@ -230,12 +233,13 @@ bfdev_list_replace(bfdev_list_head_t *oldn, bfdev_list_head_t *newn) { newn->prev = oldn->prev; newn->next = oldn->next; + newn->prev->next = newn; newn->next->prev = newn; } /** - * bfdev_list_move - move the node to the next of the node. + * bfdev_list_move() - move the node to the next of the node. * @head: the head that will precede our entry. * @node: the entry to move. */ @@ -247,7 +251,7 @@ bfdev_list_move(bfdev_list_head_t *head, bfdev_list_head_t *node) } /** - * bfdev_list_move_prev - move the node to the prev of the node. + * bfdev_list_move_prev() - move the node to the prev of the node. * @head: the head that will follow our entry. * @node: the entry to move. */ @@ -259,15 +263,16 @@ bfdev_list_move_prev(bfdev_list_head_t *head, bfdev_list_head_t *node) } /** - * bfdev_list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position. + * bfdev_list_swap() - replace entry1 with entry2 and re-add entry1 at entry2's position. * @node1: the location to place entry2. * @node2: the location to place entry1. */ static inline void bfdev_list_swap(bfdev_list_head_t *node1, bfdev_list_head_t *node2) { - bfdev_list_head_t *prev = node2->prev; + bfdev_list_head_t *prev; + prev = node2->prev; bfdev_list_del(node2); bfdev_list_replace(node1, node2); @@ -277,7 +282,7 @@ bfdev_list_swap(bfdev_list_head_t *node1, bfdev_list_head_t *node2) } /** - * bfdev_list_del_init - deletes entry from list and reinitialize it. + * bfdev_list_del_init() - deletes entry from list and reinitialize it. * @node: the element to delete from the list. */ static inline void @@ -288,7 +293,7 @@ bfdev_list_del_init(bfdev_list_head_t *node) } /** - * bfdev_list_replace_init - replace old entry by new one and initialize the old one. + * bfdev_list_replace_init() - replace old entry by new one and initialize the old one. * @oldn: the element to be replaced. * @newn: the new element to insert. */ @@ -300,7 +305,7 @@ bfdev_list_replace_init(bfdev_list_head_t *oldn, bfdev_list_head_t *newn) } /** - * bfdev_list_splice_init - join two lists and reinitialise the emptied list. + * bfdev_list_splice_init() - join two lists and reinitialise the emptied list. * @head: the place to add it in the first list. * @list: the new list to add. */ @@ -314,7 +319,7 @@ bfdev_list_splice_init(bfdev_list_head_t *head, bfdev_list_head_t *list) } /** - * bfdev_list_splice_tail_init - join two lists and reinitialise the emptied list. + * bfdev_list_splice_tail_init() - join two lists and reinitialise the emptied list. * @head: the place to add it in the first list. * @list: the new list to add. */ @@ -328,7 +333,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) } /** - * bfdev_list_entry - get the struct for this entry. + * bfdev_list_entry() - get the struct for this entry. * @ptr: the &struct list head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list head within the struct. @@ -337,7 +342,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) bfdev_container_of(ptr, type, member) /** - * bfdev_list_entry_check_head - test if the entry points to the head of the list. + * bfdev_list_entry_check_head() - test if the entry points to the head of the list. * @pos: the type * to cursor * @head: the head for your list. * @member: the name of the list head within the struct. @@ -346,7 +351,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (&(pos)->member == (head)) /** - * bfdev_list_first_entry - get the first element from a list. + * bfdev_list_first_entry() - get the first element from a list. * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list head within the struct. @@ -355,7 +360,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) bfdev_list_entry((ptr)->next, type, member) /** - * bfdev_list_last_entry - get the last element from a list. + * bfdev_list_last_entry() - get the last element from a list. * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list head within the struct. @@ -364,7 +369,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) bfdev_list_entry((ptr)->prev, type, member) /** - * bfdev_list_next_entry - get the next element in list. + * bfdev_list_next_entry() - get the next element in list. * @pos: the type * to cursor. * @member: the name of the list head within the struct. */ @@ -372,7 +377,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) bfdev_list_entry((pos)->member.next, typeof(*(pos)), member) /** - * bfdev_list_prev_entry - get the prev element in list. + * bfdev_list_prev_entry() - get the prev element in list. * @pos: the type * to cursor. * @member: the name of the list head within the struct. */ @@ -380,7 +385,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) bfdev_list_entry((pos)->member.prev, typeof(*(pos)), member) /** - * bfdev_list_first_entry_or_null - get the first element from a list or null. + * bfdev_list_first_entry_or_null() - get the first element from a list or null. * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list head within the struct. @@ -392,7 +397,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) }) /** - * bfdev_list_last_entry_or_null - get the last element from a list or null. + * bfdev_list_last_entry_or_null() - get the last element from a list or null. * @ptr: the list head to take the element from. * @type: the type of the struct this is embedded in. * @member: the name of the list head within the struct. @@ -404,7 +409,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) }) /** - * bfdev_list_next_entry_or_null - get the next element in list or null. + * bfdev_list_next_entry_or_null() - get the next element in list or null. * @pos: the type * to cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -416,7 +421,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) }) /** - * bfdev_list_prev_entry_or_null - get the prev element in list or null. + * bfdev_list_prev_entry_or_null() - get the prev element in list or null. * @pos: the type * to cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -428,7 +433,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) }) /** - * bfdev_list_for_each - iterate over a list. + * bfdev_list_for_each() - iterate over a list. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -437,7 +442,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (pos)->next) /** - * bfdev_list_for_each_reverse - iterate over a list backwards. + * bfdev_list_for_each_reverse() - iterate over a list backwards. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -446,7 +451,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (pos)->prev) /** - * bfdev_list_for_each_from - iterate over a list from the current point. + * bfdev_list_for_each_from() - iterate over a list from the current point. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -454,7 +459,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) for (; !bfdev_list_check_head(head, pos); (pos) = (pos)->next) /** - * bfdev_list_for_each_reverse_from - iterate over a list backwards from the current point. + * bfdev_list_for_each_reverse_from() - iterate over a list backwards from the current point. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -462,7 +467,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) for (; !bfdev_list_check_head(head, pos); (pos) = (pos)->prev) /** - * bfdev_list_for_each_continue - continue iteration over a list. + * bfdev_list_for_each_continue() - continue iteration over a list. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -471,7 +476,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (pos)->next) /** - * bfdev_list_for_each_reverse_continue - continue iteration over a list backwards. + * bfdev_list_for_each_reverse_continue() - continue iteration over a list backwards. * @pos: the &struct list head to use as a loop cursor. * @head: the head for your list. */ @@ -480,25 +485,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (pos)->prev) /** - * bfdev_list_for_each_continue_check - check head and continue iteration over a list. - * @pos: the &struct list head to use as a loop cursor. - * @head: the head for your list. - */ -#define bfdev_list_for_each_continue_check(pos, head) \ - for ((void)(!bfdev_list_check_head(head, pos) && ((pos) = (pos)->next)); \ - !bfdev_list_check_head(head, pos); (pos) = (pos)->next) - -/** - * bfdev_list_for_each_reverse_continue_check - check head and continue iteration over a list backwards. - * @pos: the &struct list head to use as a loop cursor. - * @head: the head for your list. - */ -#define bfdev_list_for_each_reverse_continue_check(pos, head) \ - for ((void)(!bfdev_list_check_head(head, pos) && ((pos) = (pos)->prev)); \ - !bfdev_list_check_head(head, pos); (pos) = (pos)->prev) - -/** - * bfdev_list_for_each_safe - iterate over a list safe against removal of list entry. + * bfdev_list_for_each_safe() - iterate over a list safe against removal of list entry. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. @@ -509,7 +496,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = (tmp)->next) /** - * bfdev_list_for_each_reverse_safe - iterate backwards over list safe against removal. + * bfdev_list_for_each_reverse_safe() - iterate backwards over list safe against removal. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. @@ -520,29 +507,27 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = (tmp)->prev) /** - * bfdev_list_for_each_from_safe - iterate over a list safe against removal of list entry from the current point. + * bfdev_list_for_each_from_safe() - iterate over a list safe against removal of list entry from the current point. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. */ #define bfdev_list_for_each_from_safe(pos, tmp, head) \ - for ((tmp) = (pos)->next; \ - !bfdev_list_check_head(head, pos); \ + for ((tmp) = (pos)->next; !bfdev_list_check_head(head, pos); \ (pos) = (tmp), (tmp) = (tmp)->next) /** - * bfdev_list_for_each_reverse_from_safe - iterate backwards over list safe against removal from the current point. + * bfdev_list_for_each_reverse_from_safe() - iterate backwards over list safe against removal from the current point. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. */ #define bfdev_list_for_each_reverse_from_safe(pos, tmp, head) \ - for ((tmp) = pos->prev; \ - !bfdev_list_check_head(head, pos); \ + for ((tmp) = pos->prev; !bfdev_list_check_head(head, pos); \ (pos) = (tmp), (tmp) = (tmp)->prev) /** - * bfdev_list_for_each_continue_safe - continue list iteration safe against removal. + * bfdev_list_for_each_continue_safe() - continue list iteration safe against removal. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. @@ -553,7 +538,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = (tmp)->next) /** - * bfdev_list_for_each_reverse_continue_safe - continue backwards over list iteration safe against removal. + * bfdev_list_for_each_reverse_continue_safe() - continue backwards over list iteration safe against removal. * @pos: the &struct list head to use as a loop cursor. * @tmp: another list head to use as temporary storage. * @head: the head for your list. @@ -564,31 +549,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = (tmp)->prev) /** - * bfdev_list_for_each_continue_check_safe - check head and continue list iteration safe against removal. - * @pos: the &struct list head to use as a loop cursor. - * @tmp: another list head to use as temporary storage. - * @head: the head for your list. - */ -#define bfdev_list_for_each_continue_check_safe(pos, tmp, head) \ - for ((void)(!bfdev_list_check_head(head, pos) && \ - ((pos) = (pos)->next, (tmp) = (pos)->next)); \ - !bfdev_list_check_head(head, pos); \ - (pos) = (tmp), (tmp) = (tmp)->next) - -/** - * bfdev_list_for_each_reverse_continue_check_safe - check head and continue backwards over list iteration safe against removal. - * @pos: the &struct list head to use as a loop cursor. - * @tmp: another list head to use as temporary storage. - * @head: the head for your list. - */ -#define bfdev_list_for_each_reverse_continue_check_safe(pos, tmp, head) \ - for ((void)(!bfdev_list_check_head(head, pos) && \ - ((pos) = pos->prev, (tmp) = pos->prev)); \ - !bfdev_list_check_head(head, pos); \ - (pos) = (tmp), (tmp) = (tmp)->prev) - -/** - * bfdev_list_for_each_entry - iterate over list of given type. + * bfdev_list_for_each_entry() - iterate over list of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -599,7 +560,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_next_entry(pos, member)) /** - * bfdev_list_for_each_entry_reverse - iterate backwards over list of given type. + * bfdev_list_for_each_entry_reverse() - iterate backwards over list of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -610,7 +571,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_prev_entry(pos, member)) /** - * bfdev_list_for_each_entry_from - iterate over list of given type from the current point. + * bfdev_list_for_each_entry_from() - iterate over list of given type from the current point. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -620,7 +581,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_next_entry(pos, member)) /** - * bfdev_list_for_each_entry_reverse_from - iterate backwards over list of given type from the current point. + * bfdev_list_for_each_entry_reverse_from() - iterate backwards over list of given type from the current point. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -630,7 +591,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_prev_entry(pos, member)) /** - * bfdev_list_for_each_entry_continue - continue iteration over list of given type. + * bfdev_list_for_each_entry_continue() - continue iteration over list of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -641,7 +602,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_next_entry(pos, member)) /** - * bfdev_list_for_each_entry_reverse_continue - iterate backwards from the given point. + * bfdev_list_for_each_entry_reverse_continue() - iterate backwards from the given point. * @pos: the type * to use as a loop cursor. * @head: the head for your list. * @member: the name of the list head within the struct. @@ -652,31 +613,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = bfdev_list_prev_entry(pos, member)) /** - * bfdev_list_for_each_entry_continue_check - check head and continue iteration over list of given type. - * @pos: the type * to use as a loop cursor. - * @head: the head for your list. - * @member: the name of the list head within the struct. - */ -#define bfdev_list_for_each_entry_continue_check(pos, head, member) \ - for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ - ((pos) = bfdev_list_next_entry(pos, member))); \ - !bfdev_list_entry_check_head(pos, head, member); \ - (pos) = bfdev_list_next_entry(pos, member)) - -/** - * bfdev_list_for_each_entry_reverse_continue_check - check head and iterate backwards from the given point. - * @pos: the type * to use as a loop cursor. - * @head: the head for your list. - * @member: the name of the list head within the struct. - */ -#define bfdev_list_for_each_entry_reverse_continue_check(pos, head, member) \ - for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ - ((pos) = bfdev_list_prev_entry(pos, member))); \ - !bfdev_list_entry_check_head(pos, head, member); \ - (pos) = bfdev_list_prev_entry(pos, member)) - -/** - * bfdev_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry. + * bfdev_list_for_each_entry_safe() - iterate over list of given type safe against removal of list entry. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -689,7 +626,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = bfdev_list_next_entry(tmp, member)) /** - * bfdev_list_for_each_entry_reverse_safe - iterate backwards over list safe against removal. + * bfdev_list_for_each_entry_reverse_safe() - iterate backwards over list safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -702,7 +639,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = bfdev_list_prev_entry(tmp, member)) /** - * bfdev_list_for_each_entry_from_safe - iterate over list from current point safe against removal. + * bfdev_list_for_each_entry_from_safe() - iterate over list from current point safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -714,7 +651,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = bfdev_list_next_entry(tmp, member)) /** - * bfdev_list_for_each_entry_reverse_from_safe - iterate backwards over list from current point safe against removal. + * bfdev_list_for_each_entry_reverse_from_safe() - iterate backwards over list from current point safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -726,7 +663,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = bfdev_list_prev_entry(tmp, member)) /** - * bfdev_list_for_each_entry_continue_safe - continue list iteration safe against removal. + * bfdev_list_for_each_entry_continue_safe() - continue list iteration safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -739,7 +676,7 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) (pos) = (tmp), (tmp) = bfdev_list_next_entry(tmp, member)) /** - * bfdev_list_for_each_entry_reverse_continue_safe - continue backwards over list iteration safe against removal. + * bfdev_list_for_each_entry_reverse_continue_safe() - continue backwards over list iteration safe against removal. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your list. @@ -751,34 +688,6 @@ bfdev_list_splice_tail_init(bfdev_list_head_t *head, bfdev_list_head_t *list) !bfdev_list_entry_check_head(pos, head, member); \ (pos) = (tmp), (tmp) = bfdev_list_prev_entry(tmp, member)) -/** - * bfdev_list_for_each_entry_continue_check_safe - check head and continue list iteration safe against removal. - * @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. - */ -#define bfdev_list_for_each_entry_continue_check_safe(pos, tmp, head, member) \ - for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ - ((pos) = bfdev_list_next_entry(pos, member), \ - (tmp) = bfdev_list_next_entry(pos, member))); \ - !bfdev_list_entry_check_head(pos, head, member); \ - (pos) = (tmp), (tmp) = bfdev_list_next_entry(tmp, member)) - -/** - * bfdev_list_for_each_entry_reverse_continue_check_safe - check head and continue backwards over list iteration safe against removal. - * @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. - */ -#define bfdev_list_for_each_entry_reverse_continue_check_safe(pos, tmp, head, member) \ - for ((void)(!bfdev_list_entry_check_head(pos, head, member) && \ - ((pos) = bfdev_list_prev_entry(pos, member), \ - (tmp) = bfdev_list_prev_entry(pos, member))); \ - !bfdev_list_entry_check_head(pos, head, member); \ - (pos) = (tmp), (tmp) = bfdev_list_prev_entry(tmp, member)) - BFDEV_END_DECLS #endif /* _BFDEV_LIST_H_ */ diff --git a/include/bfdev/log.h b/include/bfdev/log.h index 5289bb60..6e58410d 100644 --- a/include/bfdev/log.h +++ b/include/bfdev/log.h @@ -32,10 +32,10 @@ typedef int (*bfdev_log_write_t) enum bfdev_log_flags { __BFDEV_LOG_COLOR = 0, - __BFDEV_LOG_COMMIT, + __BFDEV_LOG_LEVEL, BFDEV_LOG_COLOR = BFDEV_BIT(__BFDEV_LOG_COLOR), - BFDEV_LOG_COMMIT = BFDEV_BIT(__BFDEV_LOG_COMMIT), + BFDEV_LOG_LEVEL = BFDEV_BIT(__BFDEV_LOG_LEVEL), }; struct bfdev_log { @@ -49,35 +49,56 @@ struct bfdev_log { struct bfdev_log_message { unsigned int level; - const char *data; + char *buff; size_t length; }; -BFDEV_BITFLAGS_STRUCT(bfdev_log, +#define BFDEV_LOG_STATIC(HEAD, DEFAULT, RECORD, FLAGS, WRITE, PDATA) { \ + .default_level = (DEFAULT), .record_level = (RECORD), \ + .flags = (FLAGS), .write = (WRITE), .pdata = (PDATA), \ +} + +#define BFDEV_LOG_INIT(head, default, record, flags, write, pdata) \ + (bfdev_log_t) BFDEV_LOG_STATIC(head, default, record, flags, write, pdata) + +#define BFDEV_DEFINE_LOG(name, default, record, flags, write, pdata) \ + bfdev_log_t name = BFDEV_LOG_INIT(&name, default, record, flags, write, pdata) + +BFDEV_BITFLAGS_STRUCT( + bfdev_log, struct bfdev_log, flags ) -BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_log, +BFDEV_BITFLAGS_STRUCT_FLAG( + bfdev_log, struct bfdev_log, flags, color, __BFDEV_LOG_COLOR ) -BFDEV_BITFLAGS_STRUCT_FLAG(bfdev_log, +BFDEV_BITFLAGS_STRUCT_FLAG( + bfdev_log, struct bfdev_log, flags, - commit, __BFDEV_LOG_COMMIT + level, __BFDEV_LOG_LEVEL ) -extern struct bfdev_log +extern bfdev_log_t bfdev_log_default; +static inline void +bfdev_log_init(bfdev_log_t *log, unsigned int def, unsigned int record, + unsigned long flags, bfdev_log_write_t write, void *pdata) +{ + *log = BFDEV_LOG_INIT(log, def, record, flags, write, pdata); +} + extern unsigned int bfdev_log_level(const char *str, const char **endptr); extern __bfdev_printf(2, 0) int -bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args); +bfdev_log_state_vprint(bfdev_log_t *log, const char *fmt, va_list args); extern __bfdev_printf(2, 3) int -bfdev_log_state_print(struct bfdev_log *log, const char *fmt, ...); +bfdev_log_state_print(bfdev_log_t *log, const char *fmt, ...); /** * bfdev_log_fmt - used by the bfdev_log_*() macros to generate the bfdev_log_print format string diff --git a/include/bfdev/log2.h b/include/bfdev/log2.h index 989d122d..2a6600ee 100644 --- a/include/bfdev/log2.h +++ b/include/bfdev/log2.h @@ -9,6 +9,8 @@ #include #include +BFDEV_BEGIN_DECLS + /** * bfdev_ilog2_const - log base 2 of long long constant unsigned value. * @value: constant value to log base 2. @@ -90,6 +92,7 @@ bfdev_ilog2_dynamic(unsigned long value) { if (value < 2) return 0; + return bfdev_fls(value) - 1; } @@ -102,6 +105,7 @@ bfdev_ilog2_64_dynamic(uint64_t value) { if (value < 2) return 0; + return bfdev_fls64(value) - 1; } @@ -112,6 +116,9 @@ bfdev_ilog2_64_dynamic(uint64_t value) static inline unsigned long bfdev_pow2_roundup_dynamic(unsigned long value) { + if (value < 2) + return value; + return 1UL << bfdev_fls(value - 1); } @@ -122,6 +129,9 @@ bfdev_pow2_roundup_dynamic(unsigned long value) static inline unsigned long bfdev_pow2_rounddown_dynamic(unsigned long value) { + if (value < 2) + return value; + return 1UL << (bfdev_fls(value) - 1); } @@ -142,31 +152,42 @@ bfdev_pow2_check(unsigned long value) #define bfdev_ilog2(value) ( \ __builtin_constant_p(value) ? \ ((value) < 2 ? 0 : \ - 63 - __builtin_clzll(value)) : \ - (sizeof(value) <= 4) ? \ - bfdev_ilog2_dynamic(value) : \ - bfdev_ilog2_64_dynamic(value) \ + (63 - __builtin_clzll(value))) : \ + ((sizeof(value) <= 4) ? \ + bfdev_ilog2_dynamic(value) : \ + bfdev_ilog2_64_dynamic(value)) \ ) /** * bfdev_pow2_roundup - round up to nearest power of two. * @value: value to round up. + * + * round the given value up to the nearest power of two. + * - the result is @value when n < 2. + * - this can be used to initialise global variables from constant data. */ -#define bfdev_pow2_roundup(value) ( \ - __builtin_constant_p(value) ? \ - (((value) == 1) ? 1 : (1UL << \ - (bfdev_ilog2((value) - 1) + 1))) : \ - bfdev_pow2_roundup_dynamic(value) \ +#define bfdev_pow2_roundup(value) ( \ + __builtin_constant_p(value) ? \ + (((value) < 2) ? (value) : \ + (1UL << (bfdev_ilog2((value) - 1) + 1))) : \ + bfdev_pow2_roundup_dynamic(value) \ ) /** * bfdev_pow2_rounddown - round down to nearest power of two. * @value: value to round down. + * + * round the given value down to the nearest power of two. + * - the result is @value when n < 2. + * - this can be used to initialise global variables from constant data. */ #define bfdev_pow2_rounddown(value) ( \ __builtin_constant_p(value) ? \ - (1UL << bfdev_ilog2(value)) : \ + (((value) < 2) ? (value) : \ + (1UL << bfdev_ilog2(value))) : \ bfdev_pow2_rounddown_dynamic(value) \ ) +BFDEV_END_DECLS + #endif /* _BFDEV_LOG2_H_ */ diff --git a/include/bfdev/macro.h b/include/bfdev/macro.h index 49e05a8a..aedd3210 100644 --- a/include/bfdev/macro.h +++ b/include/bfdev/macro.h @@ -7,6 +7,7 @@ #define _BFDEV_MACRO_H_ #include +#include BFDEV_BEGIN_DECLS @@ -31,6 +32,14 @@ BFDEV_BEGIN_DECLS (cond) ? BFDEV_BT : BFDEV_LT \ ) +/** + * BFDEV_REPEAT_BYTE() - repeat the value multiple times to unsigned long. + * @char: value to repeat. + */ +#define BFDEV_REPEAT_BYTE(char) ( \ + (~0UL / 0xff) * (char) \ +) + /** * BFDEV_ARRAY_SIZE() - get the number of elements in array. * @arr: array to be sized. diff --git a/include/bfdev/memalloc.h b/include/bfdev/memalloc.h new file mode 100644 index 00000000..a515824d --- /dev/null +++ b/include/bfdev/memalloc.h @@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#ifndef _BFDEV_MEMALLOC_H_ +#define _BFDEV_MEMALLOC_H_ + +#include +#include + +BFDEV_BEGIN_DECLS + +#ifndef BFDEV_MEMALLOC_ALIGN +# define BFDEV_MEMALLOC_ALIGN 32 +#endif + +typedef struct bfdev_memalloc_head bfdev_memalloc_head_t; +typedef struct bfdev_memalloc_chunk bfdev_memalloc_chunk_t; + +typedef bfdev_memalloc_chunk_t * +(*bfdev_memalloc_find_t)(bfdev_memalloc_head_t *head, size_t size); + +struct bfdev_memalloc_head { + bfdev_list_head_t block_list; + bfdev_list_head_t free_list; + bfdev_memalloc_find_t find; + size_t avail; +}; + +struct bfdev_memalloc_chunk { + bfdev_list_head_t block; + bfdev_list_head_t free; + size_t usize; + char data[0]; +}; + +/** + * bfdev_memalloc_first_fit() - first qualified node. + * @head: memalloc to get node. + * @size: size to get. + */ +extern bfdev_memalloc_chunk_t * +bfdev_memalloc_first_fit(bfdev_memalloc_head_t *head, size_t size); + +/** + * bfdev_memalloc_best_fit() - best qualified node. + * @head: memalloc to get node. + * @size: size to get. + */ +extern bfdev_memalloc_chunk_t * +bfdev_memalloc_best_fit(bfdev_memalloc_head_t *head, size_t size); + +/** + * bfdev_memalloc_worst_fit() - worst qualified node. + * @head: memalloc to get node. + * @size: size to get. + */ +extern bfdev_memalloc_chunk_t * +bfdev_memalloc_worst_fit(bfdev_memalloc_head_t *head, size_t size); + +/** + * bfdev_memalloc_alloc() - memory allocator allocation. + * @head: memalloc to operate. + * @size: size to allocation. + */ +extern void __bfdev_malloc * +bfdev_memalloc_alloc(bfdev_memalloc_head_t *head, size_t size); + +/** + * bfdev_memalloc_free() - memory allocator release. + * @head: memalloc to operate. + * @block: memory to free. + */ +extern void +bfdev_memalloc_free(bfdev_memalloc_head_t *head, void *block); + +/** + * bfdev_memalloc_realloc() - memory allocator reallocation. + * @head: memalloc to operate. + * @block: memory to realloc. + * @resize: size to realloc. + */ +extern void __bfdev_malloc * +bfdev_memalloc_realloc(bfdev_memalloc_head_t *head, void *block, size_t resize); + +/** + * bfdev_memalloc_init() - memory allocator setup. + * @head: memalloc to operate. + * @find: memalloc allocator algorithm. + * @memory: memalloc memory address. + * @size: memalloc memory size. + */ +extern void +bfdev_memalloc_init(bfdev_memalloc_head_t *head, bfdev_memalloc_find_t find, + void *memory, size_t size); + +BFDEV_END_DECLS + +#endif /* _BFDEV_MEMALLOC_H_ */ diff --git a/include/bfdev/minpool.h b/include/bfdev/minpool.h deleted file mode 100644 index d7b6ac64..00000000 --- a/include/bfdev/minpool.h +++ /dev/null @@ -1,99 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2023 John Sanpe - */ - -#ifndef _BFDEV_MINPOOL_H_ -#define _BFDEV_MINPOOL_H_ - -#include -#include - -BFDEV_BEGIN_DECLS - -#ifndef BFDEV_MINPOOL_ALIGN -# define BFDEV_MINPOOL_ALIGN 16 -#endif - -struct bfdev_minpool_head; -struct bfdev_minpool_node; - -typedef struct bfdev_minpool_node * -(*bfdev_minpool_find_t)(struct bfdev_minpool_head *head, size_t size); - -struct bfdev_minpool_head { - bfdev_list_head_t block_list; - bfdev_list_head_t free_list; - bfdev_minpool_find_t find; - size_t avail; -}; - -struct bfdev_minpool_node { - bfdev_list_head_t block; - bfdev_list_head_t free; - size_t usize; - char data[0]; -}; - -/** - * minpool_first_fit - Get first qualified node in mempool. - * @head: Minimum mempool to get node. - * @size: Node minimum size to get. - */ -extern struct bfdev_minpool_node * -bfdev_minpool_first_fit(struct bfdev_minpool_head *head, size_t size); - -/** - * bfdev_minpool_best_fit - Get best qualified node in mempool. - * @head: Minimum mempool to get node. - * @size: Node minimum size to get. - */ -extern struct bfdev_minpool_node * -bfdev_minpool_best_fit(struct bfdev_minpool_head *head, size_t size); - -/** - * bfdev_minpool_worst_fit - Get worst qualified node in mempool. - * @head: Minimum mempool to get node. - * @size: Node minimum size to get. - */ -extern struct bfdev_minpool_node * -bfdev_minpool_worst_fit(struct bfdev_minpool_head *head, size_t size); - -/** - * bfdev_minpool_alloc - Minimum mempool allocation. - * @head: Minimum mempool to operate. - * @size: Size to allocation. - */ -extern void __bfdev_malloc * -bfdev_minpool_alloc(struct bfdev_minpool_head *head, size_t size); - -/** - * bfdev_minpool_alloc - Minimum mempool reallocation. - * @head: Minimum mempool to operate. - * @block: Block address to realloc. - * @size: Size to realloc. - */ -extern void __bfdev_malloc * -bfdev_minpool_realloc(struct bfdev_minpool_head *head, void *block, size_t resize); - -/** - * bfdev_minpool_free - Minimum mempool release. - * @head: Minimum mempool to operate. - * @block: Block address to free. - */ -extern void -bfdev_minpool_free(struct bfdev_minpool_head *head, void *block); - -/** - * minpool_setup - Minimum mempool setup. - * @head: Minimum mempool to operate. - * @array: Mempool array address. - * @size: Mempool array size. - */ -extern void -bfdev_minpool_setup(struct bfdev_minpool_head *head, bfdev_minpool_find_t find, - void *array, size_t size); - -BFDEV_END_DECLS - -#endif /* _BFDEV_MINPOOL_H_ */ diff --git a/include/bfdev/notifier.h b/include/bfdev/notifier.h index aef75c6c..c96c5fa7 100644 --- a/include/bfdev/notifier.h +++ b/include/bfdev/notifier.h @@ -30,11 +30,9 @@ enum bfdev_notifier_ret { /** * struct bfdev_notifier - generic notification chain header. - * @name: name of notification chain. * @nodes: node header for managing notification chain nodes. */ struct bfdev_notifier { - const char *name; bfdev_ilist_head_t nodes; }; @@ -53,27 +51,26 @@ struct bfdev_notifier_node { void *pdata; }; -#define BFDEV_NOTIFIER_STATIC(HEAD, NAME) { \ - .name = (NAME), \ +#define BFDEV_NOTIFIER_STATIC(HEAD) { \ .nodes = BFDEV_ILIST_HEAD_STATIC(&(HEAD)->nodes), \ } -#define BFDEV_NOTIFIER_INIT(head, name) \ - (bfdev_notifier_t) BFDEV_NOTIFIER_STATIC(head, name) +#define BFDEV_NOTIFIER_INIT(head) \ + (bfdev_notifier_t) BFDEV_NOTIFIER_STATIC(head) -#define BFDEV_DEFINE_NOTIFIER(head, name) \ - bfdev_notifier_t head = BFDEV_NOTIFIER_INIT(&head, name) +#define BFDEV_DEFINE_NOTIFIER(name) \ + bfdev_notifier_t name = BFDEV_NOTIFIER_INIT(&name) static inline void -bfdev_notifier_init(bfdev_notifier_t *head, const char *name) +bfdev_notifier_init(bfdev_notifier_t *head) { - *head = BFDEV_NOTIFIER_INIT(head, name); + *head = BFDEV_NOTIFIER_INIT(head); } /** * bfdev_notifier_call - call the callback function of node on notification chain. * @head: chain header to be notified. - * @arg: parameters to be passed to the node. + * @args: parameters to be passed to the node. * @call_num: number of nodes to be notified. * @called_num: number of nodes actually notified. */ diff --git a/include/bfdev/port/stdio.h b/include/bfdev/port/stdio.h index 46d530a7..301f4065 100644 --- a/include/bfdev/port/stdio.h +++ b/include/bfdev/port/stdio.h @@ -38,6 +38,15 @@ bfport_fwrite(const void *restrict ptr, size_t size, } #endif +#ifndef bfport_fflush +# define bfport_fflush bfport_fflush +static __bfdev_always_inline int +bfport_fflush(FILE *restrict stream) +{ + return fflush(stream); +} +#endif + #ifndef bfport_vsnprintf # define bfport_vsnprintf bfport_vsnprintf static __bfdev_always_inline int diff --git a/include/bfdev/respool.h b/include/bfdev/respool.h index 34cfe05c..304fa9c9 100644 --- a/include/bfdev/respool.h +++ b/include/bfdev/respool.h @@ -41,7 +41,7 @@ struct bfdev_respool_node { (bfdev_respool_t) BFDEV_RESPOOL_STATIC(head) #define BFDEV_DEFINE_RESPOOL(name) \ - bfdev_respool_t head = BFDEV_RESPOOL_INIT(name) + bfdev_respool_t name = BFDEV_RESPOOL_INIT(&name) static inline void bfdev_respool_init(bfdev_respool_t *pool) @@ -59,7 +59,7 @@ static inline void bfdev_respool_insert(bfdev_respool_t *pool, bfdev_respool_node_t *node) { - bfdev_list_add_prev(&pool->nodes, &node->list); + bfdev_list_add(&pool->nodes, &node->list); } static inline void diff --git a/include/bfdev/ringbuf.h b/include/bfdev/ringbuf.h index 733e68a3..1e440671 100644 --- a/include/bfdev/ringbuf.h +++ b/include/bfdev/ringbuf.h @@ -25,14 +25,14 @@ struct bfdev_ringbuf { }; /** - * BFDEV_GENERIC_RINGBUF - define a generic ringbuf structure. + * BFDEV_GENERIC_RINGBUF() - define a generic ringbuf structure. * @datatype: ringbuf data type. * @ptrtype: ringbuf pointer containing data. * @rsize: ringbuf record size. */ #define BFDEV_GENERIC_RINGBUF(datatype, ptrtype, rsize) \ union { \ - struct bfdev_ringbuf ringbuf; \ + bfdev_ringbuf_t ringbuf; \ datatype *data; \ const datatype *cdata; \ ptrtype *ptr; \ @@ -41,7 +41,7 @@ struct bfdev_ringbuf { } /** - * BFDEV_BODY_RINGBUF - generate the body of normal ringbuf. + * BFDEV_BODY_RINGBUF() - generate the body of normal ringbuf. * @type: ringbuf contains the type of data. * @ptype: ringbuf pointer containing data. * @size: ringbuf buffer size. @@ -54,7 +54,7 @@ struct bfdev_ringbuf { } /** - * BFDEV_BODY_RINGBUF_DYNAMIC - generate the body of dynamic ringbuf. + * BFDEV_BODY_RINGBUF_DYNAMIC() - generate the body of dynamic ringbuf. * @type: ringbuf contains the type of data. * @ptype: ringbuf pointer containing data. * @rsize: ringbuf record size. @@ -65,33 +65,33 @@ struct bfdev_ringbuf { } /** - * BFDEV_RINGBUF_INIT - initialize normal ringbuf in compound literals. - * @name: the name of ringbuf to init. + * BFDEV_RINGBUF_INIT() - initialize normal ringbuf in compound literals. + * @ptr: the pointer of ringbuf to init. */ -#define BFDEV_RINGBUF_INIT(name) \ -(typeof(name)) { \ +#define BFDEV_RINGBUF_INIT(ptr) \ +(typeof(*(ptr))) { \ .ringbuf = { \ .in = 0, .out = 0, \ - .esize = sizeof(*(name).buff), \ - .mask = BFDEV_ARRAY_SIZE((name).buff) - 1, \ - .data = (name).buff, \ + .esize = sizeof(*(ptr)->buff), \ + .mask = BFDEV_ARRAY_SIZE((ptr)->buff) - 1, \ + .data = &(ptr)->buff, \ }, \ } /** - * BFDEV_RINGBUF_DYNAMIC_INIT - initialize dynamic ringbuf in compound literals. - * @name: the name of ringbuf to init. + * BFDEV_RINGBUF_DYNAMIC_INIT() - initialize dynamic ringbuf in compound literals. + * @ptr: the pointer of ringbuf to init. */ -#define BFDEV_RINGBUF_DYNAMIC_INIT(name) \ -(typeof(name)) {{ \ +#define BFDEV_RINGBUF_DYNAMIC_INIT(ptr) \ +(typeof(*(ptr))) { \ .ringbuf = { \ .in = 0, .out = 0, .mask = 0, .data = NULL, \ - .esize = sizeof(*(name).buff), \ + .esize = sizeof(*(ptr)->buff), \ }, \ -}} +} /** - * BFDEV_STRUCT_RINGBUF - generate a normal ringbuf structure. + * BFDEV_STRUCT_RINGBUF() - generate a normal ringbuf structure. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. */ @@ -99,7 +99,7 @@ struct bfdev_ringbuf { struct BFDEV_BODY_RINGBUF(type, type, size, 0) /** - * BFDEV_STRUCT_RINGBUF_RECORD - generate a record ringbuf structure. + * BFDEV_STRUCT_RINGBUF_RECORD() - generate a record ringbuf structure. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. * @record: ringbuf record size. @@ -108,14 +108,14 @@ struct bfdev_ringbuf { struct BFDEV_BODY_RINGBUF(type, type, size, record) /** - * BFDEV_DECLARE_STRUCT_RINGBUF_DYNAMIC - generate a dynamic ringbuf structure. + * BFDEV_DECLARE_STRUCT_RINGBUF_DYNAMIC() - generate a dynamic ringbuf structure. * @type: ringbuf contains the type of data. */ #define BFDEV_DECLARE_STRUCT_RINGBUF_DYNAMIC(type) \ struct BFDEV_BODY_RINGBUF_DYNAMIC(type, type, 0) /** - * BFDEV_STRUCT_RINGBUF_DYNAMIC_RECORD - generate a dynamic record ringbuf structure. + * BFDEV_STRUCT_RINGBUF_DYNAMIC_RECORD() - generate a dynamic record ringbuf structure. * @type: ringbuf contains the type of data. * @record: ringbuf record size. */ @@ -123,7 +123,7 @@ struct bfdev_ringbuf { struct BFDEV_BODY_RINGBUF_DYNAMIC(type, type, record) /** - * BFDEV_DECLARE_RINGBUF - declare a normal ringbuf structure. + * BFDEV_DECLARE_RINGBUF() - declare a normal ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. @@ -132,7 +132,7 @@ struct bfdev_ringbuf { BFDEV_STRUCT_RINGBUF(type, size) name /** - * BFDEV_DECLARE_RINGBUF_RECORD - declare a record ringbuf structure. + * BFDEV_DECLARE_RINGBUF_RECORD() - declare a record ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. @@ -142,7 +142,7 @@ struct bfdev_ringbuf { BFDEV_STRUCT_RINGBUF_RECORD(type, size, record) name /** - * BFDEV_DECLARE_RINGBUF_DYNAMIC - declare a dynamic ringbuf structure. + * BFDEV_DECLARE_RINGBUF_DYNAMIC() - declare a dynamic ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. */ @@ -150,7 +150,7 @@ struct bfdev_ringbuf { BFDEV_DECLARE_STRUCT_RINGBUF_DYNAMIC(type) name /** - * BFDEV_DECLARE_RINGBUF_DYNAMIC_RECORD - declare a dynamic record ringbuf structure. + * BFDEV_DECLARE_RINGBUF_DYNAMIC_RECORD() - declare a dynamic record ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @record: ringbuf record size. @@ -159,67 +159,67 @@ struct bfdev_ringbuf { BFDEV_STRUCT_RINGBUF_DYNAMIC_RECORD(type, record) name /** - * BFDEV_DEFINE_RINGBUF - define a normal ringbuf structure. + * BFDEV_DEFINE_RINGBUF() - define a normal ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. */ #define BFDEV_DEFINE_RINGBUF(name, type, size) \ - BFDEV_DECLARE_RINGBUF(name, type, size) = BFDEV_RINGBUF_INIT(name) + BFDEV_DECLARE_RINGBUF(name, type, size) = BFDEV_RINGBUF_INIT(&name) /** - * BFDEV_DEFINE_RINGBUF_RECORD - define a record ringbuf structure. + * BFDEV_DEFINE_RINGBUF_RECORD() - define a record ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @size: ringbuf buffer size. * @record: ringbuf record size. */ #define BFDEV_DEFINE_RINGBUF_RECORD(name, type, size, record) \ - BFDEV_DECLARE_RINGBUF_RECORD(name, type, size, record) = BFDEV_RINGBUF_INIT(name) + BFDEV_DECLARE_RINGBUF_RECORD(name, type, size, record) = BFDEV_RINGBUF_INIT(&name) /** - * BFDEV_DEFINE_RINGBUF_DYNAMIC - define a dynamic ringbuf structure. + * BFDEV_DEFINE_RINGBUF_DYNAMIC() - define a dynamic ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. */ #define BFDEV_DEFINE_RINGBUF_DYNAMIC(name, type) \ - BFDEV_DECLARE_RINGBUF_DYNAMIC(name, type) = BFDEV_RINGBUF_DYNAMIC_INIT(name) + BFDEV_DECLARE_RINGBUF_DYNAMIC(name, type) = BFDEV_RINGBUF_DYNAMIC_INIT(&name) /** - * BFDEV_DEFINE_RINGBUF_DYNAMIC_RECORD - declare define dynamic record ringbuf structure. + * BFDEV_DEFINE_RINGBUF_DYNAMIC_RECORD() - declare define dynamic record ringbuf structure. * @name: name of ringbuf structure to declare. * @type: ringbuf contains the type of data. * @record: ringbuf record size. */ #define BFDEV_DEFINE_RINGBUF_DYNAMIC_RECORD(name, type, record) \ - BFDEV_DECLARE_RINGBUF_DYNAMIC_RECORD(name, type, record) = BFDEV_RINGBUF_DYNAMIC_INIT(name) + BFDEV_DECLARE_RINGBUF_DYNAMIC_RECORD(name, type, record) = BFDEV_RINGBUF_DYNAMIC_INIT(&name) /** - * bfdev_ringbuf_initialized - check if the ringbuf is initialized. + * bfdev_ringbuf_initialized() - check if the ringbuf is initialized. * @ptr: pointer of the ringbuf to check. */ #define bfdev_ringbuf_initialized(ptr) ((ptr)->ringbuf.mask) /** - * bfdev_ringbuf_recsize - returns the size of the record length field. + * bfdev_ringbuf_recsize() - returns the size of the record length field. * @ptr: pointer of the ringbuf to get field length. */ #define bfdev_ringbuf_recsize(ptr) (sizeof(*(ptr)->rectype)) /** - * bfdev_ringbuf_size - get the size of the element managed by the ringbuf. + * bfdev_ringbuf_size() - get the size of the element managed by the ringbuf. * @ptr: pointer of the ringbuf to get size. */ #define bfdev_ringbuf_size(ptr) ((ptr)->ringbuf.mask + 1) /** - * bfdev_ringbuf_esize - get the size of the ringbuf in elements. + * bfdev_ringbuf_esize() - get the size of the ringbuf in elements. * @ptr: pointer of the ringbuf to get size. */ #define bfdev_ringbuf_esize(ptr) ((ptr)->ringbuf.esize) /** - * bfdev_ringbuf_reset - reset ringbuf state. + * bfdev_ringbuf_reset() - reset ringbuf state. * @ptr: the ringbuf to reset. */ #define bfdev_ringbuf_reset(ptr) do { \ @@ -228,7 +228,7 @@ struct bfdev_ringbuf { } while (0) /** - * bfdev_ringbuf_homing - homing unread valid data length. + * bfdev_ringbuf_homing() - homing unread valid data length. * @ptr: the ringbuf to homing. */ #define bfdev_ringbuf_homing(ptr) do { \ @@ -237,7 +237,7 @@ struct bfdev_ringbuf { } while (0) /** - * bfdev_ringbuf_len - get the valid data length in ringbuf. + * bfdev_ringbuf_len() - get the valid data length in ringbuf. * @ptr: the ringbuf to get. */ #define bfdev_ringbuf_len(ptr) ({ \ @@ -246,7 +246,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_check_empty - check w ringbuf is empty. + * bfdev_ringbuf_check_empty() - check w ringbuf is empty. * @ptr: the ringbuf to check. */ #define bfdev_ringbuf_check_empty(ptr) ({ \ @@ -255,7 +255,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_check_full - check whether ringbuf is full. + * bfdev_ringbuf_check_full() - check whether ringbuf is full. * @ptr: the ringbuf to check. */ #define bfdev_ringbuf_check_full(ptr) ({ \ @@ -264,7 +264,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_check_dynamic - check whether ringbuf is dynamic. + * bfdev_ringbuf_check_dynamic() - check whether ringbuf is dynamic. * @ptr: the ringbuf to check. */ #define bfdev_ringbuf_check_dynamic(ptr) ( \ @@ -272,7 +272,7 @@ struct bfdev_ringbuf { ) /** - * bfdev_ringbuf_alloc - dynamically allocate buffer to ringbuf. + * bfdev_ringbuf_alloc() - dynamically allocate buffer to ringbuf. * @ptr: the ringbuf to allocate buffer. * @size: size of buffer. */ @@ -285,7 +285,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_free - dynamically free buffer to ringbuf. + * bfdev_ringbuf_free() - dynamically free buffer to ringbuf. * @ptr: the ringbuf to free buffer. */ #define bfdev_ringbuf_free(ptr) ({ \ @@ -296,14 +296,14 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_peek - peek an object from ringbuf. + * bfdev_ringbuf_peek() - peek an object from ringbuf. * @struct: the ringbuf to peek object out. * @value: object to peek. */ #define bfdev_ringbuf_peek(pringbuf, value) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(__tmp->ptr) __tvalue = (value); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __recsize = sizeof(*__tmp->rectype); \ unsigned long __retval; \ if (__recsize) { \ @@ -323,14 +323,14 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_get - get an object from ringbuf. + * bfdev_ringbuf_get() - get an object from ringbuf. * @struct: the ringbuf to get object out. * @value: object to get. */ #define bfdev_ringbuf_get(pringbuf, value) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(__tmp->ptr) __tvalue = (value); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __recsize = sizeof(*__tmp->rectype); \ unsigned long __retval; \ if (__recsize) { \ @@ -351,14 +351,14 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_put - put an object into ringbuf. + * bfdev_ringbuf_put() - put an object into ringbuf. * @pringbuf: the ringbuf to put object in. * @value: object to put. */ #define bfdev_ringbuf_put(pringbuf, value) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(*__tmp->cdata) __tvalue = (value); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __recsize = sizeof(*__tmp->rectype); \ unsigned long __retval; \ if (__recsize) { \ @@ -379,7 +379,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_out_peek - peek continuous data from ringbuf. + * bfdev_ringbuf_out_peek() - peek continuous data from ringbuf. * @pringbuf: the ringbuf to peek data out. * @buff: the buffer to peek data in. * @len: number of continuously peeked objects. @@ -387,7 +387,7 @@ struct bfdev_ringbuf { #define bfdev_ringbuf_out_peek(pringbuf, buff, len) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(__tmp->ptr) __tbuff = (buff); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -396,7 +396,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_out - copy continuous data from ringbuf. + * bfdev_ringbuf_out() - copy continuous data from ringbuf. * @pringbuf: the ringbuf to copy data out. * @buff: the buffer to copy data in. * @len: number of continuously copied objects. @@ -404,7 +404,7 @@ struct bfdev_ringbuf { #define bfdev_ringbuf_out(pringbuf, buff, len) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(__tmp->ptr) __tbuff = (buff); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -413,7 +413,7 @@ struct bfdev_ringbuf { }) /** - * bfdev_ringbuf_in - copy continuous data into ringbuf. + * bfdev_ringbuf_in() - copy continuous data into ringbuf. * @pringbuf: the ringbuf to copy data in. * @buff: the buffer to copy data out. * @len: number of continuously copied objects. @@ -421,7 +421,7 @@ struct bfdev_ringbuf { #define bfdev_ringbuf_in(pringbuf, buff, len) ({ \ typeof((pringbuf) + 1) __tmp = (pringbuf); \ typeof(__tmp->cptr) __tbuff = (buff); \ - struct bfdev_ringbuf *__ringbuf = &__tmp->ringbuf; \ + bfdev_ringbuf_t *__ringbuf = &__tmp->ringbuf; \ unsigned long __tlen = (len); \ unsigned long __recsize = sizeof(*__tmp->rectype); \ (__recsize) ? \ @@ -430,35 +430,32 @@ struct bfdev_ringbuf { }) extern unsigned long -bfdev_ringbuf_peek_flat(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len); +bfdev_ringbuf_peek_flat(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len); extern unsigned long -bfdev_ringbuf_out_flat(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len); +bfdev_ringbuf_out_flat(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len); extern unsigned long -bfdev_ringbuf_in_flat(struct bfdev_ringbuf *ringbuf, const void *buff, - unsigned long len); +bfdev_ringbuf_in_flat(bfdev_ringbuf_t *ringbuf, const void *buff, unsigned long len); extern unsigned long -bfdev_ringbuf_peek_record(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len, unsigned long record); +bfdev_ringbuf_peek_record(bfdev_ringbuf_t *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); +bfdev_ringbuf_out_record(bfdev_ringbuf_t *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); +bfdev_ringbuf_in_record(bfdev_ringbuf_t *ringbuf, const void *buff, unsigned long len, + unsigned long record); extern int -bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const bfdev_alloc_t *alloc, +bfdev_ringbuf_dynamic_alloc(bfdev_ringbuf_t *ringbuf, const bfdev_alloc_t *alloc, size_t esize, size_t size); extern void -bfdev_ringbuf_dynamic_free(struct bfdev_ringbuf *ringbuf); +bfdev_ringbuf_dynamic_free(bfdev_ringbuf_t *ringbuf); BFDEV_END_DECLS diff --git a/include/bfdev/slist.h b/include/bfdev/slist.h index 48516def..17dbe426 100644 --- a/include/bfdev/slist.h +++ b/include/bfdev/slist.h @@ -39,7 +39,7 @@ bfdev_slist_check_del(bfdev_slist_head_t *node); #endif /** - * bfdev_slist_head_init - initialize a slist head structure. + * bfdev_slist_head_init() - initialize a slist head structure. * @head: slist head structure to be initialized. */ static inline void @@ -49,7 +49,7 @@ bfdev_slist_head_init(bfdev_slist_head_t *head) } /** - * bfdev_slist_add - add a new node next old node. + * bfdev_slist_add() - add a new node next old node. * @node: list head to add it next. * @newn: new entry to be added. */ @@ -66,20 +66,21 @@ bfdev_slist_add(bfdev_slist_head_t *node, bfdev_slist_head_t *newn) } /** - * bfdev_slist_del - delete a node from slist. + * bfdev_slist_del() - delete a node from slist. * @head: the head of the slist. * @node: the element to delete from the slist. */ static inline void bfdev_slist_del(bfdev_slist_head_t *head, bfdev_slist_head_t *node) { - bfdev_slist_head_t *walk = head; + bfdev_slist_head_t *walk; #ifdef BFDEV_DEBUG_SLIST if (bfdev_unlikely(!bfdev_slist_check_del(node))) return; #endif + walk = head; while (walk->next != node) walk = walk->next; @@ -88,7 +89,7 @@ bfdev_slist_del(bfdev_slist_head_t *head, bfdev_slist_head_t *node) } /** - * bfdev_slist_check_empty - check whether the node is head. + * bfdev_slist_check_empty() - check whether the node is head. * @head: slist head to check. */ static inline bool @@ -98,7 +99,7 @@ bfdev_slist_check_empty(const bfdev_slist_head_t *head) } /** - * bfdev_slist_check_first - check whether the node is a header. + * bfdev_slist_check_first() - check whether the node is a header. * @head: the head of the slist. * @node: the entry to test. */ @@ -110,7 +111,7 @@ bfdev_slist_check_first(const bfdev_slist_head_t *head, } /** - * bfdev_slist_check_next - check whether the node is a ending. + * bfdev_slist_check_next() - check whether the node is a ending. * @node: the node to check. */ static inline bool @@ -120,7 +121,7 @@ bfdev_slist_check_end(const bfdev_slist_head_t *node) } /** - * bfdev_slist_check_another - check whether has another node. + * bfdev_slist_check_another() - check whether has another node. * @head: slist head to check. * @node: the unique node. */ @@ -132,7 +133,7 @@ bfdev_slist_check_another(const bfdev_slist_head_t *head, } /** - * bfdev_slist_replace - replace a linked slist node with an external node. + * bfdev_slist_replace() - replace a linked slist node with an external node. * @head: the head of the slist. * @oldn: the element to be replaced. * @newn: the new element to insert. @@ -141,8 +142,9 @@ static inline void bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, bfdev_slist_head_t *newn) { - bfdev_slist_head_t *walk = head; + bfdev_slist_head_t *walk; + walk = head; while (walk->next != oldn) walk = walk->next; @@ -151,7 +153,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, } /** - * bfdev_slist_entry - get the struct for this entry. + * bfdev_slist_entry() - get the struct for this entry. * @ptr: the struct slist head pointer. * @type: the type of the struct this is embedded in. * @member: the name of the list_head within the struct. @@ -160,7 +162,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, bfdev_container_of_safe(ptr, type, member) /** - * bfdev_slist_first_entry - get the first element from a slist. + * 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. @@ -169,7 +171,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, bfdev_slist_entry((ptr)->next, type, member) /** - * bfdev_slist_next_entry - get the next element in slist. + * bfdev_slist_next_entry() - get the next element in slist. * @pos: the type * to cursor. * @member: the name of the slist head within the struct. */ @@ -177,7 +179,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, bfdev_slist_entry((pos)->member.next, typeof(*(pos)), member) /** - * bfdev_slist_for_each - iterate over a slist. + * bfdev_slist_for_each() - iterate over a slist. * @pos: the struct slist head to use as a loop cursor. * @head: the head for your slist. */ @@ -185,49 +187,50 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, for ((pos) = (head)->next; (pos); (pos) = (pos)->next) /** - * bfdev_slist_for_each_from - iterate over a slist from the current point. + * bfdev_slist_for_each_from() - iterate over a slist from the current point. * @pos: the &struct slist head to use as a loop cursor. */ #define bfdev_slist_for_each_from(pos) \ for (; (pos); (pos) = (pos)->next) /** - * bfdev_slist_for_each_continue - continue iteration over a slist. + * bfdev_slist_for_each_continue() - continue iteration over a slist. * @pos: the &struct slist head to use as a loop cursor. */ #define bfdev_slist_for_each_continue(pos) \ - for ((pos) = (pos)->next; (pos); (pos) = (pos)->next) + for ((void)((pos) && ((pos) = (pos)->next)); \ + (pos); (pos) = (pos)->next) /** - * bfdev_slist_for_each_safe - iterate over a slist safe against removal of slist entry. + * bfdev_slist_for_each_safe() - iterate over a slist safe against removal of slist entry. * @pos: the &struct slist head to use as a loop cursor. * @tmp: another slist head to use as temporary storage. * @head: the head for your slist. */ #define bfdev_slist_for_each_safe(pos, tmp, head) \ - for ((pos) = (head)->next, (tmp) = (pos)->next; \ - (pos); (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)(((pos) = (head)->next) && ((tmp) = (pos)->next)); \ + (pos); (void)(((pos) = (tmp)) && ((tmp) = (pos)->next))) /** - * bfdev_slist_for_each_from_safe - iterate over a slist safe against removal of slist entry from the current point. + * bfdev_slist_for_each_from_safe() - iterate over a slist safe against removal of slist entry from the current point. * @pos: the &struct slist head to use as a loop cursor. * @tmp: another slist head to use as temporary storage. */ #define bfdev_slist_for_each_from_safe(pos, tmp) \ - for ((tmp) = (pos)->next; \ - (pos); (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)((pos) && ((tmp) = (pos)->next)); \ + (pos); (void)(((pos) = (tmp)) && ((tmp) = (pos)->next))) /** - * bfdev_slist_for_each_continue_safe - continue slist iteration safe against removal. + * bfdev_slist_for_each_continue_safe() - continue slist iteration safe against removal. * @pos: the &struct slist head to use as a loop cursor. * @tmp: another slist head to use as temporary storage. */ #define bfdev_slist_for_each_continue_safe(pos, tmp) \ - for ((pos) = (pos)->next, (tmp) = (pos)->next; \ - (pos); (pos) = (tmp), ((tmp) && ((tmp) = (tmp)->next))) + for ((void)((pos) && ((pos) = (pos)->next) && ((tmp) = (pos)->next)); \ + (pos); (void)(((pos) = (tmp)) && ((tmp) = (pos)->next))) /** - * bfdev_slist_for_each_entry - iterate over slist of given type. + * bfdev_slist_for_each_entry() - iterate over slist of given type. * @pos: the type * to use as a loop cursor. * @head: the head for your slist. * @member: the name of the slist head within the struct. @@ -237,7 +240,7 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, (pos); (pos) = bfdev_slist_next_entry(pos, member)) /** - * bfdev_slist_for_each_entry_from - iterate over slist of given type from the current point. + * bfdev_slist_for_each_entry_from() - iterate over slist of given type from the current point. * @pos: the type * to use as a loop cursor. * @member: the name of the slist head within the struct. */ @@ -245,46 +248,46 @@ bfdev_slist_replace(bfdev_slist_head_t *head, bfdev_slist_head_t *oldn, for (; (pos); (pos) = bfdev_slist_next_entry(pos, member)) /** - * bfdev_slist_for_each_entry_continue - continue iteration over slist of given type. + * bfdev_slist_for_each_entry_continue() - continue iteration over slist of given type. * @pos: the type * to use as a loop cursor. * @member: the name of the slist head within the struct. */ #define bfdev_slist_for_each_entry_continue(pos, member) \ - for ((pos) = bfdev_slist_next_entry(pos, member); \ + for ((void)((pos) && ((pos) = bfdev_slist_next_entry(pos, member))); \ (pos); (pos) = bfdev_slist_next_entry(pos, member)) /** - * bfdev_slist_for_each_entry_safe - iterate over slist of given type safe against removal of slist entry. + * bfdev_slist_for_each_entry_safe() - iterate over slist of given type safe against removal of slist entry. * @pos: the type * to use as a loop cursor. * @tmp: another type * to use as temporary storage. * @head: the head for your slist. * @member: the name of the slist head within the struct. */ #define bfdev_slist_for_each_entry_safe(pos, tmp, head, member) \ - for ((pos) = bfdev_slist_first_entry(head, typeof(*(pos)), member), \ - (tmp) = bfdev_slist_next_entry(pos, member); (pos); (pos) = (tmp), \ - ((tmp) && ((tmp) = bfdev_slist_next_entry(tmp, member)))) + for ((void)(((pos) = bfdev_slist_first_entry(head, typeof(*(pos)), member)) && \ + ((tmp) = bfdev_slist_next_entry(pos, member))); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = bfdev_slist_next_entry(pos, member)))) /** - * bfdev_slist_for_each_entry_from_safe - iterate over slist from current point safe against removal. + * 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. */ #define bfdev_slist_for_each_entry_from_safe(pos, tmp, member) \ - for ((tmp) = bfdev_slist_next_entry(pos, member); (pos); (pos) = (tmp), \ - ((tmp) && ((tmp) = bfdev_slist_next_entry(pos, member)))) + for ((void)((pos) && ((tmp) = bfdev_slist_next_entry(pos, member))); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = bfdev_slist_next_entry(pos, member)))) /** - * bfdev_slist_for_each_entry_continue_safe - continue slist iteration safe against removal. + * 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. */ #define bfdev_slist_for_each_entry_continue_safe(pos, tmp, member) \ - for ((pos) = bfdev_slist_next_entry(pos, member), \ - (tmp) = bfdev_slist_next_entry(pos, member); (pos); (pos) = (tmp), \ - ((tmp) && ((tmp) = bfdev_slist_next_entry(pos, member)))) + for ((void)((pos) && ((pos) = bfdev_slist_next_entry(pos, member)) && \ + ((tmp) = bfdev_slist_next_entry(pos, member))); (pos); \ + (void)(((pos) = (tmp)) && ((tmp) = bfdev_slist_next_entry(pos, member)))) BFDEV_END_DECLS diff --git a/include/bfdev/textsearch.h b/include/bfdev/textsearch.h index 7db01778..f3415578 100644 --- a/include/bfdev/textsearch.h +++ b/include/bfdev/textsearch.h @@ -13,6 +13,14 @@ BFDEV_BEGIN_DECLS +/** + * Textsearch: + * + * The textsearch infrastructure provides text searching facilities for + * both linear and non-linear data. Individual search algorithms are + * implemented in modules and chosen by the user. + */ + 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; @@ -20,19 +28,26 @@ typedef struct bfdev_ts_algorithm bfdev_ts_algorithm_t; enum bfdev_ts_flags { __BFDEV_TS_IGCASE = 0, + + /* Searches string case insensitively */ BFDEV_TS_IGCASE = BFDEV_BIT(__BFDEV_TS_IGCASE), }; /** - * struct bfdev_ts_state - Search state. + * struct bfdev_ts_state - search state. * @offset: offset for next match. - * @offset: search context private data. + * @pdata: search context private data. */ struct bfdev_ts_state { unsigned int offset; void *pdata; }; +/** + * struct bfdev_ts_state - search linear state. + * @offset: offset for next match. + * @pdata: search context private data. + */ struct bfdev_ts_linear { bfdev_ts_state_t tss; const char *data; @@ -40,95 +55,223 @@ struct bfdev_ts_linear { }; /** - * struct bfdev_ts_context - Search context. - * @ops: chosen search algorithm. + * struct bfdev_ts_context - search context. * @flags: user flags. - * @next_block: fetch next block of data. + * @algo: chosen search algorithm. */ struct bfdev_ts_context { const bfdev_alloc_t *alloc; + bfdev_ts_algorithm_t *algo; unsigned long flags; - 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); + /** + * next_block() - fetch next block of data. + * @tsc: search configuration. + * @tss: search state. + * @consumed: number of bytes consumed by the caller. + * @dest: destination buffer. + * + * Called repeatedly until 0 is returned. Must assign the + * head of the next block of data to &*dst and return the length + * of the block or 0 if at the end. + */ + unsigned int + (*next_block)(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, + unsigned int consumed, const void **dest); }; /** - * struct bfdev_ts_algorithm - Search algorithm operations. + * struct bfdev_ts_algorithm - search algorithm operations. * @name: name of search algorithm. - * @prepare: initialization function to prepare a search. - * @destroy: destroy algorithm specific parts of a search configuration. - * @find: find the next occurrence of the pattern. - * @pattern_get: return pointer of pattern. - * @pattern_len: return length of pattern. */ struct bfdev_ts_algorithm { bfdev_list_head_t list; const char *name; - 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); - const void *(*pattern_get)(bfdev_ts_context_t *tsc); - unsigned int (*pattern_len)(bfdev_ts_context_t *tsc); + /** + * prepare() - initialization function to prepare a search. + * @alloc: allocator used to allocate memory. + * @pattern: pattern data + * @len: length of pattern + * @flags: search flags + */ + bfdev_ts_context_t * + (*prepare)(const bfdev_alloc_t *alloc, const void *pattern, + size_t len, unsigned long flags); + + /** + * destroy() - destroy algorithm specific parts of a search configuration. + * @tsc: search configuration. + */ + void + (*destroy)(bfdev_ts_context_t *tsc); + + /** + * find() - find the next occurrence of the pattern. + * @tsc: search configuration. + */ + unsigned int + (*find)(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss); + + /** + * pattern_get() - return pointer of pattern. + * @tsc: search configuration. + */ + const void * + (*pattern_get)(bfdev_ts_context_t *tsc); + + /** + * pattern_len() - return length of pattern. + * @tsc: search configuration. + */ + unsigned int + (*pattern_len)(bfdev_ts_context_t *tsc); }; -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) +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 +) + +/** + * bfdev_textsearch_find() - start searching for a pattern. + * @tsc: search configuration. + * @tss: search state. + * + * Returns the position of first occurrence of the pattern or + * UINT_MAX if no match was found. + */ static inline unsigned int bfdev_textsearch_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; tss->offset = 0; + return algo->find(tsc, tss); } +/** + * bfdev_textsearch_next() - continue searching for a pattern. + * @tsc: search configuration. + * @tss: search state. + * + * Continues a search looking for more occurrences of the pattern. + * bfdev_textsearch_find() must be called to find the first occurrence + * in order to reset the state. + * + * Returns the position of the next occurrence of the pattern or + * UINT_MAX if not match was found. + */ static inline unsigned int bfdev_textsearch_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; + return algo->find(tsc, tss); } +/** + * textsearch_pattern_get() - return head of the pattern. + * @tsc: search configuration. + */ static inline const void * textsearch_pattern_get(bfdev_ts_context_t *tsc) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; + return algo->pattern_get(tsc); } +/** + * bfdev_textsearch_pattern_len() - return length of the pattern. + * @tsc: search configuration. + */ static inline unsigned int bfdev_textsearch_pattern_len(bfdev_ts_context_t *tsc) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; + return algo->pattern_len(tsc); } +/** + * bfdev_textsearch_destroy() - destroy a search configuration. + * @tsc: search configuration. + * + * Releases all references of the configuration and frees + * up the memory. + */ static inline void bfdev_textsearch_destroy(bfdev_ts_context_t *tsc) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; + algo->destroy(tsc); } +/** + * bfdev_textsearch_linear() - search a pattern in linear data. + * @tsc: search configuration. + * @state: search state. + * @data: data to search in. + * @len: length of data. + * + * A simplified version of bfdev_textsearch_find() for continuous/linear data. + * Call bfdev_textsearch_next() to retrieve subsequent matches. + * + * Returns the position of first occurrence of the pattern or + * %UINT_MAX if no occurrence was found. + */ extern unsigned int -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(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear); +bfdev_textsearch_linear(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear, + const void *data, unsigned int len); +/** + * bfdev_textsearch_create() - prepare a search. + * @alloc: allocator used to allocate memory. + * @name: name of search algorithm. + * @pattern: pattern data. + * @len: length of pattern. + * @flags: search flags. + */ extern bfdev_ts_context_t * bfdev_textsearch_create(const bfdev_alloc_t *alloc, const char *name, const void *pattern, size_t len, unsigned long flags); +/** + * bfdev_textsearch_register() - register a textsearch module. + * @algo: operations lookup table. + * + * Returns 0 or -BFDEV_EEXISTS if another module has already + * registered with same name. + */ extern int bfdev_textsearch_register(bfdev_ts_algorithm_t *algo); -extern void +/** + * bfdev_textsearch_unregister() - unregister a textsearch module. + * @algo: operations lookup table. + * + * Returns 0 on success or -BFDEV_ENOENT if no matching + * textsearch registration was found. + */ +extern int bfdev_textsearch_unregister(bfdev_ts_algorithm_t *algo); BFDEV_END_DECLS diff --git a/include/port/log.h b/include/port/log.h index 6ec0c74b..29d55524 100644 --- a/include/port/log.h +++ b/include/port/log.h @@ -13,17 +13,19 @@ # error "please don't include this file directly" #endif -static inline void +static inline int generic_log_write(bfdev_log_message_t *msg) { bfport_file *file; if (msg->level > BFDEV_LEVEL_WARNING) file = bfport_stdout; - else + else { file = bfport_stderr; + bfport_fflush(bfport_stdout); + } - bfport_fwrite(msg->data, msg->length, 1, file); + return bfport_fwrite(msg->buff, msg->length, 1, file); } #endif /* _LOCAL_PORT_LOG_H_ */ diff --git a/scripts/CMakeLists.txt b/scripts/CMakeLists.txt index 594f61fc..62f936d3 100644 --- a/scripts/CMakeLists.txt +++ b/scripts/CMakeLists.txt @@ -19,14 +19,14 @@ host_target(gen-crc32be gen-crc32be.c) host_target(gen-crc64 gen-crc64.c) host_target(gen-crc64be gen-crc64be.c) -macro(generate_crctbl proc path table rows poly trans) +macro(generate_crctbl proc path name table rows poly trans) add_custom_command( TARGET ${proc} POST_BUILD COMMAND bash -c \" mkdir -p ${BFDEV_GENERATED_PATH}/bfdev/crypto && ${PROJECT_BINARY_DIR}/scripts/${proc} - ${table} ${rows} ${poly} ${trans} > + ${name} ${table} ${rows} ${poly} ${trans} > ${BFDEV_GENERATED_PATH}/bfdev/crypto/${path} \" ) @@ -40,54 +40,63 @@ endif() generate_crctbl( gen-crc8 crc7-table.h + "CRC-7" bfdev_crc7_table ${CRC_ROWS} 0x12 "" ) generate_crctbl( gen-crc8 crc8-table.h + "CRC-8" bfdev_crc8_table ${CRC_ROWS} 0x07 "" ) generate_crctbl( gen-crc16 crc16-table.h + "CRC-16" bfdev_crc16_table ${CRC_ROWS} 0xa001 "bfdev_cpu_to_le16" ) generate_crctbl( gen-crc16 crc-ccitt-table.h + "CRC-CCITT" bfdev_crc_ccitt_table ${CRC_ROWS} 0x8408 "bfdev_cpu_to_le16" ) generate_crctbl( gen-crc16be crc-itut-table.h + "CRC-ITUT" bfdev_crc_itut_table ${CRC_ROWS} 0x1021 "bfdev_cpu_to_be16" ) generate_crctbl( gen-crc16be crc-t10dif-table.h + "CRC-T10DIF" bfdev_crc_t10dif_table ${CRC_ROWS} 0x8bb7 "bfdev_cpu_to_be16" ) generate_crctbl( gen-crc32 crc32-table.h + "CRC-32" bfdev_crc32_table ${CRC_ROWS} 0xedb88320 "bfdev_cpu_to_le32" ) generate_crctbl( gen-crc64be crc64-table.h + "CRC-64" bfdev_crc64_table ${CRC_ROWS} 0x42f0e1eba9ea3693 "bfdev_cpu_to_be64" ) generate_crctbl( gen-crc64 crc-rocksoft-table.h + "CRC-ROCKSOFT" bfdev_crc_rocksoft_table ${CRC_ROWS} 0x9a6c9329ac4bc9b5 "bfdev_cpu_to_le64" ) diff --git a/scripts/gen-crc.c b/scripts/gen-crc.c index 06ae2546..f0d4f2ec 100644 --- a/scripts/gen-crc.c +++ b/scripts/gen-crc.c @@ -16,6 +16,7 @@ #define NAME_STRING __bfdev_stringify(GENCRC_NAME) #define TYPE_STRING __bfdev_stringify(GENCRC_TYPE) +#define BITS_STRING __bfdev_stringify(GENCRC_BITS) #define WIDE_STRING __bfdev_stringify(GENCRC_WIDE) #define CRC_TABLE_BITS 8 @@ -98,48 +99,59 @@ table_dump(unsigned int rows, const char *trans, int main(int argc, char *argv[]) { - const char *trans, *name; + const char *name, *table, *trans; unsigned int rows; GENCRC_TYPE poly; - void *table; + void *buff; - if (argc != 4 && argc != 5) { - printf("usage: %s name rows poly [trans]\n", argv[0]); + if (argc != 5 && argc != 6) { + fprintf(stderr, "usage: %s name table rows poly [trans]\n", argv[0]); return -1; } - trans = NULL; - if (argc == 5) - trans = argv[4]; - name = argv[1]; - rows = (unsigned int)strtoul(argv[2], NULL, 0); - poly = (GENCRC_TYPE)strtoull(argv[3], NULL, 0); + table = argv[2]; + rows = (unsigned int)strtoul(argv[3], NULL, 0); + poly = (GENCRC_TYPE)strtoull(argv[4], NULL, 0); + + trans = NULL; + if (argc == 6) + trans = argv[5]; if (!rows) { fprintf(stderr, "error: rows cannot be zero\n"); return -1; } - table = calloc(1, sizeof(GENCRC_TYPE) * CRC_TABLE_SIZE * rows); - if (!table) + buff = calloc(1, sizeof(GENCRC_TYPE) * CRC_TABLE_SIZE * rows); + if (!buff) return -1; - printf("/*\n"); - printf(" * Automatically generated file; DO NOT EDIT.\n"); - printf(" * bfdev scripts/gen-" NAME_STRING "\n"); - printf(" */\n\n"); - - printf("/* The poly is 0x%" WIDE_STRING "." WIDE_STRING "llx */\n", - (unsigned long long)poly); - printf("static const " TYPE_STRING " %s[%d][%d] = {\n", - name, rows, CRC_TABLE_SIZE); - - table_generic(rows, poly, table); - table_dump(rows, trans, table); + printf( + "/*\n" + " * Automatically generated file; DO NOT EDIT.\n" + " * bfdev scripts/gen-" NAME_STRING "\n" + " */\n" + "\n" + "/*\n" + " * This is the %s table.\n" + " * Generated with:\n" + " * width = " BITS_STRING " bits\n" + " * poly = 0x%" WIDE_STRING "." WIDE_STRING "llx\n" + " * byteorder = %s-endian\n" + " */\n" + "\n" + "static const " TYPE_STRING " %s[%d][%d] = {\n", + name, (unsigned long long)poly, + GENCRC_BELE ? "little" : "big", + table, rows, CRC_TABLE_SIZE + ); + + table_generic(rows, poly, buff); + table_dump(rows, trans, buff); printf("};\n"); - free(table); + free(buff); return 0; } diff --git a/scripts/packed-source.cmake b/scripts/packed-source.cmake deleted file mode 100644 index 48028edc..00000000 --- a/scripts/packed-source.cmake +++ /dev/null @@ -1,29 +0,0 @@ -# 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/array.c b/src/array.c index 0633b6db..73d53cbd 100644 --- a/src/array.c +++ b/src/array.c @@ -7,26 +7,34 @@ #include #include #include +#include #include +static inline size_t +array_reqsize(bfdev_array_t *array, unsigned long count) +{ + unsigned long request; + size_t size; + + request = bfdev_max(BFDEV_ARRAY_MSIZE, count); + size = bfdev_pow2_roundup(request * array->cells); + + return size; +} + static inline int -array_resize(bfdev_array_t *array, unsigned long count) +array_resize(bfdev_array_t *array, size_t size) { const bfdev_alloc_t *alloc; - unsigned long nalloc; - size_t size; void *data; - nalloc = bfdev_max(count, BFDEV_ARRAY_MSIZE); - size = nalloc * array->cells; - alloc = array->alloc; data = bfdev_realloc(alloc, array->data, size); if (bfdev_unlikely(!data)) return -BFDEV_ENOMEM; array->data = data; - array->capacity = nalloc; + array->capacity = size / array->cells; return -BFDEV_ENOERR; } @@ -34,10 +42,15 @@ array_resize(bfdev_array_t *array, unsigned long count) static inline int array_apply(bfdev_array_t *array, unsigned long count) { + size_t reqsize; + if (count <= array->capacity) return -BFDEV_ENOERR; - return array_resize(array, count << 1); + reqsize = array_reqsize(array, count); + BFDEV_BUG_ON(count * array->cells > reqsize); + + return array_resize(array, reqsize); } static inline void * diff --git a/src/build.cmake b/src/build.cmake index 723b4e48..6d918cb9 100644 --- a/src/build.cmake +++ b/src/build.cmake @@ -28,9 +28,8 @@ set(BFDEV_SOURCE ${CMAKE_CURRENT_LIST_DIR}/levenshtein.c ${CMAKE_CURRENT_LIST_DIR}/list-sort.c ${CMAKE_CURRENT_LIST_DIR}/llist.c - ${CMAKE_CURRENT_LIST_DIR}/log.c ${CMAKE_CURRENT_LIST_DIR}/matrix.c - ${CMAKE_CURRENT_LIST_DIR}/minpool.c + ${CMAKE_CURRENT_LIST_DIR}/memalloc.c ${CMAKE_CURRENT_LIST_DIR}/mpi.c ${CMAKE_CURRENT_LIST_DIR}/notifier.c ${CMAKE_CURRENT_LIST_DIR}/popcount.c @@ -85,4 +84,5 @@ endif() include(${CMAKE_CURRENT_LIST_DIR}/cache/build.cmake) include(${CMAKE_CURRENT_LIST_DIR}/crypto/build.cmake) include(${CMAKE_CURRENT_LIST_DIR}/libc/build.cmake) +include(${CMAKE_CURRENT_LIST_DIR}/log/build.cmake) include(${CMAKE_CURRENT_LIST_DIR}/textsearch/build.cmake) diff --git a/src/cache/cache.c b/src/cache/cache.c index 89ddc4c7..ac2f3a78 100644 --- a/src/cache/cache.c +++ b/src/cache/cache.c @@ -9,21 +9,35 @@ #include #include -static BFDEV_LIST_HEAD(cache_algorithms); +static +BFDEV_LIST_HEAD(cache_algorithms); static bfdev_cache_algo_t * cache_algorithm_find(const char *name) { - bfdev_cache_algo_t *algo; + bfdev_cache_algo_t *walk; - bfdev_list_for_each_entry(algo, &cache_algorithms, list) { - if (!bfport_strcmp(algo->name, name)) - return algo; + bfdev_list_for_each_entry(walk, &cache_algorithms, list) { + if (!bfport_strcmp(walk->name, name)) + return walk; } return NULL; } +static bool +cache_algorithm_exist(bfdev_cache_algo_t *algo) +{ + bfdev_cache_algo_t *walk; + + bfdev_list_for_each_entry(walk, &cache_algorithms, list) { + if (walk == algo) + return true; + } + + return false; +} + static __bfdev_always_inline bool cache_starving(bfdev_cache_head_t *head) { @@ -332,14 +346,17 @@ bfdev_cache_register(bfdev_cache_algo_t *algo) return -BFDEV_EALREADY; bfdev_list_add(&cache_algorithms, &algo->list); + return -BFDEV_ENOERR; } -export void +export int bfdev_cache_unregister(bfdev_cache_algo_t *algo) { - if (cache_algorithm_find(algo->name)) - return; + if (!cache_algorithm_exist(algo)) + return -BFDEV_ENOENT; bfdev_list_del(&algo->list); + + return -BFDEV_ENOERR; } diff --git a/src/cache/lfu.c b/src/cache/lfu.c index 07e57242..f37d6042 100644 --- a/src/cache/lfu.c +++ b/src/cache/lfu.c @@ -202,3 +202,9 @@ lfu_init(void) { return bfdev_cache_register(&lfu_algorithm); } + +static __bfdev_dtor int +lfu_exit(void) +{ + return bfdev_cache_unregister(&lfu_algorithm); +} diff --git a/src/cache/lru.c b/src/cache/lru.c index 9846afa9..ad772aa1 100644 --- a/src/cache/lru.c +++ b/src/cache/lru.c @@ -153,3 +153,9 @@ lru_init(void) { return bfdev_cache_register(&lru_algorithm); } + +static __bfdev_dtor int +lru_exit(void) +{ + return bfdev_cache_unregister(&lru_algorithm); +} diff --git a/src/crypto/ascii85.c b/src/crypto/ascii85.c index a4f5a6c7..8206d50b 100644 --- a/src/crypto/ascii85.c +++ b/src/crypto/ascii85.c @@ -11,127 +11,98 @@ #include static __bfdev_always_inline void -ascii85_encode(char *buff, const void *data, size_t size) +ascii85_encode(uint8_t *buff, const void *data, size_t *plen, size_t size) { uint32_t value; + size_t index; - for (; size >= 4; size -= 4) { + for (index = 0; size >= 4; size -= 4) { value = bfdev_unaligned_get_be32(data); data += 4; - if (value == 0) - *buff++ = 'z'; + if (!value) + buff[index] = 'z'; else { - buff[4] = '!' + value % 85; value /= 85; - buff[3] = '!' + value % 85; value /= 85; - buff[2] = '!' + value % 85; value /= 85; - buff[1] = '!' + value % 85; value /= 85; - buff[0] = '!' + value % 85; - buff += 5; + buff[index + 4] = '!' + value % 85; value /= 85; + buff[index + 3] = '!' + value % 85; value /= 85; + buff[index + 2] = '!' + value % 85; value /= 85; + buff[index + 1] = '!' + value % 85; value /= 85; + buff[index] = '!' + value % 85; + index += 4; } + + index++; } if (size) { value = bfdev_unaligned_get_be32(data); value &= BFDEV_BIT_HIGH_MASK((BFDEV_BYTES_PER_U32 - size) * BFDEV_BITS_PER_BYTE); - if (value == 0) - *buff++ = 'z'; + if (!value) + buff[index] = 'z'; else { - buff[4] = '!' + value % 85; value /= 85; - buff[3] = '!' + value % 85; value /= 85; - buff[2] = '!' + value % 85; value /= 85; - buff[1] = '!' + value % 85; value /= 85; - buff[0] = '!' + value % 85; - buff += 5; + buff[index + 4] = '!' + value % 85; value /= 85; + buff[index + 3] = '!' + value % 85; value /= 85; + buff[index + 2] = '!' + value % 85; value /= 85; + buff[index + 1] = '!' + value % 85; value /= 85; + buff[index] = '!' + value % 85; + index += 4; } + + index++; } - *buff = '\0'; + *plen = index; } static __bfdev_always_inline int -ascii85_decode(void *buff, const char *data, size_t size) +ascii85_decode(void *buff, const uint8_t *data, size_t *plen, size_t size) { - unsigned int index; + unsigned int count; uint32_t value; + size_t index; + + for (index = 0; size; index += 4) { + if (*data == 'z') { + bfport_memset(buff + index, 0, 4); + data++; + size--; + } - while (size) { - if (*data == 'z' && size >= 1) { - bfport_memset(buff, 0, 4); - data += 1; - size -= 1; - } else if (size >= 5) { - for (index = 0; index < 5; ++index) { - if (data[index] < '!' || 'u' < data[index]) + if (size >= 5) { + for (count = 0; count < 5; ++count) { + if (data[count] < '!' || 'u' < data[count]) return -BFDEV_EINVAL; } - value = data[0] - '!'; value *= 85; + value = data[0] - '!'; value *= 85; value += data[1] - '!'; value *= 85; value += data[2] - '!'; value *= 85; value += data[3] - '!'; value *= 85; value += data[4] - '!'; - bfdev_unaligned_set_be32(buff, value); + bfdev_unaligned_set_be32(buff + index, value); data += 5; size -= 5; - } else - return -BFDEV_EINVAL; + continue; + } - buff += 4; + return -BFDEV_EPROTO; } - return -BFDEV_ENOERR; -} - -static __bfdev_always_inline size_t -ascii85_encode_length(const uint32_t *data, size_t size) -{ - size_t csize, length; - uint32_t value; - - for (length = 0; (csize = bfdev_min(size, 4)); size -= csize) { - value = bfdev_unaligned_get_be32(data); - value &= BFDEV_BIT_HIGH_MASK((BFDEV_BYTES_PER_U32 - size) * BFDEV_BITS_PER_BYTE); - length += value ? 5 : 1; - data++; - } + *plen = index; - return length + 1; -} - -static __bfdev_always_inline size_t -ascii85_decode_length(const char *data, size_t size) -{ - while ((data = bfport_strchr(data, 'z'))) { - size += 4; - data++; - } - - return BFDEV_DIV_ROUND_UP(size, 5) * 4; + return -BFDEV_ENOERR; } export void -bfdev_ascii85_encode(void *buff, const void *data, size_t size) +bfdev_ascii85_encode(void *buff, const void *data, size_t *plen, size_t size) { - ascii85_encode(buff, data, size); + ascii85_encode(buff, data, plen, size); } export int -bfdev_ascii85_decode(void *buff, const void *data, size_t size) -{ - return ascii85_decode(buff, data, size); -} - -export size_t -bfdev_ascii85_encode_length(const void *data, size_t size) -{ - return ascii85_encode_length(data, size); -} - -export size_t -bfdev_ascii85_decode_length(const void *data, size_t size) +bfdev_ascii85_decode(void *buff, const void *data, size_t *plen, size_t size) { - return ascii85_decode_length(data, size); + return ascii85_decode(buff, data, plen, size); } diff --git a/src/crypto/base32.c b/src/crypto/base32.c index 470c30df..bfe21742 100644 --- a/src/crypto/base32.c +++ b/src/crypto/base32.c @@ -7,40 +7,34 @@ #include #include -static const char -base32_encode_table[] = { +static const uint8_t +base32_encode_table[32] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', }; -static const char -base32_decode_table[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, - 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, +static const uint8_t +base32_decode_table[256] = { + [0 ... 255] = 0xff, + ['A'] = 0x00, ['B'] = 0x01, ['C'] = 0x02, ['D'] = 0x03, + ['E'] = 0x04, ['F'] = 0x05, ['G'] = 0x06, ['H'] = 0x07, + ['I'] = 0x08, ['J'] = 0x09, ['K'] = 0x0a, ['L'] = 0x0b, + ['M'] = 0x0c, ['N'] = 0x0d, ['O'] = 0x0e, ['P'] = 0x0f, + ['Q'] = 0x10, ['R'] = 0x11, ['S'] = 0x12, ['T'] = 0x13, + ['U'] = 0x14, ['V'] = 0x15, ['W'] = 0x16, ['X'] = 0x17, + ['Y'] = 0x18, ['Z'] = 0x19, ['2'] = 0x1a, ['3'] = 0x1b, + ['4'] = 0x1c, ['5'] = 0x1d, ['6'] = 0x1e, ['7'] = 0x1f, }; static __bfdev_always_inline void base32_encode(uint8_t *buff, const uint8_t *data, size_t size) { - const uint8_t *prev = data; - unsigned int bstate = 0; + const uint8_t *prev; + unsigned int bstate; + prev = data; for (; size >= 5; size -= 5) { *buff++ = base32_encode_table[*data++ >> 3]; *buff++ = base32_encode_table[((*prev++ & 0x7) << 2) | (*data++ >> 6)]; @@ -52,6 +46,7 @@ base32_encode(uint8_t *buff, const uint8_t *data, size_t size) *buff++ = base32_encode_table[*prev++ & 0x1f]; } + bstate = 0; while (size--) switch (bstate) { case 0: *buff++ = base32_encode_table[*data++ >> 3]; @@ -90,7 +85,7 @@ base32_encode(uint8_t *buff, const uint8_t *data, size_t size) *buff++ = '='; *buff++ = '='; *buff++ = '='; - *buff++ = '='; + *buff = '='; break; case 2: @@ -98,42 +93,42 @@ base32_encode(uint8_t *buff, const uint8_t *data, size_t size) *buff++ = '='; *buff++ = '='; *buff++ = '='; - *buff++ = '='; + *buff = '='; break; case 3: *buff++ = base32_encode_table[(*prev & 0xf) << 1]; *buff++ = '='; *buff++ = '='; - *buff++ = '='; + *buff = '='; break; case 4: *buff++ = base32_encode_table[(*prev & 0x3) << 3]; - *buff++ = '='; + *buff = '='; break; default: break; } - - *buff = '\0'; } static __bfdev_always_inline int base32_decode(uint8_t *buff, const uint8_t *data, size_t size) { - unsigned int bstate = 0; + unsigned int bstate; uint8_t decode; + bstate = 0; while (size--) { - decode = base32_decode_table[*data++]; + decode = base32_decode_table[*data]; if (decode == 0xff) { - if (*data == '=') + if (bfdev_likely(*data == '=')) break; return -BFDEV_EINVAL; } + data++; switch (bstate++ & 0x7) { case 0: *buff = decode << 3; diff --git a/src/crypto/base64.c b/src/crypto/base64.c index ec32ccd5..869c50e4 100644 --- a/src/crypto/base64.c +++ b/src/crypto/base64.c @@ -7,8 +7,8 @@ #include #include -static const char -base64_encode_table[] = { +static const uint8_t +base64_encode_table[64] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', @@ -19,32 +19,34 @@ base64_encode_table[] = { '4', '5', '6', '7', '8', '9', '+', '/', }; -static const char -base64_decode_table[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, - 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, - 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, - 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, - 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, - 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, - 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff, +static const uint8_t +base64_decode_table[256] = { + [0 ... 255] = 0xff, + ['A'] = 0x00, ['B'] = 0x01, ['C'] = 0x02, ['D'] = 0x03, + ['E'] = 0x04, ['F'] = 0x05, ['G'] = 0x06, ['H'] = 0x07, + ['I'] = 0x08, ['J'] = 0x09, ['K'] = 0x0a, ['L'] = 0x0b, + ['M'] = 0x0c, ['N'] = 0x0d, ['O'] = 0x0e, ['P'] = 0x0f, + ['Q'] = 0x10, ['R'] = 0x11, ['S'] = 0x12, ['T'] = 0x13, + ['U'] = 0x14, ['V'] = 0x15, ['W'] = 0x16, ['X'] = 0x17, + ['Y'] = 0x18, ['Z'] = 0x19, ['a'] = 0x1a, ['b'] = 0x1b, + ['c'] = 0x1c, ['d'] = 0x1d, ['e'] = 0x1e, ['f'] = 0x1f, + ['g'] = 0x20, ['h'] = 0x21, ['i'] = 0x22, ['j'] = 0x23, + ['k'] = 0x24, ['l'] = 0x25, ['m'] = 0x26, ['n'] = 0x27, + ['o'] = 0x28, ['p'] = 0x29, ['q'] = 0x2a, ['r'] = 0x2b, + ['s'] = 0x2c, ['t'] = 0x2d, ['u'] = 0x2e, ['v'] = 0x2f, + ['w'] = 0x30, ['x'] = 0x31, ['y'] = 0x32, ['z'] = 0x33, + ['0'] = 0x34, ['1'] = 0x35, ['2'] = 0x36, ['3'] = 0x37, + ['4'] = 0x38, ['5'] = 0x39, ['6'] = 0x3a, ['7'] = 0x3b, + ['8'] = 0x3c, ['9'] = 0x3d, ['+'] = 0x3e, ['/'] = 0x3f, }; static __bfdev_always_inline void base64_encode(uint8_t *buff, const uint8_t *data, size_t size) { - const uint8_t *prev = data; - unsigned int bstate = 0; + const uint8_t *prev; + unsigned int bstate; + prev = data; for (; size >= 3; size -= 3) { *buff++ = base64_encode_table[*data++ >> 2]; *buff++ = base64_encode_table[((*prev++ & 0x3) << 4) | (*data++ >> 4)]; @@ -52,6 +54,7 @@ base64_encode(uint8_t *buff, const uint8_t *data, size_t size) *buff++ = base64_encode_table[*prev++ & 0x3f]; } + bstate = 0; while (size--) switch (bstate) { case 0: *buff++ = base64_encode_table[*data++ >> 2]; @@ -74,35 +77,35 @@ base64_encode(uint8_t *buff, const uint8_t *data, size_t size) case 1: *buff++ = base64_encode_table[(*prev & 0x3) << 4]; *buff++ = '='; - *buff++ = '='; + *buff = '='; break; case 2: *buff++ = base64_encode_table[(*prev & 0xf) << 2]; - *buff++ = '='; + *buff = '='; break; default: break; } - - *buff = '\0'; } static __bfdev_always_inline int base64_decode(uint8_t *buff, const uint8_t *data, size_t size) { - unsigned int bstate = 0; + unsigned int bstate; uint8_t decode; + bstate = 0; while (size--) { - decode = base64_decode_table[*data++]; + decode = base64_decode_table[*data]; if (decode == 0xff) { - if (*data == '=') + if (bfdev_likely(*data == '=')) break; return -BFDEV_EINVAL; } + data++; switch (bstate++ & 0x3) { case 0: *buff = decode << 2; diff --git a/src/fifo.c b/src/fifo.c index cf5326db..2be03dfb 100644 --- a/src/fifo.c +++ b/src/fifo.c @@ -28,7 +28,8 @@ } while (0) static __bfdev_always_inline void -fifo_out_copy(struct bfdev_fifo *fifo, void *buff, unsigned long len, unsigned long offset) +fifo_out_copy(bfdev_fifo_t *fifo, void *buff, unsigned long len, + unsigned long offset) { FIFO_GENERIC_COPY( buff, fifo->data + offset, @@ -37,7 +38,8 @@ fifo_out_copy(struct bfdev_fifo *fifo, void *buff, unsigned long len, unsigned l } static __bfdev_always_inline void -fifo_in_copy(struct bfdev_fifo *fifo, const void *buff, unsigned long len, unsigned long offset) +fifo_in_copy(bfdev_fifo_t *fifo, const void *buff, unsigned long len, + unsigned long offset) { FIFO_GENERIC_COPY( fifo->data + offset, buff, @@ -46,7 +48,7 @@ fifo_in_copy(struct bfdev_fifo *fifo, const void *buff, unsigned long len, unsig } static __bfdev_always_inline unsigned long -fifo_record_peek(struct bfdev_fifo *fifo, unsigned long recsize) +fifo_record_peek(bfdev_fifo_t *fifo, unsigned long recsize) { unsigned long mask, offset, length; uint8_t *data; @@ -72,8 +74,7 @@ fifo_record_peek(struct bfdev_fifo *fifo, unsigned long recsize) } static __bfdev_always_inline void -fifo_record_poke(struct bfdev_fifo *fifo, unsigned long len, - unsigned long recsize) +fifo_record_poke(bfdev_fifo_t *fifo, unsigned long len, unsigned long recsize) { unsigned long mask, offset; uint8_t *data; @@ -96,26 +97,25 @@ fifo_record_poke(struct bfdev_fifo *fifo, unsigned long len, } static inline bool -fifo_empty(struct bfdev_fifo *fifo) +fifo_empty(bfdev_fifo_t *fifo) { return fifo->in == fifo->out; } static inline unsigned long -fifo_valid(struct bfdev_fifo *fifo) +fifo_valid(bfdev_fifo_t *fifo) { return fifo->in - fifo->out; } static inline unsigned long -fifo_unused(struct bfdev_fifo *fifo) +fifo_unused(bfdev_fifo_t *fifo) { return (fifo->mask + 1) - (fifo->in - fifo->out); } export unsigned long -bfdev_fifo_peek_flat(struct bfdev_fifo *fifo, void *buff, - unsigned long len) +bfdev_fifo_peek_flat(bfdev_fifo_t *fifo, void *buff, unsigned long len) { unsigned long valid; @@ -127,8 +127,7 @@ bfdev_fifo_peek_flat(struct bfdev_fifo *fifo, void *buff, } export unsigned long -bfdev_fifo_out_flat(struct bfdev_fifo *fifo, void *buff, - unsigned long len) +bfdev_fifo_out_flat(bfdev_fifo_t *fifo, void *buff, unsigned long len) { unsigned long llen; @@ -139,8 +138,7 @@ bfdev_fifo_out_flat(struct bfdev_fifo *fifo, void *buff, } export unsigned long -bfdev_fifo_in_flat(struct bfdev_fifo *fifo, const void *buff, - unsigned long len) +bfdev_fifo_in_flat(bfdev_fifo_t *fifo, const void *buff, unsigned long len) { unsigned long unused; @@ -153,8 +151,8 @@ bfdev_fifo_in_flat(struct bfdev_fifo *fifo, const void *buff, } export unsigned long -bfdev_fifo_peek_record(struct bfdev_fifo *fifo, void *buff, - unsigned long len, unsigned long record) +bfdev_fifo_peek_record(bfdev_fifo_t *fifo, void *buff, unsigned long len, + unsigned long record) { unsigned long datalen; @@ -169,8 +167,8 @@ bfdev_fifo_peek_record(struct bfdev_fifo *fifo, void *buff, } export unsigned long -bfdev_fifo_out_record(struct bfdev_fifo *fifo, void *buff, - unsigned long len, unsigned long record) +bfdev_fifo_out_record(bfdev_fifo_t *fifo, void *buff, unsigned long len, + unsigned long record) { unsigned long datalen; @@ -186,8 +184,8 @@ bfdev_fifo_out_record(struct bfdev_fifo *fifo, void *buff, } export unsigned long -bfdev_fifo_in_record(struct bfdev_fifo *fifo, const void *buff, - unsigned long len, unsigned long record) +bfdev_fifo_in_record(bfdev_fifo_t *fifo, const void *buff, unsigned long len, + unsigned long record) { if (len + record > fifo_unused(fifo)) return 0; @@ -200,7 +198,7 @@ bfdev_fifo_in_record(struct bfdev_fifo *fifo, const void *buff, } export int -bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const bfdev_alloc_t *alloc, +bfdev_fifo_dynamic_alloc(bfdev_fifo_t *fifo, const bfdev_alloc_t *alloc, size_t esize, size_t size) { size = bfdev_pow2_roundup(size); @@ -221,7 +219,7 @@ bfdev_fifo_dynamic_alloc(struct bfdev_fifo *fifo, const bfdev_alloc_t *alloc, } export void -bfdev_fifo_dynamic_free(struct bfdev_fifo *fifo) +bfdev_fifo_dynamic_free(bfdev_fifo_t *fifo) { const bfdev_alloc_t *alloc; diff --git a/src/log.c b/src/log.c deleted file mode 100644 index 37c6c04d..00000000 --- a/src/log.c +++ /dev/null @@ -1,156 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2023 John Sanpe - */ - -#include -#include -#include -#include -#include - -static const unsigned int -level_color[] = { - [BFDEV_LEVEL_EMERG ] = BFDEV_COLR_RED, - [BFDEV_LEVEL_ALERT ] = BFDEV_COLR_DARK_MAGENTA, - [BFDEV_LEVEL_CRIT ] = BFDEV_COLR_MAGENTA, - [BFDEV_LEVEL_ERR ] = BFDEV_COLR_YELLOW, - [BFDEV_LEVEL_WARNING ] = BFDEV_COLR_BLUE, - [BFDEV_LEVEL_NOTICE ] = BFDEV_COLR_CYAN, - [BFDEV_LEVEL_INFO ] = BFDEV_COLR_GREEN, - [BFDEV_LEVEL_DEBUG ] = BFDEV_COLR_DARK_GRAY, - [BFDEV_LEVEL_DEFAULT ] = BFDEV_COLR_DEFAULT, -}; - -static const char * const -level_name[] = { - [BFDEV_LEVEL_EMERG ] = "emerg", - [BFDEV_LEVEL_ALERT ] = "alert", - [BFDEV_LEVEL_CRIT ] = "crit", - [BFDEV_LEVEL_ERR ] = "error", - [BFDEV_LEVEL_WARNING ] = "warning", - [BFDEV_LEVEL_NOTICE ] = "notice", - [BFDEV_LEVEL_INFO ] = "info", - [BFDEV_LEVEL_DEBUG ] = "debug", - [BFDEV_LEVEL_DEFAULT ] = "default", -}; - -export struct bfdev_log -bfdev_log_default = { - .default_level = BFDEV_LEVEL_DEFAULT, - .record_level = BFDEV_LEVEL_DEFAULT, - .flags = BFDEV_LOG_COLOR | BFDEV_LOG_COMMIT, -}; - -#define __INSIDE_LOG__ -#include - -static inline char -log_get_level(const char *str) -{ - if (str[0] == BFDEV_SOH_ASCII && str[1]) - return str[1]; - return 0; -} - -extern unsigned int -bfdev_log_level(const char *str, const char **endptr) -{ - unsigned int level; - char value; - - for (level = BFDEV_LEVEL_DEFAULT; *str; str += 2) { - value = log_get_level(str); - if (!value) - break; - - switch (value) { - case '0' ... '9': - level = value - '0'; - break; - - default: - break; - } - } - - if (*endptr) - *endptr = str; - - return level; -} - -export int -bfdev_log_state_vprint(struct bfdev_log *log, const char *fmt, va_list args) -{ - char buff[BFDEV_LOG_BUFF_SIZE]; - bfdev_log_message_t msg; - unsigned int level; - size_t offset; - int retval; - - level = bfdev_log_level(fmt, &fmt); - if (level >= BFDEV_LEVEL_DEFAULT) - level = log->default_level; - - if (level > log->record_level) - return 0; - - offset = 0; - if (bfdev_log_test_commit(log)) { - retval = bfdev_scnprintf( - buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - "[%s] ", level_name[level] - ); - offset += retval; - } - - if (bfdev_log_test_color(log)) { - retval = bfdev_scnprintf( - buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - "\e[%dm", level_color[level] - ); - offset += retval; - } - - retval = bfdev_vscnprintf( - buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - fmt, args - ); - - if (retval < 0) - return retval; - offset += retval; - - if (bfdev_log_test_color(log)) { - retval = bfdev_scnprintf( - buff + offset, BFDEV_LOG_BUFF_SIZE - offset, - "\e[0m" - ); - offset += retval; - } - - msg.data = buff; - msg.length = offset; - msg.level = level; - - if (log->write) - offset = log->write(&msg, log->pdata); - else - generic_log_write(&msg); - - return offset; -} - -export int -bfdev_log_state_print(struct bfdev_log *log, const char *fmt, ...) -{ - va_list para; - int length; - - va_start(para,fmt); - length = bfdev_log_state_vprint(log, fmt, para); - va_end(para); - - return length; -} diff --git a/src/log/build.cmake b/src/log/build.cmake new file mode 100644 index 00000000..f08e14b9 --- /dev/null +++ b/src/log/build.cmake @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 John Sanpe +# + +set(BFDEV_SOURCE + ${BFDEV_SOURCE} + ${CMAKE_CURRENT_LIST_DIR}/log.c +) diff --git a/src/log/color.c b/src/log/color.c new file mode 100644 index 00000000..e23c58b7 --- /dev/null +++ b/src/log/color.c @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#define COLOR_BLACK 0 +#define COLOR_RED 1 +#define COLOR_GREEN 2 +#define COLOR_YELLOW 3 +#define COLOR_BLUE 4 +#define COLOR_MAGENTA 5 +#define COLOR_CYAN 6 +#define COLOR_WHITE 7 +#define COLOR_DEFAULT 9 + +#define COLOR_FG(color) (30 + (color)) +#define COLOR_FG_BRIGHT(color) (90 + (color)) + +static const unsigned int +level_color[] = { + [BFDEV_LEVEL_EMERG ] = COLOR_FG_BRIGHT(COLOR_RED), + [BFDEV_LEVEL_ALERT ] = COLOR_FG(COLOR_RED), + [BFDEV_LEVEL_CRIT ] = COLOR_FG(COLOR_MAGENTA), + [BFDEV_LEVEL_ERR ] = COLOR_FG(COLOR_YELLOW), + [BFDEV_LEVEL_WARNING] = COLOR_FG(COLOR_BLUE), + [BFDEV_LEVEL_NOTICE ] = COLOR_FG_BRIGHT(COLOR_CYAN), + [BFDEV_LEVEL_INFO ] = COLOR_FG_BRIGHT(COLOR_GREEN), + [BFDEV_LEVEL_DEBUG ] = COLOR_FG_BRIGHT(COLOR_BLACK), + [BFDEV_LEVEL_DEFAULT] = COLOR_FG(COLOR_DEFAULT), +}; + +static void +log_color_prefix(bfdev_log_t *log, bfdev_log_message_t *msg) +{ + if (!bfdev_log_test_color(log)) + return; + + msg->length += bfdev_scnprintf( + msg->buff + msg->length, BFDEV_LOG_BUFF_SIZE - msg->length, + "\e[%dm", level_color[msg->level] + ); +} + +static void +log_color_suffix(bfdev_log_t *log, bfdev_log_message_t *msg) +{ + if (!bfdev_log_test_color(log)) + return; + + msg->length += bfdev_scnprintf( + msg->buff + msg->length, BFDEV_LOG_BUFF_SIZE - msg->length, + "\e[0m" + ); +} diff --git a/src/log/level.c b/src/log/level.c new file mode 100644 index 00000000..ba867867 --- /dev/null +++ b/src/log/level.c @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +static const char * const +level_name[] = { + [BFDEV_LEVEL_EMERG ] = "emerg", + [BFDEV_LEVEL_ALERT ] = "alert", + [BFDEV_LEVEL_CRIT ] = "crit", + [BFDEV_LEVEL_ERR ] = "error", + [BFDEV_LEVEL_WARNING] = "warning", + [BFDEV_LEVEL_NOTICE ] = "notice", + [BFDEV_LEVEL_INFO ] = "info", + [BFDEV_LEVEL_DEBUG ] = "debug", + [BFDEV_LEVEL_DEFAULT] = "default", +}; + +static void +log_level(bfdev_log_t *log, bfdev_log_message_t *msg) +{ + if (!bfdev_log_test_level(log)) + return; + + msg->length = bfdev_scnprintf( + msg->buff + msg->length, BFDEV_LOG_BUFF_SIZE - msg->length, + "[%s] ", level_name[msg->level] + ); +} diff --git a/src/log/log.c b/src/log/log.c new file mode 100644 index 00000000..03eb0201 --- /dev/null +++ b/src/log/log.c @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#include +#include +#include +#include +#include + +export +BFDEV_DEFINE_LOG( + bfdev_log_default, + BFDEV_LEVEL_DEFAULT, BFDEV_LEVEL_DEFAULT, + BFDEV_LOG_COLOR | BFDEV_LOG_LEVEL, + NULL, NULL +); + +#define __INSIDE_LOG__ +#include + +#include "color.c" +#include "level.c" + +static inline char +log_get_level(const char *str) +{ + if (str[0] == BFDEV_SOH_ASCII && str[1]) + return str[1]; + + return 0; +} + +extern unsigned int +bfdev_log_level(const char *str, const char **endptr) +{ + unsigned int level; + char value; + + for (level = BFDEV_LEVEL_DEFAULT; *str; str += 2) { + value = log_get_level(str); + if (!value) + break; + + switch (value) { + case '0' ... '9': + level = value - '0'; + break; + + default: + break; + } + } + + if (*endptr) + *endptr = str; + + return level; +} + +static int +log_prefix(bfdev_log_t *log, bfdev_log_message_t *msg) +{ + log_level(log, msg); + log_color_prefix(log, msg); + + return -BFDEV_ENOERR; +} + +static int +log_suffix(bfdev_log_t *log, bfdev_log_message_t *msg) +{ + log_color_suffix(log, msg); + + return -BFDEV_ENOERR; +} + +static int +log_emit(bfdev_log_t *log, unsigned int level, const char *fmt, va_list args) +{ + char buff[BFDEV_LOG_BUFF_SIZE]; + bfdev_log_message_t msg; + int retval; + + if (level >= BFDEV_LEVEL_DEFAULT) + level = log->default_level; + + if (level > log->record_level) + return 0; + + msg.level = level; + msg.buff = buff; + msg.length = 0; + + retval = log_prefix(log, &msg); + if (bfdev_unlikely(retval)) + return retval; + + msg.length += bfdev_vscnprintf( + buff + msg.length, BFDEV_LOG_BUFF_SIZE - msg.length, + fmt, args + ); + + retval = log_suffix(log, &msg); + if (bfdev_unlikely(retval)) + return retval; + + if (log->write) + retval = log->write(&msg, log->pdata); + else + retval = generic_log_write(&msg); + + return retval; +} + +export int +bfdev_log_state_vprint(bfdev_log_t *log, const char *fmt, va_list args) +{ + unsigned int level; + + level = bfdev_log_level(fmt, &fmt); + + return log_emit(log, level, fmt, args); +} + +export int +bfdev_log_state_print(bfdev_log_t *log, const char *fmt, ...) +{ + va_list para; + int length; + + va_start(para, fmt); + length = bfdev_log_state_vprint(log, fmt, para); + va_end(para); + + return length; +} diff --git a/src/memalloc.c b/src/memalloc.c new file mode 100644 index 00000000..ca4209c2 --- /dev/null +++ b/src/memalloc.c @@ -0,0 +1,292 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2021 John Sanpe + */ + +#define MODULE_NAME "bfdev-memalloc" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include + +static __bfdev_always_inline bool +pnode_get_used(bfdev_memalloc_chunk_t *node) +{ + return node->usize & BFDEV_BIT(0); +} + +static __bfdev_always_inline size_t +pnode_get_size(bfdev_memalloc_chunk_t *node) +{ + return node->usize & BFDEV_BIT_HIGH_MASK(1); +} + +static __bfdev_always_inline void +pnode_set_used(bfdev_memalloc_chunk_t *node, bool used) +{ + node->usize &= ~BFDEV_BIT(0); + node->usize |= used; +} + +static __bfdev_always_inline void +pnode_set_size(bfdev_memalloc_chunk_t *node, size_t size) +{ + node->usize &= ~BFDEV_BIT_HIGH_MASK(1); + node->usize |= size & BFDEV_BIT_HIGH_MASK(1); +} + +static __bfdev_always_inline void +pnode_set(bfdev_memalloc_chunk_t *node, size_t size, bool used) +{ + node->usize = (size & BFDEV_BIT_HIGH_MASK(1)) | used; +} + +static inline bfdev_memalloc_chunk_t * +memalloc_check(void *block) +{ + bfdev_memalloc_chunk_t *node; + + /* Check whether it's a legal node */ + node = bfdev_container_of(block, bfdev_memalloc_chunk_t, data); + +#ifdef BFDEV_DEBUG_MEMALLOC + if (bfdev_unlikely(!bfdev_list_check_empty(&node->free))) { + bfdev_log_crit("unlegal node at %p\n", block); + return NULL; + } +#endif + + return node; +} + +export bfdev_memalloc_chunk_t * +bfdev_memalloc_first_fit(bfdev_memalloc_head_t *head, size_t size) +{ + bfdev_memalloc_chunk_t *node; + + bfdev_list_for_each_entry(node, &head->free_list, free) { + if (pnode_get_size(node) >= size) + return node; + } + + return NULL; +} + +export bfdev_memalloc_chunk_t * +bfdev_memalloc_best_fit(bfdev_memalloc_head_t *head, size_t size) +{ + bfdev_memalloc_chunk_t *best, *node; + size_t walk, bsize; + + best = NULL; + bsize = BFDEV_SIZE_MAX; + + bfdev_list_for_each_entry(node, &head->free_list, free) { + walk = pnode_get_size(node); + if (walk >= size) { + if (walk == size) + return node; + else if (walk >= bsize) + continue; + best = node; + bsize = walk; + } + } + + return best; +} + +export bfdev_memalloc_chunk_t * +bfdev_memalloc_worst_fit(bfdev_memalloc_head_t *head, size_t size) +{ + bfdev_memalloc_chunk_t *worst, *node; + size_t walk, bsize; + + worst = NULL; + bsize = BFDEV_SIZE_MIN; + + bfdev_list_for_each_entry(node, &head->free_list, free) { + walk = pnode_get_size(node); + if (walk >= size) { + if (walk == head->avail) + return node; + else if (walk <= bsize) + continue; + worst = node; + bsize = walk; + } + } + + return worst; +} + +export void * +bfdev_memalloc_alloc(bfdev_memalloc_head_t *head, size_t size) +{ + bfdev_memalloc_chunk_t *node, *free; + size_t nsize, bsize, fsize; + + bfdev_align_high_adj(size, BFDEV_MEMALLOC_ALIGN); + if (bfdev_unlikely(size > head->avail)) + return NULL; + + /* Get the free memory block */ + node = head->find(head, size); + if (bfdev_unlikely(!node)) + return NULL; + + /* Adjust available size */ + nsize = pnode_get_size(node); + head->avail -= nsize; + + bsize = nsize - size; + if (bsize < sizeof(*node) + BFDEV_MEMALLOC_ALIGN) + goto finish; + + /* Setup the new free node */ + free = (void *)node->data + size; + fsize = bsize - sizeof(*free); + + pnode_set(free, fsize, false); + head->avail += fsize; + + bfdev_list_add(&node->block, &free->block); + bfdev_list_add(&head->free_list, &free->free); + pnode_set_size(node, size); + +finish: + /* Set node used */ + pnode_set_used(node, true); + bfdev_list_del_init(&node->free); + return node->data; +} + +export void * +bfdev_memalloc_realloc(bfdev_memalloc_head_t *head, void *block, size_t resize) +{ + bfdev_memalloc_chunk_t *node, *expand, *free; + size_t origin, exsize, nsize, bsize, fsize; + + node = memalloc_check(block); + if (bfdev_unlikely(!node)) + return NULL; + + bfdev_align_high_adj(resize, BFDEV_MEMALLOC_ALIGN); + if (bfdev_unlikely(resize > head->avail)) + return NULL; + + origin = pnode_get_size(node); + if (origin >= resize) + return block; + + expand = bfdev_list_next_entry_or_null(node, &head->block_list, block); + exsize = resize - origin; + + /* Check if direct expand is possible */ + if (!expand || pnode_get_used(expand) || (sizeof(*expand) + + pnode_get_size(expand)) < exsize) { + void *newblk; + + newblk = bfdev_memalloc_alloc(head, resize); + if (bfdev_unlikely(!newblk)) + return NULL; + + bfport_memcpy(newblk, block, origin); + bfdev_memalloc_free(head, block); + + return newblk; + } + + /* Adjust available size */ + nsize = pnode_get_size(expand); + head->avail -= nsize; + + bfdev_list_del(&expand->free); + bfdev_list_del(&expand->block); + + /* Use all space of the next node */ + bsize = sizeof(*expand) + nsize - exsize; + if (bsize < sizeof(*free) + BFDEV_MEMALLOC_ALIGN) { + exsize = sizeof(*node) + nsize; + resize = origin + exsize; + goto finish; + } + + /* Setup the new free node */ + free = (void *)expand + exsize; + fsize = bsize - sizeof(*free); + + pnode_set(free, fsize, false); + head->avail += fsize; + + bfdev_list_add(&node->block, &free->block); + bfdev_list_add(&head->free_list, &free->free); + +finish: + pnode_set_size(node, resize); + return block; +} + +export void +bfdev_memalloc_free(bfdev_memalloc_head_t *head, void *block) +{ + bfdev_memalloc_chunk_t *side, *node; + size_t nsize, fsize; + + node = memalloc_check(block); + if (bfdev_unlikely(!node)) + return; + + /* Set node freed */ + pnode_set_used(node, false); + bfdev_list_add(&head->free_list, &node->free); + + /* Adjust available size */ + nsize = pnode_get_size(node); + head->avail += nsize; + + /* Merge next node */ + side = bfdev_list_next_entry_or_null(node, &head->block_list, block); + if (side && !pnode_get_used(side)) { + bfdev_list_del(&side->free); + bfdev_list_del(&side->block); + + /* node size = this node + next node + next size */ + fsize = sizeof(*side) + pnode_get_size(side); + pnode_set_size(node, pnode_get_size(node) + fsize); + head->avail += sizeof(*side); + } + + /* Merge prev node */ + side = bfdev_list_prev_entry_or_null(node, &head->block_list, block); + if (side && !pnode_get_used(side)) { + bfdev_list_del(&node->free); + bfdev_list_del(&node->block); + + /* prev size = prev size + this node + this size */ + fsize = sizeof(*node) + pnode_get_size(node); + pnode_set_size(side, pnode_get_size(side) + fsize); + head->avail += sizeof(*node); + } +} + +export void +bfdev_memalloc_init(bfdev_memalloc_head_t *head, bfdev_memalloc_find_t find, + void *array, size_t size) +{ + bfdev_memalloc_chunk_t *node; + + bfdev_list_head_init(&head->block_list); + bfdev_list_head_init(&head->free_list); + head->find = find; + + node = array; + pnode_set(node, size - sizeof(*node), false); + head->avail = size - sizeof(*node); + + bfdev_list_add(&head->block_list, &node->block); + bfdev_list_add(&head->free_list, &node->free); +} diff --git a/src/minpool.c b/src/minpool.c deleted file mode 100644 index 2f6f4bfc..00000000 --- a/src/minpool.c +++ /dev/null @@ -1,277 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2021 John Sanpe - */ - -#include -#include -#include -#include - -static __bfdev_always_inline bool -minnode_get_used(struct bfdev_minpool_node *node) -{ - return node->usize & BFDEV_BIT(0); -} - -static __bfdev_always_inline size_t -minnode_get_size(struct bfdev_minpool_node *node) -{ - return node->usize & BFDEV_BIT_HIGH_MASK(1); -} - -static __bfdev_always_inline void -minnode_set_used(struct bfdev_minpool_node *node, bool used) -{ - node->usize &= ~BFDEV_BIT(0); - node->usize |= used; -} - -static __bfdev_always_inline void -minnode_set_size(struct bfdev_minpool_node *node, size_t size) -{ - node->usize &= ~BFDEV_BIT_HIGH_MASK(0); - node->usize |= size & BFDEV_BIT_HIGH_MASK(1); -} - -static __bfdev_always_inline void -minnode_set(struct bfdev_minpool_node *node, size_t size, bool used) -{ - node->usize = (size & BFDEV_BIT_HIGH_MASK(1)) | used; -} - -static inline struct bfdev_minpool_node * -minpool_check(void *block) -{ - struct bfdev_minpool_node *node; - - /* Check whether it's a legal node */ - node = bfdev_container_of(block, struct bfdev_minpool_node, data); - if (bfdev_unlikely(!bfdev_list_check_empty(&node->free))) - return NULL; - - return node; -} - -export struct bfdev_minpool_node * -bfdev_minpool_first_fit(struct bfdev_minpool_head *head, size_t size) -{ - struct bfdev_minpool_node *node; - - bfdev_list_for_each_entry(node, &head->free_list, free) { - if (minnode_get_size(node) >= size) - return node; - } - - return NULL; -} - -export struct bfdev_minpool_node * -bfdev_minpool_best_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; - - bfdev_list_for_each_entry(node, &head->free_list, free) { - walk = minnode_get_size(node); - if (walk >= size) { - if (walk == size) - return node; - else if (walk >= bsize) - continue; - best = node; - bsize = walk; - } - } - - return best; -} - -export struct bfdev_minpool_node * -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_MIN; - - bfdev_list_for_each_entry(node, &head->free_list, free) { - walk = minnode_get_size(node); - if (walk >= size) { - if (walk == head->avail) - return node; - else if (walk <= bsize) - continue; - best = node; - bsize = walk; - } - } - - return best; -} - -export void * -bfdev_minpool_alloc(struct bfdev_minpool_head *head, size_t size) -{ - struct bfdev_minpool_node *node; - size_t fsize; - - if (bfdev_unlikely(!size)) - return NULL; - - bfdev_align_high_adj(size, BFDEV_MINPOOL_ALIGN); - if (bfdev_unlikely(size > head->avail)) - return NULL; - - /* Get the free memory block */ - node = head->find(head, size); - if (bfdev_unlikely(!node)) - return NULL; - - fsize = minnode_get_size(node); - if (fsize - size >= sizeof(*node) + BFDEV_MINPOOL_ALIGN) { - struct bfdev_minpool_node *free; - - /* Setup the new free block */ - free = (void *)node->data + size; - minnode_set(free, fsize - size - sizeof(*free), false); - - bfdev_list_add(&head->free_list, &free->free); - bfdev_list_add(&node->block, &free->block); - - minnode_set_size(node, size); - head->avail -= sizeof(*free); - fsize = size; - } - - /* Set node used */ - minnode_set_used(node, true); - bfdev_list_del_init(&node->free); - - /* Adjust heap available size */ - head->avail -= fsize; - return node->data; -} - -export void -bfdev_minpool_free(struct bfdev_minpool_head *head, void *block) -{ - struct bfdev_minpool_node *other, *node; - size_t fsize; - - if (bfdev_unlikely(!block)) - return; - - node = minpool_check(block); - if (bfdev_unlikely(!node)) - return; - - /* Set node freed */ - minnode_set_used(node, false); - bfdev_list_add(&head->free_list, &node->free); - - fsize = minnode_get_size(node); - head->avail += fsize; - - /* Merge next node */ - other = bfdev_list_next_entry_or_null(node, &head->block_list, block); - if (other && !minnode_get_used(other)) { - /* node size = this node + next node + next size */ - minnode_set_size(node, fsize + sizeof(*other) + minnode_get_size(other)); - bfdev_list_del(&other->block); - bfdev_list_del(&other->free); - head->avail += sizeof(*other); - } - - /* Merge prev node */ - other = bfdev_list_prev_entry_or_null(node, &head->block_list, block); - if (other && !minnode_get_used(other)) { - /* prev size = prev size + this node + this size */ - minnode_set_size(other, minnode_get_size(other) + sizeof(*node) + fsize); - bfdev_list_del(&node->block); - bfdev_list_del(&node->free); - head->avail += sizeof(*node); - } -} - -export void * -bfdev_minpool_realloc(struct bfdev_minpool_head *head, void *block, size_t resize) -{ - struct bfdev_minpool_node *node, *expand; - size_t origin, exsize, fsize; - - if (bfdev_unlikely(!block)) - return bfdev_minpool_alloc(head, resize); - - if (bfdev_unlikely(!resize)) { - bfdev_minpool_free(head, block); - return NULL; - } - - node = minpool_check(block); - if (bfdev_unlikely(!node)) - return NULL; - - bfdev_align_high_adj(resize, BFDEV_MINPOOL_ALIGN); - if (bfdev_unlikely(resize > head->avail)) - return NULL; - - origin = minnode_get_size(node); - if (origin >= resize) - return block; - - expand = bfdev_list_next_entry_or_null(node, &head->block_list, block); - if (!expand || minnode_get_used(expand) || sizeof(*expand) + - minnode_get_size(expand) < (exsize = resize - origin)) { - void *newblk; - - newblk = bfdev_minpool_alloc(head, resize); - if (bfdev_unlikely(!newblk)) - return NULL; - - bfport_memcpy(newblk, block, origin); - bfdev_minpool_free(head, block); - - return newblk; - } - - fsize = minnode_get_size(expand); - bfdev_list_del(&expand->block); - bfdev_list_del(&expand->free); - - if (fsize - exsize < sizeof(*node) + BFDEV_MINPOOL_ALIGN) { - /* Use all space of the next node */ - exsize = sizeof(*node) + fsize; - resize = origin + exsize; - } else { - /* Detach free node */ - expand = (void *)expand + exsize; - minnode_set(expand, fsize - exsize - sizeof(*node), false); - bfdev_list_add(&node->block, &expand->block); - bfdev_list_add(&head->free_list, &expand->free); - } - - minnode_set_size(node, resize); - head->avail -= exsize; - - return block; -} - -export void -bfdev_minpool_setup(struct bfdev_minpool_head *head, bfdev_minpool_find_t find, - void *array, size_t size) -{ - struct bfdev_minpool_node *node; - - bfdev_list_head_init(&head->block_list); - bfdev_list_head_init(&head->free_list); - head->find = find; - - node = array; - minnode_set_used(node, false); - minnode_set_size(node, size - sizeof(*node)); - head->avail = size - sizeof(*node); - - bfdev_list_add(&head->block_list, &node->block); - bfdev_list_add(&head->free_list, &node->free); -} diff --git a/src/notifier.c b/src/notifier.c index 7d236f6f..f765d605 100644 --- a/src/notifier.c +++ b/src/notifier.c @@ -3,12 +3,8 @@ * Copyright(c) 2021 John Sanpe */ -#define MODULE_NAME "bfdev-notifier" -#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt - #include #include -#include #include #define ilist_to_notifier(ptr) \ @@ -18,15 +14,12 @@ static long notifier_chain_cmp(const bfdev_ilist_node_t *node1, const bfdev_ilist_node_t *node2, void *pdata) { - bfdev_notifier_node_t *nnode1, *nnode2; - - nnode1 = ilist_to_notifier(node1); - nnode2 = ilist_to_notifier(node2); + int prio1, prio2; - if (nnode1->priority == nnode2->priority) - return 0; + prio1 = ilist_to_notifier(node1)->priority; + prio2 = ilist_to_notifier(node2)->priority; - return nnode1->priority < nnode2->priority ? -1 : 1; + return bfdev_cmp(prio1 > prio2); } export bfdev_notifier_ret_t @@ -41,9 +34,7 @@ bfdev_notifier_call(bfdev_notifier_t *head, void *arg, if (!call_num--) break; - bfdev_log_debug("chain '%s' calling (%p)\n", head->name, node); retval = node->entry(arg, node->pdata); - if (called_num) (*called_num)++; @@ -67,7 +58,6 @@ bfdev_notifier_register(bfdev_notifier_t *head, bfdev_notifier_node_t *node) bfdev_ilist_node_init(&node->list); bfdev_ilist_add(&head->nodes, &node->list, notifier_chain_cmp, NULL); - bfdev_log_debug("chain '%s' register (%p)\n", head->name, node); return -BFDEV_ENOERR; } @@ -76,5 +66,4 @@ export void bfdev_notifier_unregister(bfdev_notifier_t *head, bfdev_notifier_node_t *node) { bfdev_ilist_del(&head->nodes, &node->list); - bfdev_log_debug("chain '%s' unregister (%p)\n", head->name, node); } diff --git a/src/respool.c b/src/respool.c index b7fbd530..ca20cbec 100644 --- a/src/respool.c +++ b/src/respool.c @@ -60,8 +60,8 @@ bfdev_respool_find_release(bfdev_respool_t *pool, export void bfdev_respool_release_all(bfdev_respool_t *pool, void *pdata) { - bfdev_respool_node_t *node; + bfdev_respool_node_t *node, *tmp; - bfdev_list_for_each_entry(node, &pool->nodes, list) + bfdev_list_for_each_entry_safe(node, tmp, &pool->nodes, list) node->release(node, pdata); } diff --git a/src/ringbuf.c b/src/ringbuf.c index 68b9cdf9..c7972020 100644 --- a/src/ringbuf.c +++ b/src/ringbuf.c @@ -28,7 +28,7 @@ } while (0) static __bfdev_always_inline void -ringbuf_out_copy(struct bfdev_ringbuf *ringbuf, void *buff, +ringbuf_out_copy(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len, unsigned long offset) { RINGBUF_GENERIC_COPY( @@ -38,7 +38,7 @@ ringbuf_out_copy(struct bfdev_ringbuf *ringbuf, void *buff, } static __bfdev_always_inline void -ringbuf_in_copy(struct bfdev_ringbuf *ringbuf, const void *buff, +ringbuf_in_copy(bfdev_ringbuf_t *ringbuf, const void *buff, unsigned long len, unsigned long offset) { RINGBUF_GENERIC_COPY( @@ -48,7 +48,7 @@ ringbuf_in_copy(struct bfdev_ringbuf *ringbuf, const void *buff, } static __bfdev_always_inline unsigned long -ringbuf_record_peek(struct bfdev_ringbuf *ringbuf, unsigned long recsize) +ringbuf_record_peek(bfdev_ringbuf_t *ringbuf, unsigned long recsize) { unsigned long mask, offset, length; uint8_t *data; @@ -74,8 +74,7 @@ ringbuf_record_peek(struct bfdev_ringbuf *ringbuf, unsigned long recsize) } static __bfdev_always_inline void -ringbuf_record_poke(struct bfdev_ringbuf *ringbuf, unsigned long len, - unsigned long recsize) +ringbuf_record_poke(bfdev_ringbuf_t *ringbuf, unsigned long len, unsigned long recsize) { unsigned long mask, offset; uint8_t *data; @@ -98,19 +97,19 @@ ringbuf_record_poke(struct bfdev_ringbuf *ringbuf, unsigned long len, } static inline bool -ringbuf_empty(struct bfdev_ringbuf *ringbuf) +ringbuf_empty(bfdev_ringbuf_t *ringbuf) { return ringbuf->in == ringbuf->out; } static inline unsigned long -ringbuf_valid(struct bfdev_ringbuf *ringbuf) +ringbuf_valid(bfdev_ringbuf_t *ringbuf) { return ringbuf->in - ringbuf->out; } static inline unsigned long -ringbuf_overflow(struct bfdev_ringbuf *ringbuf) +ringbuf_overflow(bfdev_ringbuf_t *ringbuf) { unsigned long size, used; @@ -124,8 +123,7 @@ ringbuf_overflow(struct bfdev_ringbuf *ringbuf) } export unsigned long -bfdev_ringbuf_peek_flat(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len) +bfdev_ringbuf_peek_flat(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len) { unsigned long valid; @@ -137,8 +135,7 @@ bfdev_ringbuf_peek_flat(struct bfdev_ringbuf *ringbuf, void *buff, } export unsigned long -bfdev_ringbuf_out_flat(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len) +bfdev_ringbuf_out_flat(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len) { unsigned long llen; @@ -149,8 +146,7 @@ bfdev_ringbuf_out_flat(struct bfdev_ringbuf *ringbuf, void *buff, } export unsigned long -bfdev_ringbuf_in_flat(struct bfdev_ringbuf *ringbuf, const void *buff, - unsigned long len) +bfdev_ringbuf_in_flat(bfdev_ringbuf_t *ringbuf, const void *buff, unsigned long len) { unsigned long size, overflow; @@ -168,8 +164,8 @@ bfdev_ringbuf_in_flat(struct bfdev_ringbuf *ringbuf, const void *buff, } export unsigned long -bfdev_ringbuf_peek_record(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len, unsigned long record) +bfdev_ringbuf_peek_record(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len, + unsigned long record) { unsigned long datalen; @@ -184,8 +180,8 @@ bfdev_ringbuf_peek_record(struct bfdev_ringbuf *ringbuf, void *buff, } export unsigned long -bfdev_ringbuf_out_record(struct bfdev_ringbuf *ringbuf, void *buff, - unsigned long len, unsigned long record) +bfdev_ringbuf_out_record(bfdev_ringbuf_t *ringbuf, void *buff, unsigned long len, + unsigned long record) { unsigned long datalen; @@ -201,8 +197,8 @@ bfdev_ringbuf_out_record(struct bfdev_ringbuf *ringbuf, void *buff, } export unsigned long -bfdev_ringbuf_in_record(struct bfdev_ringbuf *ringbuf, const void *buff, - unsigned long len, unsigned long record) +bfdev_ringbuf_in_record(bfdev_ringbuf_t *ringbuf, const void *buff, unsigned long len, + unsigned long record) { unsigned long size, offset; unsigned long overflow, datalen; @@ -227,7 +223,7 @@ bfdev_ringbuf_in_record(struct bfdev_ringbuf *ringbuf, const void *buff, } export int -bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const bfdev_alloc_t *alloc, +bfdev_ringbuf_dynamic_alloc(bfdev_ringbuf_t *ringbuf, const bfdev_alloc_t *alloc, size_t esize, size_t size) { size = bfdev_pow2_roundup(size); @@ -248,7 +244,7 @@ bfdev_ringbuf_dynamic_alloc(struct bfdev_ringbuf *ringbuf, const bfdev_alloc_t * } export void -bfdev_ringbuf_dynamic_free(struct bfdev_ringbuf *ringbuf) +bfdev_ringbuf_dynamic_free(bfdev_ringbuf_t *ringbuf) { const bfdev_alloc_t *alloc; diff --git a/src/textsearch/bm.c b/src/textsearch/bm.c index 4991cb66..c81e650e 100644 --- a/src/textsearch/bm.c +++ b/src/textsearch/bm.c @@ -20,14 +20,20 @@ struct bm_context { static const void * bm_pattern_get(bfdev_ts_context_t *tsc) { - struct bm_context *bctx = ts_to_bm(tsc); + struct bm_context *bctx; + + bctx = ts_to_bm(tsc); + return bctx->pattern; } static unsigned int bm_pattern_len(bfdev_ts_context_t *tsc) { - struct bm_context *bctx = ts_to_bm(tsc); + struct bm_context *bctx; + + bctx = ts_to_bm(tsc); + return bctx->pattern_len; } @@ -35,11 +41,16 @@ static unsigned int 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); - struct bm_context *bctx = ts_to_bm(tsc); - unsigned int bad_shift, good_shift, consumed = tss->offset; - unsigned int length, index, shift = bctx->pattern_len - 1; + struct bm_context *bctx; + unsigned int bad_shift, good_shift, consumed; + unsigned int length, index, shift; const uint8_t *text; + bool icase; + + bctx = ts_to_bm(tsc); + icase = bfdev_ts_test_igcase(tsc); + consumed = tss->offset; + shift = bctx->pattern_len - 1; for (;;) { length = tsc->next_block(tsc, tss, consumed, (const void **)&text); @@ -52,7 +63,7 @@ bm_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) break; } - if (bfdev_unlikely(index == bctx->pattern_len)) + if (index == bctx->pattern_len) return consumed + shift - bctx->pattern_len + 1; bad_shift = shift - index + bctx->bad_shift[text[shift - index]]; @@ -69,7 +80,10 @@ bm_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) static inline bool subpattern(const uint8_t *pattern, int index, int j, int g) { - int x = index + g - 1, y = j + g - 1; + int x, y; + + x = index + g - 1; + y = j + g - 1; while (y && pattern[x--] == pattern[y--]) { if (y < 0) @@ -115,10 +129,10 @@ static bfdev_ts_context_t * bm_prepare(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags) { - unsigned int gsize = sizeof(unsigned int) * len; struct bm_context *bctx; - unsigned int index; + unsigned int gsize, index; + gsize = sizeof(unsigned int) * len; bctx = bfdev_malloc(alloc, sizeof(*bctx) + gsize + len); if (bfdev_unlikely(!bctx)) return NULL; @@ -158,3 +172,9 @@ bm_init(void) { return bfdev_textsearch_register(&bm_algorithm); } + +static __bfdev_dtor int +bm_exit(void) +{ + return bfdev_textsearch_unregister(&bm_algorithm); +} diff --git a/src/textsearch/kmp.c b/src/textsearch/kmp.c index c34d8d94..3bfafe00 100644 --- a/src/textsearch/kmp.c +++ b/src/textsearch/kmp.c @@ -19,14 +19,20 @@ struct kmp_context { static const void * kmp_pattern_get(bfdev_ts_context_t *tsc) { - struct kmp_context *kctx = ts_to_kmp(tsc); + struct kmp_context *kctx; + + kctx = ts_to_kmp(tsc); + return kctx->pattern; } static unsigned int kmp_pattern_len(bfdev_ts_context_t *tsc) { - struct kmp_context *kctx = ts_to_kmp(tsc); + struct kmp_context *kctx; + + kctx = ts_to_kmp(tsc); + return kctx->pattern_len; } @@ -34,11 +40,15 @@ static unsigned int 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); - struct kmp_context *kctx = ts_to_kmp(tsc); - unsigned int consumed = tss->offset; - unsigned int length, index, match = 0; + struct kmp_context *kctx; + unsigned int consumed, length, index, match; const char *text; + bool icase; + + kctx = ts_to_kmp(tsc); + icase = bfdev_ts_test_igcase(tsc); + consumed = tss->offset; + match = 0; for (;;) { length = tsc->next_block(tsc, tss, consumed, (const void **)&text); @@ -52,7 +62,7 @@ kmp_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) if (kctx->pattern[match] == find_pattern()) match++; - if (bfdev_unlikely(match == kctx->pattern_len)) { + if (match == kctx->pattern_len) { tss->offset = consumed + index + 1; return tss->offset - kctx->pattern_len; } @@ -84,10 +94,10 @@ static bfdev_ts_context_t * kmp_prepare(const bfdev_alloc_t *alloc, const void *pattern, size_t len, unsigned long flags) { - unsigned int prefix_size = sizeof(unsigned int) * len; struct kmp_context *kctx; - unsigned int index; + unsigned int prefix_size, index; + prefix_size = sizeof(unsigned int) * len; kctx = bfdev_zalloc(alloc, sizeof(*kctx) + prefix_size + len); if (bfdev_unlikely(!kctx)) return NULL; @@ -127,3 +137,9 @@ kmp_init(void) { return bfdev_textsearch_register(&kmp_algorithm); } + +static __bfdev_dtor int +kmp_exit(void) +{ + return bfdev_textsearch_unregister(&kmp_algorithm); +} diff --git a/src/textsearch/linear.c b/src/textsearch/linear.c index 5c6b7f41..7c1913b7 100644 --- a/src/textsearch/linear.c +++ b/src/textsearch/linear.c @@ -11,8 +11,9 @@ static unsigned int linear_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, unsigned int consumed, const void **dest) { - bfdev_ts_linear_t *linear = tss->pdata; + bfdev_ts_linear_t *linear; + linear = tss->pdata; if (bfdev_likely(consumed < linear->len)) { *dest = linear->data + consumed; return linear->len - consumed; @@ -22,25 +23,18 @@ linear_next(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss, } export unsigned int -bfdev_textsearch_linear_find(bfdev_ts_context_t *tsc, - bfdev_ts_linear_t *linear, - const void *data, unsigned int len) +bfdev_textsearch_linear(bfdev_ts_context_t *tsc, bfdev_ts_linear_t *linear, + const void *data, unsigned int len) { - bfdev_ts_algorithm_t *algo = tsc->algo; + bfdev_ts_algorithm_t *algo; + + algo = tsc->algo; + tsc->next_block = linear_next; linear->data = data; linear->len = len; linear->tss.offset = 0; linear->tss.pdata = linear; - tsc->next_block = linear_next; - - return algo->find(tsc, &linear->tss); -} -export unsigned int -bfdev_textsearch_linear_next(bfdev_ts_context_t *tsc, - bfdev_ts_linear_t *linear) -{ - 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 f3d63d70..cb56e1cc 100644 --- a/src/textsearch/sunday.c +++ b/src/textsearch/sunday.c @@ -19,14 +19,20 @@ struct sunday_context { static const void * sunday_pattern_get(bfdev_ts_context_t *tsc) { - struct sunday_context *sctx = ts_to_sunday(tsc); + struct sunday_context *sctx; + + sctx = ts_to_sunday(tsc); + return sctx->pattern; } static unsigned int sunday_pattern_len(bfdev_ts_context_t *tsc) { - struct sunday_context *sctx = ts_to_sunday(tsc); + struct sunday_context *sctx; + + sctx = ts_to_sunday(tsc); + return sctx->pattern_len; } @@ -34,11 +40,15 @@ static unsigned int 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); - struct sunday_context *sctx = ts_to_sunday(tsc); - unsigned int consumed = tss->offset; - unsigned int length, index, shift = 0; + struct sunday_context *sctx; + unsigned int consumed, length, index, shift; const uint8_t *text; + bool icase; + + sctx = ts_to_sunday(tsc); + icase = bfdev_ts_test_igcase(tsc); + consumed = tss->offset; + shift = 0; for (;;) { length = tsc->next_block(tsc, tss, consumed, (const void **)&text); @@ -51,7 +61,7 @@ sunday_find(bfdev_ts_context_t *tsc, bfdev_ts_state_t *tss) break; } - if (bfdev_unlikely(index == sctx->pattern_len)) + if (index == sctx->pattern_len) return consumed + shift; if (sctx->pattern_len >= length) @@ -128,3 +138,9 @@ sunday_init(void) { return bfdev_textsearch_register(&sunday_algorithm); } + +static __bfdev_dtor int +sunday_exit(void) +{ + return bfdev_textsearch_unregister(&sunday_algorithm); +} diff --git a/src/textsearch/textsearch.c b/src/textsearch/textsearch.c index 7823f8e0..768b4f2a 100644 --- a/src/textsearch/textsearch.c +++ b/src/textsearch/textsearch.c @@ -7,21 +7,35 @@ #include #include -static BFDEV_LIST_HEAD(textsearch_algorithms); +static +BFDEV_LIST_HEAD(textsearch_algorithms); static bfdev_ts_algorithm_t * textsearch_algorithm_find(const char *name) { - bfdev_ts_algorithm_t *algo; + bfdev_ts_algorithm_t *walk; - bfdev_list_for_each_entry(algo, &textsearch_algorithms, list) { - if (!bfport_strcmp(algo->name, name)) - return algo; + bfdev_list_for_each_entry(walk, &textsearch_algorithms, list) { + if (!bfport_strcmp(walk->name, name)) + return walk; } return NULL; } +static bool +textsearch_algorithm_exist(bfdev_ts_algorithm_t *algo) +{ + bfdev_ts_algorithm_t *walk; + + bfdev_list_for_each_entry(walk, &textsearch_algorithms, list) { + if (walk == algo) + return true; + } + + return false; +} + export bfdev_ts_context_t * bfdev_textsearch_create(const bfdev_alloc_t *alloc, const char *name, const void *pattern, size_t len, unsigned long flags) @@ -55,14 +69,17 @@ bfdev_textsearch_register(bfdev_ts_algorithm_t *algo) return -BFDEV_EALREADY; bfdev_list_add(&textsearch_algorithms, &algo->list); + return -BFDEV_ENOERR; } -export void +export int bfdev_textsearch_unregister(bfdev_ts_algorithm_t *algo) { - if (textsearch_algorithm_find(algo->name)) - return; + if (!textsearch_algorithm_exist(algo)) + return -BFDEV_ENOENT; bfdev_list_del(&algo->list); + + return -BFDEV_ENOERR; } diff --git a/testsuite/CMakeLists.txt b/testsuite/CMakeLists.txt index f2ea2e5f..fa0f4a41 100644 --- a/testsuite/CMakeLists.txt +++ b/testsuite/CMakeLists.txt @@ -8,6 +8,9 @@ include(testsuite.cmake) add_subdirectory(array) add_subdirectory(bitwalk) +add_subdirectory(fifo) +add_subdirectory(hlist) add_subdirectory(list) +add_subdirectory(memalloc) add_subdirectory(mpi) add_subdirectory(slist) diff --git a/testsuite/array/fuzzy.c b/testsuite/array/fuzzy.c index 3ae4e63f..ad27b167 100644 --- a/testsuite/array/fuzzy.c +++ b/testsuite/array/fuzzy.c @@ -5,6 +5,7 @@ #include +#define TEST_CELLS 16 #define TEST_LOOP 4096 #define TEST_SIZE 65536 @@ -14,11 +15,14 @@ TESTSUITE( ) { BFDEV_DEFINE_ARRAY(array, NULL, 1); unsigned int count; + size_t size; int retval; retval = 0; for (count = 0; count < TEST_LOOP; ++count) { - retval = array_resize(&array, rand() % TEST_SIZE); + /* Not allow zero: TEST_SIZE ~ TEST_SIZE*2-1 */ + size = TEST_SIZE + rand() % TEST_SIZE; + retval = array_resize(&array, size); if (retval) break; } @@ -32,18 +36,23 @@ TESTSUITE( "array:apply", NULL, NULL, "array apply fuzzy test" ) { - BFDEV_DEFINE_ARRAY(array, NULL, 1); - unsigned int count; + bfdev_array_t array; + unsigned int count, cells; int retval; retval = 0; - for (count = 0; count < TEST_LOOP; ++count) { - retval = array_apply(&array, rand() % TEST_SIZE); - if (retval) - break; - } + for (cells = 1; cells < TEST_CELLS; ++cells) { + bfdev_array_init(&array, NULL, cells); - bfdev_array_release(&array); + for (count = 0; count < TEST_LOOP; ++count) { + /* Allow zero: 0 ~ TEST_SIZE-1 */ + retval = array_apply(&array, rand() % TEST_SIZE); + if (retval) + break; + } + + bfdev_array_release(&array); + } return retval; } diff --git a/testsuite/bitwalk/fuzzy.c b/testsuite/bitwalk/fuzzy.c index c754b03e..decd02b8 100644 --- a/testsuite/bitwalk/fuzzy.c +++ b/testsuite/bitwalk/fuzzy.c @@ -306,21 +306,21 @@ bitwalk_zero(unsigned int size, unsigned int loop) TESTSUITE( "bitwalk:bit_small", NULL, NULL, - "" + "bitwalk bit small test" ) { return bitwalk_bit(TEST_SMALL_SIZE, TEST_SMALL_LOOP); } TESTSUITE( "bitwalk:bit_large", NULL, NULL, - "" + "bitwalk bit large test" ) { return bitwalk_bit(TEST_LARGE_SIZE, TEST_LARGE_LOOP); } TESTSUITE( "bitwalk:zero_small", NULL, NULL, - "" + "bitwalk zero small test" ) { return bitwalk_zero(TEST_SMALL_SIZE, TEST_SMALL_LOOP); } @@ -328,7 +328,7 @@ TESTSUITE( TESTSUITE( "bitwalk:zero_large", NULL, NULL, - "" + "bitwalk zero large test" ) { return bitwalk_zero(TEST_LARGE_SIZE, TEST_LARGE_LOOP); } diff --git a/testsuite/fifo/.gitignore b/testsuite/fifo/.gitignore new file mode 100644 index 00000000..ffc5fcc1 --- /dev/null +++ b/testsuite/fifo/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/fifo-selftest diff --git a/testsuite/fifo/CMakeLists.txt b/testsuite/fifo/CMakeLists.txt new file mode 100644 index 00000000..cbbcba82 --- /dev/null +++ b/testsuite/fifo/CMakeLists.txt @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2024 John Sanpe +# + +add_executable(fifo-selftest selftest.c) +target_link_libraries(fifo-selftest bfdev testsuite) +add_test(fifo-selftest fifo-selftest) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(TARGETS + fifo-selftest + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/testsuite + ) +endif() diff --git a/testsuite/fifo/selftest.c b/testsuite/fifo/selftest.c new file mode 100644 index 00000000..5d6f28ba --- /dev/null +++ b/testsuite/fifo/selftest.c @@ -0,0 +1,430 @@ +/* SPDX-License-Identifier GPL-2.0-or-later */ +/* + * Copyright(c) 2022-2024 John Sanpe + */ + +#define MODULE_NAME "bitwalk-selftest" +#define bfdev_log_fmt(fmt) MODULE_NAME ":" fmt + +#include +#include +#include +#include +#include +#include + +#define TEST_LOOP 16 + +static const char +bytetest_table[TEST_LOOP] = { + 'o', 'p', 'e', 'n', 'b', 'f', 'd', 'e', + 'v', ',', 'h', 'e', 'l', 'l', 'l', 'o', +}; + +static const long +longtest_table[TEST_LOOP] = { + BFDEV_REPEAT_BYTE(0x00), BFDEV_REPEAT_BYTE(0x11), + BFDEV_REPEAT_BYTE(0x22), BFDEV_REPEAT_BYTE(0x33), + BFDEV_REPEAT_BYTE(0x44), BFDEV_REPEAT_BYTE(0x55), + BFDEV_REPEAT_BYTE(0x66), BFDEV_REPEAT_BYTE(0x77), + BFDEV_REPEAT_BYTE(0x88), BFDEV_REPEAT_BYTE(0x99), + BFDEV_REPEAT_BYTE(0xaa), BFDEV_REPEAT_BYTE(0xbb), + BFDEV_REPEAT_BYTE(0xcc), BFDEV_REPEAT_BYTE(0xdd), + BFDEV_REPEAT_BYTE(0xee), BFDEV_REPEAT_BYTE(0xff), +}; + +TESTSUITE( + "fifo:bytetest", NULL, NULL, + "fifo bytetest" +) { + BFDEV_DECLARE_FIFO(fifo, char, TEST_LOOP); + char buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_INIT(&fifo); + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_put(&fifo, bytetest_table[count]); + bfdev_log_debug("bytetest %u put '%c'\n", count, bytetest_table[count]); + if (!length || bfdev_fifo_len(&fifo) != count + 1) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("bytetest check full\n"); + if (!bfdev_fifo_check_full(&fifo)) + return -BFDEV_EFAULT; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_peek(&fifo, buffer); + bfdev_log_debug("bytetest %u peek '%c'\n", count, *buffer); + if (!length || *buffer != bytetest_table[count]) + return -BFDEV_EFAULT; + + length = bfdev_fifo_get(&fifo, buffer); + bfdev_log_debug("bytetest %u get '%c'\n", count, *buffer); + if (!length || *buffer != bytetest_table[count]) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("bytetest copy in\n"); + length = bfdev_fifo_in(&fifo, bytetest_table, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("bytetest copy peek out\n"); + length = bfdev_fifo_out_peek(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("bytetest check peek out\n"); + retval = memcmp(buffer, bytetest_table, sizeof(bytetest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_log_debug("bytetest copy out\n"); + length = bfdev_fifo_out(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("bytetest check out\n"); + retval = memcmp(buffer, bytetest_table, sizeof(bytetest_table)); + if (retval) + return -BFDEV_EFAULT; + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:longtest", NULL, NULL, + "fifo longtest" +) { + BFDEV_DECLARE_FIFO(fifo, long, TEST_LOOP); + long buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_INIT(&fifo); + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_put(&fifo, longtest_table[count]); + bfdev_log_debug("longtest %u put %#lx\n", count, longtest_table[count]); + if (!length || bfdev_fifo_len(&fifo) != count + 1) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("longtest check full\n"); + if (!bfdev_fifo_check_full(&fifo)) + return -BFDEV_EFAULT; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_peek(&fifo, buffer); + bfdev_log_debug("longtest %u peek %#lx\n", count, *buffer); + if (!length || *buffer != longtest_table[count]) + return -BFDEV_EFAULT; + + length = bfdev_fifo_get(&fifo, buffer); + bfdev_log_debug("longtest %u get %#lx\n", count, *buffer); + if (!length || *buffer != longtest_table[count]) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("longtest copy in\n"); + length = bfdev_fifo_in(&fifo, longtest_table, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("longtest copy peek out\n"); + length = bfdev_fifo_out_peek(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("longtest check peek out\n"); + retval = memcmp(buffer, longtest_table, sizeof(longtest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_log_debug("longtest copy out\n"); + length = bfdev_fifo_out(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("longtest check out\n"); + retval = memcmp(buffer, longtest_table, sizeof(longtest_table)); + if (retval) + return -BFDEV_EFAULT; + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:record_bytetest", NULL, NULL, + "fifo record bytetest" +) { + BFDEV_DECLARE_FIFO_RECORD(fifo, char, TEST_LOOP, 1); + char buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_INIT(&fifo); + for (count = 1; count < TEST_LOOP; ++count) { + bfdev_log_debug("record bytetest copy %u in\n", count); + length = bfdev_fifo_in(&fifo, bytetest_table, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record bytetest copy %u out\n", count); + length = bfdev_fifo_out(&fifo, buffer, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record bytetest check %u copy\n", count); + retval = memcmp(buffer, bytetest_table, count); + if (retval) + return -BFDEV_EFAULT; + } + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:record_longtest", NULL, NULL, + "fifo record longtest" +) { + BFDEV_DECLARE_FIFO_RECORD(fifo, long, TEST_LOOP, 1); + long buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_INIT(&fifo); + for (count = 1; count < TEST_LOOP; ++count) { + bfdev_log_debug("record longtest copy %u in\n", count); + length = bfdev_fifo_in(&fifo, longtest_table, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record longtest copy %u out\n", count); + length = bfdev_fifo_out(&fifo, buffer, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record longtest check %u copy\n", count); + retval = memcmp(buffer, longtest_table, count); + if (retval) + return -BFDEV_EFAULT; + } + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:dynamic_bytetest", NULL, NULL, + "fifo dynamic bytetest" +) { + BFDEV_DECLARE_FIFO_DYNAMIC(fifo, char); + char buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_DYNAMIC_INIT(&fifo); + retval = bfdev_fifo_alloc(&fifo, NULL, TEST_LOOP); + if (retval) + return retval; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_put(&fifo, bytetest_table[count]); + bfdev_log_debug("dynamic bytetest %u put '%c'\n", count, bytetest_table[count]); + if (!length || bfdev_fifo_len(&fifo) != count + 1) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("dynamic bytetest check full\n"); + if (!bfdev_fifo_check_full(&fifo)) + return -BFDEV_EFAULT; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_peek(&fifo, buffer); + bfdev_log_debug("dynamic bytetest %u peek '%c'\n", count, *buffer); + if (!length || *buffer != bytetest_table[count]) + return -BFDEV_EFAULT; + + length = bfdev_fifo_get(&fifo, buffer); + bfdev_log_debug("dynamic bytetest %u get '%c'\n", count, *buffer); + if (!length || *buffer != bytetest_table[count]) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("dynamic bytetest copy in\n"); + length = bfdev_fifo_in(&fifo, bytetest_table, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic bytetest copy peek out\n"); + length = bfdev_fifo_out_peek(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic bytetest check peek out\n"); + retval = memcmp(bytetest_table, bytetest_table, sizeof(bytetest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic bytetest copy out\n"); + length = bfdev_fifo_out(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic bytetest check out\n"); + retval = memcmp(buffer, bytetest_table, sizeof(bytetest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_fifo_free(&fifo); + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:dynamic_longtest", NULL, NULL, + "fifo dynamic longtest" +) { + BFDEV_DECLARE_FIFO_DYNAMIC(fifo, long); + long buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_DYNAMIC_INIT(&fifo); + retval = bfdev_fifo_alloc(&fifo, NULL, TEST_LOOP); + if (retval) + return retval; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_put(&fifo, longtest_table[count]); + bfdev_log_debug("longtest %u put %#lx\n", count, longtest_table[count]); + if (!length || bfdev_fifo_len(&fifo) != count + 1) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("dynamic longtest check full\n"); + if (!bfdev_fifo_check_full(&fifo)) + return -BFDEV_EFAULT; + + for (count = 0; count < TEST_LOOP; ++count) { + length = bfdev_fifo_peek(&fifo, buffer); + bfdev_log_debug("dynamic longtest %u peek %#lx\n", count, *buffer); + if (!length || *buffer != longtest_table[count]) + return -BFDEV_EFAULT; + + length = bfdev_fifo_get(&fifo, buffer); + bfdev_log_debug("dynamic longtest %u get %#lx\n", count, *buffer); + if (!length || *buffer != longtest_table[count]) + return -BFDEV_EFAULT; + } + + bfdev_log_debug("dynamic longtest copy in\n"); + length = bfdev_fifo_in(&fifo, longtest_table, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic longtest copy peek out\n"); + length = bfdev_fifo_out_peek(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic longtest check peek out\n"); + retval = memcmp(buffer, longtest_table, sizeof(longtest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic longtest copy out\n"); + length = bfdev_fifo_out(&fifo, buffer, TEST_LOOP); + if (length != TEST_LOOP) + return -BFDEV_EFAULT; + + bfdev_log_debug("dynamic longtest check out\n"); + retval = memcmp(buffer, longtest_table, sizeof(longtest_table)); + if (retval) + return -BFDEV_EFAULT; + + bfdev_fifo_free(&fifo); + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:dynamic_record_bytetest", NULL, NULL, + "fifo dynamic_record bytetest" +) { + BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(fifo, char, 1); + char buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_DYNAMIC_INIT(&fifo); + retval = bfdev_fifo_alloc(&fifo, NULL, TEST_LOOP); + if (retval) + return retval; + + for (count = 1; count < TEST_LOOP; ++count) { + bfdev_log_debug("record bytetest copy %u in\n", count); + length = bfdev_fifo_in(&fifo, bytetest_table, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record bytetest copy %u out\n", count); + length = bfdev_fifo_out(&fifo, buffer, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record bytetest check %u copy\n", count); + retval = memcmp(buffer, bytetest_table, count); + if (retval) + return -BFDEV_EFAULT; + } + + bfdev_fifo_free(&fifo); + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "fifo:dynamic_record_longtest", NULL, NULL, + "fifo dynamic record longtest" +) { + BFDEV_DECLARE_FIFO_DYNAMIC_RECORD(fifo, long, 1); + long buffer[TEST_LOOP]; + unsigned long length; + unsigned int count; + int retval; + + fifo = BFDEV_FIFO_DYNAMIC_INIT(&fifo); + retval = bfdev_fifo_alloc(&fifo, NULL, TEST_LOOP); + if (retval) + return retval; + + for (count = 1; count < TEST_LOOP; ++count) { + bfdev_log_debug("record longtest copy %u in\n", count); + length = bfdev_fifo_in(&fifo, longtest_table, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record longtest copy %u out\n", count); + length = bfdev_fifo_out(&fifo, buffer, count); + if (length != count) + return -BFDEV_EFAULT; + + bfdev_log_debug("record longtest check %u copy\n", count); + retval = memcmp(buffer, longtest_table, count); + if (retval) + return -BFDEV_EFAULT; + } + + bfdev_fifo_free(&fifo); + + return -BFDEV_ENOERR; +} diff --git a/testsuite/hlist/.gitignore b/testsuite/hlist/.gitignore new file mode 100644 index 00000000..be7c05a3 --- /dev/null +++ b/testsuite/hlist/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +/hlist-iterator diff --git a/testsuite/hlist/CMakeLists.txt b/testsuite/hlist/CMakeLists.txt new file mode 100644 index 00000000..86d023f3 --- /dev/null +++ b/testsuite/hlist/CMakeLists.txt @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2024 John Sanpe +# + +add_executable(hlist-iterator iterator.c) +target_link_libraries(hlist-iterator bfdev testsuite) +add_test(hlist-iterator hlist-iterator) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(TARGETS + hlist-iterator + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/testsuite + ) +endif() diff --git a/testsuite/hlist/iterator.c b/testsuite/hlist/iterator.c new file mode 100644 index 00000000..b6dbac37 --- /dev/null +++ b/testsuite/hlist/iterator.c @@ -0,0 +1,238 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "hlist-iterator" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include + +#define TEST_LOOP 10 + +struct test_node { + bfdev_hlist_node_t list; + unsigned int num; +}; + +struct test_struct { + bfdev_hlist_head_t head; + struct test_node nodes[TEST_LOOP]; +}; + +#define hlist_to_test(ptr) \ + bfdev_hlist_entry(ptr, struct test_node, list) + +static void * +test_prepare(int argc, const char *argv[]) +{ + struct test_struct *test; + unsigned int count; + + test = malloc(sizeof(*test)); + if (!test) + return NULL; + + bfdev_hlist_head_init(&test->head); + for (count = 0; count < TEST_LOOP; ++count) { + test->nodes[count].num = TEST_LOOP - count; + bfdev_hlist_head_add(&test->head, &test->nodes[count].list); + } + + return test; +} + +static void +test_release(void *data) +{ + free(data); +} + +TESTSUITE( + "hlist:for_each", + test_prepare, test_release, + "hlist for each iterator test" +) { + struct test_struct *test; + struct test_node *node; + bfdev_hlist_node_t *list, *tlist; + unsigned int count; + + test = data; + count = 0; + + bfdev_hlist_for_each(list, &test->head) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each' test: %u\n", node->num); + if (count++ == TEST_LOOP / 2) + break; + } + + tlist = list; + bfdev_hlist_for_each_continue(list) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each_continue' test: %u\n", node->num); + count++; + } + + list = tlist; + bfdev_hlist_for_each_from(list) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each_from' test: %u\n", node->num); + count++; + } + + if (count != TEST_LOOP + (TEST_LOOP / 2)) + return -BFDEV_EFAULT; + + bfdev_log_debug("'hlist_for_each_continue' null test\n"); + bfdev_hlist_for_each_continue(list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'hlist_for_each_from' null test\n"); + bfdev_hlist_for_each_from(list) + return -BFDEV_ENODATA; + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "hlist:for_each_safe", + test_prepare, test_release, + "hlist for each safe iterator test" +) { + struct test_struct *test; + struct test_node *node; + bfdev_hlist_node_t *list, *nlist, *tlist; + unsigned int count; + + test = data; + count = 0; + + bfdev_hlist_for_each_safe(list, nlist, &test->head) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each_safe' test: %u\n", node->num); + if (count++ == TEST_LOOP / 2) + break; + } + + tlist = list; + bfdev_hlist_for_each_continue_safe(list, nlist) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each_continue_safe' test: %u\n", node->num); + count++; + } + + list = tlist; + bfdev_hlist_for_each_from_safe(list, nlist) { + node = hlist_to_test(list); + bfdev_log_debug("'hlist_for_each_from_safe' test: %u\n", node->num); + count++; + } + + if (count != TEST_LOOP + (TEST_LOOP / 2)) + return -BFDEV_EFAULT; + + bfdev_log_debug("'hlist_for_each_continue_safe' null test\n"); + bfdev_hlist_for_each_continue_safe(list, nlist) + return -BFDEV_ENODATA; + + bfdev_log_debug("'hlist_for_each_from_safe' null test\n"); + bfdev_hlist_for_each_from_safe(list, nlist) + return -BFDEV_ENODATA; + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "hlist:for_each_entry", + test_prepare, test_release, + "hlist for each iterator test" +) { + struct test_struct *test; + struct test_node *node, *tnode; + unsigned int count; + + test = data; + count = 0; + + bfdev_hlist_for_each_entry(node, &test->head, list) { + bfdev_log_debug("'hlist_for_each_entry' test: %u\n", node->num); + if (count++ == TEST_LOOP / 2) + break; + } + + tnode = node; + bfdev_hlist_for_each_entry_continue(node, list) { + bfdev_log_debug("'hlist_for_each_entry_continue' test: %u\n", node->num); + count++; + } + + node = tnode; + bfdev_hlist_for_each_entry_from(node, list) { + bfdev_log_debug("'hlist_for_each_entry_from' test: %u\n", node->num); + count++; + } + + if (count != TEST_LOOP + (TEST_LOOP / 2)) + return -BFDEV_EFAULT; + + bfdev_log_debug("'hlist_for_each_entry_continue' null test\n"); + bfdev_hlist_for_each_entry_continue(node, list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'hlist_for_each_entry_from' null test\n"); + bfdev_hlist_for_each_entry_from(node, list) + return -BFDEV_ENODATA; + + return -BFDEV_ENOERR; +} + +TESTSUITE( + "hlist:for_each_entry_safe", + test_prepare, test_release, + "hlist for each entry safe iterator test" +) { + struct test_struct *test; + struct test_node *node, *nnode, *tnode; + unsigned int count; + + test = data; + count = 0; + + bfdev_hlist_for_each_entry_safe(node, nnode, &test->head, list) { + bfdev_log_debug("'hlist_for_each_entry_safe' test: %u\n", node->num); + if (count++ == TEST_LOOP / 2) + break; + } + + tnode = node; + bfdev_hlist_for_each_entry_continue_safe(node, nnode, list) { + bfdev_log_debug("'hlist_for_each_entry_continue_safe' test: %u\n", node->num); + count++; + } + + node = tnode; + bfdev_hlist_for_each_entry_from_safe(node, nnode, list) { + bfdev_log_debug("'hlist_for_each_entry_from_safe' test: %u\n", node->num); + count++; + } + + if (count != TEST_LOOP + (TEST_LOOP / 2)) + return -BFDEV_EFAULT; + + bfdev_log_debug("'hlist_for_each_entry_continue_safe' null test\n"); + bfdev_hlist_for_each_entry_continue_safe(node, nnode, list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'hlist_for_each_entry_from_safe' null test\n"); + bfdev_hlist_for_each_entry_from_safe(node, nnode, list) + return -BFDEV_ENODATA; + + return -BFDEV_ENOERR; +} diff --git a/testsuite/memalloc/.gitignore b/testsuite/memalloc/.gitignore new file mode 100644 index 00000000..a4828a10 --- /dev/null +++ b/testsuite/memalloc/.gitignore @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +<<<<<<<< Updated upstream:examples/memalloc/.gitignore +/memalloc-selftest +======== +/memalloc-fuzzy +>>>>>>>> Stashed changes:testsuite/memalloc/.gitignore diff --git a/testsuite/memalloc/CMakeLists.txt b/testsuite/memalloc/CMakeLists.txt new file mode 100644 index 00000000..178c2678 --- /dev/null +++ b/testsuite/memalloc/CMakeLists.txt @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2023 ffashion +# + +add_executable(memalloc-fuzzy fuzzy.c) +target_link_libraries(memalloc-fuzzy bfdev testsuite) +add_test(memalloc-fuzzy memalloc-fuzzy) + +if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev") + install(TARGETS + memalloc-fuzzy + DESTINATION + ${CMAKE_INSTALL_DOCDIR}/testsuite + ) +endif() diff --git a/testsuite/memalloc/fuzzy.c b/testsuite/memalloc/fuzzy.c new file mode 100644 index 00000000..84442386 --- /dev/null +++ b/testsuite/memalloc/fuzzy.c @@ -0,0 +1,144 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#define MODULE_NAME "memalloc-fuzzy" +#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define POOL_SIZE BFDEV_SZ_32MiB +#define TEST_SIZE BFDEV_SZ_16MiB +#define TEST_LOOP 4096 + +static int +test_memalloc(bfdev_memalloc_head_t *pool) +{ + bfdev_memalloc_chunk_t *node; + void *result, *data; + unsigned int count; + size_t size; + int retval; + + DEFINE_RANDPOOL(rpool1); + DEFINE_RANDPOOL(rpool2); + retval = -BFDEV_ENOERR; + + srand(time(NULL)); + for (count = 0; count < TEST_LOOP; ++count) { + size = (unsigned int)rand() % (TEST_SIZE / TEST_LOOP); + result = bfdev_memalloc_alloc(pool, size); + if (!result) { + retval = BFDEV_ENOMEM; + goto failed; + } + + retval = randpool_put(&rpool1, result); + if (retval) + goto failed; + + memset(result, 0, size); + } + + for (count = 0; count < TEST_LOOP; ++count) { + data = randpool_get(&rpool1); + size = (unsigned int)rand() % (TEST_SIZE / TEST_LOOP); + result = bfdev_memalloc_realloc(pool, data, size); + if (!result) { + retval = BFDEV_ENOMEM; + goto failed; + } + + retval = randpool_put(&rpool2, result); + if (retval) + goto failed; + + memset(result, 0, size); + } + + for (count = 0; count < TEST_LOOP; ++count) { + data = randpool_get(&rpool2); + bfdev_memalloc_free(pool, data); + } + + node = bfdev_list_first_entry(&pool->block_list, bfdev_memalloc_chunk_t, block); + if (node->usize != POOL_SIZE - sizeof(bfdev_memalloc_chunk_t)) { + bfdev_log_err("free node size leak %#lx -> %#lx\n", POOL_SIZE - + sizeof(bfdev_memalloc_chunk_t), node->usize); + retval = -BFDEV_EFAULT; + goto failed; + } + + if (pool->avail != POOL_SIZE - sizeof(bfdev_memalloc_chunk_t)) { + bfdev_log_err("total available leak %#lx -> %#lx\n", POOL_SIZE - + sizeof(bfdev_memalloc_chunk_t), pool->avail); + retval = -BFDEV_EFAULT; + goto failed; + } + +failed: + randpool_release(&rpool1, NULL, NULL); + randpool_release(&rpool2, NULL, NULL); + return retval; +} + +#define TEST_PREPARE(NAME, FUNC) \ +static void * \ +NAME(int argc, const char *argv[]) \ +{ \ + bfdev_memalloc_head_t *pool; \ + void *memory; \ + \ + pool = malloc(sizeof(*pool) + POOL_SIZE); \ + if (!pool) \ + return BFDEV_ERR_PTR(-BFDEV_ENOMEM); \ + \ + memory = (void *)pool + sizeof(*pool); \ + bfdev_memalloc_init(pool, FUNC, memory, POOL_SIZE); \ + \ + return pool; \ +} + +static void +test_release(void *data) +{ + free(data); +} + +TEST_PREPARE(first_fit_prepare, bfdev_memalloc_first_fit) +TEST_PREPARE(best_fit_prepare, bfdev_memalloc_best_fit) +TEST_PREPARE(worst_fit_prepare, bfdev_memalloc_worst_fit) + +TESTSUITE( + "memalloc:first-fit", + first_fit_prepare, test_release, + "memalloc first-fit fuzzy test" +) { + return test_memalloc(data); +} + +TESTSUITE( + "memalloc:best-fit", + best_fit_prepare, test_release, + "memalloc best-fit fuzzy test" +) { + return test_memalloc(data); +} + +TESTSUITE( + "memalloc:worst-fit", + worst_fit_prepare, test_release, + "memalloc worst-fit fuzzy test" +) { + return test_memalloc(data); +} diff --git a/testsuite/randpool.h b/testsuite/randpool.h new file mode 100644 index 00000000..e8aa5b30 --- /dev/null +++ b/testsuite/randpool.h @@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef _TESTSUITE_RANDPOOL_H_ +#define _TESTSUITE_RANDPOOL_H_ + +#include +#include +#include +#include + +typedef struct randpool_head randpool_head_t; +typedef struct randpool_node randpool_node_t; + +struct randpool_head { + bfdev_list_head_t nodes; + unsigned int count; +}; + +struct randpool_node { + bfdev_list_head_t list; + void *data; +}; + +#define DEFINE_RANDPOOL(NAME) \ + randpool_head_t NAME = { \ + .nodes = BFDEV_LIST_HEAD_INIT(&(NAME).nodes), \ + .count = 0, \ + } + +static inline int +randpool_put(randpool_head_t *pool, void *data) +{ + randpool_node_t *node; + + node = malloc(sizeof(*node)); + if (bfdev_unlikely(!node)) + return -BFDEV_ENOMEM; + + node->data = data; + bfdev_list_add(&pool->nodes, &node->list); + pool->count++; + + return -BFDEV_ENOERR; +} + +static inline void * +randpool_get(randpool_head_t *pool) +{ + randpool_node_t *node; + unsigned int index; + void *data; + + if (!pool->count) + return NULL; + + index = (unsigned int)rand() % pool->count; + bfdev_list_for_each_entry(node, &pool->nodes, list) { + if (index--) + continue; + + bfdev_list_del(&node->list); + pool->count--; + + data = node->data; + free(node); + + return data; + } + + BFDEV_BUG(); +} + +static inline void +randpool_release(randpool_head_t *pool, bfdev_release_t release, void *pdata) +{ + randpool_node_t *node, *tmp; + + bfdev_list_for_each_entry_safe(node, tmp, &pool->nodes, list) { + if (release) + release(node->data, pdata); + free(node); + } + + bfdev_list_head_init(&pool->nodes); + pool->count = 0; +} + +#endif /* _TESTSUITE_RANDPOOL_H_ */ diff --git a/testsuite/slist/iterator.c b/testsuite/slist/iterator.c index 1f9c2077..78c52dfa 100644 --- a/testsuite/slist/iterator.c +++ b/testsuite/slist/iterator.c @@ -90,6 +90,14 @@ TESTSUITE( if (count != TEST_LOOP + (TEST_LOOP / 2)) return -BFDEV_EFAULT; + bfdev_log_debug("'slist_for_each_continue' null test\n"); + bfdev_slist_for_each_continue(list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'slist_for_each_from' null test\n"); + bfdev_slist_for_each_from(list) + return -BFDEV_ENODATA; + return -BFDEV_ENOERR; } @@ -130,6 +138,14 @@ TESTSUITE( if (count != TEST_LOOP + (TEST_LOOP / 2)) return -BFDEV_EFAULT; + bfdev_log_debug("'slist_for_each_continue_safe' null test\n"); + bfdev_slist_for_each_continue_safe(list, nlist) + return -BFDEV_ENODATA; + + bfdev_log_debug("'slist_for_each_from_safe' null test\n"); + bfdev_slist_for_each_from_safe(list, nlist) + return -BFDEV_ENODATA; + return -BFDEV_ENOERR; } @@ -166,6 +182,14 @@ TESTSUITE( if (count != TEST_LOOP + (TEST_LOOP / 2)) return -BFDEV_EFAULT; + bfdev_log_debug("'slist_for_each_entry_continue' null test\n"); + bfdev_slist_for_each_entry_continue(node, list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'slist_for_each_entry_from' null test\n"); + bfdev_slist_for_each_entry_from(node, list) + return -BFDEV_ENODATA; + return -BFDEV_ENOERR; } @@ -202,5 +226,13 @@ TESTSUITE( if (count != TEST_LOOP + (TEST_LOOP / 2)) return -BFDEV_EFAULT; + bfdev_log_debug("'slist_for_each_entry_continue_safe' null test\n"); + bfdev_slist_for_each_entry_continue_safe(node, nnode, list) + return -BFDEV_ENODATA; + + bfdev_log_debug("'slist_for_each_entry_from_safe' null test\n"); + bfdev_slist_for_each_entry_from_safe(node, nnode, list) + return -BFDEV_ENODATA; + return -BFDEV_ENOERR; }