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/HidlSupport.cpp b/base/HidlSupport.cpp
index 4de7f7c..f857281 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -273,6 +273,33 @@
return mSize == 0;
}
+sp<HidlMemory> HidlMemory::getInstance(hidl_memory&& mem) {
+ sp<HidlMemory> instance = new HidlMemory();
+ *instance = std::move(mem);
+ return instance;
+}
+
+sp<HidlMemory> HidlMemory::getInstance(const hidl_string& name, int fd, uint64_t size) {
+ native_handle_t* handle = native_handle_create(1, 0);
+ if (!handle) {
+ close(fd);
+ LOG(ERROR) << "native_handle_create fails";
+ return new HidlMemory();
+ }
+ handle->data[0] = fd;
+
+ hidl_handle hidlHandle;
+ hidlHandle.setTo(handle, true /* shouldOwn */);
+
+ sp<HidlMemory> instance = new HidlMemory(name, std::move(hidlHandle), size);
+ return instance;
+}
+
+// it's required to have at least one out-of-line method to avoid weak vtable
+HidlMemory::~HidlMemory() {
+ hidl_memory::~hidl_memory();
+}
+
} // namespace hardware
} // namespace android