1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-18 01:46:47 +00:00

kern: simplify handle table registration for port/session

This commit is contained in:
Michael Scire 2021-04-07 15:03:26 -07:00 committed by SciresM
parent ee91063bbb
commit 911e431d65
2 changed files with 19 additions and 23 deletions

View file

@ -38,28 +38,27 @@ namespace ams::kern::svc {
KPort *port = KPort::Create(); KPort *port = KPort::Create();
R_UNLESS(port != nullptr, svc::ResultOutOfResource()); R_UNLESS(port != nullptr, svc::ResultOutOfResource());
/* Reserve a handle for the server port. */
R_TRY(handle_table.Reserve(out_server_handle));
auto reserve_guard = SCOPE_GUARD { handle_table.Unreserve(*out_server_handle); };
/* Initialize the new port. */ /* Initialize the new port. */
port->Initialize(max_sessions, false, 0); port->Initialize(max_sessions, false, 0);
/* Register the port. */ /* Register the port. */
KPort::Register(port); KPort::Register(port);
/* Ensure that our only reference to the port is in the handle table when we're done. */
ON_SCOPE_EXIT {
port->GetClientPort().Close();
port->GetServerPort().Close();
};
/* Register the handle in the table. */ /* Register the handle in the table. */
handle_table.Register(*out_server_handle, std::addressof(port->GetServerPort())); R_TRY(handle_table.Add(out_server_handle, std::addressof(port->GetServerPort())));
reserve_guard.Cancel(); auto handle_guard = SCOPE_GUARD { handle_table.Remove(*out_server_handle); };
auto register_guard = SCOPE_GUARD { handle_table.Remove(*out_server_handle); };
/* Create a new object name. */ /* Create a new object name. */
R_TRY(KObjectName::NewFromName(std::addressof(port->GetClientPort()), name)); R_TRY(KObjectName::NewFromName(std::addressof(port->GetClientPort()), name));
/* Perform resource cleanup. */ /* We succeeded, so don't leak the handle. */
port->GetServerPort().Close(); handle_guard.Cancel();
port->GetClientPort().Close();
register_guard.Cancel();
} else /* if (max_sessions == 0) */ { } else /* if (max_sessions == 0) */ {
/* Ensure that this else case is correct. */ /* Ensure that this else case is correct. */
MESOSPHERE_AUDIT(max_sessions == 0); MESOSPHERE_AUDIT(max_sessions == 0);
@ -157,20 +156,17 @@ namespace ams::kern::svc {
R_TRY(handle_table.Reserve(out)); R_TRY(handle_table.Reserve(out));
auto handle_guard = SCOPE_GUARD { handle_table.Unreserve(*out); }; auto handle_guard = SCOPE_GUARD { handle_table.Unreserve(*out); };
/* Create and register session. */ /* Create the session. */
KAutoObject *session;
if (client_port->IsLight()) { if (client_port->IsLight()) {
KLightClientSession *session; R_TRY(client_port->CreateLightSession(reinterpret_cast<KLightClientSession **>(std::addressof(session))));
R_TRY(client_port->CreateLightSession(std::addressof(session)));
handle_table.Register(*out, session);
session->Close();
} else { } else {
KClientSession *session; R_TRY(client_port->CreateSession(reinterpret_cast<KClientSession **>(std::addressof(session))));
R_TRY(client_port->CreateSession(std::addressof(session))); }
/* Register the session. */
handle_table.Register(*out, session); handle_table.Register(*out, session);
session->Close(); session->Close();
}
/* We succeeded. */ /* We succeeded. */
handle_guard.Cancel(); handle_guard.Cancel();

View file

@ -43,8 +43,8 @@ namespace ams::kern::svc {
/* Ensure that we clean up the session (and its only references are handle table) on function end. */ /* Ensure that we clean up the session (and its only references are handle table) on function end. */
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
session->GetServerSession().Close();
session->GetClientSession().Close(); session->GetClientSession().Close();
session->GetServerSession().Close();
}; };
/* Register the session. */ /* Register the session. */