diff options
Diffstat (limited to 'src/os/linux/linux_memory.c')
| -rw-r--r-- | src/os/linux/linux_memory.c | 38 | 
1 files changed, 38 insertions, 0 deletions
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 <os/os.h> +#include <basic/basic.h> + +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> +#include <string.h> +#include <sys/mman.h> + +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); +} +  | 
