1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 #ifndef MALLOC_H 6 #define MALLOC_H 7 8 #include <stddef.h> 9 #include <types_ext.h> 10 11 void free(void *ptr); 12 13 #ifdef ENABLE_MDBG 14 15 void *mdbg_malloc(const char *fname, int lineno, size_t size); 16 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 17 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 18 19 void mdbg_check(int bufdump); 20 21 #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 22 #define calloc(nmemb, size) \ 23 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 24 #define realloc(ptr, size) \ 25 mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 26 27 #else 28 29 void *malloc(size_t size); 30 void *calloc(size_t nmemb, size_t size); 31 void *realloc(void *ptr, size_t size); 32 33 #define mdbg_check(x) do { } while (0) 34 35 #endif 36 37 38 /* 39 * Returns true if the supplied memory area is within a buffer 40 * previously allocated (and not freed yet). 41 * 42 * Used internally by TAs 43 */ 44 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 45 46 /* 47 * Returns true if the supplied memory area is overlapping the area used 48 * for heap. 49 * 50 * Used internally by TAs 51 */ 52 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 53 54 /* 55 * Adds a pool of memory to allocate from. 56 */ 57 void malloc_add_pool(void *buf, size_t len); 58 59 #ifdef CFG_WITH_STATS 60 /* 61 * Get/reset allocation statistics 62 */ 63 64 #define TEE_ALLOCATOR_DESC_LENGTH 32 65 struct malloc_stats { 66 char desc[TEE_ALLOCATOR_DESC_LENGTH]; 67 uint32_t allocated; /* Bytes currently allocated */ 68 uint32_t max_allocated; /* Tracks max value of allocated */ 69 uint32_t size; /* Total size for this allocator */ 70 uint32_t num_alloc_fail; /* Number of failed alloc requests */ 71 uint32_t biggest_alloc_fail; /* Size of biggest failed alloc */ 72 uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */ 73 }; 74 75 void malloc_get_stats(struct malloc_stats *stats); 76 void malloc_reset_stats(void); 77 #endif /* CFG_WITH_STATS */ 78 79 80 #ifdef CFG_VIRTUALIZATION 81 82 void nex_free(void *ptr); 83 84 #ifdef ENABLE_MDBG 85 86 void *nex_mdbg_malloc(const char *fname, int lineno, size_t size); 87 void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 88 void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 89 90 void nex_mdbg_check(int bufdump); 91 92 #define nex_malloc(size) nex_mdbg_malloc(__FILE__, __LINE__, (size)) 93 #define nex_calloc(nmemb, size) \ 94 nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 95 #define nex_realloc(ptr, size) \ 96 nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 97 98 #else /* ENABLE_MDBG */ 99 100 void *nex_malloc(size_t size); 101 void *nex_calloc(size_t nmemb, size_t size); 102 void *nex_realloc(void *ptr, size_t size); 103 104 #define nex_mdbg_check(x) do { } while (0) 105 106 #endif /* ENABLE_MDBG */ 107 108 bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len); 109 bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len); 110 void nex_malloc_add_pool(void *buf, size_t len); 111 112 #ifdef CFG_WITH_STATS 113 /* 114 * Get/reset allocation statistics 115 */ 116 117 void nex_malloc_get_stats(struct malloc_stats *stats); 118 void nex_malloc_reset_stats(void); 119 120 #endif /* CFG_WITH_STATS */ 121 #else /* CFG_VIRTUALIZATION */ 122 123 #define nex_free(ptr) free(ptr) 124 #define nex_malloc(size) malloc(size) 125 #define nex_calloc(nmemb, size) calloc(nmemb, size) 126 #define nex_realloc(ptr, size) realloc(ptr, size) 127 128 #endif /* CFG_VIRTUALIZATION */ 129 130 #endif /* MALLOC_H */ 131