Repository navigation
Expand file tree
/
Copy pathmemory.wave
More file actions
99 lines (87 loc) · 2.76 KB
/
Copy pathmemory.wave
File metadata and controls
99 lines (87 loc) · 2.76 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
// SPDX-License-Identifier: Apache-2.0
// Windows system ABI provider for virtual memory.
extern(system, "VirtualAlloc") fun win_virtual_alloc(
address: ptr<i8>, size: i64, allocation_type: u32, protect: u32
) -> ptr<i8>;
extern(system, "VirtualFree") fun win_virtual_free(
address: ptr<i8>, size: i64, free_type: u32
) -> i32;
struct WindowsSystemInfo {
processor_architecture: u32;
page_size: u32;
minimum_application_address: ptr<u8>;
maximum_application_address: ptr<u8>;
active_processor_mask: u64;
processor_count: u32;
processor_type: u32;
allocation_granularity: u32;
processor_level_and_revision: u32;
}
extern(system, "GetSystemInfo") fun win_get_system_info(info: ptr<WindowsSystemInfo>);
pub const PROT_READ: i32 = 1;
pub const PROT_WRITE: i32 = 2;
pub const MAP_PRIVATE: i32 = 2;
pub const MAP_ANONYMOUS: i32 = 32;
const MEM_COMMIT_RESERVE: u32 = 12288;
const MEM_RELEASE: u32 = 32768;
const PAGE_NOACCESS: u32 = 1;
const PAGE_READONLY: u32 = 2;
const PAGE_READWRITE: u32 = 4;
const SYS_ERR_INVALID: i64 = -22;
pub fun mmap(
addr: ptr<i8>,
length: i64,
prot: i32,
flags: i32,
fd: i64,
offset: i64
) -> ptr<i8> {
if (length <= 0 || fd != -1 || offset != 0 || flags != (MAP_PRIVATE | MAP_ANONYMOUS)) {
return null;
}
// There is no write-only VirtualAlloc protection. Reject it instead of
// silently granting read access, along with unknown/execute protection bits.
var protection: u32 = PAGE_NOACCESS;
if (prot == PROT_READ) {
protection = PAGE_READONLY;
} else if (prot == (PROT_READ | PROT_WRITE)) {
protection = PAGE_READWRITE;
} else if (prot != 0) {
return null;
}
return win_virtual_alloc(addr, length, MEM_COMMIT_RESERVE, protection);
}
pub fun munmap(addr: ptr<i8>, length: i64) -> i64 {
if (addr == null || length <= 0) {
return SYS_ERR_INVALID;
}
if (win_virtual_free(addr, 0, MEM_RELEASE) == 0) {
return -1;
}
return 0;
}
// Windows has no process break; allocation callers use VirtualAlloc instead.
pub fun brk(addr: ptr<i8>) -> ptr<i8> {
return null;
}
pub fun sys_alloc(size: i64) -> ptr<u8> {
if (size <= 0) {
return null;
}
return win_virtual_alloc(null, size, MEM_COMMIT_RESERVE, PAGE_READWRITE) as ptr<u8>;
}
pub fun sys_free(p: ptr<u8>, size: i64) -> i64 {
if (p == null || size <= 0) {
return 0;
}
return munmap(p as ptr<i8>, size);
}
pub fun sys_page_size() -> i64 {
var info: WindowsSystemInfo;
win_get_system_info(&info);
if (info.page_size == 0) { return 4096; }
return info.page_size as i64;
}