-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move allocation functions into javy lib (#461)
* Move allocation functions into javy lib * Update crates/javy/src/alloc.rs Co-authored-by: Saúl Cabrera <saulecabrera@gmail.com> * Add comment about cfg_attr usage * teh -> the --------- Co-authored-by: Saúl Cabrera <saulecabrera@gmail.com>
- Loading branch information
1 parent
234544f
commit 5c0ac2d
Showing
12 changed files
with
83 additions
and
105 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::alloc::{alloc, dealloc, Layout}; | ||
use std::ptr::copy_nonoverlapping; | ||
|
||
// Unlike C's realloc, zero-length allocations need not have | ||
// unique addresses, so a zero-length allocation may be passed | ||
// in and also requested, but it's ok to return anything that's | ||
// non-zero to indicate success. | ||
const ZERO_SIZE_ALLOCATION_PTR: *mut u8 = 1 as _; | ||
|
||
// For canonical_abi_realloc and canonical_abi_free, we want the functions to be available for use | ||
// by other crates, whether or not the `export_alloc_fns` feature is enabled. When the | ||
// `export_alloc_fns` feature is enabled, we also want to export the two functions from the Wasm | ||
// module. To do that, we apply an `export_name` attribute when the `export_alloc_fns` feature is | ||
// enabled. The `export_name` attribute is what causes the functions to be exported from the Wasm | ||
// module as opposed to just exported for use by other crates. | ||
|
||
/// 1. Allocate memory of new_size with alignment. | ||
/// 2. If original_ptr != 0 | ||
/// a. copy min(new_size, original_size) bytes from original_ptr to new memory | ||
/// b. de-allocate original_ptr | ||
/// 3. Return new memory ptr. | ||
/// | ||
/// # Safety | ||
/// | ||
/// * `original_ptr` must be 0 or a valid pointer | ||
/// * if `original_ptr` is not 0, it must be valid for reads of `original_size` | ||
/// bytes | ||
/// * if `original_ptr` is not 0, it must be properly aligned | ||
/// * if `original_size` is not 0, it must match the `new_size` value provided | ||
/// in the original `canonical_abi_realloc` call that returned `original_ptr` | ||
#[cfg_attr(feature = "export_alloc_fns", export_name = "canonical_abi_realloc")] | ||
pub unsafe extern "C" fn canonical_abi_realloc( | ||
original_ptr: *mut u8, | ||
original_size: usize, | ||
alignment: usize, | ||
new_size: usize, | ||
) -> *mut std::ffi::c_void { | ||
assert!(new_size >= original_size); | ||
|
||
let new_mem = match new_size { | ||
0 => ZERO_SIZE_ALLOCATION_PTR, | ||
// this call to `alloc` is safe since `new_size` must be > 0 | ||
_ => alloc(Layout::from_size_align(new_size, alignment).unwrap()), | ||
}; | ||
|
||
if !original_ptr.is_null() && original_size != 0 { | ||
copy_nonoverlapping(original_ptr, new_mem, original_size); | ||
canonical_abi_free(original_ptr, original_size, alignment); | ||
} | ||
new_mem as _ | ||
} | ||
|
||
/// Frees memory | ||
/// | ||
/// # Safety | ||
/// | ||
/// * `ptr` must denote a block of memory allocated by `canonical_abi_realloc` | ||
/// * `size` and `alignment` must match the values provided in the original | ||
/// `canonical_abi_realloc` call that returned `ptr` | ||
#[cfg_attr(feature = "export_alloc_fns", export_name = "canonical_abi_free")] | ||
pub unsafe extern "C" fn canonical_abi_free(ptr: *mut u8, size: usize, alignment: usize) { | ||
if size > 0 { | ||
dealloc(ptr, Layout::from_size_align(size, alignment).unwrap()) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters