xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 52f8b816aab0eb226192752a93c4a8ec6b44c41d)
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>
31074ba9b2SJens Wiklander #include <types_ext.h>
32b0104773SPascal Brand 
331d171f95SJens Wiklander extern struct mutex __malloc_mu;
341d171f95SJens Wiklander 
350fcbddd4SSY Chiu void free(void *ptr);
360fcbddd4SSY Chiu 
37b0104773SPascal Brand #ifdef ENABLE_MDBG
38b0104773SPascal Brand 
39074ba9b2SJens Wiklander void *mdbg_malloc(const char *fname, int lineno, size_t size);
40074ba9b2SJens Wiklander void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
41074ba9b2SJens Wiklander void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
42074ba9b2SJens Wiklander void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
43074ba9b2SJens Wiklander 		size_t size);
44b0104773SPascal Brand 
45074ba9b2SJens Wiklander void mdbg_check(int bufdump);
46b0104773SPascal Brand 
47074ba9b2SJens Wiklander #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
48074ba9b2SJens Wiklander #define calloc(nmemb, size) \
49074ba9b2SJens Wiklander 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
50074ba9b2SJens Wiklander #define realloc(ptr, size) \
51074ba9b2SJens Wiklander 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
52074ba9b2SJens Wiklander #define memalign(alignment, size) \
53074ba9b2SJens Wiklander 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
54b0104773SPascal Brand 
55b0104773SPascal Brand #else
56b0104773SPascal Brand 
57b0104773SPascal Brand void *malloc(size_t size);
58074ba9b2SJens Wiklander void *calloc(size_t nmemb, size_t size);
59b0104773SPascal Brand void *realloc(void *ptr, size_t size);
60074ba9b2SJens Wiklander void *memalign(size_t alignment, size_t size);
61b0104773SPascal Brand 
62074ba9b2SJens Wiklander #define mdbg_check(x)        do { } while (0)
63b0104773SPascal Brand 
64074ba9b2SJens Wiklander #endif
65b0104773SPascal Brand 
66b0104773SPascal Brand 
67074ba9b2SJens Wiklander /*
68074ba9b2SJens Wiklander  * Returns true if the supplied memory area is within a buffer
69074ba9b2SJens Wiklander  * previously allocated (and not freed yet).
70074ba9b2SJens Wiklander  *
71074ba9b2SJens Wiklander  * Used internally by TAs
72074ba9b2SJens Wiklander  */
73074ba9b2SJens Wiklander bool malloc_buffer_is_within_alloced(void *buf, size_t len);
74074ba9b2SJens Wiklander 
75074ba9b2SJens Wiklander /*
76074ba9b2SJens Wiklander  * Returns true if the supplied memory area is overlapping the area used
77074ba9b2SJens Wiklander  * for heap.
78074ba9b2SJens Wiklander  *
79074ba9b2SJens Wiklander  * Used internally by TAs
80074ba9b2SJens Wiklander  */
81074ba9b2SJens Wiklander bool malloc_buffer_overlaps_heap(void *buf, size_t len);
82074ba9b2SJens Wiklander 
83074ba9b2SJens Wiklander /*
84945694b9SPascal Brand  * Adds a pool of memory to allocate from.
85074ba9b2SJens Wiklander  */
86074ba9b2SJens Wiklander void malloc_add_pool(void *buf, size_t len);
87b0104773SPascal Brand 
88*52f8b816SJerome Forissier #ifdef CFG_WITH_STATS
89*52f8b816SJerome Forissier /*
90*52f8b816SJerome Forissier  * Get/reset allocation statistics
91*52f8b816SJerome Forissier  */
92*52f8b816SJerome Forissier 
93*52f8b816SJerome Forissier #define TEE_ALLOCATOR_DESC_LENGTH 32
94*52f8b816SJerome Forissier struct malloc_stats {
95*52f8b816SJerome Forissier 	char desc[TEE_ALLOCATOR_DESC_LENGTH];
96*52f8b816SJerome Forissier 	uint32_t allocated;               /* Bytes currently allocated */
97*52f8b816SJerome Forissier 	uint32_t max_allocated;           /* Tracks max value of allocated */
98*52f8b816SJerome Forissier 	uint32_t size;                    /* Total size for this allocator */
99*52f8b816SJerome Forissier 	uint32_t num_alloc_fail;          /* Number of failed alloc requests */
100*52f8b816SJerome Forissier 	uint32_t biggest_alloc_fail;      /* Size of biggest failed alloc */
101*52f8b816SJerome Forissier 	uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */
102*52f8b816SJerome Forissier };
103*52f8b816SJerome Forissier 
104*52f8b816SJerome Forissier void malloc_get_stats(struct malloc_stats *stats);
105*52f8b816SJerome Forissier void malloc_reset_stats(void);
106*52f8b816SJerome Forissier #endif /* CFG_WITH_STATS */
1072c276d68SPascal Brand 
108b0104773SPascal Brand #endif /* MALLOC_H */
109