Blender V4.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
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:
27
33 Stack<T *, 0> free_list_;
34
35 public:
37 {
38 /* All elements need to be freed before freeing the pool. */
39 BLI_assert(this->size() == 0);
40 }
41
45 template<typename... ForwardT> T &construct(ForwardT &&...value)
46 {
47 if (free_list_.is_empty()) {
48 values_.append(std::make_unique<Chunk>());
49 T *chunk_start = values_.last()->ptr();
50 for (auto i : IndexRange(ChunkLen)) {
51 free_list_.push(chunk_start + i);
52 }
53 }
54 T *ptr = free_list_.pop();
55 new (ptr) T(std::forward<ForwardT>(value)...);
56 return *ptr;
57 }
58
63 void destruct(T &value)
64 {
65 value.~T();
66 free_list_.push(&value);
67 }
68
72 int64_t size() const
73 {
74 return values_.size() * ChunkLen - free_list_.size();
75 }
76
80 bool is_empty() const
81 {
82 return this->size() == 0;
83 }
84};
85
86} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:50
void destruct(T &value)
Definition BLI_pool.hh:63
int64_t size() const
Definition BLI_pool.hh:72
T & construct(ForwardT &&...value)
Definition BLI_pool.hh:45
bool is_empty() const
Definition BLI_pool.hh:80
bool is_empty() const
Definition BLI_stack.hh:308
void push(const T &value)
Definition BLI_stack.hh:213
int64_t size() const
Definition BLI_stack.hh:316
int64_t size() const
void append(const T &value)
const T & last(const int64_t n=0) const
#define T
__int64 int64_t
Definition stdint.h:89
PointerRNA * ptr
Definition wm_files.cc:4126