AnotherMemCheck/src/another_memcheck_external.cc

62 lines
1.7 KiB
C++
Raw Normal View History

2024-06-04 02:59:36 +00:00
// Standard library includes.
#include <iostream>
// Unix includes.
#include <dlfcn.h>
// Local includes.
#include "another_memcheck.h"
extern "C" {
void *malloc(std::size_t size) {
if (SC_AM_Internal::real_malloc == nullptr) {
SC_AM_Internal::real_malloc = reinterpret_cast<void*(*)(std::size_t)>(dlsym(RTLD_NEXT, "malloc"));
}
if (SC_AM_Internal::stats == nullptr) {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to malloc size: " << size << "...\n";
}
2024-06-04 02:59:36 +00:00
return SC_AM_Internal::stats->do_malloc(size);
}
2024-06-04 06:15:05 +00:00
void *calloc(std::size_t n, std::size_t size) {
if (SC_AM_Internal::real_calloc == nullptr) {
SC_AM_Internal::real_calloc = reinterpret_cast<void*(*)(std::size_t, std::size_t)>(
dlsym(RTLD_NEXT, "calloc"));
}
if (SC_AM_Internal::stats == nullptr) {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to calloc size: " << size << "...\n";
}
2024-06-04 06:15:05 +00:00
return SC_AM_Internal::stats->do_calloc(n, size);
}
2024-06-04 02:59:36 +00:00
void free(void *ptr) {
if (SC_AM_Internal::real_free == nullptr) {
SC_AM_Internal::real_free = reinterpret_cast<void(*)(void*)>(dlsym(RTLD_NEXT, "free"));
}
if (SC_AM_Internal::stats == nullptr) {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
if (SC_AM_Internal::is_env_status == SC_AM_Internal::ANOTHER_MEMCHECK_QUIET_NOT_EXISTS) {
std::clog << "attempting to free...\n";
}
2024-06-04 02:59:36 +00:00
return SC_AM_Internal::stats->do_free(ptr);
}
}
// vim: et sw=2 ts=2 sts=2