Skip to content

Commit 783a0b4

Browse files
author
zach
authored
fix: free parameters to host functions when using the host_fn macro (#77)
* fix: add ManagedMemory to automatically free host function parameters * chore: bump version to 1.4.0
1 parent a51da4d commit 783a0b4

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "extism-pdk"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
edition = "2021"
55
authors = ["The Extism Authors", "oss@extism.org"]
66
license = "BSD-3-Clause"
@@ -12,9 +12,9 @@ description = "Extism Plug-in Development Kit (PDK) for Rust"
1212
anyhow = "1"
1313
serde = { version = "1", features = ["derive"] }
1414
serde_json = "1"
15-
extism-pdk-derive = { path = "./derive", version = "1.3.0" }
16-
extism-manifest = { version = "1.2.0", optional = true }
17-
extism-convert = { version = "1.2.0", features = ["extism-pdk-path"] }
15+
extism-pdk-derive = { path = "./derive", version = "1.4.0" }
16+
extism-manifest = { version = "1.10.0", optional = true }
17+
extism-convert = { version = "1.10.0", features = ["extism-pdk-path"] }
1818
base64 = "0.22.1"
1919

2020
[features]

derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "extism-pdk-derive"
3-
version = "1.3.0"
3+
version = "1.4.0"
44
edition = "2021"
55
authors = ["The Extism Authors", "oss@extism.org"]
66
license = "BSD-3-Clause"

derive/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,9 @@ pub fn host_fn(
296296
match &*t.pat {
297297
syn::Pat::Ident(i) => {
298298
into_inputs
299-
.push(quote!(extism_pdk::ToMemory::to_memory(&&#i)?.offset()));
299+
.push(quote!(
300+
extism_pdk::ManagedMemory::from(extism_pdk::ToMemory::to_memory(&&#i)?).offset()
301+
));
300302
}
301303
_ => panic!("invalid host function argument"),
302304
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use anyhow::Error;
2323
pub use extism_convert::*;
2424
pub use extism_convert::{FromBytes, FromBytesOwned, ToBytes};
2525
pub use extism_pdk_derive::{host_fn, plugin_fn, shared_fn};
26-
pub use memory::{Memory, MemoryPointer};
26+
pub use memory::{ManagedMemory, Memory, MemoryPointer};
2727
pub use to_memory::ToMemory;
2828

2929
#[cfg(feature = "http")]

src/memory.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::*;
22

33
pub struct Memory(pub MemoryHandle);
44

5+
pub struct ManagedMemory(pub Memory);
6+
57
pub mod internal {
68
use super::*;
79

@@ -195,3 +197,41 @@ impl<T: FromBytesOwned> MemoryPointer<T> {
195197
}
196198
}
197199
}
200+
201+
impl Drop for ManagedMemory {
202+
fn drop(&mut self) {
203+
internal::memory_free((self.0).0)
204+
}
205+
}
206+
207+
impl ManagedMemory {
208+
pub fn new(mem: Memory) -> Self {
209+
ManagedMemory(mem)
210+
}
211+
212+
pub fn offset(&self) -> u64 {
213+
self.0.offset()
214+
}
215+
216+
pub fn len(&self) -> usize {
217+
self.0.len()
218+
}
219+
}
220+
221+
impl From<Memory> for ManagedMemory {
222+
fn from(value: Memory) -> Self {
223+
ManagedMemory(value)
224+
}
225+
}
226+
227+
impl AsRef<Memory> for ManagedMemory {
228+
fn as_ref(&self) -> &Memory {
229+
&self.0
230+
}
231+
}
232+
233+
impl AsMut<Memory> for ManagedMemory {
234+
fn as_mut(&mut self) -> &mut Memory {
235+
&mut self.0
236+
}
237+
}

0 commit comments

Comments
 (0)