• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "MemoryUtils"
18 
19 #include "MemoryUtils.h"
20 
21 #include <android-base/logging.h>
22 #include <android/hidl/allocator/1.0/IAllocator.h>
23 
24 using ::android::hardware::hidl_memory;
25 using ::android::hidl::allocator::V1_0::IAllocator;
26 
27 namespace android {
28 namespace nn {
29 
allocateSharedMemory(int64_t size)30 hidl_memory allocateSharedMemory(int64_t size) {
31     static const std::string type = "ashmem";
32     static sp<IAllocator> allocator = IAllocator::getService(type);
33 
34     hidl_memory memory;
35 
36     // TODO: should we align memory size to nearest page? doesn't seem necessary...
37     allocator->allocate(size, [&](bool success, const hidl_memory& mem) {
38         if (!success) {
39             LOG(ERROR) << "unable to allocate " << size << " bytes of " << type;
40         } else {
41             memory = mem;
42         }
43     });
44 
45     return memory;
46 }
47 
48 }  // namespace nn
49 }  // namespace android
50