Debian Codegraph @

src/track/heaptrack_api.h

raw symbols SCIP
1 /*
2 SPDX-FileCopyrightText: 2016-2017 Milian Wolff <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6
7 /**
8 * @file heaptrack_api.h
9 *
10 * This file defines a public API for heaptrack to be used in applications and
11 * libraries that implement custom allocators that do not use malloc internally.
12 *
13 * It should be enough to include this header in your code and then add calls
14 * to @c heaptrack_report_alloc, @c heaptrack_report_realloc and
15 * @c heaptrack_report_free to your code. By default, nothing will happen.
16 * Once you run your code within heaptrack though, this information will be
17 * picked up and included in the heap profile data.
18 *
19 * Note: If you use static linking, or have a custom allocator in your main
20 * executable, then you must define HEAPTRACK_API_DLSYM before including
21 * this header and link against libdl to make this work properly. The other,
22 * more common, case of pool allocators in shared libraries will work with the
23 * default implementation that relies on weak symbols and the dynamic linker
24 * on resolving the symbols for us directly.
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 * By default we rely on weak symbols that get resolved by the dynamic linker.
36 * The weak symbols defined here are usually zero, but become non-zero when
37 * we run the application with libheaptrack_preload.so loaded in.
38 *
39 * Note that this does not support run-time attaching yet.
40 * Also note that this won't work inside your main executable, nor when you use
41 * static linking. In these cases, the dlsym code below should be used instead.
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 // HEAPTRACK_API_DLSYM
69
70 /**
71 * Alternatively, we rely on dlsym to dynamically resolve the symbol to the
72 * heaptrack API functions at runtime. This works reliably, also when using
73 * static linking or when calling these functions from within your main
74 * executable. The caveat is that you need to link to libdl dynamically.
75 *
76 * @code
77 *
78 * gcc -DHEAPTRACK_API_DLSYM=1 -ldl ...
79 * @endcode
80 */
81
82 #ifndef __USE_GNU
83 // required for RTLD_NEXT
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 // HEAPTRACK_API_DLSYM
139
140 /**
141 * Optionally, you can let heaptrack mimic the Valgrind pool-allocator API.
142 *
143 * This won't work nicely when you want to enable both, Valgrind and heaptrack.
144 * Otherwise, it's an easy way to make your code ready for both tools.
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 // HEAPTRACK_API_H