forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalloc.sw
70 lines (66 loc) · 2.28 KB
/
alloc.sw
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
//! A library for allocating memory inspired by [Rust's std::alloc](https://doc.rust-lang.org/std/alloc/index.html).
library;
/// Allocates zeroed memory on the heap.
///
/// In the FuelVM, the heap begins at `VM_MAX_RAM` and grows downward.
/// The heap pointer(`$hp`) always points to the first allocated byte.
///
/// Initially the heap will look like this:
/// ```
/// ▾$hp
/// ... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
/// ▴VM_MAX_RAM
/// ```
/// After allocating with `let ptr = alloc::<u64>(1)`:
/// ```
/// ▾$hp
/// ... 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |
/// ▴ptr ▴VM_MAX_RAM
/// ```
/// After writing with `sw(ptr, u64::max())`:
/// ```
/// ▾$hp
/// ... 00 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF |
/// ▴ptr ▴VM_MAX_RAM
/// ```
/// For more information, see the Fuel Spec for [VM Initialization](https://fuellabs.github.io/fuel-specs/master/vm#vm-initialization)
/// and the VM Instruction Set for [Memory Allocation](https://fuellabs.github.io/fuel-specs/master/vm/instruction_set.html#aloc-allocate-memory).
pub fn alloc<T>(count: u64) -> raw_ptr {
asm(size: __size_of::<T>() * count, ptr) {
aloc size;
move ptr hp;
ptr: raw_ptr
}
}
/// Reallocates the given area of memory.
pub fn realloc<T>(ptr: raw_ptr, count: u64, new_count: u64) -> raw_ptr {
if new_count > count {
let new_ptr = alloc::<T>(new_count);
if count > 0 {
ptr.copy_to::<T>(new_ptr, count);
}
new_ptr
} else {
ptr
}
}
/// Allocates zeroed memory on the heap in individual bytes.
pub fn alloc_bytes(count: u64) -> raw_ptr {
asm(size: count, ptr) {
aloc size;
move ptr hp;
ptr: raw_ptr
}
}
/// Reallocates the given area of memory in individual bytes.
pub fn realloc_bytes(ptr: raw_ptr, count: u64, new_count: u64) -> raw_ptr {
if new_count > count {
let new_ptr = alloc_bytes(new_count);
if count > 0 {
ptr.copy_bytes_to(new_ptr, count);
}
new_ptr
} else {
ptr
}
}