1 /* 2 // Copyright (C) 2022 Beken Corporation 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 16 17 #ifndef _MMGMT_H_ 18 #define _MMGMT_H_ 19 20 #include "rwnx_config.h" 21 22 //#define MMGMT_DEBUG 23 24 #define MM_MAGIC 0x1234abcd 25 26 /** 27 **************************************************************************************** 28 * @defgroup KE_MEM KE_MEM 29 * @ingroup KERNEL 30 * @brief Heap management module. 31 * 32 * The KEMEM module implements heap management functions that allow initializing heap, 33 * allocating and freeing memory. 34 * 35 * @{ 36 **************************************************************************************** 37 */ 38 #define KE_HEAP_SIZE CFG_HEAP_SIZE 39 40 /// Free memory block delimiter structure (size must be word multiple) 41 struct mblock_free { 42 struct mblock_free *next; ///< Pointer to the next block 43 uint32_t size; ///< Size of the current free block (including delimiter) 44 }; 45 46 /// Used memory block delimiter structure (size must be word multiple) 47 struct mblock_used { 48 uint32_t magic; 49 uint32_t size; ///< Size of the current used block (including delimiter) 50 }; 51 52 // forward declarations 53 struct mblock_free; 54 55 extern void sys_mem_init(void); 56 57 /** 58 **************************************************************************************** 59 * @brief Heap initialization. 60 * 61 * This function performs the following operations: 62 * - sanity checks 63 * - check memory allocated is at least large enough to hold two block descriptors to hold 64 * start and end 65 * - initialize the first and last descriptors 66 * - save the pointer to the first free descriptor 67 * 68 * @return The pointer to the first free block. 69 * 70 **************************************************************************************** 71 */ 72 struct mblock_free *mmgmt_init(void); 73 74 /** 75 **************************************************************************************** 76 * @brief Allocation of a block of memory. 77 * 78 * Allocates a memory block whose size is size; if no memory is available return 79 * NULL 80 * 81 * @param[in] size Size of the memory area that need to be allocated. 82 * 83 * @return A pointer to the allocated memory area. 84 * 85 **************************************************************************************** 86 */ 87 #if CONFIG_MALLOC_STATIS 88 void *mmgmt_malloc(const char *call_func_name, int line, uint32_t size); 89 #else 90 void *mmgmt_malloc(uint32_t size); 91 #endif 92 void *mmgmt_realloc(void *ptr, size_t size); 93 uint32_t mmgmt_alloc_space(void); 94 95 /** 96 **************************************************************************************** 97 * @brief Freeing of a block of memory. 98 * 99 * Free the memory area pointed by mem_ptr : mark the block as free and insert it in 100 * the pool of free block. 101 * 102 * @param[in] mem_ptr Pointer to the memory area that need to be freed. 103 * 104 **************************************************************************************** 105 */ 106 #if CONFIG_MALLOC_STATIS 107 void mmgmt_free(const char *call_func_name, int line, void *mem_ptr); 108 #else 109 void mmgmt_free(void *mem_ptr); 110 #endif 111 112 int mm_magic_match(void *mem_ptr); 113 114 ///@} MEM 115 116 #endif // _MMGMT_H_ 117 118