xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 8541149400cfc3ddb8b329bc8dc192754f45a4d0)
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 
32 enum mdbg_mode {
33 	MDBG_MODE_STATIC,
34 	MDBG_MODE_DYNAMIC
35 };
36 
37 /*
38  * ENABLE_MDBG: malloc debug support
39  *
40  * When enabled, malloc, calloc, realloc and free are redirected from SLA
41  * to routines that trace memory callers (filename/line) and provide few other
42  * memory alloc debug features. calloc and realloc are routed to basic malloc.
43  *
44  * memalign and other standard mem alloc APIs are not handled by mdbg.
45  *
46  * If ENABLE_MDBG is not set, malloc.c acts as a wrapper to redirect std
47  * malloc apis to the apis of the embedded malloc library (SLA, dlmalloc,...).
48  */
49 #ifdef ENABLE_MDBG
50 
51 /* define mdbg 'malloc' routine and redirect std apis to these. */
52 void *mdbg_malloc(const char *fname, int lineno, unsigned nbytes);
53 void *mdbg_calloc(const char *fname, int lineno, unsigned nelem,
54 		  unsigned elsize);
55 #ifdef MDBG_REALLOC_ENABLED
56 void *mdbg_realloc(const char *fname, int lineno, void *ptr, unsigned size);
57 #endif
58 
59 void mdbg_free(void *fp);
60 void mdbg_dump(int bufdump);
61 enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode);
62 void mdbg_check(void);
63 
64 /* Redefine standard memory allocator calls to use our routines instead. */
65 #define free           mdbg_free
66 #define malloc(x)      mdbg_malloc(__FILE__, __LINE__, (x))
67 #define calloc(n, e)    mdbg_calloc(__FILE__, __LINE__, (n), (e))
68 #define realloc(p, x)   mdbg_realloc(__FILE__, __LINE__, (p), (x))
69 
70 #else
71 
72 /* mdbg not enabled: simple define standard apis */
73 void *calloc(size_t nmemb, size_t size);
74 void free(void *ptr);
75 void *malloc(size_t size);
76 void *realloc(void *ptr, size_t size);
77 
78 #define mdbg_check()        do { } while (0)
79 #define mdbg_dump(x)        do { } while (0)
80 static inline enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode)
81 {
82 	return mode;
83 }
84 
85 #endif /* ENABLE_MDBG */
86 
87 /* other standard malloc apis */
88 void *memalign(size_t align, size_t size);
89 void *valloc(size_t size);
90 void *pvalloc(size_t size);
91 
92 /* entry point for malloc init in case some inits are required */
93 void malloc_init(void *start, size_t size);
94 
95 #endif /* MALLOC_H */
96