From 6ed64f7b21f49db2d4f19207901a71580c157800 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 30 Apr 2025 13:22:38 +0200 Subject: [PATCH] Support const pointers in etl::is_aligned() (#1082) * Add Zephyr build system module.yml (#1074) The Zephyr build system requires that modules have a `module.yml` file to specify where the module cmake and kconfig files are located. These can also be explicitly set as "external" meaning that they do not exist within the module tree, itself. These build file can still be specified elsewhere via cmake variables, explained more in-depth here: https://docs.zephyrproject.org/latest/develop/modules.html#modules-module-ext-root This change makes it such that ETL can be included more easily in zephyr projects running on embedded systems. A similar change can be observed in the public nanopb repository, where the repo only requires its own `zephyr/module.yml` file to be found by the zephyr build system, but the kconfig and cmake additions can exist outside of the library repository. * Support const pointers to etl::is_aligned() --------- Co-authored-by: Zach Van Camp --- include/etl/alignment.h | 6 +++--- test/test_alignment.cpp | 17 +++++++++++++++++ zephyr/module.yml | 4 ++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 zephyr/module.yml diff --git a/include/etl/alignment.h b/include/etl/alignment.h index 627517be..dab55832 100644 --- a/include/etl/alignment.h +++ b/include/etl/alignment.h @@ -88,7 +88,7 @@ namespace etl //***************************************************************************** /// Check that 'p' has 'required_alignment'. //***************************************************************************** - inline bool is_aligned(void* p, size_t required_alignment) + inline bool is_aligned(const void* p, size_t required_alignment) { uintptr_t address = reinterpret_cast(p); return (address % required_alignment) == 0U; @@ -98,7 +98,7 @@ namespace etl /// Check that 'p' has 'Alignment'. //***************************************************************************** template - bool is_aligned(void* p) + bool is_aligned(const void* p) { uintptr_t address = reinterpret_cast(p); return (address % Alignment) == 0U; @@ -108,7 +108,7 @@ namespace etl /// Check that 'p' has the alignment of 'T'. //***************************************************************************** template - bool is_aligned(void* p) + bool is_aligned(const void* p) { return is_aligned::value>(p); } diff --git a/test/test_alignment.cpp b/test/test_alignment.cpp index d9a9de57..73c1f00f 100644 --- a/test/test_alignment.cpp +++ b/test/test_alignment.cpp @@ -161,6 +161,23 @@ namespace CHECK_FALSE(etl::is_aligned(p)); } + //************************************************************************* + TEST(test_is_aligned_tests_const) + { + alignas(uint32_t) const char buffer[2U * sizeof(uint32_t)] = {0, 1}; + + const char* p = buffer; + + CHECK_TRUE(etl::is_aligned(p, std::alignment_of())); + CHECK_TRUE(etl::is_aligned(p)); + CHECK_TRUE(etl::is_aligned(p)); + + ++p; + CHECK_FALSE(etl::is_aligned(p, std::alignment_of())); + CHECK_FALSE(etl::is_aligned(p)); + CHECK_FALSE(etl::is_aligned(p)); + } + //************************************************************************* TEST(test_type_with_alignment) { diff --git a/zephyr/module.yml b/zephyr/module.yml new file mode 100644 index 00000000..b8bf28d4 --- /dev/null +++ b/zephyr/module.yml @@ -0,0 +1,4 @@ +name: etl +build: + cmake-ext: true + kconfig-ext: true