xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 9403c583381528e7fb391e3769644cc9653cfbb6)
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 void free(void *ptr);
34 
35 #ifdef ENABLE_MDBG
36 
37 void *mdbg_malloc(const char *fname, int lineno, size_t size);
38 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
39 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
40 void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
41 		size_t size);
42 
43 void mdbg_check(int bufdump);
44 
45 #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
46 #define calloc(nmemb, size) \
47 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
48 #define realloc(ptr, size) \
49 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
50 #define memalign(alignment, size) \
51 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
52 
53 #else
54 
55 void *malloc(size_t size);
56 void *calloc(size_t nmemb, size_t size);
57 void *realloc(void *ptr, size_t size);
58 void *memalign(size_t alignment, size_t size);
59 
60 #define mdbg_check(x)        do { } while (0)
61 
62 #endif
63 
64 
65 /*
66  * Returns true if the supplied memory area is within a buffer
67  * previously allocated (and not freed yet).
68  *
69  * Used internally by TAs
70  */
71 bool malloc_buffer_is_within_alloced(void *buf, size_t len);
72 
73 /*
74  * Returns true if the supplied memory area is overlapping the area used
75  * for heap.
76  *
77  * Used internally by TAs
78  */
79 bool malloc_buffer_overlaps_heap(void *buf, size_t len);
80 
81 /*
82  * Adds a pool of memory to allocate from.
83  */
84 void malloc_add_pool(void *buf, size_t len);
85 
86 /* get malloc stats: curr allocated heap and max allocated heap since boot */
87 void malloc_reset_max_allocated(void);
88 size_t malloc_get_max_allocated(void);
89 size_t malloc_get_allocated(void);
90 size_t malloc_get_heap_size(void);
91 
92 #endif /* MALLOC_H */
93