From 38f799ce399142134330018a2167c3891270774f Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 8 Aug 2023 11:40:22 +0200 Subject: [PATCH] libos: Remove some shared pointers and change them to owned/live on the stack --- libos/include/os/LocalClient.h | 8 ++++---- libos/include/os/LocalServer.h | 15 ++++++++------- libos/src/LocalClient.cpp | 4 ++-- libos/src/LocalServer.cpp | 13 +++++++++---- wind/main.cpp | 4 ++-- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/libos/include/os/LocalClient.h b/libos/include/os/LocalClient.h index 46314053..e816db59 100644 --- a/libos/include/os/LocalClient.h +++ b/libos/include/os/LocalClient.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include namespace os @@ -7,7 +7,7 @@ namespace os /** * @brief A client used to connect to a local server socket. */ - class LocalClient : public Shareable + class LocalClient { public: /** @@ -15,9 +15,9 @@ namespace os * * @param path The path of the server socket to connect to. * @param blocking Whether the client should block if no data is available and recv() is called. - * @return Result> An error, or a new client object. + * @return Result> An error, or a new client object. */ - static Result> connect(StringView path, bool blocking); + static Result> connect(StringView path, bool blocking); /** * @brief Return the underlying socket file descriptor used by this object. diff --git a/libos/include/os/LocalServer.h b/libos/include/os/LocalServer.h index 9a1c9a7e..29f673a2 100644 --- a/libos/include/os/LocalServer.h +++ b/libos/include/os/LocalServer.h @@ -1,6 +1,6 @@ #pragma once +#include #include -#include #include namespace os @@ -8,7 +8,7 @@ namespace os /** * @brief A local domain server, used to communicate between processes on the same machine. */ - class LocalServer : public Shareable + class LocalServer { public: /** @@ -16,9 +16,9 @@ namespace os * * @param path The path to use for the server socket. * @param blocking Whether the server should block if no connections are available when calling accept(). - * @return Result> An error, or a new server object. + * @return Result> An error, or a new server object. */ - static Result> create(StringView path, bool blocking); + static Result> create(StringView path, bool blocking); /** * @brief Activate the server and start listening for connections. @@ -41,7 +41,7 @@ namespace os /** * @brief An interface to communicate with clients connected to a local server. */ - class Client : public Shareable + class Client { public: /** @@ -107,6 +107,7 @@ namespace os return m_fd; } + Client(Client&& other); Client(int fd); ~Client(); @@ -119,9 +120,9 @@ namespace os * accept() either blocks until there is one (if the object was created with blocking=true), or returns EAGAIN * (if the object was created with blocking=false). * - * @return Result> An error, or a handle to the new connection. + * @return Result An error, or a handle to the new connection. */ - Result> accept(); + Result accept(); ~LocalServer(); diff --git a/libos/src/LocalClient.cpp b/libos/src/LocalClient.cpp index 42d61a9c..a7b0aff4 100644 --- a/libos/src/LocalClient.cpp +++ b/libos/src/LocalClient.cpp @@ -7,9 +7,9 @@ namespace os { - Result> LocalClient::connect(StringView path, bool blocking) + Result> LocalClient::connect(StringView path, bool blocking) { - auto client = TRY(make_shared()); + auto client = TRY(make_owned()); int sockfd = socket(AF_UNIX, SOCK_STREAM, 0); if (sockfd < 0) return err(errno); diff --git a/libos/src/LocalServer.cpp b/libos/src/LocalServer.cpp index fba62c57..07bccd5e 100644 --- a/libos/src/LocalServer.cpp +++ b/libos/src/LocalServer.cpp @@ -8,9 +8,9 @@ namespace os { - Result> LocalServer::create(StringView path, bool blocking) + Result> LocalServer::create(StringView path, bool blocking) { - auto server = TRY(make_shared()); + auto server = TRY(make_owned()); (void)os::FileSystem::remove(path); // We explicitly ignore any error here, either it doesn't exist (which is // fine), or it cannot be removed, which will make bind() fail later. @@ -43,12 +43,12 @@ namespace os return {}; } - Result> LocalServer::accept() + Result LocalServer::accept() { int fd = ::accept(m_fd, nullptr, nullptr); if (fd < 0) return err(errno); if (!m_blocking) fcntl(fd, F_SETFL, O_NONBLOCK); - return make_shared(fd); + return Client { fd }; } LocalServer::~LocalServer() @@ -56,6 +56,11 @@ namespace os close(m_fd); } + LocalServer::Client::Client(Client&& other) : m_fd(other.m_fd) + { + other.m_fd = -1; + } + LocalServer::Client::Client(int fd) : m_fd(fd) { } diff --git a/wind/main.cpp b/wind/main.cpp index 6df9d762..e8d4442f 100644 --- a/wind/main.cpp +++ b/wind/main.cpp @@ -83,7 +83,7 @@ Result luna_main(int argc, char** argv) TRY(make(ui::Rect { 100, 100, 300, 200 }, ui::RED, "Settings"_sv)); TRY(make(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv)); - Vector> clients; + Vector clients; while (1) { @@ -125,7 +125,7 @@ Result luna_main(int argc, char** argv) { auto client = TRY(server->accept()); os::println("wind: New client connected!"); - TRY(clients.try_append(client)); + TRY(clients.try_append(move(client))); } } }