103 lines
3.4 KiB
Ziran
103 lines
3.4 KiB
Ziran
#import "file_linux"
|
|
#import "byte_text_linux"
|
|
|
|
// Private writable mapping: mutation never changes the source file. This
|
|
// makes the borrowed []u8 view compatible with Ziran's mutable byte slices.
|
|
#scope_file
|
|
libc :: #system_library "libc";
|
|
#if #defined(_WIN32) {
|
|
// FileHandle.fd holds the Win32 file handle. A copy-on-write view gives
|
|
// MAP_PRIVATE semantics, and it keeps the mapping and file open after
|
|
// both handles close, as mmap does with its descriptor.
|
|
kernel32 :: #system_library "kernel32";
|
|
CreateMappingRaw :: (file: *void, security: *void, protection: u32,
|
|
size_high: u32, size_low: u32, name: *u16) -> *void #foreign kernel32 "CreateFileMappingW";
|
|
MapViewRaw :: (mapping: *void, access: u32, offset_high: u32,
|
|
offset_low: u32, length: usize) -> *u8 #foreign kernel32 "MapViewOfFile";
|
|
UnmapViewRaw :: (address: *u8) -> s32 #foreign kernel32 "UnmapViewOfFile";
|
|
CloseHandleRaw :: (handle: *void) -> s32 #foreign kernel32 "CloseHandle";
|
|
PageWriteCopy :: 8;
|
|
FileMapCopy :: 1;
|
|
|
|
// The one caller maps readable, writable, and private.
|
|
MapRaw :: (address: *void, length: usize, protection: s32, flags: s32,
|
|
fd: s32, offset: s64) -> *u8 {
|
|
unused address
|
|
unused protection
|
|
unused flags
|
|
failed := cast(*u8)cast(isize)-1
|
|
mapping := CreateMappingRaw(cast(*void)cast(isize)fd, null,
|
|
cast(u32)PageWriteCopy, 0, 0, null)
|
|
if mapping == null { return failed }
|
|
view := MapViewRaw(mapping, cast(u32)FileMapCopy,
|
|
cast(u32)(offset >> 32), cast(u32)(offset & 0xffffffff), length)
|
|
CloseHandleRaw(mapping)
|
|
if view == null { return failed }
|
|
return view
|
|
}
|
|
|
|
UnmapRaw :: (address: *u8, length: usize) -> s32 {
|
|
unused length
|
|
if UnmapViewRaw(address) == 0 { return -1 }
|
|
return 0
|
|
}
|
|
} else {
|
|
#if OS == .ANDROID {
|
|
MapRaw :: (address: *void, length: usize, protection: s32, flags: s32,
|
|
fd: s32, offset: s64) -> *u8 #foreign libc "mmap64";
|
|
} else {
|
|
MapRaw :: (address: *void, length: usize, protection: s32, flags: s32,
|
|
fd: s32, offset: s64) -> *u8 #foreign libc "mmap";
|
|
}
|
|
UnmapRaw :: (address: *u8, length: usize) -> s32 #foreign libc "munmap";
|
|
}
|
|
|
|
#scope_export
|
|
MappedFile :: struct {
|
|
data: *u8
|
|
length: u64
|
|
valid: bool
|
|
}
|
|
|
|
#program_export
|
|
MapPrivate :: (path: string) -> MappedFile {
|
|
result: MappedFile
|
|
file: FileHandle = OpenRead(path)
|
|
if !file.valid { return result }
|
|
size: s64 = FileSize(file)
|
|
if size < 0 || cast(u64)cast(usize)size != cast(u64)size {
|
|
CloseFile(file)
|
|
return result
|
|
}
|
|
if size == 0 {
|
|
result.valid = CloseFile(file)
|
|
return result
|
|
}
|
|
mapped: *u8 = MapRaw(null, cast(usize)size, 3, 2, file.fd, 0)
|
|
closed: bool = CloseFile(file)
|
|
if cast(isize)mapped == cast(isize)-1 {
|
|
return result
|
|
}
|
|
if !closed {
|
|
UnmapRaw(mapped, cast(usize)size)
|
|
return result
|
|
}
|
|
result.data = mapped
|
|
result.length = cast(u64)size
|
|
result.valid = true
|
|
return result
|
|
}
|
|
|
|
#program_export
|
|
MappedBytes :: (file: MappedFile) -> []u8 {
|
|
if !file.valid { return BytesFromPointer(null, 0) }
|
|
return BytesFromPointer(file.data, file.length)
|
|
}
|
|
|
|
#program_export
|
|
UnmapFile :: (file: MappedFile) -> bool {
|
|
if !file.valid { return false }
|
|
if file.length == 0 { return true }
|
|
return UnmapRaw(file.data, cast(usize)file.length) == 0
|
|
}
|