From e188a61499bfe1d8eda50bc17cf27775630c5892 Mon Sep 17 00:00:00 2001
From: apio <blobs.trading@gmail.com>
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 <luna/SharedPtr.h>
+#include <luna/OwnedPtr.h>
 #include <luna/StringView.h>
 
 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<SharedPtr<LocalClient>> An error, or a new client object.
+         * @return Result<OwnedPtr<LocalClient>> An error, or a new client object.
          */
-        static Result<SharedPtr<LocalClient>> connect(StringView path, bool blocking);
+        static Result<OwnedPtr<LocalClient>> 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 <luna/OwnedPtr.h>
 #include <luna/Result.h>
-#include <luna/SharedPtr.h>
 #include <luna/StringView.h>
 
 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<SharedPtr<LocalServer>> An error, or a new server object.
+         * @return Result<OwnedPtr<LocalServer>> An error, or a new server object.
          */
-        static Result<SharedPtr<LocalServer>> create(StringView path, bool blocking);
+        static Result<OwnedPtr<LocalServer>> 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<SharedPtr<Client>> An error, or a handle to the new connection.
+         * @return Result<Client> An error, or a handle to the new connection.
          */
-        Result<SharedPtr<Client>> accept();
+        Result<Client> 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<SharedPtr<LocalClient>> LocalClient::connect(StringView path, bool blocking)
+    Result<OwnedPtr<LocalClient>> LocalClient::connect(StringView path, bool blocking)
     {
-        auto client = TRY(make_shared<LocalClient>());
+        auto client = TRY(make_owned<LocalClient>());
 
         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<SharedPtr<LocalServer>> LocalServer::create(StringView path, bool blocking)
+    Result<OwnedPtr<LocalServer>> LocalServer::create(StringView path, bool blocking)
     {
-        auto server = TRY(make_shared<LocalServer>());
+        auto server = TRY(make_owned<LocalServer>());
 
         (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<SharedPtr<LocalServer::Client>> LocalServer::accept()
+    Result<LocalServer::Client> 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<Client>(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<int> luna_main(int argc, char** argv)
     TRY(make<Window>(ui::Rect { 100, 100, 300, 200 }, ui::RED, "Settings"_sv));
     TRY(make<Window>(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv));
 
-    Vector<SharedPtr<os::LocalServer::Client>> clients;
+    Vector<os::LocalServer::Client> clients;
 
     while (1)
     {
@@ -125,7 +125,7 @@ Result<int> 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)));
         }
     }
 }