report allocation attempt when out of memory

This commit is contained in:
David Rose 2011-12-17 01:57:05 +00:00
parent 7de805989f
commit 702045608d
3 changed files with 26 additions and 22 deletions

View File

@ -188,22 +188,24 @@ MemoryHook::
////////////////////////////////////////////////////////////////////
void *MemoryHook::
heap_alloc_single(size_t size) {
size_t inflated_size = inflate_size(size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
void *alloc = call_malloc(inflate_size(size));
void *alloc = call_malloc(inflated_size);
_lock.release();
#else
void *alloc = call_malloc(inflate_size(size));
void *alloc = call_malloc(inflated_size);
#endif
while (alloc == (void *)NULL) {
alloc_fail();
alloc_fail(inflated_size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
alloc = call_malloc(inflate_size(size));
alloc = call_malloc(inflated_size);
_lock.release();
#else
alloc = call_malloc(inflate_size(size));
alloc = call_malloc(inflated_size);
#endif
}
@ -260,22 +262,24 @@ heap_free_single(void *ptr) {
////////////////////////////////////////////////////////////////////
void *MemoryHook::
heap_alloc_array(size_t size) {
size_t inflated_size = inflate_size(size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
void *alloc = call_malloc(inflate_size(size));
void *alloc = call_malloc(inflated_size);
_lock.release();
#else
void *alloc = call_malloc(inflate_size(size));
void *alloc = call_malloc(inflated_size);
#endif
while (alloc == (void *)NULL) {
alloc_fail();
alloc_fail(inflated_size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
alloc = call_malloc(inflate_size(size));
alloc = call_malloc(inflated_size);
_lock.release();
#else
alloc = call_malloc(inflate_size(size));
alloc = call_malloc(inflated_size);
#endif
}
@ -309,26 +313,28 @@ heap_realloc_array(void *ptr, size_t size) {
AtomicAdjust::add(_total_heap_array_size, (AtomicAdjust::Integer)size-(AtomicAdjust::Integer)orig_size);
#endif // DO_MEMORY_USAGE
size_t inflated_size = inflate_size(size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
alloc = call_realloc(alloc, inflate_size(size));
alloc = call_realloc(alloc, inflated_size);
_lock.release();
#else
alloc = call_realloc(alloc, inflate_size(size));
alloc = call_realloc(alloc, inflated_size);
#endif
while (alloc == (void *)NULL) {
alloc_fail();
alloc_fail(inflated_size);
// Recover the original pointer.
alloc = ptr_to_alloc(ptr, orig_size);
#ifdef MEMORY_HOOK_MALLOC_LOCK
_lock.acquire();
alloc = call_realloc(alloc, inflate_size(size));
alloc = call_realloc(alloc, inflated_size);
_lock.release();
#else
alloc = call_realloc(alloc, inflate_size(size));
alloc = call_realloc(alloc, inflated_size);
#endif
}
@ -546,8 +552,8 @@ get_deleted_chain(size_t buffer_size) {
// just to display a message and abort.
////////////////////////////////////////////////////////////////////
void MemoryHook::
alloc_fail() {
cerr << "Out of memory!\n";
alloc_fail(size_t attempted_size) {
cerr << "Out of memory allocating " << attempted_size << " bytes\n";
abort();
}

View File

@ -70,7 +70,7 @@ public:
DeletedBufferChain *get_deleted_chain(size_t buffer_size);
virtual void alloc_fail();
virtual void alloc_fail(size_t attempted_size);
private:
INLINE static size_t inflate_size(size_t size);

View File

@ -75,7 +75,7 @@ inc_memory_usage(MemoryClass memory_class, int size) {
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
assert(rnode != (TypeRegistryNode *)NULL);
AtomicAdjust::add(rnode->_memory_usage[memory_class], (AtomicAdjust::Integer)size);
// cerr << *this << ".inc(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
//cerr << *this << ".inc(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
assert(rnode->_memory_usage[memory_class] >= 0);
}
}
@ -95,9 +95,7 @@ dec_memory_usage(MemoryClass memory_class, int size) {
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
assert(rnode != (TypeRegistryNode *)NULL);
AtomicAdjust::add(rnode->_memory_usage[memory_class], -(AtomicAdjust::Integer)size);
if (rnode->_memory_usage[memory_class] < 0) {
cerr << *this << ".dec(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
}
//cerr << *this << ".dec(" << memory_class << ", " << size << ") -> " << rnode->_memory_usage[memory_class] << "\n";
assert(rnode->_memory_usage[memory_class] >= 0);
}
}