-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgrid_allocator.hpp
161 lines (134 loc) · 4.49 KB
/
grid_allocator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright Contributors to the Bonxai Project
* Copyright Contributors to the OpenVDB Project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cassert>
#include <memory>
#include <mutex>
#include <stdexcept>
#include "mask.hpp"
namespace Bonxai {
/**
* @brief The GridBlockAllocator is used to pre-allocate the meory of multiple Grids
* in "chunks". It is a very simple memory pool.
*
* Each chunk allocates memory for 512 Grids
*/
template <typename DataT>
class GridBlockAllocator {
public:
// use log2dim of the block to be allocated
GridBlockAllocator(size_t log2dim);
GridBlockAllocator(const GridBlockAllocator&) = delete;
GridBlockAllocator(GridBlockAllocator&&) = default;
GridBlockAllocator& operator=(const GridBlockAllocator& other) = delete;
GridBlockAllocator& operator=(GridBlockAllocator&& other) = default;
using Deleter = std::function<void()>;
std::pair<DataT*, Deleter> allocateBlock();
void clear() {
chunks_.clear();
size_ = 0;
capacity_ = 0;
}
void releaseUnusedMemory();
size_t capacity() const {
return capacity_;
}
size_t size() const {
return size_;
}
size_t memUsage() const;
// specific size to use for Mask(3)
static constexpr size_t blocks_per_chunk = 512;
protected:
size_t log2dim_ = 0;
size_t block_bytes_ = 0;
size_t capacity_ = 0;
size_t size_ = 0;
struct Chunk {
Chunk()
: mask(3, true) {}
Mask mask;
std::vector<char> data;
};
std::vector<std::shared_ptr<Chunk>> chunks_;
std::unique_ptr<std::mutex> mutex_;
void addNewChunk();
Deleter createDeleter(std::shared_ptr<Chunk> chunk, uint32_t index);
};
//----------------------------------------------------
//----------------- Implementations ------------------
//----------------------------------------------------
template <typename DataT>
inline GridBlockAllocator<DataT>::GridBlockAllocator(size_t log2dim)
: log2dim_(log2dim),
block_bytes_(std::pow((1 << log2dim), 3) * sizeof(DataT)),
mutex_(new std::mutex) {}
template <typename DataT>
inline std::pair<DataT*, typename GridBlockAllocator<DataT>::Deleter>
GridBlockAllocator<DataT>::allocateBlock() {
std::unique_lock lock(*mutex_);
if (size_ >= capacity_) {
// Need more memory. Create a new chunk
addNewChunk();
// first index of new chunk is available
std::shared_ptr<Chunk> chunk = chunks_.back();
DataT* ptr = reinterpret_cast<DataT*>(chunk->data.data());
chunk->mask.setOff(0);
size_++;
return {ptr, createDeleter(chunk, 0)};
}
// There must be available memory, somewhere. Search in reverse order
for (auto it = chunks_.rbegin(); it != chunks_.rend(); it++) {
std::shared_ptr<Chunk>& chunk = (*it);
auto mask_index = chunk->mask.findFirstOn();
if (mask_index < chunk->mask.size()) {
// found in this chunk
uint32_t data_index = block_bytes_ * mask_index;
DataT* ptr = reinterpret_cast<DataT*>(&chunk->data[data_index]);
chunk->mask.setOff(mask_index);
size_++;
return {ptr, createDeleter(chunk, mask_index)};
}
}
throw std::logic_error("Unexpected end of GridBlockAllocator::allocateBlock");
}
template <typename DataT>
inline void GridBlockAllocator<DataT>::releaseUnusedMemory() {
std::unique_lock lock(*mutex_);
int to_be_erased_count = 0;
auto remove_if = std::remove_if(chunks_.begin(), chunks_.end(), [&](const auto& chunk) -> bool {
bool notUsed = chunk->mask.isOn();
to_be_erased_count += (notUsed) ? 1 : 0;
return notUsed;
});
chunks_.erase(remove_if, chunks_.end());
capacity_ -= to_be_erased_count * blocks_per_chunk;
}
template <typename DataT>
inline size_t GridBlockAllocator<DataT>::memUsage() const {
return chunks_.size() * (sizeof(Chunk) + block_bytes_ * blocks_per_chunk);
}
template <typename DataT>
inline void GridBlockAllocator<DataT>::addNewChunk() {
auto chunk = std::make_shared<Chunk>();
chunk->data.resize(blocks_per_chunk * block_bytes_);
chunks_.push_back(chunk);
capacity_ += blocks_per_chunk;
}
template <typename DataT>
inline typename GridBlockAllocator<DataT>::Deleter GridBlockAllocator<DataT>::createDeleter(
std::shared_ptr<Chunk> chunk, uint32_t index) {
return [this, index, chunk] {
assert(index < blocks_per_chunk);
std::unique_lock lock(*mutex_);
chunk->mask.setOn(index);
size_--;
};
}
} // namespace Bonxai