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 <pta_stats.h>
9 #include <stddef.h>
10 #include <types_ext.h>
11 
12 /*
13  * Due to bget implementation, the first memory pool registered shall have
14  * a min size. Choose 1kB which is reasonable.
15  */
16 #define MALLOC_INITIAL_POOL_MIN_SIZE	1024
17 
18 void *malloc(size_t size);
19 void *calloc(size_t nmemb, size_t size);
20 void *realloc(void *ptr, size_t size);
21 void *memalign(size_t alignment, size_t size);
22 void free(void *ptr);
23 
24 #if __STDC_VERSION__ >= 201112L
25 void *aligned_alloc(size_t alignment, size_t size);
26 #endif
27 
28 #ifdef ENABLE_MDBG
29 
30 void *mdbg_malloc(const char *fname, int lineno, size_t size);
31 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
32 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
33 void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
34 		    size_t size);
35 
36 #if __STDC_VERSION__ >= 201112L
37 void *mdbg_aligned_alloc(const char *fname, int lineno, size_t alignment,
38 			 size_t size);
39 #endif
40 
41 void mdbg_check(int bufdump);
42 
43 #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
44 #define calloc(nmemb, size) \
45 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
46 #define realloc(ptr, size) \
47 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
48 #define memalign(alignment, size) \
49 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
50 
51 #if __STDC_VERSION__ >= 201112L
52 #define aligned_alloc(alignment, size) \
53 		mdbg_aligned_alloc(__FILE__, __LINE__, (alignment), (size))
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 
90 #ifdef CFG_NS_VIRTUALIZATION
91 
92 void nex_free(void *ptr);
93 
94 #ifdef ENABLE_MDBG
95 
96 void *nex_mdbg_malloc(const char *fname, int lineno, size_t size);
97 void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
98 void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
99 void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment,
100 			size_t size);
101 
102 void nex_mdbg_check(int bufdump);
103 
104 #define nex_malloc(size)	nex_mdbg_malloc(__FILE__, __LINE__, (size))
105 #define nex_calloc(nmemb, size) \
106 		nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
107 #define nex_realloc(ptr, size) \
108 		nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
109 #define nex_memalign(alignment, size) \
110 		nex_mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
111 
112 #else /* ENABLE_MDBG */
113 
114 void *nex_malloc(size_t size);
115 void *nex_calloc(size_t nmemb, size_t size);
116 void *nex_realloc(void *ptr, size_t size);
117 void *nex_memalign(size_t alignment, size_t size);
118 
119 #define nex_mdbg_check(x)        do { } while (0)
120 
121 #endif /* ENABLE_MDBG */
122 
123 bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len);
124 bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len);
125 void nex_malloc_add_pool(void *buf, size_t len);
126 
127 #ifdef CFG_WITH_STATS
128 /*
129  * Get/reset allocation statistics
130  */
131 
132 void nex_malloc_get_stats(struct pta_stats_alloc *stats);
133 void nex_malloc_reset_stats(void);
134 
135 #endif	/* CFG_WITH_STATS */
136 #else  /* CFG_NS_VIRTUALIZATION */
137 
138 #define nex_free(ptr) free(ptr)
139 #define nex_malloc(size) malloc(size)
140 #define nex_calloc(nmemb, size) calloc(nmemb, size)
141 #define nex_realloc(ptr, size) realloc(ptr, size)
142 #define nex_memalign(alignment, size) memalign(alignment, size)
143 #define nex_malloc_buffer_overlaps_heap(buf, len) \
144 	malloc_buffer_overlaps_heap(buf, len)
145 #define nex_malloc_buffer_is_within_alloced(buf, len) \
146 	malloc_buffer_is_within_alloced(buf, len)
147 
148 #endif	/* CFG_NS_VIRTUALIZATION */
149 
150 struct malloc_ctx;
151 void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment,
152 		   size_t pl_size, struct malloc_ctx *ctx);
153 void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size,
154 		 struct malloc_ctx *ctx);
155 void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe);
156 void *raw_calloc(size_t hdr_size, size_t ftr_size, size_t pl_nmemb,
157 		 size_t pl_size, struct malloc_ctx *ctx);
158 void *raw_realloc(void *ptr, size_t hdr_size, size_t ftr_size,
159 		  size_t pl_size, struct malloc_ctx *ctx);
160 size_t raw_malloc_get_ctx_size(void);
161 void raw_malloc_init_ctx(struct malloc_ctx *ctx);
162 void raw_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len);
163 bool raw_malloc_buffer_overlaps_heap(struct malloc_ctx *ctx,
164 				     void *buf, size_t len);
165 bool raw_malloc_buffer_is_within_alloced(struct malloc_ctx *ctx,
166 					 void *buf, size_t len);
167 #ifdef CFG_WITH_STATS
168 void raw_malloc_get_stats(struct malloc_ctx *ctx,
169 			  struct pta_stats_alloc *stats);
170 #endif
171 
172 #endif /* __MALLOC_H */
173