xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 2c276d687367efa8cf5d60cba165ad4493a72739)
1b0104773SPascal Brand /*
2b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
3b0104773SPascal Brand  * All rights reserved.
4b0104773SPascal Brand  *
5b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
6b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
7b0104773SPascal Brand  *
8b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
9b0104773SPascal Brand  * this list of conditions and the following disclaimer.
10b0104773SPascal Brand  *
11b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
12b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
13b0104773SPascal Brand  * and/or other materials provided with the distribution.
14b0104773SPascal Brand  *
15b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
26b0104773SPascal Brand  */
27b0104773SPascal Brand #ifndef MALLOC_H
28b0104773SPascal Brand #define MALLOC_H
29b0104773SPascal Brand 
30b0104773SPascal Brand #include <stddef.h>
31b0104773SPascal Brand 
32b0104773SPascal Brand enum mdbg_mode {
33b0104773SPascal Brand 	MDBG_MODE_STATIC,
34b0104773SPascal Brand 	MDBG_MODE_DYNAMIC
35b0104773SPascal Brand };
36b0104773SPascal Brand 
37b0104773SPascal Brand /*
38b0104773SPascal Brand  * ENABLE_MDBG: malloc debug support
39b0104773SPascal Brand  *
40b0104773SPascal Brand  * When enabled, malloc, calloc, realloc and free are redirected from SLA
41b0104773SPascal Brand  * to routines that trace memory callers (filename/line) and provide few other
42b0104773SPascal Brand  * memory alloc debug features. calloc and realloc are routed to basic malloc.
43b0104773SPascal Brand  *
44b0104773SPascal Brand  * memalign and other standard mem alloc APIs are not handled by mdbg.
45b0104773SPascal Brand  *
46b0104773SPascal Brand  * If ENABLE_MDBG is not set, malloc.c acts as a wrapper to redirect std
47b0104773SPascal Brand  * malloc apis to the apis of the embedded malloc library (SLA, dlmalloc,...).
48b0104773SPascal Brand  */
49b0104773SPascal Brand #ifdef ENABLE_MDBG
50b0104773SPascal Brand 
51b0104773SPascal Brand /* define mdbg 'malloc' routine and redirect std apis to these. */
52b0104773SPascal Brand void *mdbg_malloc(const char *fname, int lineno, unsigned nbytes);
53b0104773SPascal Brand void *mdbg_calloc(const char *fname, int lineno, unsigned nelem,
54b0104773SPascal Brand 		  unsigned elsize);
55b0104773SPascal Brand #ifdef MDBG_REALLOC_ENABLED
56b0104773SPascal Brand void *mdbg_realloc(const char *fname, int lineno, void *ptr, unsigned size);
57b0104773SPascal Brand #endif
58b0104773SPascal Brand 
59b0104773SPascal Brand void mdbg_free(void *fp);
60b0104773SPascal Brand void mdbg_dump(int bufdump);
61b0104773SPascal Brand enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode);
62b0104773SPascal Brand void mdbg_check(void);
63b0104773SPascal Brand 
64b0104773SPascal Brand /* Redefine standard memory allocator calls to use our routines instead. */
65b0104773SPascal Brand #define free           mdbg_free
66b0104773SPascal Brand #define malloc(x)      mdbg_malloc(__FILE__, __LINE__, (x))
67b0104773SPascal Brand #define calloc(n, e)    mdbg_calloc(__FILE__, __LINE__, (n), (e))
68b0104773SPascal Brand #define realloc(p, x)   mdbg_realloc(__FILE__, __LINE__, (p), (x))
69b0104773SPascal Brand 
70b0104773SPascal Brand #else
71b0104773SPascal Brand 
72b0104773SPascal Brand /* mdbg not enabled: simple define standard apis */
73b0104773SPascal Brand void *calloc(size_t nmemb, size_t size);
74b0104773SPascal Brand void free(void *ptr);
75b0104773SPascal Brand void *malloc(size_t size);
76b0104773SPascal Brand void *realloc(void *ptr, size_t size);
77b0104773SPascal Brand 
78b0104773SPascal Brand #define mdbg_check()        do { } while (0)
79b0104773SPascal Brand #define mdbg_dump(x)        do { } while (0)
80b0104773SPascal Brand static inline enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode)
81b0104773SPascal Brand {
82b0104773SPascal Brand 	return mode;
83b0104773SPascal Brand }
84b0104773SPascal Brand 
85b0104773SPascal Brand #endif /* ENABLE_MDBG */
86b0104773SPascal Brand 
87b0104773SPascal Brand /* other standard malloc apis */
8885411494SPascal Brand void *memalign(size_t align, size_t size);
89b0104773SPascal Brand void *valloc(size_t size);
90b0104773SPascal Brand void *pvalloc(size_t size);
91b0104773SPascal Brand 
92b0104773SPascal Brand /* entry point for malloc init in case some inits are required */
93b0104773SPascal Brand void malloc_init(void *start, size_t size);
94b0104773SPascal Brand 
95*2c276d68SPascal Brand /* get malloc stats: curr allocated heap and max allocated heap since boot */
96*2c276d68SPascal Brand void malloc_reset_max_allocated(void);
97*2c276d68SPascal Brand size_t malloc_get_max_allocated(void);
98*2c276d68SPascal Brand size_t malloc_get_allocated(void);
99*2c276d68SPascal Brand size_t malloc_get_heap_size(void);
100*2c276d68SPascal Brand 
101b0104773SPascal Brand #endif /* MALLOC_H */
102