os: remove ManagedHandle, refactor to use NativeHandle typename

This commit is contained in:
Michael Scire 2021-10-04 12:33:09 -07:00
parent a774833790
commit 6f76066d24
71 changed files with 473 additions and 397 deletions

View file

@ -15,10 +15,11 @@
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os.hpp>
namespace ams::dd {
using ProcessHandle = ::Handle;
using ProcessHandle = os::NativeHandle;
using MemoryPermission = os::MemoryPermission;
using enum os::MemoryPermission;

View file

@ -23,8 +23,8 @@
#include <stratosphere/os/os_memory_permission.hpp>
#include <stratosphere/os/os_memory_heap_api.hpp>
#include <stratosphere/os/os_memory_virtual_address_api.hpp>
#include <stratosphere/os/os_managed_handle.hpp>
#include <stratosphere/os/os_process_handle.hpp>
#include <stratosphere/os/os_native_handle.hpp>
#include <stratosphere/os/os_process_handle_api.hpp>
#include <stratosphere/os/os_random.hpp>
#include <stratosphere/os/os_mutex.hpp>
#include <stratosphere/os/os_condition_variable.hpp>

View file

@ -44,16 +44,6 @@ namespace ams::os {
inline constexpr const ProcessId InvalidProcessId = ProcessId::Invalid;
NX_INLINE Result TryGetProcessId(os::ProcessId *out, ::Handle process_handle) {
return svcGetProcessId(&out->value, process_handle);
}
NX_INLINE os::ProcessId GetProcessId(::Handle process_handle) {
os::ProcessId process_id;
R_ABORT_UNLESS(TryGetProcessId(&process_id, process_handle));
return process_id;
}
inline constexpr bool operator==(const ProcessId &lhs, const ProcessId &rhs) {
return lhs.value == rhs.value;
}

View file

@ -18,6 +18,7 @@
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/impl/os_internal_condition_variable.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -39,7 +40,7 @@ namespace ams::os {
u8 clear_mode;
u8 state;
util::TypedStorage<impl::InterruptEventImpl, sizeof(svc::Handle) * 2, alignof(svc::Handle)> impl;
util::TypedStorage<impl::InterruptEventImpl, sizeof(NativeHandle) * 2, alignof(NativeHandle)> impl;
};
static_assert(std::is_trivial<InterruptEventType>::value);

View file

@ -31,11 +31,11 @@ namespace ams::os {
/* ... */
}
IoRegion(Handle io_pool_handle, uintptr_t address, size_t size, MemoryMapping mapping, MemoryPermission permission) {
IoRegion(NativeHandle io_pool_handle, uintptr_t address, size_t size, MemoryMapping mapping, MemoryPermission permission) {
R_ABORT_UNLESS(CreateIoRegion(std::addressof(m_io_region), io_pool_handle, address, size, mapping, permission));
}
IoRegion(size_t size, Handle handle, bool managed) {
IoRegion(size_t size, NativeHandle handle, bool managed) {
this->AttachHandle(size, handle, managed);
}
@ -51,11 +51,11 @@ namespace ams::os {
DestroyIoRegion(std::addressof(m_io_region));
}
void AttachHandle(size_t size, Handle handle, bool managed) {
void AttachHandle(size_t size, NativeHandle handle, bool managed) {
AttachIoRegionHandle(std::addressof(m_io_region), size, handle, managed);
}
Handle GetHandle() const {
NativeHandle GetHandle() const {
return GetIoRegionHandle(std::addressof(m_io_region));
}

View file

@ -17,18 +17,19 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_memory_permission.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
struct IoRegionType;
Result CreateIoRegion(IoRegionType *io_region, Handle io_pool_handle, uintptr_t address, size_t size, MemoryMapping mapping, MemoryPermission permission);
Result CreateIoRegion(IoRegionType *io_region, NativeHandle io_pool_handle, uintptr_t address, size_t size, MemoryMapping mapping, MemoryPermission permission);
void AttachIoRegionHandle(IoRegionType *io_region, size_t size, Handle handle, bool managed);
void AttachIoRegionHandle(IoRegionType *io_region, size_t size, NativeHandle handle, bool managed);
void DestroyIoRegion(IoRegionType *io_region);
Handle GetIoRegionHandle(const IoRegionType *io_region);
NativeHandle GetIoRegionHandle(const IoRegionType *io_region);
Result MapIoRegion(void **out, IoRegionType *io_region, MemoryPermission perm);
void UnmapIoRegion(IoRegionType *io_region);

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -27,7 +28,7 @@ namespace ams::os {
State_Mapped = 2,
};
Handle handle;
NativeHandle handle;
u8 state;
size_t size;
void *mapped_address;

View file

@ -1,87 +0,0 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/os/os_common_types.hpp>
namespace ams::os {
class ManagedHandle {
NON_COPYABLE(ManagedHandle);
private:
Handle hnd;
public:
constexpr ManagedHandle() : hnd(INVALID_HANDLE) { /* ... */ }
constexpr ManagedHandle(Handle h) : hnd(h) { /* ... */ }
~ManagedHandle() {
if (this->hnd != INVALID_HANDLE) {
R_ABORT_UNLESS(svcCloseHandle(this->hnd));
this->hnd = INVALID_HANDLE;
}
}
ManagedHandle(ManagedHandle&& rhs) {
this->hnd = rhs.hnd;
rhs.hnd = INVALID_HANDLE;
}
ManagedHandle &operator=(ManagedHandle&& rhs) {
rhs.Swap(*this);
return *this;
}
explicit operator bool() const {
return this->hnd != INVALID_HANDLE;
}
void Swap(ManagedHandle &rhs) {
std::swap(this->hnd, rhs.hnd);
}
Handle Get() const {
return this->hnd;
}
Handle *GetPointer() {
return &this->hnd;
}
Handle *GetPointerAndClear() {
this->Clear();
return this->GetPointer();
}
Handle Move() {
const Handle h = this->hnd;
this->hnd = INVALID_HANDLE;
return h;
}
void Detach() {
const Handle h = this->Move();
AMS_UNUSED(h);
}
void Reset(Handle h) {
ManagedHandle(h).Swap(*this);
}
void Clear() {
this->Reset(INVALID_HANDLE);
}
};
}

View file

@ -16,6 +16,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_message_queue_common.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -40,6 +41,6 @@ namespace ams::os {
void SetMultiWaitHolderUserData(MultiWaitHolderType *holder, uintptr_t user_data);
uintptr_t GetMultiWaitHolderUserData(const MultiWaitHolderType *holder);
void InitializeMultiWaitHolder(MultiWaitHolderType *holder, Handle handle);
void InitializeMultiWaitHolder(MultiWaitHolderType *holder, NativeHandle handle);
}

View file

@ -0,0 +1,18 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/os/os_native_handle_types.hpp>
#include <stratosphere/os/os_native_handle_api.hpp>

View file

@ -15,18 +15,13 @@
*/
#pragma once
#include <stratosphere/os/os_managed_handle.hpp>
#include <stratosphere/ncm/ncm_program_id.hpp>
#include <vapours.hpp>
#include <stratosphere/os/os_native_handle_types.hpp>
namespace ams::os {
::Handle GetCurrentProcessHandle();
void CloseNativeHandle(NativeHandle handle);
ALWAYS_INLINE ProcessId GetCurrentProcessId() {
return GetProcessId(GetCurrentProcessHandle());
}
/* TODO: Another header? */
ncm::ProgramId GetCurrentProgramId();
NativeHandle GetCurrentProcessHandle();
}

View file

@ -0,0 +1,31 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vapours.hpp>
namespace ams::os {
#if defined(ATMOSPHERE_OS_HORIZON)
using NativeHandle = svc::Handle;
static_assert(std::unsigned_integral<NativeHandle>);
constexpr inline NativeHandle InvalidNativeHandle = svc::InvalidHandle;
#else
#error "Unknown OS for os::NativeHandle"
#endif
}

View file

@ -0,0 +1,47 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere/os/os_native_handle.hpp>
#include <stratosphere/ncm/ncm_program_id.hpp>
namespace ams::os {
Result GetProcessId(os::ProcessId *out, NativeHandle handle);
ALWAYS_INLINE ProcessId GetProcessId(NativeHandle handle) {
ProcessId process_id;
R_ABORT_UNLESS(GetProcessId(std::addressof(process_id), handle));
return process_id;
}
ALWAYS_INLINE ProcessId GetCurrentProcessId() {
return GetProcessId(GetCurrentProcessHandle());
}
Result GetProgramId(ncm::ProgramId *out, NativeHandle handle);
ALWAYS_INLINE ncm::ProgramId GetProgramId(NativeHandle handle) {
ncm::ProgramId program_id;
R_ABORT_UNLESS(GetProgramId(std::addressof(program_id), handle));
return program_id;
}
ALWAYS_INLINE ncm::ProgramId GetCurrentProgramId() {
return GetProgramId(GetCurrentProcessHandle());
}
}

View file

@ -15,12 +15,13 @@
*/
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
struct MultiWaitHolderType;
struct MultiWaitType;
Result SdkReplyAndReceive(os::MultiWaitHolderType **out, Handle reply_target, MultiWaitType *multi_wait);
Result SdkReplyAndReceive(os::MultiWaitHolderType **out, NativeHandle reply_target, MultiWaitType *multi_wait);
}

View file

@ -35,7 +35,7 @@ namespace ams::os {
R_ABORT_UNLESS(CreateSharedMemory(std::addressof(m_shared_memory), size, my_perm, other_perm));
}
SharedMemory(size_t size, Handle handle, bool managed) {
SharedMemory(size_t size, NativeHandle handle, bool managed) {
this->Attach(size, handle, managed);
}
@ -46,7 +46,7 @@ namespace ams::os {
DestroySharedMemory(std::addressof(m_shared_memory));
}
void Attach(size_t size, Handle handle, bool managed) {
void Attach(size_t size, NativeHandle handle, bool managed) {
return AttachSharedMemory(std::addressof(m_shared_memory), size, handle, managed);
}
@ -66,7 +66,7 @@ namespace ams::os {
return GetSharedMemorySize(std::addressof(m_shared_memory));
}
Handle GetHandle() const {
NativeHandle GetHandle() const {
return GetSharedMemoryHandle(std::addressof(m_shared_memory));
}

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_memory_permission.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -24,7 +25,7 @@ namespace ams::os {
Result CreateSharedMemory(SharedMemoryType *shared_memory, size_t size, MemoryPermission my_perm, MemoryPermission other_perm);
void AttachSharedMemory(SharedMemoryType *shared_memory, size_t size, Handle handle, bool managed);
void AttachSharedMemory(SharedMemoryType *shared_memory, size_t size, NativeHandle handle, bool managed);
void DestroySharedMemory(SharedMemoryType *shared_memory);
@ -33,6 +34,6 @@ namespace ams::os {
void *GetSharedMemoryAddress(const SharedMemoryType *shared_memory);
size_t GetSharedMemorySize(const SharedMemoryType *shared_memory);
Handle GetSharedMemoryHandle(const SharedMemoryType *shared_memory);
NativeHandle GetSharedMemoryHandle(const SharedMemoryType *shared_memory);
}

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -33,7 +34,7 @@ namespace ams::os {
void *address;
size_t size;
Handle handle;
NativeHandle handle;
mutable impl::InternalCriticalSectionStorage cs_shared_memory;
};

View file

@ -35,7 +35,7 @@ namespace ams::os {
R_ABORT_UNLESS(CreateSystemEvent(std::addressof(this->system_event), clear_mode, inter_process));
}
explicit SystemEvent(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
explicit SystemEvent(NativeHandle read_handle, bool manage_read_handle, NativeHandle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AttachSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, write_handle, manage_write_handle, clear_mode);
}
@ -46,26 +46,26 @@ namespace ams::os {
DestroySystemEvent(std::addressof(this->system_event));
}
void Attach(Handle read_handle, bool manage_read_handle, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
void Attach(NativeHandle read_handle, bool manage_read_handle, NativeHandle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, write_handle, manage_write_handle, clear_mode);
}
void AttachReadableHandle(Handle read_handle, bool manage_read_handle, EventClearMode clear_mode) {
void AttachReadableHandle(NativeHandle read_handle, bool manage_read_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachReadableHandleToSystemEvent(std::addressof(this->system_event), read_handle, manage_read_handle, clear_mode);
}
void AttachWritableHandle(Handle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
void AttachWritableHandle(NativeHandle write_handle, bool manage_write_handle, EventClearMode clear_mode) {
AMS_ABORT_UNLESS(this->system_event.state == SystemEventType::State_NotInitialized);
return AttachWritableHandleToSystemEvent(std::addressof(this->system_event), write_handle, manage_write_handle, clear_mode);
}
Handle DetachReadableHandle() {
NativeHandle DetachReadableHandle() {
return DetachReadableHandleOfSystemEvent(std::addressof(this->system_event));
}
Handle DetachWritableHandle() {
NativeHandle DetachWritableHandle() {
return DetachWritableHandleOfSystemEvent(std::addressof(this->system_event));
}
@ -89,11 +89,11 @@ namespace ams::os {
return ClearSystemEvent(std::addressof(this->system_event));
}
Handle GetReadableHandle() const {
NativeHandle GetReadableHandle() const {
return GetReadableHandleOfSystemEvent(std::addressof(this->system_event));
}
Handle GetWritableHandle() const {
NativeHandle GetWritableHandle() const {
return GetWritableHandleOfSystemEvent(std::addressof(this->system_event));
}

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_event_common.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -26,15 +27,15 @@ namespace ams::os {
Result CreateSystemEvent(SystemEventType *event, EventClearMode clear_mode, bool inter_process);
void DestroySystemEvent(SystemEventType *event);
void AttachSystemEvent(SystemEventType *event, Handle read_handle, bool read_handle_managed, Handle write_handle, bool write_handle_managed, EventClearMode clear_mode);
void AttachReadableHandleToSystemEvent(SystemEventType *event, Handle read_handle, bool manage_read_handle, EventClearMode clear_mode);
void AttachWritableHandleToSystemEvent(SystemEventType *event, Handle write_handle, bool manage_write_handle, EventClearMode clear_mode);
void AttachSystemEvent(SystemEventType *event, NativeHandle read_handle, bool read_handle_managed, NativeHandle write_handle, bool write_handle_managed, EventClearMode clear_mode);
void AttachReadableHandleToSystemEvent(SystemEventType *event, NativeHandle read_handle, bool manage_read_handle, EventClearMode clear_mode);
void AttachWritableHandleToSystemEvent(SystemEventType *event, NativeHandle write_handle, bool manage_write_handle, EventClearMode clear_mode);
Handle DetachReadableHandleOfSystemEvent(SystemEventType *event);
Handle DetachWritableHandleOfSystemEvent(SystemEventType *event);
NativeHandle DetachReadableHandleOfSystemEvent(SystemEventType *event);
NativeHandle DetachWritableHandleOfSystemEvent(SystemEventType *event);
Handle GetReadableHandleOfSystemEvent(const SystemEventType *event);
Handle GetWritableHandleOfSystemEvent(const SystemEventType *event);
NativeHandle GetReadableHandleOfSystemEvent(const SystemEventType *event);
NativeHandle GetWritableHandleOfSystemEvent(const SystemEventType *event);
void SignalSystemEvent(SystemEventType *event);
void WaitSystemEvent(SystemEventType *event);

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_event_types.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -34,8 +35,8 @@ namespace ams::os {
u8 state;
bool is_readable_handle_managed;
bool is_writable_handle_managed;
Handle readable_handle;
Handle writable_handle;
NativeHandle readable_handle;
NativeHandle writable_handle;
};
static_assert(std::is_trivial<InterProcessEventType>::value);

View file

@ -35,7 +35,7 @@ namespace ams::os {
R_ABORT_UNLESS(CreateTransferMemory(std::addressof(this->tmem), address, size, perm));
}
TransferMemory(size_t size, Handle handle, bool managed) {
TransferMemory(size_t size, NativeHandle handle, bool managed) {
this->Attach(size, handle, managed);
}
@ -46,11 +46,11 @@ namespace ams::os {
DestroyTransferMemory(std::addressof(this->tmem));
}
void Attach(size_t size, Handle handle, bool managed) {
void Attach(size_t size, NativeHandle handle, bool managed) {
AttachTransferMemory(std::addressof(this->tmem), size, handle, managed);
}
Handle Detach() {
NativeHandle Detach() {
return DetachTransferMemory(std::addressof(this->tmem));
}

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/os_memory_permission.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -24,8 +25,8 @@ namespace ams::os {
Result CreateTransferMemory(TransferMemoryType *tmem, void *address, size_t size, MemoryPermission perm);
void AttachTransferMemory(TransferMemoryType *tmem, size_t size, Handle handle, bool managed);
Handle DetachTransferMemory(TransferMemoryType *tmem);
void AttachTransferMemory(TransferMemoryType *tmem, size_t size, NativeHandle handle, bool managed);
NativeHandle DetachTransferMemory(TransferMemoryType *tmem);
void DestroyTransferMemory(TransferMemoryType *tmem);

View file

@ -17,6 +17,7 @@
#pragma once
#include <vapours.hpp>
#include <stratosphere/os/impl/os_internal_critical_section.hpp>
#include <stratosphere/os/os_native_handle.hpp>
namespace ams::os {
@ -34,7 +35,7 @@ namespace ams::os {
void *address;
size_t size;
Handle handle;
NativeHandle handle;
mutable impl::InternalCriticalSectionStorage cs_transfer_memory;
};