2012-05-31 04:49:55 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string.h>
|
2013-02-08 19:34:44 +00:00
|
|
|
#include <memory>
|
2013-02-09 00:12:04 +00:00
|
|
|
#include <vector>
|
2015-12-27 10:58:20 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/likely.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Defines.h>
|
2018-09-02 21:28:25 +00:00
|
|
|
#include <Common/memcpySmall.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
#include <Common/Allocator.h>
|
2012-05-31 04:49:55 +00:00
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
namespace ProfileEvents
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const Event ArenaAllocChunks;
|
|
|
|
extern const Event ArenaAllocBytes;
|
2016-10-24 02:02:37 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 04:49:55 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
/** Memory pool to append something. For example, short strings.
|
|
|
|
* Usage scenario:
|
|
|
|
* - put lot of strings inside pool, keep their addresses;
|
|
|
|
* - addresses remain valid during lifetime of pool;
|
|
|
|
* - at destruction of pool, all memory is freed;
|
|
|
|
* - memory is allocated and freed by large chunks;
|
|
|
|
* - freeing parts of data is not possible (but look at ArenaWithFreeLists if you need);
|
2012-05-31 04:49:55 +00:00
|
|
|
*/
|
2015-12-27 10:58:20 +00:00
|
|
|
class Arena : private boost::noncopyable
|
2012-05-31 04:49:55 +00:00
|
|
|
{
|
|
|
|
private:
|
2018-09-02 21:28:25 +00:00
|
|
|
/// Padding allows to use 'memcpySmallAllowReadWriteOverflow15' instead of 'memcpy'.
|
|
|
|
static constexpr size_t pad_right = 15;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Contiguous chunk of memory and pointer to free space inside it. Member of single-linked list.
|
2018-12-25 23:39:40 +00:00
|
|
|
struct alignas(16) Chunk : private Allocator<false> /// empty base optimization
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
char * begin;
|
|
|
|
char * pos;
|
2018-09-02 21:28:25 +00:00
|
|
|
char * end; /// does not include padding.
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
Chunk * prev;
|
|
|
|
|
|
|
|
Chunk(size_t size_, Chunk * prev_)
|
|
|
|
{
|
|
|
|
ProfileEvents::increment(ProfileEvents::ArenaAllocChunks);
|
|
|
|
ProfileEvents::increment(ProfileEvents::ArenaAllocBytes, size_);
|
|
|
|
|
|
|
|
begin = reinterpret_cast<char *>(Allocator::alloc(size_));
|
|
|
|
pos = begin;
|
2018-09-02 21:28:25 +00:00
|
|
|
end = begin + size_ - pad_right;
|
2017-04-01 07:20:54 +00:00
|
|
|
prev = prev_;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Chunk()
|
|
|
|
{
|
|
|
|
Allocator::free(begin, size());
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
delete prev;
|
|
|
|
}
|
|
|
|
|
2018-09-02 21:28:25 +00:00
|
|
|
size_t size() const { return end + pad_right - begin; }
|
2017-12-06 03:10:32 +00:00
|
|
|
size_t remaining() const { return end - pos; }
|
2017-04-01 07:20:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t growth_factor;
|
|
|
|
size_t linear_growth_threshold;
|
|
|
|
|
|
|
|
/// Last contiguous chunk of memory.
|
|
|
|
Chunk * head;
|
|
|
|
size_t size_in_bytes;
|
|
|
|
|
|
|
|
static size_t roundUpToPageSize(size_t s)
|
|
|
|
{
|
|
|
|
return (s + 4096 - 1) / 4096 * 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If chunks size is less than 'linear_growth_threshold', then use exponential growth, otherwise - linear growth
|
|
|
|
/// (to not allocate too much excessive memory).
|
|
|
|
size_t nextSize(size_t min_next_size) const
|
|
|
|
{
|
|
|
|
size_t size_after_grow = 0;
|
|
|
|
|
|
|
|
if (head->size() < linear_growth_threshold)
|
|
|
|
size_after_grow = head->size() * growth_factor;
|
|
|
|
else
|
|
|
|
size_after_grow = linear_growth_threshold;
|
|
|
|
|
|
|
|
if (size_after_grow < min_next_size)
|
|
|
|
size_after_grow = min_next_size;
|
|
|
|
|
|
|
|
return roundUpToPageSize(size_after_grow);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add next contiguous chunk of memory with size not less than specified.
|
|
|
|
void NO_INLINE addChunk(size_t min_size)
|
|
|
|
{
|
2018-09-02 21:28:25 +00:00
|
|
|
head = new Chunk(nextSize(min_size + pad_right), head);
|
2017-04-01 07:20:54 +00:00
|
|
|
size_in_bytes += head->size();
|
|
|
|
}
|
2012-05-31 04:49:55 +00:00
|
|
|
|
2017-06-26 12:16:29 +00:00
|
|
|
friend class ArenaAllocator;
|
2018-09-02 05:23:20 +00:00
|
|
|
template <size_t> friend class AlignedArenaAllocator;
|
2017-06-26 12:16:29 +00:00
|
|
|
|
2012-05-31 04:49:55 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
Arena(size_t initial_size_ = 4096, size_t growth_factor_ = 2, size_t linear_growth_threshold_ = 128 * 1024 * 1024)
|
|
|
|
: growth_factor(growth_factor_), linear_growth_threshold(linear_growth_threshold_),
|
|
|
|
head(new Chunk(initial_size_, nullptr)), size_in_bytes(head->size())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Arena()
|
|
|
|
{
|
|
|
|
delete head;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get piece of memory, without alignment.
|
|
|
|
char * alloc(size_t size)
|
|
|
|
{
|
|
|
|
if (unlikely(head->pos + size > head->end))
|
|
|
|
addChunk(size);
|
|
|
|
|
|
|
|
char * res = head->pos;
|
|
|
|
head->pos += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-01 03:17:43 +00:00
|
|
|
/// Get peice of memory with alignment
|
|
|
|
char * alignedAlloc(size_t size, size_t alignment)
|
2018-08-05 08:45:15 +00:00
|
|
|
{
|
2018-09-01 03:17:43 +00:00
|
|
|
do
|
2018-08-05 08:45:15 +00:00
|
|
|
{
|
2018-09-01 03:17:43 +00:00
|
|
|
void * head_pos = head->pos;
|
|
|
|
size_t space = head->end - head->pos;
|
|
|
|
|
|
|
|
auto res = static_cast<char *>(std::align(alignment, size, head_pos, space));
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
head->pos = static_cast<char *>(head_pos);
|
|
|
|
head->pos += size;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
addChunk(size + alignment);
|
|
|
|
} while (true);
|
2018-08-05 08:45:15 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 23:27:52 +00:00
|
|
|
template <typename T>
|
|
|
|
T * alloc()
|
|
|
|
{
|
|
|
|
return reinterpret_cast<T *>(alignedAlloc(sizeof(T), alignof(T)));
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Rollback just performed allocation.
|
|
|
|
* Must pass size not more that was just allocated.
|
|
|
|
*/
|
|
|
|
void rollback(size_t size)
|
|
|
|
{
|
|
|
|
head->pos -= size;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:25:03 +00:00
|
|
|
/** Begin or expand allocation of contiguous piece of memory without alignment.
|
2017-04-01 07:20:54 +00:00
|
|
|
* 'begin' - current begin of piece of memory, if it need to be expanded, or nullptr, if it need to be started.
|
|
|
|
* If there is no space in chunk to expand current piece of memory - then copy all piece to new chunk and change value of 'begin'.
|
2017-04-16 03:38:37 +00:00
|
|
|
* NOTE This method is usable only for latest allocation. For earlier allocations, see 'realloc' method.
|
2017-04-01 07:20:54 +00:00
|
|
|
*/
|
|
|
|
char * allocContinue(size_t size, char const *& begin)
|
|
|
|
{
|
|
|
|
while (unlikely(head->pos + size > head->end))
|
|
|
|
{
|
|
|
|
char * prev_end = head->pos;
|
|
|
|
addChunk(size);
|
|
|
|
|
|
|
|
if (begin)
|
|
|
|
begin = insert(begin, prev_end - begin);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * res = head->pos;
|
|
|
|
head->pos += size;
|
|
|
|
|
|
|
|
if (!begin)
|
|
|
|
begin = res;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-02 04:58:55 +00:00
|
|
|
char * alignedAllocContinue(size_t size, char const *& begin, size_t alignment)
|
|
|
|
{
|
|
|
|
char * res;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
void * head_pos = head->pos;
|
|
|
|
size_t space = head->end - head->pos;
|
|
|
|
|
|
|
|
res = static_cast<char *>(std::align(alignment, size, head_pos, space));
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
head->pos = static_cast<char *>(head_pos);
|
|
|
|
head->pos += size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
char * prev_end = head->pos;
|
|
|
|
addChunk(size + alignment);
|
|
|
|
|
|
|
|
if (begin)
|
|
|
|
begin = alignedInsert(begin, prev_end - begin, alignment);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while (true);
|
|
|
|
|
|
|
|
if (!begin)
|
|
|
|
begin = res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-16 03:38:37 +00:00
|
|
|
/// NOTE Old memory region is wasted.
|
|
|
|
char * realloc(const char * old_data, size_t old_size, size_t new_size)
|
|
|
|
{
|
|
|
|
char * res = alloc(new_size);
|
|
|
|
if (old_data)
|
2018-09-06 02:30:58 +00:00
|
|
|
memcpy(res, old_data, old_size);
|
2017-04-16 03:38:37 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-01 04:25:03 +00:00
|
|
|
char * alignedRealloc(const char * old_data, size_t old_size, size_t new_size, size_t alignment)
|
|
|
|
{
|
|
|
|
char * res = alignedAlloc(new_size, alignment);
|
|
|
|
if (old_data)
|
2018-09-06 02:30:58 +00:00
|
|
|
memcpy(res, old_data, old_size);
|
2018-09-01 04:25:03 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Insert string without alignment.
|
|
|
|
const char * insert(const char * data, size_t size)
|
|
|
|
{
|
|
|
|
char * res = alloc(size);
|
2018-09-06 02:30:58 +00:00
|
|
|
memcpy(res, data, size);
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-09-02 04:58:55 +00:00
|
|
|
const char * alignedInsert(const char * data, size_t size, size_t alignment)
|
|
|
|
{
|
|
|
|
char * res = alignedAlloc(size, alignment);
|
2018-09-06 02:30:58 +00:00
|
|
|
memcpy(res, data, size);
|
2018-09-02 04:58:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Size of chunks in bytes.
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return size_in_bytes;
|
|
|
|
}
|
2017-12-05 21:25:01 +00:00
|
|
|
|
2017-12-06 03:10:32 +00:00
|
|
|
size_t remainingSpaceInCurrentChunk() const
|
2017-12-05 21:25:01 +00:00
|
|
|
{
|
2017-12-06 03:10:32 +00:00
|
|
|
return head->remaining();
|
2017-12-05 21:25:01 +00:00
|
|
|
}
|
2012-05-31 04:49:55 +00:00
|
|
|
};
|
|
|
|
|
2016-05-28 10:15:36 +00:00
|
|
|
using ArenaPtr = std::shared_ptr<Arena>;
|
|
|
|
using Arenas = std::vector<ArenaPtr>;
|
2013-02-09 00:12:04 +00:00
|
|
|
|
2012-05-31 04:49:55 +00:00
|
|
|
|
|
|
|
}
|