Compare commits

...

3 commits

Author SHA1 Message Date
Ronald Caesar
9341e8d4ee
Merge remote-tracking branch 'Xphalnos/arm64' into arm64
Made freeing memory from our arena allocator safer. Replaced free() with munmap().
2025-10-17 17:13:47 -04:00
Ronald Caesar
194b15b556
host: add assertions
Munmap can fail if the length argument is 0, and the address being freed
is not a multiple of the host's page size.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2025-10-17 16:28:02 -04:00
Xphalnos
51e7adcbee Replace free() by munmap()
Signed-off-by: Xphalnos <yiga.steam@gmail.com>
2025-10-16 08:16:32 +02:00

View file

@ -5,6 +5,7 @@
#ifdef WIN32
#include <Windows.h>
#else
#include <unistd.h>
#include <sys/mman.h>
#endif
@ -35,7 +36,6 @@ arena_t arena_init(size_t capacity)
};
return arena;
}
// new more memsafe code (ownedbywuigi) (i give up on windows compatibility for now, will stick to the old unsafe code)
void* arena_allocate(memory::arena_t* arena, const std::size_t size)
{
@ -54,15 +54,29 @@ void arena_reset(memory::arena_t* arena)
}
void arena_free(memory::arena_t* arena)
{
PVM_ASSERT(arena != nullptr);
PVM_ASSERT(nullptr != arena);
PVM_ASSERT(nullptr != arena->data);
#ifdef WIN32
size_t size = 0;
const int return_val = VirtualFree(arena->data, size, MEM_RELEASE);
if (0 == return_val)
{
PVM_ASSERT_MSG(false, "Failed to free arena memory");
}
#else
long page_size = sysconf(_SC_PAGESIZE);
PVM_ASSERT(page_size > 0);
PVM_ASSERT(arena->capacity > 0);
PVM_ASSERT(0 == ((uintptr_t)arena->data % (size_t)page_size));
int return_val = munmap(arena->data, arena->capacity);
if (-1 == return_val)
{
PVM_ASSERT_MSG(false, "Failed to free arena memory");
}
#endif
arena->capacity = 0;
arena->size = 0;
// TODO(GloriousTaco:memory): Replace free with a memory safe alternative.
#ifdef WIN32
VirtualFree(arena->data, 0, MEM_RELEASE);
#else
free(arena->data);
#endif
}
} // namespace pound::host::memory