Repository navigation
Expand file tree
/
Copy pathmemory.wave
More file actions
117 lines (104 loc) · 3.51 KB
/
Copy pathmemory.wave
File metadata and controls
117 lines (104 loc) · 3.51 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// 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
// Single-threaded linear-memory allocator with splitting and coalescing.
import("std::sys::wasm_blocks")::{MemoryBlock, blocks_reuse, blocks_append, blocks_free};
extern(c, "llvm.wasm.memory.size.i32") fun wasm_memory_size(index: i32) -> i32;
extern(c, "llvm.wasm.memory.grow.i32") fun wasm_memory_grow(index: i32, pages: i32) -> i32;
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 WASM_PAGE_SIZE: u64 = 65536;
const MAX_MEMORY: u64 = 4294967296;
static next_address: u64 = 0;
static memory_limit: u64 = 0;
static blocks: ptr<MemoryBlock> = null;
static blocks_tail: ptr<MemoryBlock> = null;
static free_blocks: ptr<MemoryBlock> = null;
fun align16(value: u64) -> u64 {
return (value + 15) & 18446744073709551600;
}
fun initialize_allocator() -> bool {
if (memory_limit != 0) {
return true;
}
var pages: i32 = wasm_memory_size(0);
if (pages < 0) {
return false;
}
next_address = pages as u64 * WASM_PAGE_SIZE;
memory_limit = next_address;
// Reserve a non-null header when an embedding starts with zero pages.
if (pages == 0) {
if (wasm_memory_grow(0, 1) < 0) {
return false;
}
next_address = 16;
memory_limit = WASM_PAGE_SIZE;
}
return true;
}
pub fun sys_alloc(size: i64) -> ptr<u8> {
if (size <= 0) {
return null;
}
var capacity: u64 = align16(size as u64);
if (capacity > MAX_MEMORY - 32) {
return null;
}
if (!initialize_allocator()) {
return null;
}
var reused: ptr<u8> = blocks_reuse(&free_blocks, &blocks_tail, size, capacity);
if (reused != null) {
return reused;
}
var total: u64 = capacity + 32;
var start: u64 = next_address;
if (start > MAX_MEMORY - total) {
return null;
}
if (total > memory_limit - start) {
// Grow complete owned pages. If another user grew linear memory in
// between, its pages remain outside this allocator's block list.
var pages: u64 = total / WASM_PAGE_SIZE;
if (total % WASM_PAGE_SIZE != 0) {
pages += 1;
}
var current: i32 = wasm_memory_size(0);
if (current < 0 || current as u64 > MAX_MEMORY / WASM_PAGE_SIZE - pages) {
return null;
}
var previous: i32 = wasm_memory_grow(0, pages as i32);
if (previous < 0) {
return null;
}
start = previous as u64 * WASM_PAGE_SIZE;
memory_limit = start + pages * WASM_PAGE_SIZE;
}
next_address = start + total;
return blocks_append(&blocks, &blocks_tail, start as ptr<u8>, size, capacity);
}
pub fun sys_free(p: ptr<u8>, size: i64) -> i64 {
return blocks_free(blocks, &blocks_tail, &free_blocks, p, size);
}
pub fun mmap(
addr: ptr<i8>, length: i64, prot: i32, flags: i32, fd: i64, offset: i64
) -> ptr<i8> {
if (addr != null || length <= 0 || prot != (PROT_READ | PROT_WRITE)
|| flags != (MAP_PRIVATE | MAP_ANONYMOUS) || fd != -1 || offset != 0) {
return null;
}
return sys_alloc(length) as ptr<i8>;
}
pub fun munmap(addr: ptr<i8>, length: i64) -> i64 {
return sys_free(addr as ptr<u8>, length);
}
pub fun brk(addr: ptr<i8>) -> ptr<i8> {
return null;
}
pub fun sys_page_size() -> i64 {
return WASM_PAGE_SIZE as i64;
}