1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "AshmemAllocator"
18 #include <android-base/logging.h>
19 
20 #include "AshmemAllocator.h"
21 
22 #include <cutils/ashmem.h>
23 
24 namespace android {
25 namespace hidl {
26 namespace allocator {
27 namespace V1_0 {
28 namespace implementation {
29 
allocateOne(uint64_t size)30 static hidl_memory allocateOne(uint64_t size) {
31     int fd = ashmem_create_region("AshmemAllocator_hidl", size);
32     if (fd < 0) {
33         LOG(WARNING) << "ashmem_create_region(" << size << ") fails with " << fd;
34         return hidl_memory();
35     }
36 
37     native_handle_t* handle = native_handle_create(1, 0);
38     handle->data[0] = fd;
39     LOG(VERBOSE) << "ashmem_create_region(" << size << ") returning hidl_memory(" << handle << ", "
40                  << size << ")";
41     return hidl_memory("ashmem", handle, size);
42 }
43 
cleanup(hidl_memory && memory)44 static void cleanup(hidl_memory&& memory) {
45     if (memory.handle() == nullptr) {
46         return;
47     }
48 
49     native_handle_close(const_cast<native_handle_t *>(memory.handle()));
50     native_handle_delete(const_cast<native_handle_t *>(memory.handle()));
51 }
52 
allocate(uint64_t size,allocate_cb _hidl_cb)53 Return<void> AshmemAllocator::allocate(uint64_t size, allocate_cb _hidl_cb) {
54     hidl_memory memory = allocateOne(size);
55     _hidl_cb(memory.handle() != nullptr /* success */, memory);
56     cleanup(std::move(memory));
57 
58     return Void();
59 }
60 
batchAllocate(uint64_t size,uint64_t count,batchAllocate_cb _hidl_cb)61 Return<void> AshmemAllocator::batchAllocate(uint64_t size, uint64_t count, batchAllocate_cb _hidl_cb) {
62     // resize fails if count > 2^32
63     if (count > UINT32_MAX) {
64         _hidl_cb(false /* success */, {});
65         return Void();
66     }
67 
68     hidl_vec<hidl_memory> batch;
69     batch.resize(count);
70 
71     uint64_t allocated;
72     for (allocated = 0; allocated < count; allocated++) {
73         batch[allocated] = allocateOne(size);
74 
75         if (batch[allocated].handle() == nullptr) {
76             LOG(WARNING) << "batchAllocate(" << size << ", " << count << ") fails @ #" << allocated;
77             break;
78         }
79     }
80 
81     // batch[i].handle() != nullptr for i in [0, allocated - 1].
82     // batch[i].handle() == nullptr for i in [allocated, count - 1].
83 
84     if (allocated < count) {
85         _hidl_cb(false /* success */, {});
86     } else {
87         _hidl_cb(true /* success */, batch);
88     }
89 
90     for (uint64_t i = 0; i < allocated; i++) {
91         cleanup(std::move(batch[i]));
92     }
93 
94     return Void();
95 }
96 
97 }  // namespace implementation
98 }  // namespace V1_0
99 }  // namespace allocator
100 }  // namespace hidl
101 }  // namespace android
102