Skip to content

Instantly share code, notes, and snippets.

@mazbox
Created May 15, 2023 13:09
Show Gist options
  • Save mazbox/9b90f712d9efc7c6b43a4eec540e7c3b to your computer and use it in GitHub Desktop.
Save mazbox/9b90f712d9efc7c6b43a4eec540e7c3b to your computer and use it in GitHub Desktop.
#ifndef WIN32
#include <dlfcn.h>
#include <stdio.h>
#include <vector>
#if DEBUG
void* malloc(size_t sz) {
static void *(*libc_malloc)(size_t) = nullptr;
if(libc_malloc==nullptr) {
void *a = dlsym(RTLD_NEXT, "malloc");
libc_malloc = (void *(*)(size_t))a;
}
return libc_malloc(sz);
}
void free(void *p) {
static void (*libc_free)(void*) = nullptr;
if(libc_free==nullptr) {
void *a = dlsym(RTLD_NEXT, "free");
libc_free = (void (*)(void*))a;
}
libc_free(p);
}
void * operator new(decltype(sizeof(0)) n) noexcept(false) {
if(isAudioThread()) {
printf("malloc on audio thread\n");
}
return malloc(n);
}
void operator delete(void * p) throw() {
if(isAudioThread()) {
printf("delete on audio thread\n");
}
free(p);
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment