1 /*
2  * Copyright (C) 2020 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 #include "SharedMemory.h"
18 
19 #include <android-base/logging.h>
20 
21 #include <limits>
22 #include <optional>
23 #include <utility>
24 #include <variant>
25 #include <vector>
26 
27 #include "Result.h"
28 #include "TypeUtils.h"
29 #include "Types.h"
30 
31 namespace android::nn {
32 
MutableMemoryBuilder(uint32_t poolIndex)33 MutableMemoryBuilder::MutableMemoryBuilder(uint32_t poolIndex) : mPoolIndex(poolIndex) {}
34 
append(size_t length,size_t alignment,size_t padding)35 DataLocation MutableMemoryBuilder::append(size_t length, size_t alignment, size_t padding) {
36     CHECK_GT(length, 0u);
37     mSize = roundUp(mSize, alignment);
38     const size_t offset = mSize;
39     const size_t paddedLength = roundUp(length, padding);
40     CHECK_LE(offset, std::numeric_limits<uint32_t>::max());
41     CHECK_LE(paddedLength, std::numeric_limits<uint32_t>::max());
42     mSize += paddedLength;
43     return {.poolIndex = mPoolIndex,
44             .offset = static_cast<uint32_t>(offset),
45             .length = static_cast<uint32_t>(length),
46             .padding = static_cast<uint32_t>(paddedLength - length)};
47 }
48 
empty() const49 bool MutableMemoryBuilder::empty() const {
50     return mSize == 0;
51 }
52 
finish()53 GeneralResult<SharedMemory> MutableMemoryBuilder::finish() {
54     return createSharedMemory(mSize);
55 }
56 
ConstantMemoryBuilder(uint32_t poolIndex)57 ConstantMemoryBuilder::ConstantMemoryBuilder(uint32_t poolIndex) : mBuilder(poolIndex) {}
58 
append(const void * data,size_t length)59 DataLocation ConstantMemoryBuilder::append(const void* data, size_t length) {
60     const auto location = mBuilder.append(length);
61     CHECK_EQ(location.length, length);
62     mSlices.push_back({.data = data, .length = length, .offset = location.offset});
63     return location;
64 }
65 
empty() const66 bool ConstantMemoryBuilder::empty() const {
67     return mBuilder.empty();
68 }
69 
finish()70 GeneralResult<SharedMemory> ConstantMemoryBuilder::finish() {
71     // Allocate the memory.
72     auto memory = NN_TRY(mBuilder.finish());
73 
74     // Map the memory.
75     const auto [pointer, size, context] = NN_TRY(map(memory););
76 
77     // Get mutable pointer.
78     uint8_t* mutablePointer = static_cast<uint8_t*>(std::get<void*>(pointer));
79 
80     // Copy data to the memory pool.
81     std::for_each(mSlices.begin(), mSlices.end(), [mutablePointer](const auto& slice) {
82         std::memcpy(mutablePointer + slice.offset, slice.data, slice.length);
83     });
84 
85     return memory;
86 }
87 
88 }  // namespace android::nn
89