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