From 00b6aaa735ef3aab8a96eceafd010d17a875d8d5 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 19 Apr 2025 06:37:17 -0500 Subject: [PATCH] refactor(allocator): made seperate allocator function for malloc --- Include/Hallocy/Core/Allocator.h | 5 ++++- Src/Core/Allocator.c | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Include/Hallocy/Core/Allocator.h b/Include/Hallocy/Core/Allocator.h index 825bad7..04ff2e5 100644 --- a/Include/Hallocy/Core/Allocator.h +++ b/Include/Hallocy/Core/Allocator.h @@ -24,10 +24,13 @@ #define HALLOCY_ALLOCATOR #include +#include #include "../Utils/Error.h" -void *hallocy_malloc(size_t size); +void *hallocy_allocate(size_t size, bool zero_memory); + +static inline void *hallocy_malloc(size_t size) { return hallocy_allocate(size, false); } HallocyError hallocy_free(void *pointer); #endif \ No newline at end of file diff --git a/Src/Core/Allocator.c b/Src/Core/Allocator.c index 9ff04ba..42b88b4 100644 --- a/Src/Core/Allocator.c +++ b/Src/Core/Allocator.c @@ -21,8 +21,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Hallocy/Core/Allocator.h" - -#include +#include "../../Include/Hallocy/Core/Memory.h" #if defined(_WIN32) #include @@ -70,7 +69,7 @@ static BOOL CALLBACK hallocy_initialize_mutex(PINIT_ONCE init_once, PVOID parame } #endif -void *hallocy_malloc(size_t size) { +void *hallocy_allocate(size_t size, bool zero_memory) { if (page_size == 0) { #if defined(_WIN32) SYSTEM_INFO system_info; @@ -102,6 +101,10 @@ void *hallocy_malloc(size_t size) { } memory_pointer->next = NULL; + if (zero_memory) { + hallocy_set_memory(memory_pointer + 1, 0, aligned_size - sizeof(HallocyMemoryHeader)); + } + return (void*)(memory_pointer + 1); } @@ -114,7 +117,12 @@ void *hallocy_malloc(size_t size) { hallocy_heap = GetProcessHeap(); } - memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size); + if (zero_memory) { + memory_pointer = HeapAlloc(hallocy_heap, HEAP_ZERO_MEMORY, aligned_size); + } else { + memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size); + } + if (memory_pointer == NULL) { return NULL; } @@ -162,6 +170,10 @@ void *hallocy_malloc(size_t size) { #endif memory_pointer->next = NULL; + if (zero_memory) { + hallocy_set_memory(memory_pointer + 1, 0, aligned_size - sizeof(HallocyMemoryHeader)); + } + return (void*)(memory_pointer + 1); } @@ -174,7 +186,12 @@ void *hallocy_malloc(size_t size) { hallocy_heap = GetProcessHeap(); } - memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size); + if (zero_memory) { + memory_pointer = HeapAlloc(hallocy_heap, HEAP_ZERO_MEMORY, aligned_size); + } else { + memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size); + } + if (memory_pointer == NULL) { return NULL; }