0875b58f |
1 | /* -*-c-*- |
2 | * |
c846879c |
3 | * $Id: track.h,v 1.2 1999/05/05 18:50:31 mdw Exp $ |
0875b58f |
4 | * |
5 | * Tracing functions for debugging |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
c846879c |
10 | /*----- Licensing notice --------------------------------------------------* |
0875b58f |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
c846879c |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
0875b58f |
19 | * mLib is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c846879c |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with mLib; if not, write to the Free Software |
26 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
0875b58f |
27 | */ |
28 | |
29 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: track.h,v $ |
c846879c |
32 | * Revision 1.2 1999/05/05 18:50:31 mdw |
33 | * Change licensing conditions to LGPL. |
34 | * |
35 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
36 | * Initial version of mLib |
0875b58f |
37 | * |
38 | */ |
39 | |
40 | #ifndef TRACK_H |
41 | #define TRACK_H |
42 | |
43 | #ifdef __cplusplus |
44 | extern "C" { |
45 | #endif |
46 | |
47 | #include <stdlib.h> |
48 | |
49 | /*----- Options and conventions -------------------------------------------* |
50 | * |
51 | * The following macros affect the tracking system: |
52 | * |
53 | * @TRACK_ENABLE@: Enable tracking of memory allocations |
54 | * @TRACK_BLAME@: Register my context blocks in allocations |
55 | * |
56 | * The reason there are two switches is simple. It's often the case that a |
57 | * library routine allocates memory for its client. Therefore, whether we |
58 | * want to record the library or the client depends on how much we trust |
59 | * the two pieces of software. Setting @TRACK_ENABLE@ and @TRACK_BLAME@ |
60 | * suggests that the current source file might leak memory, so we want its |
61 | * context markers in the list. Setting @TRACK_ENABLE@ but not |
62 | * @TRACK_BLAME@ suggests that we trust this code, but not the code which |
63 | * calls it, so we want to preserve the caller's context markers. |
64 | * |
65 | * Got it? Good. |
66 | */ |
67 | |
68 | /*----- Type definitions --------------------------------------------------*/ |
69 | |
70 | /* --- A context buffer --- */ |
71 | |
72 | typedef struct track_ctx { |
73 | struct track_ctx *next; |
74 | const char *s; |
75 | } track_ctx; |
76 | |
77 | /*----- Functions provided ------------------------------------------------*/ |
78 | |
79 | /* --- @track_setLevel@ --- * |
80 | * |
81 | * Arguments: @unsigned int l@ = tracing level for allocation messages |
82 | * |
83 | * Returns: --- |
84 | * |
85 | * Use: Sets the trace level for allocation messages. |
86 | */ |
87 | |
88 | extern void track_setLevel(unsigned int /*l*/); |
89 | |
90 | /* --- @track_pushContext@ --- * |
91 | * |
92 | * Arguments: @track_ctx *ctx@ = context holder to push |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Pushes the given context block onto the stack. |
97 | */ |
98 | |
99 | extern void track_pushContext(track_ctx */*ctx*/); |
100 | |
101 | /* --- @track_popContext@ --- * |
102 | * |
103 | * Arguments: @track_ctx *ctx@ = context holder to pop |
104 | * |
105 | * Returns: --- |
106 | * |
107 | * Use: Removes the given context block from the stack. |
108 | */ |
109 | |
110 | extern void track_popContext(track_ctx */*ctx*/); |
111 | |
112 | /* --- @track_malloc@ --- * |
113 | * |
114 | * Arguments: @size_t sz@ = size requested |
115 | * |
116 | * Returns: Pointer to allocated space, or null |
117 | * |
118 | * Use: Allocates memory, and tracks how much is allocated. |
119 | */ |
120 | |
121 | extern void *track_malloc(size_t /*sz*/); |
122 | |
123 | /* --- @track_free@ --- * |
124 | * |
125 | * Arguments: @void *p@ = pointer to an allocated block |
126 | * |
127 | * Returns: --- |
128 | * |
129 | * Use: Frees memory, and tracks how much is still allocated. |
130 | */ |
131 | |
132 | extern void track_free(void */*p*/); |
133 | |
134 | /* --- @track_realloc@ --- * |
135 | * |
136 | * Arguments: @void *p@ = pointer to an allocated block |
137 | * @size_t sz@ = how big it wants to be |
138 | * |
139 | * Returns: Pointer to the new block. |
140 | * |
141 | * Use: Reallocates a block, tracking how much memory is still |
142 | * available. |
143 | */ |
144 | |
145 | extern void *track_realloc(void */*p*/, size_t /*sz*/); |
146 | |
147 | /* --- @track_used@ --- * |
148 | * |
149 | * Arguments: --- |
150 | * |
151 | * Returns: A count of how much memory is used currently. |
152 | * |
153 | * Use: Returns the amount of memory which the @track_@-functions |
154 | * above have counted as being currently allocated. |
155 | */ |
156 | |
157 | extern unsigned long track_used(void); |
158 | |
159 | /* --- @track_list@ --- * |
160 | * |
161 | * Arguments: @unsigned int l@ = trace level to use |
162 | * |
163 | * Returns: --- |
164 | * |
165 | * Use: Traces a dump of the currently known blocks. Combined with |
166 | * a verbose dump of allocations and deallocations, and a |
167 | * good idea of which blocks were allocated where, this can |
168 | * be useful for locating memory leaks. It's not exactly a |
169 | * picnic, though. |
170 | */ |
171 | |
172 | extern void track_list(unsigned int l); |
173 | |
174 | /*----- Macro wrappers ----------------------------------------------------*/ |
175 | |
176 | |
177 | /* --- If tracking is to be done, set it up --- */ |
178 | |
179 | #ifdef TRACK_ENABLE |
180 | # undef malloc |
181 | # define malloc(sz) track_malloc(sz) |
182 | # undef free |
183 | # define free(p) track_free(p) |
184 | # undef realloc |
185 | # define realloc(p, sz) track_realloc(p, sz) |
186 | #endif |
187 | |
188 | /* --- Provide a context for doing track-related things --- */ |
189 | |
190 | #ifdef TRACK_ENABLE |
191 | # define TRACK(x) x |
192 | #else |
193 | # define TRACK(x) |
194 | #endif |
195 | |
196 | /* --- Handle contexts --- */ |
197 | |
198 | #if defined(TRACK_ENABLE) && defined(TRACK_BLAME) |
199 | # define TRACK_NCTX(name, string) track__context name = { 0, string } |
200 | # define TRACK_NPUSH(name) track_pushContext(name) |
201 | # define TRACK_NPOP(name) track_popContext(name) |
202 | # define TRACK_CTX(string) TRACK_NCTX(track__localContext, string) |
203 | # define TRACK_PUSH TRACK_NPUSH(track__localContext) |
204 | # define TRACK_POP TRACK_NPOP(track__localContext) |
205 | #else |
206 | # define TRACK_NCTX(name, string) |
207 | # define TRACK_NPUSH(name) ((void)0) |
208 | # define TRACK_NPOP(name) ((void)0) |
209 | # define TRACK_CTX(string) |
210 | # define TRACK_PUSH ((void)0) |
211 | # define TRACK_POP ((void)0) |
212 | #endif |
213 | |
214 | /*----- That's all, folks -------------------------------------------------*/ |
215 | |
216 | #ifdef __cplusplus |
217 | } |
218 | #endif |
219 | |
220 | #endif |