1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef MALLOC_H 28 #define MALLOC_H 29 30 #include <stddef.h> 31 #include <types_ext.h> 32 33 enum mdbg_mode { 34 MDBG_MODE_STATIC, 35 MDBG_MODE_DYNAMIC 36 }; 37 38 void free(void *ptr); 39 40 #ifdef ENABLE_MDBG 41 42 void *mdbg_malloc(const char *fname, int lineno, size_t size); 43 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 44 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 45 void *mdbg_memalign(const char *fname, int lineno, size_t alignment, 46 size_t size); 47 48 enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode); 49 void mdbg_check(int bufdump); 50 51 #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 52 #define calloc(nmemb, size) \ 53 mdbg_calloc(__FILE__, __LINE__, (nmemb), (size)) 54 #define realloc(ptr, size) \ 55 mdbg_realloc(__FILE__, __LINE__, (ptr), (size)) 56 #define memalign(alignment, size) \ 57 mdbg_memalign(__FILE__, __LINE__, (alignment), (size)) 58 59 #else 60 61 void *malloc(size_t size); 62 void *calloc(size_t nmemb, size_t size); 63 void *realloc(void *ptr, size_t size); 64 void *memalign(size_t alignment, size_t size); 65 66 #define mdbg_check(x) do { } while (0) 67 static inline enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode) 68 { 69 return mode; 70 } 71 72 #endif 73 74 75 /* 76 * Returns true if the supplied memory area is within a buffer 77 * previously allocated (and not freed yet). 78 * 79 * Used internally by TAs 80 */ 81 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 82 83 /* 84 * Returns true if the supplied memory area is overlapping the area used 85 * for heap. 86 * 87 * Used internally by TAs 88 */ 89 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 90 91 /* 92 * Sets an initial pool of memory to allocate from, must only be called 93 * once. 94 */ 95 void malloc_init(void *buf, size_t len); 96 97 /* 98 * Adds a pool of memory to allocate from. If malloc_init hasn't been 99 * called before this function it will do a malloc_init internally. 100 */ 101 void malloc_add_pool(void *buf, size_t len); 102 103 /* get malloc stats: curr allocated heap and max allocated heap since boot */ 104 void malloc_reset_max_allocated(void); 105 size_t malloc_get_max_allocated(void); 106 size_t malloc_get_allocated(void); 107 size_t malloc_get_heap_size(void); 108 109 #endif /* MALLOC_H */ 110