mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #55 from oneandonlydean/perf/mspace-ordered-allocator
perf(libc): replace the mspace block list with an ordered allocator
This commit is contained in:
+162
-105
@@ -2,73 +2,128 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <utility>
|
||||
|
||||
extern "C" int* APS5_VABI __error_nid_postfix();
|
||||
|
||||
namespace {
|
||||
struct alignas(16) Block {
|
||||
std::size_t capacity;
|
||||
Block* next;
|
||||
void* pointer;
|
||||
std::size_t used;
|
||||
};
|
||||
struct alignas(16) Arena {
|
||||
Arena* next;
|
||||
Block* first;
|
||||
std::uintptr_t end;
|
||||
std::size_t inUse;
|
||||
std::size_t peakInUse;
|
||||
};
|
||||
|
||||
constexpr unsigned MspaceThreadUnsafe = 1;
|
||||
constexpr std::uintptr_t Granule = 16;
|
||||
constexpr std::uintptr_t ArenaHeaderBytes = 64;
|
||||
|
||||
struct Chunk {
|
||||
std::uintptr_t end;
|
||||
bool used;
|
||||
std::size_t requested;
|
||||
};
|
||||
|
||||
struct Arena {
|
||||
std::uintptr_t base;
|
||||
std::uintptr_t end;
|
||||
std::map<std::uintptr_t, Chunk> chunks;
|
||||
std::set<std::pair<std::size_t, std::uintptr_t>> free;
|
||||
std::size_t inUse = 0;
|
||||
std::size_t peakInUse = 0;
|
||||
};
|
||||
|
||||
std::mutex arenaMutex;
|
||||
Arena* arenas = nullptr;
|
||||
std::map<std::uintptr_t, std::unique_ptr<Arena>> arenas;
|
||||
|
||||
void Error(int value) { *__error_nid_postfix() = value; }
|
||||
|
||||
std::uintptr_t AlignUp(std::uintptr_t value, std::uintptr_t alignment) {
|
||||
return (value + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
Arena* Find(void* handle) {
|
||||
for (auto* arena = arenas; arena; arena = arena->next)
|
||||
if (arena == handle) return arena;
|
||||
const auto found = arenas.find(reinterpret_cast<std::uintptr_t>(handle));
|
||||
if (found != arenas.end()) return found->second.get();
|
||||
Error(22);
|
||||
return nullptr;
|
||||
}
|
||||
Block* FindBlock(Arena* arena, const void* pointer) {
|
||||
if (arena && pointer)
|
||||
for (auto* block = arena->first; block; block = block->next)
|
||||
if (block->pointer == pointer) return block;
|
||||
Error(22);
|
||||
return nullptr;
|
||||
|
||||
void AddFree(Arena& arena, std::uintptr_t start, std::uintptr_t end) {
|
||||
arena.chunks[start] = {end, false, 0};
|
||||
arena.free.emplace(end - start, start);
|
||||
}
|
||||
|
||||
void RemoveFree(Arena& arena, std::map<std::uintptr_t, Chunk>::iterator chunk) {
|
||||
arena.free.erase({chunk->second.end - chunk->first, chunk->first});
|
||||
}
|
||||
|
||||
void* Allocate(Arena* arena, std::size_t size, std::size_t alignment) {
|
||||
if (!arena) return nullptr;
|
||||
size = std::max<std::size_t>(size, 1);
|
||||
for (auto* block = arena->first; block; block = block->next) {
|
||||
if (block->pointer) continue;
|
||||
const auto start = reinterpret_cast<std::uintptr_t>(block + 1);
|
||||
if (start > std::numeric_limits<std::uintptr_t>::max() - (alignment - 1)) continue;
|
||||
const auto aligned = (start + alignment - 1) & ~(alignment - 1);
|
||||
const auto padding = aligned - start;
|
||||
if (padding > block->capacity || size > block->capacity - padding) continue;
|
||||
auto consumed = padding + size;
|
||||
// Splitting preserves header alignment and avoids small unusable fragments.
|
||||
if (consumed <= std::numeric_limits<std::size_t>::max() - 15) {
|
||||
const auto rounded = (consumed + 15) & ~std::size_t{15};
|
||||
if (rounded <= block->capacity && block->capacity - rounded >= sizeof(Block) + 16) {
|
||||
auto* tail = reinterpret_cast<Block*>(start + rounded);
|
||||
*tail = {block->capacity - rounded - sizeof(Block), block->next, nullptr, 0};
|
||||
block->capacity = rounded;
|
||||
block->next = tail;
|
||||
}
|
||||
}
|
||||
block->pointer = reinterpret_cast<void*>(aligned);
|
||||
block->used = size;
|
||||
arena->inUse += sizeof(Block) + block->capacity;
|
||||
alignment = std::max<std::size_t>(alignment, Granule);
|
||||
const auto needed = AlignUp(std::max<std::size_t>(size, 1), Granule);
|
||||
if (needed < size || needed > std::numeric_limits<std::uintptr_t>::max() - alignment) {
|
||||
Error(12);
|
||||
return nullptr;
|
||||
}
|
||||
for (auto candidate = arena->free.lower_bound({needed, 0}); candidate != arena->free.end(); ++candidate) {
|
||||
const auto start = candidate->second;
|
||||
const auto chunk = arena->chunks.find(start);
|
||||
const auto end = chunk->second.end;
|
||||
const auto aligned = AlignUp(start, alignment);
|
||||
if (aligned < start || aligned > end || end - aligned < needed) continue;
|
||||
RemoveFree(*arena, chunk);
|
||||
arena->chunks.erase(chunk);
|
||||
if (aligned > start) AddFree(*arena, start, aligned);
|
||||
const auto finish = aligned + needed;
|
||||
if (finish < end) AddFree(*arena, finish, end);
|
||||
arena->chunks[aligned] = {finish, true, size};
|
||||
arena->inUse += needed;
|
||||
arena->peakInUse = std::max(arena->peakInUse, arena->inUse);
|
||||
return block->pointer;
|
||||
return reinterpret_cast<void*>(aligned);
|
||||
}
|
||||
Error(12);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool FindUsed(Arena* arena, const void* pointer, std::map<std::uintptr_t, Chunk>::iterator& chunk) {
|
||||
if (arena && pointer) {
|
||||
chunk = arena->chunks.find(reinterpret_cast<std::uintptr_t>(pointer));
|
||||
if (chunk != arena->chunks.end() && chunk->second.used) return true;
|
||||
}
|
||||
Error(22);
|
||||
return false;
|
||||
}
|
||||
|
||||
void Release(Arena& arena, std::map<std::uintptr_t, Chunk>::iterator chunk) {
|
||||
auto start = chunk->first;
|
||||
auto end = chunk->second.end;
|
||||
arena.inUse -= end - start;
|
||||
if (chunk != arena.chunks.begin()) {
|
||||
const auto previous = std::prev(chunk);
|
||||
if (!previous->second.used) {
|
||||
start = previous->first;
|
||||
RemoveFree(arena, previous);
|
||||
arena.chunks.erase(previous);
|
||||
}
|
||||
}
|
||||
const auto next = std::next(chunk);
|
||||
if (next != arena.chunks.end() && !next->second.used) {
|
||||
end = next->second.end;
|
||||
RemoveFree(arena, next);
|
||||
arena.chunks.erase(next);
|
||||
}
|
||||
arena.chunks.erase(chunk);
|
||||
AddFree(arena, start, end);
|
||||
}
|
||||
|
||||
bool InsideAllocation(const Arena& arena, std::uintptr_t start, std::size_t size) {
|
||||
auto found = arena.chunks.upper_bound(start);
|
||||
if (found == arena.chunks.begin()) return false;
|
||||
--found;
|
||||
return found->second.used && size <= found->second.end - start && start - found->first <= found->second.end - found->first - size;
|
||||
}
|
||||
|
||||
struct MallocManagedSize {
|
||||
std::uint16_t size;
|
||||
std::uint16_t version;
|
||||
@@ -85,7 +140,7 @@ int FillStats(void* handle, MallocManagedSize* stats) {
|
||||
std::lock_guard lock(arenaMutex);
|
||||
auto* arena = Find(handle);
|
||||
if (!arena) return 22;
|
||||
const auto system = arena->end - reinterpret_cast<std::uintptr_t>(arena);
|
||||
const auto system = arena->end - arena->base;
|
||||
stats->maxSystemSize = system;
|
||||
stats->currentSystemSize = system;
|
||||
stats->maxInuseSize = arena->peakInUse;
|
||||
@@ -93,25 +148,6 @@ int FillStats(void* handle, MallocManagedSize* stats) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool InsideAllocation(Arena* arena, std::uintptr_t start, std::size_t size) {
|
||||
for (auto* block = arena->first; block; block = block->next) {
|
||||
const auto pointer = reinterpret_cast<std::uintptr_t>(block->pointer);
|
||||
if (pointer && start >= pointer && size <= block->used && start - pointer <= block->used - size) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Release(Arena* arena, Block* released) {
|
||||
arena->inUse -= sizeof(Block) + released->capacity;
|
||||
released->pointer = nullptr;
|
||||
released->used = 0;
|
||||
for (auto* block = arena->first; block && block->next;) {
|
||||
if (!block->pointer && !block->next->pointer) {
|
||||
block->capacity += sizeof(Block) + block->next->capacity;
|
||||
block->next = block->next->next;
|
||||
} else block = block->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
@@ -119,48 +155,46 @@ void* APS5_VABI sceLibcMspaceCreate_nid_postfix(const char* name, void* base,
|
||||
std::size_t size, unsigned flags) {
|
||||
(void)name;
|
||||
const auto start = reinterpret_cast<std::uintptr_t>(base);
|
||||
if (!base || (start & 15) || size < sizeof(Arena) + sizeof(Block) + 16 ||
|
||||
if (!base || (start & (Granule - 1)) || size < ArenaHeaderBytes + 2 * Granule ||
|
||||
size > std::numeric_limits<std::uintptr_t>::max() - start || (flags & ~MspaceThreadUnsafe) != 0) {
|
||||
Error(22);
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard lock(arenaMutex);
|
||||
for (auto* arena = arenas; arena; arena = arena->next) {
|
||||
if (start < arena->end && reinterpret_cast<std::uintptr_t>(arena) < start + size && !InsideAllocation(arena, start, size)) {
|
||||
for (const auto& [otherBase, other] : arenas) {
|
||||
if (start < other->end && otherBase < start + size && !InsideAllocation(*other, start, size)) {
|
||||
Error(22);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
auto* arena = static_cast<Arena*>(base);
|
||||
auto* block = reinterpret_cast<Block*>(arena + 1);
|
||||
*block = {size - sizeof(Arena) - sizeof(Block), nullptr, nullptr, 0};
|
||||
*arena = {arenas, block, start + size, 0, 0};
|
||||
arenas = arena;
|
||||
return arena;
|
||||
auto arena = std::make_unique<Arena>();
|
||||
arena->base = start;
|
||||
arena->end = start + size;
|
||||
const auto first = start + ArenaHeaderBytes;
|
||||
const auto last = start + (size & ~(Granule - 1));
|
||||
AddFree(*arena, first, last);
|
||||
arenas.emplace(start, std::move(arena));
|
||||
return base;
|
||||
}
|
||||
|
||||
int APS5_VABI sceLibcMspaceDestroy_nid_postfix(void* handle) {
|
||||
std::lock_guard lock(arenaMutex);
|
||||
for (auto** entry = &arenas; *entry; entry = &(*entry)->next) {
|
||||
if (*entry == handle) {
|
||||
*entry = (*entry)->next;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (arenas.erase(reinterpret_cast<std::uintptr_t>(handle)) != 0) return 0;
|
||||
Error(22);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void* APS5_VABI sceLibcMspaceMalloc_nid_postfix(void* handle, std::size_t size) {
|
||||
std::lock_guard lock(arenaMutex);
|
||||
return Allocate(Find(handle), size, 16);
|
||||
return Allocate(Find(handle), size, Granule);
|
||||
}
|
||||
|
||||
void APS5_VABI sceLibcMspaceFree_nid_postfix(void* handle, void* pointer) {
|
||||
if (!pointer) return;
|
||||
std::lock_guard lock(arenaMutex);
|
||||
auto* arena = Find(handle);
|
||||
if (auto* block = FindBlock(arena, pointer)) Release(arena, block);
|
||||
std::map<std::uintptr_t, Chunk>::iterator chunk;
|
||||
if (FindUsed(arena, pointer, chunk)) Release(*arena, chunk);
|
||||
}
|
||||
|
||||
void* APS5_VABI sceLibcMspaceCalloc_nid_postfix(void* handle, std::size_t count, std::size_t size) {
|
||||
@@ -169,7 +203,7 @@ void* APS5_VABI sceLibcMspaceCalloc_nid_postfix(void* handle, std::size_t count,
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard lock(arenaMutex);
|
||||
void* result = Allocate(Find(handle), count * size, 16);
|
||||
void* result = Allocate(Find(handle), count * size, Granule);
|
||||
if (result) std::memset(result, 0, count * size);
|
||||
return result;
|
||||
}
|
||||
@@ -177,16 +211,34 @@ void* APS5_VABI sceLibcMspaceCalloc_nid_postfix(void* handle, std::size_t count,
|
||||
void* APS5_VABI sceLibcMspaceRealloc_nid_postfix(void* handle, void* pointer, std::size_t size) {
|
||||
std::lock_guard lock(arenaMutex);
|
||||
auto* arena = Find(handle);
|
||||
if (!pointer) return Allocate(arena, size, 16);
|
||||
auto* block = FindBlock(arena, pointer);
|
||||
if (!block) return nullptr;
|
||||
if (!size) { Release(arena, block); return nullptr; }
|
||||
const auto padding = reinterpret_cast<std::uintptr_t>(pointer) - reinterpret_cast<std::uintptr_t>(block + 1);
|
||||
if (size <= block->capacity - padding) { block->used = size; return pointer; }
|
||||
void* result = Allocate(arena, size, 16);
|
||||
if (!pointer) return Allocate(arena, size, Granule);
|
||||
std::map<std::uintptr_t, Chunk>::iterator chunk;
|
||||
if (!FindUsed(arena, pointer, chunk)) return nullptr;
|
||||
if (!size) { Release(*arena, chunk); return nullptr; }
|
||||
const auto needed = AlignUp(size, Granule);
|
||||
const auto start = chunk->first;
|
||||
const auto capacity = chunk->second.end - start;
|
||||
if (needed <= capacity) {
|
||||
chunk->second.requested = size;
|
||||
return pointer;
|
||||
}
|
||||
const auto next = std::next(chunk);
|
||||
if (next != arena->chunks.end() && !next->second.used && next->first == chunk->second.end && next->second.end - start >= needed) {
|
||||
const auto nextEnd = next->second.end;
|
||||
RemoveFree(*arena, next);
|
||||
arena->chunks.erase(next);
|
||||
const auto finish = start + needed;
|
||||
if (finish < nextEnd) AddFree(*arena, finish, nextEnd);
|
||||
arena->inUse += needed - capacity;
|
||||
arena->peakInUse = std::max(arena->peakInUse, arena->inUse);
|
||||
arena->chunks[start] = {finish, true, size};
|
||||
return pointer;
|
||||
}
|
||||
const auto previousSize = chunk->second.requested;
|
||||
void* result = Allocate(arena, size, Granule);
|
||||
if (result) {
|
||||
std::memcpy(result, pointer, block->used);
|
||||
Release(arena, block);
|
||||
std::memcpy(result, pointer, previousSize);
|
||||
Release(*arena, arena->chunks.find(start));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -197,7 +249,7 @@ int APS5_VABI sceLibcMspacePosixMemalign_nid_postfix(void* handle, void** result
|
||||
std::lock_guard lock(arenaMutex);
|
||||
auto* arena = Find(handle);
|
||||
if (!arena) return 22;
|
||||
void* pointer = Allocate(arena, size, std::max<std::size_t>(alignment, 16));
|
||||
void* pointer = Allocate(arena, size, alignment);
|
||||
if (!pointer) return 12;
|
||||
*result = pointer;
|
||||
return 0;
|
||||
@@ -209,7 +261,7 @@ void* APS5_VABI sceLibcMspaceMemalign_nid_postfix(void* handle, std::size_t alig
|
||||
return nullptr;
|
||||
}
|
||||
std::lock_guard lock(arenaMutex);
|
||||
return Allocate(Find(handle), size, std::max<std::size_t>(alignment, 16));
|
||||
return Allocate(Find(handle), size, alignment);
|
||||
}
|
||||
|
||||
void* APS5_VABI sceLibcMspaceReallocalign_nid_postfix(void* handle, void* pointer, std::size_t size, std::size_t alignment) {
|
||||
@@ -221,18 +273,19 @@ void* APS5_VABI sceLibcMspaceReallocalign_nid_postfix(void* handle, void* pointe
|
||||
std::lock_guard lock(arenaMutex);
|
||||
auto* arena = Find(handle);
|
||||
if (!pointer) return Allocate(arena, size, effective);
|
||||
auto* block = FindBlock(arena, pointer);
|
||||
if (!block) return nullptr;
|
||||
if (!size) { Release(arena, block); return nullptr; }
|
||||
const auto padding = reinterpret_cast<std::uintptr_t>(pointer) - reinterpret_cast<std::uintptr_t>(block + 1);
|
||||
if (size <= block->capacity - padding && (reinterpret_cast<std::uintptr_t>(pointer) & (effective - 1)) == 0) {
|
||||
block->used = size;
|
||||
std::map<std::uintptr_t, Chunk>::iterator chunk;
|
||||
if (!FindUsed(arena, pointer, chunk)) return nullptr;
|
||||
if (!size) { Release(*arena, chunk); return nullptr; }
|
||||
const auto start = chunk->first;
|
||||
if (AlignUp(size, Granule) <= chunk->second.end - start && (start & (effective - 1)) == 0) {
|
||||
chunk->second.requested = size;
|
||||
return pointer;
|
||||
}
|
||||
const auto previousSize = chunk->second.requested;
|
||||
void* result = Allocate(arena, size, effective);
|
||||
if (result) {
|
||||
std::memcpy(result, pointer, std::min(block->used, size));
|
||||
Release(arena, block);
|
||||
std::memcpy(result, pointer, std::min(previousSize, size));
|
||||
Release(*arena, arena->chunks.find(start));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -248,9 +301,13 @@ int APS5_VABI sceLibcMspaceMallocStatsFast_nid_postfix(void* handle, MallocManag
|
||||
std::size_t APS5_VABI sceLibcMspaceMallocUsableSize_nid_postfix(const void* pointer) {
|
||||
if (!pointer) return 0;
|
||||
std::lock_guard lock(arenaMutex);
|
||||
for (auto* arena = arenas; arena; arena = arena->next)
|
||||
for (auto* block = arena->first; block; block = block->next)
|
||||
if (block->pointer == pointer) return block->used;
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(pointer);
|
||||
for (auto arena = arenas.upper_bound(address); arena != arenas.begin();) {
|
||||
--arena;
|
||||
if (address >= arena->second->end) continue;
|
||||
const auto found = arena->second->chunks.find(address);
|
||||
if (found != arena->second->chunks.end() && found->second.used) return found->second.end - found->first;
|
||||
}
|
||||
Error(22);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,21 @@ int main() {
|
||||
void* large = sceLibcMspaceMalloc_nid_postfix(arena, storage.size() - 256);
|
||||
Require(large != nullptr); // freeing coalesced all the fragmented blocks
|
||||
sceLibcMspaceFree_nid_postfix(arena, large);
|
||||
void* head = sceLibcMspaceMalloc_nid_postfix(arena, 64);
|
||||
Require(head != nullptr && sceLibcMspaceRealloc_nid_postfix(arena, head, 1024) == head);
|
||||
Require(sceLibcMspaceMallocUsableSize_nid_postfix(head) == 1024);
|
||||
sceLibcMspaceFree_nid_postfix(arena, head);
|
||||
std::array<void*, 2000> small{};
|
||||
for (auto& pointer : small) {
|
||||
pointer = sceLibcMspaceMalloc_nid_postfix(arena, 16);
|
||||
Require(pointer != nullptr);
|
||||
}
|
||||
for (std::size_t i = 0; i < small.size(); i += 2) sceLibcMspaceFree_nid_postfix(arena, small[i]);
|
||||
Require(sceLibcMspaceMalloc_nid_postfix(arena, storage.size() - 256) == nullptr);
|
||||
for (std::size_t i = 1; i < small.size(); i += 2) sceLibcMspaceFree_nid_postfix(arena, small[i]);
|
||||
large = sceLibcMspaceMalloc_nid_postfix(arena, storage.size() - 256);
|
||||
Require(large != nullptr);
|
||||
sceLibcMspaceFree_nid_postfix(arena, large);
|
||||
std::array<std::thread, 4> workers;
|
||||
for (auto& worker : workers) worker = std::thread([&] {
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
@@ -98,6 +113,7 @@ int main() {
|
||||
Require(nested == region);
|
||||
void* inner = sceLibcMspaceMalloc_nid_postfix(nested, 64);
|
||||
Require(inner > region && inner < static_cast<unsigned char*>(region) + 8192);
|
||||
Require(sceLibcMspaceMallocUsableSize_nid_postfix(inner) == 64 && sceLibcMspaceMallocUsableSize_nid_postfix(region) == 8192);
|
||||
Require(sceLibcMspaceCreate_nid_postfix("flags", storage.data(), storage.size(), 2) == nullptr);
|
||||
Require(sceLibcMspaceDestroy_nid_postfix(nested) == 0);
|
||||
sceLibcMspaceFree_nid_postfix(arena, region);
|
||||
|
||||
Reference in New Issue
Block a user