1 /* 2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef _BLE_KE_MEM_H 16 #define _BLE_KE_MEM_H 17 18 #include "ble_ip_config.h" // IP configuration 19 #include <stdint.h> // standard integer 20 #include <stdbool.h> // standard includes 21 22 /** 23 * @defgroup MEM Memory 24 * @ingroup KERNEL 25 * @brief Heap management module. 26 * 27 * This module implements heap management functions that allow initializing heap, 28 * allocating and freeing memory. 29 * 30 */ 31 32 // forward declarations 33 struct mblock_free; 34 35 #ifdef CFG_AON 36 struct mblock_free 37 { 38 /// Used to check if memory block has been corrupted or not 39 uint16_t corrupt_check; 40 /// Size of the current free block (including delimiter) 41 uint16_t free_size; 42 /// Next free block pointer 43 struct mblock_free* next; 44 /// Previous free block pointer 45 struct mblock_free* previous; 46 }; 47 #endif 48 49 typedef struct mblock_free aon_mblock_free; 50 /** 51 **************************************************************************************** 52 * @brief Heap initialization. 53 * 54 * This function performs the following operations: 55 * - sanity checks 56 * - check memory allocated is at least large enough to hold two block descriptors to hold 57 * start and end 58 * - initialize the first and last descriptors 59 * - save heap into kernel environment variable. 60 * 61 * @param[in] type Memory type. 62 * @param[in|out] heap Heap pointer 63 * @param[in] heap_size Size of the heap 64 * 65 * 66 **************************************************************************************** 67 */ 68 void ke_mem_init(uint8_t type, uint8_t* heap, uint16_t heap_size); 69 70 /** 71 **************************************************************************************** 72 * @brief Allocation of a block of memory. 73 * 74 * Allocates a memory block whose size is size; if no memory is available return NULL 75 * 76 * @param[in] size Size of the memory area that need to be allocated. 77 * @param[in] type Type of memory block 78 * 79 * @return A pointer to the allocated memory area. 80 * 81 **************************************************************************************** 82 */ 83 void *ke_malloc(uint32_t size, uint8_t type); 84 85 86 /** 87 **************************************************************************************** 88 * @brief Check if it's possible to allocate a block of memory with a specific size. 89 * 90 * @param[in] size Size of the memory area that need to be allocated. 91 * @param[in] type Type of memory block 92 * 93 * @return True if memory block can be allocated, False else. 94 * 95 **************************************************************************************** 96 */ 97 bool ke_check_malloc(uint32_t size, uint8_t type); 98 99 /** 100 **************************************************************************************** 101 * @brief Freeing of a block of memory. 102 * 103 * Free the memory area pointed by mem_ptr : mark the block as free and insert it in 104 * the pool of free block. 105 * 106 * @param[in] mem_ptr Pointer to the memory area that need to be freed. 107 * 108 **************************************************************************************** 109 */ 110 void ke_free(void *mem_ptr); 111 112 113 /** 114 **************************************************************************************** 115 * @brief Check if current heap is empty or not (not used) 116 * 117 * @param[in] type Type of memory heap block 118 * 119 * @return true if heap not used, false else. 120 **************************************************************************************** 121 */ 122 bool ke_mem_is_empty(uint8_t type); 123 124 125 126 /** 127 **************************************************************************************** 128 * @brief Check if current pointer is free or not 129 * 130 * @param[in] mem_ptr pointer to a memory block 131 * 132 * @return true if already free, false else. 133 **************************************************************************************** 134 */ 135 bool ke_is_free(void* mem_ptr); 136 137 #if (KE_PROFILING) 138 139 /** 140 **************************************************************************************** 141 * @brief Retrieve memory usage of selected heap. 142 * 143 * @param[in] type Type of memory heap block 144 * 145 * @return current memory usage of current heap. 146 **************************************************************************************** 147 */ 148 uint16_t ke_get_mem_usage(uint8_t type); 149 150 151 /** 152 **************************************************************************************** 153 * @brief Retrieve max memory usage of all heap. 154 * This command also resets max measured value. 155 * 156 * @return max memory usage of all heap. 157 **************************************************************************************** 158 */ 159 uint32_t ke_get_max_mem_usage(void); 160 161 #endif // (KE_PROFILING) 162 163 #endif // _BLE_KE_MEM_H 164 165