1*b0104773SPascal Brand /* 2*b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V. 3*b0104773SPascal Brand * All rights reserved. 4*b0104773SPascal Brand * 5*b0104773SPascal Brand * Redistribution and use in source and binary forms, with or without 6*b0104773SPascal Brand * modification, are permitted provided that the following conditions are met: 7*b0104773SPascal Brand * 8*b0104773SPascal Brand * 1. Redistributions of source code must retain the above copyright notice, 9*b0104773SPascal Brand * this list of conditions and the following disclaimer. 10*b0104773SPascal Brand * 11*b0104773SPascal Brand * 2. Redistributions in binary form must reproduce the above copyright notice, 12*b0104773SPascal Brand * this list of conditions and the following disclaimer in the documentation 13*b0104773SPascal Brand * and/or other materials provided with the distribution. 14*b0104773SPascal Brand * 15*b0104773SPascal Brand * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16*b0104773SPascal Brand * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*b0104773SPascal Brand * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*b0104773SPascal Brand * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19*b0104773SPascal Brand * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20*b0104773SPascal Brand * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21*b0104773SPascal Brand * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22*b0104773SPascal Brand * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23*b0104773SPascal Brand * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24*b0104773SPascal Brand * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25*b0104773SPascal Brand * POSSIBILITY OF SUCH DAMAGE. 26*b0104773SPascal Brand */ 27*b0104773SPascal Brand #ifndef MALLOC_H 28*b0104773SPascal Brand #define MALLOC_H 29*b0104773SPascal Brand 30*b0104773SPascal Brand #include <stddef.h> 31*b0104773SPascal Brand 32*b0104773SPascal Brand enum mdbg_mode { 33*b0104773SPascal Brand MDBG_MODE_STATIC, 34*b0104773SPascal Brand MDBG_MODE_DYNAMIC 35*b0104773SPascal Brand }; 36*b0104773SPascal Brand 37*b0104773SPascal Brand /* 38*b0104773SPascal Brand * ENABLE_MDBG: malloc debug support 39*b0104773SPascal Brand * 40*b0104773SPascal Brand * When enabled, malloc, calloc, realloc and free are redirected from SLA 41*b0104773SPascal Brand * to routines that trace memory callers (filename/line) and provide few other 42*b0104773SPascal Brand * memory alloc debug features. calloc and realloc are routed to basic malloc. 43*b0104773SPascal Brand * 44*b0104773SPascal Brand * memalign and other standard mem alloc APIs are not handled by mdbg. 45*b0104773SPascal Brand * 46*b0104773SPascal Brand * If ENABLE_MDBG is not set, malloc.c acts as a wrapper to redirect std 47*b0104773SPascal Brand * malloc apis to the apis of the embedded malloc library (SLA, dlmalloc,...). 48*b0104773SPascal Brand */ 49*b0104773SPascal Brand #ifdef ENABLE_MDBG 50*b0104773SPascal Brand 51*b0104773SPascal Brand /* define mdbg 'malloc' routine and redirect std apis to these. */ 52*b0104773SPascal Brand void *mdbg_malloc(const char *fname, int lineno, unsigned nbytes); 53*b0104773SPascal Brand void *mdbg_calloc(const char *fname, int lineno, unsigned nelem, 54*b0104773SPascal Brand unsigned elsize); 55*b0104773SPascal Brand #ifdef MDBG_REALLOC_ENABLED 56*b0104773SPascal Brand void *mdbg_realloc(const char *fname, int lineno, void *ptr, unsigned size); 57*b0104773SPascal Brand #endif 58*b0104773SPascal Brand 59*b0104773SPascal Brand void mdbg_free(void *fp); 60*b0104773SPascal Brand void mdbg_dump(int bufdump); 61*b0104773SPascal Brand enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode); 62*b0104773SPascal Brand void mdbg_check(void); 63*b0104773SPascal Brand 64*b0104773SPascal Brand /* Redefine standard memory allocator calls to use our routines instead. */ 65*b0104773SPascal Brand #define free mdbg_free 66*b0104773SPascal Brand #define malloc(x) mdbg_malloc(__FILE__, __LINE__, (x)) 67*b0104773SPascal Brand #define calloc(n, e) mdbg_calloc(__FILE__, __LINE__, (n), (e)) 68*b0104773SPascal Brand #define realloc(p, x) mdbg_realloc(__FILE__, __LINE__, (p), (x)) 69*b0104773SPascal Brand 70*b0104773SPascal Brand #else 71*b0104773SPascal Brand 72*b0104773SPascal Brand /* mdbg not enabled: simple define standard apis */ 73*b0104773SPascal Brand void *calloc(size_t nmemb, size_t size); 74*b0104773SPascal Brand void free(void *ptr); 75*b0104773SPascal Brand void *malloc(size_t size); 76*b0104773SPascal Brand void *realloc(void *ptr, size_t size); 77*b0104773SPascal Brand 78*b0104773SPascal Brand #define mdbg_check() do { } while (0) 79*b0104773SPascal Brand #define mdbg_dump(x) do { } while (0) 80*b0104773SPascal Brand static inline enum mdbg_mode mdbg_set_mode(enum mdbg_mode mode) 81*b0104773SPascal Brand { 82*b0104773SPascal Brand return mode; 83*b0104773SPascal Brand } 84*b0104773SPascal Brand 85*b0104773SPascal Brand #endif /* ENABLE_MDBG */ 86*b0104773SPascal Brand 87*b0104773SPascal Brand /* other standard malloc apis */ 88*b0104773SPascal Brand void *memalign(size_t size, size_t a); 89*b0104773SPascal Brand void *valloc(size_t size); 90*b0104773SPascal Brand void *pvalloc(size_t size); 91*b0104773SPascal Brand 92*b0104773SPascal Brand /* entry point for malloc init in case some inits are required */ 93*b0104773SPascal Brand void malloc_init(void *start, size_t size); 94*b0104773SPascal Brand 95*b0104773SPascal Brand #endif /* MALLOC_H */ 96