memgraph/include/utils/memory/block_allocator.hpp

52 lines
914 B
C++
Raw Normal View History

#pragma once
2015-10-09 07:24:12 +08:00
#include <memory>
#include <vector>
#include "utils/auto_scope.hpp"
template <size_t block_size>
class BlockAllocator
{
struct Block
{
2016-09-16 03:19:31 +08:00
Block() { data = malloc(block_size); }
2015-10-09 07:24:12 +08:00
2016-09-16 03:19:31 +08:00
Block(void *ptr) { data = ptr; }
2015-10-09 07:24:12 +08:00
2016-09-16 03:19:31 +08:00
void *data;
2015-10-09 07:24:12 +08:00
};
public:
static constexpr size_t size = block_size;
BlockAllocator(size_t capacity = 0)
{
2016-09-16 03:19:31 +08:00
for (size_t i = 0; i < capacity; ++i)
2015-10-09 07:24:12 +08:00
blocks.emplace_back();
}
2016-09-16 03:19:31 +08:00
~BlockAllocator()
2015-10-09 07:24:12 +08:00
{
2016-09-16 03:19:31 +08:00
for (auto b : blocks) {
free(b.data);
}
blocks.clear();
2015-10-09 07:24:12 +08:00
}
2016-09-16 03:19:31 +08:00
// Returns nullptr on no memory.
void *acquire()
2015-10-09 07:24:12 +08:00
{
2016-09-16 03:19:31 +08:00
if (blocks.size() == 0) blocks.emplace_back();
auto ptr = blocks.back().data;
Auto(blocks.pop_back());
return ptr;
2015-10-09 07:24:12 +08:00
}
2016-09-16 03:19:31 +08:00
void release(void *ptr) { blocks.emplace_back(ptr); }
2015-10-09 07:24:12 +08:00
private:
std::vector<Block> blocks;
};