1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package mem_virtual
import "core:os"
Map_File_Error :: enum {
None,
Open_Failure,
Stat_Failure,
Negative_Size,
Too_Large_Size,
Map_Failure,
}
Map_File_Flag :: enum u32 {
Read,
Write,
}
Map_File_Flags :: distinct bit_set[Map_File_Flag; u32]
map_file :: proc{
map_file_from_path,
map_file_from_file_descriptor,
}
map_file_from_path :: proc(filename: string, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
fd, err := os.open(filename, os.O_RDWR)
if err != 0 {
return nil, .Open_Failure
}
defer os.close(fd)
return map_file_from_file_descriptor(uintptr(fd), flags)
}
map_file_from_file_descriptor :: proc(fd: uintptr, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
size, os_err := os.file_size(os.Handle(fd))
if os_err != 0 {
return nil, .Stat_Failure
}
if size < 0 {
return nil, .Negative_Size
}
if size != i64(int(size)) {
return nil, .Too_Large_Size
}
return _map_file(fd, size, flags)
}
|