diff options
| -rw-r--r-- | src/core/hle/ipc_helpers.h | 113 | 
1 files changed, 103 insertions, 10 deletions
diff --git a/src/core/hle/ipc_helpers.h b/src/core/hle/ipc_helpers.h index a3abc102e..5f370bc3b 100644 --- a/src/core/hle/ipc_helpers.h +++ b/src/core/hle/ipc_helpers.h @@ -4,6 +4,10 @@  #pragma once +#include <array> +#include <tuple> +#include <type_traits> +#include <utility>  #include "core/hle/ipc.h"  #include "core/hle/kernel/handle_table.h"  #include "core/hle/kernel/hle_ipc.h" @@ -105,6 +109,9 @@ public:      template <typename... H>      void PushMoveHandles(H... handles); +    template <typename... O> +    void PushObjects(Kernel::SharedPtr<O>... pointers); +      void PushCurrentPIDHandle();      void PushStaticBuffer(VAddr buffer_vaddr, u32 size, u8 buffer_id); @@ -170,6 +177,11 @@ inline void RequestBuilder::PushMoveHandles(H... handles) {      Push(static_cast<Kernel::Handle>(handles)...);  } +template <typename... O> +inline void RequestBuilder::PushObjects(Kernel::SharedPtr<O>... pointers) { +    PushMoveHandles(context->AddOutgoingHandle(std::move(pointers))...); +} +  inline void RequestBuilder::PushCurrentPIDHandle() {      Push(CallingPidDesc());      Push(u32(0)); @@ -229,10 +241,52 @@ public:      template <typename First, typename... Other>      void Pop(First& first_value, Other&... other_values); +    /// Equivalent to calling `PopHandles<1>()[0]`.      Kernel::Handle PopHandle(); +    /** +     * Pops a descriptor containing `N` handles. The handles are returned as an array. The +     * descriptor must contain exactly `N` handles, it is not permitted to, for example, call +     * PopHandles<1>() twice to read a multi-handle descriptor with 2 handles, or to make a single +     * PopHandles<2>() call to read 2 single-handle descriptors. +     */ +    template <unsigned int N> +    std::array<Kernel::Handle, N> PopHandles(); + +    /// Convenience wrapper around PopHandles() which assigns the handles to the passed references.      template <typename... H> -    void PopHandles(H&... handles); +    void PopHandles(H&... handles) { +        std::tie(handles...) = PopHandles<sizeof...(H)>(); +    } + +    /// Equivalent to calling `PopGenericObjects<1>()[0]`. +    Kernel::SharedPtr<Kernel::Object> PopGenericObject(); + +    /// Equivalent to calling `std::get<0>(PopObjects<T>())`. +    template <typename T> +    Kernel::SharedPtr<T> PopObject(); + +    /** +     * Pop a descriptor containing `N` handles and resolves them to Kernel::Object pointers. If a +     * handle is invalid, null is returned for that object instead. The same caveats from +     * PopHandles() apply regarding `N` matching the number of handles in the descriptor. +     */ +    template <unsigned int N> +    std::array<Kernel::SharedPtr<Kernel::Object>, N> PopGenericObjects(); + +    /** +     * Resolves handles to Kernel::Objects as in PopGenericsObjects(), but then also casts them to +     * the passed `T` types, while verifying that the cast is valid. If the type of an object does +     * not match, null is returned instead. +     */ +    template <typename... T> +    std::tuple<Kernel::SharedPtr<T>...> PopObjects(); + +    /// Convenience wrapper around PopObjects() which assigns the handles to the passed references. +    template <typename... T> +    void PopObjects(Kernel::SharedPtr<T>&... pointers) { +        std::tie(pointers...) = PopObjects<T...>(); +    }      /**       * @brief Pops the static buffer vaddr @@ -344,15 +398,54 @@ inline Kernel::Handle RequestParser::PopHandle() {      return Pop<Kernel::Handle>();  } -template <typename... H> -void RequestParser::PopHandles(H&... handles) { -    const u32 handle_descriptor = Pop<u32>(); -    const int handles_number = sizeof...(H); -    DEBUG_ASSERT_MSG(IsHandleDescriptor(handle_descriptor), -                     "Tried to pop handle(s) but the descriptor is not a handle descriptor"); -    DEBUG_ASSERT_MSG(handles_number == HandleNumberFromDesc(handle_descriptor), -                     "Number of handles doesn't match the descriptor"); -    Pop(static_cast<Kernel::Handle&>(handles)...); +template <unsigned int N> +std::array<Kernel::Handle, N> RequestParser::PopHandles() { +    u32 handle_descriptor = Pop<u32>(); +    ASSERT_MSG(IsHandleDescriptor(handle_descriptor), +               "Tried to pop handle(s) but the descriptor is not a handle descriptor"); +    ASSERT_MSG(N == HandleNumberFromDesc(handle_descriptor), +               "Number of handles doesn't match the descriptor"); + +    std::array<Kernel::Handle, N> handles{}; +    for (Kernel::Handle& handle : handles) { +        handle = Pop<Kernel::Handle>(); +    } +    return handles; +} + +inline Kernel::SharedPtr<Kernel::Object> RequestParser::PopGenericObject() { +    Kernel::Handle handle = PopHandle(); +    return context->GetIncomingHandle(handle); +} + +template <typename T> +Kernel::SharedPtr<T> RequestParser::PopObject() { +    return Kernel::DynamicObjectCast<T>(PopGenericObject()); +} + +template <unsigned int N> +inline std::array<Kernel::SharedPtr<Kernel::Object>, N> RequestParser::PopGenericObjects() { +    std::array<Kernel::Handle, N> handles = PopHandles<N>(); +    std::array<Kernel::SharedPtr<Kernel::Object>, N> pointers; +    for (int i = 0; i < N; ++i) { +        pointers[i] = context->GetIncomingHandle(handles[i]); +    } +    return pointers; +} + +namespace detail { +template <typename... T, size_t... I> +std::tuple<Kernel::SharedPtr<T>...> PopObjectsHelper( +    std::array<Kernel::SharedPtr<Kernel::Object>, sizeof...(T)>&& pointers, +    std::index_sequence<I...>) { +    return std::make_tuple(Kernel::DynamicObjectCast<T>(std::move(pointers[I]))...); +} +} // namespace detail + +template <typename... T> +inline std::tuple<Kernel::SharedPtr<T>...> RequestParser::PopObjects() { +    return detail::PopObjectsHelper<T...>(PopGenericObjects<sizeof...(T)>(), +                                          std::index_sequence_for<T...>{});  }  inline VAddr RequestParser::PopStaticBuffer(size_t* data_size, bool useStaticBuffersToGetVaddr) {  | 
