From fa74edd54f09ffffd2dc3c4126a84a3831591532 Mon Sep 17 00:00:00 2001 From: cxl Date: Tue, 3 Dec 2013 13:04:05 +0000 Subject: [PATCH] .cosmetics git-svn-id: svn://ultimatepp.org/upp/trunk@6633 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/HeapImp.h | 12 ++++++------ uppsrc/Core/heap.cpp | 4 ++-- uppsrc/Core/heaputil.cpp | 7 +++++++ uppsrc/Core/sheap.cpp | 32 ++++++++++++++++---------------- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/uppsrc/Core/HeapImp.h b/uppsrc/Core/HeapImp.h index 15564be1b..3238f4c1c 100644 --- a/uppsrc/Core/HeapImp.h +++ b/uppsrc/Core/HeapImp.h @@ -79,11 +79,11 @@ struct Heap { }; static StaticMutex mutex; - Page work[NKLASS][1]; - Page full[NKLASS][1]; - Page *empty[NKLASS]; - FreeLink *cache[NKLASS]; - int cachen[NKLASS]; + Page work[NKLASS][1]; // circular list of pages that contain some empty blocks + Page full[NKLASS][1]; // circular list of pages that contain NO empty blocks + Page *empty[NKLASS]; // last fully freed page per klass (hot) + FreeLink *cache[NKLASS]; // hot frontend cache of small blocks + int cachen[NKLASS]; // counter of small blocks that are allowed to be stored in cache bool initialized; @@ -99,7 +99,7 @@ struct Heap { FreeLink *remote_free; static DLink big[1]; - static Heap aux; + static Heap aux; // Single global auxiliary heap to store orphans and global list of free pages #ifdef HEAPDBG static void DbgFreeFill(void *ptr, size_t size); diff --git a/uppsrc/Core/heap.cpp b/uppsrc/Core/heap.cpp index 4a7321e1b..b8ef32006 100644 --- a/uppsrc/Core/heap.cpp +++ b/uppsrc/Core/heap.cpp @@ -44,7 +44,7 @@ void Heap::Init() void Heap::RemoteFree(void *ptr) { LLOG("RemoteFree " << ptr); - Mutex::Lock __(mutex); // TODO: Go lockless! + Mutex::Lock __(mutex); FreeLink *f = (FreeLink *)ptr; f->next = remote_free; remote_free = f; @@ -68,7 +68,7 @@ void Heap::FreeRemote() } void Heap::Shutdown() -{ +{ // Move all pages to global aux heap LLOG("Shutdown"); Mutex::Lock __(mutex); Init(); diff --git a/uppsrc/Core/heaputil.cpp b/uppsrc/Core/heaputil.cpp index 0f37d0ceb..c859bda36 100644 --- a/uppsrc/Core/heaputil.cpp +++ b/uppsrc/Core/heaputil.cpp @@ -193,6 +193,13 @@ void Heap::Make(MemoryProfile& f) f.allocated[qq] += p->active; p = p->next; } + if(empty[i]) + f.freepages++; + p = aux.empty[i]; + while(p) { + f.freepages++; + p = p->next; + } } int ii = 0; int fi = 0; diff --git a/uppsrc/Core/sheap.cpp b/uppsrc/Core/sheap.cpp index e5f196bb5..f69c82565 100644 --- a/uppsrc/Core/sheap.cpp +++ b/uppsrc/Core/sheap.cpp @@ -26,23 +26,23 @@ inline void Heap::Page::Format(int k) freelist = l; } -Heap::Page *Heap::WorkPage(int k) +Heap::Page *Heap::WorkPage(int k) // get a new empty workpage { LLOG("AllocK - next work not available " << k << " empty: " << (void *)empty[k]); - Page *page = empty[k]; + Page *page = empty[k]; // hot empty page of the same klass empty[k] = NULL; - if(!page) { + if(!page) { // try to reacquire pages freed remotely LLOG("AllocK - trying FreeRemote"); FreeRemote(); - if(work[k]->freelist) { + if(work[k]->freelist) { // partially free page found LLOG("AllocK - work available after FreeRemote " << k); return work[k]; } - page = empty[k]; + page = empty[k]; // hot empty page empty[k] = NULL; } if(!page) - for(int i = 0; i < NKLASS; i++) + for(int i = 0; i < NKLASS; i++) // Try hot local page of different klass if(empty[i]) { LLOG("AllocK - free page available for reformatting " << k); page = empty[i]; @@ -50,22 +50,22 @@ Heap::Page *Heap::WorkPage(int k) page->Format(k); break; } - if(!page) { + if(!page) { // Attempt to find page in global storage of free pages Mutex::Lock __(mutex); aux.FreeRemoteRaw(); - if(aux.work[k]->next != aux.work[k]) { + if(aux.work[k]->next != aux.work[k]) { // Try page of the same klass first page = aux.work[k]->next; page->Unlink(); page->heap = this; LLOG("AllocK - adopting aux page " << k << " page: " << (void *)page << ", free " << (void *)page->freelist); } - if(!page && aux.empty[k]) { + if(!page && aux.empty[k]) { // Try hot empty page of the same klass page = aux.empty[k]; aux.empty[k] = page->next; LLOG("AllocK - empty aux page available of the same format " << k << " page: " << (void *)page << ", free " << (void *)page->freelist); } if(!page) - for(int i = 0; i < NKLASS; i++) + for(int i = 0; i < NKLASS; i++) // Finally try to to find hot page of different klass if(aux.empty[i]) { page = aux.empty[i]; aux.empty[i] = page->next; @@ -73,7 +73,7 @@ Heap::Page *Heap::WorkPage(int k) LLOG("AllocK - empty aux page available for reformatting " << k << " page: " << (void *)page << ", free " << (void *)page->freelist); break; } - if(!page) { + if(!page) { // Not free memory was found, ask system for the new page page = (Page *)AllocRaw4KB(Ksz(k)); LLOG("AllocK - allocated new system page " << (void *)page << " " << k); page->Format(k); @@ -178,8 +178,8 @@ void Heap::FreeK(void *ptr, Page *page, int k) ((FreeLink *)ptr)->next = NULL; } page->freelist = (FreeLink *)ptr; - if(--page->active == 0) { - LLOG("Free page is empty" << " " << (void *)page); + if(--page->active == 0) { // there are no more allocated blocks in this page + LLOG("Free page is empty " << (void *)page); page->Unlink(); if(this == &aux) { LLOG("...is aux"); @@ -187,7 +187,7 @@ void Heap::FreeK(void *ptr, Page *page, int k) empty[k] = page; } else { - if(empty[k]) { + if(empty[k]) { // Keep one hot empty page per klass in thread, put rest to 'aux' global storage LLOG("Global free " << k << " " << (void *)empty[k]); Mutex::Lock __(mutex); empty[k]->heap = &aux; @@ -209,8 +209,8 @@ void Heap::Free(void *ptr) LLOG("Small free page: " << (void *)page << ", k: " << k << ", ksz: " << Ksz(k)); ASSERT((4096 - ((uintptr_t)ptr & (uintptr_t)4095)) % Ksz(k) == 0); #ifdef _MULTITHREADED - if(page->heap != this) { - page->heap->RemoteFree(ptr); + if(page->heap != this) { // freeing page allocated in different thread + page->heap->RemoteFree(ptr); // add to original heap's list of free pages to be properly freed later return; } #endif