| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifndef HEAPTRACK_API_H |
| 28 |
#define HEAPTRACK_API_H |
| 29 |
|
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
#ifndef HEAPTRACK_API_DLSYM |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#ifdef __cplusplus |
| 45 |
extern "C" { |
| 46 |
#endif |
| 47 |
|
| 48 |
__attribute__((weak)) void heaptrack_malloc(void* ptr, size_t size); |
| 49 |
__attribute__((weak)) void heaptrack_realloc(void* ptr_in, size_t size, void* ptr_out); |
| 50 |
__attribute__((weak)) void heaptrack_free(void* ptr); |
| 51 |
|
| 52 |
#ifdef __cplusplus |
| 53 |
} |
| 54 |
#endif |
| 55 |
|
| 56 |
#define heaptrack_report_alloc(ptr, size) \ |
| 57 |
if (heaptrack_malloc) \ |
| 58 |
heaptrack_malloc(ptr, size) |
| 59 |
|
| 60 |
#define heaptrack_report_realloc(ptr_in, size, ptr_out) \ |
| 61 |
if (heaptrack_realloc) \ |
| 62 |
heaptrack_realloc(ptr_in, size, ptr_out) |
| 63 |
|
| 64 |
#define heaptrack_report_free(ptr) \ |
| 65 |
if (heaptrack_free) \ |
| 66 |
heaptrack_free(ptr) |
| 67 |
|
| 68 |
#else |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
#ifndef __USE_GNU |
| 83 |
|
| 84 |
#define __USE_GNU |
| 85 |
#endif |
| 86 |
|
| 87 |
#include <dlfcn.h> |
| 88 |
|
| 89 |
struct heaptrack_api_t |
| 90 |
{ |
| 91 |
void (*malloc)(void*, size_t); |
| 92 |
void (*free)(void*); |
| 93 |
void (*realloc)(void*, size_t, void*); |
| 94 |
}; |
| 95 |
static struct heaptrack_api_t heaptrack_api = {0, 0, 0}; |
| 96 |
|
| 97 |
void heaptrack_init_api() |
| 98 |
{ |
| 99 |
static int initialized = 0; |
| 100 |
if (!initialized) { |
| 101 |
void* sym = dlsym(RTLD_NEXT, "heaptrack_malloc"); |
| 102 |
if (sym) |
| 103 |
heaptrack_api.malloc = (void (*)(void*, size_t))sym; |
| 104 |
|
| 105 |
sym = dlsym(RTLD_NEXT, "heaptrack_realloc"); |
| 106 |
if (sym) |
| 107 |
heaptrack_api.realloc = (void (*)(void*, size_t, void*))sym; |
| 108 |
|
| 109 |
sym = dlsym(RTLD_NEXT, "heaptrack_free"); |
| 110 |
if (sym) |
| 111 |
heaptrack_api.free = (void (*)(void*))sym; |
| 112 |
|
| 113 |
initialized = 1; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
#define heaptrack_report_alloc(ptr, size) \ |
| 118 |
do { \ |
| 119 |
heaptrack_init_api(); \ |
| 120 |
if (heaptrack_api.malloc) \ |
| 121 |
heaptrack_api.malloc(ptr, size); \ |
| 122 |
} while (0) |
| 123 |
|
| 124 |
#define heaptrack_report_realloc(ptr_in, size, ptr_out) \ |
| 125 |
do { \ |
| 126 |
heaptrack_init_api(); \ |
| 127 |
if (heaptrack_api.realloc) \ |
| 128 |
heaptrack_api.realloc(ptr_in, size, ptr_out); \ |
| 129 |
} while (0) |
| 130 |
|
| 131 |
#define heaptrack_report_free(ptr) \ |
| 132 |
do { \ |
| 133 |
heaptrack_init_api(); \ |
| 134 |
if (heaptrack_api.free) \ |
| 135 |
heaptrack_api.free(ptr); \ |
| 136 |
} while (0) |
| 137 |
|
| 138 |
#endif |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
#ifdef HEAPTRACK_DEFINE_VALGRIND_MACROS |
| 147 |
|
| 148 |
#define VALGRIND_DISABLE_ERROR_REPORTING |
| 149 |
#define VALGRIND_ENABLE_ERROR_REPORTING |
| 150 |
#define VALGRIND_CREATE_MEMPOOL(...) |
| 151 |
#define VALGRIND_DESTROY_MEMPOOL(...) |
| 152 |
#define VALGRIND_MAKE_MEM_NOACCESS(...) |
| 153 |
|
| 154 |
#define VALGRIND_MEMPOOL_ALLOC(pool, ptr, size) heaptrack_report_alloc(ptr, size) |
| 155 |
#define VALGRIND_MEMPOOL_FREE(pool, ptr) heaptrack_report_free(ptr) |
| 156 |
|
| 157 |
#endif |
| 158 |
|
| 159 |
#endif |