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