diff --git a/lib/mammoth/include/mammoth/endpoint_server.h b/lib/mammoth/include/mammoth/endpoint_server.h index 7dd9267..6190cd6 100644 --- a/lib/mammoth/include/mammoth/endpoint_server.h +++ b/lib/mammoth/include/mammoth/endpoint_server.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -7,8 +8,12 @@ class EndpointServer { public: - static glcr::ErrorOr Create(); - static EndpointServer Adopt(z_cap_t endpoint_cap); + EndpointServer() = delete; + EndpointServer(const EndpointServer&) = delete; + EndpointServer& operator=(const EndpointServer&) = delete; + + static glcr::ErrorOr> Create(); + static glcr::UniquePtr Adopt(z_cap_t endpoint_cap); glcr::ErrorOr CreateClient(); diff --git a/lib/mammoth/src/endpoint_server.cpp b/lib/mammoth/src/endpoint_server.cpp index 8516085..5f951c3 100644 --- a/lib/mammoth/src/endpoint_server.cpp +++ b/lib/mammoth/src/endpoint_server.cpp @@ -1,13 +1,13 @@ #include "mammoth/endpoint_server.h" -glcr::ErrorOr EndpointServer::Create() { +glcr::ErrorOr> EndpointServer::Create() { uint64_t cap; RET_ERR(ZEndpointCreate(&cap)); - return EndpointServer(cap); + return glcr::UniquePtr(new EndpointServer(cap)); } -EndpointServer EndpointServer::Adopt(z_cap_t endpoint_cap) { - return EndpointServer(endpoint_cap); +glcr::UniquePtr EndpointServer::Adopt(z_cap_t endpoint_cap) { + return glcr::UniquePtr(new EndpointServer(endpoint_cap)); } glcr::ErrorOr EndpointServer::CreateClient() { diff --git a/sys/denali/denali.cpp b/sys/denali/denali.cpp index 320159b..3b86a16 100644 --- a/sys/denali/denali.cpp +++ b/sys/denali/denali.cpp @@ -28,11 +28,12 @@ uint64_t main(uint64_t init_port_cap) { auto resp_cap = resp_cap_or.value(); PortClient notify = PortClient::AdoptPort(resp_cap.second()); - ASSIGN_OR_RETURN(EndpointServer endpoint, EndpointServer::Create()); - ASSIGN_OR_RETURN(EndpointClient client, endpoint.CreateClient()); + ASSIGN_OR_RETURN(glcr::UniquePtr endpoint, + EndpointServer::Create()); + ASSIGN_OR_RETURN(EndpointClient client, endpoint->CreateClient()); notify.WriteMessage("denali", client.GetCap()); - DenaliServer server(endpoint, driver); + DenaliServer server(glcr::Move(endpoint), driver); RET_ERR(server.RunServer()); // FIXME: Add thread join. return 0; diff --git a/sys/denali/denali_server.cpp b/sys/denali/denali_server.cpp index bf8aa5b..f34b21a 100644 --- a/sys/denali/denali_server.cpp +++ b/sys/denali/denali_server.cpp @@ -1,5 +1,6 @@ #include "denali_server.h" +#include #include #include #include @@ -12,8 +13,9 @@ void HandleResponse(z_cap_t reply_port, uint64_t lba, uint64_t size, } } // namespace -DenaliServer::DenaliServer(EndpointServer server, AhciDriver& driver) - : server_(server), driver_(driver) { +DenaliServer::DenaliServer(glcr::UniquePtr server, + AhciDriver& driver) + : server_(glcr::Move(server)), driver_(driver) { gServer = this; } @@ -21,7 +23,7 @@ glcr::ErrorCode DenaliServer::RunServer() { while (true) { uint64_t buff_size = kBuffSize; z_cap_t reply_port; - RET_ERR(server_.Recieve(&buff_size, read_buffer_, &reply_port)); + RET_ERR(server_->Recieve(&buff_size, read_buffer_, &reply_port)); if (buff_size < sizeof(uint64_t)) { dbgln("Skipping invalid message"); continue; diff --git a/sys/denali/denali_server.h b/sys/denali/denali_server.h index 3da19a4..2cef169 100644 --- a/sys/denali/denali_server.h +++ b/sys/denali/denali_server.h @@ -8,7 +8,7 @@ class DenaliServer { public: - DenaliServer(EndpointServer server, AhciDriver& driver); + DenaliServer(glcr::UniquePtr server, AhciDriver& driver); glcr::ErrorCode RunServer(); @@ -17,7 +17,7 @@ class DenaliServer { private: static const uint64_t kBuffSize = 1024; - EndpointServer server_; + glcr::UniquePtr server_; uint8_t read_buffer_[kBuffSize]; AhciDriver& driver_; diff --git a/sys/yellowstone/yellowstone.cpp b/sys/yellowstone/yellowstone.cpp index e0e6b82..4afae9c 100644 --- a/sys/yellowstone/yellowstone.cpp +++ b/sys/yellowstone/yellowstone.cpp @@ -12,18 +12,18 @@ uint64_t main(uint64_t port_cap) { dbgln("Yellowstone Initializing."); check(ParseInitPort(port_cap)); - ASSIGN_OR_RETURN(YellowstoneServer server, YellowstoneServer::Create()); - Thread server_thread = server.RunServer(); - Thread registration_thread = server.RunRegistration(); + ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create()); + Thread server_thread = server->RunServer(); + Thread registration_thread = server->RunRegistration(); DumpPciEDevices(); uint64_t vaddr; check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootDenaliVmmoCap, &vaddr)); - ASSIGN_OR_RETURN(EndpointClient client, server.GetServerClient()); + ASSIGN_OR_RETURN(EndpointClient client, server->GetServerClient()); check(SpawnProcessFromElfRegion(vaddr, client)); check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootVictoriaFallsVmmoCap, &vaddr)); - ASSIGN_OR_RETURN(client, server.GetServerClient()); + ASSIGN_OR_RETURN(client, server->GetServerClient()); check(SpawnProcessFromElfRegion(vaddr, client)); check(server_thread.Join()); diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index 1e8e0cb..543b73e 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -30,14 +30,16 @@ glcr::ErrorCode HandleDenaliRegistration(z_cap_t endpoint_cap) { } // namespace -glcr::ErrorOr YellowstoneServer::Create() { - ASSIGN_OR_RETURN(EndpointServer server, EndpointServer::Create()); +glcr::ErrorOr> YellowstoneServer::Create() { + ASSIGN_OR_RETURN(auto server, EndpointServer::Create()); ASSIGN_OR_RETURN(PortServer port, PortServer::Create()); - return YellowstoneServer(server, port); + return glcr::UniquePtr( + new YellowstoneServer(glcr::Move(server), port)); } -YellowstoneServer::YellowstoneServer(EndpointServer server, PortServer port) - : server_(server), register_port_(port) {} +YellowstoneServer::YellowstoneServer(glcr::UniquePtr server, + PortServer port) + : server_(glcr::Move(server)), register_port_(port) {} Thread YellowstoneServer::RunServer() { return Thread(ServerThreadBootstrap, this); @@ -52,7 +54,7 @@ void YellowstoneServer::ServerThread() { uint64_t num_bytes = kBufferSize; uint64_t reply_port_cap; // FIXME: Error handling. - check(server_.Recieve(&num_bytes, server_buffer_, &reply_port_cap)); + check(server_->Recieve(&num_bytes, server_buffer_, &reply_port_cap)); YellowstoneGetReq* req = reinterpret_cast(server_buffer_); switch (req->type) { @@ -102,5 +104,5 @@ void YellowstoneServer::RegistrationThread() { } glcr::ErrorOr YellowstoneServer::GetServerClient() { - return server_.CreateClient(); + return server_->CreateClient(); } diff --git a/sys/yellowstone/yellowstone_server.h b/sys/yellowstone/yellowstone_server.h index 9b24d0a..faa7d88 100644 --- a/sys/yellowstone/yellowstone_server.h +++ b/sys/yellowstone/yellowstone_server.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -7,7 +8,7 @@ class YellowstoneServer { public: - static glcr::ErrorOr Create(); + static glcr::ErrorOr> Create(); Thread RunServer(); Thread RunRegistration(); @@ -18,7 +19,7 @@ class YellowstoneServer { glcr::ErrorOr GetServerClient(); private: - EndpointServer server_; + glcr::UniquePtr server_; PortServer register_port_; static const uint64_t kBufferSize = 128; @@ -29,5 +30,5 @@ class YellowstoneServer { z_cap_t denali_cap_ = 0; z_cap_t victoria_falls_cap_ = 0; - YellowstoneServer(EndpointServer server, PortServer port); + YellowstoneServer(glcr::UniquePtr server, PortServer port); };