From a6a6453c604cf093a073ab2e25fe0f1423df40ac Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Wed, 7 Nov 2012 17:35:22 +0100 Subject: [PATCH] Add custom "grain" for block allocator. Set the big block allocs to grain 256. --- Core/HLE/sceKernelMemory.cpp | 4 +-- Core/Util/BlockAllocator.cpp | 70 ++++++++++++++++++++++-------------- Core/Util/BlockAllocator.h | 6 +++- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/Core/HLE/sceKernelMemory.cpp b/Core/HLE/sceKernelMemory.cpp index c0dba7012f..fd16ba283f 100644 --- a/Core/HLE/sceKernelMemory.cpp +++ b/Core/HLE/sceKernelMemory.cpp @@ -27,8 +27,8 @@ ////////////////////////////////////////////////////////////////////////// // STATE BEGIN -BlockAllocator userMemory; -BlockAllocator kernelMemory; +BlockAllocator userMemory(256); +BlockAllocator kernelMemory(256); // STATE END ////////////////////////////////////////////////////////////////////////// diff --git a/Core/Util/BlockAllocator.cpp b/Core/Util/BlockAllocator.cpp index 3f9953ea8b..652ea2499a 100644 --- a/Core/Util/BlockAllocator.cpp +++ b/Core/Util/BlockAllocator.cpp @@ -3,7 +3,7 @@ // Slow freaking thing but works (eventually) :) -BlockAllocator::BlockAllocator() +BlockAllocator::BlockAllocator(int grain) : grain_(grain) { blocks.clear(); } @@ -35,8 +35,8 @@ u32 BlockAllocator::Alloc(u32 &size, bool fromTop, const char *tag) return 0; } - // upalign size to 16-byte blocks (is this right?) - size = (size+15) & ~15; + // upalign size to grain + size = (size + grain_ - 1) & ~(grain_ - 1); if (!fromTop) { @@ -101,13 +101,20 @@ u32 BlockAllocator::Alloc(u32 &size, bool fromTop, const char *tag) u32 BlockAllocator::AllocAt(u32 position, u32 size, const char *tag) { + CheckBlocks(); if (size > rangeSize_) { ERROR_LOG(HLE, "Clearly bogus size: %08x - failing allocation", size); return 0; } - //upalign size - size=(size+15) & ~15; + // upalign size to grain + size = (size + grain_ - 1) & ~(grain_ - 1); + + // check that position is aligned + if (position & (grain_ - 1)) { + ERROR_LOG(HLE, "Position %08x does not align to grain. Grain will be off.", position); + } + std::list::iterator iter = GetBlockIterFromAddress(position); if (iter != blocks.end()) { @@ -126,6 +133,7 @@ u32 BlockAllocator::AllocAt(u32 position, u32 size, const char *tag) b.taken = true; b.size = size; b.SetTag(tag); + CheckBlocks(); return position; } else @@ -159,31 +167,28 @@ u32 BlockAllocator::AllocAt(u32 position, u32 size, const char *tag) void BlockAllocator::MergeFreeBlocks() { - bool found; - do +restart: + DEBUG_LOG(HLE, "Merging Blocks"); + std::list::iterator iter1, iter2; + iter1 = blocks.begin(); + iter2 = blocks.begin(); + iter2++; + while (iter2 != blocks.end()) { - found = false; - std::list::iterator iter1, iter2; - iter1 = blocks.begin(); - iter2 = blocks.begin(); - iter2++; - while (iter2 != blocks.end()) - { - BlockAllocator::Block &b1 = *iter1; - BlockAllocator::Block &b2 = *iter2; + BlockAllocator::Block &b1 = *iter1; + BlockAllocator::Block &b2 = *iter2; - if (b1.taken == false && b2.taken == false) - { - DEBUG_LOG(HLE, "Block Alloc found adjacent free blocks - merging"); - b1.size += b2.size; - blocks.erase(iter2); - found = true; - break; //iterators now invalid - we have to restart our search - } - iter1++; - iter2++; + if (b1.taken == false && b2.taken == false) + { + DEBUG_LOG(HLE, "Block Alloc found adjacent free blocks - merging"); + b1.size += b2.size; + blocks.erase(iter2); + CheckBlocks(); + goto restart; //iterators now invalid - we have to restart our search } - } while (found); + iter1++; + iter2++; + } } void BlockAllocator::Free(u32 position) @@ -200,6 +205,17 @@ void BlockAllocator::Free(u32 position) } } +void BlockAllocator::CheckBlocks() +{ + for (std::list::iterator iter = blocks.begin(); iter != blocks.end(); iter++) + { + BlockAllocator::Block &b = *iter; + if (b.start > 0xc0000000) { // probably free'd debug values + ERROR_LOG(HLE, "Bogus block in allocator"); + } + } +} + std::list::iterator BlockAllocator::GetBlockIterFromAddress(u32 addr) { for (std::list::iterator iter = blocks.begin(); iter != blocks.end(); iter++) diff --git a/Core/Util/BlockAllocator.h b/Core/Util/BlockAllocator.h index e32269df90..dc90b1dd8b 100644 --- a/Core/Util/BlockAllocator.h +++ b/Core/Util/BlockAllocator.h @@ -14,7 +14,7 @@ class BlockAllocator { public: - BlockAllocator(); + BlockAllocator(int grain = 16); // 16 byte granularity by default. ~BlockAllocator(); void Init(u32 _rangeStart, u32 _rangeSize); @@ -43,6 +43,8 @@ public: u32 GetTotalFreeBytes(); private: + void CheckBlocks(); + struct Block { Block(u32 _start, u32 _size, bool _taken) : start(_start), size(_size), taken(_taken) @@ -65,6 +67,8 @@ private: u32 rangeStart_; u32 rangeSize_; + u32 grain_; + Block *GetBlockFromAddress(u32 addr); std::list::iterator GetBlockIterFromAddress(u32 addr); };