Extract android::nn::allocateSharedMemory into a separate header file

While untangling our VTS code, I realized that some VTS files depend on Utils.h
only for the allocateSharedMemory function. Utils.h is problematic
because it includes HalInterfaces.h, which, in turn, creates a
dependency on *all* NNAPI HAL versions. To circumvent that, I have
extracted allocateSharedMemory into a separate header file with minimal
dependencies.

Bug: 74827824
Test: mma
Change-Id: I279d65a2ea2ca64228ed5bedeaf28059dca451c5
Merged-In: I279d65a2ea2ca64228ed5bedeaf28059dca451c5
(cherry picked from commit b16066e6a9789d5cee4b8d83ebd33fb742dcf188)
diff --git a/common/Android.bp b/common/Android.bp
index 95d12c6..e16a2a5 100644
--- a/common/Android.bp
+++ b/common/Android.bp
@@ -68,6 +68,7 @@
     export_include_dirs: ["include"],
     srcs: [
         "Utils.cpp",
+        "MemoryUtils.cpp",
         "ExecutionBurstController.cpp",
         "ExecutionBurstServer.cpp",
     ],
@@ -122,6 +123,7 @@
         "ExecutionBurstServer.cpp",
         "GraphDump.cpp",
         "IndexedShapeWrapper.cpp",
+        "MemoryUtils.cpp",
         "OperationsUtils.cpp",
         "TokenHasher.cpp",
         "Utils.cpp",
diff --git a/common/MemoryUtils.cpp b/common/MemoryUtils.cpp
new file mode 100644
index 0000000..3a95b0d
--- /dev/null
+++ b/common/MemoryUtils.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#define LOG_TAG "MemoryUtils"
+
+#include "MemoryUtils.h"
+
+#include <android-base/logging.h>
+#include <android/hidl/allocator/1.0/IAllocator.h>
+
+using ::android::hardware::hidl_memory;
+using ::android::hidl::allocator::V1_0::IAllocator;
+
+namespace android {
+namespace nn {
+
+hidl_memory allocateSharedMemory(int64_t size) {
+    static const std::string type = "ashmem";
+    static sp<IAllocator> allocator = IAllocator::getService(type);
+
+    hidl_memory memory;
+
+    // TODO: should we align memory size to nearest page? doesn't seem necessary...
+    allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
+        if (!success) {
+            LOG(ERROR) << "unable to allocate " << size << " bytes of " << type;
+        } else {
+            memory = mem;
+        }
+    });
+
+    return memory;
+}
+
+}  // namespace nn
+}  // namespace android
diff --git a/common/Utils.cpp b/common/Utils.cpp
index 731127a..18eedd7 100644
--- a/common/Utils.cpp
+++ b/common/Utils.cpp
@@ -30,8 +30,6 @@
 #include <algorithm>
 #include <unordered_map>
 
-using ::android::hidl::allocator::V1_0::IAllocator;
-
 namespace android {
 namespace nn {
 
@@ -310,24 +308,6 @@
                                           operand.dimensions.size());
 }
 
-hidl_memory allocateSharedMemory(int64_t size) {
-    static const std::string type = "ashmem";
-    static sp<IAllocator> allocator = IAllocator::getService(type);
-
-    hidl_memory memory;
-
-    // TODO: should we align memory size to nearest page? doesn't seem necessary...
-    allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
-        if (!success) {
-            LOG(ERROR) << "unable to allocate " << size << " bytes of " << type;
-        } else {
-            memory = mem;
-        }
-    });
-
-    return memory;
-}
-
 uint32_t alignBytesNeeded(uint32_t index, size_t length) {
     uint32_t pattern;
     if (length < 2) {
diff --git a/common/include/MemoryUtils.h b/common/include/MemoryUtils.h
new file mode 100644
index 0000000..bc27262
--- /dev/null
+++ b/common/include/MemoryUtils.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+#ifndef ANDROID_ML_NN_COMMON_MEMORY_UTILS_H
+#define ANDROID_ML_NN_COMMON_MEMORY_UTILS_H
+
+#include <hidlmemory/mapping.h>
+
+namespace android {
+namespace nn {
+
+// Memory is unmapped.
+// Memory is reference counted by hidl_memory instances, and is deallocated
+// once there are no more references.
+hardware::hidl_memory allocateSharedMemory(int64_t size);
+
+}  // namespace nn
+}  // namespace android
+
+#endif  // ANDROID_ML_NN_COMMON_MEMORY_UTILS_H
diff --git a/common/include/Utils.h b/common/include/Utils.h
index f09e690..49bf23d 100644
--- a/common/include/Utils.h
+++ b/common/include/Utils.h
@@ -220,11 +220,6 @@
 bool tensorHasUnspecifiedDimensions(const Operand& operand);
 bool tensorHasUnspecifiedDimensions(const ANeuralNetworksOperandType* type);
 
-// Memory is unmapped.
-// Memory is reference counted by hidl_memory instances, and is deallocated
-// once there are no more references.
-hidl_memory allocateSharedMemory(int64_t size);
-
 // Returns the number of padding bytes needed to align data of the
 // specified length.  It aligns object of length:
 // 2, 3 on a 2 byte boundary,
diff --git a/runtime/Memory.cpp b/runtime/Memory.cpp
index 097b140..2748850 100644
--- a/runtime/Memory.cpp
+++ b/runtime/Memory.cpp
@@ -20,6 +20,7 @@
 
 #include "ExecutionBurstController.h"
 #include "HalInterfaces.h"
+#include "MemoryUtils.h"
 #include "Utils.h"
 
 namespace android {