Skip to content

Commit 2d42b88

Browse files
committed
Merge pull request #104124 from Ivorforce/alloc-static-calloc
Add `Memory::alloc_static_zeroed` to allocate memory that's filled with zeroes.
2 parents fa90be2 + 3207066 commit 2d42b88

File tree

8 files changed

+32
-47
lines changed

8 files changed

+32
-47
lines changed

core/io/zip_io.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ int zipio_testerror(voidpf opaque, voidpf stream) {
161161
}
162162

163163
voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
164-
voidpf ptr = memalloc((size_t)items * size);
165-
memset(ptr, 0, items * size);
164+
voidpf ptr = memalloc_zeroed((size_t)items * size);
166165
return ptr;
167166
}
168167

core/os/memory.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,20 @@ void Memory::free_aligned_static(void *p_memory) {
9393
free(p);
9494
}
9595

96+
template <bool p_ensure_zero>
9697
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
9798
#ifdef DEBUG_ENABLED
9899
bool prepad = true;
99100
#else
100101
bool prepad = p_pad_align;
101102
#endif
102103

103-
void *mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
104+
void *mem;
105+
if constexpr (p_ensure_zero) {
106+
mem = calloc(1, p_bytes + (prepad ? DATA_OFFSET : 0));
107+
} else {
108+
mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
109+
}
104110

105111
ERR_FAIL_NULL_V(mem, nullptr);
106112

@@ -120,6 +126,9 @@ void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
120126
}
121127
}
122128

129+
template void *Memory::alloc_static<true>(size_t p_bytes, bool p_pad_align);
130+
template void *Memory::alloc_static<false>(size_t p_bytes, bool p_pad_align);
131+
123132
void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
124133
if (p_memory == nullptr) {
125134
return alloc_static(p_bytes, p_pad_align);

core/os/memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ class Memory {
5454
static constexpr size_t ELEMENT_OFFSET = ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t) == 0) ? (SIZE_OFFSET + sizeof(uint64_t)) : ((SIZE_OFFSET + sizeof(uint64_t)) + alignof(uint64_t) - ((SIZE_OFFSET + sizeof(uint64_t)) % alignof(uint64_t)));
5555
static constexpr size_t DATA_OFFSET = ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t) == 0) ? (ELEMENT_OFFSET + sizeof(uint64_t)) : ((ELEMENT_OFFSET + sizeof(uint64_t)) + alignof(max_align_t) - ((ELEMENT_OFFSET + sizeof(uint64_t)) % alignof(max_align_t)));
5656

57+
template <bool p_ensure_zero = false>
5758
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
59+
_FORCE_INLINE_ static void *alloc_static_zeroed(size_t p_bytes, bool p_pad_align = false) { return alloc_static<true>(p_bytes, p_pad_align); }
5860
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
5961
static void free_static(void *p_ptr, bool p_pad_align = false);
6062

@@ -107,6 +109,7 @@ void operator delete(void *p_mem, void *p_pointer, size_t check, const char *p_d
107109
#endif
108110

109111
#define memalloc(m_size) Memory::alloc_static(m_size)
112+
#define memalloc_zeroed(m_size) Memory::alloc_static_zeroed(m_size)
110113
#define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
111114
#define memfree(m_mem) Memory::free_static(m_mem)
112115

core/templates/a_hash_map.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,9 @@ class AHashMap {
214214

215215
HashMapData *old_map_data = map_data;
216216

217-
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static(sizeof(HashMapData) * real_capacity));
217+
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static_zeroed(sizeof(HashMapData) * real_capacity));
218218
elements = reinterpret_cast<MapKeyValue *>(Memory::realloc_static(elements, sizeof(MapKeyValue) * (_get_resize_count(capacity) + 1)));
219219

220-
memset(map_data, EMPTY_HASH, real_capacity * sizeof(HashMapData));
221-
222220
if (num_elements != 0) {
223221
for (uint32_t i = 0; i < real_old_capacity; i++) {
224222
HashMapData data = old_map_data[i];
@@ -236,10 +234,8 @@ class AHashMap {
236234
// Allocate on demand to save memory.
237235

238236
uint32_t real_capacity = capacity + 1;
239-
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static(sizeof(HashMapData) * real_capacity));
237+
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static_zeroed(sizeof(HashMapData) * real_capacity));
240238
elements = reinterpret_cast<MapKeyValue *>(Memory::alloc_static(sizeof(MapKeyValue) * (_get_resize_count(capacity) + 1)));
241-
242-
memset(map_data, EMPTY_HASH, real_capacity * sizeof(HashMapData));
243239
}
244240

245241
if (unlikely(num_elements > _get_resize_count(capacity))) {

core/templates/hash_map.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,9 @@ class HashMap : private Allocator {
169169
uint32_t *old_hashes = hashes;
170170

171171
num_elements = 0;
172-
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
173-
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
174-
175-
for (uint32_t i = 0; i < capacity; i++) {
176-
hashes[i] = 0;
177-
elements[i] = nullptr;
178-
}
172+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
173+
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
174+
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));
179175

180176
if (old_capacity == 0) {
181177
// Nothing to do.
@@ -199,13 +195,9 @@ class HashMap : private Allocator {
199195
if (unlikely(elements == nullptr)) {
200196
// Allocate on demand to save memory.
201197

202-
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
203-
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));
204-
205-
for (uint32_t i = 0; i < capacity; i++) {
206-
hashes[i] = EMPTY_HASH;
207-
elements[i] = nullptr;
208-
}
198+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
199+
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
200+
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));
209201
}
210202

211203
if (num_elements + 1 > MAX_OCCUPANCY * capacity) {

core/templates/hash_set.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,12 @@ class HashSet {
144144
uint32_t *old_hashes = hashes;
145145
uint32_t *old_key_to_hash = key_to_hash;
146146

147-
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
147+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
148+
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
148149
keys = reinterpret_cast<TKey *>(Memory::realloc_static(keys, sizeof(TKey) * capacity));
149150
key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
150151
hash_to_key = reinterpret_cast<uint32_t *>(Memory::realloc_static(hash_to_key, sizeof(uint32_t) * capacity));
151152

152-
for (uint32_t i = 0; i < capacity; i++) {
153-
hashes[i] = EMPTY_HASH;
154-
}
155-
156153
for (uint32_t i = 0; i < num_elements; i++) {
157154
uint32_t h = old_hashes[old_key_to_hash[i]];
158155
_insert_with_hash(h, i);
@@ -167,14 +164,11 @@ class HashSet {
167164
if (unlikely(keys == nullptr)) {
168165
// Allocate on demand to save memory.
169166

170-
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
167+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
168+
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
171169
keys = reinterpret_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
172170
key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
173171
hash_to_key = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
174-
175-
for (uint32_t i = 0; i < capacity; i++) {
176-
hashes[i] = EMPTY_HASH;
177-
}
178172
}
179173

180174
uint32_t pos = 0;

core/templates/oa_hash_map.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,8 @@ class OAHashMap {
153153
num_elements = 0;
154154
keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
155155
values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
156-
hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
157-
158-
for (uint32_t i = 0; i < capacity; i++) {
159-
hashes[i] = 0;
160-
}
156+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
157+
hashes = static_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
161158

162159
if (old_capacity == 0) {
163160
// Nothing to do.
@@ -384,11 +381,8 @@ class OAHashMap {
384381

385382
keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
386383
values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
387-
hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
388-
389-
for (uint32_t i = 0; i < capacity; i++) {
390-
hashes[i] = EMPTY_HASH;
391-
}
384+
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
385+
hashes = static_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
392386
}
393387

394388
~OAHashMap() {

drivers/gles3/shader_gles3.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
341341
iloglen = 4096; // buggy driver (Adreno 220+)
342342
}
343343

344-
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
345-
memset(ilogmem, 0, iloglen + 1);
344+
char *ilogmem = (char *)Memory::alloc_static_zeroed(iloglen + 1);
346345
glGetShaderInfoLog(spec.vert_id, iloglen, &iloglen, ilogmem);
347346

348347
String err_string = name + ": Vertex shader compilation failed:\n";
@@ -390,8 +389,7 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
390389
iloglen = 4096; // buggy driver (Adreno 220+)
391390
}
392391

393-
char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
394-
memset(ilogmem, 0, iloglen + 1);
392+
char *ilogmem = (char *)Memory::alloc_static_zeroed(iloglen + 1);
395393
glGetShaderInfoLog(spec.frag_id, iloglen, &iloglen, ilogmem);
396394

397395
String err_string = name + ": Fragment shader compilation failed:\n";

0 commit comments

Comments
 (0)