aboutsummaryrefslogtreecommitdiff
path: root/core/mem/virtual/file.odin
blob: 660210bbf1570b0efe5cb9f4a39892eccc159de2 (plain)
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
#+build !freestanding
#+build !js
package mem_virtual

import "core:os"

map_file :: proc{
	map_file_from_path,
	map_file_from_file,
}

map_file_from_path :: proc(filename: string, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
	f, err := os.open(filename, os.O_RDWR)
	if err != nil {
		return nil, .Open_Failure
	}
	defer os.close(f)
	return map_file_from_file(f, flags)
}

map_file_from_file :: proc(f: ^os.File, flags: Map_File_Flags) -> (data: []byte, error: Map_File_Error) {
	size, os_err := os.file_size(f)
	if os_err != nil {
		return nil, .Stat_Failure
	}
	if size < 0 {
		return nil, .Negative_Size
	}
	if size != i64(int(size)) {
		return nil, .Too_Large_Size
	}
	fd := os.fd(f)
	return _map_file(fd, size, flags)
}

unmap_file :: proc(data: []byte) {
	if raw_data(data) != nil {
		_unmap_file(data)
	}
}