2011-12-30 17:57:02 +00:00
|
|
|
#pragma once
|
2011-06-27 18:22:14 +00:00
|
|
|
|
2012-06-22 16:54:51 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2011-06-27 18:22:14 +00:00
|
|
|
|
2014-01-04 04:53:07 +00:00
|
|
|
#include <DB/Common/ProfileEvents.h>
|
2015-08-16 13:00:22 +00:00
|
|
|
#include <DB/Common/Allocator.h>
|
2014-01-04 04:53:07 +00:00
|
|
|
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2015-04-12 04:39:20 +00:00
|
|
|
#include <DB/Core/Defines.h>
|
2011-06-27 18:22:14 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
|
|
|
extern const Event IOBufferAllocs;
|
|
|
|
extern const Event IOBufferAllocBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-27 18:22:14 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** 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).
|
2013-09-08 05:53:10 +00:00
|
|
|
*/
|
2015-11-15 03:09:40 +00:00
|
|
|
struct Memory : boost::noncopyable, Allocator<false>
|
2012-06-21 18:43:29 +00:00
|
|
|
{
|
2014-03-13 20:12:40 +00:00
|
|
|
size_t m_capacity = 0;
|
|
|
|
size_t m_size = 0;
|
|
|
|
char * m_data = nullptr;
|
|
|
|
size_t alignment = 0;
|
2013-10-13 06:53:11 +00:00
|
|
|
|
2017-03-09 04:26:17 +00:00
|
|
|
Memory() {}
|
2012-06-21 18:43:29 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/// If alignment != 0, then allocate memory aligned to specified value.
|
2013-10-13 06:53:11 +00:00
|
|
|
Memory(size_t size_, size_t alignment_ = 0) : m_capacity(size_), m_size(m_capacity), alignment(alignment_)
|
|
|
|
{
|
|
|
|
alloc();
|
|
|
|
}
|
2012-06-21 18:43:29 +00:00
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
~Memory()
|
|
|
|
{
|
2013-10-13 06:53:11 +00:00
|
|
|
dealloc();
|
2013-09-08 05:53:10 +00:00
|
|
|
}
|
2012-06-21 18:43:29 +00:00
|
|
|
|
2014-03-13 20:12:40 +00:00
|
|
|
Memory(Memory && rhs)
|
|
|
|
{
|
|
|
|
*this = std::move(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Memory & operator=(Memory && rhs)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
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]; }
|
2016-07-31 05:56:36 +00:00
|
|
|
const char * data() const { return m_data; }
|
|
|
|
char * data() { return m_data; }
|
2012-06-21 18:43:29 +00:00
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
void resize(size_t new_size)
|
|
|
|
{
|
2015-09-01 15:08:37 +00:00
|
|
|
if (0 == m_capacity)
|
|
|
|
{
|
|
|
|
m_size = m_capacity = new_size;
|
|
|
|
alloc();
|
|
|
|
}
|
|
|
|
else if (new_size < m_capacity)
|
2012-06-21 18:43:29 +00:00
|
|
|
{
|
2013-09-08 05:53:10 +00:00
|
|
|
m_size = new_size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-31 16:49:09 +00:00
|
|
|
new_size = align(new_size, alignment);
|
2016-10-24 02:02:37 +00:00
|
|
|
m_data = static_cast<char *>(Allocator::realloc(m_data, m_capacity, new_size, alignment));
|
2013-09-08 05:53:10 +00:00
|
|
|
m_capacity = new_size;
|
|
|
|
m_size = m_capacity;
|
2013-10-13 06:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:49:09 +00:00
|
|
|
static size_t align(const size_t value, const size_t alignment)
|
2015-08-16 13:00:22 +00:00
|
|
|
{
|
|
|
|
if (!alignment)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
return (value + alignment - 1) / alignment * alignment;
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:49:09 +00:00
|
|
|
private:
|
2013-10-13 06:53:11 +00:00
|
|
|
void alloc()
|
|
|
|
{
|
|
|
|
if (!m_capacity)
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
m_data = nullptr;
|
2013-10-13 06:53:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-04 04:53:07 +00:00
|
|
|
ProfileEvents::increment(ProfileEvents::IOBufferAllocs);
|
|
|
|
ProfileEvents::increment(ProfileEvents::IOBufferAllocBytes, m_capacity);
|
|
|
|
|
2015-08-31 16:49:09 +00:00
|
|
|
size_t new_capacity = align(m_capacity, alignment);
|
2016-10-24 02:02:37 +00:00
|
|
|
m_data = static_cast<char *>(Allocator::alloc(new_capacity, alignment));
|
2015-08-16 13:00:22 +00:00
|
|
|
m_capacity = new_capacity;
|
2015-04-10 17:51:39 +00:00
|
|
|
m_size = m_capacity;
|
2013-10-13 06:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dealloc()
|
|
|
|
{
|
2014-05-03 22:57:43 +00:00
|
|
|
if (!m_data)
|
|
|
|
return;
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
Allocator::free(m_data, m_capacity);
|
|
|
|
m_data = nullptr; /// To avoid double free if next alloc will throw an exception.
|
2013-09-08 05:53:10 +00:00
|
|
|
}
|
|
|
|
};
|
2012-06-21 18:43:29 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** Buffer that could own its working memory.
|
|
|
|
* Template parameter: ReadBuffer or WriteBuffer
|
2011-06-27 18:22:14 +00:00
|
|
|
*/
|
|
|
|
template <typename Base>
|
|
|
|
class BufferWithOwnMemory : public Base
|
|
|
|
{
|
|
|
|
protected:
|
2013-09-08 05:53:10 +00:00
|
|
|
Memory memory;
|
2011-06-27 18:22:14 +00:00
|
|
|
public:
|
2016-10-24 02:02:37 +00:00
|
|
|
/// If non-nullptr 'existing_memory' is passed, then buffer will not create its own memory and will use existing_memory without ownership.
|
2016-07-31 05:56:36 +00:00
|
|
|
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)
|
2011-06-27 18:22:14 +00:00
|
|
|
{
|
2016-07-31 05:56:36 +00:00
|
|
|
Base::set(existing_memory ? existing_memory : memory.data(), size);
|
2011-06-27 18:22:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|