Refine HidlMemory

Add construct from a const hidl_memory& so that we can easily
copy it out of a synchronous callback

Bug: 67758915
Test: hidl_test
Change-Id: I0e10cc4c9321197787657f957ea184a5e7262744
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index aec3fed..8f3c057 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -273,9 +273,15 @@
     return mSize == 0;
 }
 
+sp<HidlMemory> HidlMemory::getInstance(const hidl_memory& mem) {
+    sp<HidlMemory> instance = new HidlMemory();
+    instance->hidl_memory::operator=(mem);
+    return instance;
+}
+
 sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) {
     sp<HidlMemory> instance = new HidlMemory();
-    *instance = std::move(mem);
+    instance->hidl_memory::operator=(std::move(mem));
     return instance;
 }
 
@@ -295,6 +301,11 @@
     return instance;
 }
 
+HidlMemory::HidlMemory() : hidl_memory() {}
+
+HidlMemory::HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
+        : hidl_memory(name, std::move(handle), size) {}
+
 // it's required to have at least one out-of-line method to avoid weak vtable
 HidlMemory::~HidlMemory() {
     hidl_memory::~hidl_memory();
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index e60f2f3..486fd0a 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -295,23 +295,22 @@
 // to support other type of hidl_memory without break the ABI.
 class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase {
 public:
+    static sp<HidlMemory> getInstance(const hidl_memory& mem);
+
     static sp<HidlMemory> getInstance(hidl_memory&& mem);
 
     static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size);
     // @param fd, shall be opened and points to the resource.
     // @note this method takes the ownership of the fd and will close it in
     //     destructor
+    // @return nullptr in failure with the fd closed
     static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
 
     virtual ~HidlMemory();
+
 protected:
-    HidlMemory() : hidl_memory() {}
-    HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
-        : hidl_memory(name, std::move(handle), size) {}
-    HidlMemory& operator=(hidl_memory&& src) {
-        hidl_memory::operator=(src);
-        return *this;
-    }
+    HidlMemory();
+    HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size);
 };
 ////////////////////////////////////////////////////////////////////////////////