1*5f2eab64SJohn Marino /* 2*5f2eab64SJohn Marino xmalloc.h - Simple malloc debugging library API 3*5f2eab64SJohn Marino 4*5f2eab64SJohn Marino This software is released under a BSD-style license. 5*5f2eab64SJohn Marino See the file LICENSE for details and copyright. 6*5f2eab64SJohn Marino 7*5f2eab64SJohn Marino */ 8*5f2eab64SJohn Marino 9*5f2eab64SJohn Marino #ifndef _XMALLOC_H 10*5f2eab64SJohn Marino #define _XMALLOC_H 1 11*5f2eab64SJohn Marino 12*5f2eab64SJohn Marino void *xmalloc_impl(size_t size, const char *file, int line, const char *func); 13*5f2eab64SJohn Marino void *xcalloc_impl(size_t nmemb, size_t size, const char *file, int line, 14*5f2eab64SJohn Marino const char *func); 15*5f2eab64SJohn Marino void xfree_impl(void *ptr, const char *file, int line, const char *func); 16*5f2eab64SJohn Marino void *xrealloc_impl(void *ptr, size_t new_size, const char *file, int line, 17*5f2eab64SJohn Marino const char *func); 18*5f2eab64SJohn Marino int xmalloc_dump_leaks(void); 19*5f2eab64SJohn Marino void xmalloc_configure(int fail_after); 20*5f2eab64SJohn Marino 21*5f2eab64SJohn Marino 22*5f2eab64SJohn Marino #ifndef XMALLOC_INTERNAL 23*5f2eab64SJohn Marino #ifdef MALLOC_DEBUGGING 24*5f2eab64SJohn Marino 25*5f2eab64SJohn Marino /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__' 26*5f2eab64SJohn Marino which contains the name of the function currently being defined. 27*5f2eab64SJohn Marino # define __XMALLOC_FUNCTION __PRETTY_FUNCTION__ 28*5f2eab64SJohn Marino This is broken in G++ before version 2.6. 29*5f2eab64SJohn Marino C9x has a similar variable called __func__, but prefer the GCC one since 30*5f2eab64SJohn Marino it demangles C++ function names. */ 31*5f2eab64SJohn Marino # ifdef __GNUC__ 32*5f2eab64SJohn Marino # if __GNUC__ > 2 || (__GNUC__ == 2 \ 33*5f2eab64SJohn Marino && __GNUC_MINOR__ >= (defined __cplusplus ? 6 : 4)) 34*5f2eab64SJohn Marino # define __XMALLOC_FUNCTION __PRETTY_FUNCTION__ 35*5f2eab64SJohn Marino # else 36*5f2eab64SJohn Marino # define __XMALLOC_FUNCTION ((const char *) 0) 37*5f2eab64SJohn Marino # endif 38*5f2eab64SJohn Marino # else 39*5f2eab64SJohn Marino # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L 40*5f2eab64SJohn Marino # define __XMALLOC_FUNCTION __func__ 41*5f2eab64SJohn Marino # else 42*5f2eab64SJohn Marino # define __XMALLOC_FUNCTION ((const char *) 0) 43*5f2eab64SJohn Marino # endif 44*5f2eab64SJohn Marino # endif 45*5f2eab64SJohn Marino 46*5f2eab64SJohn Marino #define xmalloc(size) xmalloc_impl(size, __FILE__, __LINE__, \ 47*5f2eab64SJohn Marino __XMALLOC_FUNCTION) 48*5f2eab64SJohn Marino #define xcalloc(nmemb, size) xcalloc_impl(nmemb, size, __FILE__, __LINE__, \ 49*5f2eab64SJohn Marino __XMALLOC_FUNCTION) 50*5f2eab64SJohn Marino #define xfree(ptr) xfree_impl(ptr, __FILE__, __LINE__, __XMALLOC_FUNCTION) 51*5f2eab64SJohn Marino #define xrealloc(ptr, new_size) xrealloc_impl(ptr, new_size, __FILE__, \ 52*5f2eab64SJohn Marino __LINE__, __XMALLOC_FUNCTION) 53*5f2eab64SJohn Marino #undef malloc 54*5f2eab64SJohn Marino #undef calloc 55*5f2eab64SJohn Marino #undef free 56*5f2eab64SJohn Marino #undef realloc 57*5f2eab64SJohn Marino 58*5f2eab64SJohn Marino #define malloc USE_XMALLOC_INSTEAD_OF_MALLOC 59*5f2eab64SJohn Marino #define calloc USE_XCALLOC_INSTEAD_OF_CALLOC 60*5f2eab64SJohn Marino #define free USE_XFREE_INSTEAD_OF_FREE 61*5f2eab64SJohn Marino #define realloc USE_XREALLOC_INSTEAD_OF_REALLOC 62*5f2eab64SJohn Marino 63*5f2eab64SJohn Marino #else /* !MALLOC_DEBUGGING */ 64*5f2eab64SJohn Marino 65*5f2eab64SJohn Marino #include <stdlib.h> 66*5f2eab64SJohn Marino 67*5f2eab64SJohn Marino #define xmalloc(size) malloc(size) 68*5f2eab64SJohn Marino #define xcalloc(nmemb, size) calloc(nmemb, size) 69*5f2eab64SJohn Marino #define xfree(ptr) free(ptr) 70*5f2eab64SJohn Marino #define xrealloc(ptr, new_size) realloc(ptr, new_size) 71*5f2eab64SJohn Marino 72*5f2eab64SJohn Marino #endif /* !MALLOC_DEBUGGING */ 73*5f2eab64SJohn Marino #endif /* !XMALLOC_INTERNAL */ 74*5f2eab64SJohn Marino 75*5f2eab64SJohn Marino #endif /* _XMALLOC_H */ 76*5f2eab64SJohn Marino 77*5f2eab64SJohn Marino /* EOF */ 78