From 0321ef40bbddee0be3ac945ece61461d22dd959d Mon Sep 17 00:00:00 2001 From: Khalil Estell Date: Thu, 25 Jul 2024 11:29:11 -0700 Subject: [PATCH] :green_heart: Remove template argument from single allocator --- CMakeLists.txt | 1 + conanfile.py | 2 ++ src/control.cpp | 21 ++++++--------------- tests/control.test.cpp | 23 +++++++++++++++++++++++ tests/main.test.cpp | 2 ++ 5 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 tests/control.test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e86120f..4192dc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ libhal_unit_test( SOURCES src/control.cpp tests/main.test.cpp + tests/control.test.cpp ${SOURCE_LIST} PACKAGES diff --git a/conanfile.py b/conanfile.py index e5162e1..ee09f72 100644 --- a/conanfile.py +++ b/conanfile.py @@ -67,6 +67,8 @@ def _runtime_select(self): return "ARM_CORTEX_GCC" elif self._is_arm_cortex and self.options.runtime == "estell": return "ARM_CORTEX_ESTELL" + else: + return "ARM_CORTEX_GCC" def validate(self): if self.settings.get_safe("compiler.cppstd"): diff --git a/src/control.cpp b/src/control.cpp index 9e49878..6a4fbbb 100644 --- a/src/control.cpp +++ b/src/control.cpp @@ -14,10 +14,9 @@ #include #include -#include +#include #include -#include namespace __cxxabiv1 { // NOLINT std::terminate_handler __terminate_handler = +[]() { // NOLINT @@ -46,19 +45,12 @@ std::terminate_handler get_terminate() noexcept * * This allocator can only allocates space for a single exception object at a * time. - * - * @tparam size - size of the exception object memory buffer. If this is set too - * small (less than 128 bytes), then it is likely that the memory will not be - * enough for any exception runtime and will result in terminate being called. */ -template class single_exception_allocator : public std::pmr::memory_resource { public: single_exception_allocator() = default; - virtual ~single_exception_allocator() override - { - } + ~single_exception_allocator() override = default; private: void* do_allocate(std::size_t p_size, @@ -87,19 +79,18 @@ class single_exception_allocator : public std::pmr::memory_resource return this == &other; } - std::array m_buffer{}; + std::array m_buffer{}; bool m_allocated = false; }; // TODO(#11): Add macro to IFDEF this out if the user want to save 256 bytes. -using default_exception_allocator = single_exception_allocator<256>; -default_exception_allocator _default_allocator{}; // NOLINT +single_exception_allocator _default_allocator{}; // NOLINT std::pmr::memory_resource* _exception_allocator = &_default_allocator; // NOLINT -void set_exception_allocator(std::pmr::memory_resource* p_allocator) noexcept +void set_exception_allocator(std::pmr::memory_resource& p_allocator) noexcept { - _exception_allocator = p_allocator; + _exception_allocator = &p_allocator; } std::pmr::memory_resource& get_exception_allocator() noexcept diff --git a/tests/control.test.cpp b/tests/control.test.cpp new file mode 100644 index 0000000..afcf8dd --- /dev/null +++ b/tests/control.test.cpp @@ -0,0 +1,23 @@ +#include +#include + +#include + +#include + +namespace hal { +void control_test() +{ + using namespace boost::ut; + + // setup + std::array buffer; + std::pmr::monotonic_buffer_resource resource(buffer.data(), buffer.size()); + + // exercise + hal::set_exception_allocator(resource); + + // verify + expect(that % &resource == &hal::get_exception_allocator()); +} +} // namespace hal \ No newline at end of file diff --git a/tests/main.test.cpp b/tests/main.test.cpp index 7cb7c4e..24cc09b 100644 --- a/tests/main.test.cpp +++ b/tests/main.test.cpp @@ -13,9 +13,11 @@ // limitations under the License. namespace hal { +extern void control_test(); } // namespace hal int main() { + hal::control_test(); return 0; } \ No newline at end of file