Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: free memory blocks when they're not needed #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ pub fn get_memory(key: impl AsRef<str>) -> Result<Option<Memory>, Error> {
/// let my_config = config::get("my_config")?.unwrap_or(0u32);
/// ```
pub fn get(key: impl AsRef<str>) -> Result<Option<String>, Error> {
Ok(get_memory(key)?.map(|x| x.to_string().expect("Config value is not a valid string")))
Ok(get_memory(key)?.map(|x| {
let s = x.to_string().expect("Config value is not a valid string");
x.free();
s
}))
}
2 changes: 2 additions & 0 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub fn request<T: ToMemory>(
};
let data = body.as_ref().map(|x| x.offset()).unwrap_or(0);
let offs = unsafe { extism::http_request(req.offset(), data) };
req.free();
body.map(|x| x.free());
let status = unsafe { extism::http_status_code() };
let len = unsafe { extism::length_unsafe(offs) };
Ok(HttpResponse {
Expand Down
3 changes: 2 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ macro_rules! log {
($lvl:expr, $($arg:tt)+) => {{
let fmt = format!($($arg)+);
let memory = $crate::Memory::from_bytes(&fmt).unwrap();
memory.log($lvl)
memory.log($lvl);
memory.free()
}}
}

Expand Down
9 changes: 8 additions & 1 deletion src/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ pub fn get_memory(key: impl AsRef<str>) -> Result<Option<Memory>, Error> {
/// let my_var = var::get("my_var")?.unwrap_or(0u32);
/// ```
pub fn get<T: FromBytesOwned>(key: impl AsRef<str>) -> Result<Option<T>, Error> {
match get_memory(key)?.map(|x| x.to_vec()) {
match get_memory(key)?.map(|x| {
let res = x.to_vec();
x.free();
res
}) {
Some(v) => Ok(Some(T::from_bytes(&v)?)),
None => Ok(None),
}
Expand All @@ -56,6 +60,8 @@ pub fn set(key: impl AsRef<str>, val: impl ToMemory) -> Result<(), Error> {
let val = val.to_memory()?;
let key = Memory::from_bytes(key.as_ref().as_bytes())?;
unsafe { extism::var_set(key.offset(), val.offset()) }
key.free();
val.free();
Ok(())
}

Expand All @@ -74,5 +80,6 @@ pub fn set(key: impl AsRef<str>, val: impl ToMemory) -> Result<(), Error> {
pub fn remove(key: impl AsRef<str>) -> Result<(), Error> {
let key = Memory::from_bytes(key.as_ref().as_bytes())?;
unsafe { extism::var_set(key.offset(), 0) };
key.free();
Ok(())
}
Binary file modified test/code.wasm
Binary file not shown.
Binary file modified test/host_function.wasm
Binary file not shown.
Binary file modified test/http.wasm
Binary file not shown.
Loading