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 void *__mdbg_alloc(uint32_t flags, void *ptr, size_t alignment, size_t nmemb, 32 size_t size, const char *fname, int lineno); 33 34 void mdbg_check(int bufdump); 35 36 #define malloc(size) __mdbg_alloc(MAF_NULL, NULL, 1, 1, \ 37 (size), __FILE__, __LINE__) 38 #define calloc(nmemb, size) __mdbg_alloc(MAF_ZERO_INIT, NULL, 1, (nmemb), \ 39 (size), __FILE__, __LINE__) 40 #define realloc(ptr, size) __mdbg_alloc(MAF_NULL, (ptr), 1, 1, \ 41 (size), __FILE__, __LINE__) 42 #define memalign(align, size) __mdbg_alloc(MAF_NULL, NULL, (align), 1, \ 43 (size), __FILE__, __LINE__) 44 45 #if __STDC_VERSION__ >= 201112L 46 #define aligned_alloc(align, size) \ 47 __mdbg_alloc(MAF_NULL, NULL, (align), 1, (size), __FILE__, __LINE__) 48 #endif /* __STDC_VERSION__ */ 49 50 #else 51 52 #define mdbg_check(x) do { } while (0) 53 54 #endif 55 56 /* 57 * Returns true if the supplied memory area is within a buffer 58 * previously allocated (and not freed yet). 59 * 60 * Used internally by TAs 61 */ 62 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 63 64 /* 65 * Returns true if the supplied memory area is overlapping the area used 66 * for heap. 67 * 68 * Used internally by TAs 69 */ 70 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 71 72 /* 73 * Adds a pool of memory to allocate from. 74 */ 75 void malloc_add_pool(void *buf, size_t len); 76 77 #ifdef CFG_WITH_STATS 78 /* Get/reset allocation statistics */ 79 void malloc_get_stats(struct pta_stats_alloc *stats); 80 void malloc_reset_stats(void); 81 #endif /* CFG_WITH_STATS */ 82 83 #ifdef CFG_NS_VIRTUALIZATION 84 #ifdef ENABLE_MDBG 85 86 void nex_mdbg_check(int bufdump); 87 88 #define nex_malloc(size) __mdbg_alloc(MAF_NEX, NULL, 1, 1, \ 89 (size), __FILE__, __LINE__) 90 #define nex_calloc(nmemb, size) __mdbg_alloc(MAF_NEX | MAF_ZERO_INIT, NULL, 1, \ 91 (nmemb), (size), __FILE__, \ 92 __LINE__) 93 #define nex_realloc(ptr, size) __mdbg_alloc(MAF_NEX, (ptr), 1, 1, \ 94 (size), __FILE__, __LINE__) 95 #define nex_memalign(align, size) __mdbg_alloc(MAF_NEX, NULL, (align), 1, \ 96 (size), __FILE__, __LINE__) 97 #else /* ENABLE_MDBG */ 98 99 #define nex_mdbg_check(x) do { } while (0) 100 101 void *nex_malloc(size_t size); 102 void *nex_calloc(size_t nmemb, size_t size); 103 void *nex_realloc(void *ptr, size_t size); 104 void *nex_memalign(size_t alignment, size_t size); 105 106 #endif /* ENABLE_MDBG */ 107 108 void nex_free(void *ptr); 109 110 bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len); 111 bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len); 112 void nex_malloc_add_pool(void *buf, size_t len); 113 114 #ifdef CFG_WITH_STATS 115 /* 116 * Get/reset allocation statistics 117 */ 118 119 void nex_malloc_get_stats(struct pta_stats_alloc *stats); 120 void nex_malloc_reset_stats(void); 121 122 #endif /* CFG_WITH_STATS */ 123 #else /* CFG_NS_VIRTUALIZATION */ 124 125 #define nex_free(ptr) free(ptr) 126 #define nex_malloc(size) malloc(size) 127 #define nex_calloc(nmemb, size) calloc(nmemb, size) 128 #define nex_realloc(ptr, size) realloc(ptr, size) 129 #define nex_memalign(alignment, size) memalign(alignment, size) 130 #define nex_malloc_buffer_overlaps_heap(buf, len) \ 131 malloc_buffer_overlaps_heap(buf, len) 132 #define nex_malloc_buffer_is_within_alloced(buf, len) \ 133 malloc_buffer_is_within_alloced(buf, len) 134 135 #endif /* CFG_NS_VIRTUALIZATION */ 136 137 struct malloc_ctx; 138 void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment, 139 size_t pl_size, struct malloc_ctx *ctx); 140 void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size, 141 struct malloc_ctx *ctx); 142 void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe); 143 void *raw_calloc(size_t hdr_size, size_t ftr_size, size_t pl_nmemb, 144 size_t pl_size, struct malloc_ctx *ctx); 145 void *raw_realloc(void *ptr, size_t hdr_size, size_t ftr_size, 146 size_t pl_size, struct malloc_ctx *ctx); 147 size_t raw_malloc_get_ctx_size(void); 148 void raw_malloc_init_ctx(struct malloc_ctx *ctx); 149 void raw_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len); 150 bool raw_malloc_buffer_overlaps_heap(struct malloc_ctx *ctx, 151 void *buf, size_t len); 152 bool raw_malloc_buffer_is_within_alloced(struct malloc_ctx *ctx, 153 void *buf, size_t len); 154 #ifdef CFG_WITH_STATS 155 void raw_malloc_get_stats(struct malloc_ctx *ctx, 156 struct pta_stats_alloc *stats); 157 #endif 158 159 #endif /* __MALLOC_H */ 160