HostConnection: Use unique_ptr for HostConnection objects.

Bug: https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=46839
Bug: 142677230

Change-Id: I7b8a27942b1309862413ec8563148523c5e0717c
diff --git a/system/OpenglSystemCommon/HostConnection.cpp b/system/OpenglSystemCommon/HostConnection.cpp
index a1b17b1..7829545 100644
--- a/system/OpenglSystemCommon/HostConnection.cpp
+++ b/system/OpenglSystemCommon/HostConnection.cpp
@@ -376,20 +376,19 @@
 }
 
 // static
-HostConnection* HostConnection::connect(HostConnection* con) {
-    if (!con) return con;
-
+std::unique_ptr<HostConnection> HostConnection::connect() {
     const enum HostConnectionType connType = getConnectionTypeFromProperty();
     // const enum HostConnectionType connType = HOST_CONNECTION_VIRTIO_GPU;
 
+    // Use "new" to access a non-public constructor.
+    auto con = std::unique_ptr<HostConnection>(new HostConnection);
     switch (connType) {
         case HOST_CONNECTION_ADDRESS_SPACE: {
             auto stream = std::unique_ptr<AddressSpaceStream>(
                 createAddressSpaceStream(STREAM_BUFFER_SIZE));
             if (!stream) {
                 ALOGE("Failed to create AddressSpaceStream for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_ADDRESS_SPACE;
             con->m_grallocType = GRALLOC_TYPE_RANCHU;
@@ -402,13 +401,11 @@
             auto stream = std::make_unique<QemuPipeStream>(STREAM_BUFFER_SIZE);
             if (!stream) {
                 ALOGE("Failed to create QemuPipeStream for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             if (stream->connect() < 0) {
                 ALOGE("Failed to connect to host (QemuPipeStream)!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_QEMU_PIPE;
             con->m_grallocType = GRALLOC_TYPE_RANCHU;
@@ -420,21 +417,18 @@
         case HOST_CONNECTION_TCP: {
 #ifdef __Fuchsia__
             ALOGE("Fuchsia doesn't support HOST_CONNECTION_TCP!!!\n");
-            delete con;
-            return NULL;
+            return nullptr;
             break;
 #else
             auto stream = std::make_unique<TcpStream>(STREAM_BUFFER_SIZE);
             if (!stream) {
                 ALOGE("Failed to create TcpStream for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
 
             if (stream->connect("10.0.2.2", STREAM_PORT_NUM) < 0) {
                 ALOGE("Failed to connect to host (TcpStream)!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_TCP;
             con->m_grallocType = GRALLOC_TYPE_RANCHU;
@@ -449,13 +443,11 @@
             auto stream = std::make_unique<VirtioGpuStream>(STREAM_BUFFER_SIZE);
             if (!stream) {
                 ALOGE("Failed to create VirtioGpu for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             if (stream->connect() < 0) {
                 ALOGE("Failed to connect to host (VirtioGpu)!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_VIRTIO_GPU;
             con->m_grallocType = GRALLOC_TYPE_MINIGBM;
@@ -473,13 +465,11 @@
                 std::make_unique<VirtioGpuPipeStream>(STREAM_BUFFER_SIZE);
             if (!stream) {
                 ALOGE("Failed to create VirtioGpu for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             if (stream->connect() < 0) {
                 ALOGE("Failed to connect to host (VirtioGpu)!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_VIRTIO_GPU_PIPE;
             con->m_grallocType = getGrallocTypeFromProperty();
@@ -509,8 +499,7 @@
                 createVirtioGpuAddressSpaceStream(STREAM_BUFFER_SIZE));
             if (!stream) {
                 ALOGE("Failed to create virtgpu AddressSpaceStream for host connection!!!\n");
-                delete con;
-                return NULL;
+                return nullptr;
             }
             con->m_connectionType = HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE;
             con->m_grallocType = getGrallocTypeFromProperty();
@@ -548,7 +537,7 @@
     con->m_stream->commitBuffer(sizeof(unsigned int));
 
     ALOGD("HostConnection::get() New Host Connection established %p, tid %d\n",
-          con, getCurrentThreadId());
+          con.get(), getCurrentThreadId());
 
     // ALOGD("Address space echo latency check done\n");
     return con;
@@ -565,13 +554,10 @@
     }
 
     if (tinfo->hostConn == NULL) {
-        HostConnection *con = new HostConnection();
-        con = connect(con);
-
-        tinfo->hostConn = con;
+        tinfo->hostConn = HostConnection::createUnique();
     }
 
-    return tinfo->hostConn;
+    return tinfo->hostConn.get();
 }
 
 void HostConnection::exit() {
@@ -580,21 +566,13 @@
         return;
     }
 
-    if (tinfo->hostConn) {
-        delete tinfo->hostConn;
-        tinfo->hostConn = NULL;
-    }
+    tinfo->hostConn.reset();
 }
 
 // static
-HostConnection *HostConnection::createUnique() {
+std::unique_ptr<HostConnection> HostConnection::createUnique() {
     ALOGD("%s: call\n", __func__);
-    return connect(new HostConnection());
-}
-
-// static
-void HostConnection::teardownUnique(HostConnection* con) {
-    delete con;
+    return connect();
 }
 
 GLEncoder *HostConnection::glEncoder()
diff --git a/system/OpenglSystemCommon/HostConnection.h b/system/OpenglSystemCommon/HostConnection.h
index e297b37..4ccd5a7 100644
--- a/system/OpenglSystemCommon/HostConnection.h
+++ b/system/OpenglSystemCommon/HostConnection.h
@@ -141,8 +141,8 @@
     static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo);
     static void exit();
 
-    static HostConnection *createUnique();
-    static void teardownUnique(HostConnection* con);
+    static std::unique_ptr<HostConnection> createUnique();
+    HostConnection(const HostConnection&) = delete;
 
     ~HostConnection();
 
@@ -188,7 +188,7 @@
 private:
     // If the connection failed, |conn| is deleted.
     // Returns NULL if connection failed.
-    static HostConnection* connect(HostConnection* con);
+    static std::unique_ptr<HostConnection> connect();
 
     HostConnection();
     static gl_client_context_t  *s_getGLContext();
diff --git a/system/OpenglSystemCommon/ThreadInfo.cpp b/system/OpenglSystemCommon/ThreadInfo.cpp
index fea6cb7..99b0b9b 100644
--- a/system/OpenglSystemCommon/ThreadInfo.cpp
+++ b/system/OpenglSystemCommon/ThreadInfo.cpp
@@ -45,7 +45,6 @@
 #endif
         ) {
         EGLThreadInfo *ti = (EGLThreadInfo *)ptr;
-        delete ti->hostConn;
         delete ti;
 #ifdef __ANDROID__
         ((void **)__get_tls())[TLS_SLOT_OPENGL] = NULL;
diff --git a/system/OpenglSystemCommon/ThreadInfo.h b/system/OpenglSystemCommon/ThreadInfo.h
index 7d2260a..fa6b44b 100644
--- a/system/OpenglSystemCommon/ThreadInfo.h
+++ b/system/OpenglSystemCommon/ThreadInfo.h
@@ -24,10 +24,10 @@
 
 struct EGLThreadInfo
 {
-    EGLThreadInfo() : currentContext(NULL), hostConn(NULL), eglError(EGL_SUCCESS) { }
+    EGLThreadInfo() : currentContext(NULL), eglError(EGL_SUCCESS) {}
 
     EGLContext_t *currentContext;
-    HostConnection *hostConn;
+    std::unique_ptr<HostConnection> hostConn;
     int           eglError;
 };
 
diff --git a/system/gralloc/gralloc_30.cpp b/system/gralloc/gralloc_30.cpp
index b67f4c5..c012908 100644
--- a/system/gralloc/gralloc_30.cpp
+++ b/system/gralloc/gralloc_30.cpp
@@ -149,7 +149,9 @@
 
 class goldfish_gralloc30_module_t {
 public:
-    goldfish_gralloc30_module_t(): m_hostConn(HostConnection::createUnique()) {
+    // TODO(b/142677230): Use unique pointers instead.
+    goldfish_gralloc30_module_t()
+        : m_hostConn(HostConnection::createUnique().release()) {
         CRASH_IF(!m_hostConn, "m_hostConn cannot be nullptr");
         m_bufferManager = create_buffer_manager(this);
         CRASH_IF(!m_bufferManager, "m_bufferManager cannot be nullptr");
diff --git a/system/gralloc/gralloc_old.cpp b/system/gralloc/gralloc_old.cpp
index 7af9dfe..40f6b6c 100644
--- a/system/gralloc/gralloc_old.cpp
+++ b/system/gralloc/gralloc_old.cpp
@@ -446,7 +446,7 @@
 
 static HostConnection* createOrGetHostConnection() {
     if (!sHostCon) {
-        sHostCon = HostConnection::createUnique();
+        sHostCon = HostConnection::createUnique().release();
     }
     return sHostCon;
 }
diff --git a/system/hwc2/EmuHWC2.cpp b/system/hwc2/EmuHWC2.cpp
index e77e27d..7cdbd60 100644
--- a/system/hwc2/EmuHWC2.cpp
+++ b/system/hwc2/EmuHWC2.cpp
@@ -47,13 +47,13 @@
     return reinterpret_cast<hwc2_function_pointer_t>(function);
 }
 
-static HostConnection *sHostCon = nullptr;
+static std::unique_ptr<HostConnection> sHostCon;
 
 static HostConnection* createOrGetHostConnection() {
     if (!sHostCon) {
         sHostCon = HostConnection::createUnique();
     }
-    return sHostCon;
+    return sHostCon.get();
 }
 
 #define DEFINE_AND_VALIDATE_HOST_CONNECTION \