xref: /optee_os/lib/libutils/isoc/include/malloc.h (revision 1bb929836182ecb96d2d9d268daa807c67596396)
1*1bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */
2b0104773SPascal Brand /*
3b0104773SPascal Brand  * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand  * All rights reserved.
5b0104773SPascal Brand  *
6b0104773SPascal Brand  * Redistribution and use in source and binary forms, with or without
7b0104773SPascal Brand  * modification, are permitted provided that the following conditions are met:
8b0104773SPascal Brand  *
9b0104773SPascal Brand  * 1. Redistributions of source code must retain the above copyright notice,
10b0104773SPascal Brand  * this list of conditions and the following disclaimer.
11b0104773SPascal Brand  *
12b0104773SPascal Brand  * 2. Redistributions in binary form must reproduce the above copyright notice,
13b0104773SPascal Brand  * this list of conditions and the following disclaimer in the documentation
14b0104773SPascal Brand  * and/or other materials provided with the distribution.
15b0104773SPascal Brand  *
16b0104773SPascal Brand  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17b0104773SPascal Brand  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b0104773SPascal Brand  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b0104773SPascal Brand  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20b0104773SPascal Brand  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21b0104773SPascal Brand  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22b0104773SPascal Brand  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23b0104773SPascal Brand  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24b0104773SPascal Brand  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25b0104773SPascal Brand  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26b0104773SPascal Brand  * POSSIBILITY OF SUCH DAMAGE.
27b0104773SPascal Brand  */
28b0104773SPascal Brand #ifndef MALLOC_H
29b0104773SPascal Brand #define MALLOC_H
30b0104773SPascal Brand 
31b0104773SPascal Brand #include <stddef.h>
32074ba9b2SJens Wiklander #include <types_ext.h>
33b0104773SPascal Brand 
3482c9f597SVolodymyr Babchuk extern unsigned int __malloc_spinlock;
351d171f95SJens Wiklander 
360fcbddd4SSY Chiu void free(void *ptr);
370fcbddd4SSY Chiu 
38b0104773SPascal Brand #ifdef ENABLE_MDBG
39b0104773SPascal Brand 
40074ba9b2SJens Wiklander void *mdbg_malloc(const char *fname, int lineno, size_t size);
41074ba9b2SJens Wiklander void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size);
42074ba9b2SJens Wiklander void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size);
43074ba9b2SJens Wiklander void *mdbg_memalign(const char *fname, int lineno, size_t alignment,
44074ba9b2SJens Wiklander 		size_t size);
45b0104773SPascal Brand 
46074ba9b2SJens Wiklander void mdbg_check(int bufdump);
47b0104773SPascal Brand 
48074ba9b2SJens Wiklander #define malloc(size)	mdbg_malloc(__FILE__, __LINE__, (size))
49074ba9b2SJens Wiklander #define calloc(nmemb, size) \
50074ba9b2SJens Wiklander 		mdbg_calloc(__FILE__, __LINE__, (nmemb), (size))
51074ba9b2SJens Wiklander #define realloc(ptr, size) \
52074ba9b2SJens Wiklander 		mdbg_realloc(__FILE__, __LINE__, (ptr), (size))
53074ba9b2SJens Wiklander #define memalign(alignment, size) \
54074ba9b2SJens Wiklander 		mdbg_memalign(__FILE__, __LINE__, (alignment), (size))
55b0104773SPascal Brand 
56b0104773SPascal Brand #else
57b0104773SPascal Brand 
58b0104773SPascal Brand void *malloc(size_t size);
59074ba9b2SJens Wiklander void *calloc(size_t nmemb, size_t size);
60b0104773SPascal Brand void *realloc(void *ptr, size_t size);
61074ba9b2SJens Wiklander void *memalign(size_t alignment, size_t size);
62b0104773SPascal Brand 
63074ba9b2SJens Wiklander #define mdbg_check(x)        do { } while (0)
64b0104773SPascal Brand 
65074ba9b2SJens Wiklander #endif
66b0104773SPascal Brand 
67b0104773SPascal Brand 
68074ba9b2SJens Wiklander /*
69074ba9b2SJens Wiklander  * Returns true if the supplied memory area is within a buffer
70074ba9b2SJens Wiklander  * previously allocated (and not freed yet).
71074ba9b2SJens Wiklander  *
72074ba9b2SJens Wiklander  * Used internally by TAs
73074ba9b2SJens Wiklander  */
74074ba9b2SJens Wiklander bool malloc_buffer_is_within_alloced(void *buf, size_t len);
75074ba9b2SJens Wiklander 
76074ba9b2SJens Wiklander /*
77074ba9b2SJens Wiklander  * Returns true if the supplied memory area is overlapping the area used
78074ba9b2SJens Wiklander  * for heap.
79074ba9b2SJens Wiklander  *
80074ba9b2SJens Wiklander  * Used internally by TAs
81074ba9b2SJens Wiklander  */
82074ba9b2SJens Wiklander bool malloc_buffer_overlaps_heap(void *buf, size_t len);
83074ba9b2SJens Wiklander 
84074ba9b2SJens Wiklander /*
85945694b9SPascal Brand  * Adds a pool of memory to allocate from.
86074ba9b2SJens Wiklander  */
87074ba9b2SJens Wiklander void malloc_add_pool(void *buf, size_t len);
88b0104773SPascal Brand 
8952f8b816SJerome Forissier #ifdef CFG_WITH_STATS
9052f8b816SJerome Forissier /*
9152f8b816SJerome Forissier  * Get/reset allocation statistics
9252f8b816SJerome Forissier  */
9352f8b816SJerome Forissier 
9452f8b816SJerome Forissier #define TEE_ALLOCATOR_DESC_LENGTH 32
9552f8b816SJerome Forissier struct malloc_stats {
9652f8b816SJerome Forissier 	char desc[TEE_ALLOCATOR_DESC_LENGTH];
9752f8b816SJerome Forissier 	uint32_t allocated;               /* Bytes currently allocated */
9852f8b816SJerome Forissier 	uint32_t max_allocated;           /* Tracks max value of allocated */
9952f8b816SJerome Forissier 	uint32_t size;                    /* Total size for this allocator */
10052f8b816SJerome Forissier 	uint32_t num_alloc_fail;          /* Number of failed alloc requests */
10152f8b816SJerome Forissier 	uint32_t biggest_alloc_fail;      /* Size of biggest failed alloc */
10252f8b816SJerome Forissier 	uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */
10352f8b816SJerome Forissier };
10452f8b816SJerome Forissier 
10552f8b816SJerome Forissier void malloc_get_stats(struct malloc_stats *stats);
10652f8b816SJerome Forissier void malloc_reset_stats(void);
10752f8b816SJerome Forissier #endif /* CFG_WITH_STATS */
1082c276d68SPascal Brand 
109b0104773SPascal Brand #endif /* MALLOC_H */
110