From f100601cafe333fb873b2582a10761b5f9f2e975 Mon Sep 17 00:00:00 2001 From: wheremyfoodat Date: Mon, 19 Sep 2022 23:27:54 +0300 Subject: [PATCH] [Kernel] More cleanup --- include/kernel/kernel_types.hpp | 23 +++++++++++++++++++---- src/core/kernel/events.cpp | 6 +----- src/core/kernel/kernel.cpp | 4 ++-- src/core/kernel/ports.cpp | 28 +++++++++------------------- src/core/kernel/resource_limits.cpp | 2 +- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/include/kernel/kernel_types.hpp b/include/kernel/kernel_types.hpp index 3add4b04..2d6a7fc6 100644 --- a/include/kernel/kernel_types.hpp +++ b/include/kernel/kernel_types.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include "handles.hpp" #include "helpers.hpp" @@ -57,24 +58,33 @@ struct ResourceLimits { s32 currentCommit = 0; }; -struct ProcessData { +struct Process { // Resource limits for this process ResourceLimits limits; }; -struct EventData { +struct Event { ResetType resetType = RESET_ONESHOT; + + Event(ResetType resetType) : resetType(resetType) {} }; -struct PortData { +struct Port { static constexpr u32 maxNameLen = 11; char name[maxNameLen + 1] = {}; bool isPublic = false; // Setting name=NULL creates a private port not accessible from svcConnectToPort. + + Port(const char* name) { + // If the name is empty (ie the first char is the null terminator) then the port is private + isPublic = name[0] != '\0'; + std::strncpy(this->name, name, maxNameLen); + } }; -struct SessionData { +struct Session { Handle portHandle; // The port this session is subscribed to + Session(Handle portHandle) : portHandle(portHandle) {} }; struct Thread { @@ -106,4 +116,9 @@ struct KernelObject { // Our destructor does not free the data in order to avoid it being freed when our std::vector is expanded // Thus, the kernel needs to delete it when appropriate ~KernelObject() {} + + template + T* getData() { + return static_cast(data); + } }; \ No newline at end of file diff --git a/src/core/kernel/events.cpp b/src/core/kernel/events.cpp index 44b0ae94..05644040 100644 --- a/src/core/kernel/events.cpp +++ b/src/core/kernel/events.cpp @@ -11,11 +11,7 @@ const char* Kernel::resetTypeToString(u32 type) { Handle Kernel::makeEvent(ResetType resetType) { Handle ret = makeObject(KernelObjectType::Event); - objects[ret].data = new EventData(); - - auto eventData = static_cast(objects[ret].data); - eventData->resetType = resetType; - + objects[ret].data = new Event(resetType); return ret; } diff --git a/src/core/kernel/kernel.cpp b/src/core/kernel/kernel.cpp index f9b2d390..b578c854 100644 --- a/src/core/kernel/kernel.cpp +++ b/src/core/kernel/kernel.cpp @@ -26,8 +26,8 @@ Handle Kernel::makeProcess() { const Handle resourceLimitHandle = makeObject(KernelObjectType::ResourceLimit); // Allocate data - objects[processHandle].data = new ProcessData(); - const auto processData = static_cast(objects[processHandle].data); + objects[processHandle].data = new Process(); + const auto processData = objects[processHandle].getData(); // Link resource limit object with its parent process objects[resourceLimitHandle].data = &processData->limits; diff --git a/src/core/kernel/ports.cpp b/src/core/kernel/ports.cpp index ead1362b..d2f4158b 100644 --- a/src/core/kernel/ports.cpp +++ b/src/core/kernel/ports.cpp @@ -4,13 +4,7 @@ Handle Kernel::makePort(const char* name) { Handle ret = makeObject(KernelObjectType::Port); portHandles.push_back(ret); // Push the port handle to our cache of port handles - - objects[ret].data = new PortData(); - const auto data = static_cast(objects[ret].data); - std::strncpy(data->name, name, PortData::maxNameLen); - - // If the name is empty (ie the first char is the null terminator) then the port is private - data->isPublic = name[0] != '\0'; + objects[ret].data = new Port(name); // printf("Created %s port \"%s\" with handle %d\n", data->isPublic ? "public" : "private", data->name, ret); return ret; @@ -24,11 +18,7 @@ Handle Kernel::makeSession(Handle portHandle) { // Allocate data for session const Handle ret = makeObject(KernelObjectType::Session); - objects[ret].data = new SessionData(); - auto sessionData = static_cast(objects[ret].data); - - // Link session with its parent port - sessionData->portHandle = portHandle; + objects[ret].data = new Session(portHandle); return ret; } @@ -36,8 +26,8 @@ Handle Kernel::makeSession(Handle portHandle) { // If there's no such port, return nullopt std::optional Kernel::getPortHandle(const char* name) { for (auto handle : portHandles) { - const auto data = static_cast(objects[handle].data); - if (std::strncmp(name, data->name, PortData::maxNameLen) == 0) { + const auto data = objects[handle].getData(); + if (std::strncmp(name, data->name, Port::maxNameLen) == 0) { return handle; } } @@ -49,9 +39,9 @@ std::optional Kernel::getPortHandle(const char* name) { void Kernel::connectToPort() { const u32 handlePointer = regs[0]; // Read up to max + 1 characters to see if the name is too long - std::string port = mem.readString(regs[1], PortData::maxNameLen + 1); + std::string port = mem.readString(regs[1], Port::maxNameLen + 1); - if (port.size() > PortData::maxNameLen) { + if (port.size() > Port::maxNameLen) { Helpers::panic("ConnectToPort: Port name too long\n"); regs[0] = SVCResult::PortNameTooLong; return; @@ -68,7 +58,7 @@ void Kernel::connectToPort() { Handle portHandle = optionalHandle.value(); printf("ConnectToPort(handle pointer = %08X, port = \"%s\")\n", handlePointer, port.c_str()); - const auto portData = static_cast(objects[portHandle].data); + const auto portData = objects[portHandle].getData(); if (!portData->isPublic) { Helpers::panic("ConnectToPort: Attempted to connect to private port"); } @@ -102,13 +92,13 @@ void Kernel::sendSyncRequest() { return; } - const auto sessionData = static_cast(session->data); + const auto sessionData = static_cast(session->data); const Handle portHandle = sessionData->portHandle; if (portHandle == srvHandle) { // Special-case SendSyncRequest targetting the "srv: port" serviceManager.handleSyncRequest(messagePointer); } else { - const auto portData = static_cast(objects[portHandle].data); + const auto portData = objects[portHandle].getData(); Helpers::panic("SendSyncRequest targetting port %s\n", portData->name); } diff --git a/src/core/kernel/resource_limits.cpp b/src/core/kernel/resource_limits.cpp index 30f0d695..e96691d0 100644 --- a/src/core/kernel/resource_limits.cpp +++ b/src/core/kernel/resource_limits.cpp @@ -14,7 +14,7 @@ void Kernel::getResourceLimit() { return; } - const auto processData = static_cast(process->data); + const auto processData = static_cast(process->data); regs[0] = SVCResult::Success; regs[1] = processData->limits.handle;