xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 741b437f06a298194b4a1b43b526fdd18ee7fda8)
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 #endif /* MALLOC_H */
85