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
|
|
|
|
|
|
|
|
|
#define DBMS_DEFAULT_BUFFER_SIZE 1048576ULL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-06-21 18:43:29 +00:00
|
|
|
|
namespace detail
|
|
|
|
|
{
|
|
|
|
|
/** Замена std::vector<char> для использования в буферах.
|
|
|
|
|
* Отличается тем, что не делает лишний memset. (И почти ничего не делает.)
|
|
|
|
|
*/
|
2012-06-22 16:54:51 +00:00
|
|
|
|
struct Memory : boost::noncopyable
|
2012-06-21 18:43:29 +00:00
|
|
|
|
{
|
2012-06-21 19:04:52 +00:00
|
|
|
|
size_t m_capacity;
|
2012-06-21 18:43:29 +00:00
|
|
|
|
size_t m_size;
|
|
|
|
|
char * m_data;
|
|
|
|
|
|
2012-06-21 19:04:52 +00:00
|
|
|
|
Memory() : m_capacity(0), m_size(0), m_data(NULL) {}
|
2013-06-15 04:44:19 +00:00
|
|
|
|
Memory(size_t size_) : m_capacity(size_), m_size(m_capacity), m_data(size_ ? new char[m_capacity] : NULL) {}
|
2012-06-21 18:43:29 +00:00
|
|
|
|
|
|
|
|
|
~Memory()
|
|
|
|
|
{
|
|
|
|
|
if (m_data)
|
|
|
|
|
{
|
|
|
|
|
delete[] m_data;
|
|
|
|
|
m_data = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]; }
|
|
|
|
|
|
|
|
|
|
void resize(size_t new_size)
|
|
|
|
|
{
|
2012-06-21 19:04:52 +00:00
|
|
|
|
if (new_size < m_capacity)
|
|
|
|
|
{
|
|
|
|
|
m_size = new_size;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (m_data)
|
|
|
|
|
delete[] m_data;
|
|
|
|
|
|
|
|
|
|
m_capacity = new_size;
|
|
|
|
|
m_size = m_capacity;
|
|
|
|
|
m_data = new char[m_capacity];
|
|
|
|
|
}
|
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:
|
2012-06-21 18:43:29 +00:00
|
|
|
|
detail::Memory memory;
|
2011-06-27 18:22:14 +00:00
|
|
|
|
public:
|
2013-06-15 04:44:19 +00:00
|
|
|
|
/// Если передать не-NULL existing_memory, то буфер не будет создавать свой кусок памяти, а будет использовать существующий (и не будет им владеть).
|
|
|
|
|
BufferWithOwnMemory(size_t size = DBMS_DEFAULT_BUFFER_SIZE, char * existing_memory = NULL) : Base(NULL, 0), memory(existing_memory ? 0 : size)
|
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
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|