CETL 0.0.0
 
Loading...
Searching...
No Matches
Example 5: Using the UnsynchronizedBArrayMemoryResource class

Also see Example 4: Using the UnsynchronizedBufferMemoryResource class

#include <iostream>
#include <gtest/gtest.h>
TEST(example_05_array_memory_resource, example_0)
{
static_assert(alignof(std::max_align_t) < 128, "Wow, what hardware are you running on?");
constexpr std::size_t BufferSizeBytes = 64;
// let's say we have a buffer that must be aligned to a 128-byte (1024-bit) boundary. If we tried to use
// UnsynchronizedArrayMemoryResource with a 64-byte internal array, on a typical system, the allocation would fail.
void* r = nullptr;
#ifdefined(__cpp_exceptions)
try
{
#endif
r = resource.allocate(64, 128);
#ifdefined(__cpp_exceptions)
} catch (const std::bad_alloc&)
{
// This is expected.
}
#endif
std::cout << "Over-aligned attempt failed: " << r << std::endl;
}
TEST(example_05_array_memory_resource, example_1)
{
// By over-provisioning the buffer you can now get the alignment you want:
constexpr std::size_t BufferSizeBytes = 64 + 128;
void* r = resource.allocate(64, 128);
std::cout << "Over-aligned address at: " << r << std::endl;
resource.deallocate(r, 64, 128);
}
Defines a memory_resource type backed by an array data member and using cetl::pf17 types.
Defines a type compatible with C++17 std::byte.
Implementation of cetl::pf17::pmr::memory_resource that uses cetl::pmr::UnsynchronizedBufferMemoryRes...
Definition array_memory_resource.hpp:45
void * allocate(std::size_t size_bytes, std::size_t alignment=alignof(std::max_align_t))
Allocate memory.
Definition memory_resource.hpp:74
T endl(T... args)