summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWonsik Kim <wonsik@google.com>2014-03-20 15:42:03 +0900
committerWonsik Kim <wonsik@google.com>2014-03-24 11:40:36 +0900
commitc4cc584bbd031b94c37912d8c5e340b24ff31b57 (patch)
treea70d9c987788b1503ecfe2fd5936d07d681a59cc
parenta2910877a6781f57a7ef3eea7343fa46bc3e1bd0 (diff)
downloadsystem_core-c4cc584bbd031b94c37912d8c5e340b24ff31b57.tar.gz
system_core-c4cc584bbd031b94c37912d8c5e340b24ff31b57.tar.bz2
system_core-c4cc584bbd031b94c37912d8c5e340b24ff31b57.zip
Clarify ownership for NativeHandle::mHandle
Change-Id: I0835278df1aa78f10d5493d7ef2c9e4a15c0fee9
-rw-r--r--include/utils/NativeHandle.h9
-rw-r--r--libutils/NativeHandle.cpp15
2 files changed, 15 insertions, 9 deletions
diff --git a/include/utils/NativeHandle.h b/include/utils/NativeHandle.h
index dc49b5181..b82516879 100644
--- a/include/utils/NativeHandle.h
+++ b/include/utils/NativeHandle.h
@@ -26,9 +26,11 @@ namespace android {
class NativeHandle: public LightRefBase<NativeHandle> {
public:
- // Create a refcounted wrapper around a native_handle_t.
+ // Create a refcounted wrapper around a native_handle_t, and declare
+ // whether the wrapper owns the handle (so that it should clean up the
+ // handle upon destruction) or not.
// If handle is NULL, no NativeHandle will be created.
- static sp<NativeHandle> create(native_handle_t* handle);
+ static sp<NativeHandle> create(native_handle_t* handle, bool ownsHandle);
const native_handle_t* handle() const {
return mHandle;
@@ -38,10 +40,11 @@ private:
// for access to the destructor
friend class LightRefBase<NativeHandle>;
- NativeHandle(native_handle_t* handle);
+ NativeHandle(native_handle_t* handle, bool ownsHandle);
virtual ~NativeHandle();
native_handle_t* mHandle;
+ bool mOwnsHandle;
// non-copyable
NativeHandle(const NativeHandle&);
diff --git a/libutils/NativeHandle.cpp b/libutils/NativeHandle.cpp
index 6632851af..e4daca7ff 100644
--- a/libutils/NativeHandle.cpp
+++ b/libutils/NativeHandle.cpp
@@ -19,17 +19,20 @@
namespace android {
-sp<NativeHandle> NativeHandle::create(native_handle_t* handle) {
- return handle ? new NativeHandle(handle) : NULL;
+sp<NativeHandle> NativeHandle::create(
+ native_handle_t* handle, bool ownsHandle) {
+ return handle ? new NativeHandle(handle, ownsHandle) : NULL;
}
-NativeHandle::NativeHandle(native_handle_t* handle)
-: mHandle(handle)
+NativeHandle::NativeHandle(native_handle_t* handle, bool ownsHandle)
+: mHandle(handle), mOwnsHandle(ownsHandle)
{}
NativeHandle::~NativeHandle() {
- native_handle_close(mHandle);
- native_handle_delete(mHandle);
+ if (mOwnsHandle) {
+ native_handle_close(mHandle);
+ native_handle_delete(mHandle);
+ }
}
} // namespace android