Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions core/libs/prx/libSceAgcDriver/Execution/src/HostAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ struct Cache {
std::uint16_t counts[Classes];
};

constexpr std::uint16_t KeptLists = 8;

struct Depot {
SRWLOCK lock = SRWLOCK_INIT;
void* lists = nullptr;
std::uint16_t count = 0;
};

Depot depots[Classes];

bool KeepList(std::size_t sizeClass, void* list) noexcept {
auto& depot = depots[sizeClass];
AcquireSRWLockExclusive(&depot.lock);
const bool kept = depot.count < KeptLists;
if (kept) {
static_cast<void**>(list)[1] = depot.lists;
depot.lists = list;
++depot.count;
}
ReleaseSRWLockExclusive(&depot.lock);
return kept;
}

void* TakeList(std::size_t sizeClass) noexcept {
auto& depot = depots[sizeClass];
AcquireSRWLockExclusive(&depot.lock);
void* list = depot.lists;
if (list != nullptr) {
depot.lists = static_cast<void**>(list)[1];
--depot.count;
}
ReleaseSRWLockExclusive(&depot.lock);
return list;
}

Cache* const Closed = reinterpret_cast<Cache*>(std::uintptr_t{1});

constexpr unsigned long TlsSlotsOffset = 0x1480;
Expand Down Expand Up @@ -114,6 +149,12 @@ void* TryAllocate(std::size_t bytes) noexcept {
if (bytes <= LargestCached) {
const auto sizeClass = (bytes - 1) / Granule;
if (auto* cache = CurrentCache()) {
if (cache->heads[sizeClass] == nullptr) {
if (void* list = TakeList(sizeClass)) {
cache->heads[sizeClass] = list;
cache->counts[sizeClass] = Depth(sizeClass);
}
}
if (void* block = cache->heads[sizeClass]) {
cache->heads[sizeClass] = *static_cast<void**>(block);
--cache->counts[sizeClass];
Expand All @@ -140,6 +181,10 @@ void Release(void* block, std::size_t bytes = 0) noexcept {
const auto usable = bytes != 0 && bytes % Granule == 0 ? bytes : _msize(block);
if (usable >= Granule && usable < LargestCached + Granule) {
const auto sizeClass = usable / Granule - 1;
if (cache->counts[sizeClass] == Depth(sizeClass) && KeepList(sizeClass, cache->heads[sizeClass])) {
cache->heads[sizeClass] = nullptr;
cache->counts[sizeClass] = 0;
}
if (cache->counts[sizeClass] < Depth(sizeClass)) {
*static_cast<void**>(block) = cache->heads[sizeClass];
cache->heads[sizeClass] = block;
Expand Down
46 changes: 46 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/HostAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdlib>
#include <cstring>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <thread>
Expand Down Expand Up @@ -112,6 +113,50 @@ std::size_t PrivateBytes() {
#endif
}

void BlocksFlowToAnotherThread() {
constexpr int rounds = 12;
constexpr int perRound = 200000;
std::size_t afterEarlyRounds = 0;
for (int round = 0; round < rounds; ++round) {
std::mutex lock;
std::vector<unsigned char*> handed;
std::atomic<bool> done{false};
std::thread freer([&] {
std::vector<unsigned char*> taken;
for (;;) {
const bool finished = done.load(std::memory_order_acquire);
{
std::lock_guard guard(lock);
taken.swap(handed);
}
for (auto* block : taken) {
const auto size = static_cast<std::size_t>(block[0]) * 4 + 1;
Check(block[size - 1] == block[0], "a block freed on another thread keeps its bytes until then");
::operator delete(block);
}
taken.clear();
if (finished) break;
std::this_thread::yield();
}
});
for (int i = 0; i < perRound; ++i) {
const auto seed = static_cast<unsigned char>(i % 256);
const auto size = static_cast<std::size_t>(seed) * 4 + 1;
auto* block = static_cast<unsigned char*>(::operator new(size));
block[0] = seed;
block[size - 1] = seed;
std::lock_guard guard(lock);
handed.push_back(block);
}
done.store(true, std::memory_order_release);
freer.join();
if (round == 3) afterEarlyRounds = PrivateBytes();
}
const auto afterLastRound = PrivateBytes();
std::printf("private bytes after 4 and after %d rounds of blocks freed on another thread: %zu, %zu\n", rounds, afterEarlyRounds, afterLastRound);
Check(afterLastRound < afterEarlyRounds + (64u << 20), "blocks freed on another thread do not pile up");
}

void ThreadsEndWithCachedBlocks() {
constexpr int waves = 24;
std::size_t afterEarlyWaves = 0;
Expand Down Expand Up @@ -144,6 +189,7 @@ int main() {
SizedDeletesKeepTheirClass();
BlocksCrossIntoLibstdcxx();
BlocksMoveBetweenThreads();
BlocksFlowToAnotherThread();
ThreadsEndWithCachedBlocks();
if (failures != 0) {
std::fprintf(stderr, "%d host allocator checks failed\n", failures);
Expand Down
Loading