mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
97f2a2213e
* Move some code outside dbms/src folder * Fix paths
50 lines
969 B
C++
50 lines
969 B
C++
#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;
|
|
int res = ::posix_memalign(&new_buf, std::max(alignment, sizeof(void*)), size);
|
|
if (0 != res)
|
|
throwFromErrno("Cannot allocate memory (posix_memalign), size: "
|
|
+ formatReadableSizeWithBinarySuffix(size) + ", alignment: " + formatReadableSizeWithBinarySuffix(alignment) + ".",
|
|
ErrorCodes::CANNOT_ALLOCATE_MEMORY, res);
|
|
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();
|
|
}
|
|
|
|
}
|