diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 4c80676..f42a18a 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -283,7 +283,7 @@ impl MemoryRoot { // Bump the position by the size of the actual data + the size of the MemoryBlock structure self.position.fetch_add( - length + core::mem::size_of::() as u64 - 1, + length + core::mem::size_of::() as u64, Ordering::SeqCst, ); @@ -316,9 +316,7 @@ impl MemoryBlock { /// is calculated based on metadata provided by the current block #[inline] pub unsafe fn next_ptr(&mut self) -> *mut MemoryBlock { - self.data - .as_mut_ptr() - .add(self.size + core::mem::size_of::()) as *mut MemoryBlock + self.data.as_mut_ptr().add(self.size) as *mut MemoryBlock } /// Mark a block as free diff --git a/runtime/src/extism-runtime.wasm b/runtime/src/extism-runtime.wasm index 4a33778..5f3ead8 100755 Binary files a/runtime/src/extism-runtime.wasm and b/runtime/src/extism-runtime.wasm differ diff --git a/runtime/src/tests/kernel.rs b/runtime/src/tests/kernel.rs index 0416bbb..6289cde 100644 --- a/runtime/src/tests/kernel.rs +++ b/runtime/src/tests/kernel.rs @@ -182,17 +182,18 @@ fn test_kernel_allocations() { assert_eq!(extism_length(&mut store, instance, x), 2); extism_free(&mut store, instance, x); - // 64 bytes - let p = extism_alloc(&mut store, instance, 64); - assert!(p > 0); - assert_eq!(extism_length(&mut store, instance, p), 64); - extism_free(&mut store, instance, p); + for i in 0..64 { + let p = extism_alloc(&mut store, instance, 64 - i); + assert!(p > 0); + assert_eq!(extism_length(&mut store, instance, p), 64 - i); + extism_free(&mut store, instance, p); - // 64 bytes, should re-use the last allocation - let q = extism_alloc(&mut store, instance, 64); - assert_eq!(p, q); - assert_eq!(extism_length(&mut store, instance, q), 64); - extism_free(&mut store, instance, q); + // should re-use the last allocation + let q = extism_alloc(&mut store, instance, 64 - i); + assert_eq!(p, q); + assert_eq!(extism_length(&mut store, instance, q), 64 - i); + extism_free(&mut store, instance, q); + } // 512 bytes, test block re-use + splitting let p = extism_alloc(&mut store, instance, 512);