2015-11-17 16:08:31 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
#include <DB/Common/Arena.h>
|
2015-11-24 12:47:51 +00:00
|
|
|
#include <ext/bit_cast.hpp>
|
2015-11-19 15:06:00 +00:00
|
|
|
#include <ext/size.hpp>
|
2015-11-17 16:08:31 +00:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
2015-11-19 15:06:00 +00:00
|
|
|
#include <array>
|
2015-11-17 16:08:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
class ArenaWithFreeLists : private Allocator<false>
|
2015-11-17 16:08:31 +00:00
|
|
|
{
|
|
|
|
private:
|
2015-11-19 15:06:00 +00:00
|
|
|
struct Block { Block * next; };
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
static const std::array<std::size_t, 14> & getSizes()
|
2015-11-17 16:08:31 +00:00
|
|
|
{
|
2015-11-19 15:06:00 +00:00
|
|
|
static constexpr std::array<std::size_t, 14> sizes{
|
|
|
|
8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536
|
2015-11-19 13:41:35 +00:00
|
|
|
};
|
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
static_assert(sizes.front() >= sizeof(Block), "Can't make allocations smaller than sizeof(Block)");
|
2015-11-19 13:41:35 +00:00
|
|
|
|
|
|
|
return sizes;
|
|
|
|
}
|
|
|
|
|
2015-11-23 19:04:15 +00:00
|
|
|
static auto sizeToPreviousPowerOfTwo(const int size) { return _bit_scan_reverse(size - 1); }
|
|
|
|
|
|
|
|
static auto getMinBucketNum()
|
|
|
|
{
|
|
|
|
static const auto val = sizeToPreviousPowerOfTwo(getSizes().front());
|
|
|
|
return val;
|
|
|
|
}
|
2015-11-19 15:06:00 +00:00
|
|
|
static auto getMaxFixedBlockSize() { return getSizes().back(); }
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
Arena pool;
|
|
|
|
const std::unique_ptr<Block * []> free_lists = std::make_unique<Block * []>(ext::size(getSizes()));
|
2015-11-17 16:08:31 +00:00
|
|
|
|
|
|
|
static std::size_t findFreeListIndex(const std::size_t size)
|
|
|
|
{
|
|
|
|
/// shift powers of two into previous bucket by subtracting 1
|
2015-11-23 19:04:15 +00:00
|
|
|
const auto bucket_num = sizeToPreviousPowerOfTwo(size);
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 13:41:35 +00:00
|
|
|
return std::max(bucket_num, getMinBucketNum()) - getMinBucketNum();
|
2015-11-17 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ArenaWithFreeLists(
|
|
|
|
const std::size_t initial_size = 4096, const std::size_t growth_factor = 2,
|
|
|
|
const std::size_t linear_growth_threshold = 128 * 1024 * 1024)
|
2015-11-19 15:06:00 +00:00
|
|
|
: pool{initial_size, growth_factor, linear_growth_threshold}
|
2015-11-17 16:08:31 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
char * alloc(const std::size_t size)
|
|
|
|
{
|
2015-11-19 15:06:00 +00:00
|
|
|
if (size > getMaxFixedBlockSize())
|
|
|
|
return static_cast<char *>(Allocator::alloc(size));
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
/// find list of required size
|
|
|
|
const auto list_idx = findFreeListIndex(size);
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
if (auto & block = free_lists[list_idx])
|
|
|
|
{
|
|
|
|
const auto res = ext::bit_cast<char *>(block);
|
|
|
|
block = block->next;
|
|
|
|
return res;
|
|
|
|
}
|
2015-11-17 16:08:31 +00:00
|
|
|
|
2015-11-19 15:06:00 +00:00
|
|
|
/// no block of corresponding size, allocate a new one
|
|
|
|
return pool.alloc(getSizes()[list_idx]);
|
2015-11-17 16:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free(const void * ptr, const std::size_t size)
|
|
|
|
{
|
2015-11-19 15:06:00 +00:00
|
|
|
if (size > getMaxFixedBlockSize())
|
|
|
|
return Allocator::free(const_cast<void *>(ptr), size);
|
|
|
|
|
|
|
|
/// find list of required size
|
|
|
|
const auto list_idx = findFreeListIndex(size);
|
|
|
|
|
|
|
|
auto & block = free_lists[list_idx];
|
|
|
|
const auto old = block;
|
|
|
|
block = ext::bit_cast<Block *>(ptr);
|
|
|
|
block->next = old;
|
2015-11-17 16:08:31 +00:00
|
|
|
}
|
2015-11-18 11:53:01 +00:00
|
|
|
|
|
|
|
/// Размер выделенного пула в байтах
|
|
|
|
size_t size() const
|
|
|
|
{
|
2015-11-19 15:06:00 +00:00
|
|
|
return pool.size();
|
2015-11-18 11:53:01 +00:00
|
|
|
}
|
2015-11-17 16:08:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|