1226035Sgabor/* $FreeBSD: releng/10.3/usr.bin/grep/regex/xmalloc.h 226035 2011-10-05 09:56:43Z gabor $ */
2226035Sgabor
3226035Sgabor/*
4226035Sgabor  xmalloc.h - Simple malloc debugging library API
5226035Sgabor
6226035Sgabor  This software is released under a BSD-style license.
7226035Sgabor  See the file LICENSE for details and copyright.
8226035Sgabor
9226035Sgabor*/
10226035Sgabor
11226035Sgabor#ifndef _XMALLOC_H
12226035Sgabor#define _XMALLOC_H 1
13226035Sgabor
14226035Sgaborvoid *xmalloc_impl(size_t size, const char *file, int line, const char *func);
15226035Sgaborvoid *xcalloc_impl(size_t nmemb, size_t size, const char *file, int line,
16226035Sgabor		   const char *func);
17226035Sgaborvoid xfree_impl(void *ptr, const char *file, int line, const char *func);
18226035Sgaborvoid *xrealloc_impl(void *ptr, size_t new_size, const char *file, int line,
19226035Sgabor		    const char *func);
20226035Sgaborint xmalloc_dump_leaks(void);
21226035Sgaborvoid xmalloc_configure(int fail_after);
22226035Sgabor
23226035Sgabor
24226035Sgabor#ifndef XMALLOC_INTERNAL
25226035Sgabor#ifdef MALLOC_DEBUGGING
26226035Sgabor
27226035Sgabor/* Version 2.4 and later of GCC define a magical variable `__PRETTY_FUNCTION__'
28226035Sgabor   which contains the name of the function currently being defined.
29226035Sgabor#  define __XMALLOC_FUNCTION	 __PRETTY_FUNCTION__
30226035Sgabor   This is broken in G++ before version 2.6.
31226035Sgabor   C9x has a similar variable called __func__, but prefer the GCC one since
32226035Sgabor   it demangles C++ function names.  */
33226035Sgabor# ifdef __GNUC__
34226035Sgabor#  if __GNUC__ > 2 || (__GNUC__ == 2 \
35226035Sgabor		       && __GNUC_MINOR__ >= (defined __cplusplus ? 6 : 4))
36226035Sgabor#   define __XMALLOC_FUNCTION	 __PRETTY_FUNCTION__
37226035Sgabor#  else
38226035Sgabor#   define __XMALLOC_FUNCTION	 ((const char *) 0)
39226035Sgabor#  endif
40226035Sgabor# else
41226035Sgabor#  if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
42226035Sgabor#   define __XMALLOC_FUNCTION	 __func__
43226035Sgabor#  else
44226035Sgabor#   define __XMALLOC_FUNCTION	 ((const char *) 0)
45226035Sgabor#  endif
46226035Sgabor# endif
47226035Sgabor
48226035Sgabor#define xmalloc(size) xmalloc_impl(size, __FILE__, __LINE__, \
49226035Sgabor				   __XMALLOC_FUNCTION)
50226035Sgabor#define xcalloc(nmemb, size) xcalloc_impl(nmemb, size, __FILE__, __LINE__, \
51226035Sgabor					  __XMALLOC_FUNCTION)
52226035Sgabor#define xfree(ptr) xfree_impl(ptr, __FILE__, __LINE__, __XMALLOC_FUNCTION)
53226035Sgabor#define xrealloc(ptr, new_size) xrealloc_impl(ptr, new_size, __FILE__, \
54226035Sgabor					      __LINE__, __XMALLOC_FUNCTION)
55226035Sgabor#undef malloc
56226035Sgabor#undef calloc
57226035Sgabor#undef free
58226035Sgabor#undef realloc
59226035Sgabor
60226035Sgabor#define malloc	USE_XMALLOC_INSTEAD_OF_MALLOC
61226035Sgabor#define calloc	USE_XCALLOC_INSTEAD_OF_CALLOC
62226035Sgabor#define free	USE_XFREE_INSTEAD_OF_FREE
63226035Sgabor#define realloc USE_XREALLOC_INSTEAD_OF_REALLOC
64226035Sgabor
65226035Sgabor#else /* !MALLOC_DEBUGGING */
66226035Sgabor
67226035Sgabor#include <stdlib.h>
68226035Sgabor
69226035Sgabor#define xmalloc(size) malloc(size)
70226035Sgabor#define xcalloc(nmemb, size) calloc(nmemb, size)
71226035Sgabor#define xfree(ptr) free(ptr)
72226035Sgabor#define xrealloc(ptr, new_size) realloc(ptr, new_size)
73226035Sgabor
74226035Sgabor#endif /* !MALLOC_DEBUGGING */
75226035Sgabor#endif /* !XMALLOC_INTERNAL */
76226035Sgabor
77226035Sgabor#endif /* _XMALLOC_H */
78226035Sgabor
79226035Sgabor/* EOF */
80