ClickHouse/src/Common/SmallObjectPool.h

53 lines
936 B
C++
Raw Normal View History

#pragma once
#include <Common/Arena.h>
2021-10-02 07:13:14 +00:00
#include <base/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:46:43 +00:00
char * free_list = nullptr;
public:
explicit SmallObjectPool(size_t object_size_)
2020-11-06 17:34:14 +00:00
: object_size{std::max(object_size_, sizeof(char *))}
{
}
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();
}
};
}