From 181738a147b3208b29eb8d899db6b0335d6ee813 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Tue, 19 Nov 2024 12:02:45 +0100 Subject: [PATCH] UPP allocator now throwing bad_alloc --- uppsrc/Core/App.cpp | 4 ++++ uppsrc/Core/heaputil.cpp | 16 +++++++++------- uppsrc/Core/hheap.cpp | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/uppsrc/Core/App.cpp b/uppsrc/Core/App.cpp index 9a8edc8f0..a8e1865ca 100644 --- a/uppsrc/Core/App.cpp +++ b/uppsrc/Core/App.cpp @@ -482,6 +482,10 @@ void AppExecute__(void (*app)()) catch(ExitExc) { return; } + catch(std::bad_alloc) { + extern char out_of_memory_message[200]; + Panic(out_of_memory_message); + } } #ifdef PLATFORM_POSIX diff --git a/uppsrc/Core/heaputil.cpp b/uppsrc/Core/heaputil.cpp index 5c766db10..8203bb5ce 100644 --- a/uppsrc/Core/heaputil.cpp +++ b/uppsrc/Core/heaputil.cpp @@ -10,13 +10,6 @@ namespace Upp { #include "HeapImp.h" -void OutOfMemoryPanic(size_t size) -{ - char h[200]; - snprintf(h, 200, "Out of memory!\nU++ allocated memory: %d KB", MemoryUsedKb()); - Panic(h); -} - size_t Heap::huge_4KB_count; int Heap::free_4KB; size_t Heap::big_size; @@ -26,6 +19,15 @@ size_t Heap::sys_count; size_t Heap::huge_chunks; size_t Heap::huge_4KB_count_max; +char out_of_memory_message[200]; + +void OutOfMemoryPanic(size_t size) +{ + snprintf(out_of_memory_message, 200, "Out of memory!\nTrying to allocate: %d KB", + (int)(min(size >> 10, (size_t)INT_MAX))); + throw std::bad_alloc(); +} + int MemoryUsedKb() { return int(4 * (Heap::huge_4KB_count - Heap::free_4KB)); diff --git a/uppsrc/Core/hheap.cpp b/uppsrc/Core/hheap.cpp index b98870332..a339d6a66 100644 --- a/uppsrc/Core/hheap.cpp +++ b/uppsrc/Core/hheap.cpp @@ -130,7 +130,8 @@ void *Heap::HugeAlloc(size_t count) // count in 4kb pages AddChunk((BlkHeader *)ptr, HPAGE); } } - Panic("Out of memory"); + void OutOfMemoryPanic(size_t size); + OutOfMemoryPanic(count * 4096); return NULL; }