From 681773f2919ddf8af0e2ff3474a443df6a15a5e2 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Mon, 10 Feb 2020 13:36:49 -0800 Subject: [PATCH] scudo: Instead of exporting a pointer to the allocator, export the allocator directly. NFCI. This lets us remove two pointer indirections (one by removing the pointer, and another by making the AllocatorPtr declaration hidden) in the C++ wrappers. Differential Revision: https://reviews.llvm.org/D74356 --- .../lib/scudo/standalone/internal_defs.h | 1 + .../lib/scudo/standalone/wrappers_c.cpp | 8 +-- .../scudo/standalone/wrappers_c_bionic.cpp | 10 --- .../lib/scudo/standalone/wrappers_cpp.cpp | 62 +++++++++---------- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/compiler-rt/lib/scudo/standalone/internal_defs.h b/compiler-rt/lib/scudo/standalone/internal_defs.h index 8f6a89ecba737..c61f8e6c71b86 100644 --- a/compiler-rt/lib/scudo/standalone/internal_defs.h +++ b/compiler-rt/lib/scudo/standalone/internal_defs.h @@ -29,6 +29,7 @@ // Attributes & builtins related macros. #define INTERFACE __attribute__((visibility("default"))) +#define HIDDEN __attribute__((visibility("hidden"))) #define WEAK __attribute__((weak)) #define ALWAYS_INLINE inline __attribute__((always_inline)) #define ALIAS(X) __attribute__((alias(X))) diff --git a/compiler-rt/lib/scudo/standalone/wrappers_c.cpp b/compiler-rt/lib/scudo/standalone/wrappers_c.cpp index 93a666c4d61e3..098cc089a1cab 100644 --- a/compiler-rt/lib/scudo/standalone/wrappers_c.cpp +++ b/compiler-rt/lib/scudo/standalone/wrappers_c.cpp @@ -22,13 +22,11 @@ #define SCUDO_ALLOCATOR Allocator extern "C" void SCUDO_PREFIX(malloc_postinit)(); -static scudo::Allocator - SCUDO_ALLOCATOR; -// Pointer to the static allocator so that the C++ wrappers can access it. + +// Export the static allocator so that the C++ wrappers can access it. // Technically we could have a completely separated heap for C & C++ but in // reality the amount of cross pollination between the two is staggering. -scudo::Allocator * - CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR; +scudo::Allocator SCUDO_ALLOCATOR; #include "wrappers_c.inc" diff --git a/compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp b/compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp index cab565b0664d0..7a012a23bcf1b 100644 --- a/compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp +++ b/compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp @@ -25,11 +25,6 @@ extern "C" void SCUDO_PREFIX(malloc_postinit)(); static scudo::Allocator SCUDO_ALLOCATOR; -// Pointer to the static allocator so that the C++ wrappers can access it. -// Technically we could have a completely separated heap for C & C++ but in -// reality the amount of cross pollination between the two is staggering. -scudo::Allocator * - CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR; #include "wrappers_c.inc" @@ -44,11 +39,6 @@ extern "C" void SCUDO_PREFIX(malloc_postinit)(); static scudo::Allocator SCUDO_ALLOCATOR; -// Pointer to the static allocator so that the C++ wrappers can access it. -// Technically we could have a completely separated heap for C & C++ but in -// reality the amount of cross pollination between the two is staggering. -scudo::Allocator * - CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR; #include "wrappers_c.inc" diff --git a/compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp b/compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp index 1da5385c77890..adb1041181232 100644 --- a/compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp +++ b/compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp @@ -16,7 +16,7 @@ #include extern "C" void malloc_postinit(); -extern scudo::Allocator *AllocatorPtr; +extern HIDDEN scudo::Allocator Allocator; namespace std { struct nothrow_t {}; @@ -24,85 +24,85 @@ enum class align_val_t : size_t {}; } // namespace std INTERFACE WEAK void *operator new(size_t size) { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::New); + return Allocator.allocate(size, scudo::Chunk::Origin::New); } INTERFACE WEAK void *operator new[](size_t size) { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::NewArray); + return Allocator.allocate(size, scudo::Chunk::Origin::NewArray); } INTERFACE WEAK void *operator new(size_t size, std::nothrow_t const &) NOEXCEPT { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::New); + return Allocator.allocate(size, scudo::Chunk::Origin::New); } INTERFACE WEAK void *operator new[](size_t size, std::nothrow_t const &) NOEXCEPT { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::NewArray); + return Allocator.allocate(size, scudo::Chunk::Origin::NewArray); } INTERFACE WEAK void *operator new(size_t size, std::align_val_t align) { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::New, - static_cast(align)); + return Allocator.allocate(size, scudo::Chunk::Origin::New, + static_cast(align)); } INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::NewArray, - static_cast(align)); + return Allocator.allocate(size, scudo::Chunk::Origin::NewArray, + static_cast(align)); } INTERFACE WEAK void *operator new(size_t size, std::align_val_t align, std::nothrow_t const &) NOEXCEPT { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::New, - static_cast(align)); + return Allocator.allocate(size, scudo::Chunk::Origin::New, + static_cast(align)); } INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align, std::nothrow_t const &) NOEXCEPT { - return AllocatorPtr->allocate(size, scudo::Chunk::Origin::NewArray, - static_cast(align)); + return Allocator.allocate(size, scudo::Chunk::Origin::NewArray, + static_cast(align)); } INTERFACE WEAK void operator delete(void *ptr)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New); } INTERFACE WEAK void operator delete[](void *ptr) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray); } INTERFACE WEAK void operator delete(void *ptr, std::nothrow_t const &)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New); } INTERFACE WEAK void operator delete[](void *ptr, std::nothrow_t const &) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray); } INTERFACE WEAK void operator delete(void *ptr, size_t size)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New, size); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size); } INTERFACE WEAK void operator delete[](void *ptr, size_t size) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray, size); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size); } INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New, 0, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0, + static_cast(align)); } INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, + static_cast(align)); } INTERFACE WEAK void operator delete(void *ptr, std::align_val_t align, std::nothrow_t const &)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New, 0, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0, + static_cast(align)); } INTERFACE WEAK void operator delete[](void *ptr, std::align_val_t align, std::nothrow_t const &) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0, + static_cast(align)); } INTERFACE WEAK void operator delete(void *ptr, size_t size, std::align_val_t align)NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::New, size, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size, + static_cast(align)); } INTERFACE WEAK void operator delete[](void *ptr, size_t size, std::align_val_t align) NOEXCEPT { - AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray, size, - static_cast(align)); + Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size, + static_cast(align)); } #endif // !SCUDO_ANDROID || !_BIONIC