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/platform/assert.h"
20 #include "chre/util/system/debug_dump.h"
21 
22 namespace chre {
23 
nanoappAlloc(Nanoapp * app,uint32_t bytes)24 void *MemoryManager::nanoappAlloc(Nanoapp *app, uint32_t bytes) {
25   HeapBlockHeader *header = nullptr;
26   if (bytes > 0) {
27     if (mAllocationCount >= kMaxAllocationCount) {
28       LOGE("Failed to allocate memory from Nanoapp ID %" PRIu16
29            ": allocation count exceeded limit.",
30            app->getInstanceId());
31     } else if ((bytes > kMaxAllocationBytes) ||
32                ((mTotalAllocatedBytes + bytes) > kMaxAllocationBytes)) {
33       LOGE("Failed to allocate memory from Nanoapp ID %" PRIu16
34            ": not enough space.",
35            app->getInstanceId());
36     } else {
37       header = static_cast<HeapBlockHeader *>(
38           doAlloc(app, sizeof(HeapBlockHeader) + bytes));
39 
40       if (header != nullptr) {
41         app->setTotalAllocatedBytes(app->getTotalAllocatedBytes() + bytes);
42         mTotalAllocatedBytes += bytes;
43         if (mTotalAllocatedBytes > mPeakAllocatedBytes) {
44           mPeakAllocatedBytes = mTotalAllocatedBytes;
45         }
46         mAllocationCount++;
47         app->linkHeapBlock(header);
48         header->data.bytes = bytes;
49         header->data.instanceId = app->getInstanceId();
50         header++;
51       }
52     }
53   }
54   return header;
55 }
56 
nanoappFree(Nanoapp * app,void * ptr)57 void MemoryManager::nanoappFree(Nanoapp *app, void *ptr) {
58   if (ptr != nullptr) {
59     HeapBlockHeader *header = static_cast<HeapBlockHeader *>(ptr);
60     header--;
61 
62     // TODO: Clean up API contract of chreSendEvent to specify nanoapps can't
63     // release ownership of data to other nanoapps so a CHRE_ASSERT_LOG can be
64     // used below and the code can return.
65     if (app->getInstanceId() != header->data.instanceId) {
66       LOGW("Nanoapp ID=%" PRIu16 " tried to free data from nanoapp ID=%" PRIu16,
67            app->getInstanceId(), header->data.instanceId);
68     }
69 
70     size_t nanoAppTotalAllocatedBytes = app->getTotalAllocatedBytes();
71     if (nanoAppTotalAllocatedBytes >= header->data.bytes) {
72       app->setTotalAllocatedBytes(nanoAppTotalAllocatedBytes -
73                                   header->data.bytes);
74     } else {
75       app->setTotalAllocatedBytes(0);
76     }
77 
78     if (mTotalAllocatedBytes >= header->data.bytes) {
79       mTotalAllocatedBytes -= header->data.bytes;
80     } else {
81       mTotalAllocatedBytes = 0;
82     }
83     if (mAllocationCount > 0) {
84       mAllocationCount--;
85     }
86 
87     app->unlinkHeapBlock(header);
88     doFree(app, header);
89   }
90 }
91 
nanoappFreeAll(Nanoapp * app)92 uint32_t MemoryManager::nanoappFreeAll(Nanoapp *app) {
93   HeapBlockHeader *current = app->getFirstHeapBlock();
94 
95   // totalNumBlocks is used a safeguard to avoid entering an infinite loop if
96   // some headers got corrupted. It represents the number of blocks currently
97   // allocated for all the nanoapps and is used as an upper bound for the number
98   // of blocks allocated by the current nanoapp.
99   size_t totalNumBlocks = mAllocationCount;
100   uint32_t numFreedBlocks = 0;
101 
102   while (current != nullptr && totalNumBlocks > 0) {
103     HeapBlockHeader *next = current->data.next;
104     // nanoappFree expects a pointer past the header.
105     HeapBlockHeader *pointerAfterHeader = current + 1;
106     nanoappFree(app, pointerAfterHeader);
107     numFreedBlocks++;
108     current = next;
109     totalNumBlocks--;
110   }
111 
112   return numFreedBlocks;
113 }
114 
logStateToBuffer(DebugDumpWrapper & debugDump) const115 void MemoryManager::logStateToBuffer(DebugDumpWrapper &debugDump) const {
116   debugDump.print(
117       "\nNanoapp heap usage: %zu bytes allocated, %zu peak bytes"
118       " allocated, count %zu\n",
119       getTotalAllocatedBytes(), getPeakAllocatedBytes(), getAllocationCount());
120 }
121 
122 }  // namespace chre
123