15ed7fa21659887c067475479e0fdcc059da26fb
[mLib] / mem / track.c
1 /* -*-c-*-
2 *
3 * Tracing functions for debugging
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 /* --- ANSI headers --- */
31
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 /* --- Local headers --- */
39
40 #include "trace.h"
41 #include "track.h"
42
43 /*----- Type definitions --------------------------------------------------*/
44
45 /* --- A track block --- *
46 *
47 * This gets prefixed to every block I manage.
48 */
49
50 typedef union block {
51 struct {
52 union block *next; /* Link to previous block */
53 union block *prev; /* Link to next block */
54 size_t sz; /* Size of the block */
55 const char *ctx; /* Pointer to allocating context */
56 } x; /* Main data area */
57 long double _ld; /* Long double for alignment */
58 void *_p; /* Void pointer for alignment */
59 } block;
60
61 /*----- Private state -----------------------------------------------------*/
62
63 /* --- Tracking memory usage --- */
64
65 static unsigned int used = 0; /* Count of bytes occupied */
66 static block *list; /* List of allocated blocks */
67
68 /* --- Trace level for verbose messages --- */
69
70 static unsigned int vLevel = 0;
71
72 /* --- Context tracking --- */
73
74 static track_ctx baseContext = {
75 0, "[unknown context]"
76 };
77
78 static track_ctx *context = &baseContext;
79
80 /*----- Functions provided ------------------------------------------------*/
81
82 /* --- @track_setLevel@ --- *
83 *
84 * Arguments: @unsigned int l@ = tracing level for allocation messages
85 *
86 * Returns: ---
87 *
88 * Use: Sets the trace level for allocation messages.
89 */
90
91 void track_setLevel(unsigned int l)
92 {
93 vLevel = l;
94 }
95
96 /* --- @track_pushContext@ --- *
97 *
98 * Arguments: @track_ctx *ctx@ = context holder to push
99 *
100 * Returns: ---
101 *
102 * Use: Pushes the given context block onto the stack.
103 */
104
105 void track_pushContext(track_ctx *ctx)
106 {
107 ctx->next = context;
108 context = ctx;
109 }
110
111 /* --- @track_popContext@ --- *
112 *
113 * Arguments: @track_ctx *ctx@ = context holder to pop
114 *
115 * Returns: ---
116 *
117 * Use: Removes the given context block from the stack.
118 */
119
120 void track_popContext(track_ctx *ctx)
121 {
122 context = ctx->next;
123 }
124
125 /* --- @track_malloc@ --- *
126 *
127 * Arguments: @size_t sz@ = size requested
128 *
129 * Returns: Pointer to allocated space, or null
130 *
131 * Use: Allocates memory, and tracks how much is allocated.
132 */
133
134 void *track_malloc(size_t sz)
135 {
136 block *q = (malloc)(sz + sizeof(block));
137 if (q) {
138 used += sz;
139 if (vLevel) {
140 trace(vLevel, "(track) allocated %lu at %p in %s",
141 (unsigned long)sz, (void *)(q + 1), context->s);
142 }
143 q->x.sz = sz;
144 q->x.next = list;
145 q->x.prev = 0;
146 q->x.ctx = context->s;
147 if (q->x.next)
148 q->x.next->x.prev = q;
149 list = q;
150 return (q + 1);
151 }
152 return (0);
153 }
154
155 /* --- @track_free@ --- *
156 *
157 * Arguments: @void *p@ = pointer to an allocated block
158 *
159 * Returns: ---
160 *
161 * Use: Frees memory, and tracks how much is still allocated.
162 */
163
164 void track_free(void *p)
165 {
166 block *q;
167
168 if (!p)
169 return;
170 q = (block *)p - 1;
171 if (vLevel) {
172 trace(vLevel, "(track) freed %lu at %p for %s in %s",
173 (unsigned long)q->x.sz, (void *)(q + 1),
174 q->x.ctx, context->s);
175 }
176 if (q->x.next)
177 q->x.next->x.prev = q->x.prev;
178 if (q->x.prev)
179 q->x.prev->x.next = q->x.next;
180 else
181 list = q->x.next;
182 used -= q->x.sz;
183 (free)(q);
184 }
185
186 /* --- @track_realloc@ --- *
187 *
188 * Arguments: @void *p@ = pointer to an allocated block
189 * @size_t sz@ = how big it wants to be
190 *
191 * Returns: Pointer to the new block.
192 *
193 * Use: Reallocates a block, tracking how much memory is still
194 * available.
195 */
196
197 void *track_realloc(void *p, size_t sz)
198 {
199 size_t osz;
200 block *q, *qq;
201 if (p) {
202 q = (block *)p - 1;
203 osz = q->x.sz;
204 if (q->x.next)
205 q->x.next->x.prev = q->x.prev;
206 if (q->x.prev)
207 q->x.prev->x.next = q->x.next;
208 else
209 list = q->x.next;
210 } else {
211 q = 0;
212 osz = 0;
213 }
214 qq = (realloc)(q, sz + sizeof(block));
215 if (qq) {
216 if (vLevel) {
217 trace(vLevel,
218 "(track) reallocated %lu at %p to %lu for %s in %s",
219 (unsigned long)osz, (void *)(q + 1),
220 (unsigned long)sz, (void *)(qq + 1),
221 qq->x.ctx, context->s);
222 }
223 qq->x.sz = sz;
224 qq->x.next = list;
225 qq->x.prev = 0;
226 if (qq->x.next)
227 qq->x.next->x.prev = qq;
228 list = qq;
229 used += sz - osz;
230 qq->x.sz = sz;
231 return (qq + 1);
232 }
233 return (0);
234 }
235
236 /* --- @track_used@ --- *
237 *
238 * Arguments: ---
239 *
240 * Returns: A count of how much memory is used currently.
241 *
242 * Use: Returns the amount of memory which the @track_@-functions
243 * above have counted as being currently allocated.
244 */
245
246 unsigned long track_used(void)
247 {
248 return (used);
249 }
250
251 /* --- @track_list@ --- *
252 *
253 * Arguments: @unsigned int l@ = trace level to use
254 *
255 * Returns: ---
256 *
257 * Use: Traces a dump of the currently known blocks. Combined with
258 * a verbose dump of allocations and deallocations, and a
259 * good idea of which blocks were allocated where, this can
260 * be useful for locating memory leaks. It's not exactly a
261 * picnic, though.
262 */
263
264 void track_list(unsigned int l)
265 {
266 block *q = list;
267
268 if (!(tracing() & l))
269 return;
270
271 trace(l, "(track dump) Dumping all blocks. Stand well back...");
272 while (q) {
273 trace(l, "(track dump) %p: %lu in %s",
274 (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
275 q = q->x.next;
276 }
277 }
278
279 /*----- That's all, folks -------------------------------------------------*/