Skip to content

Commit

Permalink
scudo: Instead of exporting a pointer to the allocator, export the al…
Browse files Browse the repository at this point in the history
…locator 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
  • Loading branch information
pcc committed Feb 10, 2020
1 parent 52f2df1 commit 681773f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
1 change: 1 addition & 0 deletions compiler-rt/lib/scudo/standalone/internal_defs.h
Expand Up @@ -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)))
Expand Down
8 changes: 3 additions & 5 deletions compiler-rt/lib/scudo/standalone/wrappers_c.cpp
Expand Up @@ -22,13 +22,11 @@
#define SCUDO_ALLOCATOR Allocator

extern "C" void SCUDO_PREFIX(malloc_postinit)();
static scudo::Allocator<scudo::Config, SCUDO_PREFIX(malloc_postinit)>
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<scudo::Config, SCUDO_PREFIX(malloc_postinit)> *
CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR;
scudo::Allocator<scudo::Config, SCUDO_PREFIX(malloc_postinit)> SCUDO_ALLOCATOR;

#include "wrappers_c.inc"

Expand Down
10 changes: 0 additions & 10 deletions compiler-rt/lib/scudo/standalone/wrappers_c_bionic.cpp
Expand Up @@ -25,11 +25,6 @@
extern "C" void SCUDO_PREFIX(malloc_postinit)();
static scudo::Allocator<scudo::AndroidConfig, SCUDO_PREFIX(malloc_postinit)>
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<scudo::AndroidConfig, SCUDO_PREFIX(malloc_postinit)> *
CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR;

#include "wrappers_c.inc"

Expand All @@ -44,11 +39,6 @@ extern "C" void SCUDO_PREFIX(malloc_postinit)();
static scudo::Allocator<scudo::AndroidSvelteConfig,
SCUDO_PREFIX(malloc_postinit)>
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<scudo::AndroidSvelteConfig, SCUDO_PREFIX(malloc_postinit)> *
CONCATENATE(SCUDO_ALLOCATOR, Ptr) = &SCUDO_ALLOCATOR;

#include "wrappers_c.inc"

Expand Down
62 changes: 31 additions & 31 deletions compiler-rt/lib/scudo/standalone/wrappers_cpp.cpp
Expand Up @@ -16,93 +16,93 @@
#include <stdint.h>

extern "C" void malloc_postinit();
extern scudo::Allocator<scudo::Config, malloc_postinit> *AllocatorPtr;
extern HIDDEN scudo::Allocator<scudo::Config, malloc_postinit> Allocator;

namespace std {
struct nothrow_t {};
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<scudo::uptr>(align));
return Allocator.allocate(size, scudo::Chunk::Origin::New,
static_cast<scudo::uptr>(align));
}
INTERFACE WEAK void *operator new[](size_t size, std::align_val_t align) {
return AllocatorPtr->allocate(size, scudo::Chunk::Origin::NewArray,
static_cast<scudo::uptr>(align));
return Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
return Allocator.allocate(size, scudo::Chunk::Origin::New,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
return Allocator.allocate(size, scudo::Chunk::Origin::NewArray,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
static_cast<scudo::uptr>(align));
}
INTERFACE WEAK void operator delete[](void *ptr,
std::align_val_t align) NOEXCEPT {
AllocatorPtr->deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
static_cast<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::New, 0,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, 0,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::New, size,
static_cast<scudo::uptr>(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<scudo::uptr>(align));
Allocator.deallocate(ptr, scudo::Chunk::Origin::NewArray, size,
static_cast<scudo::uptr>(align));
}

#endif // !SCUDO_ANDROID || !_BIONIC

0 comments on commit 681773f

Please sign in to comment.