參考代碼
MemoryRegion(size_t size, size_t alignment = 0)
: IMemoryRegion(size), _mem(nullptr), _ptr(nullptr)
{
if(size != 0)
{
// Allocate backing memory
size_t space = size + alignment;
_mem = std::shared_ptr<uint8_t>(new uint8_t[space](), [](uint8_t *ptr)
{
delete[] ptr;
});
_ptr = _mem.get();
// Calculate alignment offset
if(alignment != 0)
{
void *aligned_ptr = _mem.get();
std::align(alignment, size, aligned_ptr, space);
_ptr = aligned_ptr;
}
}
}
aligned_alloc
Defined in header <stdlib.h>
void *aligned_alloc( size_talignment, size_t size);
(since C11) |