diff --git a/uppsrc/Core/HeapImp.h b/uppsrc/Core/HeapImp.h index 4fb81e353..f4f5ae867 100644 --- a/uppsrc/Core/HeapImp.h +++ b/uppsrc/Core/HeapImp.h @@ -1,8 +1,10 @@ -void *SysAllocRaw(size_t size); +void OutOfMemoryPanic(size_t size); + +void *SysAllocRaw(size_t size, int reqsize); void SysFreeRaw(void *ptr, size_t size); -void *AllocRaw4KB(); -void *AllocRaw64KB(); +void *AllocRaw4KB(int reqsize); +void *AllocRaw64KB(int reqsize); void *LAlloc(size_t& size); void LFree(void *ptr); @@ -141,7 +143,7 @@ struct Heap { static int SizeToBin(int n) { return SzBin[(n - 1) >> 3]; } void LinkFree(DLink *b, int size); - DLink *AddChunk(); + DLink *AddChunk(int reqsize); void *DivideBlock(DLink *b, int size, int ii); void *TryLAlloc(int ii, size_t size); void *LAlloc(size_t& size); diff --git a/uppsrc/Core/heaputil.cpp b/uppsrc/Core/heaputil.cpp index b4ab6b2fd..9198e0b14 100644 --- a/uppsrc/Core/heaputil.cpp +++ b/uppsrc/Core/heaputil.cpp @@ -19,7 +19,7 @@ void *MemoryAllocPermanentRaw(size_t size) static byte *ptr = NULL; static byte *limit = NULL; if(ptr + size >= limit) { - ptr = (byte *)AllocRaw4KB(); + ptr = (byte *)AllocRaw4KB(size); limit = ptr + 4096; } void *p = ptr; @@ -48,11 +48,19 @@ void DoPeakProfile() heap.Make(*sPeak); } +void OutOfMemoryPanic(size_t size) +{ + char h[200]; + sprintf(h, "Out of memory!\nRequested size: %lld\nU++ allocated memory: %d KB", + (long long)size, MemoryUsedKb()); + Panic(h); +} + int sKB; int MemoryUsedKb() { return sKB; } -void *SysAllocRaw(size_t size) +void *SysAllocRaw(size_t size, int reqsize) { sKB += int(((size + 4095) & ~4095) >> 10); #ifdef PLATFORM_WIN32 @@ -67,7 +75,7 @@ void *SysAllocRaw(size_t size) ptr = NULL; #endif if(!ptr) - Panic("Out of memory!"); + OutOfMemoryPanic((size_t)reqsize); DoPeakProfile(); return ptr; } @@ -85,14 +93,14 @@ void SysFreeRaw(void *ptr, size_t size) int s4kb__; int s64kb__; -void *AllocRaw4KB() +void *AllocRaw4KB(int reqsize) { static int left; static byte *ptr; static int n = 32; if(left == 0) { left = n >> 5; - ptr = (byte *)SysAllocRaw(left * 4096); + ptr = (byte *)SysAllocRaw(left * 4096, reqsize); } n = n + 1; if(n > 4096) n = 4096; @@ -104,14 +112,14 @@ void *AllocRaw4KB() return p; } -void *AllocRaw64KB() +void *AllocRaw64KB(int reqsize) { static int left; static byte *ptr; static int n = 32; if(left == 0) { left = n >> 5; - ptr = (byte *)SysAllocRaw(left * 65536); + ptr = (byte *)SysAllocRaw(left * 65536, reqsize); } n = n + 1; if(n > 256) n = 256; diff --git a/uppsrc/Core/lheap.cpp b/uppsrc/Core/lheap.cpp index 44119e70a..77ffe0c61 100644 --- a/uppsrc/Core/lheap.cpp +++ b/uppsrc/Core/lheap.cpp @@ -50,7 +50,7 @@ void Heap::LinkFree(DLink *b, int size) b->Link(freebin[q]); } -Heap::DLink *Heap::AddChunk() +Heap::DLink *Heap::AddChunk(int reqsize) { DLink *ml; if(lempty->next != lempty) { @@ -59,7 +59,7 @@ Heap::DLink *Heap::AddChunk() LLOG("Retrieved empty large " << (void *)ml); } else { - ml = (DLink *)AllocRaw64KB(); + ml = (DLink *)AllocRaw64KB(reqsize); LLOG("AllocRaw64KB " << (void *)ml); } lcount++; @@ -157,9 +157,7 @@ void *Heap::LAlloc(size_t& size) { Init(); if(size > MAXBLOCK) { Mutex::Lock __(mutex); - BigHdr *h = (BigHdr *)SysAllocRaw(size + BIGHDRSZ); - if(!h) - Panic("Out of memory!"); + BigHdr *h = (BigHdr *)SysAllocRaw(size + BIGHDRSZ, size); h->Link(big); h->size = size = ((size + BIGHDRSZ + 4095) & ~4095) - BIGHDRSZ; Header *b = (Header *)((byte *)h + BIGHDRSZ - sizeof(Header)); @@ -188,7 +186,7 @@ void *Heap::LAlloc(size_t& size) { ptr = TryLAlloc(bini, size); if(ptr) return ptr; } - DLink *n = AddChunk(); + DLink *n = AddChunk(size); if(!n) Panic("Out of memory!"); ptr = DivideBlock(n, (int)size, LBINS - 1); diff --git a/uppsrc/Core/sheap.cpp b/uppsrc/Core/sheap.cpp index dfb1b2432..902761992 100644 --- a/uppsrc/Core/sheap.cpp +++ b/uppsrc/Core/sheap.cpp @@ -74,7 +74,7 @@ Heap::Page *Heap::WorkPage(int k) break; } if(!page) { - page = (Page *)AllocRaw4KB(); + page = (Page *)AllocRaw4KB(Ksz(k)); LLOG("AllocK - allocated new system page " << (void *)page << " " << k); page->Format(k); }