Merge pull request #49 from oneandonlydean/feat/mspace-extensions

feat(libc): mspace memalign, malloc stats, thread-unsafe flag and nested mspaces
- Memalign uses the existing aligned allocation.
- Stats fill `SceLibcMallocManagedSize` (size checked against 0x28) from new `inUse`/`peakInUse` counters on the arena. The system size is the arena's extent.
- The thread-unsafe flag is accepted: the implementation always locks, so skipping locks is only an optimisation it doesn't need. Other flags are still rejected.
- An overlapping create is allowed only when the new range lies entirely inside a live block of the other mspace. Every other overlap is still rejected.
This commit is contained in:
boykopovar
2026-09-28 08:58:28 +03:00
committed by GitHub
2 changed files with 91 additions and 3 deletions
+58 -3
View File
@@ -18,7 +18,10 @@ struct alignas(16) Arena {
Arena* next;
Block* first;
std::uintptr_t end;
std::size_t inUse;
std::size_t peakInUse;
};
constexpr unsigned MspaceThreadUnsafe = 1;
std::mutex arenaMutex;
Arena* arenas = nullptr;
@@ -59,12 +62,47 @@ void* Allocate(Arena* arena, std::size_t size, std::size_t alignment) {
}
block->pointer = reinterpret_cast<void*>(aligned);
block->used = size;
arena->inUse += sizeof(Block) + block->capacity;
arena->peakInUse = std::max(arena->peakInUse, arena->inUse);
return block->pointer;
}
Error(12);
return nullptr;
}
struct MallocManagedSize {
std::uint16_t size;
std::uint16_t version;
std::uint32_t reserved;
std::size_t maxSystemSize;
std::size_t currentSystemSize;
std::size_t maxInuseSize;
std::size_t currentInuseSize;
};
static_assert(sizeof(MallocManagedSize) == 0x28);
int FillStats(void* handle, MallocManagedSize* stats) {
if (!stats || stats->size < sizeof(MallocManagedSize)) return 22;
std::lock_guard lock(arenaMutex);
auto* arena = Find(handle);
if (!arena) return 22;
const auto system = arena->end - reinterpret_cast<std::uintptr_t>(arena);
stats->maxSystemSize = system;
stats->currentSystemSize = system;
stats->maxInuseSize = arena->peakInUse;
stats->currentInuseSize = arena->inUse;
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;) {
@@ -82,13 +120,13 @@ void* APS5_VABI sceLibcMspaceCreate_nid_postfix(const char* name, void* base,
(void)name;
const auto start = reinterpret_cast<std::uintptr_t>(base);
if (!base || (start & 15) || size < sizeof(Arena) + sizeof(Block) + 16 ||
size > std::numeric_limits<std::uintptr_t>::max() - start || flags != 0) {
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) {
if (start < arena->end && reinterpret_cast<std::uintptr_t>(arena) < start + size && !InsideAllocation(arena, start, size)) {
Error(22);
return nullptr;
}
@@ -96,7 +134,7 @@ void* APS5_VABI sceLibcMspaceCreate_nid_postfix(const char* name, void* base,
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};
*arena = {arenas, block, start + size, 0, 0};
arenas = arena;
return arena;
}
@@ -165,6 +203,23 @@ int APS5_VABI sceLibcMspacePosixMemalign_nid_postfix(void* handle, void** result
return 0;
}
void* APS5_VABI sceLibcMspaceMemalign_nid_postfix(void* handle, std::size_t alignment, std::size_t size) {
if (alignment == 0 || (alignment & (alignment - 1))) {
Error(22);
return nullptr;
}
std::lock_guard lock(arenaMutex);
return Allocate(Find(handle), size, std::max<std::size_t>(alignment, 16));
}
int APS5_VABI sceLibcMspaceMallocStats_nid_postfix(void* handle, MallocManagedSize* stats) {
return FillStats(handle, stats);
}
int APS5_VABI sceLibcMspaceMallocStatsFast_nid_postfix(void* handle, MallocManagedSize* stats) {
return FillStats(handle, stats);
}
std::size_t APS5_VABI sceLibcMspaceMallocUsableSize_nid_postfix(const void* pointer) {
if (!pointer) return 0;
std::lock_guard lock(arenaMutex);
+33
View File
@@ -16,7 +16,19 @@ void* APS5_VABI sceLibcMspaceRealloc_nid_postfix(void*, void*, std::size_t);
void APS5_VABI sceLibcMspaceFree_nid_postfix(void*, void*);
int APS5_VABI sceLibcMspacePosixMemalign_nid_postfix(void*, void**, std::size_t, std::size_t);
std::size_t APS5_VABI sceLibcMspaceMallocUsableSize_nid_postfix(const void*);
void* APS5_VABI sceLibcMspaceMemalign_nid_postfix(void*, std::size_t, std::size_t);
int APS5_VABI sceLibcMspaceMallocStats_nid_postfix(void*, void*);
int APS5_VABI sceLibcMspaceMallocStatsFast_nid_postfix(void*, void*);
}
struct MallocManagedSize {
std::uint16_t size;
std::uint16_t version;
std::uint32_t reserved;
std::size_t maxSystemSize;
std::size_t currentSystemSize;
std::size_t maxInuseSize;
std::size_t currentInuseSize;
};
static void Require(bool value) {
if (!value) { std::fputs("Mspace check failed\n", stderr); std::abort(); }
}
@@ -58,6 +70,27 @@ int main() {
}
});
for (auto& worker : workers) worker.join();
MallocManagedSize stats{sizeof(MallocManagedSize), 1, 0, 0, 0, 0, 0};
Require(sceLibcMspaceMallocStats_nid_postfix(arena, &stats) == 0);
Require(stats.currentSystemSize == storage.size() && stats.maxSystemSize == storage.size());
Require(stats.currentInuseSize == 0 && stats.maxInuseSize >= storage.size() - 256);
MallocManagedSize shortStats{8, 1, 0, 0, 0, 0, 0};
Require(sceLibcMspaceMallocStatsFast_nid_postfix(arena, &shortStats) == 22);
void* memaligned = sceLibcMspaceMemalign_nid_postfix(arena, 256, 100);
Require(memaligned && (reinterpret_cast<std::uintptr_t>(memaligned) & 255) == 0);
Require(sceLibcMspaceMemalign_nid_postfix(arena, 24, 100) == nullptr);
Require(sceLibcMspaceMallocStatsFast_nid_postfix(arena, &stats) == 0 && stats.currentInuseSize >= 100);
sceLibcMspaceFree_nid_postfix(arena, memaligned);
void* region = sceLibcMspaceMalloc_nid_postfix(arena, 8192);
Require(region != nullptr);
Require(sceLibcMspaceCreate_nid_postfix("outside", static_cast<unsigned char*>(region) + 8192, 4096, 0) == nullptr);
void* nested = sceLibcMspaceCreate_nid_postfix("nested", region, 8192, 1);
Require(nested == region);
void* inner = sceLibcMspaceMalloc_nid_postfix(nested, 64);
Require(inner > region && inner < static_cast<unsigned char*>(region) + 8192);
Require(sceLibcMspaceCreate_nid_postfix("flags", storage.data(), storage.size(), 2) == nullptr);
Require(sceLibcMspaceDestroy_nid_postfix(nested) == 0);
sceLibcMspaceFree_nid_postfix(arena, region);
Require(sceLibcMspaceDestroy_nid_postfix(arena) == 0);
Require(sceLibcMspaceMalloc_nid_postfix(arena, 8) == nullptr);
Require(sceLibcMspaceCreate_nid_postfix("reuse", storage.data(), storage.size(), 0) == arena);