libcruft-util/test/alloc/linear.cpp
Danny Robson c2265b9ed2 alloc: add aligned::foreign allocator
sometimes we need to ensure memory allocation has a particular alignment
in an _offset_ buffer (which we have no control over, eg renderdoc's
OpenGL buffers).

this applies an offset to various operations that make the
aligned::direct allocator correctly align allocations for buffers that
aren't themselves aligned.
2018-03-02 12:21:38 +11:00

47 lines
1.3 KiB
C++

#include "tap.hpp"
#include "alloc/raw/linear.hpp"
///////////////////////////////////////////////////////////////////////////////
int
main (void)
{
util::TAP::logger tap;
constexpr size_t BUFFER_SIZE = 1024;
alignas (std::max_align_t) std::byte memory[BUFFER_SIZE];
util::alloc::raw::linear store (util::make_view (memory));
tap.expect_eq (store.begin (), std::begin (memory), "base pointers match");
tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");
tap.expect_eq (store.capacity (), BUFFER_SIZE, "bytes capacity matches");
tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (BUFFER_SIZE + 1, 1); },
"excessive allocation throws bad_alloc"
);
tap.expect_nothrow (
[&] (void) { store.allocate (BUFFER_SIZE); },
"maximum allocation succeeds"
);
tap.expect_eq (store.used (), BUFFER_SIZE, "bytes used matches");
tap.expect_eq (store.remain (), 0u, "bytes remain matches");
tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (1, 1); },
"minimum allocation fails after exhaustion"
);
store.reset ();
tap.expect_nothrow (
[&] (void) { store.allocate (1, 1); },
"minimum allocation succeeds after reset"
);
return tap.status ();
}