1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef __MALLOC_H 6 #define __MALLOC_H 7 8 #include <stddef.h> 9 #include <types_ext.h> 10 11 /* 12 * Due to bget implementation, the first memory pool registered shall have 13 * a min size. Choose 1kB which is reasonable. 14 */ 15 #define MALLOC_INITIAL_POOL_MIN_SIZE 1024 16 17 void *malloc(size_t size); 18 void *calloc(size_t nmemb, size_t size); 19 void *realloc(void *ptr, size_t size); 20 void *memalign(size_t alignment, size_t size); 21 void free(void *ptr); 22 23 #if __STDC_VERSION__ >= 201112L 24 void *aligned_alloc(size_t alignment, size_t size); 25 #endif 26 27 #ifdef ENABLE_MDBG 28 29 void *mdbg_malloc(const char *fname, int lineno, size_t size); 30 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 31 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 32 void *mdbg_memalign(const char *fname, int lineno, size_t alignment, 33 size_t size); 34 35 #if __STDC_VERSION__ >= 201112L 36 void *mdbg_aligned_alloc(const char *fname, int lineno, size_t alignment, 37 size_t size); 38 #endif 39 40 void mdbg_check(int bufdump); 41 42 #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 43 #define calloc(nmemb, size) \ 44 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 45 #define realloc(ptr, size) \ 46 mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 47 #define memalign(alignment, size) \ 48 mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 49 50 #if __STDC_VERSION__ >= 201112L 51 #define aligned_alloc(alignment, size) \ 52 mdbg_aligned_alloc(__FILE__, __LINE__, (alignment), (size)) 53 #endif /* __STDC_VERSION__ */ 54 55 #else 56 57 #define mdbg_check(x) do { } while (0) 58 59 #endif 60 61 /* 62 * Returns true if the supplied memory area is within a buffer 63 * previously allocated (and not freed yet). 64 * 65 * Used internally by TAs 66 */ 67 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 68 69 /* 70 * Returns true if the supplied memory area is overlapping the area used 71 * for heap. 72 * 73 * Used internally by TAs 74 */ 75 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 76 77 /* 78 * Adds a pool of memory to allocate from. 79 */ 80 void malloc_add_pool(void *buf, size_t len); 81 82 #ifdef CFG_WITH_STATS 83 /* 84 * Get/reset allocation statistics 85 */ 86 87 #define TEE_ALLOCATOR_DESC_LENGTH 32 88 struct malloc_stats { 89 char desc[TEE_ALLOCATOR_DESC_LENGTH]; 90 uint32_t allocated; /* Bytes currently allocated */ 91 uint32_t max_allocated; /* Tracks max value of allocated */ 92 uint32_t size; /* Total size for this allocator */ 93 uint32_t num_alloc_fail; /* Number of failed alloc requests */ 94 uint32_t biggest_alloc_fail; /* Size of biggest failed alloc */ 95 uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */ 96 }; 97 98 void malloc_get_stats(struct malloc_stats *stats); 99 void malloc_reset_stats(void); 100 #endif /* CFG_WITH_STATS */ 101 102 103 #ifdef CFG_VIRTUALIZATION 104 105 void nex_free(void *ptr); 106 107 #ifdef ENABLE_MDBG 108 109 void *nex_mdbg_malloc(const char *fname, int lineno, size_t size); 110 void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 111 void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 112 void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment, 113 size_t size); 114 115 void nex_mdbg_check(int bufdump); 116 117 #define nex_malloc(size) nex_mdbg_malloc(__FILE__, __LINE__, (size)) 118 #define nex_calloc(nmemb, size) \ 119 nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 120 #define nex_realloc(ptr, size) \ 121 nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 122 #define nex_memalign(alignment, size) \ 123 nex_mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 124 125 #else /* ENABLE_MDBG */ 126 127 void *nex_malloc(size_t size); 128 void *nex_calloc(size_t nmemb, size_t size); 129 void *nex_realloc(void *ptr, size_t size); 130 void *nex_memalign(size_t alignment, size_t size); 131 132 #define nex_mdbg_check(x) do { } while (0) 133 134 #endif /* ENABLE_MDBG */ 135 136 bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len); 137 bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len); 138 void nex_malloc_add_pool(void *buf, size_t len); 139 140 #ifdef CFG_WITH_STATS 141 /* 142 * Get/reset allocation statistics 143 */ 144 145 void nex_malloc_get_stats(struct malloc_stats *stats); 146 void nex_malloc_reset_stats(void); 147 148 #endif /* CFG_WITH_STATS */ 149 #else /* CFG_VIRTUALIZATION */ 150 151 #define nex_free(ptr) free(ptr) 152 #define nex_malloc(size) malloc(size) 153 #define nex_calloc(nmemb, size) calloc(nmemb, size) 154 #define nex_realloc(ptr, size) realloc(ptr, size) 155 #define nex_memalign(alignment, size) memalign(alignment, size) 156 157 #endif /* CFG_VIRTUALIZATION */ 158 159 struct malloc_ctx; 160 void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment, 161 size_t pl_size, struct malloc_ctx *ctx); 162 void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size, 163 struct malloc_ctx *ctx); 164 void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe); 165 void *raw_calloc(size_t hdr_size, size_t ftr_size, size_t pl_nmemb, 166 size_t pl_size, struct malloc_ctx *ctx); 167 void *raw_realloc(void *ptr, size_t hdr_size, size_t ftr_size, 168 size_t pl_size, struct malloc_ctx *ctx); 169 size_t raw_malloc_get_ctx_size(void); 170 void raw_malloc_init_ctx(struct malloc_ctx *ctx); 171 void raw_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len); 172 bool raw_malloc_buffer_overlaps_heap(struct malloc_ctx *ctx, 173 void *buf, size_t len); 174 bool raw_malloc_buffer_is_within_alloced(struct malloc_ctx *ctx, 175 void *buf, size_t len); 176 #ifdef CFG_WITH_STATS 177 void raw_malloc_get_stats(struct malloc_ctx *ctx, struct malloc_stats *stats); 178 #endif 179 180 #endif /* __MALLOC_H */ 181