Skip to content

Commit 0af6124

Browse files
authored
Simplify allocation APIs (#101)
1 parent 0d5d39b commit 0af6124

File tree

3 files changed

+44
-73
lines changed

3 files changed

+44
-73
lines changed

provider/src/alloc.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
1-
use std::alloc::{alloc, dealloc, Layout};
2-
use std::ptr::copy_nonoverlapping;
1+
use std::alloc::{alloc, Layout};
32

43
const ZERO_SIZE_ALLOCATION_PTR: *mut u8 = 1 as _;
54

65
// Allocation functions
7-
#[export_name = "shopify_function_realloc"]
8-
pub unsafe extern "C" fn shopify_function_realloc(
9-
original_ptr: *mut u8,
10-
original_size: usize,
11-
alignment: usize,
12-
new_size: usize,
13-
) -> *mut std::ffi::c_void {
14-
assert!(new_size >= original_size);
15-
16-
let new_mem = match new_size {
6+
#[export_name = "_shopify_function_alloc"]
7+
pub unsafe extern "C" fn shopify_function_alloc(size: usize) -> *mut std::ffi::c_void {
8+
let new_mem = match size {
179
0 => ZERO_SIZE_ALLOCATION_PTR,
18-
// this call to `alloc` is safe since `new_size` must be > 0
19-
_ => alloc(Layout::from_size_align(new_size, alignment).unwrap()),
10+
// this call to `alloc` is safe since `size` must be > 0
11+
_ => alloc(Layout::from_size_align(size, 1).unwrap()),
2012
};
21-
22-
if !original_ptr.is_null() && original_size != 0 {
23-
copy_nonoverlapping(original_ptr, new_mem, original_size);
24-
shopify_function_free(original_ptr, original_size, alignment);
25-
}
2613
new_mem as _
2714
}
28-
29-
#[export_name = "shopify_function_free"]
30-
pub unsafe extern "C" fn shopify_function_free(ptr: *mut u8, size: usize, alignment: usize) {
31-
if size > 0 {
32-
dealloc(ptr, Layout::from_size_align(size, alignment).unwrap())
33-
};
34-
}

trampoline/src/lib.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub struct TrampolineCodegen {
9393
provider_memory_id: OnceCell<MemoryId>,
9494
memcpy_to_guest: OnceCell<FunctionId>,
9595
memcpy_to_provider: OnceCell<FunctionId>,
96-
imported_shopify_function_realloc: OnceCell<FunctionId>,
96+
imported_shopify_function_alloc: OnceCell<FunctionId>,
9797
alloc: OnceCell<FunctionId>,
9898
}
9999

@@ -107,7 +107,7 @@ impl TrampolineCodegen {
107107
provider_memory_id: OnceCell::new(),
108108
memcpy_to_guest: OnceCell::new(),
109109
memcpy_to_provider: OnceCell::new(),
110-
imported_shopify_function_realloc: OnceCell::new(),
110+
imported_shopify_function_alloc: OnceCell::new(),
111111
alloc: OnceCell::new(),
112112
})
113113
}
@@ -198,25 +198,23 @@ impl TrampolineCodegen {
198198
})
199199
}
200200

201-
fn emit_shopify_function_realloc_import(&mut self) -> FunctionId {
202-
*self.imported_shopify_function_realloc.get_or_init(|| {
203-
let shopify_function_realloc_type = self.module.types.add(
204-
&[ValType::I32, ValType::I32, ValType::I32, ValType::I32],
205-
&[ValType::I32],
206-
);
201+
fn emit_shopify_function_alloc_import(&mut self) -> FunctionId {
202+
*self.imported_shopify_function_alloc.get_or_init(|| {
203+
let shopify_function_alloc_type =
204+
self.module.types.add(&[ValType::I32], &[ValType::I32]);
207205

208-
let (imported_shopify_function_realloc, _) = self.module.add_import_func(
206+
let (imported_shopify_function_alloc, _) = self.module.add_import_func(
209207
PROVIDER_MODULE_NAME,
210-
"shopify_function_realloc",
211-
shopify_function_realloc_type,
208+
"_shopify_function_alloc",
209+
shopify_function_alloc_type,
212210
);
213211

214-
imported_shopify_function_realloc
212+
imported_shopify_function_alloc
215213
})
216214
}
217215

218216
fn emit_alloc(&mut self) -> FunctionId {
219-
let imported_shopify_function_realloc = self.emit_shopify_function_realloc_import();
217+
let imported_shopify_function_alloc = self.emit_shopify_function_alloc_import();
220218

221219
*self.alloc.get_or_init(|| {
222220
let mut alloc =
@@ -226,11 +224,8 @@ impl TrampolineCodegen {
226224

227225
alloc
228226
.func_body()
229-
.i32_const(0)
230-
.i32_const(0)
231-
.i32_const(1)
232227
.local_get(size)
233-
.call(imported_shopify_function_realloc);
228+
.call(imported_shopify_function_alloc);
234229

235230
alloc.finish(vec![size], &mut self.module.funcs)
236231
})
@@ -607,7 +602,7 @@ impl TrampolineCodegen {
607602
&& (!IMPORTS.iter().any(|(orig_name, new_name)| {
608603
*orig_name == import.name || *new_name == import.name
609604
}) && import.name != "_shopify_function_input_get_utf8_str_addr"
610-
&& import.name != "shopify_function_realloc"
605+
&& import.name != "_shopify_function_alloc"
611606
&& import.name != "memory")
612607
}) {
613608
bail!(

trampoline/src/snapshots/shopify_function_trampoline__test__disassemble_trampoline@consumer.wat.snap

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,28 @@ input_file: trampoline/src/test_data/consumer.wat
1111
(type (;4;) (func (param i32 i32)))
1212
(type (;5;) (func (param i32 i32) (result i32)))
1313
(type (;6;) (func (param i32 i32 i32)))
14-
(type (;7;) (func (param i32 i32 i32 i32) (result i32)))
15-
(type (;8;) (func (param i64) (result i32)))
16-
(type (;9;) (func (param i64 i32) (result i64)))
17-
(type (;10;) (func (param i64 i32 i32) (result i64)))
18-
(type (;11;) (func (param f64) (result i32)))
14+
(type (;7;) (func (param i64) (result i32)))
15+
(type (;8;) (func (param i64 i32) (result i64)))
16+
(type (;9;) (func (param i64 i32 i32) (result i64)))
17+
(type (;10;) (func (param f64) (result i32)))
1918
(import "shopify_function_v2" "_shopify_function_input_get" (func (;0;) (type 1)))
20-
(import "shopify_function_v2" "_shopify_function_input_get_interned_obj_prop" (func (;1;) (type 9)))
21-
(import "shopify_function_v2" "_shopify_function_input_get_at_index" (func (;2;) (type 9)))
22-
(import "shopify_function_v2" "_shopify_function_input_get_obj_key_at_index" (func (;3;) (type 9)))
23-
(import "shopify_function_v2" "_shopify_function_input_get_val_len" (func (;4;) (type 8)))
19+
(import "shopify_function_v2" "_shopify_function_input_get_interned_obj_prop" (func (;1;) (type 8)))
20+
(import "shopify_function_v2" "_shopify_function_input_get_at_index" (func (;2;) (type 8)))
21+
(import "shopify_function_v2" "_shopify_function_input_get_obj_key_at_index" (func (;3;) (type 8)))
22+
(import "shopify_function_v2" "_shopify_function_input_get_val_len" (func (;4;) (type 7)))
2423
(import "shopify_function_v2" "_shopify_function_output_new_bool" (func (;5;) (type 2)))
2524
(import "shopify_function_v2" "_shopify_function_output_new_null" (func (;6;) (type 0)))
2625
(import "shopify_function_v2" "_shopify_function_output_new_i32" (func (;7;) (type 2)))
27-
(import "shopify_function_v2" "_shopify_function_output_new_f64" (func (;8;) (type 11)))
26+
(import "shopify_function_v2" "_shopify_function_output_new_f64" (func (;8;) (type 10)))
2827
(import "shopify_function_v2" "_shopify_function_output_new_object" (func (;9;) (type 2)))
2928
(import "shopify_function_v2" "_shopify_function_output_finish_object" (func (;10;) (type 0)))
3029
(import "shopify_function_v2" "_shopify_function_output_new_array" (func (;11;) (type 2)))
3130
(import "shopify_function_v2" "_shopify_function_output_finish_array" (func (;12;) (type 0)))
3231
(import "shopify_function_v2" "_shopify_function_output_new_interned_utf8_str" (func (;13;) (type 2)))
3332
(import "shopify_function_v2" "_shopify_function_input_get_utf8_str_addr" (func (;14;) (type 2)))
3433
(import "shopify_function_v2" "memory" (memory (;0;) 1))
35-
(import "shopify_function_v2" "_shopify_function_input_get_obj_prop" (func (;15;) (type 10)))
36-
(import "shopify_function_v2" "shopify_function_realloc" (func (;16;) (type 7)))
34+
(import "shopify_function_v2" "_shopify_function_input_get_obj_prop" (func (;15;) (type 9)))
35+
(import "shopify_function_v2" "_shopify_function_alloc" (func (;16;) (type 2)))
3736
(import "shopify_function_v2" "_shopify_function_output_new_utf8_str" (func (;17;) (type 3)))
3837
(import "shopify_function_v2" "_shopify_function_intern_utf8_str" (func (;18;) (type 3)))
3938
(import "shopify_function_v2" "_shopify_function_log_new_utf8_str" (func (;19;) (type 2)))
@@ -58,7 +57,7 @@ input_file: trampoline/src/test_data/consumer.wat
5857
i32.add
5958
local.tee 0
6059
local.get 5
61-
call 27
60+
call 26
6261
local.get 5
6362
local.get 1
6463
i32.ne
@@ -74,7 +73,7 @@ input_file: trampoline/src/test_data/consumer.wat
7473
local.get 5
7574
i32.add
7675
local.get 7
77-
call 27
76+
call 26
7877
else
7978
end
8079
)
@@ -90,7 +89,7 @@ input_file: trampoline/src/test_data/consumer.wat
9089
i32.wrap_i64
9190
local.get 0
9291
local.get 1
93-
call 27
92+
call 26
9493
)
9594
(func (;22;) (type 5) (param i32 i32) (result i32)
9695
(local i64)
@@ -104,16 +103,16 @@ input_file: trampoline/src/test_data/consumer.wat
104103
i32.wrap_i64
105104
local.get 0
106105
local.get 1
107-
call 27
106+
call 26
108107
)
109-
(func (;23;) (type 10) (param i64 i32 i32) (result i64)
108+
(func (;23;) (type 9) (param i64 i32 i32) (result i64)
110109
(local i32)
111110
local.get 2
112-
call 25
111+
call 27
113112
local.tee 3
114113
local.get 1
115114
local.get 2
116-
call 27
115+
call 26
117116
local.get 0
118117
local.get 3
119118
local.get 2
@@ -124,27 +123,24 @@ input_file: trampoline/src/test_data/consumer.wat
124123
local.get 0
125124
call 14
126125
local.get 2
127-
call 26
128-
)
129-
(func (;25;) (type 2) (param i32) (result i32)
130-
i32.const 0
131-
i32.const 0
132-
i32.const 1
133-
local.get 0
134-
call 16
126+
call 25
135127
)
136-
(func (;26;) (type 6) (param i32 i32 i32)
128+
(func (;25;) (type 6) (param i32 i32 i32)
137129
local.get 0
138130
local.get 1
139131
local.get 2
140132
memory.copy 1 0
141133
)
142-
(func (;27;) (type 6) (param i32 i32 i32)
134+
(func (;26;) (type 6) (param i32 i32 i32)
143135
local.get 0
144136
local.get 1
145137
local.get 2
146138
memory.copy 0 1
147139
)
140+
(func (;27;) (type 2) (param i32) (result i32)
141+
local.get 0
142+
call 16
143+
)
148144
(@producers
149145
(processed-by "walrus" "0.23.3")
150146
)

0 commit comments

Comments
 (0)