• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.", app->getInstanceId());
29     } else if ((mTotalAllocatedBytes + bytes) > kMaxAllocationBytes) {
30       LOGE("Failed to allocate memory from Nanoapp ID %" PRIu32
31            ": not enough space.", app->getInstanceId());
32     } else {
33       header = static_cast<AllocHeader*>(
34           doAlloc(app, sizeof(AllocHeader) + bytes));
35 
36       if (header != nullptr) {
37         app->setTotalAllocatedBytes(app->getTotalAllocatedBytes() + bytes);
38         mTotalAllocatedBytes += bytes;
39         if (mTotalAllocatedBytes > mPeakAllocatedBytes) {
40           mPeakAllocatedBytes = mTotalAllocatedBytes;
41         }
42         mAllocationCount++;
43         header->data.bytes = bytes;
44         header->data.instanceId = app->getInstanceId();
45         header++;
46       }
47     }
48   }
49   return header;
50 }
51 
nanoappFree(Nanoapp * app,void * ptr)52 void MemoryManager::nanoappFree(Nanoapp *app, void *ptr) {
53   if (ptr != nullptr) {
54     AllocHeader *header = static_cast<AllocHeader*>(ptr);
55     header--;
56 
57     // TODO: Clean up API contract of chreSendEvent to specify nanoapps can't
58     // release ownership of data to other nanoapps so a CHRE_ASSERT_LOG can be
59     // used below and the code can return.
60     if (app->getInstanceId() != header->data.instanceId) {
61       LOGW("Nanoapp ID=%" PRIu32 " tried to free data from nanoapp ID=%" PRIu32,
62           app->getInstanceId(), header->data.instanceId);
63     }
64 
65     size_t nanoAppTotalAllocatedBytes = app->getTotalAllocatedBytes();
66     if (nanoAppTotalAllocatedBytes >= header->data.bytes) {
67       app->setTotalAllocatedBytes(
68           nanoAppTotalAllocatedBytes - header->data.bytes);
69     } else {
70       app->setTotalAllocatedBytes(0);
71     }
72 
73     if (mTotalAllocatedBytes >= header->data.bytes) {
74       mTotalAllocatedBytes -= header->data.bytes;
75     } else {
76       mTotalAllocatedBytes = 0;
77     }
78     if (mAllocationCount > 0) {
79       mAllocationCount--;
80     }
81 
82     doFree(app, header);
83   }
84 }
85 
logStateToBuffer(char * buffer,size_t * bufferPos,size_t bufferSize) const86 void MemoryManager::logStateToBuffer(char *buffer, size_t *bufferPos,
87                                      size_t bufferSize) const {
88   debugDumpPrint(buffer, bufferPos, bufferSize,
89                  "\nNanoapp heap usage: %zu bytes allocated, %zu peak bytes"
90                  " allocated, count %zu\n", getTotalAllocatedBytes(),
91                  getPeakAllocatedBytes(), getAllocationCount());
92 }
93 
94 }  // namespace chre
95