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 #ifdef ENABLE_MDBG 39 40 void *mdbg_malloc(const char *fname, int lineno, size_t size); 41 void mdbg_free(void *ptr); 42 void *mdbg_calloc(const char *fname, int lineno, size_t nmemb, size_t size); 43 void *mdbg_realloc(const char *fname, int lineno, void *ptr, size_t size); 44 void *mdbg_memalign(const char *fname, int lineno, size_t alignment, 45 size_t size); 46 47 enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode); 48 void mdbg_check(int bufdump); 49 50 #define malloc(size) mdbg_malloc(__FILE__, __LINE__, (size)) 51 #define free(ptr) mdbg_free((ptr)) 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 free(void *ptr); 63 void *calloc(size_t nmemb, size_t size); 64 void *realloc(void *ptr, size_t size); 65 void *memalign(size_t alignment, size_t size); 66 67 #define mdbg_check(x) do { } while (0) 68 static inline enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode) 69 { 70 return mode; 71 } 72 73 #endif 74 75 76 /* 77 * Returns true if the supplied memory area is within a buffer 78 * previously allocated (and not freed yet). 79 * 80 * Used internally by TAs 81 */ 82 bool malloc_buffer_is_within_alloced(void *buf, size_t len); 83 84 /* 85 * Returns true if the supplied memory area is overlapping the area used 86 * for heap. 87 * 88 * Used internally by TAs 89 */ 90 bool malloc_buffer_overlaps_heap(void *buf, size_t len); 91 92 /* 93 * Sets an initial pool of memory to allocate from, must only be called 94 * once. 95 */ 96 void malloc_init(void *buf, size_t len); 97 98 /* 99 * Adds a pool of memory to allocate from. If malloc_init hasn't been 100 * called before this function it will do a malloc_init internally. 101 */ 102 void malloc_add_pool(void *buf, size_t len); 103 104 /* get malloc stats: curr allocated heap and max allocated heap since boot */ 105 void malloc_reset_max_allocated(void); 106 size_t malloc_get_max_allocated(void); 107 size_t malloc_get_allocated(void); 108 size_t malloc_get_heap_size(void); 109 110 #endif /* MALLOC_H */ 111