Browse Source

report allocation attempt when out of memory

David Rose 14 years ago
parent
commit
702045608d

+ 23 - 17
dtool/src/dtoolbase/memoryHook.cxx

@@ -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();
 }
 

+ 1 - 1
dtool/src/dtoolbase/memoryHook.h

@@ -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);

+ 2 - 4
dtool/src/dtoolbase/typeHandle.cxx

@@ -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);
   }
 }