summaryrefslogtreecommitdiff
path: root/base/HidlSupport.cpp
diff options
context:
space:
mode:
authorHoward Chen <howardsoc@google.com>2017-10-13 14:21:46 +0800
committerSteven Moreland <smoreland@google.com>2017-10-19 01:12:38 -0700
commit9bc35c40a958afcc5711c5c4b306ddc7b68974b0 (patch)
tree7e14ea2a56b768d581fb47062b6a191f2c618cf8 /base/HidlSupport.cpp
parent77f4c859c984264a232a6fa2617c0279be25a366 (diff)
downloadlibhidl-9bc35c40a958afcc5711c5c4b306ddc7b68974b0.tar.gz
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.
Diffstat (limited to 'base/HidlSupport.cpp')
-rw-r--r--base/HidlSupport.cpp27
1 files changed, 27 insertions, 0 deletions
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 @@ bool hidl_string::empty() const {
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