*** NEW CODING POLICY ***
[ardour.git] / libs / pbd / dmalloc.cc
1 /*
2  * file that facilitates C++ program debugging.
3  *
4  * Copyright 1995 by Gray Watson
5  *
6  * This file is part of the dmalloc package.
7  *
8  * Permission to use, copy, modify, and distribute this software for any
9  * NON-COMMERCIAL purpose and without fee is hereby granted, provided
10  * that the above copyright notice and this permission notice appear
11  * in all copies, and that the name of Gray Watson not be used in
12  * advertising or publicity pertaining to distribution of the document
13  * or software without specific, written prior permission.
14  *
15  * Please see the PERMISSIONS file or contact the author for information
16  * about commercial licenses.
17  *
18  * Gray Watson makes no representations about the suitability of the
19  * software described herein for any purpose.  It is provided "as is"
20  * without express or implied warranty.
21  *
22  * The author may be contacted via http://www.letters.com/~gray/
23  *
24  * $Id$
25  */
26
27 /*
28  * This file is used to effectively redirect new to the more familiar
29  * malloc and delete to the more familiar free so they can be debugged
30  * with the debug malloc library..  They also give the known error
31  * behavior, too.
32  *
33  * Compile and link this in with the C++ program you want to debug.
34  *
35  * NOTE: I am not a C++ hacker so feedback in the form of other hints
36  * and ideas for C++ users would be much appreciated.
37  */
38
39 #ifdef DEBUG_MALLOC
40
41 extern "C" {
42 #include <stdlib.h>
43 #include <dmalloc.h>
44 #include "/usr/local/src/dmalloc-4.1.2/return.h"
45 }
46
47 /*
48  * An overload function for the C++ new.
49  */
50 void *
51 operator new(size_t size)
52 {
53   char  *file;
54   GET_RET_ADDR(file);
55
56   /* handle correct C++ semantics for an alloc of size 0 */
57   
58   if (size == 0) size = 1;
59   
60   return _malloc_leap(file, 0, size);
61 }
62
63 /*
64  * An overload function for the C++ new[].
65  */
66 void *
67 operator new[](size_t size)
68 {
69   char  *file;
70   GET_RET_ADDR(file);
71
72   /* handle correct C++ semantics for an alloc of size 0 */
73   
74   if (size == 0) size = 1;
75
76   return _malloc_leap(file, 0, size);
77 }
78
79 /*
80  * An overload function for the C++ delete.
81  */
82 void
83 operator delete(void *pnt)
84 {
85   char  *file;
86   GET_RET_ADDR(file);
87   _free_leap(file, 0, pnt);
88 }
89
90 /*
91  * An overload function for the C++ delete[].  Thanks to Jens Krinke
92  * <j.krinke@gmx.de>
93  */
94 void
95 operator delete[](void *pnt)
96 {
97   char  *file;
98   GET_RET_ADDR(file);
99   _free_leap(file, 0, pnt);
100 }
101
102 #endif