From 811644f4c75ad2d22b546ac9a7c9c8ea00f15271 Mon Sep 17 00:00:00 2001 From: Carsten Larsen Date: Sat, 30 Jan 2021 17:42:13 +0100 Subject: [PATCH] Zero out allocated memory --- src/clib/mem.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/clib/mem.c b/src/clib/mem.c index e9f9c5c6..d3f55c11 100644 --- a/src/clib/mem.c +++ b/src/clib/mem.c @@ -37,7 +37,7 @@ #include #define ALLOC_LIST(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR) #define FREE_LIST(x) FreeVec(x) -#define ALLOC_MEM(x, y) AllocPooled(x, y) +#define ALLOC_MEM(x, y) AllocPooledZero(x, y) #define FREE_MEM(x, y, z) FreePooled(x, y, z) #define Debug(x, y, z) #else @@ -85,6 +85,31 @@ struct MemoryList *list = nullptr; void alloc_error(char *, size_t); void dealloc_error(char *, void *); +#if defined(AMIGA) + +static void MemZero(void *address, ULONG size) +{ + char *c = (char *)address; + int n = size; + do + { + *c++ = '\0'; + } while (--n); +} + +static void *AllocPooledZero(void *pool, size_t size) +{ + ULONG memSize = (ULONG)size; + void *memory = AllocPooled(pool, memSize); + if (memory != NULL) + { + MemZero(memory, memSize); + } + return memory; +} + +#endif + /** * @brief Allocate memory and add it to the global memory list. */ @@ -112,11 +137,11 @@ void *AllocMemSafe(size_t size) } #ifdef P64BIT - // Align to bytes of 8 - allocsize = (size + 7) & ~0x07; + // Align to bytes of 8, remove 0 bytes allocations + allocsize = (size + 8) & ~0x07; #else - // Align to bytes of 4 - allocsize = (size + 3) & ~0x03; + // Align to bytes of 4, remove 0 bytes allocations + allocsize = (size + 4) & ~0x03; #endif newblock = (struct MemoryBlock *)ALLOC_MEM(list->pool, sizeof(struct MemoryBlock));