Skip to content

Commit 0321ef4

Browse files
committed
💚 Remove template argument from single allocator
1 parent 5c6ed12 commit 0321ef4

File tree

5 files changed

+34
-15
lines changed

5 files changed

+34
-15
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ libhal_unit_test(
5252
SOURCES
5353
src/control.cpp
5454
tests/main.test.cpp
55+
tests/control.test.cpp
5556
${SOURCE_LIST}
5657

5758
PACKAGES

conanfile.py

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def _runtime_select(self):
6767
return "ARM_CORTEX_GCC"
6868
elif self._is_arm_cortex and self.options.runtime == "estell":
6969
return "ARM_CORTEX_ESTELL"
70+
else:
71+
return "ARM_CORTEX_GCC"
7072

7173
def validate(self):
7274
if self.settings.get_safe("compiler.cppstd"):

src/control.cpp

+6-15
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
#include <array>
1616
#include <cstdint>
17-
#include <exception>
17+
#include <memory_resource>
1818

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

2221
namespace __cxxabiv1 { // NOLINT
2322
std::terminate_handler __terminate_handler = +[]() { // NOLINT
@@ -46,19 +45,12 @@ std::terminate_handler get_terminate() noexcept
4645
*
4746
* This allocator can only allocates space for a single exception object at a
4847
* time.
49-
*
50-
* @tparam size - size of the exception object memory buffer. If this is set too
51-
* small (less than 128 bytes), then it is likely that the memory will not be
52-
* enough for any exception runtime and will result in terminate being called.
5348
*/
54-
template<size_t size>
5549
class single_exception_allocator : public std::pmr::memory_resource
5650
{
5751
public:
5852
single_exception_allocator() = default;
59-
virtual ~single_exception_allocator() override
60-
{
61-
}
53+
~single_exception_allocator() override = default;
6254

6355
private:
6456
void* do_allocate(std::size_t p_size,
@@ -87,19 +79,18 @@ class single_exception_allocator : public std::pmr::memory_resource
8779
return this == &other;
8880
}
8981

90-
std::array<std::uint8_t, size> m_buffer{};
82+
std::array<std::uint8_t, 256> m_buffer{};
9183
bool m_allocated = false;
9284
};
9385

9486
// TODO(#11): Add macro to IFDEF this out if the user want to save 256 bytes.
95-
using default_exception_allocator = single_exception_allocator<256>;
96-
default_exception_allocator _default_allocator{}; // NOLINT
87+
single_exception_allocator _default_allocator{}; // NOLINT
9788
std::pmr::memory_resource* _exception_allocator =
9889
&_default_allocator; // NOLINT
9990

100-
void set_exception_allocator(std::pmr::memory_resource* p_allocator) noexcept
91+
void set_exception_allocator(std::pmr::memory_resource& p_allocator) noexcept
10192
{
102-
_exception_allocator = p_allocator;
93+
_exception_allocator = &p_allocator;
10394
}
10495

10596
std::pmr::memory_resource& get_exception_allocator() noexcept

tests/control.test.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <array>
2+
#include <memory_resource>
3+
4+
#include <libhal-exceptions/control.hpp>
5+
6+
#include <boost/ut.hpp>
7+
8+
namespace hal {
9+
void control_test()
10+
{
11+
using namespace boost::ut;
12+
13+
// setup
14+
std::array<std::byte, 1024> buffer;
15+
std::pmr::monotonic_buffer_resource resource(buffer.data(), buffer.size());
16+
17+
// exercise
18+
hal::set_exception_allocator(resource);
19+
20+
// verify
21+
expect(that % &resource == &hal::get_exception_allocator());
22+
}
23+
} // namespace hal

tests/main.test.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
// limitations under the License.
1414

1515
namespace hal {
16+
extern void control_test();
1617
} // namespace hal
1718

1819
int main()
1920
{
21+
hal::control_test();
2022
return 0;
2123
}

0 commit comments

Comments
 (0)