xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef MALLOC_H
29 #define MALLOC_H
30 
31 #include <stddef.h>
32 #include <types_ext.h>
33 
34 extern unsigned int __malloc_spinlock;
35 
36 void free(void *ptr);
37 
38 #ifdef ENABLE_MDBG
39 
40 void *mdbg_malloc(const char *fname, int lineno, size_t size);
41 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
42 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
43 void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
44 		size_t size);
45 
46 void mdbg_check(int bufdump);
47 
48 #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
49 #define calloc(nmemb, size) \
50 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
51 #define realloc(ptr, size) \
52 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
53 #define memalign(alignment, size) \
54 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
55 
56 #else
57 
58 void *malloc(size_t size);
59 void *calloc(size_t nmemb, size_t size);
60 void *realloc(void *ptr, size_t size);
61 void *memalign(size_t alignment, size_t size);
62 
63 #define mdbg_check(x)        do { } while (0)
64 
65 #endif
66 
67 
68 /*
69  * Returns true if the supplied memory area is within a buffer
70  * previously allocated (and not freed yet).
71  *
72  * Used internally by TAs
73  */
74 bool malloc_buffer_is_within_alloced(void *buf, size_t len);
75 
76 /*
77  * Returns true if the supplied memory area is overlapping the area used
78  * for heap.
79  *
80  * Used internally by TAs
81  */
82 bool malloc_buffer_overlaps_heap(void *buf, size_t len);
83 
84 /*
85  * Adds a pool of memory to allocate from.
86  */
87 void malloc_add_pool(void *buf, size_t len);
88 
89 #ifdef CFG_WITH_STATS
90 /*
91  * Get/reset allocation statistics
92  */
93 
94 #define TEE_ALLOCATOR_DESC_LENGTH 32
95 struct malloc_stats {
96 	char desc[TEE_ALLOCATOR_DESC_LENGTH];
97 	uint32_t allocated;               /* Bytes currently allocated */
98 	uint32_t max_allocated;           /* Tracks max value of allocated */
99 	uint32_t size;                    /* Total size for this allocator */
100 	uint32_t num_alloc_fail;          /* Number of failed alloc requests */
101 	uint32_t biggest_alloc_fail;      /* Size of biggest failed alloc */
102 	uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */
103 };
104 
105 void malloc_get_stats(struct malloc_stats *stats);
106 void malloc_reset_stats(void);
107 #endif /* CFG_WITH_STATS */
108 
109 #endif /* MALLOC_H */
110