fix(util/memory): redefine the non-throwing versions of the new

operator.

In some platforms, the following operator new is used

   void* operator new(std::size_t sz, std::nothrow_t const &)

Since, it was not defined by memory.cpp, a crash would happen whenever
our delete was invoked.

   void  operator delete(void * ptr) throw() { return lean::free(ptr); }

Our delete assumes the memory was allocated with our new at memory.cpp

   void* operator new(std::size_t sz)
This commit is contained in:
Leonardo de Moura 2014-09-21 10:54:41 -07:00
parent 10a4148adb
commit 648f209cfe

View file

@ -14,12 +14,18 @@ size_t get_allocated_memory() {
return 0; return 0;
} }
void * malloc(size_t sz) { void * malloc(size_t sz, bool use_ex) {
void * r = ::malloc(sz); void * r = ::malloc(sz);
if (r || sz == 0) if (r || sz == 0)
return r; return r;
else else if (use_ex)
throw std::bad_alloc(); throw std::bad_alloc();
else
return nullptr;
}
void * malloc(size_t sz) {
return malloc(sz, true);
} }
void * realloc(void * ptr, size_t sz) { void * realloc(void * ptr, size_t sz) {
@ -105,17 +111,23 @@ static alloc_info g_global_memory;
size_t get_allocated_memory() { return g_global_memory.size(); } size_t get_allocated_memory() { return g_global_memory.size(); }
void * malloc(size_t sz) { void * malloc(size_t sz, bool use_ex) {
void * r = malloc_core(sz); void * r = malloc_core(sz);
if (r || sz == 0) { if (r || sz == 0) {
size_t rsz = malloc_size(r); size_t rsz = malloc_size(r);
g_global_memory.inc(rsz); g_global_memory.inc(rsz);
return r; return r;
} else { } else if (use_ex) {
throw std::bad_alloc(); throw std::bad_alloc();
} else {
return nullptr;
} }
} }
void * malloc(size_t sz) {
return malloc(sz, true);
}
void * realloc(void * ptr, size_t sz) { void * realloc(void * ptr, size_t sz) {
if (ptr == nullptr) if (ptr == nullptr)
return malloc(sz); return malloc(sz);
@ -143,8 +155,10 @@ void free(void * ptr) {
} }
} }
void* operator new(std::size_t sz) throw(std::bad_alloc) { return lean::malloc(sz); } void* operator new(std::size_t sz) throw(std::bad_alloc) { return lean::malloc(sz, true); }
void operator delete(void * ptr) throw() { return lean::free(ptr); } void operator delete(void * ptr) throw() { return lean::free(ptr); }
void* operator new[](std::size_t sz) throw(std::bad_alloc) { return lean::malloc(sz); } void* operator new[](std::size_t sz) throw(std::bad_alloc) { return lean::malloc(sz, true); }
void operator delete[](void * ptr) throw() { return lean::free(ptr); } void operator delete[](void * ptr) throw() { return lean::free(ptr); }
void* operator new(std::size_t sz, std::nothrow_t const &) { return lean::malloc(sz, false); }
void* operator new[](std::size_t sz, std::nothrow_t const &) { return lean::malloc(sz, false); }
#endif #endif