split android.hidl.memory

Split android.hidl.memory into android.hidl.memory and
android.hidl.allocator. This must be done because IAllocator is served
over hwbinder transport and IMapper/IMemory are always going to be
passthrough hals.

Test: hidl_test, device boots with allocator
Bug: 35327976

Merged-In: Ib135b5e101fadadbbf16126a98f90181f76a253b
Change-Id: Ib135b5e101fadadbbf16126a98f90181f76a253b
diff --git a/transport/allocator/1.0/Android.bp b/transport/allocator/1.0/Android.bp
new file mode 100644
index 0000000..b610db4
--- /dev/null
+++ b/transport/allocator/1.0/Android.bp
@@ -0,0 +1,59 @@
+// This file is autogenerated by hidl-gen. Do not edit manually.
+
+filegroup {
+    name: "android.hidl.allocator@1.0_hal",
+    srcs: [
+        "IAllocator.hal",
+    ],
+}
+
+genrule {
+    name: "android.hidl.allocator@1.0_genc++",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl/transport android.hidl.allocator@1.0",
+    srcs: [
+        ":android.hidl.allocator@1.0_hal",
+    ],
+    out: [
+        "android/hidl/allocator/1.0/AllocatorAll.cpp",
+    ],
+}
+
+genrule {
+    name: "android.hidl.allocator@1.0_genc++_headers",
+    tools: ["hidl-gen"],
+    cmd: "$(location hidl-gen) -o $(genDir) -Lc++ -randroid.hidl:system/libhidl/transport android.hidl.allocator@1.0",
+    srcs: [
+        ":android.hidl.allocator@1.0_hal",
+    ],
+    out: [
+        "android/hidl/allocator/1.0/IAllocator.h",
+        "android/hidl/allocator/1.0/IHwAllocator.h",
+        "android/hidl/allocator/1.0/BnHwAllocator.h",
+        "android/hidl/allocator/1.0/BpHwAllocator.h",
+        "android/hidl/allocator/1.0/BsAllocator.h",
+    ],
+}
+
+cc_library_shared {
+    name: "android.hidl.allocator@1.0",
+    generated_sources: ["android.hidl.allocator@1.0_genc++"],
+    generated_headers: ["android.hidl.allocator@1.0_genc++_headers"],
+    export_generated_headers: ["android.hidl.allocator@1.0_genc++_headers"],
+    shared_libs: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "liblog",
+        "libutils",
+        "libcutils",
+        "android.hidl.base@1.0",
+    ],
+    export_shared_lib_headers: [
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "libutils",
+        "android.hidl.base@1.0",
+    ],
+}
diff --git a/transport/allocator/1.0/IAllocator.hal b/transport/allocator/1.0/IAllocator.hal
new file mode 100644
index 0000000..814c69d
--- /dev/null
+++ b/transport/allocator/1.0/IAllocator.hal
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+package android.hidl.allocator@1.0;
+
+/**
+ * Interface which allocates the required memory.
+ */
+interface IAllocator {
+
+    /**
+     * Return memory must have instance name corresponding to this type of memory.
+     *
+     * @param size Size of memory to allocate in bytes.
+     * @return success Whether allocation succeeded.
+     * @return memory Unmapped memory object.
+     */
+    allocate(uint64_t size) generates (bool success, memory mem);
+};
\ No newline at end of file
diff --git a/transport/allocator/1.0/default/Android.bp b/transport/allocator/1.0/default/Android.bp
new file mode 100644
index 0000000..914fc8d
--- /dev/null
+++ b/transport/allocator/1.0/default/Android.bp
@@ -0,0 +1,35 @@
+// Copyright (C) 2016 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_binary {
+    name: "android.hidl.allocator@1.0-service",
+    relative_install_path: "hw",
+    srcs: [
+        "AshmemAllocator.cpp",
+        "service.cpp"
+    ],
+    init_rc: ["android.hidl.allocator@1.0-service.rc"],
+
+    shared_libs: [
+        "android.hidl.base@1.0",
+        "android.hidl.allocator@1.0",
+        "libhidlbase",
+        "libhidltransport",
+        "libhwbinder",
+        "libbase",
+        "liblog",
+        "libutils",
+        "libcutils",
+    ],
+}
diff --git a/transport/allocator/1.0/default/AshmemAllocator.cpp b/transport/allocator/1.0/default/AshmemAllocator.cpp
new file mode 100644
index 0000000..ce6dbf7
--- /dev/null
+++ b/transport/allocator/1.0/default/AshmemAllocator.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2016 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 "AshmemAllocator.h"
+
+#include <cutils/ashmem.h>
+
+namespace android {
+namespace hidl {
+namespace allocator {
+namespace V1_0 {
+namespace implementation {
+
+// Methods from ::android::hidl::allocator::V1_0::IAllocator follow.
+Return<void> AshmemAllocator::allocate(uint64_t size, allocate_cb _hidl_cb) {
+    int fd = ashmem_create_region("AshmemAllocator_hidl", size);
+    if (fd < 0) {
+        _hidl_cb(false /* success */, hidl_memory());
+        return Void();
+    }
+
+    native_handle_t* handle = native_handle_create(1, 0);
+    handle->data[0] = fd;
+    hidl_memory memory("ashmem", handle, size);
+
+    _hidl_cb(true /* success */, memory);
+    native_handle_close(handle);
+    native_handle_delete(handle);
+    return Void();
+}
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace allocator
+}  // namespace hidl
+}  // namespace android
diff --git a/transport/allocator/1.0/default/AshmemAllocator.h b/transport/allocator/1.0/default/AshmemAllocator.h
new file mode 100644
index 0000000..307cb5a
--- /dev/null
+++ b/transport/allocator/1.0/default/AshmemAllocator.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 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_HIDL_ASHMEM_ALLOCATOR_V1_0_ALLOCATOR_H
+#define ANDROID_HIDL_ASHMEM_ALLOCATOR_V1_0_ALLOCATOR_H
+
+#include <android/hidl/allocator/1.0/IAllocator.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace android {
+namespace hidl {
+namespace allocator {
+namespace V1_0 {
+namespace implementation {
+
+using ::android::hidl::allocator::V1_0::IAllocator;
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct AshmemAllocator : public IAllocator {
+    // Methods from ::android::hidl::allocator::V1_0::IAllocator follow.
+    Return<void> allocate(uint64_t size, allocate_cb _hidl_cb) override;
+
+};
+
+}  // namespace implementation
+}  // namespace V1_0
+}  // namespace allocator
+}  // namespace hidl
+}  // namespace android
+
+#endif  // ANDROID_HIDL_ASHMEM_ALLOCATOR_V1_0_ALLOCATOR_H
diff --git a/transport/allocator/1.0/default/android.hidl.allocator@1.0-service.rc b/transport/allocator/1.0/default/android.hidl.allocator@1.0-service.rc
new file mode 100644
index 0000000..4f05523
--- /dev/null
+++ b/transport/allocator/1.0/default/android.hidl.allocator@1.0-service.rc
@@ -0,0 +1,4 @@
+service hidl_memory /system/bin/hw/android.hidl.allocator@1.0-service
+    class hal
+    user system
+    group system
diff --git a/transport/allocator/1.0/default/service.cpp b/transport/allocator/1.0/default/service.cpp
new file mode 100644
index 0000000..63babd3
--- /dev/null
+++ b/transport/allocator/1.0/default/service.cpp
@@ -0,0 +1,30 @@
+#define LOG_TAG "android.hidl.allocator@1.0-service"
+
+#include "AshmemAllocator.h"
+
+#include <android-base/logging.h>
+#include <android/hidl/allocator/1.0/IAllocator.h>
+#include <hidl/HidlTransportSupport.h>
+
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+using android::hidl::allocator::V1_0::IAllocator;
+using android::hidl::allocator::V1_0::implementation::AshmemAllocator;
+using android::sp;
+using android::status_t;
+
+int main() {
+    configureRpcThreadpool(1, true /* callerWillJoin */);
+
+    sp<IAllocator> allocator = new AshmemAllocator();
+
+    status_t status = allocator->registerAsService("ashmem");
+
+    if (android::OK != status) {
+        LOG(FATAL) << "Unable to register allocator service: " << status;
+    }
+
+    joinRpcThreadpool();
+
+    return -1;
+}