Repository navigation
Expand file tree
/
Copy pathwasm_blocks.wave
More file actions
146 lines (137 loc) · 4.69 KB
/
Copy pathwasm_blocks.wave
File metadata and controls
146 lines (137 loc) · 4.69 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// 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
import("std::sys::errors")::{SYS_ERR_INVAL};
// Single-threaded allocator metadata shared by wasm32 and Memory64.
// Each header occupies 32 bytes; payloads and split headers stay 16-aligned.
// The address-ordered list contains live and free blocks, so free validates
// ownership and the original requested size before touching caller memory.
pub struct MemoryBlock {
capacity: u64;
requested: i64;
next: ptr<MemoryBlock>;
free_next: ptr<MemoryBlock>;
}
fun free_list_insert(head: ptr<ptr<MemoryBlock>>, block: ptr<MemoryBlock>) {
if (deref head == null || block as u64 < deref head as u64) {
block.free_next = deref head;
deref head = block;
return;
}
var previous: ptr<MemoryBlock> = deref head;
while (previous.free_next != null && previous.free_next as u64 < block as u64) {
previous = previous.free_next;
}
block.free_next = previous.free_next;
previous.free_next = block;
}
fun free_list_remove(head: ptr<ptr<MemoryBlock>>, target: ptr<MemoryBlock>) {
var previous: ptr<MemoryBlock> = null;
var block: ptr<MemoryBlock> = deref head;
while (block != null) {
if (block == target) {
if (previous == null) {
deref head = block.free_next;
} else {
previous.free_next = block.free_next;
}
block.free_next = null;
return;
}
previous = block;
block = block.free_next;
}
}
pub fun blocks_reuse(
free_head: ptr<ptr<MemoryBlock>>, tail: ptr<ptr<MemoryBlock>>, size: i64, capacity: u64
) -> ptr<u8> {
var block: ptr<MemoryBlock> = deref free_head;
while (block != null) {
if (block.capacity >= capacity) {
free_list_remove(free_head, block);
var remaining: u64 = block.capacity - capacity;
if (remaining >= 48) {
var split: ptr<MemoryBlock> = ((block as u64) + 32 + capacity) as ptr<MemoryBlock>;
deref split = MemoryBlock {
capacity: remaining - 32,
requested: 0,
next: block.next,
free_next: null
};
block.next = split;
block.capacity = capacity;
if (deref tail == block) {
deref tail = split;
}
free_list_insert(free_head, split);
}
block.requested = size;
block.free_next = null;
return ((block as u64) + 32) as ptr<u8>;
}
block = block.free_next;
}
return null;
}
pub fun blocks_append(
head: ptr<ptr<MemoryBlock>>, tail: ptr<ptr<MemoryBlock>>, storage: ptr<u8>, size: i64, capacity: u64
) -> ptr<u8> {
var block: ptr<MemoryBlock> = storage as ptr<MemoryBlock>;
deref block = MemoryBlock { capacity: capacity, requested: size, next: null, free_next: null };
if (deref head == null) {
deref head = block;
} else {
(deref tail).next = block;
}
deref tail = block;
return ((block as u64) + 32) as ptr<u8>;
}
pub fun blocks_free(
head: ptr<MemoryBlock>, tail: ptr<ptr<MemoryBlock>>, free_head: ptr<ptr<MemoryBlock>>,
address: ptr<u8>, size: i64
) -> i64 {
if (address == null) {
return 0;
}
if (size <= 0) {
return SYS_ERR_INVAL;
}
var previous: ptr<MemoryBlock> = null;
var block: ptr<MemoryBlock> = head;
while (block != null) {
if ((block as u64) + 32 == address as u64) {
if (block.requested != size) {
return SYS_ERR_INVAL;
}
block.requested = 0;
break;
}
previous = block;
block = block.next;
}
if (block == null) {
return SYS_ERR_INVAL;
}
var next_block: ptr<MemoryBlock> = block.next;
if (next_block != null && next_block.requested == 0
&& (block as u64) + 32 + block.capacity == next_block as u64) {
free_list_remove(free_head, next_block);
block.capacity += 32 + next_block.capacity;
block.next = next_block.next;
if (deref tail == next_block) {
deref tail = block;
}
}
if (previous != null && previous.requested == 0
&& (previous as u64) + 32 + previous.capacity == block as u64) {
previous.capacity += 32 + block.capacity;
previous.next = block.next;
if (deref tail == block) {
deref tail = previous;
}
} else {
free_list_insert(free_head, block);
}
return 0;
}