1#if defined(OS_LINUX)
2#include <stdlib.h>
3
4/// Interposing these symbols explicitly. The idea works like this: malloc.cpp compiles to a
5/// dedicated object (namely clickhouse_malloc.o), and it will show earlier in the link command
6/// than malloc libs like libjemalloc.a. As a result, these symbols get picked in time right after.
7
8#pragma GCC diagnostic push
9#pragma GCC diagnostic ignored "-Wredundant-decls"
10extern "C"
11{
12 void *malloc(size_t size);
13 void free(void *ptr);
14 void *calloc(size_t nmemb, size_t size);
15 void *realloc(void *ptr, size_t size);
16 int posix_memalign(void **memptr, size_t alignment, size_t size);
17 void *aligned_alloc(size_t alignment, size_t size);
18 void *valloc(size_t size);
19 void *memalign(size_t alignment, size_t size);
20 void *pvalloc(size_t size);
21}
22#pragma GCC diagnostic pop
23
24template<typename T>
25inline void ignore(T x __attribute__((unused)))
26{
27}
28
29static void dummyFunctionForInterposing() __attribute__((used));
30static void dummyFunctionForInterposing()
31{
32 void* dummy;
33 /// Suppression for PVS-Studio.
34 free(nullptr); // -V575
35 ignore(malloc(0)); // -V575
36 ignore(calloc(0, 0)); // -V575
37 ignore(realloc(nullptr, 0)); // -V575
38 ignore(posix_memalign(&dummy, 0, 0)); // -V575
39 ignore(aligned_alloc(0, 0)); // -V575
40 ignore(valloc(0)); // -V575
41 ignore(memalign(0, 0)); // -V575
42 ignore(pvalloc(0)); // -V575
43}
44#endif
45