/* * Copyright (c) 2018-2020 Atmosphère-NX * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include #include #include namespace ams::tipc { namespace impl { /* Buffer utilities. */ struct BufferBaseTag{}; } namespace impl { class BufferBase : public BufferBaseTag { public: static constexpr u32 AdditionalAttributes = 0; private: const tipc::PointerAndSize pas; protected: constexpr ALWAYS_INLINE uintptr_t GetAddressImpl() const { return this->pas.GetAddress(); } template constexpr ALWAYS_INLINE size_t GetSizeImpl() const { return this->pas.GetSize() / sizeof(Entry); } public: constexpr ALWAYS_INLINE BufferBase() : pas() { /* ... */ } constexpr ALWAYS_INLINE BufferBase(const tipc::PointerAndSize &_pas) : pas(_pas) { /* ... */ } constexpr ALWAYS_INLINE BufferBase(uintptr_t ptr, size_t sz) : pas(ptr, sz) { /* ... */ } }; class InBufferBase : public BufferBase { public: using BaseType = BufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes | SfBufferAttr_In; public: constexpr ALWAYS_INLINE InBufferBase() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE InBufferBase(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE InBufferBase(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE InBufferBase(const void *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE InBufferBase(const u8 *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } }; class OutBufferBase : public BufferBase { public: using BaseType = BufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes | SfBufferAttr_Out; public: constexpr ALWAYS_INLINE OutBufferBase() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE OutBufferBase(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE OutBufferBase(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE OutBufferBase(void *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE OutBufferBase(u8 *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } }; template class InBufferImpl : public InBufferBase { public: using BaseType = InBufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes | ExtraAttributes; public: constexpr ALWAYS_INLINE InBufferImpl() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE InBufferImpl(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE InBufferImpl(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE InBufferImpl(const void *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE InBufferImpl(const u8 *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE const u8 *GetPointer() const { return reinterpret_cast(this->GetAddressImpl()); } constexpr ALWAYS_INLINE size_t GetSize() const { return this->GetSizeImpl(); } }; template class OutBufferImpl : public OutBufferBase { public: using BaseType = OutBufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes | ExtraAttributes; public: constexpr ALWAYS_INLINE OutBufferImpl() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE OutBufferImpl(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE OutBufferImpl(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE OutBufferImpl(void *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE OutBufferImpl(u8 *ptr, size_t sz) : BaseType(reinterpret_cast(ptr), sz) { /* ... */ } constexpr ALWAYS_INLINE u8 *GetPointer() const { return reinterpret_cast(this->GetAddressImpl()); } constexpr ALWAYS_INLINE size_t GetSize() const { return this->GetSizeImpl(); } }; template struct InArrayImpl : public InBufferBase { public: using BaseType = InBufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes; public: constexpr ALWAYS_INLINE InArrayImpl() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE InArrayImpl(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE InArrayImpl(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE InArrayImpl(const T *ptr, size_t num_elements) : BaseType(reinterpret_cast(ptr), num_elements * sizeof(T)) { /* ... */ } constexpr ALWAYS_INLINE const T *GetPointer() const { return reinterpret_cast(this->GetAddressImpl()); } constexpr ALWAYS_INLINE size_t GetSize() const { return this->GetSizeImpl(); } constexpr ALWAYS_INLINE const T &operator[](size_t i) const { return this->GetPointer()[i]; } constexpr explicit ALWAYS_INLINE operator Span() const { return {this->GetPointer(), this->GetSize()}; } constexpr ALWAYS_INLINE Span ToSpan() const { return {this->GetPointer(), this->GetSize()}; } }; template struct OutArrayImpl : public OutBufferBase { public: using BaseType = OutBufferBase; static constexpr u32 AdditionalAttributes = BaseType::AdditionalAttributes; public: constexpr ALWAYS_INLINE OutArrayImpl() : BaseType() { /* ... */ } constexpr ALWAYS_INLINE OutArrayImpl(const tipc::PointerAndSize &_pas) : BaseType(_pas) { /* ... */ } constexpr ALWAYS_INLINE OutArrayImpl(uintptr_t ptr, size_t sz) : BaseType(ptr, sz) { /* ... */ } constexpr ALWAYS_INLINE OutArrayImpl(T *ptr, size_t num_elements) : BaseType(reinterpret_cast(ptr), num_elements * sizeof(T)) { /* ... */ } constexpr ALWAYS_INLINE T *GetPointer() const { return reinterpret_cast(this->GetAddressImpl()); } constexpr ALWAYS_INLINE size_t GetSize() const { return this->GetSizeImpl(); } constexpr ALWAYS_INLINE T &operator[](size_t i) const { return this->GetPointer()[i]; } constexpr explicit ALWAYS_INLINE operator Span() const { return {this->GetPointer(), this->GetSize()}; } constexpr ALWAYS_INLINE Span ToSpan() const { return {this->GetPointer(), this->GetSize()}; } }; } /* Buffer Types. */ using InBuffer = typename impl::InBufferImpl<>; // using InNonSecureBuffer = typename impl::InBufferImpl; // using InNonDeviceBuffer = typename impl::InBufferImpl; using OutBuffer = typename impl::OutBufferImpl<>; //using OutNonSecureBuffer = typename impl::OutBufferImpl; //using OutNonDeviceBuffer = typename impl::OutBufferImpl; template using InArray = typename impl::InArrayImpl; template using OutArray = typename impl::OutArrayImpl; /* Attribute serialization structs. */ template concept IsBuffer = std::derived_from; template requires IsBuffer constexpr inline u32 BufferAttributes = SfBufferAttr_HipcMapAlias | T::AdditionalAttributes; }