Blender V5.3
BLI_memarena.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
16
17#include <cstdlib>
18#include <cstring>
19
20#include "MEM_guardedalloc.h"
21
22#include "BLI_asan.hh"
23#include "BLI_memarena.hh"
24#include "BLI_utildefines.hh"
25
26#ifdef WITH_MEM_VALGRIND
27# include "valgrind/memcheck.h"
28#else
29# define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) UNUSED_VARS(pool, rzB, is_zeroed)
30# define VALGRIND_DESTROY_MEMPOOL(pool) UNUSED_VARS(pool)
31# define VALGRIND_MEMPOOL_ALLOC(pool, addr, size) UNUSED_VARS(pool, addr, size)
32# define VALGRIND_MOVE_MEMPOOL(pool_a, pool_b) UNUSED_VARS(pool_a, pool_b)
33#endif
34
35#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
36
37namespace blender {
38
39struct MemBuf {
41#ifdef WITH_ASAN
42 size_t size;
43#endif
45};
46
47struct MemArena {
49 const char *name;
51
53 size_t align;
54
56};
57
59{
60 while (mb != nullptr) {
61 MemBuf *mb_next = mb->next;
62
63#ifdef WITH_ASAN
64 /* Unpoison memory because #MEM_delete_void might overwrite it. */
65 BLI_asan_unpoison(mb, uint(mb->size));
66#endif
67
68 MEM_delete(mb);
69 mb = mb_next;
70 }
71}
72
73MemArena *BLI_memarena_new(const size_t bufsize, const char *name)
74{
75 MemArena *ma = MEM_new_zeroed<MemArena>("memarena");
76 ma->bufsize = bufsize;
77 ma->align = 8;
78 ma->name = name;
79
80 VALGRIND_CREATE_MEMPOOL(ma, 0, false);
81
82 return ma;
83}
84
86{
87 ma->use_calloc = true;
88}
89
91{
92 ma->use_calloc = false;
93}
94
95void BLI_memarena_use_align(MemArena *ma, const size_t align)
96{
97 /* Align must be a power of two. */
98 BLI_assert((align & (align - 1)) == 0);
99
100 ma->align = align;
101}
102
104{
106
108
109 MEM_delete(ma);
110}
111
113#define PADUP(num, amt) (((num) + ((amt) - 1)) & ~((amt) - 1))
114
117{
118 uchar *tmp;
119
120 tmp = reinterpret_cast<uchar *> PADUP(intptr_t(ma->curbuf), int(ma->align));
121 ma->cursize -= size_t(tmp - ma->curbuf);
122 ma->curbuf = tmp;
123}
124
125void *BLI_memarena_alloc(MemArena *ma, size_t size)
126{
127 void *ptr;
128
129 /* Ensure proper alignment by rounding size up to multiple of 8. */
130 size = PADUP(size, ma->align);
131
132 if (size > ma->cursize) [[unlikely]] {
133 if (size > ma->bufsize - (ma->align - 1)) {
134 ma->cursize = PADUP(size + 1, ma->align);
135 }
136 else {
137 ma->cursize = ma->bufsize;
138 }
139
140 MemBuf *mb;
141 if (ma->use_calloc) {
142 mb = static_cast<MemBuf *>(MEM_new_zeroed(sizeof(*mb) + ma->cursize, ma->name));
143 }
144 else {
145 mb = static_cast<MemBuf *>(MEM_new_uninitialized(sizeof(*mb) + ma->cursize, ma->name));
146 }
147 ma->curbuf = mb->data;
148 mb->next = ma->bufs;
149#ifdef WITH_ASAN
150 mb->size = sizeof(*mb) + ma->cursize;
151#endif
152 ma->bufs = mb;
153
155
157 }
158
159 ptr = ma->curbuf;
160 ma->curbuf += size;
161 ma->cursize -= size;
162
163 VALGRIND_MEMPOOL_ALLOC(ma, ptr, size);
164
165 BLI_asan_unpoison(ptr, size);
166
167 return ptr;
168}
169
170void *BLI_memarena_calloc(MemArena *ma, size_t size)
171{
172 void *ptr;
173
174 /* No need to use this function call if we're calloc'ing by default. */
175 BLI_assert(ma->use_calloc == false);
176
177 ptr = BLI_memarena_alloc(ma, size);
178 BLI_assert(ptr != nullptr);
179 memset(ptr, 0, size);
180
181 return ptr;
182}
183
185{
186 /* Memory arenas must be compatible. */
187 BLI_assert(ma_dst != ma_src);
188 BLI_assert(ma_dst->align == ma_src->align);
189 BLI_assert(ma_dst->use_calloc == ma_src->use_calloc);
190 BLI_assert(ma_dst->bufsize == ma_src->bufsize);
191
192 if (ma_src->bufs == nullptr) {
193 return;
194 }
195
196 if (ma_dst->bufs == nullptr) [[unlikely]] {
197 BLI_assert(ma_dst->curbuf == nullptr);
198 ma_dst->bufs = ma_src->bufs;
199 ma_dst->curbuf = ma_src->curbuf;
200 ma_dst->cursize = ma_src->cursize;
201 }
202 else {
203 /* Keep the 'ma_dst->curbuf' for simplicity.
204 * Insert buffers after the first. */
205 if (ma_dst->bufs->next != nullptr) {
206 /* Loop over `ma_src` instead of `ma_dst` since it's likely the destination is larger
207 * when used for accumulating from multiple sources. */
208 MemBuf *mb_src = ma_src->bufs;
209 while (mb_src->next) {
210 mb_src = mb_src->next;
211 }
212 mb_src->next = ma_dst->bufs->next;
213 }
214 ma_dst->bufs->next = ma_src->bufs;
215 }
216
217 ma_src->bufs = nullptr;
218 ma_src->curbuf = nullptr;
219 ma_src->cursize = 0;
220
221 VALGRIND_MOVE_MEMPOOL(ma_src, ma_dst);
222 VALGRIND_CREATE_MEMPOOL(ma_src, 0, false);
223}
224
226{
227 if (ma->bufs) {
228 if (ma->bufs->next) {
230 ma->bufs->next = nullptr;
231 }
232
233 const uchar *curbuf_prev = ma->curbuf;
234 ma->curbuf = ma->bufs->data;
236
237 /* restore to original size */
238 const size_t curbuf_used = size_t(curbuf_prev - ma->curbuf);
239 ma->cursize += curbuf_used;
240
241 if (ma->use_calloc) {
242 memset(ma->curbuf, 0, curbuf_used);
243 }
245 }
246
248 VALGRIND_CREATE_MEMPOOL(ma, 0, false);
249}
250
251} // namespace blender
#define BLI_asan_unpoison(addr, size)
Definition BLI_asan.hh:36
#define BLI_asan_poison(addr, size)
Definition BLI_asan.hh:31
#define PADUP(num, amt)
#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)
#define VALGRIND_MOVE_MEMPOOL(pool_a, pool_b)
#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)
#define VALGRIND_DESTROY_MEMPOOL(pool)
Read Blender memory allocation and freeing.
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void memarena_curbuf_align(MemArena *ma)
void BLI_memarena_merge(MemArena *ma_dst, MemArena *ma_src)
void BLI_memarena_use_calloc(MemArena *ma) ATTR_NONNULL(1)
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
unsigned int uint
Definition BKE_text.h:55
void BLI_memarena_use_align(MemArena *ma, size_t align) ATTR_NONNULL(1)
void BLI_memarena_use_malloc(MemArena *ma) ATTR_NONNULL(1)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
unsigned char uchar
Definition BKE_text.h:58
void BLI_memarena_clear(MemArena *ma) ATTR_NONNULL(1)
static void memarena_buf_free_all(MemBuf *mb)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void * BLI_memarena_calloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
_W64 int intptr_t
Definition BKE_text.h:119
const char * name