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>
This commit is contained in:
Ronald Caesar 2025-07-09 17:38:10 -04:00
parent b9adfe0ff0
commit d1ada1740a
2 changed files with 9 additions and 68 deletions

View file

@ -1,24 +1,17 @@
#include "arena.h"
#include <sys/mman.h>
#include "Base/Assert.h"
#include "sys/mman.h"
// Memory::Arena Memory::arena_init() {
// // TODO(GloriousTaco): The line below is stupidly unsafe. Replace malloc with mmap() and check the return value.
// auto data =
// static_cast<uint8_t*>(malloc(sizeof(uint8_t) * MEMORY_CAPACITY));
// Memory::Arena arena = {
// .capacity = MEMORY_CAPACITY,
// .size = 0,
// .data = data,
// };
// return arena;
// }
// old unsafe code
Memory::Arena Memory::arena_init() {
void* data = mmap(nullptr, MEMORY_CAPACITY, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
// 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
return {0, 0, nullptr}; // Return invalid arena on failure
}
Memory::Arena arena = {
.capacity = MEMORY_CAPACITY,

View file

@ -1,52 +0,0 @@
// mman.h
// only there for Windows compatibility
#ifndef MMAN_H
#define MMAN_H
#if defined(__linux__) || defined(__APPLE__)
// Linux or macOS: Use standard sys/mman.h
#include <sys/mman.h>
#else
// Windows: Define mmap, munmap, and MAP_FAILED
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define MAP_FAILED ((void*)-1)
// Protection and flag constants (minimal subset for your use case)
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define MAP_PRIVATE 0x2
#define MAP_ANONYMOUS 0x20
// mmap equivalent using VirtualAlloc
inline void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
(void)addr; // Ignored (VirtualAlloc doesn't support specific address)
(void)flags; // Ignored (simplified for anonymous mapping)
(void)fd; // Ignored (no file mapping)
(void)offset; // Ignored (no file mapping)
DWORD protect = 0;
if (prot & PROT_READ && prot & PROT_WRITE) {
protect = PAGE_READWRITE;
} else if (prot & PROT_READ) {
protect = PAGE_READONLY;
} else {
return MAP_FAILED; // Unsupported protection
}
void* ptr = VirtualAlloc(nullptr, length, MEM_COMMIT | MEM_RESERVE, protect);
return ptr ? ptr : MAP_FAILED;
}
// munmap equivalent using VirtualFree
inline int munmap(void* addr, size_t length) {
(void)length; // Ignored (VirtualFree doesn't need length for committed memory)
BOOL result = VirtualFree(addr, 0, MEM_RELEASE);
return result ? 0 : -1; // Return 0 on success, -1 on failure
}
#endif
#endif // MMAN_H