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 #ifdef ENABLE_MDBG
24 
25 void *mdbg_malloc(const char *fname, int lineno, size_t size);
26 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
27 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
28 void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
29 		    size_t size);
30 
31 void mdbg_check(int bufdump);
32 
33 #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
34 #define calloc(nmemb, size) \
35 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
36 #define realloc(ptr, size) \
37 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
38 #define memalign(alignment, size) \
39 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
40 
41 #else
42 
43 #define mdbg_check(x)        do { } while (0)
44 
45 #endif
46 
47 /*
48  * Returns true if the supplied memory area is within a buffer
49  * previously allocated (and not freed yet).
50  *
51  * Used internally by TAs
52  */
53 bool malloc_buffer_is_within_alloced(void *buf, size_t len);
54 
55 /*
56  * Returns true if the supplied memory area is overlapping the area used
57  * for heap.
58  *
59  * Used internally by TAs
60  */
61 bool malloc_buffer_overlaps_heap(void *buf, size_t len);
62 
63 /*
64  * Adds a pool of memory to allocate from.
65  */
66 void malloc_add_pool(void *buf, size_t len);
67 
68 #ifdef CFG_WITH_STATS
69 /*
70  * Get/reset allocation statistics
71  */
72 
73 #define TEE_ALLOCATOR_DESC_LENGTH 32
74 struct malloc_stats {
75 	char desc[TEE_ALLOCATOR_DESC_LENGTH];
76 	uint32_t allocated;               /* Bytes currently allocated */
77 	uint32_t max_allocated;           /* Tracks max value of allocated */
78 	uint32_t size;                    /* Total size for this allocator */
79 	uint32_t num_alloc_fail;          /* Number of failed alloc requests */
80 	uint32_t biggest_alloc_fail;      /* Size of biggest failed alloc */
81 	uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */
82 };
83 
84 void malloc_get_stats(struct malloc_stats *stats);
85 void malloc_reset_stats(void);
86 #endif /* CFG_WITH_STATS */
87 
88 
89 #ifdef CFG_VIRTUALIZATION
90 
91 void nex_free(void *ptr);
92 
93 #ifdef ENABLE_MDBG
94 
95 void *nex_mdbg_malloc(const char *fname, int lineno, size_t size);
96 void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
97 void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
98 void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment,
99 			size_t size);
100 
101 void nex_mdbg_check(int bufdump);
102 
103 #define nex_malloc(size)	nex_mdbg_malloc(__FILE__, __LINE__, (size))
104 #define nex_calloc(nmemb, size) \
105 		nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
106 #define nex_realloc(ptr, size) \
107 		nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
108 #define nex_memalign(alignment, size) \
109 		nex_mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
110 
111 #else /* ENABLE_MDBG */
112 
113 void *nex_malloc(size_t size);
114 void *nex_calloc(size_t nmemb, size_t size);
115 void *nex_realloc(void *ptr, size_t size);
116 void *nex_memalign(size_t alignment, size_t size);
117 
118 #define nex_mdbg_check(x)        do { } while (0)
119 
120 #endif /* ENABLE_MDBG */
121 
122 bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len);
123 bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len);
124 void nex_malloc_add_pool(void *buf, size_t len);
125 
126 #ifdef CFG_WITH_STATS
127 /*
128  * Get/reset allocation statistics
129  */
130 
131 void nex_malloc_get_stats(struct malloc_stats *stats);
132 void nex_malloc_reset_stats(void);
133 
134 #endif	/* CFG_WITH_STATS */
135 #else  /* CFG_VIRTUALIZATION */
136 
137 #define nex_free(ptr) free(ptr)
138 #define nex_malloc(size) malloc(size)
139 #define nex_calloc(nmemb, size) calloc(nmemb, size)
140 #define nex_realloc(ptr, size) realloc(ptr, size)
141 #define nex_memalign(alignment, size) memalign(alignment, size)
142 
143 #endif	/* CFG_VIRTUALIZATION */
144 
145 struct malloc_ctx;
146 void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment,
147 		   size_t pl_size, struct malloc_ctx *ctx);
148 void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size,
149 		 struct malloc_ctx *ctx);
150 void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe);
151 void *raw_calloc(size_t hdr_size, size_t ftr_size, size_t pl_nmemb,
152 		 size_t pl_size, struct malloc_ctx *ctx);
153 void *raw_realloc(void *ptr, size_t hdr_size, size_t ftr_size,
154 		  size_t pl_size, struct malloc_ctx *ctx);
155 size_t raw_malloc_get_ctx_size(void);
156 void raw_malloc_init_ctx(struct malloc_ctx *ctx);
157 void raw_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len);
158 #ifdef CFG_WITH_STATS
159 void raw_malloc_get_stats(struct malloc_ctx *ctx, struct malloc_stats *stats);
160 #endif
161 
162 #endif /* MALLOC_H */
163