Skip to content

Commit dd7aaa8

Browse files
committed
feat(sys): dealloc memory pointers on receiving results
1 parent 05e7fde commit dd7aaa8

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

crates/sys/src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub fn authorize(args: AuthorizeArgs) -> Result<Executed> {
1515
len: encoded.len(),
1616
};
1717

18-
let output = unsafe { abi::authorize(input) };
19-
codec::decode(output.as_slice()).map_err(Into::into)
18+
let output: Buffer = unsafe { abi::authorize(input) };
19+
codec::decode(&output.to_vec()).map_err(Into::into)
2020
}
2121

2222
/// Run the refine invocation
@@ -28,7 +28,7 @@ pub fn refine(args: RefineArgs) -> Result<Refined> {
2828
};
2929

3030
let output = unsafe { abi::refine(input) };
31-
codec::decode(output.as_slice()).map_err(Into::into)
31+
codec::decode(&output.to_vec()).map_err(Into::into)
3232
}
3333

3434
/// Run the accumulate invocation
@@ -40,7 +40,7 @@ pub fn accumulate(args: AccumulateArgs) -> Result<Accumulated> {
4040
};
4141

4242
let output = unsafe { abi::accumulate(input) };
43-
codec::decode(output.as_slice()).map_err(Into::into)
43+
codec::decode(&output.to_vec()).map_err(Into::into)
4444
}
4545

4646
mod abi {
@@ -53,16 +53,21 @@ mod abi {
5353
pub use {comp_accumulate as accumulate, comp_authorize as authorize, comp_refine as refine};
5454

5555
#[repr(C)]
56-
#[derive(Copy, Clone)]
56+
#[derive(Copy, Clone, Debug)]
5757
pub struct Buffer {
5858
pub ptr: *const u8,
5959
pub len: usize,
6060
}
6161

6262
impl Buffer {
6363
/// Get the buffer as a byte slice
64-
pub fn as_slice(&self) -> &[u8] {
65-
unsafe { std::slice::from_raw_parts(self.ptr, self.len) }
64+
pub fn to_vec(&self) -> Vec<u8> {
65+
let result = unsafe { core::slice::from_raw_parts(self.ptr, self.len).to_vec() };
66+
unsafe {
67+
let layout = std::alloc::Layout::from_size_align(self.len, 1).unwrap();
68+
std::alloc::dealloc(self.ptr as *mut _, layout);
69+
}
70+
result
6671
}
6772
}
6873

crates/testing/src/exec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ impl Jam {
7575
work.auth_code_host,
7676
hex::encode(work.auth_code_hash)
7777
);
78+
7879
spacevm::authorize(AuthorizeArgs {
7980
package: work.clone(),
8081
core_idx: core_idx,

services/nauth/tests/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn test_nauth() {
1515
.expect("failed to send work item");
1616

1717
let result = jam.authorize(&package, 0).expect("failed to authorize");
18-
assert!(result.is_ok());
18+
assert!(result.is_ok(), "{:?}", result);
1919
}

0 commit comments

Comments
 (0)