mirror of
https://github.com/panda3d/panda3d.git
synced 2025-10-02 09:52:27 -04:00
report allocation attempt when out of memory
This commit is contained in:
parent
7de805989f
commit
702045608d
@ -188,22 +188,24 @@ MemoryHook::
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void *MemoryHook::
|
void *MemoryHook::
|
||||||
heap_alloc_single(size_t size) {
|
heap_alloc_single(size_t size) {
|
||||||
|
size_t inflated_size = inflate_size(size);
|
||||||
|
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
void *alloc = call_malloc(inflate_size(size));
|
void *alloc = call_malloc(inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
void *alloc = call_malloc(inflate_size(size));
|
void *alloc = call_malloc(inflated_size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (alloc == (void *)NULL) {
|
while (alloc == (void *)NULL) {
|
||||||
alloc_fail();
|
alloc_fail(inflated_size);
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
alloc = call_malloc(inflate_size(size));
|
alloc = call_malloc(inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
alloc = call_malloc(inflate_size(size));
|
alloc = call_malloc(inflated_size);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,22 +262,24 @@ heap_free_single(void *ptr) {
|
|||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void *MemoryHook::
|
void *MemoryHook::
|
||||||
heap_alloc_array(size_t size) {
|
heap_alloc_array(size_t size) {
|
||||||
|
size_t inflated_size = inflate_size(size);
|
||||||
|
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
void *alloc = call_malloc(inflate_size(size));
|
void *alloc = call_malloc(inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
void *alloc = call_malloc(inflate_size(size));
|
void *alloc = call_malloc(inflated_size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (alloc == (void *)NULL) {
|
while (alloc == (void *)NULL) {
|
||||||
alloc_fail();
|
alloc_fail(inflated_size);
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
alloc = call_malloc(inflate_size(size));
|
alloc = call_malloc(inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
alloc = call_malloc(inflate_size(size));
|
alloc = call_malloc(inflated_size);
|
||||||
#endif
|
#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);
|
AtomicAdjust::add(_total_heap_array_size, (AtomicAdjust::Integer)size-(AtomicAdjust::Integer)orig_size);
|
||||||
#endif // DO_MEMORY_USAGE
|
#endif // DO_MEMORY_USAGE
|
||||||
|
|
||||||
|
size_t inflated_size = inflate_size(size);
|
||||||
|
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
alloc = call_realloc(alloc, inflate_size(size));
|
alloc = call_realloc(alloc, inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
alloc = call_realloc(alloc, inflate_size(size));
|
alloc = call_realloc(alloc, inflated_size);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
while (alloc == (void *)NULL) {
|
while (alloc == (void *)NULL) {
|
||||||
alloc_fail();
|
alloc_fail(inflated_size);
|
||||||
|
|
||||||
// Recover the original pointer.
|
// Recover the original pointer.
|
||||||
alloc = ptr_to_alloc(ptr, orig_size);
|
alloc = ptr_to_alloc(ptr, orig_size);
|
||||||
|
|
||||||
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
#ifdef MEMORY_HOOK_MALLOC_LOCK
|
||||||
_lock.acquire();
|
_lock.acquire();
|
||||||
alloc = call_realloc(alloc, inflate_size(size));
|
alloc = call_realloc(alloc, inflated_size);
|
||||||
_lock.release();
|
_lock.release();
|
||||||
#else
|
#else
|
||||||
alloc = call_realloc(alloc, inflate_size(size));
|
alloc = call_realloc(alloc, inflated_size);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -546,8 +552,8 @@ get_deleted_chain(size_t buffer_size) {
|
|||||||
// just to display a message and abort.
|
// just to display a message and abort.
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
void MemoryHook::
|
void MemoryHook::
|
||||||
alloc_fail() {
|
alloc_fail(size_t attempted_size) {
|
||||||
cerr << "Out of memory!\n";
|
cerr << "Out of memory allocating " << attempted_size << " bytes\n";
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +70,7 @@ public:
|
|||||||
|
|
||||||
DeletedBufferChain *get_deleted_chain(size_t buffer_size);
|
DeletedBufferChain *get_deleted_chain(size_t buffer_size);
|
||||||
|
|
||||||
virtual void alloc_fail();
|
virtual void alloc_fail(size_t attempted_size);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
INLINE static size_t inflate_size(size_t size);
|
INLINE static size_t inflate_size(size_t size);
|
||||||
|
@ -75,7 +75,7 @@ inc_memory_usage(MemoryClass memory_class, int size) {
|
|||||||
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
|
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
|
||||||
assert(rnode != (TypeRegistryNode *)NULL);
|
assert(rnode != (TypeRegistryNode *)NULL);
|
||||||
AtomicAdjust::add(rnode->_memory_usage[memory_class], (AtomicAdjust::Integer)size);
|
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);
|
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);
|
TypeRegistryNode *rnode = TypeRegistry::ptr()->look_up(*this, NULL);
|
||||||
assert(rnode != (TypeRegistryNode *)NULL);
|
assert(rnode != (TypeRegistryNode *)NULL);
|
||||||
AtomicAdjust::add(rnode->_memory_usage[memory_class], -(AtomicAdjust::Integer)size);
|
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);
|
assert(rnode->_memory_usage[memory_class] >= 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user