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/.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2013-02-04 04:31:46 +00:00
|
|
|
class PointerWrap;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-12-30 09:17:11 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
class BlockAllocator
|
|
|
|
{
|
|
|
|
public:
|
2024-10-28 16:46:32 +00:00
|
|
|
BlockAllocator(int grain = 16) : grain_(grain) {} // 16 byte granularity by default.
|
2012-11-01 15:19:01 +00:00
|
|
|
~BlockAllocator();
|
|
|
|
|
2021-02-02 08:08:05 +00:00
|
|
|
void Init(u32 _rangeStart, u32 _rangeSize, bool suballoc);
|
2012-11-01 15:19:01 +00:00
|
|
|
void Shutdown();
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
void ListBlocks() const;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2012-12-27 05:07:29 +00:00
|
|
|
// WARNING: size can be modified upwards!
|
2012-11-05 11:02:09 +00:00
|
|
|
u32 Alloc(u32 &size, bool fromTop = false, const char *tag = 0);
|
2013-03-04 04:20:49 +00:00
|
|
|
u32 AllocAligned(u32 &size, u32 sizeGrain, u32 grain, bool fromTop = false, const char *tag = 0);
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 AllocAt(u32 position, u32 size, const char *tag = 0);
|
|
|
|
|
2012-11-07 18:10:34 +00:00
|
|
|
bool Free(u32 position);
|
2013-01-16 08:47:06 +00:00
|
|
|
bool FreeExact(u32 position);
|
2012-11-01 15:19:01 +00:00
|
|
|
bool IsBlockFree(u32 position) {
|
|
|
|
Block *b = GetBlockFromAddress(position);
|
|
|
|
if (b)
|
|
|
|
return !b->taken;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
u32 GetBlockStartFromAddress(u32 addr) const;
|
|
|
|
u32 GetBlockSizeFromAddress(u32 addr) const;
|
|
|
|
u32 GetLargestFreeBlockSize() const;
|
|
|
|
u32 GetTotalFreeBytes() const;
|
2012-11-05 11:02:09 +00:00
|
|
|
|
2015-04-27 14:53:19 +00:00
|
|
|
const char *GetBlockTag(u32 addr) const;
|
|
|
|
|
2012-12-27 05:07:29 +00:00
|
|
|
void DoState(PointerWrap &p);
|
|
|
|
|
2012-11-05 11:02:09 +00:00
|
|
|
private:
|
2013-05-27 06:09:27 +00:00
|
|
|
void CheckBlocks() const;
|
2012-11-07 16:35:22 +00:00
|
|
|
|
2024-10-28 16:46:32 +00:00
|
|
|
struct Block {
|
2013-12-31 05:37:19 +00:00
|
|
|
Block(u32 _start, u32 _size, bool _taken, Block *_prev, Block *_next);
|
2021-02-02 08:08:05 +00:00
|
|
|
void SetAllocated(const char *_tag, bool suballoc);
|
2013-02-04 06:10:06 +00:00
|
|
|
void DoState(PointerWrap &p);
|
2012-11-05 11:02:09 +00:00
|
|
|
u32 start;
|
|
|
|
u32 size;
|
|
|
|
bool taken;
|
2012-11-07 18:10:34 +00:00
|
|
|
char tag[32];
|
2013-05-27 06:09:27 +00:00
|
|
|
Block *prev;
|
|
|
|
Block *next;
|
2012-11-05 11:02:09 +00:00
|
|
|
};
|
|
|
|
|
2024-10-28 16:46:32 +00:00
|
|
|
Block *bottom_ = nullptr;
|
|
|
|
Block *top_ = nullptr;
|
|
|
|
u32 rangeStart_ = 0;
|
|
|
|
u32 rangeSize_ = 0;
|
2012-11-05 11:02:09 +00:00
|
|
|
|
2012-11-07 16:35:22 +00:00
|
|
|
u32 grain_;
|
2024-10-28 16:46:32 +00:00
|
|
|
bool suballoc_ = false;
|
2012-11-07 16:35:22 +00:00
|
|
|
|
2013-05-27 06:09:27 +00:00
|
|
|
void MergeFreeBlocks(Block *fromBlock);
|
2012-11-05 11:02:09 +00:00
|
|
|
Block *GetBlockFromAddress(u32 addr);
|
2013-05-27 06:09:27 +00:00
|
|
|
const Block *GetBlockFromAddress(u32 addr) const;
|
2014-07-21 01:29:43 +00:00
|
|
|
Block *InsertFreeBefore(Block *b, u32 size);
|
|
|
|
Block *InsertFreeAfter(Block *b, u32 size);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|