Blender V5.3
BLI_pool.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
15
16#pragma once
17
18#include "BLI_stack.hh"
19#include "BLI_utility_mixins.hh"
20#include "BLI_vector.hh"
21
22namespace blender {
23
24template<typename T, int64_t ChunkLen = 64> class Pool : NonCopyable {
25 private:
26 using Chunk = TypedBuffer<T, ChunkLen>;
27
35 Stack<T *, 0> free_list_;
36
37 public:
39 {
40 /* All elements need to be freed before freeing the pool. */
41 BLI_assert(this->size() == 0);
42 }
43
47 template<typename... ForwardT> T &construct(ForwardT &&...value)
48 {
49 if (free_list_.is_empty()) {
50 values_.append(std::make_unique<Chunk>());
51 T *chunk_start = values_.last()->ptr();
52 for (auto i : IndexRange(ChunkLen)) {
53 free_list_.push(chunk_start + i);
54 }
55 }
56 T *ptr = free_list_.pop();
57 new (ptr) T(std::forward<ForwardT>(value)...);
58 return *ptr;
59 }
60
65 void destruct(T &value)
66 {
67 value.~T();
68 free_list_.push(&value);
69 }
70
74 int64_t size() const
75 {
76 return values_.size() * ChunkLen - free_list_.size();
77 }
78
82 bool is_empty() const
83 {
84 return this->size() == 0;
85 }
86};
87
88} // namespace blender
constexpr IndexRange()=default
void destruct(T &value)
Definition BLI_pool.hh:65
int64_t size() const
Definition BLI_pool.hh:74
T & construct(ForwardT &&...value)
Definition BLI_pool.hh:47
bool is_empty() const
Definition BLI_pool.hh:82
NonCopyable(const NonCopyable &other)=delete
#define T
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90