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