Permalink
Browse files

print stacktrace on large allocation memory

  • Loading branch information...
stephentu committed Mar 10, 2013
1 parent 28a113f commit fdc60d6a66c2750cd3e0aec2d4b80735ebdaaaa9
Showing with 16 additions and 9 deletions.
  1. +16 −9 memory.cc
View
@@ -1,17 +1,24 @@
#include <stdlib.h>
#include <stdint.h>
#include <new>
#include "macros.h"
#include "macros.h" // for TRAP_LARGE_ALLOOCATIONS
#ifdef TRAP_LARGE_ALLOOCATIONS
#include <new>
#include <iostream>
#include <stdlib.h>
#include <stdint.h>
#include <execinfo.h>
#include <unistd.h>
static void *
do_allocation(size_t sz, bool do_throw)
{
if (unlikely(sz > (1 << 30)))
// allow us to trap
ALWAYS_ASSERT(false);
if (unlikely(sz > (1 << 20))) { // allocations more than 1MB are suspect
// print stacktrace:
std::cerr << "Warning: Large memory allocation (" << sz << " bytes)" << std::endl;
void *addrs[128];
const size_t naddrs = backtrace(addrs, ARRAY_NELEMS(addrs));
backtrace_symbols_fd(addrs, naddrs, STDERR_FILENO);
}
void *ret = malloc(sz);
if (unlikely(!ret && do_throw))
throw std::bad_alloc();
@@ -72,4 +79,4 @@ operator delete[](void *p, const std::nothrow_t &) throw ()
return do_deletion(p);
}
#endif /* CODEX_TAG_MEMORY */
#endif

0 comments on commit fdc60d6

Please sign in to comment.