Skip to content

Commit

Permalink
Add guards for glibc specific allocators
Browse files Browse the repository at this point in the history
`glibc` exposes some specific allocators that other libc implementations
do not. To avoid having to rely of a configure step (which would
complicate considerably our build process), add some macro guards to
only define and override these allocator functions and APIs if `glibc`
is detected on the system.

Signed-off-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
pablogsal committed May 6, 2022
1 parent 02e13cf commit e5405e5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/memray/_memray/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) noexc
return ptr;
}

#if defined(__GLIBC__)
void*
mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) noexcept
{
Expand All @@ -114,6 +115,7 @@ mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) n
tracking_api::Tracker::trackAllocation(ptr, length, hooks::Allocator::MMAP);
return ptr;
}
#endif

int
munmap(void* addr, size_t length) noexcept
Expand Down Expand Up @@ -210,6 +212,7 @@ valloc(size_t size) noexcept
return ret;
}

#if defined(__GLIBC__)
void*
pvalloc(size_t size) noexcept
{
Expand All @@ -221,6 +224,7 @@ pvalloc(size_t size) noexcept
}
return ret;
}
#endif

void*
dlopen(const char* filename, int flag) noexcept
Expand Down
51 changes: 35 additions & 16 deletions src/memray/_memray/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,39 @@

#include "logging.h"

#define MEMRAY_HOOKED_FUNCTIONS \
FOR_EACH_HOOKED_FUNCTION(malloc) \
FOR_EACH_HOOKED_FUNCTION(free) \
FOR_EACH_HOOKED_FUNCTION(calloc) \
FOR_EACH_HOOKED_FUNCTION(realloc) \
FOR_EACH_HOOKED_FUNCTION(posix_memalign) \
FOR_EACH_HOOKED_FUNCTION(memalign) \
FOR_EACH_HOOKED_FUNCTION(valloc) \
FOR_EACH_HOOKED_FUNCTION(pvalloc) \
FOR_EACH_HOOKED_FUNCTION(dlopen) \
FOR_EACH_HOOKED_FUNCTION(dlclose) \
FOR_EACH_HOOKED_FUNCTION(mmap) \
FOR_EACH_HOOKED_FUNCTION(mmap64) \
FOR_EACH_HOOKED_FUNCTION(munmap) \
FOR_EACH_HOOKED_FUNCTION(prctl) \
FOR_EACH_HOOKED_FUNCTION(PyGILState_Ensure)
#if defined(__GLIBC__)
# define MEMRAY_HOOKED_FUNCTIONS \
FOR_EACH_HOOKED_FUNCTION(malloc) \
FOR_EACH_HOOKED_FUNCTION(free) \
FOR_EACH_HOOKED_FUNCTION(calloc) \
FOR_EACH_HOOKED_FUNCTION(realloc) \
FOR_EACH_HOOKED_FUNCTION(posix_memalign) \
FOR_EACH_HOOKED_FUNCTION(memalign) \
FOR_EACH_HOOKED_FUNCTION(valloc) \
FOR_EACH_HOOKED_FUNCTION(pvalloc) \
FOR_EACH_HOOKED_FUNCTION(dlopen) \
FOR_EACH_HOOKED_FUNCTION(dlclose) \
FOR_EACH_HOOKED_FUNCTION(mmap) \
FOR_EACH_HOOKED_FUNCTION(mmap64) \
FOR_EACH_HOOKED_FUNCTION(munmap) \
FOR_EACH_HOOKED_FUNCTION(prctl) \
FOR_EACH_HOOKED_FUNCTION(PyGILState_Ensure)
#else
# define MEMRAY_HOOKED_FUNCTIONS \
FOR_EACH_HOOKED_FUNCTION(malloc) \
FOR_EACH_HOOKED_FUNCTION(free) \
FOR_EACH_HOOKED_FUNCTION(calloc) \
FOR_EACH_HOOKED_FUNCTION(realloc) \
FOR_EACH_HOOKED_FUNCTION(posix_memalign) \
FOR_EACH_HOOKED_FUNCTION(memalign) \
FOR_EACH_HOOKED_FUNCTION(valloc) \
FOR_EACH_HOOKED_FUNCTION(dlopen) \
FOR_EACH_HOOKED_FUNCTION(dlclose) \
FOR_EACH_HOOKED_FUNCTION(mmap) \
FOR_EACH_HOOKED_FUNCTION(munmap) \
FOR_EACH_HOOKED_FUNCTION(prctl) \
FOR_EACH_HOOKED_FUNCTION(PyGILState_Ensure)
#endif

namespace memray::hooks {

Expand Down Expand Up @@ -154,8 +171,10 @@ dlclose(void* handle) noexcept;
void*
mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) noexcept;

#if defined(__GLIBC__)
void*
mmap64(void* addr, size_t length, int prot, int flags, int fd, off64_t offset) noexcept;
#endif

int
munmap(void* addr, size_t length) noexcept;
Expand Down
21 changes: 2 additions & 19 deletions src/memray/_memray/tracking_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ using namespace std::chrono_literals;

namespace {

struct RecursionGuard
{
RecursionGuard()
: wasLocked(isActive)
{
isActive = true;
}

~RecursionGuard()
{
isActive = wasLocked;
}

const bool wasLocked;
MEMRAY_FAST_TLS static thread_local bool isActive;
};

MEMRAY_FAST_TLS thread_local bool RecursionGuard::isActive = false;

std::string
get_executable()
{
Expand All @@ -64,6 +45,8 @@ std::atomic<unsigned int> g_tracker_generation;

namespace memray::tracking_api {

MEMRAY_FAST_TLS thread_local bool RecursionGuard::isActive = false;

static inline thread_id_t
thread_id()
{
Expand Down
17 changes: 17 additions & 0 deletions src/memray/_memray/tracking_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@

namespace memray::tracking_api {

struct RecursionGuard
{
RecursionGuard()
: wasLocked(isActive)
{
isActive = true;
}

~RecursionGuard()
{
isActive = wasLocked;
}

const bool wasLocked;
MEMRAY_FAST_TLS static thread_local bool isActive;
};

// Trace function interface

/**
Expand Down

0 comments on commit e5405e5

Please sign in to comment.