Newer
Older
#import "include/shamalloc.h"
#include <cstdio>
#include <cstdlib>
#include <dlfcn.h>
#include <new>
// A hacky override to get new working with Valgrind
void* operator new(std::size_t size) {
static void *(*fptr)(size_t) = NULL;
if (shamalloc_time_until_broken == 0) {
throw std::bad_alloc{};
} else {
if (shamalloc_time_until_broken > 0)
shamalloc_time_until_broken -= 1;
// Load the orginal new
// TODO: is _Znwm global across all platforms?
fptr = fptr ? fptr
: ((void *(*)(size_t)) dlsym(RTLD_NEXT, "_Znwm"));
if (fptr == NULL) std::abort();
return (*fptr)(size);
}
}