[lldb] Change Communication::SetConnection to take a unique_ptr

The function takes ownership of the object. This makes that explicit,
and avoids unowned pointers floating around.
This commit is contained in:
Pavel Labath 2020-04-02 14:40:59 +02:00
parent 13a1504ffb
commit 451741a9d7
14 changed files with 33 additions and 35 deletions

View File

@ -221,7 +221,7 @@ public:
///
/// \see
/// class Connection
void SetConnection(Connection *connection);
void SetConnection(std::unique_ptr<Connection> connection);
/// Starts a read thread whose sole purpose it to read bytes from the
/// current connection. This function will call connection's read function:

View File

@ -63,7 +63,7 @@ ConnectionStatus SBCommunication::Connect(const char *url) {
if (m_opaque) {
if (!m_opaque->HasConnection())
m_opaque->SetConnection(Host::CreateDefaultConnection(url).release());
m_opaque->SetConnection(Host::CreateDefaultConnection(url));
return m_opaque->Connect(url, nullptr);
}
return eConnectionStatusNoConnection;
@ -79,7 +79,8 @@ ConnectionStatus SBCommunication::AdoptFileDesriptor(int fd, bool owns_fd) {
if (m_opaque->IsConnected())
m_opaque->Disconnect();
}
m_opaque->SetConnection(new ConnectionFileDescriptor(fd, owns_fd));
m_opaque->SetConnection(
std::make_unique<ConnectionFileDescriptor>(fd, owns_fd));
if (m_opaque->IsConnected())
status = eConnectionStatusSuccess;
else

View File

@ -399,10 +399,10 @@ void Communication::SynchronizeWithReadThread() {
listener_sp->GetEvent(event_sp, llvm::None);
}
void Communication::SetConnection(Connection *connection) {
void Communication::SetConnection(std::unique_ptr<Connection> connection) {
Disconnect(nullptr);
StopReadThread(nullptr);
m_connection_sp.reset(connection);
m_connection_sp = std::move(connection);
}
const char *

View File

@ -290,7 +290,7 @@ Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
GetHostname());
} else {
if (args.GetArgumentCount() == 1) {
m_gdb_client.SetConnection(new ConnectionFileDescriptor());
m_gdb_client.SetConnection(std::make_unique<ConnectionFileDescriptor>());
// we're going to reuse the hostname when we connect to the debugserver
int port;
std::string path;

View File

@ -250,7 +250,7 @@ Status ProcessKDP::DoConnectRemote(Stream *strm, llvm::StringRef remote_url) {
const uint16_t reply_port = socket.GetLocalPortNumber();
if (reply_port != 0) {
m_comm.SetConnection(conn_up.release());
m_comm.SetConnection(std::move(conn_up));
if (m_comm.SendRequestReattach(reply_port)) {
if (m_comm.SendRequestConnect(reply_port, reply_port,

View File

@ -869,7 +869,7 @@ Status GDBRemoteCommunication::StartListenThread(const char *hostname,
else
snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
m_listen_url = listen_url;
SetConnection(new ConnectionFileDescriptor());
SetConnection(std::make_unique<ConnectionFileDescriptor>());
llvm::Expected<HostThread> listen_thread = ThreadLauncher::LaunchThread(
listen_url, GDBRemoteCommunication::ListenThread, this);
if (!listen_thread)
@ -1252,11 +1252,12 @@ GDBRemoteCommunication::ConnectLocally(GDBRemoteCommunication &client,
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"Unable to connect: %s", status.AsCString());
client.SetConnection(conn_up.release());
client.SetConnection(std::move(conn_up));
if (llvm::Error error = accept_status.get().ToError())
return error;
server.SetConnection(new ConnectionFileDescriptor(accept_socket));
server.SetConnection(
std::make_unique<ConnectionFileDescriptor>(accept_socket));
return llvm::Error::success();
}

View File

@ -1015,9 +1015,9 @@ void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() {
}
Status GDBRemoteCommunicationServerLLGS::InitializeConnection(
std::unique_ptr<Connection> &&connection) {
std::unique_ptr<Connection> connection) {
IOObjectSP read_object_sp = connection->GetReadObject();
GDBRemoteCommunicationServer::SetConnection(connection.release());
GDBRemoteCommunicationServer::SetConnection(std::move(connection));
Status error;
m_network_handle_up = m_mainloop.RegisterReadObject(
@ -1053,7 +1053,7 @@ Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) {
}
m_stdio_communication.SetCloseOnEOF(false);
m_stdio_communication.SetConnection(conn_up.release());
m_stdio_communication.SetConnection(std::move(conn_up));
if (!m_stdio_communication.IsConnected()) {
error.SetErrorString(
"failed to set connection for inferior I/O communication");

View File

@ -67,7 +67,7 @@ public:
void DidExec(NativeProcessProtocol *process) override;
Status InitializeConnection(std::unique_ptr<Connection> &&connection);
Status InitializeConnection(std::unique_ptr<Connection> connection);
protected:
MainLoop &m_mainloop;

View File

@ -960,7 +960,7 @@ Status ProcessGDBRemote::ConnectToDebugserver(llvm::StringRef connect_url) {
uint32_t retry_count = 0;
while (!m_gdb_comm.IsConnected()) {
if (conn_up->Connect(connect_url, &error) == eConnectionStatusSuccess) {
m_gdb_comm.SetConnection(conn_up.release());
m_gdb_comm.SetConnection(std::move(conn_up));
break;
} else if (error.WasInterrupted()) {
// If we were interrupted, don't keep retrying.
@ -3482,7 +3482,8 @@ Status ProcessGDBRemote::LaunchAndConnectToDebugserver(
// Our process spawned correctly, we can now set our connection to use
// our end of the socket pair
cleanup_our.release();
m_gdb_comm.SetConnection(new ConnectionFileDescriptor(our_socket, true));
m_gdb_comm.SetConnection(
std::make_unique<ConnectionFileDescriptor>(our_socket, true));
#endif
StartAsyncThread();
}

View File

@ -953,7 +953,7 @@ bool ScriptInterpreterPythonImpl::ExecuteOneLine(
true));
#endif
if (conn_up->IsConnected()) {
output_comm.SetConnection(conn_up.release());
output_comm.SetConnection(std::move(conn_up));
output_comm.SetReadThreadBytesReceivedCallback(
ReadThreadBytesReceived, &result->GetOutputStream());
output_comm.StartReadThread();

View File

@ -4428,23 +4428,18 @@ protected:
void Process::SetSTDIOFileDescriptor(int fd) {
// First set up the Read Thread for reading/handling process I/O
m_stdio_communication.SetConnection(
std::make_unique<ConnectionFileDescriptor>(fd, true));
if (m_stdio_communication.IsConnected()) {
m_stdio_communication.SetReadThreadBytesReceivedCallback(
STDIOReadThreadBytesReceived, this);
m_stdio_communication.StartReadThread();
std::unique_ptr<ConnectionFileDescriptor> conn_up(
new ConnectionFileDescriptor(fd, true));
// Now read thread is set up, set up input reader.
if (conn_up) {
m_stdio_communication.SetConnection(conn_up.release());
if (m_stdio_communication.IsConnected()) {
m_stdio_communication.SetReadThreadBytesReceivedCallback(
STDIOReadThreadBytesReceived, this);
m_stdio_communication.StartReadThread();
// Now read thread is set up, set up input reader.
if (!m_process_input_reader)
m_process_input_reader =
std::make_shared<IOHandlerProcessSTDIO>(this, fd);
}
if (!m_process_input_reader)
m_process_input_reader =
std::make_shared<IOHandlerProcessSTDIO>(this, fd);
}
}

View File

@ -343,7 +343,7 @@ int main_platform(int argc, char *argv[]) {
// connections while a connection is active.
acceptor_up.reset();
}
platform.SetConnection(conn);
platform.SetConnection(std::unique_ptr<Connection>(conn));
if (platform.IsConnected()) {
if (inferior_arguments.GetArgumentCount() > 0) {

View File

@ -77,7 +77,7 @@ public:
class MockServerWithMockConnection : public MockServer {
public:
MockServerWithMockConnection() : MockServer() {
SetConnection(new MockConnection(m_packets));
SetConnection(std::make_unique<MockConnection>(m_packets));
}
llvm::ArrayRef<std::string> GetPackets() { return m_packets; };

View File

@ -30,7 +30,7 @@ using namespace llgs_tests;
#endif
TestClient::TestClient(std::unique_ptr<Connection> Conn) {
SetConnection(Conn.release());
SetConnection(std::move(Conn));
SetPacketTimeout(std::chrono::seconds(10));
}