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 #ifndef CHRE_PLATFORM_MEMORY_MANAGER_H_
18 #define CHRE_PLATFORM_MEMORY_MANAGER_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include "chre/core/nanoapp.h"
24 #include "chre/util/non_copyable.h"
25 #include "chre/util/system/debug_dump.h"
26 #include "heap_block_header.h"
27 
28 // This default value can be overridden in the variant-specific makefile.
29 #ifndef CHRE_MAX_ALLOCATION_BYTES
30 #define CHRE_MAX_ALLOCATION_BYTES 262144  // 256 * 1024
31 #endif
32 
33 namespace chre {
34 
35 /**
36  * The MemoryManager keeps track of heap memory allocated/deallocated by all
37  * nanoapps.
38  */
39 class MemoryManager : public NonCopyable {
40  public:
41   /**
42    * Allocate heap memory in CHRE.
43    *
44    * @param app The pointer to the nanoapp requesting memory.
45    * @param bytes The size in bytes to allocate.
46    * @return the allocated memory pointer. nullptr if the allocation fails.
47    */
48   void *nanoappAlloc(Nanoapp *app, uint32_t bytes);
49 
50   /**
51    * Free heap memory in CHRE.
52    *
53    * @param app The pointer to the nanoapp requesting memory free.
54    * @param ptr The pointer to the memory to deallocate.
55    */
56   void nanoappFree(Nanoapp *app, void *ptr);
57 
58   /**
59    * Free all allocated heap blocks for the nanoapp.
60    *
61    * @param app The pointer to the nanoapp.
62    * @return The number of heap blocks freed.
63    */
64   uint32_t nanoappFreeAll(Nanoapp *app);
65 
66   /**
67    * @return current total allocated memory in bytes.
68    */
getTotalAllocatedBytes()69   size_t getTotalAllocatedBytes() const {
70     return mTotalAllocatedBytes;
71   }
72 
73   /**
74    * @return peak total allocated memory in bytes.
75    */
getPeakAllocatedBytes()76   size_t getPeakAllocatedBytes() const {
77     return mPeakAllocatedBytes;
78   }
79 
80   /**
81    * @return current count of allocated memory spaces.
82    */
getAllocationCount()83   size_t getAllocationCount() const {
84     return mAllocationCount;
85   }
86 
87   /**
88    * @return max total allocatable memory in bytes.
89    */
getMaxAllocationBytes()90   size_t getMaxAllocationBytes() const {
91     return kMaxAllocationBytes;
92   }
93 
94   /**
95    * @return max allocatable memory counts.
96    */
getMaxAllocationCount()97   size_t getMaxAllocationCount() const {
98     return kMaxAllocationCount;
99   }
100 
101   /**
102    * Prints state in a string buffer. Must only be called from the context of
103    * the main CHRE thread.
104    *
105    * @param debugDump The debug dump wrapper where a string can be printed into
106    *    one of the buffers.
107    */
108   void logStateToBuffer(DebugDumpWrapper &debugDump) const;
109 
110  private:
111   //! The total allocated memory in bytes (not including header).
112   size_t mTotalAllocatedBytes = 0;
113 
114   //! The peak allocated memory in bytes (not including header).
115   size_t mPeakAllocatedBytes = 0;
116 
117   //! Stores total number of allocated memory spaces.
118   size_t mAllocationCount = 0;
119 
120   //! The maximum allowable total allocated memory in bytes for all nanoapps.
121   static constexpr size_t kMaxAllocationBytes = CHRE_MAX_ALLOCATION_BYTES;
122 
123   //! The maximum allowable count of memory allocations for all nanoapps.
124   static constexpr size_t kMaxAllocationCount = (8 * 1024);
125 
126   /**
127    * Called by nanoappAlloc to perform the appropriate call to memory alloc.
128    *
129    * The semantics are the same as nanoappAlloc.
130    */
131   void *doAlloc(Nanoapp *app, uint32_t size);
132 
133   /**
134    * Called by nanoappFree to perform the appropriate call to memory free.
135    *
136    * The sematics are the same as nanoappFree.
137    */
138   void doFree(Nanoapp *app, void *ptr);
139 };
140 
141 }  // namespace chre
142 
143 #endif  // CHRE_PLATFORM_MEMORY_MANAGER_H_
144