-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap_linux.c
76 lines (66 loc) · 2.08 KB
/
wrap_linux.c
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2022 Datadog, Inc.
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdatomic.h>
#include <stdint.h>
#include "profiler_internal.h"
/* The GNU linker supports a --wrap flag that lets wrap allocation calls
* without the problem of using dlsym in functions called by dlsym */
extern __thread int in_allocation;
void *__real_malloc(size_t size);
void *__wrap_malloc(size_t size) {
void *ret_addr = __builtin_return_address(0);
profile_allocation_checked(size, ret_addr);
in_allocation++;
void *res = __real_malloc(size);
in_allocation--;
return res;
}
void *__real_calloc(size_t nmemb, size_t size);
void *__wrap_calloc(size_t nmemb, size_t size) {
// If the allocation size would overflow, don't bother profiling, and
// let the real calloc implementation (possibly) fail.
if ((size > 0) && (nmemb > (SIZE_MAX/size))) {
return __real_calloc(nmemb, size);
}
profile_allocation(size * nmemb);
in_allocation++;
void *res = __real_calloc(nmemb, size);
in_allocation--;
return res;
}
void *__real_realloc(void *p, size_t size);
void *__wrap_realloc(void *p, size_t size) {
profile_allocation(size);
in_allocation++;
void *res = __real_realloc(p, size);
in_allocation--;
return res;
}
void *__real_valloc(size_t size);
void *__wrap_valloc(size_t size) {
profile_allocation(size);
in_allocation++;
void *res = __real_valloc(size);
in_allocation--;
return res;
}
void *__real_aligned_alloc(size_t alignment, size_t size);
void *__wrap_aligned_alloc(size_t alignment, size_t size) {
profile_allocation(size);
in_allocation++;
void *res = __real_aligned_alloc(alignment, size);
in_allocation--;
return res;
}
int __real_posix_memalign(void **p, size_t alignment, size_t size);
int __wrap_posix_memalign(void **p, size_t alignment, size_t size) {
profile_allocation(size);
in_allocation++;
int res = __real_posix_memalign(p, alignment, size);
in_allocation--;
return res;
}