2013-09-15 10:53:53 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
2013-12-08 02:29:40 +00:00
|
|
|
|
#include <boost/iterator_adaptors.hpp>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
|
|
|
|
|
#include <Yandex/likely.h>
|
|
|
|
|
#include <Yandex/strong_typedef.h>
|
|
|
|
|
|
2014-05-03 22:57:43 +00:00
|
|
|
|
#include <DB/Common/MemoryTracker.h>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Динамический массив для POD-типов.
|
|
|
|
|
* Предназначен для небольшого количества больших массивов (а не большого количества маленьких).
|
|
|
|
|
* А точнее - для использования в ColumnVector.
|
|
|
|
|
* Отличается от std::vector тем, что не инициализирует элементы.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
*
|
|
|
|
|
* Сделан некопируемым, чтобы не было случайных копий. Скопировать данные можно с помощью метода assign.
|
|
|
|
|
*
|
2013-09-15 10:53:53 +00:00
|
|
|
|
* Поддерживается только часть интерфейса std::vector.
|
|
|
|
|
*
|
2013-12-08 02:29:40 +00:00
|
|
|
|
* Конструктор по-умолчанию создаёт пустой объект, который не выделяет память.
|
2015-01-22 01:13:13 +00:00
|
|
|
|
* Затем выделяется память минимум под POD_ARRAY_INITIAL_SIZE элементов.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
*
|
|
|
|
|
* При первом выделении памяти использует std::allocator.
|
|
|
|
|
* В реализации из libstdc++ он кэширует куски памяти несколько больше, чем обычный malloc.
|
|
|
|
|
*
|
|
|
|
|
* При изменении размера, использует realloc, который может (но не обязан) использовать mremap для больших кусков памяти.
|
|
|
|
|
* По факту, mremap используется при использовании аллокатора из glibc, но не используется, например, в tcmalloc.
|
|
|
|
|
*
|
2013-09-15 10:53:53 +00:00
|
|
|
|
* Если вставлять элементы push_back-ом, не делая reserve, то PODArray примерно в 2.5 раза быстрее std::vector.
|
|
|
|
|
*/
|
2015-01-22 01:13:13 +00:00
|
|
|
|
#define POD_ARRAY_INITIAL_SIZE 4096UL
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
template <typename T>
|
|
|
|
|
class PODArray : private boost::noncopyable, private std::allocator<char> /// empty base optimization
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
typedef std::allocator<char> Allocator;
|
|
|
|
|
|
|
|
|
|
char * c_start;
|
|
|
|
|
char * c_end;
|
|
|
|
|
char * c_end_of_storage;
|
|
|
|
|
|
|
|
|
|
bool use_libc_realloc;
|
|
|
|
|
|
|
|
|
|
T * t_start() { return reinterpret_cast<T *>(c_start); }
|
|
|
|
|
T * t_end() { return reinterpret_cast<T *>(c_end); }
|
|
|
|
|
T * t_end_of_storage() { return reinterpret_cast<T *>(c_end_of_storage); }
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
const T * t_start() const { return reinterpret_cast<const T *>(c_start); }
|
|
|
|
|
const T * t_end() const { return reinterpret_cast<const T *>(c_end); }
|
|
|
|
|
const T * t_end_of_storage() const { return reinterpret_cast<const T *>(c_end_of_storage); }
|
|
|
|
|
|
|
|
|
|
size_t storage_size() const { return c_end_of_storage - c_start; }
|
|
|
|
|
static size_t byte_size(size_t n) { return n * sizeof(T); }
|
|
|
|
|
|
|
|
|
|
static size_t round_up_to_power_of_two(size_t n)
|
|
|
|
|
{
|
|
|
|
|
--n;
|
|
|
|
|
n |= n >> 1;
|
|
|
|
|
n |= n >> 2;
|
|
|
|
|
n |= n >> 4;
|
|
|
|
|
n |= n >> 8;
|
|
|
|
|
n |= n >> 16;
|
|
|
|
|
n |= n >> 32;
|
|
|
|
|
++n;
|
|
|
|
|
|
|
|
|
|
return n;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2015-01-22 01:13:13 +00:00
|
|
|
|
static size_t to_size(size_t n) { return byte_size(std::max(POD_ARRAY_INITIAL_SIZE, round_up_to_power_of_two(n))); }
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
void alloc(size_t n)
|
|
|
|
|
{
|
2013-12-08 02:29:40 +00:00
|
|
|
|
if (n == 0)
|
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
c_start = c_end = c_end_of_storage = nullptr;
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
size_t bytes_to_alloc = to_size(n);
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
|
|
if (current_memory_tracker)
|
|
|
|
|
current_memory_tracker->alloc(bytes_to_alloc);
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_start = c_end = Allocator::allocate(bytes_to_alloc);
|
|
|
|
|
c_end_of_storage = c_start + bytes_to_alloc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dealloc()
|
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
if (c_start == nullptr)
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return;
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
if (use_libc_realloc)
|
|
|
|
|
::free(c_start);
|
|
|
|
|
else
|
|
|
|
|
Allocator::deallocate(c_start, storage_size());
|
2014-05-03 22:57:43 +00:00
|
|
|
|
|
|
|
|
|
if (current_memory_tracker)
|
|
|
|
|
current_memory_tracker->free(storage_size());
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void realloc(size_t n)
|
|
|
|
|
{
|
2014-04-08 07:31:51 +00:00
|
|
|
|
if (c_start == nullptr)
|
2013-12-08 02:29:40 +00:00
|
|
|
|
{
|
|
|
|
|
alloc(n);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
ptrdiff_t end_diff = c_end - c_start;
|
|
|
|
|
size_t bytes_to_alloc = to_size(n);
|
|
|
|
|
|
|
|
|
|
char * old_c_start = c_start;
|
|
|
|
|
char * old_c_end_of_storage = c_end_of_storage;
|
|
|
|
|
|
2014-05-03 22:57:43 +00:00
|
|
|
|
if (current_memory_tracker)
|
|
|
|
|
current_memory_tracker->realloc(storage_size(), bytes_to_alloc);
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
if (use_libc_realloc)
|
|
|
|
|
{
|
|
|
|
|
c_start = reinterpret_cast<char *>(::realloc(c_start, bytes_to_alloc));
|
|
|
|
|
|
2014-04-08 07:47:51 +00:00
|
|
|
|
if (nullptr == c_start)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
throwFromErrno("PODArray: cannot realloc", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
c_start = reinterpret_cast<char *>(malloc(bytes_to_alloc));
|
|
|
|
|
|
2014-04-08 07:47:51 +00:00
|
|
|
|
if (nullptr == c_start)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
throwFromErrno("PODArray: cannot realloc", ErrorCodes::CANNOT_ALLOCATE_MEMORY);
|
|
|
|
|
|
2014-12-15 07:35:40 +00:00
|
|
|
|
memcpy(c_start, old_c_start, std::min(bytes_to_alloc, static_cast<size_t>(end_diff)));
|
2013-09-15 10:53:53 +00:00
|
|
|
|
Allocator::deallocate(old_c_start, old_c_end_of_storage - old_c_start);
|
|
|
|
|
}
|
2014-04-02 18:38:17 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + end_diff;
|
|
|
|
|
c_end_of_storage = c_start + bytes_to_alloc;
|
|
|
|
|
|
|
|
|
|
use_libc_realloc = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
typedef T value_type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Просто typedef нельзя, так как возникает неоднозначность для конструкторов и функций assign.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
struct iterator : public boost::iterator_adaptor<iterator, T*>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
iterator() {}
|
2014-12-19 18:33:30 +00:00
|
|
|
|
iterator(T * ptr_) : iterator::iterator_adaptor_(ptr_) {}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
struct const_iterator : public boost::iterator_adaptor<const_iterator, const T*>
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
const_iterator() {}
|
2013-12-08 02:29:40 +00:00
|
|
|
|
const_iterator(const T * ptr_) : const_iterator::iterator_adaptor_(ptr_) {}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
PODArray() : use_libc_realloc(false) { alloc(0); }
|
2013-09-15 10:53:53 +00:00
|
|
|
|
PODArray(size_t n) : use_libc_realloc(false) { alloc(n); c_end += byte_size(n); }
|
|
|
|
|
PODArray(size_t n, const T & x) : use_libc_realloc(false) { alloc(n); assign(n, x); }
|
|
|
|
|
PODArray(const_iterator from_begin, const_iterator from_end) : use_libc_realloc(false) { alloc(from_end - from_begin); insert(from_begin, from_end); }
|
|
|
|
|
~PODArray() { dealloc(); }
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
size_t size() const { return t_end() - t_start(); }
|
|
|
|
|
bool empty() const { return t_end() == t_start(); }
|
|
|
|
|
size_t capacity() const { return t_end_of_storage() - t_start(); }
|
|
|
|
|
|
|
|
|
|
T & operator[] (size_t n) { return t_start()[n]; }
|
|
|
|
|
const T & operator[] (size_t n) const { return t_start()[n]; }
|
|
|
|
|
|
|
|
|
|
T & front() { return t_start()[0]; }
|
|
|
|
|
T & back() { return t_end()[-1]; }
|
|
|
|
|
const T & front() const { return t_start()[0]; }
|
|
|
|
|
const T & back() const { return t_end()[-1]; }
|
|
|
|
|
|
|
|
|
|
iterator begin() { return t_start(); }
|
|
|
|
|
iterator end() { return t_end(); }
|
|
|
|
|
const_iterator begin() const { return t_start(); }
|
|
|
|
|
const_iterator end() const { return t_end(); }
|
|
|
|
|
|
|
|
|
|
void reserve(size_t n)
|
|
|
|
|
{
|
|
|
|
|
if (n > capacity())
|
|
|
|
|
realloc(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reserve()
|
|
|
|
|
{
|
2013-12-08 02:29:40 +00:00
|
|
|
|
if (size() == 0)
|
2015-01-22 01:13:13 +00:00
|
|
|
|
realloc(POD_ARRAY_INITIAL_SIZE);
|
2013-12-08 02:29:40 +00:00
|
|
|
|
else
|
|
|
|
|
realloc(size() * 2);
|
2013-09-15 10:53:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resize(size_t n)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
2014-08-28 11:59:41 +00:00
|
|
|
|
resize_assume_reserved(n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resize_assume_reserved(const size_t n)
|
|
|
|
|
{
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 10:01:04 +00:00
|
|
|
|
/// Как resize, но обнуляет новые элементы.
|
|
|
|
|
void resize_fill(size_t n)
|
|
|
|
|
{
|
|
|
|
|
size_t old_size = size();
|
|
|
|
|
if (n > old_size)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
|
|
|
|
memset(c_end, 0, n - old_size);
|
|
|
|
|
}
|
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-28 16:23:07 +00:00
|
|
|
|
void resize_fill(size_t n, const T & value)
|
|
|
|
|
{
|
|
|
|
|
size_t old_size = size();
|
|
|
|
|
if (n > old_size)
|
|
|
|
|
{
|
|
|
|
|
reserve(n);
|
|
|
|
|
std::fill(t_end(), reinterpret_cast<T *>(c_end + n - old_size), value);
|
|
|
|
|
}
|
|
|
|
|
c_end = c_start + byte_size(n);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 10:53:53 +00:00
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
c_end = c_start;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void push_back(const T & x)
|
|
|
|
|
{
|
|
|
|
|
if (unlikely(c_end == c_end_of_storage))
|
|
|
|
|
reserve();
|
|
|
|
|
|
|
|
|
|
*t_end() = x;
|
|
|
|
|
c_end += byte_size(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Не вставляйте в массив кусок самого себя. Потому что при ресайзе, итераторы на самого себя могут инвалидироваться.
|
2013-12-08 02:29:40 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void insert(It1 from_begin, It2 from_end)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
size_t required_capacity = size() + (from_end - from_begin);
|
|
|
|
|
if (required_capacity > capacity())
|
|
|
|
|
reserve(round_up_to_power_of_two(required_capacity));
|
|
|
|
|
|
2014-08-17 07:39:28 +00:00
|
|
|
|
insert_assume_reserved(from_begin, from_end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void insert_assume_reserved(It1 from_begin, It2 from_end)
|
|
|
|
|
{
|
2013-09-15 10:53:53 +00:00
|
|
|
|
size_t bytes_to_copy = byte_size(from_end - from_begin);
|
|
|
|
|
memcpy(c_end, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
|
|
|
|
|
c_end += bytes_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void swap(PODArray<T> & rhs)
|
|
|
|
|
{
|
|
|
|
|
std::swap(c_start, rhs.c_start);
|
|
|
|
|
std::swap(c_end, rhs.c_end);
|
|
|
|
|
std::swap(c_end_of_storage, rhs.c_end_of_storage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void assign(size_t n, const T & x)
|
|
|
|
|
{
|
|
|
|
|
resize(n);
|
|
|
|
|
std::fill(begin(), end(), x);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
template <typename It1, typename It2>
|
|
|
|
|
void assign(It1 from_begin, It2 from_end)
|
2013-09-15 10:53:53 +00:00
|
|
|
|
{
|
|
|
|
|
size_t required_capacity = from_end - from_begin;
|
|
|
|
|
if (required_capacity > capacity())
|
|
|
|
|
reserve(round_up_to_power_of_two(required_capacity));
|
|
|
|
|
|
|
|
|
|
size_t bytes_to_copy = byte_size(required_capacity);
|
2013-12-08 02:29:40 +00:00
|
|
|
|
memcpy(c_start, reinterpret_cast<const void *>(&*from_begin), bytes_to_copy);
|
2013-09-15 10:53:53 +00:00
|
|
|
|
c_end = c_start + bytes_to_copy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void assign(const PODArray<T> & from)
|
|
|
|
|
{
|
|
|
|
|
assign(from.begin(), from.end());
|
|
|
|
|
}
|
2013-12-08 02:29:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool operator== (const PODArray<T> & other) const
|
|
|
|
|
{
|
|
|
|
|
if (size() != other.size())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const_iterator this_it = begin();
|
|
|
|
|
const_iterator that_it = other.begin();
|
|
|
|
|
|
|
|
|
|
while (this_it != end())
|
|
|
|
|
{
|
|
|
|
|
if (*this_it != *that_it)
|
|
|
|
|
return false;
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
++this_it;
|
|
|
|
|
++that_it;
|
|
|
|
|
}
|
2014-07-06 04:22:12 +00:00
|
|
|
|
|
2013-12-08 02:29:40 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator!= (const PODArray<T> & other) const
|
|
|
|
|
{
|
|
|
|
|
return !operator==(other);
|
|
|
|
|
}
|
2013-09-15 10:53:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|