1 #ifndef MPT_HEAPDIFF_H
2 #define MPT_HEAPDIFF_H
3 
4 
5 /*
6  * mpatrol
7  * A library for controlling and tracing dynamic memory allocations.
8  * Copyright (C) 1997-2002 Graeme S. Roy <graeme.roy@analog.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free
22  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307, USA.
24  */
25 
26 
27 /*
28  * Heap difference logger.  Uses mpatrol to log the differences in the
29  * heap between two points in a program's execution, which can then be
30  * used to help detect localised memory leaks.
31  */
32 
33 
34 /*
35  * $Id: heapdiff.h,v 1.9 2002/01/08 20:05:10 graeme Exp $
36  */
37 
38 
39 /*
40  * This file defines heapdiffstart() and heapdiffend(), which must be
41  * called in matching pairs.  They both take a heapdiff object as their
42  * first parameter, which must still be in scope when the matching call
43  * to heapdiffend() is made.  The heapdiff object is initialised at the
44  * call to heapdiffstart() and is finalised when heapdiffend() is called.
45  * It must not be modified in between and should be treated as an opaque
46  * type.  heapdiffend() can only be called once per heapdiff object
47  * before requiring that the heapdiff object be reinitialised through a
48  * call to heapdiffstart().
49  *
50  * The second parameter to heapdiffstart() specifies a set of flags that
51  * can be used to control what is written to the mpatrol log.  A list of
52  * all unfreed memory allocations can be logged with the HD_UNFREED flag
53  * and a list of all freed memory allocations can be logged with the
54  * HD_FREED flag, although the latter makes use of the NOFREE option and
55  * can incur a large performance and space penalty, and also relies on
56  * the NOFREE option being unmodified between the calls to heapdiffstart()
57  * and heapdiffend().  Note that marked allocations are not normally
58  * logged but this can be changed by adding the HD_MARKED flag.
59  *
60  * By default, only a minimal amount of detail is logged for each
61  * allocation, but this can be changed with the HD_FULL flag to log full
62  * details for each allocation.  If the filename and line number for an
63  * allocation is known and the EDIT or LIST option is being used then
64  * using HD_VIEW will edit or list the relevant source file at the correct
65  * line number, but only if the EDIT or LIST options are supported.
66  *
67  * If the HD_CONTENTS flag is specified then the contents of all current
68  * memory allocations will be written to files and then compared with their
69  * subsequent contents when heapdiffend() is called.  If the heap is large
70  * then this option can require a substantial amount of disk space.  All
71  * of the allocation contents files will be deleted when the matching call
72  * to heapdiffend() is made.
73  */
74 
75 
76 #include <mpatrol.h>
77 
78 
79 #define HD_FREED    1  /* log all freed allocations */
80 #define HD_UNFREED  2  /* log all unfreed allocations */
81 #define HD_MARKED   4  /* include marked allocations */
82 #define HD_FULL     8  /* log full details of each allocation */
83 #define HD_CONTENTS 16 /* detect any changes in the contents of allocations */
84 #define HD_VIEW     32 /* view each allocation */
85 
86 
87 /* The structure used to store the current state of the heap and any
88  * useful statistics gathered.
89  */
90 
91 typedef struct heapdiff
92 {
93     unsigned long id;     /* heapdiff identifier */
94     unsigned long event;  /* event at time of snapshot */
95     unsigned long flags;  /* flags determining behaviour */
96     unsigned long nofree; /* previous NOFREE setting */
97     unsigned long count;  /* total number of allocations */
98     unsigned long total;  /* total bytes in allocations */
99 }
100 heapdiff;
101 
102 
103 #ifndef NDEBUG
104 
105 #define heapdiffstart(h, f) __mpt_heapdiffstart(&(h), (f), __FILE__, __LINE__)
106 #define heapdiffend(h) __mpt_heapdiffend(&(h), __FILE__, __LINE__)
107 
108 
109 #ifdef __cplusplus
110 extern "C"
111 {
112 #endif /* __cplusplus */
113 
114 
115 void __mpt_heapdiffstart(heapdiff *, unsigned long, MP_CONST char *,
116                          unsigned long);
117 void __mpt_heapdiffend(heapdiff *, MP_CONST char *, unsigned long);
118 
119 
120 #ifdef __cplusplus
121 }
122 #endif /* __cplusplus */
123 
124 #else /* NDEBUG */
125 
126 #define heapdiffstart(h, f) ((void) 0)
127 #define heapdiffend(h) ((void) 0)
128 
129 #endif /* NDEBUG */
130 
131 
132 #endif /* MPT_HEAPDIFF_H */
133