ClickHouse/src/IO/BufferWithOwnMemory.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

203 lines
4.9 KiB
C++
Raw Normal View History

#pragma once
2012-06-22 16:54:51 +00:00
#include <boost/noncopyable.hpp>
#include <Common/ProfileEvents.h>
#include <Common/Allocator.h>
#include <Common/Exception.h>
#include <Core/Defines.h>
2022-08-17 22:35:50 +00:00
#include <base/arithmeticOverflow.h>
namespace ProfileEvents
{
extern const Event IOBufferAllocs;
extern const Event IOBufferAllocBytes;
}
namespace DB
{
2022-08-17 22:35:50 +00:00
namespace ErrorCodes
{
extern const int ARGUMENT_OUT_OF_BOUND;
}
/** Replacement for std::vector<char> to use in buffers.
* Differs in that is doesn't do unneeded memset. (And also tries to do as little as possible.)
* Also allows to allocate aligned piece of memory (to use with O_DIRECT, for example).
*/
template <typename Allocator = Allocator<false>>
struct Memory : boost::noncopyable, Allocator
2012-06-21 18:43:29 +00:00
{
static constexpr size_t pad_right = PADDING_FOR_SIMD - 1;
2018-08-30 20:12:15 +00:00
size_t m_capacity = 0; /// With padding.
size_t m_size = 0;
char * m_data = nullptr;
size_t alignment = 0;
Memory() = default;
/// If alignment != 0, then allocate memory aligned to specified value.
2022-08-17 22:35:50 +00:00
explicit Memory(size_t size_, size_t alignment_ = 0) : alignment(alignment_)
{
2022-08-17 22:35:50 +00:00
alloc(size_);
}
~Memory()
{
dealloc();
}
2021-07-26 00:34:36 +00:00
void swap(Memory & rhs) noexcept
{
std::swap(m_capacity, rhs.m_capacity);
std::swap(m_size, rhs.m_size);
std::swap(m_data, rhs.m_data);
std::swap(alignment, rhs.alignment);
2021-07-26 00:34:36 +00:00
}
2021-07-26 00:34:36 +00:00
Memory(Memory && rhs) noexcept
{
swap(rhs);
}
Memory & operator=(Memory && rhs) noexcept
{
swap(rhs);
return *this;
}
size_t size() const { return m_size; }
const char & operator[](size_t i) const { return m_data[i]; }
char & operator[](size_t i) { return m_data[i]; }
const char * data() const { return m_data; }
char * data() { return m_data; }
void resize(size_t new_size)
{
2022-08-17 22:35:50 +00:00
if (!m_data)
{
2022-08-17 22:35:50 +00:00
alloc(new_size);
return;
}
2022-08-17 22:35:50 +00:00
if (new_size <= m_capacity - pad_right)
2012-06-21 18:43:29 +00:00
{
m_size = new_size;
return;
}
size_t new_capacity = withPadding(new_size);
2022-08-17 22:35:50 +00:00
size_t diff = new_capacity - m_capacity;
ProfileEvents::increment(ProfileEvents::IOBufferAllocBytes, diff);
m_data = static_cast<char *>(Allocator::realloc(m_data, m_capacity, new_capacity, alignment));
m_capacity = new_capacity;
m_size = new_size;
}
private:
static size_t withPadding(size_t value)
{
2022-08-17 22:35:50 +00:00
size_t res = 0;
if (common::addOverflow<size_t>(value, pad_right, res))
throw Exception(ErrorCodes::ARGUMENT_OUT_OF_BOUND, "value is too big to apply padding");
2022-08-17 22:35:50 +00:00
return res;
}
void alloc(size_t new_size)
{
if (!new_size)
{
2014-04-08 07:31:51 +00:00
m_data = nullptr;
return;
}
size_t new_capacity = withPadding(new_size);
2022-08-17 22:35:50 +00:00
ProfileEvents::increment(ProfileEvents::IOBufferAllocs);
2022-08-17 22:35:50 +00:00
ProfileEvents::increment(ProfileEvents::IOBufferAllocBytes, new_capacity);
m_data = static_cast<char *>(Allocator::alloc(new_capacity, alignment));
m_capacity = new_capacity;
2022-08-17 22:35:50 +00:00
m_size = new_size;
}
void dealloc()
{
if (!m_data)
return;
Allocator::free(m_data, m_capacity);
m_data = nullptr; /// To avoid double free if next alloc will throw an exception.
}
};
2012-06-21 18:43:29 +00:00
/** Buffer that could own its working memory.
* Template parameter: ReadBuffer or WriteBuffer
*/
template <typename Base>
class BufferWithOwnMemory : public Base
{
protected:
Memory<> memory;
public:
/// If non-nullptr 'existing_memory' is passed, then buffer will not create its own memory and will use existing_memory without ownership.
explicit BufferWithOwnMemory(size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = nullptr, size_t alignment = 0)
: Base(nullptr, 0), memory(existing_memory ? 0 : size, alignment)
{
Base::set(existing_memory ? existing_memory : memory.data(), size);
Base::padded = !existing_memory;
}
};
2020-06-11 23:00:49 +00:00
/** Buffer that could write data to external memory which came from outside
* Template parameter: ReadBuffer or WriteBuffer
*/
template <typename Base>
class BufferWithOutsideMemory : public Base
{
protected:
Memory<> & memory;
public:
2020-08-14 00:34:35 +00:00
explicit BufferWithOutsideMemory(Memory<> & memory_)
2020-10-05 15:59:29 +00:00
: Base(memory_.data(), memory_.size()), memory(memory_)
2020-06-11 23:00:49 +00:00
{
2020-10-05 15:59:29 +00:00
Base::set(memory.data(), memory.size(), 0);
2020-06-11 23:00:49 +00:00
Base::padded = false;
}
2020-08-14 00:34:35 +00:00
size_t getActualSize()
{
return Base::count();
}
2020-06-11 23:00:49 +00:00
private:
void nextImpl() final
2020-06-11 23:00:49 +00:00
{
2020-12-29 22:34:26 +00:00
const size_t prev_size = Base::position() - memory.data();
2020-10-05 15:59:29 +00:00
memory.resize(2 * prev_size + 1);
2020-10-06 14:02:01 +00:00
Base::set(memory.data() + prev_size, memory.size() - prev_size, 0);
2020-06-11 23:00:49 +00:00
}
2023-11-18 16:30:49 +00:00
void finalizeImpl() final
{
/// there is no need to allocate twice more memory at finalize()
/// So make that call no op, do not call here nextImpl()
}
2020-06-11 23:00:49 +00:00
};
}