From ecf77ecef0c190b6adf4619447cda165aa5ebad5 Mon Sep 17 00:00:00 2001 From: Elad <18193363+elad335@users.noreply.github.com> Date: Sun, 31 May 2026 18:53:02 +0300 Subject: [PATCH] Add type-safe idm::last_id() overload --- rpcs3/Emu/Cell/PPUThread.cpp | 4 ++-- rpcs3/Emu/Cell/SPUThread.cpp | 4 ++-- rpcs3/Emu/Cell/lv2/sys_cond.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_config.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_event.cpp | 6 +++--- rpcs3/Emu/Cell/lv2/sys_event_flag.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_interrupt.cpp | 8 ++++---- rpcs3/Emu/Cell/lv2/sys_mmapper.cpp | 8 ++++---- rpcs3/Emu/Cell/lv2/sys_mutex.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_net.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_overlay.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_prx.cpp | 4 ++-- rpcs3/Emu/Cell/lv2/sys_rwlock.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_semaphore.cpp | 2 +- rpcs3/Emu/Cell/lv2/sys_spu.cpp | 6 +++--- rpcs3/Emu/IdManager.h | 14 ++++++++++++++ 16 files changed, 42 insertions(+), 28 deletions(-) diff --git a/rpcs3/Emu/Cell/PPUThread.cpp b/rpcs3/Emu/Cell/PPUThread.cpp index 3813b28952..11bfd40eca 100644 --- a/rpcs3/Emu/Cell/PPUThread.cpp +++ b/rpcs3/Emu/Cell/PPUThread.cpp @@ -2425,7 +2425,7 @@ ppu_thread::~ppu_thread() } ppu_thread::ppu_thread(const ppu_thread_params& param, std::string_view name, u32 _prio, int detached) - : cpu_thread(idm::last_id()) + : cpu_thread(idm::last_id()) , stack_size(param.stack_size) , stack_addr(param.stack_addr) , joiner(detached != 0 ? ppu_join_status::detached : ppu_join_status::joinable) @@ -2525,7 +2525,7 @@ struct save_lv2_tag }; ppu_thread::ppu_thread(utils::serial& ar) - : cpu_thread(idm::last_id()) // last_id() is showed to constructor on serialization + : cpu_thread(idm::last_id()) // last_id<>() is shown to constructor on serialization , stack_size(ar) , stack_addr(ar) , joiner(ar.pop()) diff --git a/rpcs3/Emu/Cell/SPUThread.cpp b/rpcs3/Emu/Cell/SPUThread.cpp index 431a385b86..9ef5da81eb 100644 --- a/rpcs3/Emu/Cell/SPUThread.cpp +++ b/rpcs3/Emu/Cell/SPUThread.cpp @@ -1757,7 +1757,7 @@ void spu_thread::init_spu_decoder() } spu_thread::spu_thread(lv2_spu_group* group, u32 index, std::string_view name, u32 lv2_id, bool is_isolated, u32 option) - : cpu_thread(idm::last_id()) + : cpu_thread(idm::last_id()) , group(group) , index(index) , thread_type(group ? spu_type::threaded : is_isolated ? spu_type::isolated : spu_type::raw) @@ -1819,7 +1819,7 @@ void spu_thread::serialize_common(utils::serial& ar) } spu_thread::spu_thread(utils::serial& ar, lv2_spu_group* group) - : cpu_thread(idm::last_id()) + : cpu_thread(idm::last_id()) , group(group) , index(ar) , thread_type(group ? spu_type::threaded : ar.pop() ? spu_type::isolated : spu_type::raw) diff --git a/rpcs3/Emu/Cell/lv2/sys_cond.cpp b/rpcs3/Emu/Cell/lv2/sys_cond.cpp index f66146ae01..fcfd6824d1 100644 --- a/rpcs3/Emu/Cell/lv2/sys_cond.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_cond.cpp @@ -114,7 +114,7 @@ error_code sys_cond_create(ppu_thread& ppu, vm::ptr cond_id, u32 mutex_id, } ppu.check_state(); - *cond_id = idm::last_id(); + *cond_id = idm::last_id(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_config.cpp b/rpcs3/Emu/Cell/lv2/sys_config.cpp index 54cb2d6ce8..d6810de684 100644 --- a/rpcs3/Emu/Cell/lv2/sys_config.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_config.cpp @@ -319,7 +319,7 @@ error_code sys_config_open(u32 equeue_hdl, vm::ptr out_config_hdl) const auto config = lv2_config_handle::create(std::move(queue)); if (config) { - *out_config_hdl = idm::last_id(); + *out_config_hdl = idm::last_id(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_event.cpp b/rpcs3/Emu/Cell/lv2/sys_event.cpp index c5fa15cf55..b405788ca8 100644 --- a/rpcs3/Emu/Cell/lv2/sys_event.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_event.cpp @@ -15,7 +15,7 @@ LOG_CHANNEL(sys_event); lv2_event_queue::lv2_event_queue(u32 protocol, s32 type, s32 size, u64 name, u64 ipc_key) noexcept - : id(idm::last_id()) + : id(idm::last_id()) , protocol{static_cast(protocol)} , type(static_cast(type)) , size(static_cast(size)) @@ -25,7 +25,7 @@ lv2_event_queue::lv2_event_queue(u32 protocol, s32 type, s32 size, u64 name, u64 } lv2_event_queue::lv2_event_queue(utils::serial& ar) noexcept - : id(idm::last_id()) + : id(idm::last_id()) , protocol(ar) , type(ar) , size(ar) @@ -260,7 +260,7 @@ error_code sys_event_queue_create(cpu_thread& cpu, vm::ptr equeue_id, vm::p } cpu.check_state(); - *equeue_id = idm::last_id(); + *equeue_id = idm::last_id(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp b/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp index 89a6c42ac5..19df83c75c 100644 --- a/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_event_flag.cpp @@ -90,7 +90,7 @@ error_code sys_event_flag_create(ppu_thread& ppu, vm::ptr id, vm::ptr(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_interrupt.cpp b/rpcs3/Emu/Cell/lv2/sys_interrupt.cpp index 4ac829d4bb..72d13879cb 100644 --- a/rpcs3/Emu/Cell/lv2/sys_interrupt.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_interrupt.cpp @@ -13,13 +13,13 @@ LOG_CHANNEL(sys_interrupt); lv2_int_tag::lv2_int_tag() noexcept : lv2_obj(1) - , id(idm::last_id()) + , id(idm::last_id()) { } lv2_int_tag::lv2_int_tag(utils::serial& ar) noexcept : lv2_obj(1) - , id(idm::last_id()) + , id(idm::last_id()) , handler([&]() { const u32 id{ar}; @@ -46,7 +46,7 @@ void lv2_int_tag::save(utils::serial& ar) lv2_int_serv::lv2_int_serv(shared_ptr> thread, u64 arg1, u64 arg2) noexcept : lv2_obj(1) - , id(idm::last_id()) + , id(idm::last_id()) , thread(thread) , arg1(arg1) , arg2(arg2) @@ -55,7 +55,7 @@ lv2_int_serv::lv2_int_serv(shared_ptr> thread, u64 arg1 lv2_int_serv::lv2_int_serv(utils::serial& ar) noexcept : lv2_obj(1) - , id(idm::last_id()) + , id(idm::last_id()) , thread(idm::get_unlocked>(ar.pop())) , arg1(ar) , arg2(ar) diff --git a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp index b9d414a0dc..6c7ca748da 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_mmapper.cpp @@ -235,7 +235,7 @@ error_code sys_mmapper_allocate_shared_memory(ppu_thread& ppu, u64 ipc_key, u64 } ppu.check_state(); - *mem_id = idm::last_id(); + *mem_id = idm::last_id(); return CELL_OK; } @@ -291,7 +291,7 @@ error_code sys_mmapper_allocate_shared_memory_from_container(ppu_thread& ppu, u6 } ppu.check_state(); - *mem_id = idm::last_id(); + *mem_id = idm::last_id(); return CELL_OK; } @@ -390,7 +390,7 @@ error_code sys_mmapper_allocate_shared_memory_ext(ppu_thread& ppu, u64 ipc_key, } ppu.check_state(); - *mem_id = idm::last_id(); + *mem_id = idm::last_id(); return CELL_OK; } @@ -493,7 +493,7 @@ error_code sys_mmapper_allocate_shared_memory_from_container_ext(ppu_thread& ppu } ppu.check_state(); - *mem_id = idm::last_id(); + *mem_id = idm::last_id(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_mutex.cpp b/rpcs3/Emu/Cell/lv2/sys_mutex.cpp index 9f436ae4d3..2f657616e5 100644 --- a/rpcs3/Emu/Cell/lv2/sys_mutex.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_mutex.cpp @@ -99,7 +99,7 @@ error_code sys_mutex_create(ppu_thread& ppu, vm::ptr mutex_id, vm::ptr(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_net.cpp b/rpcs3/Emu/Cell/lv2/sys_net.cpp index a860d43b12..31a3c6ea07 100644 --- a/rpcs3/Emu/Cell/lv2/sys_net.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_net.cpp @@ -257,7 +257,7 @@ lv2_socket::lv2_socket(utils::serial& ar, lv2_socket_type _type) ar(so_rcvtimeo, so_sendtimeo); - lv2_id = idm::last_id(); + lv2_id = idm::last_id(); ar(last_bound_addr); } diff --git a/rpcs3/Emu/Cell/lv2/sys_overlay.cpp b/rpcs3/Emu/Cell/lv2/sys_overlay.cpp index 33475a8553..2fe1a83b3e 100644 --- a/rpcs3/Emu/Cell/lv2/sys_overlay.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_overlay.cpp @@ -70,7 +70,7 @@ static error_code overlay_load_module(vm::ptr ovlmid, const std::string& vp sys_overlay.success("Loaded overlay: \"%s\" (id=0x%x)", vpath, idm::last_id()); - *ovlmid = idm::last_id(); + *ovlmid = idm::last_id(); *entry = ovlm->entry; return CELL_OK; diff --git a/rpcs3/Emu/Cell/lv2/sys_prx.cpp b/rpcs3/Emu/Cell/lv2/sys_prx.cpp index d24bb54004..4775224eb1 100644 --- a/rpcs3/Emu/Cell/lv2/sys_prx.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_prx.cpp @@ -237,7 +237,7 @@ static error_code prx_load_module(const std::string& vpath, u64 flags, vm::ptr()); }; if (ignore) @@ -300,7 +300,7 @@ static error_code prx_load_module(const std::string& vpath, u64 flags, vm::ptr()); } fs::file make_file_view(fs::file&& file, u64 offset, u64 size); diff --git a/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp b/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp index e60d4895cc..6e30e15e08 100644 --- a/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_rwlock.cpp @@ -61,7 +61,7 @@ error_code sys_rwlock_create(ppu_thread& ppu, vm::ptr rw_lock_id, vm::ptr(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp b/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp index b6ca578977..dea96fcf5d 100644 --- a/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_semaphore.cpp @@ -74,7 +74,7 @@ error_code sys_semaphore_create(ppu_thread& ppu, vm::ptr sem_id, vm::ptr(); return CELL_OK; } diff --git a/rpcs3/Emu/Cell/lv2/sys_spu.cpp b/rpcs3/Emu/Cell/lv2/sys_spu.cpp index 13733965a3..ee0e070508 100644 --- a/rpcs3/Emu/Cell/lv2/sys_spu.cpp +++ b/rpcs3/Emu/Cell/lv2/sys_spu.cpp @@ -833,7 +833,7 @@ error_code sys_spu_thread_initialize(ppu_thread& ppu, vm::ptr thread, u32 g if (auto state = +group->run_state; state != SPU_THREAD_GROUP_STATUS_NOT_INITIALIZED) { lock.unlock(); - idm::remove>(idm::last_id()); + ensure(idm::remove>(idm::last_id())); if (state == SPU_THREAD_GROUP_STATUS_DESTROYED) { @@ -846,7 +846,7 @@ error_code sys_spu_thread_initialize(ppu_thread& ppu, vm::ptr thread, u32 g if (group->threads_map[spu_num] != -1) { lock.unlock(); - idm::remove>(idm::last_id()); + ensure(idm::remove>(idm::last_id())); return CELL_EBUSY; } @@ -1137,7 +1137,7 @@ error_code sys_spu_thread_group_create(ppu_thread& ppu, vm::ptr id, u32 num sys_spu.warning("sys_spu_thread_group_create(): Thread group \"%s\" created (id=0x%x)", group->name, idm::last_id()); ppu.check_state(); - *id = idm::last_id(); + *id = idm::last_id(); return CELL_OK; } diff --git a/rpcs3/Emu/IdManager.h b/rpcs3/Emu/IdManager.h index b3c898c23b..e8dde8fb8d 100644 --- a/rpcs3/Emu/IdManager.h +++ b/rpcs3/Emu/IdManager.h @@ -591,6 +591,20 @@ public: return id_manager::g_id; } + // Get last ID with type validation + template + static inline u32 last_id(std::source_location src = std::source_location::current()) + { + const u32 last = id_manager::g_id; + + if (get_index(last) >= T::id_count) + { + fmt::raw_range_error(src, last, T::id_base); + } + + return last; + } + // Add a new ID of specified type with specified constructor arguments (returns object or null_ptr) template requires (std::is_constructible_v) static inline stx::shared_ptr make_ptr(Args&&... args)