summaryrefslogtreecommitdiff
path: root/libs/pbd/dmalloc.cc
blob: 0e730946c8586283fba3b59cb3c6cf3868619bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * file that facilitates C++ program debugging.
 *
 * Copyright 1995 by Gray Watson
 *
 * This file is part of the dmalloc package.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * NON-COMMERCIAL purpose and without fee is hereby granted, provided
 * that the above copyright notice and this permission notice appear
 * in all copies, and that the name of Gray Watson not be used in
 * advertising or publicity pertaining to distribution of the document
 * or software without specific, written prior permission.
 *
 * Please see the PERMISSIONS file or contact the author for information
 * about commercial licenses.
 *
 * Gray Watson makes no representations about the suitability of the
 * software described herein for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * The author may be contacted via http://www.letters.com/~gray/
 *
 * $Id$
 */

/*
 * This file is used to effectively redirect new to the more familiar
 * malloc and delete to the more familiar free so they can be debugged
 * with the debug malloc library..  They also give the known error
 * behavior, too.
 *
 * Compile and link this in with the C++ program you want to debug.
 *
 * NOTE: I am not a C++ hacker so feedback in the form of other hints
 * and ideas for C++ users would be much appreciated.
 */

#ifdef DEBUG_MALLOC

extern "C" {
#include <stdlib.h>
#include <dmalloc.h>
#include "/usr/local/src/dmalloc-4.1.2/return.h"
}

/*
 * An overload function for the C++ new.
 */
void *
operator new(size_t size)
{
  char	*file;
  GET_RET_ADDR(file);

  /* handle correct C++ semantics for an alloc of size 0 */
  
  if (size == 0) size = 1;
  
  return _malloc_leap(file, 0, size);
}

/*
 * An overload function for the C++ new[].
 */
void *
operator new[](size_t size)
{
  char	*file;
  GET_RET_ADDR(file);

  /* handle correct C++ semantics for an alloc of size 0 */
  
  if (size == 0) size = 1;

  return _malloc_leap(file, 0, size);
}

/*
 * An overload function for the C++ delete.
 */
void
operator delete(void *pnt)
{
  char	*file;
  GET_RET_ADDR(file);
  _free_leap(file, 0, pnt);
}

/*
 * An overload function for the C++ delete[].  Thanks to Jens Krinke
 * <j.krinke@gmx.de>
 */
void
operator delete[](void *pnt)
{
  char	*file;
  GET_RET_ADDR(file);
  _free_leap(file, 0, pnt);
}

#endif