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 extern unsigned int __malloc_spinlock; 12 13 void free(void *ptr); 14 15 #ifdef ENABLE_MDBG 16 17 void *mdbg_malloc(const char *fname, int lineno, size_t size); 18 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 19 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 20 void *mdbg_memalign(const char *fname, int lineno, size_t alignment, 21 size_t size); 22 23 void mdbg_check(int bufdump); 24 25 #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 26 #define calloc(nmemb, size) \ 27 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 28 #define realloc(ptr, size) \ 29 mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 30 #define memalign(alignment, size) \ 31 mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 32 33 #else 34 35 void *malloc(size_t size); 36 void *calloc(size_t nmemb, size_t size); 37 void *realloc(void *ptr, size_t size); 38 void *memalign(size_t alignment, size_t size); 39 40 #define mdbg_check(x) do { } while (0) 41 42 #endif 43 44 45 /* 46 * Returns true if the supplied memory area is within a buffer 47 * previously allocated (and not freed yet). 48 * 49 * Used internally by TAs 50 */ 51 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 52 53 /* 54 * Returns true if the supplied memory area is overlapping the area used 55 * for heap. 56 * 57 * Used internally by TAs 58 */ 59 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 60 61 /* 62 * Adds a pool of memory to allocate from. 63 */ 64 void malloc_add_pool(void *buf, size_t len); 65 66 #ifdef CFG_WITH_STATS 67 /* 68 * Get/reset allocation statistics 69 */ 70 71 #define TEE_ALLOCATOR_DESC_LENGTH 32 72 struct malloc_stats { 73 char desc[TEE_ALLOCATOR_DESC_LENGTH]; 74 uint32_t allocated; /* Bytes currently allocated */ 75 uint32_t max_allocated; /* Tracks max value of allocated */ 76 uint32_t size; /* Total size for this allocator */ 77 uint32_t num_alloc_fail; /* Number of failed alloc requests */ 78 uint32_t biggest_alloc_fail; /* Size of biggest failed alloc */ 79 uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */ 80 }; 81 82 void malloc_get_stats(struct malloc_stats *stats); 83 void malloc_reset_stats(void); 84 #endif /* CFG_WITH_STATS */ 85 86 #endif /* MALLOC_H */ 87