Blender V4.3
BLI_memarena.c
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
17#include <stdlib.h>
18#include <string.h>
19
20#include "MEM_guardedalloc.h"
21
22#include "BLI_asan.h"
23#include "BLI_memarena.h"
24#include "BLI_utildefines.h"
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.h" /* Keep last. */
36
37struct MemBuf {
38 struct MemBuf *next;
39 uchar data[0];
40};
41
42struct MemArena {
44 const char *name;
45 struct MemBuf *bufs;
46
48 size_t align;
49
51};
52
53static void memarena_buf_free_all(struct MemBuf *mb)
54{
55 while (mb != NULL) {
56 struct MemBuf *mb_next = mb->next;
57
58 /* Unpoison memory because #MEM_freeN might overwrite it. */
60
61 MEM_freeN(mb);
62 mb = mb_next;
63 }
64}
65
66MemArena *BLI_memarena_new(const size_t bufsize, const char *name)
67{
68 MemArena *ma = MEM_callocN(sizeof(*ma), "memarena");
69 ma->bufsize = bufsize;
70 ma->align = 8;
71 ma->name = name;
72
73 VALGRIND_CREATE_MEMPOOL(ma, 0, false);
74
75 return ma;
76}
77
79{
80 ma->use_calloc = 1;
81}
82
84{
85 ma->use_calloc = 0;
86}
87
88void BLI_memarena_use_align(MemArena *ma, const size_t align)
89{
90 /* Align must be a power of two. */
91 BLI_assert((align & (align - 1)) == 0);
92
93 ma->align = align;
94}
95
97{
99
101
102 MEM_freeN(ma);
103}
104
106#define PADUP(num, amt) (((num) + ((amt)-1)) & ~((amt)-1))
107
110{
111 uchar *tmp;
112
113 tmp = (uchar *)PADUP((intptr_t)ma->curbuf, (int)ma->align);
114 ma->cursize -= (size_t)(tmp - ma->curbuf);
115 ma->curbuf = tmp;
116}
117
118void *BLI_memarena_alloc(MemArena *ma, size_t size)
119{
120 void *ptr;
121
122 /* Ensure proper alignment by rounding size up to multiple of 8. */
123 size = PADUP(size, ma->align);
124
125 if (UNLIKELY(size > ma->cursize)) {
126 if (size > ma->bufsize - (ma->align - 1)) {
127 ma->cursize = PADUP(size + 1, ma->align);
128 }
129 else {
130 ma->cursize = ma->bufsize;
131 }
132
133 struct MemBuf *mb = (ma->use_calloc ? MEM_callocN : MEM_mallocN)(sizeof(*mb) + ma->cursize,
134 ma->name);
135 ma->curbuf = mb->data;
136 mb->next = ma->bufs;
137 ma->bufs = mb;
138
140
142 }
143
144 ptr = ma->curbuf;
145 ma->curbuf += size;
146 ma->cursize -= size;
147
148 VALGRIND_MEMPOOL_ALLOC(ma, ptr, size);
149
150 BLI_asan_unpoison(ptr, size);
151
152 return ptr;
153}
154
155void *BLI_memarena_calloc(MemArena *ma, size_t size)
156{
157 void *ptr;
158
159 /* No need to use this function call if we're calloc'ing by default. */
160 BLI_assert(ma->use_calloc == false);
161
162 ptr = BLI_memarena_alloc(ma, size);
163 BLI_assert(ptr != NULL);
164 memset(ptr, 0, size);
165
166 return ptr;
167}
168
170{
171 /* Memory arenas must be compatible. */
172 BLI_assert(ma_dst != ma_src);
173 BLI_assert(ma_dst->align == ma_src->align);
174 BLI_assert(ma_dst->use_calloc == ma_src->use_calloc);
175 BLI_assert(ma_dst->bufsize == ma_src->bufsize);
176
177 if (ma_src->bufs == NULL) {
178 return;
179 }
180
181 if (UNLIKELY(ma_dst->bufs == NULL)) {
182 BLI_assert(ma_dst->curbuf == NULL);
183 ma_dst->bufs = ma_src->bufs;
184 ma_dst->curbuf = ma_src->curbuf;
185 ma_dst->cursize = ma_src->cursize;
186 }
187 else {
188 /* Keep the 'ma_dst->curbuf' for simplicity.
189 * Insert buffers after the first. */
190 if (ma_dst->bufs->next != NULL) {
191 /* Loop over `ma_src` instead of `ma_dst` since it's likely the destination is larger
192 * when used for accumulating from multiple sources. */
193 struct MemBuf *mb_src = ma_src->bufs;
194 while (mb_src->next) {
195 mb_src = mb_src->next;
196 }
197 mb_src->next = ma_dst->bufs->next;
198 }
199 ma_dst->bufs->next = ma_src->bufs;
200 }
201
202 ma_src->bufs = NULL;
203 ma_src->curbuf = NULL;
204 ma_src->cursize = 0;
205
206 VALGRIND_MOVE_MEMPOOL(ma_src, ma_dst);
207 VALGRIND_CREATE_MEMPOOL(ma_src, 0, false);
208}
209
211{
212 if (ma->bufs) {
213 if (ma->bufs->next) {
215 ma->bufs->next = NULL;
216 }
217
218 const uchar *curbuf_prev = ma->curbuf;
219 ma->curbuf = ma->bufs->data;
221
222 /* restore to original size */
223 const size_t curbuf_used = (size_t)(curbuf_prev - ma->curbuf);
224 ma->cursize += curbuf_used;
225
226 if (ma->use_calloc) {
227 memset(ma->curbuf, 0, curbuf_used);
228 }
230 }
231
233 VALGRIND_CREATE_MEMPOOL(ma, 0, false);
234}
#define BLI_asan_unpoison(addr, size)
Definition BLI_asan.h:32
#define BLI_asan_poison(addr, size)
Definition BLI_asan.h:27
#define BLI_assert(a)
Definition BLI_assert.h:50
static void memarena_curbuf_align(MemArena *ma)
#define PADUP(num, amt)
void BLI_memarena_use_calloc(MemArena *ma)
#define VALGRIND_MEMPOOL_ALLOC(pool, addr, size)
void BLI_memarena_merge(MemArena *ma_dst, MemArena *ma_src)
void * BLI_memarena_calloc(MemArena *ma, size_t size)
MemArena * BLI_memarena_new(const size_t bufsize, const char *name)
#define VALGRIND_MOVE_MEMPOOL(pool_a, pool_b)
#define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed)
void BLI_memarena_use_malloc(MemArena *ma)
static void memarena_buf_free_all(struct MemBuf *mb)
void BLI_memarena_use_align(MemArena *ma, const size_t align)
#define VALGRIND_DESTROY_MEMPOOL(pool)
void BLI_memarena_free(MemArena *ma)
void * BLI_memarena_alloc(MemArena *ma, size_t size)
void BLI_memarena_clear(MemArena *ma)
unsigned char uchar
unsigned int uint
#define UNLIKELY(x)
Read Guarded memory(de)allocation.
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define NULL
draw_view push_constant(Type::INT, "radiance_src") .push_constant(Type capture_info_buf storage_buf(1, Qualifier::READ, "ObjectBounds", "bounds_buf[]") .push_constant(Type draw_view int
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
size_t(* MEM_allocN_len)(const void *vmemh)
Definition mallocn.cc:36
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
void *(* MEM_callocN)(size_t len, const char *str)
Definition mallocn.cc:42
_W64 int intptr_t
Definition stdint.h:118
struct MemBuf * bufs
uchar * curbuf
const char * name
size_t align
bool use_calloc
size_t cursize
size_t bufsize
uchar data[0]
struct MemBuf * next
PointerRNA * ptr
Definition wm_files.cc:4126