diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 6da1c93..0000000 --- a/.clang-format +++ /dev/null @@ -1,4 +0,0 @@ ---- -BasedOnStyle: LLVM -IndentWidth: 4 ---- diff --git a/Cargo.lock b/Cargo.lock index 45baf02..becf772 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,12 +17,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" -[[package]] -name = "cc" -version = "1.0.73" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" - [[package]] name = "cfg-if" version = "1.0.0" @@ -33,7 +27,6 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "fatalloc" version = "0.1.0" dependencies = [ - "cc", "libc", "rlsf", ] diff --git a/Cargo.toml b/Cargo.toml index f26d0cd..d35336b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,9 +12,6 @@ crate-type = ["cdylib"] libc = { version = "0.2.126", default-features = false } rlsf = "0.1.2" -[build-dependencies] -cc = "1.0" - [profile.dev] panic = "abort" diff --git a/build.rs b/build.rs deleted file mode 100644 index ff12c8a..0000000 --- a/build.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - cc::Build::new() - .file("src/ovrride.cpp") - .flag_if_supported("-std=c++17") - .compile("fatalloc_ovrride_cpp"); -} diff --git a/src/ovrride.cpp b/src/ovrride.cpp deleted file mode 100644 index 7338d6b..0000000 --- a/src/ovrride.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include - -#if __cpp_aligned_new >= 201606 -using my_align_val_t = std::align_val_t; -#else -enum class my_align_val_t : std::size_t {}; -#endif - -namespace { -void *new_inner(std::size_t size, my_align_val_t align, bool is_noexcept) { - void *ptr = std::size_t(align) ? aligned_alloc(std::size_t(align), size) - : malloc(size); - if (!ptr && !is_noexcept) { - // TODO: handle failure by throwing an exception - fprintf(stderr, "fatalloc: allocation failed\n"); - abort(); - } - return ptr; -} -} // namespace - -void *operator new(std::size_t size) { - return new_inner(size, my_align_val_t(0), false); -} -void *operator new[](std::size_t size) { - return new_inner(size, my_align_val_t(0), false); -} -void *operator new(std::size_t size, const std::nothrow_t &) noexcept { - return new_inner(size, my_align_val_t(0), true); -} -void *operator new[](std::size_t size, const std::nothrow_t &) noexcept { - return new_inner(size, my_align_val_t(0), true); -} -void operator delete(void *ptr) noexcept { free(ptr); } - -void operator delete[](void *ptr) noexcept { free(ptr); } -void operator delete(void *ptr, const std::nothrow_t &) noexcept { free(ptr); } -void operator delete[](void *ptr, const std::nothrow_t &) noexcept { - free(ptr); -} - -void operator delete(void *ptr, std::size_t) noexcept { free(ptr); } -void operator delete[](void *ptr, std::size_t) noexcept { free(ptr); } - -#if __cpp_aligned_new >= 201606 -void *operator new(std::size_t size, my_align_val_t align) { - return new_inner(size, align, false); -} -void *operator new(std::size_t size, my_align_val_t align, - const std::nothrow_t &) noexcept { - return new_inner(size, align, true); -} -void *operator new[](std::size_t size, my_align_val_t align) { - return new_inner(size, align, false); -} -void *operator new[](std::size_t size, my_align_val_t align, - const std::nothrow_t &) noexcept { - return new_inner(size, align, true); -} -void operator delete(void *ptr, my_align_val_t) noexcept { free(ptr); } -void operator delete(void *ptr, my_align_val_t, - const std::nothrow_t &) noexcept { - free(ptr); -} -void operator delete(void *ptr, std::size_t, my_align_val_t) noexcept { - free(ptr); -} -void operator delete[](void *ptr, my_align_val_t) noexcept { free(ptr); } -void operator delete[](void *ptr, my_align_val_t, - const std::nothrow_t &) noexcept { - free(ptr); -} -void operator delete[](void *ptr, std::size_t, my_align_val_t) noexcept { - free(ptr); -} -#endif diff --git a/src/ovrride.rs b/src/ovrride.rs index dcaf800..4f07d0b 100644 --- a/src/ovrride.rs +++ b/src/ovrride.rs @@ -153,3 +153,147 @@ pub unsafe extern "C" fn free(ptr: *mut c_void) { CAlloc::deallocate(&ALLOC, ptr.cast()); } } + +// TODO: Find a way to define these in a C++ source file and make sure the +// symbols are exported by the final cdylib file +/// `operator delete[](void*, unsigned long, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPvmSt11align_val_t(p: *mut c_void, _: usize, _: usize) { + free(p); +} + +/// `operator delete[](void*, std::align_val_t, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPvSt11align_val_tRKSt9nothrow_t(p: *mut c_void, _: usize, _: &c_void) { + free(p); +} + +/// `operator delete[](void*, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPvSt11align_val_t(p: *mut c_void, _: usize) { + free(p); +} + +/// `operator delete(void*, unsigned long, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPvmSt11align_val_t(p: *mut c_void, _: usize, _: usize) { + free(p); +} + +/// `operator delete(void*, std::align_val_t, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPvSt11align_val_tRKSt9nothrow_t(p: *mut c_void, _: usize, _: usize) { + free(p); +} + +/// `operator delete(void*, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPvSt11align_val_t(p: *mut c_void, _: usize) { + free(p); +} + +/// `operator new[](unsigned long, std::align_val_t, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZnamSt11align_val_tRKSt9nothrow_t( + size: usize, + align: usize, + _: &c_void, +) -> *mut c_void { + cpp_new_impl(size, align, true) +} + +/// `operator new[](unsigned long, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZnamSt11align_val_t(size: usize, align: usize) -> *mut c_void { + cpp_new_impl(size, align, false) +} + +/// `operator new(unsigned long, std::align_val_t, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZnwmSt11align_val_tRKSt9nothrow_t( + size: usize, + align: usize, + _: &c_void, +) -> *mut c_void { + cpp_new_impl(size, align, true) +} + +/// `operator new(unsigned long, std::align_val_t)` +#[no_mangle] +pub unsafe extern "C" fn _ZnwmSt11align_val_t(size: usize, align: usize) -> *mut c_void { + cpp_new_impl(size, align, false) +} + +/// `operator delete[](void*, unsigned long)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPvm(p: *mut c_void, _: usize) { + free(p); +} + +/// `operator delete(void*, unsigned long)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPvm(p: *mut c_void, _: usize) { + free(p); +} + +/// `operator delete[](void*, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPvRKSt9nothrow_t(p: *mut c_void, _: &c_void) { + free(p); +} + +/// `operator delete(void*, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPvRKSt9nothrow_t(p: *mut c_void, _: &c_void) { + free(p); +} + +/// `operator delete[](void*)` +#[no_mangle] +pub unsafe extern "C" fn _ZdaPv(p: *mut c_void) { + free(p); +} + +/// `operator delete(void*)` +#[no_mangle] +pub unsafe extern "C" fn _ZdlPv(p: *mut c_void) { + free(p); +} + +/// `operator new[](unsigned long, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZnamRKSt9nothrow_t(size: usize, _: &c_void) -> *mut c_void { + cpp_new_impl(size, 0, true) +} + +/// `operator new(unsigned long, std::nothrow_t const&)` +#[no_mangle] +pub unsafe extern "C" fn _ZnwmRKSt9nothrow_t(size: usize, _: &c_void) -> *mut c_void { + cpp_new_impl(size, 0, true) +} + +/// `operator new[](unsigned long)` +#[no_mangle] +pub unsafe extern "C" fn _Znam(size: usize) -> *mut c_void { + cpp_new_impl(size, 0, false) +} + +/// `operator new(unsigned long)` +#[no_mangle] +pub unsafe extern "C" fn _Znwm(size: usize) -> *mut c_void { + cpp_new_impl(size, 0, false) +} + +fn cpp_new_impl(size: usize, align: usize, is_noexcept: bool) -> *mut c_void { + let ptr = unsafe { + if align == 0 { + malloc(size) + } else { + aligned_alloc(align, size) + } + }; + if !is_noexcept && ptr.is_null() { + panic!("allocation of size {size} and alignment {align} failed"); + } + ptr +}