1 #include "macros.h" // for TRAP_LARGE_ALLOOCATIONS
3 #ifdef TRAP_LARGE_ALLOOCATIONS
13 do_allocation(size_t sz, bool do_throw)
15 if (unlikely(sz > (1 << 20))) { // allocations more than 1MB are suspect
17 std::cerr << "Warning: Large memory allocation (" << sz << " bytes)" << std::endl;
19 const size_t naddrs = backtrace(addrs, ARRAY_NELEMS(addrs));
20 backtrace_symbols_fd(addrs, naddrs, STDERR_FILENO);
22 void *ret = malloc(sz);
23 if (unlikely(!ret && do_throw))
24 throw std::bad_alloc();
35 operator new(size_t sz) throw (std::bad_alloc)
37 return do_allocation(sz, true);
41 operator new(size_t sz, const std::nothrow_t&) throw ()
43 return do_allocation(sz, false);
47 operator new[](size_t sz) throw (std::bad_alloc)
49 return do_allocation(sz, true);
53 operator new[](size_t sz, std::nothrow_t &) throw ()
55 return do_allocation(sz, false);
59 operator delete(void *p) throw ()
61 return do_deletion(p);
65 operator delete(void *p, const std::nothrow_t &) throw ()
67 return do_deletion(p);
71 operator delete[](void *p) throw ()
73 return do_deletion(p);
77 operator delete[](void *p, const std::nothrow_t &) throw ()
79 return do_deletion(p);