Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions core/io/zip_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ int zipio_testerror(voidpf opaque, voidpf stream) {
}

voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) {
voidpf ptr = memalloc((size_t)items * size);
memset(ptr, 0, items * size);
voidpf ptr = memalloc_zeroed((size_t)items * size);
return ptr;
}

Expand Down
11 changes: 10 additions & 1 deletion core/os/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ void Memory::free_aligned_static(void *p_memory) {
free(p);
}

template <bool p_ensure_zero>
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
#ifdef DEBUG_ENABLED
bool prepad = true;
#else
bool prepad = p_pad_align;
#endif

void *mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
void *mem;
if constexpr (p_ensure_zero) {
mem = calloc(1, p_bytes + (prepad ? DATA_OFFSET : 0));
} else {
mem = malloc(p_bytes + (prepad ? DATA_OFFSET : 0));
}

ERR_FAIL_NULL_V(mem, nullptr);

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

template void *Memory::alloc_static<true>(size_t p_bytes, bool p_pad_align);
template void *Memory::alloc_static<false>(size_t p_bytes, bool p_pad_align);

void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
if (p_memory == nullptr) {
return alloc_static(p_bytes, p_pad_align);
Expand Down
3 changes: 3 additions & 0 deletions core/os/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Memory {
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)));
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)));

template <bool p_ensure_zero = false>
static void *alloc_static(size_t p_bytes, bool p_pad_align = false);
_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); }
static void *realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align = false);
static void free_static(void *p_ptr, bool p_pad_align = false);

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

#define memalloc(m_size) Memory::alloc_static(m_size)
#define memalloc_zeroed(m_size) Memory::alloc_static_zeroed(m_size)
#define memrealloc(m_mem, m_size) Memory::realloc_static(m_mem, m_size)
#define memfree(m_mem) Memory::free_static(m_mem)

Expand Down
8 changes: 2 additions & 6 deletions core/templates/a_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,9 @@ class AHashMap {

HashMapData *old_map_data = map_data;

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

memset(map_data, EMPTY_HASH, real_capacity * sizeof(HashMapData));

if (num_elements != 0) {
for (uint32_t i = 0; i < real_old_capacity; i++) {
HashMapData data = old_map_data[i];
Expand All @@ -236,10 +234,8 @@ class AHashMap {
// Allocate on demand to save memory.

uint32_t real_capacity = capacity + 1;
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static(sizeof(HashMapData) * real_capacity));
map_data = reinterpret_cast<HashMapData *>(Memory::alloc_static_zeroed(sizeof(HashMapData) * real_capacity));
elements = reinterpret_cast<MapKeyValue *>(Memory::alloc_static(sizeof(MapKeyValue) * (_get_resize_count(capacity) + 1)));

memset(map_data, EMPTY_HASH, real_capacity * sizeof(HashMapData));
}

if (unlikely(num_elements > _get_resize_count(capacity))) {
Expand Down
20 changes: 6 additions & 14 deletions core/templates/hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,9 @@ class HashMap {
uint32_t *old_hashes = hashes;

num_elements = 0;
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = 0;
elements[i] = nullptr;
}
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));

if (old_capacity == 0) {
// Nothing to do.
Expand All @@ -200,13 +196,9 @@ class HashMap {
if (unlikely(elements == nullptr)) {
// Allocate on demand to save memory.

hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static(sizeof(HashMapElement<TKey, TValue> *) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = EMPTY_HASH;
elements[i] = nullptr;
}
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
elements = reinterpret_cast<HashMapElement<TKey, TValue> **>(Memory::alloc_static_zeroed(sizeof(HashMapElement<TKey, TValue> *) * capacity));
}

if (num_elements + 1 > MAX_OCCUPANCY * capacity) {
Expand Down
14 changes: 4 additions & 10 deletions core/templates/hash_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,12 @@ class HashSet {
uint32_t *old_hashes = hashes;
uint32_t *old_key_to_hash = key_to_hash;

hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
keys = reinterpret_cast<TKey *>(Memory::realloc_static(keys, sizeof(TKey) * capacity));
key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
hash_to_key = reinterpret_cast<uint32_t *>(Memory::realloc_static(hash_to_key, sizeof(uint32_t) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = EMPTY_HASH;
}

Comment thread
Ivorforce marked this conversation as resolved.
Outdated
for (uint32_t i = 0; i < num_elements; i++) {
uint32_t h = old_hashes[old_key_to_hash[i]];
_insert_with_hash(h, i);
Expand All @@ -167,14 +164,11 @@ class HashSet {
if (unlikely(keys == nullptr)) {
// Allocate on demand to save memory.

hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = reinterpret_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
keys = reinterpret_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
key_to_hash = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));
hash_to_key = reinterpret_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = EMPTY_HASH;
}
}

uint32_t pos = 0;
Expand Down
14 changes: 4 additions & 10 deletions core/templates/oa_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,8 @@ class OAHashMap {
num_elements = 0;
keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = 0;
}
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = static_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));

if (old_capacity == 0) {
// Nothing to do.
Expand Down Expand Up @@ -384,11 +381,8 @@ class OAHashMap {

keys = static_cast<TKey *>(Memory::alloc_static(sizeof(TKey) * capacity));
values = static_cast<TValue *>(Memory::alloc_static(sizeof(TValue) * capacity));
hashes = static_cast<uint32_t *>(Memory::alloc_static(sizeof(uint32_t) * capacity));

for (uint32_t i = 0; i < capacity; i++) {
hashes[i] = EMPTY_HASH;
}
static_assert(EMPTY_HASH == 0, "Assuming EMPTY_HASH = 0 for alloc_static_zeroed call");
hashes = static_cast<uint32_t *>(Memory::alloc_static_zeroed(sizeof(uint32_t) * capacity));
}

~OAHashMap() {
Expand Down
6 changes: 2 additions & 4 deletions drivers/gles3/shader_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
iloglen = 4096; // buggy driver (Adreno 220+)
}

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

String err_string = name + ": Vertex shader compilation failed:\n";
Expand Down Expand Up @@ -388,8 +387,7 @@ void ShaderGLES3::_compile_specialization(Version::Specialization &spec, uint32_
iloglen = 4096; // buggy driver (Adreno 220+)
}

char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
memset(ilogmem, 0, iloglen + 1);
char *ilogmem = (char *)Memory::alloc_static_zeroed(iloglen + 1);
glGetShaderInfoLog(spec.frag_id, iloglen, &iloglen, ilogmem);

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