Core: Out of memory panic now displays info about requested block size and allocated memory (RM #240)

git-svn-id: svn://ultimatepp.org/upp/trunk@4471 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-01-26 19:25:05 +00:00
parent d6c8aef327
commit dbd4a0a4de
4 changed files with 26 additions and 18 deletions

View file

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

View file

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

View file

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

View file

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