Skip to content
Open
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
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ if (FOLLY_HAVE_OPENSSL)
)
endif ()

if (FOLLY_USE_JEMALLOC)
# Consumers may include jemalloc before a Folly header.
target_compile_definitions(folly_deps INTERFACE JEMALLOC_NO_DEMANGLE)
endif ()

# Generate pkg-config variables from folly_deps before we add our own
# build/install-time include directory generator expressions
include(GenPkgConfig)
Expand Down Expand Up @@ -1488,6 +1493,10 @@ if (BUILD_TESTS OR BUILD_BENCHMARKS)
SOURCES
MallctlHelperTest.cpp
TEST
memory_malloc_test
SOURCES
MallocTest.cpp
TEST
memory_uninitialized_test
SOURCES
UninitializedTest.cpp
Expand Down
13 changes: 13 additions & 0 deletions folly/memory/test/MallocTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <folly/memory/Malloc.h>

#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <new>
#include <stdexcept>
Expand All @@ -26,6 +28,17 @@

namespace folly {

TEST(MallocTest, standardAllocationAndSizedFree) {
for (size_t requested : {1, 64, 4096, 65536}) {
size_t size = goodMallocSize(requested);
ASSERT_GE(size, requested);
void* ptr = std::malloc(size);
ASSERT_NE(ptr, nullptr);
std::memset(ptr, 0xA5, size);
sizedFree(ptr, size);
}
}

TEST(MallocTest, getJEMallocMallctlArenasAll) {
#if FOLLY_HAS_JEMALLOC_DEFS
EXPECT_EQ(4096, getJEMallocMallctlArenasAll());
Expand Down
25 changes: 25 additions & 0 deletions folly/portability/Malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,33 @@
#include <malloc_np.h> // @manual
#define FOLLY_HAS_JEMALLOC_DEFS 0
#else
// Keep jemalloc's stable API aliases without remapping malloc/free macros.
#ifndef JEMALLOC_NO_DEMANGLE
#define JEMALLOC_NO_DEMANGLE
#endif
#include <jemalloc/jemalloc.h> // @manual
#define FOLLY_HAS_JEMALLOC_DEFS 1

#ifdef USE_JEMALLOC
// Explicit jemalloc builds can bind its helpers directly. Leave the weak-symbol
// path unchanged when the allocator is selected at runtime (e.g. TCMalloc).
namespace folly {
inline constexpr auto malloc_usable_size = ::je_malloc_usable_size;
inline constexpr auto mallocx = ::je_mallocx;
inline constexpr auto rallocx = ::je_rallocx;
inline constexpr auto xallocx = ::je_xallocx;
inline constexpr auto sallocx = ::je_sallocx;
inline constexpr auto dallocx = ::je_dallocx;
inline constexpr auto sdallocx = ::je_sdallocx;
inline constexpr auto nallocx = ::je_nallocx;
inline constexpr auto mallctl = ::je_mallctl;
inline constexpr auto mallctlnametomib = ::je_mallctlnametomib;
inline constexpr auto mallctlbymib = ::je_mallctlbymib;
#ifdef je_free_aligned_sized
inline constexpr auto free_aligned_sized = ::je_free_aligned_sized;
#endif
} // namespace folly
#endif
#endif
#else
#if !defined(__FreeBSD__)
Expand Down