From 9190dc9980d1465b287862ea51fe7274725fe651 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 1 Apr 2025 17:06:42 +0800 Subject: [PATCH 1/5] feat platform: optimize cross-compile by using try compile to determine types Signed-off-by: John Sanpe --- scripts/compare-type.c | 25 ++++++++++++++++++++++++ scripts/get-type.c | 39 ------------------------------------- scripts/get-type.cmake | 43 +++++++++++++++++++++++++++++++++++++++++ scripts/platform.cmake | 44 ++++-------------------------------------- 4 files changed, 72 insertions(+), 79 deletions(-) create mode 100644 scripts/compare-type.c delete mode 100644 scripts/get-type.c create mode 100644 scripts/get-type.cmake diff --git a/scripts/compare-type.c b/scripts/compare-type.c new file mode 100644 index 00000000..09af0a19 --- /dev/null +++ b/scripts/compare-type.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#include +#include + +#if !defined(TYPE1) || !defined(TYPE2) +# error "Undefine types" +#endif + +#define typecheck(type, object) ({ \ + type __dummy1; \ + typeof(object) __dummy2; \ + (void)(&__dummy1 == &__dummy2); \ + 1; \ +}) + +int +main(int argc, const char *argv[]) +{ + typecheck(TYPE1, (TYPE2){0}); + return 0; +} diff --git a/scripts/get-type.c b/scripts/get-type.c deleted file mode 100644 index 89277f06..00000000 --- a/scripts/get-type.c +++ /dev/null @@ -1,39 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* - * Copyright(c) 2023 John Sanpe - */ - -#include -#include -#include - -#ifndef TYPE -# error "undefine type" -#endif - -int -main(int argc, const char *argv[]) -{ - const char *name; - TYPE type; - - name = _Generic(type, - __signed__ char: "char", - unsigned char: "char", - __signed__ short: "short", - unsigned short: "short", - __signed__ int: "int", - unsigned int: "int", - __signed__ long: "long", - unsigned long: "long", - __signed__ long long: "long long", - unsigned long long: "long long", - default: NULL - ); - - if (!name) - return 1; - printf("%s", name); - - return 0; -} diff --git a/scripts/get-type.cmake b/scripts/get-type.cmake new file mode 100644 index 00000000..2d86c16b --- /dev/null +++ b/scripts/get-type.cmake @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# +# Copyright(c) 2025 John Sanpe +# + +function(compare_type result type1 type2) + message(DEBUG "Compare type: '${type1}' '${type2}'") + try_compile( + COMPILE_RESULT + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_CURRENT_LIST_DIR}/compare-type.c + COMPILE_DEFINITIONS "-Werror" + COMPILE_DEFINITIONS "-DTYPE1=${type1}" + COMPILE_DEFINITIONS "-DTYPE2=${type2}" + OUTPUT_VARIABLE OUTPUT + ) + message(DEBUG "${OUTPUT}") + set(${result} ${COMPILE_RESULT} PARENT_SCOPE) +endfunction() + +function(get_type result name default) + message(CHECK_START "Looking for type '${name}'") + set(candidates "int" "long" "long long" "char" "short") + + foreach(walk IN LISTS candidates) + compare_type(compare "${name}" "${walk}") + if(NOT ${compare}) + compare_type(compare "${name}" "unsigned ${walk}") + endif() + + if(${compare}) + set(type "${walk}") + message(CHECK_PASS "found '${walk}'") + break() + endif() + endforeach() + + if(NOT type) + message(CHECK_FAIL "use default '${default}'") + set(type "${default}") + endif() + set(${result} "${type}" PARENT_SCOPE) +endfunction() diff --git a/scripts/platform.cmake b/scripts/platform.cmake index 2e7f7e15..167ce759 100644 --- a/scripts/platform.cmake +++ b/scripts/platform.cmake @@ -3,29 +3,6 @@ # Copyright(c) 2025 John Sanpe # -if(HOST_C_COMPILER) - set(PRINT_TYPE_LINK "-static") -endif() - -macro(print_type output name) - message(STATUS "Print type: " ${name}) - try_run( - RUN_RESULT - COMPILE_RESULT - ${CMAKE_CURRENT_BINARY_DIR} - ${CMAKE_CURRENT_LIST_DIR}/get-type.c - COMPILE_DEFINITIONS "-Werror -DTYPE=${name}" - LINK_OPTIONS "${PRINT_TYPE_LINK}" - RUN_OUTPUT_VARIABLE ${output} - COMPILE_OUTPUT_VARIABLE COMPILE_OUTPUT - ) - if ((NOT COMPILE_RESULT) OR (NOT RUN_RESULT EQUAL 0)) - message("Compile output: ${COMPILE_OUTPUT}") - message("Run result: ${RUN_RESULT}") - message(FATAL_ERROR "Failed to retrieve the original type ${name}") - endif() -endmacro() - if(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|Darwin|GNU") set(BFDEV_PORT_TYPE "posix") endif() @@ -42,20 +19,7 @@ if(CMAKE_SYSTEM_NAME MATCHES "Generic") set(BFDEV_PORT_TYPE "generic") endif() -if(BFDEV_PORT_TYPE STREQUAL "posix") - print_type(BFDEV_TYPE_ADDR "size_t") - print_type(BFDEV_TYPE_W64 "int64_t") - print_type(BFDEV_TYPE_MAX "intmax_t") -endif() - -if(NOT BFDEV_TYPE_ADDR) - set(BFDEV_TYPE_ADDR "long") -endif() - -if(NOT BFDEV_TYPE_W64) - set(BFDEV_TYPE_W64 "long long") -endif() - -if(NOT BFDEV_TYPE_MAX) - set(BFDEV_TYPE_MAX "long long") -endif() +include(${CMAKE_CURRENT_LIST_DIR}/get-type.cmake) +get_type(BFDEV_TYPE_ADDR "size_t" "long") +get_type(BFDEV_TYPE_W64 "int64_t" "long long") +get_type(BFDEV_TYPE_MAX "intmax_t" "long long") From f12b8f1f0cfdc18ad735fca5b3f41836c542c4ca Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 1 Apr 2025 15:50:14 +0800 Subject: [PATCH 2/5] ci github: added corss compile tests Signed-off-by: John Sanpe --- .github/workflows/ubuntu-aarch64-gcc.yml | 60 ++++++++++++++++++++++++ .github/workflows/ubuntu-armv7-gcc.yml | 60 ++++++++++++++++++++++++ README.md | 16 ++++--- 3 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ubuntu-aarch64-gcc.yml create mode 100644 .github/workflows/ubuntu-armv7-gcc.yml diff --git a/.github/workflows/ubuntu-aarch64-gcc.yml b/.github/workflows/ubuntu-aarch64-gcc.yml new file mode 100644 index 00000000..62b6eeb4 --- /dev/null +++ b/.github/workflows/ubuntu-aarch64-gcc.yml @@ -0,0 +1,60 @@ +name: build default on ubuntu aarch64 gcc + +on: + repository_dispatch: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: '0 */2 * * *' + +env: + BUILD_TYPE: Release + LD_LIBRARY_PATH: /usr/aarch64-linux-gnu/lib + +jobs: + build: + name: Test on ${{matrix.os}} + runs-on: ${{matrix.os}} + strategy: + matrix: + os: [ubuntu-22.04] + + steps: + - name: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: install dependencies + run: | + sudo apt update + sudo apt install cmake make qemu-user-binfmt \ + gcc gcc-aarch64-linux-gnu + + - name: configure cmake + run: | + cmake -B ${{github.workspace}}/build \ + -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install \ + -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ + -D CMAKE_C_COMPILER=aarch64-linux-gnu-gcc \ + -D HOST_C_COMPILER=gcc \ + -D BFDEV_STRICT=ON \ + -D BFDEV_EXAMPLES=ON \ + -D BFDEV_TESTSUITE=ON + + - name: make + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: install + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} -- install + + - name: ctest + working-directory: ${{github.workspace}}/build + run: | + sudo ln -s /usr/aarch64-linux-gnu/lib/ld-linux-aarch64.so.1 /lib + ctest -C ${{env.BUILD_TYPE}} -V diff --git a/.github/workflows/ubuntu-armv7-gcc.yml b/.github/workflows/ubuntu-armv7-gcc.yml new file mode 100644 index 00000000..4ce78046 --- /dev/null +++ b/.github/workflows/ubuntu-armv7-gcc.yml @@ -0,0 +1,60 @@ +name: build default on ubuntu armv7 gcc + +on: + repository_dispatch: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: '0 */2 * * *' + +env: + BUILD_TYPE: Release + LD_LIBRARY_PATH: /usr/arm-linux-gnueabihf/lib + +jobs: + build: + name: Test on ${{matrix.os}} + runs-on: ${{matrix.os}} + strategy: + matrix: + os: [ubuntu-22.04] + + steps: + - name: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: install dependencies + run: | + sudo apt update + sudo apt install cmake make qemu-user-binfmt \ + gcc gcc-arm-linux-gnueabihf + + - name: configure cmake + run: | + cmake -B ${{github.workspace}}/build \ + -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install \ + -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ + -D CMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \ + -D HOST_C_COMPILER=gcc \ + -D BFDEV_STRICT=ON \ + -D BFDEV_EXAMPLES=ON \ + -D BFDEV_TESTSUITE=ON + + - name: make + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: install + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} -- install + + - name: ctest + working-directory: ${{github.workspace}}/build + run: | + sudo ln -s /usr/arm-linux-gnueabihf/lib/ld-linux-armhf.so.3 /lib + ctest -C ${{env.BUILD_TYPE}} -V diff --git a/README.md b/README.md index 31ad1fb4..62b77444 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,15 @@ bfdev is a high-performance, aesthetically pleasing, and portable infrastructure ## Continuous Integration Status -| Status (master) | Status (devel) | Description | -| :----------------------------------------------------------: | :----------------------------------------------------------: | :----------------------------------: | -| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml?query=branch%3Adevel) | Build default config on Ubuntu gcc | -| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml?query=branch%3Adevel) | Build default config on Ubuntu clang | -| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml?query=branch%3Adevel) | Build default config on Macos | -| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Adevel) | Build default config on Windows | -| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Adevel) | Code analyse on codeql | +| Status (master) | Status (devel) | Description | +| :-------------: | :------------: | :---------: | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-gcc.yml?query=branch%3Adevel) | Build default config on Ubuntu GCC | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-clang.yml?query=branch%3Adevel) | Build default config on Ubuntu Clang | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/macos.yml?query=branch%3Adevel) | Build default config on Macos | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Adevel) | Build default config on Windows | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml?query=branch%3Adevel) | Cross build ARMv7 default on Ubuntu GCC | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml?query=branch%3Adevel) | Cross build AArch64 default on Ubuntu GCC | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Adevel) | Code analyse on codeql | ## Why Choose From b199a20b616fc67579a3faac8e55238a8e892613 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 1 Apr 2025 17:39:48 +0800 Subject: [PATCH 3/5] ci github: added mips and mips64 corss compile tests Signed-off-by: John Sanpe --- .github/workflows/ubuntu-mips-gcc.yml | 60 +++++++++++++++++++++++++ .github/workflows/ubuntu-mips64-gcc.yml | 60 +++++++++++++++++++++++++ README.md | 2 + 3 files changed, 122 insertions(+) create mode 100644 .github/workflows/ubuntu-mips-gcc.yml create mode 100644 .github/workflows/ubuntu-mips64-gcc.yml diff --git a/.github/workflows/ubuntu-mips-gcc.yml b/.github/workflows/ubuntu-mips-gcc.yml new file mode 100644 index 00000000..da609f59 --- /dev/null +++ b/.github/workflows/ubuntu-mips-gcc.yml @@ -0,0 +1,60 @@ +name: build default on ubuntu mips gcc + +on: + repository_dispatch: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: '0 */2 * * *' + +env: + BUILD_TYPE: Release + LD_LIBRARY_PATH: /usr/mips-linux-gnu/lib + +jobs: + build: + name: Test on ${{matrix.os}} + runs-on: ${{matrix.os}} + strategy: + matrix: + os: [ubuntu-22.04] + + steps: + - name: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: install dependencies + run: | + sudo apt update + sudo apt install cmake make qemu-user-binfmt \ + gcc gcc-mips-linux-gnu + + - name: configure cmake + run: | + cmake -B ${{github.workspace}}/build \ + -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install \ + -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ + -D CMAKE_C_COMPILER=mips-linux-gnu-gcc \ + -D HOST_C_COMPILER=gcc \ + -D BFDEV_STRICT=ON \ + -D BFDEV_EXAMPLES=ON \ + -D BFDEV_TESTSUITE=ON + + - name: make + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: install + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} -- install + + - name: ctest + working-directory: ${{github.workspace}}/build + run: | + sudo ln -s /usr/mips-linux-gnu/lib/ld.so.1 /lib + ctest -C ${{env.BUILD_TYPE}} -V diff --git a/.github/workflows/ubuntu-mips64-gcc.yml b/.github/workflows/ubuntu-mips64-gcc.yml new file mode 100644 index 00000000..71b3c9d2 --- /dev/null +++ b/.github/workflows/ubuntu-mips64-gcc.yml @@ -0,0 +1,60 @@ +name: build default on ubuntu mips64 gcc + +on: + repository_dispatch: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: '0 */2 * * *' + +env: + BUILD_TYPE: Release + LD_LIBRARY_PATH: /usr/mips64-linux-gnuabi64/lib + +jobs: + build: + name: Test on ${{matrix.os}} + runs-on: ${{matrix.os}} + strategy: + matrix: + os: [ubuntu-22.04] + + steps: + - name: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: install dependencies + run: | + sudo apt update + sudo apt install cmake make qemu-user-binfmt \ + gcc gcc-mips64-linux-gnuabi64 + + - name: configure cmake + run: | + cmake -B ${{github.workspace}}/build \ + -D CMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install \ + -D CMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \ + -D CMAKE_C_COMPILER=mips64-linux-gnuabi64-gcc \ + -D HOST_C_COMPILER=gcc \ + -D BFDEV_STRICT=ON \ + -D BFDEV_EXAMPLES=ON \ + -D BFDEV_TESTSUITE=ON + + - name: make + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} + + - name: install + run: | + cmake --build ${{github.workspace}}/build \ + --config ${{env.BUILD_TYPE}} -- install + + - name: ctest + working-directory: ${{github.workspace}}/build + run: | + sudo ln -s /usr/mips64-linux-gnuabi64/lib/ld.so.1 /lib64 + ctest -C ${{env.BUILD_TYPE}} -V diff --git a/README.md b/README.md index 62b77444..5b48be76 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ bfdev is a high-performance, aesthetically pleasing, and portable infrastructure | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/windows.yml?query=branch%3Adevel) | Build default config on Windows | | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-armv7-gcc.yml?query=branch%3Adevel) | Cross build ARMv7 default on Ubuntu GCC | | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-aarch64-gcc.yml?query=branch%3Adevel) | Cross build AArch64 default on Ubuntu GCC | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips-gcc.yml?query=branch%3Adevel) | Cross build MIPS default on Ubuntu GCC | +| [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips64-gcc.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips64-gcc.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips64-gcc.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/ubuntu-mips64-gcc.yml?query=branch%3Adevel) | Cross build MIPS64 default on Ubuntu GCC | | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=master)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Amaster) | [![build status](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml/badge.svg?branch=devel)](https://github.com/openbfdev/bfdev/actions/workflows/codeql.yml?query=branch%3Adevel) | Code analyse on codeql | ## Why Choose From af21b52fd855866f163f267e52d8b01b8ada9917 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 1 Apr 2025 20:07:55 +0800 Subject: [PATCH 4/5] fixup bitmap: fixed align issues in big endian machine Signed-off-by: John Sanpe --- include/bfdev/bitmap.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/include/bfdev/bitmap.h b/include/bfdev/bitmap.h index 587db470..4c5a410f 100644 --- a/include/bfdev/bitmap.h +++ b/include/bfdev/bitmap.h @@ -36,6 +36,9 @@ BFDEV_BEGIN_DECLS bfdev_align_check(bits, BFDEV_BITMAP_ALIGN) \ ) +#define bfdev_bitmap_size(nbits) \ + BFDEV_BITS_DIV_U8(bfdev_align_high(nbits, BFDEV_BITS_PER_LONG)) + static __bfdev_always_inline bfdev_bool bfdev_bitmap_empty(const unsigned long *src, unsigned int bits) { @@ -45,7 +48,7 @@ bfdev_bitmap_empty(const unsigned long *src, unsigned int bits) if (!bfdev_bitmap_const_aligned(bits)) return bfdev_find_first_bit(src, bits) >= bits; - return !bfdev_memdiff(src, BFDEV_UINT8_MIN, bits / BFDEV_BITS_PER_BYTE); + return !bfdev_memdiff(src, BFDEV_UINT8_MIN, BFDEV_BITS_DIV_U8(bits)); } static __bfdev_always_inline bfdev_bool @@ -57,7 +60,7 @@ bfdev_bitmap_full(const unsigned long *src, unsigned int bits) if (!bfdev_bitmap_const_aligned(bits)) return bfdev_find_first_zero(src, bits) >= bits; - return !bfdev_memdiff(src, BFDEV_UINT8_MAX, bits / BFDEV_BITS_PER_BYTE); + return !bfdev_memdiff(src, BFDEV_UINT8_MAX, BFDEV_BITS_DIV_U8(bits)); } static __bfdev_always_inline bfdev_bool @@ -70,7 +73,7 @@ bfdev_bitmap_equal(const unsigned long *src1, const unsigned long *src2, if (!bfdev_bitmap_const_aligned(bits)) return bfdev_bitmap_comp_equal(src1, src2, bits); - return bfdev_memcmp(src1, src2, bits / BFDEV_BITS_PER_BYTE); + return bfdev_memcmp(src1, src2, BFDEV_BITS_DIV_U8(bits)); } static __bfdev_always_inline bfdev_bool @@ -202,8 +205,8 @@ bfdev_bitmap_set(unsigned long *bitmap, unsigned int start, unsigned int bits) !bfdev_bitmap_const_aligned(bits)) return bfdev_bitmap_comp_set(bitmap, start, bits); - offset = start / BFDEV_BITS_PER_BYTE; - size = bits / BFDEV_BITS_PER_BYTE; + offset = BFDEV_BITS_DIV_U8(start); + size = BFDEV_BITS_DIV_U8(bits); bfdev_memset((char *)bitmap + offset, 0xff, size); } @@ -220,8 +223,8 @@ bfdev_bitmap_clr(unsigned long *bitmap, unsigned int start, unsigned int bits) !bfdev_bitmap_const_aligned(bits)) return bfdev_bitmap_comp_clr(bitmap, start, bits); - offset = start / BFDEV_BITS_PER_BYTE; - size = bits / BFDEV_BITS_PER_BYTE; + offset = BFDEV_BITS_DIV_U8(start); + size = BFDEV_BITS_DIV_U8(bits); bfdev_memset((char *)bitmap + offset, 0, size); } @@ -236,7 +239,7 @@ bfdev_bitmap_zero(unsigned long *bitmap, unsigned int bits) return; } - length = BFDEV_BITS_TO_U8(bits); + length = bfdev_bitmap_size(bits); bfdev_memset(bitmap, 0, length); } @@ -250,7 +253,7 @@ bfdev_bitmap_fill(unsigned long *bitmap, unsigned int bits) return; } - length = BFDEV_BITS_TO_U8(bits); + length = bfdev_bitmap_size(bits); bfdev_memset(bitmap, BFDEV_UINT8_MAX, length); } @@ -264,7 +267,7 @@ bfdev_bitmap_copy(unsigned long *dest, unsigned long *src, unsigned int bits) return; } - length = BFDEV_BITS_TO_U8(bits); + length = bfdev_bitmap_size(bits); bfdev_memcpy(dest, src, length); } From f767ab0de19fad857f81fac4cb3e8507f5fd14e1 Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Tue, 1 Apr 2025 20:08:25 +0800 Subject: [PATCH 5/5] docs bitwalk: fixup some document issues Signed-off-by: John Sanpe --- include/bfdev/bitwalk.h | 38 +++++++++++++++++++++++--------------- src/bitmap.c | 10 ++++------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/include/bfdev/bitwalk.h b/include/bfdev/bitwalk.h index 59a03d30..2a8492b3 100644 --- a/include/bfdev/bitwalk.h +++ b/include/bfdev/bitwalk.h @@ -28,8 +28,10 @@ bfdev_find_first_bit(const unsigned long *addr, unsigned int bits) { unsigned long value; - if (!bfdev_const_small_nbits(bits)) - return bfdev_comp_find_first_bit(addr, bits, 0UL, bfdev_false); + if (!bfdev_const_small_nbits(bits)) { + return bfdev_comp_find_first_bit(addr, bits, + BFDEV_ULONG_MIN, bfdev_false); + } value = *addr & BFDEV_BIT_LOW_MASK(bits); if (value == BFDEV_ULONG_MIN) @@ -45,8 +47,10 @@ bfdev_find_last_bit(const unsigned long *addr, unsigned int bits) { unsigned long value; - if (!bfdev_const_small_nbits(bits)) - return bfdev_comp_find_last_bit(addr, bits, 0UL, bfdev_false); + if (!bfdev_const_small_nbits(bits)) { + return bfdev_comp_find_last_bit(addr, bits, + BFDEV_ULONG_MIN, bfdev_false); + } value = *addr & BFDEV_BIT_LOW_MASK(bits); if (value == BFDEV_ULONG_MIN) @@ -70,8 +74,10 @@ bfdev_find_first_zero(const unsigned long *addr, unsigned int bits) { unsigned long value; - if (!bfdev_const_small_nbits(bits)) - return bfdev_comp_find_first_bit(addr, bits, ~0UL, bfdev_false); + if (!bfdev_const_small_nbits(bits)) { + return bfdev_comp_find_first_bit(addr, bits, + BFDEV_ULONG_MAX, bfdev_false); + } value = *addr | BFDEV_BIT_HIGH_MASK(bits); if (value == BFDEV_ULONG_MAX) @@ -87,8 +93,10 @@ bfdev_find_last_zero(const unsigned long *addr, unsigned int bits) { unsigned long value; - if (!bfdev_const_small_nbits(bits)) - return bfdev_comp_find_last_bit(addr, bits, ~0UL, bfdev_false); + if (!bfdev_const_small_nbits(bits)) { + return bfdev_comp_find_last_bit(addr, bits, + BFDEV_ULONG_MAX, bfdev_false); + } value = *addr | BFDEV_BIT_HIGH_MASK(bits); if (value == BFDEV_ULONG_MAX) @@ -116,7 +124,7 @@ bfdev_find_next_bit(const unsigned long *addr, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_next_bit(addr, BFDEV_NULL, - bits, offset, 0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MIN, bfdev_false); } if (bfdev_unlikely(offset >= bits)) @@ -139,7 +147,7 @@ bfdev_find_prev_bit(const unsigned long *addr, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_prev_bit(addr, BFDEV_NULL, - bits, offset, 0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MIN, bfdev_false); } if (bfdev_unlikely(offset >= bits)) @@ -154,7 +162,7 @@ bfdev_find_prev_bit(const unsigned long *addr, #endif /** - * bfdev_find_next_bit() - find next zero in a region. + * bfdev_find_next_zero() - find next zero in a region. * @block: the block to find. * @bits: number of bits in the block. * @offset: the bitnumber to start searching at. @@ -171,7 +179,7 @@ bfdev_find_next_zero(const unsigned long *addr, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_next_bit(addr, BFDEV_NULL, - bits, offset, ~0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MAX, bfdev_false); } if (bfdev_unlikely(offset >= bits)) @@ -194,7 +202,7 @@ bfdev_find_prev_zero(const unsigned long *addr, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_prev_bit(addr, BFDEV_NULL, - bits, offset, ~0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MAX, bfdev_false); } if (bfdev_unlikely(offset >= bits)) @@ -230,7 +238,7 @@ bfdev_find_next_and_bit(const unsigned long *addr1, const unsigned long *addr2, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_next_bit(addr1, addr2, - bits, offset, 0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MIN, bfdev_false); } value = *addr1 & *addr2 & BFDEV_BIT_RANGE(bits - 1, offset); @@ -250,7 +258,7 @@ bfdev_find_prev_and_bit(const unsigned long *addr1, const unsigned long *addr2, if (!bfdev_const_small_nbits(bits)) { return bfdev_comp_find_prev_bit(addr1, addr2, - bits, offset, 0UL, bfdev_false); + bits, offset, BFDEV_ULONG_MIN, bfdev_false); } if (bfdev_unlikely(offset >= bits)) diff --git a/src/bitmap.c b/src/bitmap.c index 4aa54d63..c3307b26 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -343,17 +343,15 @@ bfdev_bitmap_comp_clr(unsigned long *bitmap, unsigned int start, export unsigned long * bfdev_bitmap_alloc(const bfdev_alloc_t *alloc, unsigned int bits) { - return bfdev_malloc_array( - alloc, BFDEV_BITS_TO_LONG(bits), sizeof(unsigned long) - ); + return bfdev_malloc_array(alloc, BFDEV_BITS_TO_LONG(bits), + BFDEV_BYTES_PER_LONG); } export unsigned long * bfdev_bitmap_zalloc(const bfdev_alloc_t *alloc, unsigned int bits) { - return bfdev_zalloc_array( - alloc, BFDEV_BITS_TO_LONG(bits), sizeof(unsigned long) - ); + return bfdev_zalloc_array(alloc, BFDEV_BITS_TO_LONG(bits), + BFDEV_BYTES_PER_LONG); } export void