ClickHouse/src/Common/SmallObjectPool.h

68 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
#include <Common/Arena.h>
2020-11-06 17:34:14 +00:00
#include <common/unaligned.h>
namespace DB
{
/** Can allocate memory objects of fixed size with deletion support.
2020-11-06 17:34:14 +00:00
* For small `object_size`s allocated no less than pointer size.
*/
class SmallObjectPool
{
private:
ColumnConst unification (#1011) * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * Fixed error in ColumnArray::replicateGeneric [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150]. * ColumnConst: unification (incomplete) [#CLICKHOUSE-3150].
2017-07-21 06:35:58 +00:00
const size_t object_size;
Arena pool;
2020-11-06 17:34:14 +00:00
char * free_list{};
public:
2020-11-06 17:34:14 +00:00
SmallObjectPool(size_t object_size_)
: object_size{std::max(object_size_, sizeof(char *))}
{
if (pool.size() < object_size)
return;
2020-11-06 17:34:14 +00:00
const size_t num_objects = pool.size() / object_size;
free_list = pool.alloc(num_objects * object_size);
char * head = free_list;
2020-11-06 17:34:14 +00:00
for (size_t i = 0; i < num_objects - 1; ++i)
{
2020-11-06 17:34:14 +00:00
char * next = head + object_size;
unalignedStore<char *>(head, next);
head = next;
}
2020-11-06 17:34:14 +00:00
unalignedStore<char *>(head, nullptr);
}
char * alloc()
{
if (free_list)
{
2020-11-06 17:34:14 +00:00
char * res = free_list;
free_list = unalignedLoad<char *>(free_list);
return res;
}
return pool.alloc(object_size);
}
2020-11-06 17:34:14 +00:00
void free(char * ptr)
{
2020-11-06 17:34:14 +00:00
unalignedStore<char *>(ptr, free_list);
free_list = ptr;
}
2017-05-07 20:25:26 +00:00
/// The size of the allocated pool in bytes
size_t size() const
{
return pool.size();
}
};
}