1/* $FreeBSD$ */
2
3/*
4  xmalloc.h - Simple malloc debugging library API
5
6  This software is released under a BSD-style license.
7  See the file LICENSE for details and copyright.
8
9*/
10
11#ifndef _XMALLOC_H
12#define _XMALLOC_H 1
13
14void *xmalloc_impl(size_t size, const char *file, int line, const char *func);
15void *xcalloc_impl(size_t nmemb, size_t size, const char *file, int line,
16		   const char *func);
17void xfree_impl(void *ptr, const char *file, int line, const char *func);
18void *xrealloc_impl(void *ptr, size_t new_size, const char *file, int line,
19		    const char *func);
20int xmalloc_dump_leaks(void);
21void xmalloc_configure(int fail_after);
22
23
24#ifndef XMALLOC_INTERNAL
25#ifdef MALLOC_DEBUGGING
26
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.  */
33# ifdef __GNUC__
34#  if __GNUC__ > 2 || (__GNUC__ == 2 \
35		       && __GNUC_MINOR__ >= (defined __cplusplus ? 6 : 4))
36#   define __XMALLOC_FUNCTION	 __PRETTY_FUNCTION__
37#  else
38#   define __XMALLOC_FUNCTION	 ((const char *) 0)
39#  endif
40# else
41#  if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
42#   define __XMALLOC_FUNCTION	 __func__
43#  else
44#   define __XMALLOC_FUNCTION	 ((const char *) 0)
45#  endif
46# endif
47
48#define xmalloc(size) xmalloc_impl(size, __FILE__, __LINE__, \
49				   __XMALLOC_FUNCTION)
50#define xcalloc(nmemb, size) xcalloc_impl(nmemb, size, __FILE__, __LINE__, \
51					  __XMALLOC_FUNCTION)
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)
55#undef malloc
56#undef calloc
57#undef free
58#undef realloc
59
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
64
65#else /* !MALLOC_DEBUGGING */
66
67#include <stdlib.h>
68
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)
73
74#endif /* !MALLOC_DEBUGGING */
75#endif /* !XMALLOC_INTERNAL */
76
77#endif /* _XMALLOC_H */
78
79/* EOF */
80