12 #if !defined(__x86_64__) && !defined(__i386__) 13 #define USE_LOCAL_MALLOC 15 #if defined(__GLIBC__) && (__GLIBC__ == 2) && (__GLIBC_MINOR__ < 28) 17 #define USE_LOCAL_MALLOC 21 std::atomic<bool>& alloc_monitor_running_state() {
22 static std::atomic<bool> s_state =
false;
27 T get(
const char* iName) {
28 void*
original = dlsym(RTLD_NEXT, iName);
33 #ifdef USE_LOCAL_MALLOC 39 constexpr auto max_align =
alignof(std::max_align_t);
40 alignas(max_align)
char tmpbuff[131072];
41 unsigned long tmppos = 0;
42 unsigned long tmpallocs = 0;
44 void* local_malloc(
size_t size) noexcept {
46 size = ((
size + max_align - 1) / max_align) * max_align;
47 if (tmppos +
size <
sizeof(tmpbuff)) {
48 void* retptr = tmpbuff + tmppos;
58 void* local_calloc(
size_t nitems,
size_t item_size) noexcept {
return local_malloc(nitems * item_size); }
60 inline bool is_local_alloc(
void* ptr) noexcept {
return ptr >= (
void*)tmpbuff && ptr <= (
void*)(tmpbuff + tmppos); }
66 inline static void init() noexcept {
69 malloc = get<decltype(&::malloc)>(
"malloc");
70 calloc = get<decltype(&::calloc)>(
"calloc");
80 bool Originals::set =
false;
82 constexpr inline bool is_local_alloc(
void* ptr) noexcept {
return false; }
95 #ifdef USE_LOCAL_MALLOC 99 if (not alloc_monitor_running_state()) {
103 return reg.allocCalled(
107 void*
calloc(
size_t nitems,
size_t item_size) noexcept {
110 if (not alloc_monitor_running_state()) {
114 return reg.allocCalled(
117 [](
auto ret) {
return malloc_usable_size(
ret); });
120 void*
malloc(
size_t size) noexcept {
122 if (not alloc_monitor_running_state()) {
126 return reg.allocCalled(
127 size, [size]() {
return original(size); }, [](
auto ret) {
return malloc_usable_size(
ret); });
130 void*
calloc(
size_t nitems,
size_t item_size) noexcept {
132 if (not alloc_monitor_running_state()) {
136 return reg.allocCalled(
138 [nitems, item_size]() {
return original(nitems, item_size); },
139 [](
auto ret) {
return malloc_usable_size(
ret); });
143 void*
realloc(
void* ptr,
size_t size) noexcept {
145 if (not alloc_monitor_running_state()) {
148 size_t oldsize = malloc_usable_size(ptr);
153 [[maybe_unused]]
auto g = reg.makeGuard();
156 size_t used = malloc_usable_size(
ret);
157 if (used != oldsize) {
159 ptr, [](
auto) {}, [oldsize](
auto) {
return oldsize; });
161 size, []() {
return nullptr; }, [used](
auto) {
return used; });
168 if (not alloc_monitor_running_state()) {
173 return reg.allocCalled(
176 [](
auto ret) {
return malloc_usable_size(
ret); });
182 if (not alloc_monitor_running_state()) {
194 [](
auto ret) {
return malloc_usable_size(
ret); });
201 if (not alloc_monitor_running_state()) {
206 return reg.allocCalled(
209 [](
auto ret) {
return malloc_usable_size(
ret); });
212 void free(
void* ptr) noexcept {
215 if (not is_local_alloc(ptr)) {
216 if (not alloc_monitor_running_state()) {
223 ptr, [](
auto ptr) {
original(ptr); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
231 #define CPP_MEM_OVERRIDE 233 #if defined(CPP_MEM_OVERRIDE) 236 void*
operator new(std::size_t size) {
238 if (not alloc_monitor_running_state()) {
243 return reg.allocCalled(
244 size, [size]() {
return original(size); }, [](
auto ret) {
return malloc_usable_size(
ret); });
247 void operator delete(
void* ptr) noexcept {
249 if (not alloc_monitor_running_state()) {
256 ptr, [](
auto ptr) {
original(ptr); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
259 void*
operator new[](std::size_t size) {
261 if (not alloc_monitor_running_state()) {
266 return reg.allocCalled(
267 size, [size]() {
return original(size); }, [](
auto ret) {
return malloc_usable_size(
ret); });
270 void operator delete[](
void* ptr) noexcept {
273 if (not alloc_monitor_running_state()) {
279 ptr, [](
auto ptr) {
original(ptr); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
282 void*
operator new(std::size_t size, std::align_val_t al) {
283 CMS_SA_ALLOW static const auto original = get<void* (*)(std::size_t, std::align_val_t)>(
"_ZnwmSt11align_val_t");
284 if (not alloc_monitor_running_state()) {
289 return reg.allocCalled(
290 size, [size, al]() {
return original(size, al); }, [](
auto ret) {
return malloc_usable_size(
ret); });
293 void*
operator new[](std::size_t size, std::align_val_t al) {
294 CMS_SA_ALLOW static const auto original = get<void* (*)(std::size_t, std::align_val_t)>(
"_ZnamSt11align_val_t");
296 if (not alloc_monitor_running_state()) {
301 return reg.allocCalled(
302 size, [size, al]() {
return original(size, al); }, [](
auto ret) {
return malloc_usable_size(
ret); });
305 void*
operator new(std::size_t size,
const std::nothrow_t&
tag) noexcept {
307 get<void* (*)(std::size_t, const std::nothrow_t&) noexcept>(
"_ZnwmRKSt9nothrow_t");
309 if (not alloc_monitor_running_state()) {
314 return reg.allocCalled(
315 size, [size, &
tag]() {
return original(size,
tag); }, [](
auto ret) {
return malloc_usable_size(
ret); });
318 void*
operator new[](std::size_t size,
const std::nothrow_t&
tag) noexcept {
320 get<void* (*)(std::size_t, const std::nothrow_t&) noexcept>(
"_ZnamRKSt9nothrow_t");
322 if (not alloc_monitor_running_state()) {
327 return reg.allocCalled(
328 size, [size, &
tag]() {
return original(size,
tag); }, [](
auto ret) {
return malloc_usable_size(
ret); });
331 void*
operator new(std::size_t size, std::align_val_t al,
const std::nothrow_t&
tag) noexcept {
333 get<void* (*)(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept>(
334 "_ZnwmSt11align_val_tRKSt9nothrow_t");
336 if (not alloc_monitor_running_state()) {
341 return reg.allocCalled(
342 size, [size, al, &
tag]() {
return original(size, al,
tag); }, [](
auto ret) {
return malloc_usable_size(
ret); });
345 void*
operator new[](std::size_t size, std::align_val_t al,
const std::nothrow_t&
tag) noexcept {
347 get<void* (*)(std::size_t, std::align_val_t, const std::nothrow_t&) noexcept>(
348 "_ZnamSt11align_val_tRKSt9nothrow_t");
350 if (not alloc_monitor_running_state()) {
355 return reg.allocCalled(
356 size, [size, al, &
tag]() {
return original(size, al,
tag); }, [](
auto ret) {
return malloc_usable_size(
ret); });
359 void operator delete(
void* ptr, std::align_val_t al) noexcept {
360 CMS_SA_ALLOW static const auto original = get<void (*)(void*, std::align_val_t) noexcept>(
"_ZdlPvSt11align_val_t");
362 if (not alloc_monitor_running_state()) {
368 ptr, [al](
auto ptr) {
original(ptr, al); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
371 void operator delete[](
void* ptr, std::align_val_t al) noexcept {
372 CMS_SA_ALLOW static const auto original = get<void (*)(void*, std::align_val_t) noexcept>(
"_ZdaPvSt11align_val_t");
374 if (not alloc_monitor_running_state()) {
380 ptr, [al](
auto ptr) {
original(ptr, al); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
383 void operator delete(
void* ptr, std::size_t sz) noexcept {
384 CMS_SA_ALLOW static const auto original = get<void (*)(void*, std::size_t) noexcept>(
"_ZdlPvm");
386 if (not alloc_monitor_running_state()) {
392 ptr, [sz](
auto ptr) {
original(ptr, sz); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
395 void operator delete[](
void* ptr, std::size_t sz) noexcept {
396 CMS_SA_ALLOW static const auto original = get<void (*)(void*, std::size_t) noexcept>(
"_ZdaPvm");
398 if (not alloc_monitor_running_state()) {
404 ptr, [sz](
auto ptr) {
original(ptr, sz); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
407 void operator delete(
void* ptr, std::size_t sz, std::align_val_t al) noexcept {
409 get<void (*)(void*, std::size_t, std::align_val_t) noexcept>(
"_ZdlPvmSt11align_val_t");
411 if (not alloc_monitor_running_state()) {
417 ptr, [sz, al](
auto ptr) {
original(ptr, sz, al); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
420 void operator delete[](
void* ptr, std::size_t sz, std::align_val_t al) noexcept {
422 get<void (*)(void*, std::size_t, std::align_val_t) noexcept>(
"_ZdaPvmSt11align_val_t");
424 if (not alloc_monitor_running_state()) {
430 ptr, [sz, al](
auto ptr) {
original(ptr, sz, al); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
433 void operator delete(
void* ptr,
const std::nothrow_t&
tag) noexcept {
435 get<void (*)(void*, const std::nothrow_t&) noexcept>(
"_ZdlPvRKSt9nothrow_t");
437 if (not alloc_monitor_running_state()) {
443 ptr, [&
tag](
auto ptr) {
original(ptr,
tag); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
446 void operator delete[](
void* ptr,
const std::nothrow_t&
tag) noexcept {
448 get<void (*)(void*, const std::nothrow_t&) noexcept>(
"_ZdaPvRKSt9nothrow_t");
450 if (not alloc_monitor_running_state()) {
456 ptr, [&
tag](
auto ptr) {
original(ptr,
tag); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
459 void operator delete(
void* ptr, std::align_val_t al,
const std::nothrow_t&
tag) noexcept {
461 get<void (*)(void*, std::align_val_t, const std::nothrow_t&) noexcept>(
"_ZdlPvSt11align_val_tRKSt9nothrow_t");
463 if (not alloc_monitor_running_state()) {
469 ptr, [al, &
tag](
auto ptr) {
original(ptr, al,
tag); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
472 void operator delete[](
void* ptr, std::align_val_t al,
const std::nothrow_t&
tag) noexcept {
474 get<void (*)(void*, std::align_val_t, const std::nothrow_t&) noexcept>(
"_ZdaPvSt11align_val_tRKSt9nothrow_t");
476 if (not alloc_monitor_running_state()) {
482 ptr, [al, &
tag](
auto ptr) {
original(ptr, al,
tag); }, [](
auto ptr) {
return malloc_usable_size(ptr); });
ret
prodAgent to be discontinued
int posix_memalign(void **memptr, size_t alignment, size_t size) noexcept
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
void free(void *ptr) noexcept
void * aligned_alloc(size_t alignment, size_t size) noexcept
void * malloc(size_t size) noexcept
void * realloc(void *ptr, size_t size) noexcept
void alloc_monitor_start()
void * memalign(size_t alignment, size_t size) noexcept
void alloc_monitor_stop()
void * calloc(size_t nitems, size_t item_size) noexcept