1 /*
2 * Copyright (C) 2017 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 "chre/platform/memory_manager.h"
18
19 #include "chre/util/system/debug_dump.h"
20
21 namespace chre {
22
nanoappAlloc(Nanoapp * app,uint32_t bytes)23 void *MemoryManager::nanoappAlloc(Nanoapp *app, uint32_t bytes) {
24 AllocHeader *header = nullptr;
25 if (bytes > 0) {
26 if (mAllocationCount >= kMaxAllocationCount) {
27 LOGE("Failed to allocate memory from Nanoapp ID %" PRIu32
28 ": allocation count exceeded limit.",
29 app->getInstanceId());
30 } else if ((mTotalAllocatedBytes + bytes) > kMaxAllocationBytes) {
31 LOGE("Failed to allocate memory from Nanoapp ID %" PRIu32
32 ": not enough space.",
33 app->getInstanceId());
34 } else {
35 header =
36 static_cast<AllocHeader *>(doAlloc(app, sizeof(AllocHeader) + bytes));
37
38 if (header != nullptr) {
39 app->setTotalAllocatedBytes(app->getTotalAllocatedBytes() + bytes);
40 mTotalAllocatedBytes += bytes;
41 if (mTotalAllocatedBytes > mPeakAllocatedBytes) {
42 mPeakAllocatedBytes = mTotalAllocatedBytes;
43 }
44 mAllocationCount++;
45 header->data.bytes = bytes;
46 header->data.instanceId = app->getInstanceId();
47 header++;
48 }
49 }
50 }
51 return header;
52 }
53
nanoappFree(Nanoapp * app,void * ptr)54 void MemoryManager::nanoappFree(Nanoapp *app, void *ptr) {
55 if (ptr != nullptr) {
56 AllocHeader *header = static_cast<AllocHeader *>(ptr);
57 header--;
58
59 // TODO: Clean up API contract of chreSendEvent to specify nanoapps can't
60 // release ownership of data to other nanoapps so a CHRE_ASSERT_LOG can be
61 // used below and the code can return.
62 if (app->getInstanceId() != header->data.instanceId) {
63 LOGW("Nanoapp ID=%" PRIu32 " tried to free data from nanoapp ID=%" PRIu32,
64 app->getInstanceId(), header->data.instanceId);
65 }
66
67 size_t nanoAppTotalAllocatedBytes = app->getTotalAllocatedBytes();
68 if (nanoAppTotalAllocatedBytes >= header->data.bytes) {
69 app->setTotalAllocatedBytes(nanoAppTotalAllocatedBytes -
70 header->data.bytes);
71 } else {
72 app->setTotalAllocatedBytes(0);
73 }
74
75 if (mTotalAllocatedBytes >= header->data.bytes) {
76 mTotalAllocatedBytes -= header->data.bytes;
77 } else {
78 mTotalAllocatedBytes = 0;
79 }
80 if (mAllocationCount > 0) {
81 mAllocationCount--;
82 }
83
84 doFree(app, header);
85 }
86 }
87
logStateToBuffer(DebugDumpWrapper & debugDump) const88 void MemoryManager::logStateToBuffer(DebugDumpWrapper &debugDump) const {
89 debugDump.print(
90 "\nNanoapp heap usage: %zu bytes allocated, %zu peak bytes"
91 " allocated, count %zu\n",
92 getTotalAllocatedBytes(), getPeakAllocatedBytes(), getAllocationCount());
93 }
94
95 } // namespace chre
96