2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/alloc/aligned/foreign.hpp>
|
|
|
|
#include <cruft/util/alloc/linear.hpp>
|
|
|
|
#include <cruft/util/pointer.hpp>
|
|
|
|
#include <cruft/util/tap.hpp>
|
2018-03-02 12:18:20 +11:00
|
|
|
|
|
|
|
int
|
|
|
|
main ()
|
|
|
|
{
|
2018-12-19 17:16:57 +11:00
|
|
|
static u08 buffer[1024*1024];
|
2018-03-02 12:18:20 +11:00
|
|
|
static constexpr std::size_t alignment = 3;
|
|
|
|
static constexpr std::size_t increment = 1;
|
|
|
|
|
|
|
|
// ensure we have an base pointer that's off-by-one to a likely natural
|
|
|
|
// system alignment
|
2018-12-19 17:16:57 +11:00
|
|
|
u08* base = cruft::align::up (
|
2018-03-02 12:18:20 +11:00
|
|
|
std::data (buffer),
|
|
|
|
alignof (std::max_align_t)
|
|
|
|
) + increment;
|
|
|
|
|
2018-12-19 17:55:24 +11:00
|
|
|
cruft::alloc::aligned::foreign<cruft::alloc::linear> alloc (
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::view(base,std::end(buffer)),
|
2018-03-02 12:18:20 +11:00
|
|
|
alignment
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2018-03-02 12:18:20 +11:00
|
|
|
|
|
|
|
// ensure the first element allocated falls at the base address
|
|
|
|
tap.expect_eq (base, alloc.data (), "allocator base address is the supplied base address");
|
2018-12-19 17:16:57 +11:00
|
|
|
tap.expect_eq (base, alloc.allocate (8).data (), "first allocation is the supplied base address");
|
2018-03-02 12:18:20 +11:00
|
|
|
|
|
|
|
// allocate a range of values and make sure they all satisfy our alignment.
|
|
|
|
// don't choose values which are likely to combine with the testing
|
|
|
|
// alignment to produce a likely system alignment. eg, 3 + 5 == 8 which is
|
|
|
|
// a power-of-2.
|
|
|
|
|
|
|
|
static const struct {
|
|
|
|
size_t size;
|
|
|
|
const char *message;
|
|
|
|
} TESTS[] = {
|
|
|
|
{ 9, "just over a power of two" },
|
|
|
|
{ 1, "a single byte" },
|
|
|
|
{ 64, "a cache line" },
|
|
|
|
{ 250, "multiple cache lines, but not a power of two" },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &t: TESTS) {
|
2018-12-19 17:16:57 +11:00
|
|
|
auto ptr = reinterpret_cast<uintptr_t> (alloc.allocate (t.size).data ());
|
2018-05-03 18:32:08 +10:00
|
|
|
auto offset = ptr - reinterpret_cast<uintptr_t> (base);
|
2021-04-13 16:05:08 +10:00
|
|
|
tap.expect_mod (offset, alignment, "{:s}", t.message);
|
2018-03-02 12:18:20 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
};
|