From 72862a7eb44b85706e2135531347f1f953743ef2 Mon Sep 17 00:00:00 2001 From: Michael Wu Date: Mon, 29 Feb 2016 14:50:53 -0500 Subject: [PATCH] Add __rust_unsized_deallocate for C libraries to hook into. __rust_deallocate requires a size to be passed in, especially when jemalloc is used. Most C/C++ libraries don't use sized deallocation, so this allows non-rust code to hook into Rust's allocator without changing every free() call. --- src/doc/book/custom-allocators.md | 5 +++++ src/liballoc_jemalloc/lib.rs | 9 +++++++++ src/liballoc_system/lib.rs | 5 +++++ src/test/auxiliary/allocator-dummy.rs | 8 ++++++++ 4 files changed, 27 insertions(+) diff --git a/src/doc/book/custom-allocators.md b/src/doc/book/custom-allocators.md index d69ef6cf7e83a..1b6b925648843 100644 --- a/src/doc/book/custom-allocators.md +++ b/src/doc/book/custom-allocators.md @@ -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) } diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs index c96d303e6bb64..e32b7912ddae4 100644 --- a/src/liballoc_jemalloc/lib.rs +++ b/src/liballoc_jemalloc/lib.rs @@ -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); @@ -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); diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs index 6a62e00d31169..0a52060fa1648 100644 --- a/src/liballoc_system/lib.rs +++ b/src/liballoc_system/lib.rs @@ -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) } diff --git a/src/test/auxiliary/allocator-dummy.rs b/src/test/auxiliary/allocator-dummy.rs index a1d21db8f4d5d..d5eb87dd3ecac 100644 --- a/src/test/auxiliary/allocator-dummy.rs +++ b/src/test/auxiliary/allocator-dummy.rs @@ -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 {