1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 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 * Description: malloc function 15 * Author: 16 * Create: 17 */ 18 19 #ifndef IRMALLOC_H 20 #define IRMALLOC_H 21 22 #include "std_def.h" 23 24 /** 25 * @defgroup connectivity_libs_irmalloc IRMALLOC 26 * @ingroup connectivity_libs 27 * @{ 28 */ 29 // for IAR compiler 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 /** Initialise the internal memory with the default pool passed in via the linker */ 35 void irmalloc_init_default(void); 36 37 /** 38 * @brief Add an additional memory pool to allocate from; the irmalloc implementation is not obliged to use this memory 39 * @param start is the start memory address of the new pool 40 * @param size is number of bytes in the new memory pool 41 */ 42 void irmalloc_new_pool(unsigned char *start, size_t size); 43 44 /** 45 * @brief Allocate a block of memory from internal RAM 46 * @param size size of the block to allocate 47 * @return pointer to allocated memory, or NULL 48 */ 49 //lint -function(malloc,irmalloc) irmalloc has the same semantics as malloc. 50 void *irmalloc(size_t size); 51 52 53 void *irmemalign(size_t boundary, size_t size); 54 55 /** 56 * @brief Allocate a block of memory from internal RAM or panic on failure 57 * @param size size of the block to allocate 58 * @return pointer to allocated memory 59 */ 60 //lint -function(malloc(0),irmalloc_panicking) irmalloc has the same semantics as malloc, but doesn't return NULL. 61 //lint -function(malloc(1),irmalloc_panicking) irmalloc has the same semantics as malloc, but doesn't return NULL. 62 void *irmalloc_panicking(size_t size); 63 64 /** 65 * @brief Allocate a block of memory and zero it. 66 * @param size size of the block to allocate 67 * @return pointer to allocated memory, or NULL 68 */ 69 //lint -function(malloc,irzalloc) irzalloc has the same semantics as malloc. 70 void *irzalloc(size_t size); 71 72 /** 73 * @brief Allocate a block of memory and zero it. 74 * @param size size of each block to allocate 75 * @param number number of blocks to allocate 76 * @return pointer to allocated memory, or NULL 77 */ 78 #define ircalloc(size, number) irzalloc((size) * (number)) 79 80 /** 81 * @brief Free a block of memory 82 * @param buf 83 */ 84 //lint -function(free,irfree) irfree has the same semantics as free. 85 void irfree(void *buf); 86 87 /** 88 * @brief Duplicate the contents of a block of memory into irmalloc. 89 * @param orig pointer to memory to duplicate 90 * @param size size of the block to duplicate 91 * @return pointer to block contain 92 */ 93 void *irmalloc_dup(const void *orig, size_t size); 94 95 /** 96 * @brief Reallocate a block of memory 97 */ 98 //lint -function(realloc,irrealloc) irrealloc has the same semantics as realloc. 99 void *irrealloc(void *buf, size_t size); 100 101 /** Reallocate a block of memory or panic on failure */ 102 // irrealloc_panicking has the same semantics as realloc, but doesn't return NULL. 103 void *irrealloc_panicking(void *buf, size_t size); 104 105 /** 106 * @brief Get some human readable statistics on the memory management state 107 * @return the irmalloc'ed memory buffer containing a NULL terminated ASCII string of the human readbale stats; 108 * this memory must be irfree'd by the calling code 109 * note The stats format is variable and is not guaranteed to be parsable.NULL is returned if no memory is irmallocable 110 */ 111 char *irmalloc_get_human_readable_stats(void); 112 113 /** 114 * @brief Get some macine readable statistics on the memory management state 115 * @param current_allocated pointer to memory to hold current allocated size 116 * @param total_free pointer to memory to hold current free pool size 117 * @param max_free pointer to memory to hold maximum free block size 118 * @param num_allocs pointer to memory to hold accumulated number of mallocs 119 * @param num_frees pointer to memory to hold accumulated number of frees 120 */ 121 void irmalloc_get_machine_readable_stats(uint32_t *current_allocated, uint32_t *total_free, 122 uint32_t *max_free, uint32_t *num_allocs, uint32_t *num_frees); 123 124 #ifdef __cplusplus 125 } 126 #endif 127 /** 128 * @} 129 */ 130 #endif 131