Merge "Align all shared memory offsets to word boundary."
diff --git a/Android.bp b/Android.bp
index 5fe6ad5..d44a5e7 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,6 +30,7 @@
         "libhwbinder",
         "liblog",
         "libutils",
+        "libcutils",
     ],
     static_libs: ["libgtest"],
 
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 3cc4b14..dce2585 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -153,18 +153,34 @@
 
 // ----------------------------------------------------------------------
 // HidlInstrumentor implementation.
-HidlInstrumentor::HidlInstrumentor(const std::string &prefix) {
-    mEnableInstrumentation = property_get_bool("hal.instrumentation.enable",
-                                               false);
-    if (mEnableInstrumentation) {
-        registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks);
-    }
+HidlInstrumentor::HidlInstrumentor(const std::string &prefix)
+        : mInstrumentationLibPrefix(prefix) {
+    configureInstrumentation(false);
 }
 
 HidlInstrumentor:: ~HidlInstrumentor() {}
 
+void HidlInstrumentor::configureInstrumentation(bool log) {
+    bool enable_instrumentation = property_get_bool(
+            "hal.instrumentation.enable",
+            false);
+    if (enable_instrumentation != mEnableInstrumentation) {
+        mEnableInstrumentation = enable_instrumentation;
+        if (mEnableInstrumentation) {
+            if (log) {
+                LOG(INFO) << "Enable instrumentation.";
+            }
+            registerInstrumentationCallbacks (&mInstrumentationCallbacks);
+        } else {
+            if (log) {
+                LOG(INFO) << "Disable instrumentation.";
+            }
+            mInstrumentationCallbacks.clear();
+        }
+    }
+}
+
 void HidlInstrumentor::registerInstrumentationCallbacks(
-        const std::string &profilerPrefix,
         std::vector<InstrumentationCallback> *instrumentationCallbacks) {
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
     std::vector<std::string> instrumentationLibPaths;
@@ -188,7 +204,7 @@
 
         struct dirent *file;
         while ((file = readdir(dir)) != NULL) {
-            if (!isInstrumentationLib(profilerPrefix, file))
+            if (!isInstrumentationLib(file))
                 continue;
 
             void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
@@ -224,13 +240,11 @@
 #endif
 }
 
-bool HidlInstrumentor::isInstrumentationLib(
-        const std::string &profiler_prefix,
-        const dirent *file) {
+bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
     if (file->d_type != DT_REG) return false;
     std::cmatch cm;
-    std::regex e("^" + profiler_prefix + "(.*).profiler.so$");
+    std::regex e("^" + mInstrumentationLibPrefix + "(.*).profiler.so$");
     if (std::regex_match(file->d_name, cm, e)) return true;
 #endif
     return false;
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 1b0184a..106fb02 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -226,8 +226,15 @@
     // copy assignment
     hidl_memory &operator=(const hidl_memory &other) {
         if (this != &other) {
-            mOwnsHandle = true;
-            mHandle = native_handle_clone(other.mHandle);
+            cleanup();
+
+            if (other.mHandle == nullptr) {
+                mHandle = nullptr;
+                mOwnsHandle = false;
+            } else {
+                mOwnsHandle = true;
+                mHandle = native_handle_clone(other.mHandle);
+            }
             mSize = other.mSize;
             mName = other.mName;
         }
@@ -238,10 +245,7 @@
     // TODO move constructor/move assignment
 
     ~hidl_memory() {
-        // TODO if we had previously mapped from this object, unmap
-        if (mOwnsHandle) {
-            native_handle_close(mHandle);
-        }
+        cleanup();
     }
 
     const native_handle_t* handle() const {
@@ -260,11 +264,19 @@
     static const size_t kOffsetOfHandle;
     // offsetof(hidl_memory, mName) exposed since mHandle is private.
     static const size_t kOffsetOfName;
+
 private:
     bool mOwnsHandle;
     hidl_handle mHandle;
     size_t mSize;
     hidl_string mName;
+
+    void cleanup() {
+        // TODO(b/33812533): native_handle_delete
+        if (mOwnsHandle && mHandle != nullptr) {
+            native_handle_close(mHandle);
+        }
+    }
 };
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -839,6 +851,10 @@
     virtual ~HidlInstrumentor();
 
  protected:
+    // Set mEnableInstrumentation based on system property
+    // hal.instrumentation.enable, register/de-register instrumentation
+    // callbacks if mEnableInstrumentation is true/false.
+    void configureInstrumentation(bool log=true);
     // Function that lookup and dynamically loads the hidl instrumentation
     // libraries and registers the instrumentation callback functions.
     //
@@ -852,18 +868,18 @@
     //
     // A no-op for user build.
     void registerInstrumentationCallbacks(
-            const std::string &profilerPrefix,
             std::vector<InstrumentationCallback> *instrumentationCallbacks);
 
     // Utility function to determine whether a give file is a instrumentation
     // library (i.e. the file name follow the expected pattern).
-    bool isInstrumentationLib(
-            const std::string &profilerPrefix,
-            const dirent *file);
+    bool isInstrumentationLib(const dirent *file);
+
     // A list of registered instrumentation callbacks.
     std::vector<InstrumentationCallback> mInstrumentationCallbacks;
     // Flag whether to enable instrumentation.
     bool mEnableInstrumentation;
+    // Prefix to lookup the instrumentation libraries.
+    std::string mInstrumentationLibPrefix;
 };
 
 }  // namespace hardware
diff --git a/base/include/hidl/Status.h b/base/include/hidl/Status.h
index f17c968..1be818d 100644
--- a/base/include/hidl/Status.h
+++ b/base/include/hidl/Status.h
@@ -172,12 +172,6 @@
             return mStatus.isOk();
         }
 
-        // TODO(b/31348667) deprecate and replace with 'string description()'
-        const Status& getStatus() const {
-            mCheckedStatus = true;
-            return mStatus;
-        }
-
         // For debugging purposes only
         std::string description() const {
             // Doesn't consider checked.
diff --git a/test_main.cpp b/test_main.cpp
index bdf4b49..5349fc8 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -110,6 +110,30 @@
     EXPECT_FALSE(hs1 == stringNE);
 }
 
+TEST_F(LibHidlTest, MemoryTest) {
+    using android::hardware::hidl_memory;
+
+    hidl_memory mem1 = hidl_memory(); // default constructor
+    hidl_memory mem2 = mem1; // copy constructor (nullptr)
+
+    EXPECT_EQ(nullptr, mem2.handle());
+
+    native_handle_t* testHandle = native_handle_create(0 /* numInts */, 0 /* numFds */);
+
+    hidl_memory mem3 = hidl_memory("foo", testHandle, 42 /* size */); // owns testHandle
+    hidl_memory mem4 = mem3; // copy constructor (regular handle)
+
+    EXPECT_EQ(mem3.name(), mem4.name());
+    EXPECT_EQ(mem3.size(), mem4.size());
+    EXPECT_NE(nullptr, mem4.handle());
+    EXPECT_NE(mem3.handle(), mem4.handle()); // check handle cloned
+
+    hidl_memory mem5 = hidl_memory("foo", nullptr, 0); // hidl memory works with nullptr handle
+    hidl_memory mem6 = mem5;
+    EXPECT_EQ(nullptr, mem5.handle());
+    EXPECT_EQ(nullptr, mem6.handle());
+}
+
 TEST_F(LibHidlTest, VecInitTest) {
     using android::hardware::hidl_vec;
     using std::vector;
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 6753cb8..1e6e3df 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -29,12 +29,12 @@
 #include <unistd.h>
 
 #include <android/hidl/manager/1.0/IServiceManager.h>
-#include <android/hidl/manager/1.0/BpServiceManager.h>
-#include <android/hidl/manager/1.0/BnServiceManager.h>
+#include <android/hidl/manager/1.0/BpHwServiceManager.h>
+#include <android/hidl/manager/1.0/BnHwServiceManager.h>
 
 using android::hidl::manager::V1_0::IServiceManager;
-using android::hidl::manager::V1_0::BpServiceManager;
-using android::hidl::manager::V1_0::BnServiceManager;
+using android::hidl::manager::V1_0::BpHwServiceManager;
+using android::hidl::manager::V1_0::BnHwServiceManager;
 
 namespace android {
 namespace hardware {
@@ -50,7 +50,7 @@
     {
         AutoMutex _l(gDefaultServiceManagerLock);
         while (gDefaultServiceManager == NULL) {
-            gDefaultServiceManager = fromBinder<IServiceManager, BpServiceManager, BnServiceManager>(
+            gDefaultServiceManager = fromBinder<IServiceManager, BpHwServiceManager, BnHwServiceManager>(
                 ProcessState::self()->getContextObject(NULL));
             if (gDefaultServiceManager == NULL)
                 sleep(1);
diff --git a/transport/base/1.0/Android.bp b/transport/base/1.0/Android.bp
index 0bb6b4a..1b68720 100644
--- a/transport/base/1.0/Android.bp
+++ b/transport/base/1.0/Android.bp
@@ -22,8 +22,8 @@
     out: [
         "android/hidl/base/1.0/IBase.h",
         "android/hidl/base/1.0/IHwBase.h",
-        "android/hidl/base/1.0/BnBase.h",
-        "android/hidl/base/1.0/BpBase.h",
+        "android/hidl/base/1.0/BnHwBase.h",
+        "android/hidl/base/1.0/BpHwBase.h",
         "android/hidl/base/1.0/BsBase.h",
     ],
 }
diff --git a/transport/base/1.0/IBase.hal b/transport/base/1.0/IBase.hal
index d696249..30be023 100644
--- a/transport/base/1.0/IBase.hal
+++ b/transport/base/1.0/IBase.hal
@@ -71,4 +71,10 @@
      * @return success whether the death recipient was unregistered successfully.
      */
     unlinkToDeath(death_recipient recipient) generates (bool success);
+
+    /*
+     * This method trigger the interface to enable/disable instrumentation based
+     * on system property hal.instrumentation.enable.
+     */
+    oneway setHALInstrumentation();
 };
diff --git a/transport/include/hidl/HidlBinderSupport.h b/transport/include/hidl/HidlBinderSupport.h
index cad68be..1ff925c 100644
--- a/transport/include/hidl/HidlBinderSupport.h
+++ b/transport/include/hidl/HidlBinderSupport.h
@@ -26,7 +26,7 @@
 #include <hwbinder/IPCThreadState.h>
 #include <hwbinder/Parcel.h>
 #include <hwbinder/ProcessState.h>
-#include <android/hidl/base/1.0/BnBase.h>
+#include <android/hidl/base/1.0/BnHwBase.h>
 // Defines functions for hidl_string, hidl_version, Status, hidl_vec, MQDescriptor,
 // etc. to interact with Parcel.
 
@@ -338,7 +338,7 @@
 template <typename IType, typename ProxyType, typename StubType>
 sp<IType> fromBinder(const sp<IBinder>& binderIface) {
     using ::android::hidl::base::V1_0::IBase;
-    using ::android::hidl::base::V1_0::BnBase;
+    using ::android::hidl::base::V1_0::BnHwBase;
 
     if (binderIface.get() == nullptr) {
         return nullptr;
@@ -346,7 +346,7 @@
     if (binderIface->localBinder() == nullptr) {
         return new ProxyType(binderIface);
     }
-    sp<IBase> base = static_cast<BnBase*>(binderIface.get())->getImpl();
+    sp<IBase> base = static_cast<BnHwBase*>(binderIface.get())->getImpl();
     if (canCastInterface(base.get(), IType::descriptor)) {
         StubType* stub = static_cast<StubType*>(binderIface.get());
         return stub->getImpl();
diff --git a/transport/manager/1.0/Android.bp b/transport/manager/1.0/Android.bp
index 5cd06d6..b8e1987 100644
--- a/transport/manager/1.0/Android.bp
+++ b/transport/manager/1.0/Android.bp
@@ -25,13 +25,13 @@
     out: [
         "android/hidl/manager/1.0/IServiceManager.h",
         "android/hidl/manager/1.0/IHwServiceManager.h",
-        "android/hidl/manager/1.0/BnServiceManager.h",
-        "android/hidl/manager/1.0/BpServiceManager.h",
+        "android/hidl/manager/1.0/BnHwServiceManager.h",
+        "android/hidl/manager/1.0/BpHwServiceManager.h",
         "android/hidl/manager/1.0/BsServiceManager.h",
         "android/hidl/manager/1.0/IServiceNotification.h",
         "android/hidl/manager/1.0/IHwServiceNotification.h",
-        "android/hidl/manager/1.0/BnServiceNotification.h",
-        "android/hidl/manager/1.0/BpServiceNotification.h",
+        "android/hidl/manager/1.0/BnHwServiceNotification.h",
+        "android/hidl/manager/1.0/BpHwServiceNotification.h",
         "android/hidl/manager/1.0/BsServiceNotification.h",
     ],
 }
diff --git a/transport/memory/1.0/Android.bp b/transport/memory/1.0/Android.bp
index efebe7e..ca8341d 100644
--- a/transport/memory/1.0/Android.bp
+++ b/transport/memory/1.0/Android.bp
@@ -28,18 +28,18 @@
     out: [
         "android/hidl/memory/1.0/IAllocator.h",
         "android/hidl/memory/1.0/IHwAllocator.h",
-        "android/hidl/memory/1.0/BnAllocator.h",
-        "android/hidl/memory/1.0/BpAllocator.h",
+        "android/hidl/memory/1.0/BnHwAllocator.h",
+        "android/hidl/memory/1.0/BpHwAllocator.h",
         "android/hidl/memory/1.0/BsAllocator.h",
         "android/hidl/memory/1.0/IMapper.h",
         "android/hidl/memory/1.0/IHwMapper.h",
-        "android/hidl/memory/1.0/BnMapper.h",
-        "android/hidl/memory/1.0/BpMapper.h",
+        "android/hidl/memory/1.0/BnHwMapper.h",
+        "android/hidl/memory/1.0/BpHwMapper.h",
         "android/hidl/memory/1.0/BsMapper.h",
         "android/hidl/memory/1.0/IMemory.h",
         "android/hidl/memory/1.0/IHwMemory.h",
-        "android/hidl/memory/1.0/BnMemory.h",
-        "android/hidl/memory/1.0/BpMemory.h",
+        "android/hidl/memory/1.0/BnHwMemory.h",
+        "android/hidl/memory/1.0/BpHwMemory.h",
         "android/hidl/memory/1.0/BsMemory.h",
     ],
 }
diff --git a/transport/memory/1.0/default/Android.bp b/transport/memory/1.0/default/Android.bp
index 2b89f07..62fb556 100644
--- a/transport/memory/1.0/default/Android.bp
+++ b/transport/memory/1.0/default/Android.bp
@@ -14,6 +14,7 @@
 
 cc_library_shared {
     name: "android.hidl.memory@1.0-impl",
+    compile_multilib: "both",
     relative_install_path: "hw",
     srcs: [
         "AshmemMapper.cpp",
@@ -55,9 +56,4 @@
         "libutils",
         "libcutils",
     ],
-
-    required: [
-        // only one implementation is allowed
-        "android.hidl.memory@1.0-impl"
-    ]
 }
diff --git a/transport/token/1.0/Android.bp b/transport/token/1.0/Android.bp
index 0ebe1e4..9113e41 100644
--- a/transport/token/1.0/Android.bp
+++ b/transport/token/1.0/Android.bp
@@ -22,8 +22,8 @@
     out: [
         "android/hidl/token/1.0/ITokenManager.h",
         "android/hidl/token/1.0/IHwTokenManager.h",
-        "android/hidl/token/1.0/BnTokenManager.h",
-        "android/hidl/token/1.0/BpTokenManager.h",
+        "android/hidl/token/1.0/BnHwTokenManager.h",
+        "android/hidl/token/1.0/BpHwTokenManager.h",
         "android/hidl/token/1.0/BsTokenManager.h",
     ],
 }