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