pound-emu_pound/core/memory/arena.cpp
Ronald Caesar d1ada1740a fix(memory): Fixed missing mman.h include definitions
This commit removes a custom mmap() function which was supposed
to support both windows and linux but the linux and macOS builds
failed to compile.

The custom mmap() function will be replaced by malloc() for windows
and linux's native mmap().

Signed-off-by: Ronald Caesar <github43132@proton.me>
2025-07-09 17:38:10 -04:00

43 lines
1.4 KiB
C++

#include "arena.h"
#include <sys/mman.h>
#include "Base/Assert.h"
Memory::Arena Memory::arena_init() {
// TODO(GloriousEggroll): Replace malloc with a windows memory mapping API.
#ifdef WIN32
static_cast<uint8_t*>(malloc(sizeof(uint8_t) * MEMORY_CAPACITY));
#else
void* data = mmap(nullptr, MEMORY_CAPACITY, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
if (data == MAP_FAILED) {
return {0, 0, nullptr}; // Return invalid arena on failure
}
Memory::Arena arena = {
.capacity = MEMORY_CAPACITY,
.size = 0,
.data = static_cast<uint8_t*>(data),
};
return arena;
}
// new more memsafe code (ownedbywuigi) (i give up on windows compatibility for now, will stick to the old unsafe code)
const uint8_t* Memory::arena_allocate(Memory::Arena* arena,
const std::size_t size) {
ASSERT(arena != nullptr);
ASSERT(arena->size + size < arena->capacity);
const uint8_t* const data = &(arena->data[arena->size]);
arena->size += size;
return data;
}
void Memory::arena_reset(Memory::Arena* arena) {
ASSERT(arena != nullptr);
arena->size = 0;
}
void Memory::arena_free(Memory::Arena* arena) {
ASSERT(arena != nullptr);
arena->capacity = 0;
arena->size = 0;
// TODO(GloriousTaco): Replace free with a memory safe alternative.
free(arena->data);
}