2013-02-22 19:05:07 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2013-12-31 05:37:19 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
2013-08-20 05:45:38 +00:00
|
|
|
#include "Common/Log.h"
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/Serializer.h"
|
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2017-02-24 17:59:41 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2021-02-02 08:08:05 +00:00
|
|
|
#include "Core/Debugger/MemBlockInfo.h"
|
2013-08-20 05:45:38 +00:00
|
|
|
#include "Core/Util/BlockAllocator.h"
|
|
|
|
#include "Core/Reporting.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// Slow freaking thing but works (eventually) :)
|
|
|
|
|
2013-06-01 05:40:32 +00:00
|
|
|
BlockAllocator::BlockAllocator(int grain) : bottom_(NULL), top_(NULL), grain_(grain)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockAllocator::~BlockAllocator()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2021-02-02 08:08:05 +00:00
|
|
|
void BlockAllocator::Init(u32 rangeStart, u32 rangeSize, bool suballoc) {
|
2012-11-01 15:19:01 +00:00
|
|
|
Shutdown();
|
2012-11-05 12:36:12 +00:00
|
|
|
rangeStart_ = rangeStart;
|
|
|
|
rangeSize_ = rangeSize;
|
2012-11-01 15:19:01 +00:00
|
|
|
//Initial block, covering everything
|
2013-05-27 06:09:27 +00:00
|
|
|
top_ = new Block(rangeStart_, rangeSize_, false, NULL, NULL);
|
|
|
|
bottom_ = top_;
|
2021-02-02 08:08:05 +00:00
|
|
|
suballoc_ = suballoc;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockAllocator::Shutdown()
|
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
while (bottom_ != NULL)
|
|
|
|
{
|
|
|
|
Block *next = bottom_->next;
|
|
|
|
delete bottom_;
|
|
|
|
bottom_ = next;
|
|
|
|
}
|
|
|
|
top_ = NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-03-04 04:20:49 +00:00
|
|
|
u32 BlockAllocator::AllocAligned(u32 &size, u32 sizeGrain, u32 grain, bool fromTop, const char *tag)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-05 12:36:12 +00:00
|
|
|
// Sanity check
|
2012-11-06 14:46:21 +00:00
|
|
|
if (size == 0 || size > rangeSize_) {
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Clearly bogus size: %08x - failing allocation", size);
|
2012-12-31 07:22:30 +00:00
|
|
|
return -1;
|
2012-11-05 12:36:12 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 05:04:41 +00:00
|
|
|
// It could be off step, but the grain should generally be a power of 2.
|
|
|
|
if (grain < grain_)
|
|
|
|
grain = grain_;
|
2013-03-04 04:20:49 +00:00
|
|
|
if (sizeGrain < grain_)
|
|
|
|
sizeGrain = grain_;
|
2013-01-14 05:04:41 +00:00
|
|
|
|
2012-11-07 16:35:22 +00:00
|
|
|
// upalign size to grain
|
2013-03-04 04:20:49 +00:00
|
|
|
size = (size + sizeGrain - 1) & ~(sizeGrain - 1);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-11-05 12:36:12 +00:00
|
|
|
if (!fromTop)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2012-11-05 12:36:12 +00:00
|
|
|
//Allocate from bottom of mem
|
2013-05-27 06:09:27 +00:00
|
|
|
for (Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block &b = *bp;
|
2013-01-14 05:04:41 +00:00
|
|
|
u32 offset = b.start % grain;
|
2013-03-01 16:14:38 +00:00
|
|
|
if (offset != 0)
|
|
|
|
offset = grain - offset;
|
2013-01-14 05:04:41 +00:00
|
|
|
u32 needed = offset + size;
|
|
|
|
if (b.taken == false && b.size >= needed)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-01-14 05:04:41 +00:00
|
|
|
if (b.size == needed)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
if (offset >= grain_)
|
|
|
|
InsertFreeBefore(&b, offset);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2014-07-21 01:29:43 +00:00
|
|
|
return b.start;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
InsertFreeAfter(&b, b.size - needed);
|
|
|
|
if (offset >= grain_)
|
|
|
|
InsertFreeBefore(&b, offset);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2014-07-21 01:29:43 +00:00
|
|
|
return b.start;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-05 12:36:12 +00:00
|
|
|
// Allocate from top of mem.
|
2013-05-27 06:09:27 +00:00
|
|
|
for (Block *bp = top_; bp != NULL; bp = bp->prev)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block &b = *bp;
|
2013-03-01 16:14:38 +00:00
|
|
|
u32 offset = (b.start + b.size - size) % grain;
|
2013-01-14 05:04:41 +00:00
|
|
|
u32 needed = offset + size;
|
|
|
|
if (b.taken == false && b.size >= needed)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-01-14 05:04:41 +00:00
|
|
|
if (b.size == needed)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
if (offset >= grain_)
|
|
|
|
InsertFreeAfter(&b, offset);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2013-03-01 16:14:38 +00:00
|
|
|
return b.start;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
InsertFreeBefore(&b, b.size - needed);
|
|
|
|
if (offset >= grain_)
|
|
|
|
InsertFreeAfter(&b, offset);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2013-03-01 16:14:38 +00:00
|
|
|
return b.start;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Out of memory :(
|
|
|
|
ListBlocks();
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Block Allocator (%08x-%08x) failed to allocate %i (%08x) bytes of contiguous memory", rangeStart_, rangeStart_ + rangeSize_, size, size);
|
2012-11-01 15:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-01-14 05:04:41 +00:00
|
|
|
u32 BlockAllocator::Alloc(u32 &size, bool fromTop, const char *tag)
|
|
|
|
{
|
|
|
|
// We want to make sure it's aligned in case AllocAt() was used.
|
2013-03-04 04:20:49 +00:00
|
|
|
return AllocAligned(size, grain_, grain_, fromTop, tag);
|
2013-01-14 05:04:41 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 BlockAllocator::AllocAt(u32 position, u32 size, const char *tag)
|
|
|
|
{
|
2012-11-07 16:35:22 +00:00
|
|
|
CheckBlocks();
|
2012-11-05 12:36:12 +00:00
|
|
|
if (size > rangeSize_) {
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Clearly bogus size: %08x - failing allocation", size);
|
2012-12-31 07:22:30 +00:00
|
|
|
return -1;
|
2012-11-05 12:36:12 +00:00
|
|
|
}
|
2015-07-02 13:55:09 +00:00
|
|
|
|
2013-08-20 07:58:38 +00:00
|
|
|
// Downalign the position so we're allocating full blocks.
|
|
|
|
u32 alignedPosition = position;
|
|
|
|
u32 alignedSize = size;
|
2012-11-07 16:35:22 +00:00
|
|
|
if (position & (grain_ - 1)) {
|
2017-03-08 15:00:02 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "Position %08x does not align to grain.", position);
|
2013-08-20 07:58:38 +00:00
|
|
|
alignedPosition &= ~(grain_ - 1);
|
|
|
|
|
|
|
|
// Since the position was decreased, size must increase.
|
2020-08-05 10:43:45 +00:00
|
|
|
alignedSize += position - alignedPosition;
|
2012-11-07 16:35:22 +00:00
|
|
|
}
|
|
|
|
|
2013-08-20 07:58:38 +00:00
|
|
|
// Upalign size to grain.
|
|
|
|
alignedSize = (alignedSize + grain_ - 1) & ~(grain_ - 1);
|
|
|
|
// Tell the caller the allocated size from their requested starting position.
|
2020-08-05 11:01:00 +00:00
|
|
|
size = alignedSize - (position - alignedPosition);
|
2013-08-20 07:58:38 +00:00
|
|
|
|
|
|
|
Block *bp = GetBlockFromAddress(alignedPosition);
|
2013-05-27 06:09:27 +00:00
|
|
|
if (bp != NULL)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block &b = *bp;
|
2012-11-01 15:19:01 +00:00
|
|
|
if (b.taken)
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Block allocator AllocAt failed, block taken! %08x, %i", position, size);
|
2012-11-01 15:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-09-12 05:52:22 +00:00
|
|
|
// Make sure the block is big enough to split.
|
|
|
|
if (b.start + b.size < alignedPosition + alignedSize)
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Block allocator AllocAt failed, not enough contiguous space %08x, %i", position, size);
|
2013-09-12 05:52:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
//good to go
|
2013-09-12 05:52:22 +00:00
|
|
|
else if (b.start == alignedPosition)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
if (b.size != alignedSize)
|
|
|
|
InsertFreeAfter(&b, b.size - alignedSize);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2012-11-07 16:35:22 +00:00
|
|
|
CheckBlocks();
|
2012-11-01 15:19:01 +00:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
InsertFreeBefore(&b, alignedPosition - b.start);
|
|
|
|
if (b.size > alignedSize)
|
|
|
|
InsertFreeAfter(&b, b.size - alignedSize);
|
2012-11-01 15:19:01 +00:00
|
|
|
b.taken = true;
|
2021-02-02 08:08:05 +00:00
|
|
|
b.SetAllocated(tag, suballoc_);
|
2013-09-12 05:52:22 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Block allocator AllocAt failed :( %08x, %i", position, size);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:55:09 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
//Out of memory :(
|
|
|
|
ListBlocks();
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "Block Allocator (%08x-%08x) failed to allocate %i (%08x) bytes of contiguous memory", rangeStart_, rangeStart_ + rangeSize_, alignedSize, alignedSize);
|
2012-11-01 15:19:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
void BlockAllocator::MergeFreeBlocks(Block *fromBlock)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "Merging Blocks");
|
2013-05-27 06:09:27 +00:00
|
|
|
|
|
|
|
Block *prev = fromBlock->prev;
|
|
|
|
while (prev != NULL && prev->taken == false)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "Block Alloc found adjacent free blocks - merging");
|
2013-05-27 06:09:27 +00:00
|
|
|
prev->size += fromBlock->size;
|
|
|
|
if (fromBlock->next == NULL)
|
|
|
|
top_ = prev;
|
|
|
|
else
|
|
|
|
fromBlock->next->prev = prev;
|
|
|
|
prev->next = fromBlock->next;
|
|
|
|
delete fromBlock;
|
|
|
|
fromBlock = prev;
|
|
|
|
prev = fromBlock->prev;
|
2012-11-07 16:35:22 +00:00
|
|
|
}
|
2013-05-27 06:09:27 +00:00
|
|
|
|
|
|
|
if (prev == NULL)
|
|
|
|
bottom_ = fromBlock;
|
|
|
|
else
|
|
|
|
prev->next = fromBlock;
|
|
|
|
|
|
|
|
Block *next = fromBlock->next;
|
|
|
|
while (next != NULL && next->taken == false)
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "Block Alloc found adjacent free blocks - merging");
|
2013-05-27 06:09:27 +00:00
|
|
|
fromBlock->size += next->size;
|
|
|
|
fromBlock->next = next->next;
|
|
|
|
delete next;
|
|
|
|
next = fromBlock->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next == NULL)
|
|
|
|
top_ = fromBlock;
|
|
|
|
else
|
|
|
|
next->prev = fromBlock;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 18:10:34 +00:00
|
|
|
bool BlockAllocator::Free(u32 position)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block *b = GetBlockFromAddress(position);
|
2012-11-07 18:10:34 +00:00
|
|
|
if (b && b->taken)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2021-02-02 08:08:05 +00:00
|
|
|
NotifyMemInfo(suballoc_ ? MemBlockFlags::SUB_FREE : MemBlockFlags::FREE, b->start, b->size, "");
|
2012-11-01 15:19:01 +00:00
|
|
|
b->taken = false;
|
2013-05-27 06:09:27 +00:00
|
|
|
MergeFreeBlocks(b);
|
2012-11-07 18:10:34 +00:00
|
|
|
return true;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "BlockAllocator : invalid free %08x", position);
|
2012-11-07 18:10:34 +00:00
|
|
|
return false;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 08:47:06 +00:00
|
|
|
bool BlockAllocator::FreeExact(u32 position)
|
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block *b = GetBlockFromAddress(position);
|
2013-01-16 08:47:06 +00:00
|
|
|
if (b && b->taken && b->start == position)
|
2013-05-27 06:48:55 +00:00
|
|
|
{
|
2021-02-02 08:08:05 +00:00
|
|
|
NotifyMemInfo(suballoc_ ? MemBlockFlags::SUB_FREE : MemBlockFlags::FREE, b->start, b->size, "");
|
2013-05-27 06:48:55 +00:00
|
|
|
b->taken = false;
|
|
|
|
MergeFreeBlocks(b);
|
|
|
|
return true;
|
|
|
|
}
|
2013-01-16 08:47:06 +00:00
|
|
|
else
|
|
|
|
{
|
2017-03-08 15:00:02 +00:00
|
|
|
ERROR_LOG(SCEKERNEL, "BlockAllocator : invalid free %08x", position);
|
2013-01-16 08:47:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:29:43 +00:00
|
|
|
BlockAllocator::Block *BlockAllocator::InsertFreeBefore(Block *b, u32 size)
|
2013-05-27 06:09:27 +00:00
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
Block *inserted = new Block(b->start, size, false, b->prev, b);
|
2013-05-27 06:09:27 +00:00
|
|
|
b->prev = inserted;
|
|
|
|
if (inserted->prev == NULL)
|
|
|
|
bottom_ = inserted;
|
|
|
|
else
|
|
|
|
inserted->prev->next = inserted;
|
|
|
|
|
2014-07-21 01:29:43 +00:00
|
|
|
b->start += size;
|
|
|
|
b->size -= size;
|
2013-05-27 06:09:27 +00:00
|
|
|
return inserted;
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:29:43 +00:00
|
|
|
BlockAllocator::Block *BlockAllocator::InsertFreeAfter(Block *b, u32 size)
|
2013-05-27 06:09:27 +00:00
|
|
|
{
|
2014-07-21 01:29:43 +00:00
|
|
|
Block *inserted = new Block(b->start + b->size - size, size, false, b, b->next);
|
2013-05-27 06:09:27 +00:00
|
|
|
b->next = inserted;
|
|
|
|
if (inserted->next == NULL)
|
|
|
|
top_ = inserted;
|
|
|
|
else
|
|
|
|
inserted->next->prev = inserted;
|
|
|
|
|
2014-07-21 01:29:43 +00:00
|
|
|
b->size -= size;
|
2013-05-27 06:09:27 +00:00
|
|
|
return inserted;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockAllocator::CheckBlocks() const
|
2012-11-07 16:35:22 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-07 16:35:22 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block &b = *bp;
|
2012-11-07 16:35:22 +00:00
|
|
|
if (b.start > 0xc0000000) { // probably free'd debug values
|
2013-09-12 05:52:22 +00:00
|
|
|
ERROR_LOG_REPORT(HLE, "Bogus block in allocator");
|
|
|
|
}
|
|
|
|
// Outside the valid range, probably logic bug in allocation.
|
|
|
|
if (b.start + b.size > rangeStart_ + rangeSize_ || b.start < rangeStart_) {
|
|
|
|
ERROR_LOG_REPORT(HLE, "Bogus block in allocator");
|
2012-11-07 16:35:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:53:19 +00:00
|
|
|
const char *BlockAllocator::GetBlockTag(u32 addr) const {
|
|
|
|
const Block *b = GetBlockFromAddress(addr);
|
|
|
|
return b->tag;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:48:55 +00:00
|
|
|
inline BlockAllocator::Block *BlockAllocator::GetBlockFromAddress(u32 addr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
for (Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
Block &b = *bp;
|
|
|
|
if (b.start <= addr && b.start + b.size > addr)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
// Got one!
|
2013-05-27 06:09:27 +00:00
|
|
|
return bp;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-05-27 06:09:27 +00:00
|
|
|
return NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
const BlockAllocator::Block *BlockAllocator::GetBlockFromAddress(u32 addr) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
|
|
|
{
|
|
|
|
const Block &b = *bp;
|
|
|
|
if (b.start <= addr && b.start + b.size > addr)
|
|
|
|
{
|
|
|
|
// Got one!
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
u32 BlockAllocator::GetBlockStartFromAddress(u32 addr) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block *b = GetBlockFromAddress(addr);
|
2012-11-01 15:19:01 +00:00
|
|
|
if (b)
|
|
|
|
return b->start;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
u32 BlockAllocator::GetBlockSizeFromAddress(u32 addr) const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block *b = GetBlockFromAddress(addr);
|
2012-11-01 15:19:01 +00:00
|
|
|
if (b)
|
|
|
|
return b->size;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
void BlockAllocator::ListBlocks() const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2020-09-21 07:56:52 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL,"-----------");
|
2013-05-27 06:09:27 +00:00
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block &b = *bp;
|
2020-09-21 07:56:52 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL, "Block: %08x - %08x size %08x taken=%i tag=%s", b.start, b.start+b.size, b.size, b.taken ? 1:0, b.tag);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2020-09-21 07:56:52 +00:00
|
|
|
DEBUG_LOG(SCEKERNEL,"-----------");
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
u32 BlockAllocator::GetLargestFreeBlockSize() const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
u32 maxFreeBlock = 0;
|
2013-05-27 06:09:27 +00:00
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block &b = *bp;
|
2012-11-01 15:19:01 +00:00
|
|
|
if (!b.taken)
|
|
|
|
{
|
|
|
|
if (b.size > maxFreeBlock)
|
|
|
|
maxFreeBlock = b.size;
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 05:45:38 +00:00
|
|
|
if (maxFreeBlock & (grain_ - 1))
|
|
|
|
WARN_LOG_REPORT(HLE, "GetLargestFreeBlockSize: free size %08x does not align to grain %08x.", maxFreeBlock, grain_);
|
2012-11-01 15:19:01 +00:00
|
|
|
return maxFreeBlock;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
u32 BlockAllocator::GetTotalFreeBytes() const
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
u32 sum = 0;
|
2013-05-27 06:09:27 +00:00
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block &b = *bp;
|
2012-11-01 15:19:01 +00:00
|
|
|
if (!b.taken)
|
|
|
|
{
|
|
|
|
sum += b.size;
|
|
|
|
}
|
|
|
|
}
|
2013-08-20 05:45:38 +00:00
|
|
|
if (sum & (grain_ - 1))
|
|
|
|
WARN_LOG_REPORT(HLE, "GetTotalFreeBytes: free size %08x does not align to grain %08x.", sum, grain_);
|
2012-11-01 15:19:01 +00:00
|
|
|
return sum;
|
|
|
|
}
|
2012-12-27 05:07:29 +00:00
|
|
|
|
|
|
|
void BlockAllocator::DoState(PointerWrap &p)
|
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("BlockAllocator", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (p.mode == p.MODE_READ)
|
|
|
|
{
|
|
|
|
Shutdown();
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, count);
|
2013-05-27 06:09:27 +00:00
|
|
|
|
|
|
|
bottom_ = new Block(0, 0, false, NULL, NULL);
|
|
|
|
bottom_->DoState(p);
|
|
|
|
--count;
|
|
|
|
|
|
|
|
top_ = bottom_;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
top_->next = new Block(0, 0, false, top_, NULL);
|
|
|
|
top_->next->DoState(p);
|
|
|
|
top_ = top_->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const Block *bp = bottom_; bp != NULL; bp = bp->next)
|
|
|
|
++count;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, count);
|
2013-05-27 06:09:27 +00:00
|
|
|
|
|
|
|
bottom_->DoState(p);
|
|
|
|
--count;
|
|
|
|
|
|
|
|
Block *last = bottom_;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
last->next->DoState(p);
|
|
|
|
last = last->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, rangeStart_);
|
|
|
|
Do(p, rangeSize_);
|
|
|
|
Do(p, grain_);
|
2012-12-27 05:07:29 +00:00
|
|
|
}
|
2013-02-04 06:10:06 +00:00
|
|
|
|
2013-12-31 05:37:19 +00:00
|
|
|
BlockAllocator::Block::Block(u32 _start, u32 _size, bool _taken, Block *_prev, Block *_next)
|
|
|
|
: start(_start), size(_size), taken(_taken), prev(_prev), next(_next)
|
|
|
|
{
|
2017-02-24 17:59:41 +00:00
|
|
|
truncate_cpy(tag, "(untitled)");
|
2013-12-31 05:37:19 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 08:08:05 +00:00
|
|
|
void BlockAllocator::Block::SetAllocated(const char *_tag, bool suballoc) {
|
2021-02-08 03:10:08 +00:00
|
|
|
NotifyMemInfo(suballoc ? MemBlockFlags::SUB_ALLOC : MemBlockFlags::ALLOC, start, size, _tag ? _tag : "");
|
2013-12-31 05:37:19 +00:00
|
|
|
if (_tag)
|
2017-02-24 17:59:41 +00:00
|
|
|
truncate_cpy(tag, _tag);
|
2013-12-31 05:37:19 +00:00
|
|
|
else
|
2017-02-24 17:59:41 +00:00
|
|
|
truncate_cpy(tag, "---");
|
2013-12-31 05:37:19 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 06:10:06 +00:00
|
|
|
void BlockAllocator::Block::DoState(PointerWrap &p)
|
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("Block", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, start);
|
|
|
|
Do(p, size);
|
|
|
|
Do(p, taken);
|
2018-06-23 04:25:07 +00:00
|
|
|
// Since we use truncate_cpy, the empty space is not zeroed. Zero it now.
|
|
|
|
// This avoids saving uninitialized memory.
|
|
|
|
size_t tagLen = strlen(tag);
|
|
|
|
if (tagLen != sizeof(tag))
|
|
|
|
memset(tag + tagLen, 0, sizeof(tag) - tagLen);
|
2020-08-10 04:20:42 +00:00
|
|
|
DoArray(p, tag, sizeof(tag));
|
2013-02-04 06:10:06 +00:00
|
|
|
}
|