From 2050c0e0576f05156f192aa4caf48834d2f28b14 Mon Sep 17 00:00:00 2001 From: fschildt Date: Fri, 22 Aug 2025 15:23:11 +0200 Subject: first commit --- src/os/linux/linux_memory.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/os/linux/linux_memory.c (limited to 'src/os/linux/linux_memory.c') diff --git a/src/os/linux/linux_memory.c b/src/os/linux/linux_memory.c new file mode 100644 index 0000000..668604e --- /dev/null +++ b/src/os/linux/linux_memory.c @@ -0,0 +1,38 @@ +#include +#include + +#include +#include +#include +#include +#include + +b32 +os_memory_allocate(OSMemory *memory, size_t size) +{ + // @Performance: Keep the fd of /dev/zero open for the whole runtime? + int fd = open("/dev/zero", O_RDWR); + if (fd == -1) { + printf("open() failed: %s\n", strerror(errno)); + return false; + } + + void *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0); + if (address == MAP_FAILED) { + printf("mmap failed\n"); + return false; + } + + close(fd); + + memory->size = size; + memory->p = address; + return true; +} + +void +os_memory_free(OSMemory *memory) +{ + munmap(memory->p, memory->size); +} + -- cgit v1.2.3