diff --git a/src/memory_manager.rs b/src/memory_manager.rs index 90fcbb7228..38dc26d046 100644 --- a/src/memory_manager.rs +++ b/src/memory_manager.rs @@ -364,6 +364,20 @@ pub fn counted_malloc(mmtk: &MMTK, size: usize) -> Address { crate::util::malloc::counted_malloc(mmtk, size) } +pub fn posix_memalign(ptr: *mut Address, align: usize, size: usize) -> i32 { + crate::util::malloc::posix_memalign(ptr, align, size) +} + +#[cfg(feature = "malloc_counted_size")] +pub fn counted_posix_memalign( + mmtk: &MMTK, + ptr: *mut Address, + align: usize, + size: usize, +) -> i32 { + crate::util::malloc::counted_posix_memalign(mmtk, ptr, align, size) +} + /// The standard calloc. pub fn calloc(num: usize, size: usize) -> Address { crate::util::malloc::calloc(num, size) @@ -385,13 +399,13 @@ pub fn realloc(addr: Address, size: usize) -> Address { /// Thus the method requires a reference to an MMTk instance, and the size of the existing memory that will be reallocated. /// The `addr` in the arguments must be an address that is earlier returned from MMTk's `malloc()`, `calloc()` or `realloc()`. #[cfg(feature = "malloc_counted_size")] -pub fn realloc_with_old_size( +pub fn counted_realloc_with_old_size( mmtk: &MMTK, addr: Address, size: usize, old_size: usize, ) -> Address { - crate::util::malloc::realloc_with_old_size(mmtk, addr, size, old_size) + crate::util::malloc::counted_realloc_with_old_size(mmtk, addr, size, old_size) } /// The standard free. @@ -404,8 +418,13 @@ pub fn free(addr: Address) { /// Thus the method requires a reference to an MMTk instance, and the size of the memory to free. /// The `addr` in the arguments must be an address that is earlier returned from MMTk's `malloc()`, `calloc()` or `realloc()`. #[cfg(feature = "malloc_counted_size")] -pub fn free_with_size(mmtk: &MMTK, addr: Address, old_size: usize) { - crate::util::malloc::free_with_size(mmtk, addr, old_size) +pub fn counted_free_with_size(mmtk: &MMTK, addr: Address, old_size: usize) { + crate::util::malloc::counted_free_with_size(mmtk, addr, old_size) +} + +#[cfg(feature = "malloc_counted_size")] +pub fn counted_free(mmtk: &MMTK, addr: Address) { + crate::util::malloc::counted_free(mmtk, addr) } /// Poll for GC. MMTk will decide if a GC is needed. If so, this call will block diff --git a/src/util/malloc/mod.rs b/src/util/malloc/mod.rs index 3d538caedc..e5e48f984c 100644 --- a/src/util/malloc/mod.rs +++ b/src/util/malloc/mod.rs @@ -32,6 +32,24 @@ pub fn counted_malloc(mmtk: &MMTK, size: usize) -> Address { res } +pub fn posix_memalign(ptr: *mut Address, align: usize, size: usize) -> i32 { + unsafe { self::library::posix_memalign(ptr as _, align, size) } +} + +#[cfg(feature = "malloc_counted_size")] +pub fn counted_posix_memalign( + mmtk: &MMTK, + ptr: *mut Address, + align: usize, + size: usize, +) -> i32 { + let ret = unsafe { self::library::posix_memalign(ptr as _, align, size) }; + if ret == 0 { + mmtk.plan.base().increase_malloc_bytes_by(size); + } + ret +} + pub fn calloc(num: usize, size: usize) -> Address { Address::from_mut_ptr(unsafe { self::library::calloc(num, size) }) } @@ -50,7 +68,7 @@ pub fn realloc(addr: Address, size: usize) -> Address { } #[cfg(feature = "malloc_counted_size")] -pub fn realloc_with_old_size( +pub fn counted_realloc_with_old_size( mmtk: &MMTK, addr: Address, size: usize, @@ -73,9 +91,18 @@ pub fn free(addr: Address) { } #[cfg(feature = "malloc_counted_size")] -pub fn free_with_size(mmtk: &MMTK, addr: Address, old_size: usize) { +pub fn counted_free_with_size(mmtk: &MMTK, addr: Address, old_size: usize) { free(addr); if !addr.is_zero() { mmtk.plan.base().decrease_malloc_bytes_by(old_size); } } + +#[cfg(feature = "malloc_counted_size")] +pub fn counted_free(mmtk: &MMTK, addr: Address) { + let sz = unsafe { self::library::malloc_usable_size(addr.to_mut_ptr()) }; + free(addr); + if !addr.is_zero() { + mmtk.plan.base().decrease_malloc_bytes_by(sz); + } +}