mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(libc): implement sceLibcMspaceReallocalign
This commit is contained in:
@@ -212,6 +212,31 @@ void* APS5_VABI sceLibcMspaceMemalign_nid_postfix(void* handle, std::size_t alig
|
||||
return Allocate(Find(handle), size, std::max<std::size_t>(alignment, 16));
|
||||
}
|
||||
|
||||
void* APS5_VABI sceLibcMspaceReallocalign_nid_postfix(void* handle, void* pointer, std::size_t size, std::size_t alignment) {
|
||||
if (alignment == 0 || (alignment & (alignment - 1))) {
|
||||
Error(22);
|
||||
return nullptr;
|
||||
}
|
||||
const auto effective = std::max<std::size_t>(alignment, 16);
|
||||
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;
|
||||
return pointer;
|
||||
}
|
||||
void* result = Allocate(arena, size, effective);
|
||||
if (result) {
|
||||
std::memcpy(result, pointer, std::min(block->used, size));
|
||||
Release(arena, block);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int APS5_VABI sceLibcMspaceMallocStats_nid_postfix(void* handle, MallocManagedSize* stats) {
|
||||
return FillStats(handle, stats);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ 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);
|
||||
void* APS5_VABI sceLibcMspaceReallocalign_nid_postfix(void*, void*, std::size_t, std::size_t);
|
||||
int APS5_VABI sceLibcMspaceMallocStats_nid_postfix(void*, void*);
|
||||
int APS5_VABI sceLibcMspaceMallocStatsFast_nid_postfix(void*, void*);
|
||||
}
|
||||
@@ -79,6 +80,15 @@ int main() {
|
||||
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);
|
||||
unsigned char* realigned = static_cast<unsigned char*>(sceLibcMspaceReallocalign_nid_postfix(arena, nullptr, 64, 64));
|
||||
Require(realigned && (reinterpret_cast<std::uintptr_t>(realigned) & 63) == 0);
|
||||
for (int i = 0; i < 64; ++i) realigned[i] = static_cast<unsigned char>(i + 1);
|
||||
unsigned char* regrown = static_cast<unsigned char*>(sceLibcMspaceReallocalign_nid_postfix(arena, realigned, 256, 64));
|
||||
Require(regrown && (reinterpret_cast<std::uintptr_t>(regrown) & 63) == 0);
|
||||
for (int i = 0; i < 64; ++i) Require(regrown[i] == static_cast<unsigned char>(i + 1));
|
||||
Require(sceLibcMspaceReallocalign_nid_postfix(arena, regrown, 16, 0) == nullptr);
|
||||
Require(sceLibcMspaceReallocalign_nid_postfix(arena, regrown, 16, 3) == nullptr);
|
||||
Require(sceLibcMspaceReallocalign_nid_postfix(arena, regrown, 0, 16) == nullptr);
|
||||
Require(sceLibcMspaceMallocStatsFast_nid_postfix(arena, &stats) == 0 && stats.currentInuseSize >= 100);
|
||||
sceLibcMspaceFree_nid_postfix(arena, memaligned);
|
||||
void* region = sceLibcMspaceMalloc_nid_postfix(arena, 8192);
|
||||
|
||||
Reference in New Issue
Block a user