Add HidlMemory

HidlMemory is a wrapper class to support sp<> for hidl_memory. It
also provides a factory method to create an instance of the 'ashmem'
hidl_memory from a opened file descriptor. The number of factory
methods can be increase to support other type of hidl_memory without
break the ABI.

Change-Id: I18176aecdde1c5c1e10582a4a2e87fe466730e17
Bug: 67758915
Test: with the master branch on the Pixel phone.
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index ab16709..78e0867 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -268,6 +268,9 @@
         return mSize;
     }
 
+    // @return true if it's valid
+    inline bool valid() const { return handle() != nullptr; }
+
     // offsetof(hidl_memory, mHandle) exposed since mHandle is private.
     static const size_t kOffsetOfHandle;
     // offsetof(hidl_memory, mName) exposed since mHandle is private.
@@ -279,6 +282,30 @@
     hidl_string mName __attribute__ ((aligned(8)));
 };
 
+// HidlMemory is a wrapper class to support sp<> for hidl_memory. It also
+// provides factory methods to create an instance from hidl_memory or
+// from a opened file descriptor. The number of factory methods can be increase
+// 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(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
+    static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size);
+
+protected:
+    HidlMemory() : hidl_memory() {}
+    HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size)
+        : hidl_memory(name, std::move(handle), size) {}
+    ~HidlMemory();
+    HidlMemory& operator=(hidl_memory&& src) {
+        hidl_memory::operator=(src);
+        return *this;
+    }
+};
 ////////////////////////////////////////////////////////////////////////////////
 
 template<typename T>