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 26 namespace chre { 27 28 /** 29 * The MemoryManager keeps track of heap memory allocated/deallocated by all 30 * nanoapps. 31 * 32 * TODO: Free memory space when nanoapps are unloaded. 33 */ 34 class MemoryManager : public NonCopyable { 35 public: 36 /** 37 * Allocate heap memory in CHRE. 38 * 39 * @param app The pointer to the nanoapp requesting memory. 40 * @param bytes The size in bytes to allocate. 41 * @return the allocated memory pointer. nullptr if the allocation fails. 42 */ 43 void *nanoappAlloc(Nanoapp *app, uint32_t bytes); 44 45 /** 46 * Free heap memory in CHRE. 47 * 48 * @param app The pointer to the nanoapp requesting memory free. 49 * @param ptr The pointer to the memory to deallocate. 50 */ 51 void nanoappFree(Nanoapp *app, void *ptr); 52 53 /** 54 * @return current total allocated memory in bytes. 55 */ 56 size_t getTotalAllocatedBytes() const { 57 return mTotalAllocatedBytes; 58 } 59 60 /** 61 * @return current count of allocated memory spaces. 62 */ 63 size_t getAllocationCount() const { 64 return mAllocationCount; 65 } 66 67 /** 68 * @return max total allocatable memory in bytes. 69 */ 70 size_t getMaxAllocationBytes() const { 71 return kMaxAllocationBytes; 72 } 73 74 /** 75 * @return max allocatable memory counts. 76 */ 77 size_t getMaxAllocationCount() const { 78 return kMaxAllocationCount; 79 } 80 81 /** 82 * Prints state in a string buffer. Must only be called from the context of 83 * the main CHRE thread. 84 * 85 * @param buffer Pointer to the start of the buffer. 86 * @param bufferPos Pointer to buffer position to start the print (in-out). 87 * @param size Size of the buffer in bytes. 88 * 89 * @return true if entire log printed, false if overflow or error. 90 */ 91 bool logStateToBuffer(char *buffer, size_t *bufferPos, 92 size_t bufferSize) const; 93 94 private: 95 /** 96 * Header to store allocation details for tracking. 97 * We use a union to ensure proper memory alignment. 98 */ 99 union AllocHeader { 100 struct { 101 //! The amount of memory in bytes allocated (not including header). 102 uint32_t bytes; 103 104 //! The ID of nanoapp requesting memory allocation. 105 uint32_t instanceId; 106 } data; 107 108 //! Makes sure header is a multiple of the size of max_align_t 109 max_align_t aligner; 110 }; 111 112 //! Stores total allocated memory in bytes (not including header). 113 size_t mTotalAllocatedBytes = 0; 114 115 //! Stores total number of allocated memory spaces. 116 size_t mAllocationCount = 0; 117 118 //! The maximum allowable total allocated memory in bytes for all nanoapps. 119 static constexpr size_t kMaxAllocationBytes = (128 * 1024); 120 121 //! The maximum allowable count of memory allocations for all nanoapps. 122 static constexpr size_t kMaxAllocationCount = (8 * 1024); 123 124 /** 125 * Called by nanoappAlloc to perform the appropriate call to memory alloc. 126 * 127 * The semantics are the same as nanoappAlloc. 128 */ 129 void *doAlloc(Nanoapp *app, uint32_t size); 130 131 /** 132 * Called by nanoappFree to perform the appropriate call to memory free. 133 * 134 * The sematics are the same as nanoappFree. 135 */ 136 void doFree(Nanoapp *app, void *ptr); 137 }; 138 139 } // namespace chre 140 141 #endif // CHRE_PLATFORM_MEMORY_MANAGER_H_ 142