4 xmalloc.h - Simple malloc debugging library API
6 This software is released under a BSD-style license.
7 See the file LICENSE for details and copyright.
14 void *xmalloc_impl(size_t size, const char *file, int line, const char *func);
15 void *xcalloc_impl(size_t nmemb, size_t size, const char *file, int line,
17 void xfree_impl(void *ptr, const char *file, int line, const char *func);
18 void *xrealloc_impl(void *ptr, size_t new_size, const char *file, int line,
20 int xmalloc_dump_leaks(void);
21 void xmalloc_configure(int fail_after);
24 #ifndef XMALLOC_INTERNAL
25 #ifdef MALLOC_DEBUGGING
27 /* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
28 which contains the name of the function currently being defined.
29 # define __XMALLOC_FUNCTION __PRETTY_FUNCTION__
30 This is broken in G++ before version 2.6.
31 C9x has a similar variable called __func__, but prefer the GCC one since
32 it demangles C++ function names. */
34 # if __GNUC__ > 2 || (__GNUC__ == 2 \
35 && __GNUC_MINOR__ >= (defined __cplusplus ? 6 : 4))
36 # define __XMALLOC_FUNCTION __PRETTY_FUNCTION__
38 # define __XMALLOC_FUNCTION ((const char *) 0)
41 # if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
42 # define __XMALLOC_FUNCTION __func__
44 # define __XMALLOC_FUNCTION ((const char *) 0)
48 #define xmalloc(size) xmalloc_impl(size, __FILE__, __LINE__, \
50 #define xcalloc(nmemb, size) xcalloc_impl(nmemb, size, __FILE__, __LINE__, \
52 #define xfree(ptr) xfree_impl(ptr, __FILE__, __LINE__, __XMALLOC_FUNCTION)
53 #define xrealloc(ptr, new_size) xrealloc_impl(ptr, new_size, __FILE__, \
54 __LINE__, __XMALLOC_FUNCTION)
60 #define malloc USE_XMALLOC_INSTEAD_OF_MALLOC
61 #define calloc USE_XCALLOC_INSTEAD_OF_CALLOC
62 #define free USE_XFREE_INSTEAD_OF_FREE
63 #define realloc USE_XREALLOC_INSTEAD_OF_REALLOC
65 #else /* !MALLOC_DEBUGGING */
69 #define xmalloc(size) malloc(size)
70 #define xcalloc(nmemb, size) calloc(nmemb, size)
71 #define xfree(ptr) free(ptr)
72 #define xrealloc(ptr, new_size) realloc(ptr, new_size)
74 #endif /* !MALLOC_DEBUGGING */
75 #endif /* !XMALLOC_INTERNAL */
77 #endif /* _XMALLOC_H */