Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💚 Remove template argument from single allocator #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ libhal_unit_test(
SOURCES
src/control.cpp
tests/main.test.cpp
tests/control.test.cpp
${SOURCE_LIST}

PACKAGES
Expand Down
2 changes: 2 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
21 changes: 6 additions & 15 deletions src/control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@

#include <array>
#include <cstdint>
#include <exception>
#include <memory_resource>

#include <libhal-exceptions/control.hpp>
#include <memory_resource>

namespace __cxxabiv1 { // NOLINT
std::terminate_handler __terminate_handler = +[]() { // NOLINT
Expand Down Expand Up @@ -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<size_t size>
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,
Expand Down Expand Up @@ -87,19 +79,18 @@ class single_exception_allocator : public std::pmr::memory_resource
return this == &other;
}

std::array<std::uint8_t, size> m_buffer{};
std::array<std::uint8_t, 256> 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
Expand Down
23 changes: 23 additions & 0 deletions tests/control.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <array>
#include <memory_resource>

#include <libhal-exceptions/control.hpp>

#include <boost/ut.hpp>

namespace hal {
void control_test()
{
using namespace boost::ut;

// setup
std::array<std::byte, 1024> 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
2 changes: 2 additions & 0 deletions tests/main.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
// limitations under the License.

namespace hal {
extern void control_test();
} // namespace hal

int main()
{
hal::control_test();
return 0;
}
Loading