libhidlallocatorutils: IMemoryHeap -> hidl_memory
am: cb9e4900dc

Change-Id: Ic8436fba98b7e395201f2dda6150b1f699de0b8f
diff --git a/transport/Android.bp b/transport/Android.bp
index 23f6904..e09cb4d 100644
--- a/transport/Android.bp
+++ b/transport/Android.bp
@@ -15,6 +15,7 @@
 subdirs = [
     "allocator/1.0",
     "allocator/1.0/default",
+    "allocator/1.0/utils",
     "base/1.0",
     "manager/1.0",
     "manager/1.1",
diff --git a/transport/allocator/1.0/utils/Android.bp b/transport/allocator/1.0/utils/Android.bp
new file mode 100644
index 0000000..b1a3f41
--- /dev/null
+++ b/transport/allocator/1.0/utils/Android.bp
@@ -0,0 +1,39 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+cc_library {
+    name: "libhidlallocatorutils",
+    vendor_available: true,
+    vndk: {
+        enabled: true,
+    },
+    defaults: ["libhidl-defaults"],
+    shared_libs: [
+        "libbinder",
+        "libcutils",
+        "libhidlbase",
+        "libhidltransport",
+        "android.hidl.memory@1.0"
+    ],
+    export_include_dirs: ["include"],
+
+    export_shared_lib_headers: [
+        "libbinder",
+        "libhidlbase"
+    ],
+
+    srcs: [
+        "FrameworkUtils.cpp"
+    ],
+}
diff --git a/transport/allocator/1.0/utils/FrameworkUtils.cpp b/transport/allocator/1.0/utils/FrameworkUtils.cpp
new file mode 100644
index 0000000..1fc6f3f
--- /dev/null
+++ b/transport/allocator/1.0/utils/FrameworkUtils.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hidlmemory/FrameworkUtils.h>
+
+namespace android {
+
+namespace hardware {
+
+sp<HidlMemory> fromHeap(const sp<IMemoryHeap>& heap) {
+    int fd = dup(heap->getHeapID());
+
+    if (fd < 0) {
+        return HidlMemory::getInstance(hidl_memory());
+    }
+
+    // Only being used because this library is paired with the IAllocator
+    // ashmem. Other places should not make assumptions about the contents
+    // of this memory.
+    return HidlMemory::getInstance("ashmem", fd, heap->getSize());
+}
+
+}  // namespace hardware
+}  // namespace android
diff --git a/transport/allocator/1.0/utils/include/hidlmemory/FrameworkUtils.h b/transport/allocator/1.0/utils/include/hidlmemory/FrameworkUtils.h
new file mode 100644
index 0000000..0a62575
--- /dev/null
+++ b/transport/allocator/1.0/utils/include/hidlmemory/FrameworkUtils.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <binder/IMemory.h>
+#include <hidl/HidlSupport.h>
+
+namespace android {
+
+namespace hardware {
+
+/**
+ * Returns a new IMemory instance corresponding to a framework IMemoryHeap object.
+ * This encapsulates the idea that IMemoryHeap and the ashmem instance of hidl
+ * IMemory are backed by the same object.
+ *
+ * Return is never nullptr. May be an invalid hidl_memory object.
+ */
+sp<HidlMemory> fromHeap(const sp<IMemoryHeap>& heap);
+
+}  // namespace hardware
+}  // namespace android