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

Expose posix_memalign. Rename counted free/realloc #868

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
27 changes: 23 additions & 4 deletions src/memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,20 @@ pub fn counted_malloc<VM: VMBinding>(mmtk: &MMTK<VM>, 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<VM: VMBinding>(
mmtk: &MMTK<VM>,
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)
Expand All @@ -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<VM: VMBinding>(
pub fn counted_realloc_with_old_size<VM: VMBinding>(
mmtk: &MMTK<VM>,
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.
Expand All @@ -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<VM: VMBinding>(mmtk: &MMTK<VM>, addr: Address, old_size: usize) {
crate::util::malloc::free_with_size(mmtk, addr, old_size)
pub fn counted_free_with_size<VM: VMBinding>(mmtk: &MMTK<VM>, 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<VM: VMBinding>(mmtk: &MMTK<VM>, 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
Expand Down
31 changes: 29 additions & 2 deletions src/util/malloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ pub fn counted_malloc<VM: VMBinding>(mmtk: &MMTK<VM>, 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<VM: VMBinding>(
mmtk: &MMTK<VM>,
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) })
}
Expand All @@ -50,7 +68,7 @@ pub fn realloc(addr: Address, size: usize) -> Address {
}

#[cfg(feature = "malloc_counted_size")]
pub fn realloc_with_old_size<VM: VMBinding>(
pub fn counted_realloc_with_old_size<VM: VMBinding>(
mmtk: &MMTK<VM>,
addr: Address,
size: usize,
Expand All @@ -73,9 +91,18 @@ pub fn free(addr: Address) {
}

#[cfg(feature = "malloc_counted_size")]
pub fn free_with_size<VM: VMBinding>(mmtk: &MMTK<VM>, addr: Address, old_size: usize) {
pub fn counted_free_with_size<VM: VMBinding>(mmtk: &MMTK<VM>, 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<VM: VMBinding>(mmtk: &MMTK<VM>, 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);
}
}