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>
|
|
|
|
|
|
2013-10-13 06:53:11 +00:00
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
2011-06-27 18:22:14 +00:00
|
|
|
|
#define DBMS_DEFAULT_BUFFER_SIZE 1048576ULL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
|
/** Замена std::vector<char> для использования в буферах.
|
|
|
|
|
* Отличается тем, что не делает лишний memset. (И почти ничего не делает.)
|
2013-10-13 06:53:11 +00:00
|
|
|
|
* Также можно попросить выделять выровненный кусок памяти.
|
2013-09-08 05:53:10 +00:00
|
|
|
|
*/
|
|
|
|
|
struct Memory : boost::noncopyable
|
2012-06-21 18:43:29 +00:00
|
|
|
|
{
|
2013-09-08 05:53:10 +00:00
|
|
|
|
size_t m_capacity;
|
|
|
|
|
size_t m_size;
|
|
|
|
|
char * m_data;
|
2013-10-13 06:53:11 +00:00
|
|
|
|
size_t alignment;
|
|
|
|
|
|
|
|
|
|
Memory() : m_capacity(0), m_size(0), m_data(NULL), alignment(0) {}
|
2012-06-21 18:43:29 +00:00
|
|
|
|
|
2013-10-13 06:53:11 +00:00
|
|
|
|
/// Если alignment != 0, то будет выделяться память, выровненная на alignment.
|
|
|
|
|
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
|
|
|
|
|
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]; }
|
2012-06-21 18:43:29 +00:00
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
|
void resize(size_t new_size)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2013-10-13 06:53:11 +00:00
|
|
|
|
dealloc();
|
2012-06-21 19:04:52 +00:00
|
|
|
|
|
2013-09-08 05:53:10 +00:00
|
|
|
|
m_capacity = new_size;
|
|
|
|
|
m_size = m_capacity;
|
2013-10-13 06:53:11 +00:00
|
|
|
|
|
|
|
|
|
alloc();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void alloc()
|
|
|
|
|
{
|
|
|
|
|
if (!m_capacity)
|
|
|
|
|
{
|
|
|
|
|
m_data = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-04 04:53:07 +00:00
|
|
|
|
ProfileEvents::increment(ProfileEvents::IOBufferAllocs);
|
|
|
|
|
ProfileEvents::increment(ProfileEvents::IOBufferAllocBytes, m_capacity);
|
|
|
|
|
|
2013-10-13 06:53:11 +00:00
|
|
|
|
if (!alignment)
|
|
|
|
|
{
|
|
|
|
|
m_data = reinterpret_cast<char *>(malloc(m_capacity));
|
|
|
|
|
|
|
|
|
|
if (!m_data)
|
|
|
|
|
throw Exception("Cannot allocate memory (malloc)", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
|
|
|
|
|
|
|
|
|
return;
|
2012-06-21 18:43:29 +00:00
|
|
|
|
}
|
2013-10-13 06:53:11 +00:00
|
|
|
|
|
|
|
|
|
int res = posix_memalign(reinterpret_cast<void **>(&m_data), alignment, (m_capacity + alignment - 1) / alignment * alignment);
|
|
|
|
|
|
|
|
|
|
if (0 != res)
|
|
|
|
|
DB::throwFromErrno("Cannot allocate memory (posix_memalign)", ErrorCodes::CANNOT_ALLOCATE_MEMORY, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dealloc()
|
|
|
|
|
{
|
|
|
|
|
if (m_data)
|
|
|
|
|
free(reinterpret_cast<void *>(m_data));
|
2013-09-08 05:53:10 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2012-06-21 18:43:29 +00:00
|
|
|
|
|
|
|
|
|
|
2013-06-15 04:44:19 +00:00
|
|
|
|
/** Буфер, который может сам владеть своим куском памяти для работы.
|
2011-06-27 18:22:14 +00:00
|
|
|
|
* Аргумент шаблона - ReadBuffer или WriteBuffer
|
|
|
|
|
*/
|
|
|
|
|
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:
|
2013-06-15 04:44:19 +00:00
|
|
|
|
/// Если передать не-NULL existing_memory, то буфер не будет создавать свой кусок памяти, а будет использовать существующий (и не будет им владеть).
|
2013-10-13 06:53:11 +00:00
|
|
|
|
BufferWithOwnMemory(size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = NULL, size_t alignment = 0) : Base(NULL, 0), memory(existing_memory ? 0 : size, alignment)
|
2011-06-27 18:22:14 +00:00
|
|
|
|
{
|
2013-06-15 04:44:19 +00:00
|
|
|
|
Base::set(existing_memory ? existing_memory : &memory[0], size);
|
2011-06-27 18:22:14 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|