Skip to content

Commit

Permalink
chore: add bindings to length_unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Jan 3, 2024
1 parent 44370c5 commit 919be2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/extism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern "C" {
pub fn input_load_u8(offs: u64) -> u8;
pub fn input_load_u64(offs: u64) -> u64;
pub fn length(offs: u64) -> u64;
pub fn length_unsafe(offs: u64) -> u64;
pub fn alloc(length: u64) -> u64;
pub fn free(offs: u64);
pub fn output_set(offs: u64, length: u64);
Expand Down Expand Up @@ -37,7 +38,7 @@ pub unsafe fn load(offs: u64, data: &mut [u8]) {

let mut_ptr = data.as_mut_ptr() as *mut u64;
for chunk_idx in 0..chunk_count {
let x = load_u64(offs + (chunk_idx<<3) as u64);
let x = load_u64(offs + (chunk_idx << 3) as u64);
mut_ptr.add(chunk_idx).write(x);
}

Expand Down Expand Up @@ -89,10 +90,7 @@ pub unsafe fn store(offs: u64, data: &[u8]) {

let ptr = data.as_ptr() as *const u64;
for chunk_idx in 0..chunk_count {
store_u64(
offs + (chunk_idx << 3) as u64,
ptr.add(chunk_idx).read(),
);
store_u64(offs + (chunk_idx << 3) as u64, ptr.add(chunk_idx).read());
}

let remainder = len & 7;
Expand Down
7 changes: 7 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ pub mod internal {
data
}

/// Get the length of the memory handle stored at the given offset, this will return 0 if called on a non-handle pointer
pub fn memory_length(offs: u64) -> u64 {
unsafe { extism::length(offs) }
}

/// Get the length of the memory handle stored at the given offset, this may return garbage if called on a non-handle pointer
pub fn memory_length_unsafe(offs: u64) -> u64 {
unsafe { extism::length_unsafe(offs) }
}

/// Load data from memory into a `u8` slice
pub fn load(handle: MemoryHandle, mut buf: impl AsMut<[u8]>) {
let buf = buf.as_mut();
Expand Down

0 comments on commit 919be2f

Please sign in to comment.