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