2018-09-01 04:25:43 +00:00
|
|
|
#include <Common/AlignedBuffer.h>
|
|
|
|
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/formatReadable.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_ALLOCATE_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AlignedBuffer::alloc(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
void * new_buf;
|
2018-09-01 04:39:46 +00:00
|
|
|
int res = ::posix_memalign(&new_buf, std::max(alignment, sizeof(void*)), size);
|
2018-09-01 04:25:43 +00:00
|
|
|
if (0 != res)
|
2023-12-15 18:25:49 +00:00
|
|
|
throw ErrnoException(
|
|
|
|
ErrorCodes::CANNOT_ALLOCATE_MEMORY,
|
|
|
|
"Cannot allocate memory (posix_memalign), size: {}, alignment: {}.",
|
|
|
|
ReadableSize(size),
|
|
|
|
ReadableSize(alignment));
|
2018-09-01 04:25:43 +00:00
|
|
|
buf = new_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlignedBuffer::dealloc()
|
|
|
|
{
|
|
|
|
if (buf)
|
|
|
|
::free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlignedBuffer::reset(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
dealloc();
|
|
|
|
alloc(size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedBuffer::AlignedBuffer(size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
alloc(size, alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
AlignedBuffer::~AlignedBuffer()
|
|
|
|
{
|
|
|
|
dealloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|