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

Add __rust_unsized_deallocate for C libraries to hook into. #31976

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
5 changes: 5 additions & 0 deletions src/doc/book/custom-allocators.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ pub extern fn __rust_allocate(size: usize, _align: usize) -> *mut u8 {
unsafe { libc::malloc(size as libc::size_t) as *mut u8 }
}

#[no_mangle]
pub extern fn __rust_unsized_deallocate(ptr: *mut u8, _align: usize) {
unsafe { libc::free(ptr as *mut libc::c_void) }
}

#[no_mangle]
pub extern fn __rust_deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
unsafe { libc::free(ptr as *mut libc::c_void) }
Expand Down
9 changes: 9 additions & 0 deletions src/liballoc_jemalloc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ extern {
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
link_name = "je_xallocx")]
fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
link_name = "je_dallocx")]
fn dallocx(ptr: *mut c_void, flags: c_int);
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios"),
link_name = "je_sdallocx")]
fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
Expand Down Expand Up @@ -114,6 +117,12 @@ pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
unsafe { xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
}

#[no_mangle]
pub extern "C" fn __rust_unsized_deallocate(ptr: *mut u8, align: usize) {
let flags = align_to_flags(align);
unsafe { dallocx(ptr as *mut c_void, flags) }
}

#[no_mangle]
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
let flags = align_to_flags(align);
Expand Down
5 changes: 5 additions & 0 deletions src/liballoc_system/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
unsafe { imp::allocate(size, align) }
}

#[no_mangle]
pub extern "C" fn __rust_unsized_deallocate(ptr: *mut u8, align: usize) {
unsafe { imp::deallocate(ptr, 0, align) }
}

#[no_mangle]
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
unsafe { imp::deallocate(ptr, old_size, align) }
Expand Down
8 changes: 8 additions & 0 deletions src/test/auxiliary/allocator-dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
}
}

#[no_mangle]
pub extern fn __rust_unsized_deallocate(ptr: *mut u8, align: usize) {
unsafe {
HITS += 1;
libc::free(ptr as *mut _)
}
}

#[no_mangle]
pub extern fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
unsafe {
Expand Down