mirror of
https://github.com/RPCSX/rpcsx.git
synced 2025-12-06 07:12:14 +01:00
Replaces `std::shared_pointer` with `stx::atomic_ptr` and `stx::shared_ptr`. Notes to programmers: * This pr kills the use of `dynamic_cast`, `std::dynamic_pointer_cast` and `std::weak_ptr` on IDM objects, possible replacement is to save the object ID on the base object, then use idm::check/get_unlocked to the destination type via the saved ID which may be null. Null pointer check is how you can tell type mismatch (as dynamic cast) or object destruction (as weak_ptr locking). * Double-inheritance on IDM objects should be used with care, `stx::shared_ptr` does not support constant-evaluated pointer offsetting to parent/child type. * `idm::check/get_unlocked` can now be used anywhere. Misc fixes: * Fixes some segfaults with RPCN with interaction with IDM. * Fix deadlocks in access violation handler due locking recursion. * Fixes race condition in process exit-spawn on memory containers read. * Fix bug that theoretically can prevent RPCS3 from booting - fix `id_manager::typeinfo` comparison to compare members instead of `memcmp` which can fail spuriously on padding bytes. * Ensure all IDM inherited types of base, either has `id_base` or `id_type` defined locally, this allows to make getters such as `idm::get_unlocked<lv2_socket, lv2_socket_raw>()` which were broken before. (requires save-states invalidation) * Removes broken operator[] overload of `stx::shared_ptr` and `stx::single_ptr` for non-array types.
88 lines
2 KiB
C++
88 lines
2 KiB
C++
#include "stdafx.h"
|
|
#include "IdManager.h"
|
|
#include "Utilities/Thread.h"
|
|
|
|
shared_mutex id_manager::g_mutex;
|
|
|
|
namespace id_manager
|
|
{
|
|
thread_local u32 g_id = 0;
|
|
}
|
|
|
|
template <>
|
|
bool serialize<std::shared_ptr<utils::serial>>(utils::serial& ar, std::shared_ptr<utils::serial>& o)
|
|
{
|
|
if (!o)
|
|
{
|
|
o = std::make_shared<utils::serial>();
|
|
}
|
|
|
|
if (!ar.is_writing())
|
|
{
|
|
o->set_reading_state();
|
|
}
|
|
|
|
ar(o->data);
|
|
return true;
|
|
}
|
|
|
|
std::vector<std::pair<u128, id_manager::typeinfo>>& id_manager::get_typeinfo_map()
|
|
{
|
|
// Magic static
|
|
static std::vector<std::pair<u128, id_manager::typeinfo>> s_map;
|
|
return s_map;
|
|
}
|
|
|
|
id_manager::id_key* idm::allocate_id(std::span<id_manager::id_key> keys, usz& highest_index, u32 type_id, u32 dst_id, u32 base, u32 step, u32 count, bool uses_lowest_id, std::pair<u32, u32> invl_range)
|
|
{
|
|
if (dst_id != (base ? 0 : u32{umax}))
|
|
{
|
|
// Fixed position construction
|
|
const u32 index = id_manager::get_index(dst_id, base, step, count, invl_range);
|
|
ensure(index < count);
|
|
|
|
highest_index = std::max<usz>(highest_index, index + 1);
|
|
|
|
if (keys[index].type() != umax)
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
id_manager::g_id = dst_id;
|
|
keys[index] = id_manager::id_key(dst_id, type_id);
|
|
return &keys[index];
|
|
}
|
|
|
|
if (uses_lowest_id)
|
|
{
|
|
// Disable the optimization below (hurts accuracy for known cases)
|
|
highest_index = count;
|
|
}
|
|
else if (highest_index < count)
|
|
{
|
|
// Try to emplace back
|
|
const u32 _next = base + step * highest_index;
|
|
id_manager::g_id = _next;
|
|
return &(keys[highest_index++] = (id_manager::id_key(_next, type_id)));
|
|
}
|
|
|
|
// Check all IDs starting from "next id" (TODO)
|
|
for (u32 i = 0, next = base; i < count; i++, next += step)
|
|
{
|
|
const auto ptr = &keys[i];
|
|
|
|
// Look for free ID
|
|
if (ptr->type() == umax)
|
|
{
|
|
// Incremenet ID invalidation counter
|
|
const u32 id = next | ((ptr->value() + (1u << invl_range.first)) & (invl_range.second ? (((1u << invl_range.second) - 1) << invl_range.first) : 0));
|
|
id_manager::g_id = id;
|
|
*ptr = id_manager::id_key(id, type_id);
|
|
return ptr;
|
|
}
|
|
}
|
|
|
|
// Out of IDs
|
|
return nullptr;
|
|
}
|