Add batching to hidl allocator HAL.
am: eca68d224d

Change-Id: I1a8c19ffa902f04a4ae8dc7ab0271bd98c3e0fe0
diff --git a/transport/allocator/1.0/IAllocator.hal b/transport/allocator/1.0/IAllocator.hal
index 814c69d..6f531dd 100644
--- a/transport/allocator/1.0/IAllocator.hal
+++ b/transport/allocator/1.0/IAllocator.hal
@@ -29,4 +29,14 @@
      * @return memory Unmapped memory object.
      */
     allocate(uint64_t size) generates (bool success, memory mem);
+
+    /**
+     * Return memory must have instance name corresponding to this type of memory.
+     *
+     * @param size Size of memory to allocate in bytes.
+     * @param count Number of memory instances to allocate.
+     * @return success Whether allocation succeeded (returns false if any allocation failed).
+     * @return batch Unmapped memory objects.
+     */
+    batchAllocate(uint64_t size, uint64_t count) generates (bool success, vec<memory> batch);
 };
\ No newline at end of file
diff --git a/transport/allocator/1.0/default/AshmemAllocator.cpp b/transport/allocator/1.0/default/AshmemAllocator.cpp
index ce6dbf7..0bd4f81 100644
--- a/transport/allocator/1.0/default/AshmemAllocator.cpp
+++ b/transport/allocator/1.0/default/AshmemAllocator.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "AshmemAllocator"
+#include <android-base/logging.h>
+
 #include "AshmemAllocator.h"
 
 #include <cutils/ashmem.h>
@@ -24,21 +27,70 @@
 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) {
+static hidl_memory allocateOne(uint64_t size) {
     int fd = ashmem_create_region("AshmemAllocator_hidl", size);
     if (fd < 0) {
-        _hidl_cb(false /* success */, hidl_memory());
-        return Void();
+        LOG(WARNING) << "ashmem_create_region(" << size << ") fails with " << fd;
+        return hidl_memory();
     }
 
     native_handle_t* handle = native_handle_create(1, 0);
     handle->data[0] = fd;
-    hidl_memory memory("ashmem", handle, size);
+    LOG(WARNING) << "ashmem_create_region(" << size << ") returning hidl_memory(" << handle
+            << ", " << size << ")";
+    return hidl_memory("ashmem", handle, size);
+}
 
-    _hidl_cb(true /* success */, memory);
-    native_handle_close(handle);
-    native_handle_delete(handle);
+static void cleanup(hidl_memory&& memory) {
+    if (memory.handle() == nullptr) {
+        return;
+    }
+
+    native_handle_close(const_cast<native_handle_t *>(memory.handle()));
+    native_handle_delete(const_cast<native_handle_t *>(memory.handle()));
+}
+
+Return<void> AshmemAllocator::allocate(uint64_t size, allocate_cb _hidl_cb) {
+    hidl_memory memory = allocateOne(size);
+    _hidl_cb(memory.handle() != nullptr /* success */, memory);
+    cleanup(std::move(memory));
+
+    return Void();
+}
+
+Return<void> AshmemAllocator::batchAllocate(uint64_t size, uint64_t count, batchAllocate_cb _hidl_cb) {
+    // resize fails if count > 2^32
+    if (count > UINT32_MAX) {
+        _hidl_cb(false /* success */, {});
+        return Void();
+    }
+
+    hidl_vec<hidl_memory> batch;
+    batch.resize(count);
+
+    uint64_t allocated;
+    for (allocated = 0; allocated < count; allocated++) {
+        batch[allocated] = allocateOne(size);
+
+        if (batch[allocated].handle() == nullptr) {
+            LOG(WARNING) << "batchAllocate(" << size << ", " << count << ") fails @ #" << allocated;
+            break;
+        }
+    }
+
+    // batch[i].handle() != nullptr for i in [0, allocated - 1].
+    // batch[i].handle() == nullptr for i in [allocated, count - 1].
+
+    if (allocated < count) {
+        _hidl_cb(false /* success */, {});
+    } else {
+        _hidl_cb(true /* success */, batch);
+    }
+
+    for (uint64_t i = 0; i < allocated; i++) {
+        cleanup(std::move(batch[i]));
+    }
+
     return Void();
 }
 
diff --git a/transport/allocator/1.0/default/AshmemAllocator.h b/transport/allocator/1.0/default/AshmemAllocator.h
index 307cb5a..131417d 100644
--- a/transport/allocator/1.0/default/AshmemAllocator.h
+++ b/transport/allocator/1.0/default/AshmemAllocator.h
@@ -39,7 +39,7 @@
 struct AshmemAllocator : public IAllocator {
     // Methods from ::android::hidl::allocator::V1_0::IAllocator follow.
     Return<void> allocate(uint64_t size, allocate_cb _hidl_cb) override;
-
+    Return<void> batchAllocate(uint64_t size, uint64_t count, batchAllocate_cb _hidl_cb) override;
 };
 
 }  // namespace implementation
diff --git a/transport/current.txt b/transport/current.txt
index b6d8949..a6ecd81 100644
--- a/transport/current.txt
+++ b/transport/current.txt
@@ -3,7 +3,7 @@
 
 # HALs released in Android O
 
-43a52a3777b4d6ec1bb0f5c371bbcecc51898e76695834e0971fef424659f66a android.hidl.allocator@1.0::IAllocator
+fc6cbbc8a22edabd4b58f8949e591359d3138d16a82506052e25ac43e1dbda68 android.hidl.allocator@1.0::IAllocator
 bddab6184d7a346da6a07dc0828cf19a696f4caa3611c51f2e14565a14b40fd9 android.hidl.base@1.0::IBase
 500ec34f1b0826a93c4abe45b23c4d85565d8041acaf3cf9fb23c09702967567 android.hidl.base@1.0::types
 4d046a598e85f1c2d383c3a9096c3c0578e97458072ee7e67f704e99d5fb0d3f android.hidl.manager@1.0::IServiceManager