11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */ 2b0104773SPascal Brand /* 3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 4b0104773SPascal Brand */ 57eaed3a3SEtienne Carriere #ifndef __MALLOC_H 67eaed3a3SEtienne Carriere #define __MALLOC_H 7b0104773SPascal Brand 8b0104773SPascal Brand #include <stddef.h> 9074ba9b2SJens Wiklander #include <types_ext.h> 10b0104773SPascal Brand 11c46bd3e1SEtienne Carriere /* 12c46bd3e1SEtienne Carriere * Due to bget implementation, the first memory pool registered shall have 13c46bd3e1SEtienne Carriere * a min size. Choose 1kB which is reasonable. 14c46bd3e1SEtienne Carriere */ 15c46bd3e1SEtienne Carriere #define MALLOC_INITIAL_POOL_MIN_SIZE 1024 16c46bd3e1SEtienne Carriere 17eacabbbcSJerome Forissier void *malloc(size_t size); 18eacabbbcSJerome Forissier void *calloc(size_t nmemb, size_t size); 19eacabbbcSJerome Forissier void *realloc(void *ptr, size_t size); 20eacabbbcSJerome Forissier void *memalign(size_t alignment, size_t size); 210fcbddd4SSY Chiu void free(void *ptr); 220fcbddd4SSY Chiu 23bc8fe8c2SEtienne Carriere #if __STDC_VERSION__ >= 201112L 24bc8fe8c2SEtienne Carriere void *aligned_alloc(size_t alignment, size_t size); 25bc8fe8c2SEtienne Carriere #endif 26bc8fe8c2SEtienne Carriere 27b0104773SPascal Brand #ifdef ENABLE_MDBG 28b0104773SPascal Brand 29074ba9b2SJens Wiklander void *mdbg_malloc(const char *fname, int lineno, size_t size); 30074ba9b2SJens Wiklander void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 31074ba9b2SJens Wiklander void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 323f286c3bSJens Wiklander void *mdbg_memalign(const char *fname, int lineno, size_t alignment, 333f286c3bSJens Wiklander size_t size); 34b0104773SPascal Brand 35bc8fe8c2SEtienne Carriere #if __STDC_VERSION__ >= 201112L 36bc8fe8c2SEtienne Carriere void *mdbg_aligned_alloc(const char *fname, int lineno, size_t alignment, 37bc8fe8c2SEtienne Carriere size_t size); 38bc8fe8c2SEtienne Carriere #endif 39bc8fe8c2SEtienne Carriere 40074ba9b2SJens Wiklander void mdbg_check(int bufdump); 41b0104773SPascal Brand 42074ba9b2SJens Wiklander #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 43074ba9b2SJens Wiklander #define calloc(nmemb, size) \ 44074ba9b2SJens Wiklander mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 45074ba9b2SJens Wiklander #define realloc(ptr, size) \ 46074ba9b2SJens Wiklander mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 473f286c3bSJens Wiklander #define memalign(alignment, size) \ 483f286c3bSJens Wiklander mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 49b0104773SPascal Brand 50bc8fe8c2SEtienne Carriere #if __STDC_VERSION__ >= 201112L 51bc8fe8c2SEtienne Carriere #define aligned_alloc(alignment, size) \ 52bc8fe8c2SEtienne Carriere mdbg_aligned_alloc(__FILE__, __LINE__, (alignment), (size)) 53bc8fe8c2SEtienne Carriere #endif /* __STDC_VERSION__ */ 54bc8fe8c2SEtienne Carriere 55b0104773SPascal Brand #else 56b0104773SPascal Brand 57074ba9b2SJens Wiklander #define mdbg_check(x) do { } while (0) 58b0104773SPascal Brand 59074ba9b2SJens Wiklander #endif 60b0104773SPascal Brand 61074ba9b2SJens Wiklander /* 62074ba9b2SJens Wiklander * Returns true if the supplied memory area is within a buffer 63074ba9b2SJens Wiklander * previously allocated (and not freed yet). 64074ba9b2SJens Wiklander * 65074ba9b2SJens Wiklander * Used internally by TAs 66074ba9b2SJens Wiklander */ 67074ba9b2SJens Wiklander bool malloc_buffer_is_within_alloced(void *buf, size_t len); 68074ba9b2SJens Wiklander 69074ba9b2SJens Wiklander /* 70074ba9b2SJens Wiklander * Returns true if the supplied memory area is overlapping the area used 71074ba9b2SJens Wiklander * for heap. 72074ba9b2SJens Wiklander * 73074ba9b2SJens Wiklander * Used internally by TAs 74074ba9b2SJens Wiklander */ 75074ba9b2SJens Wiklander bool malloc_buffer_overlaps_heap(void *buf, size_t len); 76074ba9b2SJens Wiklander 77074ba9b2SJens Wiklander /* 78945694b9SPascal Brand * Adds a pool of memory to allocate from. 79074ba9b2SJens Wiklander */ 80074ba9b2SJens Wiklander void malloc_add_pool(void *buf, size_t len); 81b0104773SPascal Brand 8252f8b816SJerome Forissier #ifdef CFG_WITH_STATS 8352f8b816SJerome Forissier /* 8452f8b816SJerome Forissier * Get/reset allocation statistics 8552f8b816SJerome Forissier */ 8652f8b816SJerome Forissier 8752f8b816SJerome Forissier #define TEE_ALLOCATOR_DESC_LENGTH 32 8852f8b816SJerome Forissier struct malloc_stats { 8952f8b816SJerome Forissier char desc[TEE_ALLOCATOR_DESC_LENGTH]; 9052f8b816SJerome Forissier uint32_t allocated; /* Bytes currently allocated */ 9152f8b816SJerome Forissier uint32_t max_allocated; /* Tracks max value of allocated */ 9252f8b816SJerome Forissier uint32_t size; /* Total size for this allocator */ 9352f8b816SJerome Forissier uint32_t num_alloc_fail; /* Number of failed alloc requests */ 9452f8b816SJerome Forissier uint32_t biggest_alloc_fail; /* Size of biggest failed alloc */ 9552f8b816SJerome Forissier uint32_t biggest_alloc_fail_used; /* Alloc bytes when above occurred */ 9652f8b816SJerome Forissier }; 9752f8b816SJerome Forissier 9852f8b816SJerome Forissier void malloc_get_stats(struct malloc_stats *stats); 9952f8b816SJerome Forissier void malloc_reset_stats(void); 10052f8b816SJerome Forissier #endif /* CFG_WITH_STATS */ 1012c276d68SPascal Brand 102386fc264SVolodymyr Babchuk 103*b76b2296SJerome Forissier #ifdef CFG_NS_VIRTUALIZATION 104386fc264SVolodymyr Babchuk 105386fc264SVolodymyr Babchuk void nex_free(void *ptr); 106386fc264SVolodymyr Babchuk 107386fc264SVolodymyr Babchuk #ifdef ENABLE_MDBG 108386fc264SVolodymyr Babchuk 109386fc264SVolodymyr Babchuk void *nex_mdbg_malloc(const char *fname, int lineno, size_t size); 110386fc264SVolodymyr Babchuk void *nex_mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 111386fc264SVolodymyr Babchuk void *nex_mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 1123f286c3bSJens Wiklander void *nex_mdbg_memalign(const char *fname, int lineno, size_t alignment, 1133f286c3bSJens Wiklander size_t size); 114386fc264SVolodymyr Babchuk 115386fc264SVolodymyr Babchuk void nex_mdbg_check(int bufdump); 116386fc264SVolodymyr Babchuk 117386fc264SVolodymyr Babchuk #define nex_malloc(size) nex_mdbg_malloc(__FILE__, __LINE__, (size)) 118386fc264SVolodymyr Babchuk #define nex_calloc(nmemb, size) \ 119386fc264SVolodymyr Babchuk nex_mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 120386fc264SVolodymyr Babchuk #define nex_realloc(ptr, size) \ 121386fc264SVolodymyr Babchuk nex_mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 1223f286c3bSJens Wiklander #define nex_memalign(alignment, size) \ 1233f286c3bSJens Wiklander nex_mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 124386fc264SVolodymyr Babchuk 125386fc264SVolodymyr Babchuk #else /* ENABLE_MDBG */ 126386fc264SVolodymyr Babchuk 127386fc264SVolodymyr Babchuk void *nex_malloc(size_t size); 128386fc264SVolodymyr Babchuk void *nex_calloc(size_t nmemb, size_t size); 129386fc264SVolodymyr Babchuk void *nex_realloc(void *ptr, size_t size); 1303f286c3bSJens Wiklander void *nex_memalign(size_t alignment, size_t size); 131386fc264SVolodymyr Babchuk 132386fc264SVolodymyr Babchuk #define nex_mdbg_check(x) do { } while (0) 133386fc264SVolodymyr Babchuk 134386fc264SVolodymyr Babchuk #endif /* ENABLE_MDBG */ 135386fc264SVolodymyr Babchuk 136386fc264SVolodymyr Babchuk bool nex_malloc_buffer_is_within_alloced(void *buf, size_t len); 137386fc264SVolodymyr Babchuk bool nex_malloc_buffer_overlaps_heap(void *buf, size_t len); 138386fc264SVolodymyr Babchuk void nex_malloc_add_pool(void *buf, size_t len); 139386fc264SVolodymyr Babchuk 140386fc264SVolodymyr Babchuk #ifdef CFG_WITH_STATS 141386fc264SVolodymyr Babchuk /* 142386fc264SVolodymyr Babchuk * Get/reset allocation statistics 143386fc264SVolodymyr Babchuk */ 144386fc264SVolodymyr Babchuk 145386fc264SVolodymyr Babchuk void nex_malloc_get_stats(struct malloc_stats *stats); 146386fc264SVolodymyr Babchuk void nex_malloc_reset_stats(void); 147386fc264SVolodymyr Babchuk 148386fc264SVolodymyr Babchuk #endif /* CFG_WITH_STATS */ 149*b76b2296SJerome Forissier #else /* CFG_NS_VIRTUALIZATION */ 150386fc264SVolodymyr Babchuk 151386fc264SVolodymyr Babchuk #define nex_free(ptr) free(ptr) 152386fc264SVolodymyr Babchuk #define nex_malloc(size) malloc(size) 153386fc264SVolodymyr Babchuk #define nex_calloc(nmemb, size) calloc(nmemb, size) 154386fc264SVolodymyr Babchuk #define nex_realloc(ptr, size) realloc(ptr, size) 1553f286c3bSJens Wiklander #define nex_memalign(alignment, size) memalign(alignment, size) 156386fc264SVolodymyr Babchuk 157*b76b2296SJerome Forissier #endif /* CFG_NS_VIRTUALIZATION */ 158386fc264SVolodymyr Babchuk 159680bc37cSJens Wiklander struct malloc_ctx; 160680bc37cSJens Wiklander void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment, 161680bc37cSJens Wiklander size_t pl_size, struct malloc_ctx *ctx); 162680bc37cSJens Wiklander void *raw_malloc(size_t hdr_size, size_t ftr_size, size_t pl_size, 163680bc37cSJens Wiklander struct malloc_ctx *ctx); 164680bc37cSJens Wiklander void raw_free(void *ptr, struct malloc_ctx *ctx, bool wipe); 165680bc37cSJens Wiklander void *raw_calloc(size_t hdr_size, size_t ftr_size, size_t pl_nmemb, 166680bc37cSJens Wiklander size_t pl_size, struct malloc_ctx *ctx); 167680bc37cSJens Wiklander void *raw_realloc(void *ptr, size_t hdr_size, size_t ftr_size, 168680bc37cSJens Wiklander size_t pl_size, struct malloc_ctx *ctx); 169680bc37cSJens Wiklander size_t raw_malloc_get_ctx_size(void); 170680bc37cSJens Wiklander void raw_malloc_init_ctx(struct malloc_ctx *ctx); 171680bc37cSJens Wiklander void raw_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len); 17212d739bdSJens Wiklander bool raw_malloc_buffer_overlaps_heap(struct malloc_ctx *ctx, 17312d739bdSJens Wiklander void *buf, size_t len); 17412d739bdSJens Wiklander bool raw_malloc_buffer_is_within_alloced(struct malloc_ctx *ctx, 17512d739bdSJens Wiklander void *buf, size_t len); 176a51d45b5SJens Wiklander #ifdef CFG_WITH_STATS 177680bc37cSJens Wiklander void raw_malloc_get_stats(struct malloc_ctx *ctx, struct malloc_stats *stats); 178a51d45b5SJens Wiklander #endif 179680bc37cSJens Wiklander 1807eaed3a3SEtienne Carriere #endif /* __MALLOC_H */ 181