Add virtual.map_file

This commit is contained in:
gingerBill
2024-01-17 22:41:22 +00:00
parent 90ac400ec5
commit 248a0bfa5f
5 changed files with 140 additions and 2 deletions
+18
View File
@@ -48,3 +48,21 @@ _platform_memory_init :: proc() {
// is power of two
assert(DEFAULT_PAGE_SIZE != 0 && (DEFAULT_PAGE_SIZE & (DEFAULT_PAGE_SIZE-1)) == 0)
}
_map_file :: proc "contextless" (fd: uintptr, size: i64, flags: Mapped_File_Flags) -> (data: []byte, error: Mapped_File_Error) {
prot: linux.Mem_Protection
if .Read in flags {
prot += {.READ}
}
if .Write in flags {
prot += {.WRITE}
}
flags := linux.Map_Flags{.SHARED}
addr, errno := linux.mmap(0, uint(size), prot, flags, linux.Fd(fd), offset=0)
if addr == nil || error != nil {
return nil, .Map_Failure
}
return ([^]byte)(addr)[:size], nil
}