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