3 * $Id: track.c,v 1.2 1999/05/05 18:50:31 mdw Exp $
5 * Tracing functions for debugging
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
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.
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
22 * GNU Library General Public License for more details.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/05/05 18:50:31 mdw
33 * Change licensing conditions to LGPL.
35 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
36 * Initial version of mLib
40 /*----- Header files ------------------------------------------------------*/
42 /* --- ANSI headers --- */
50 /* --- Local headers --- */
55 /*----- Type definitions --------------------------------------------------*/
57 /* --- A track block --- *
59 * This gets prefixed to every block I manage.
62 typedef union track__block {
64 union track__block *next; /* Link to previous block */
65 union track__block *prev; /* Link to next block */
66 size_t sz; /* Size of the block */
67 const char *ctx; /* Pointer to allocating context */
68 } x; /* Main data area */
69 long double _ld; /* Long double for alignment */
70 void *_p; /* Void pointer for alignment */
73 /*----- Private state -----------------------------------------------------*/
75 /* --- Tracking memory usage --- */
77 static unsigned int track__used = 0; /* Count of bytes occupied */
78 static track__block *track__list; /* List of allocated blocks */
80 /* --- Trace level for verbose messages --- */
82 static unsigned int track__vLevel = 0;
84 /* --- Context tracking --- */
86 static track_ctx track__baseContext = {
87 0, "[unknown context]"
90 static track_ctx *track__context = &track__baseContext;
92 /*----- Functions provided ------------------------------------------------*/
94 /* --- @track_setLevel@ --- *
96 * Arguments: @unsigned int l@ = tracing level for allocation messages
100 * Use: Sets the trace level for allocation messages.
103 void track_setLevel(unsigned int l)
108 /* --- @track_pushContext@ --- *
110 * Arguments: @track_ctx *ctx@ = context holder to push
114 * Use: Pushes the given context block onto the stack.
117 void track_pushContext(track_ctx *ctx)
119 ctx->next = track__context;
120 track__context = ctx;
123 /* --- @track_popContext@ --- *
125 * Arguments: @track_ctx *ctx@ = context holder to pop
129 * Use: Removes the given context block from the stack.
132 void track_popContext(track_ctx *ctx)
134 track__context = ctx->next;
137 /* --- @track_malloc@ --- *
139 * Arguments: @size_t sz@ = size requested
141 * Returns: Pointer to allocated space, or null
143 * Use: Allocates memory, and tracks how much is allocated.
146 void *track_malloc(size_t sz)
148 track__block *q = (malloc)(sz + sizeof(track__block));
152 trace(track__vLevel, "(track) allocated %lu at %p in %s",
153 (unsigned long)sz, (void *)(q + 1), track__context->s);
156 q->x.next = track__list;
158 q->x.ctx = track__context->s;
160 q->x.next->x.prev = q;
167 /* --- @track_free@ --- *
169 * Arguments: @void *p@ = pointer to an allocated block
173 * Use: Frees memory, and tracks how much is still allocated.
176 void track_free(void *p)
182 q = (track__block *)p - 1;
184 trace(track__vLevel, "(track) freed %lu at %p for %s in %s",
185 (unsigned long)q->x.sz, (void *)(q + 1),
186 q->x.ctx, track__context->s);
189 q->x.next->x.prev = q->x.prev;
191 q->x.prev->x.next = q->x.next;
193 track__list = q->x.next;
194 track__used -= q->x.sz;
198 /* --- @track_realloc@ --- *
200 * Arguments: @void *p@ = pointer to an allocated block
201 * @size_t sz@ = how big it wants to be
203 * Returns: Pointer to the new block.
205 * Use: Reallocates a block, tracking how much memory is still
209 void *track_realloc(void *p, size_t sz)
212 track__block *q, *qq;
214 q = (track__block *)p - 1;
217 q->x.next->x.prev = q->x.prev;
219 q->x.prev->x.next = q->x.next;
221 track__list = q->x.next;
226 qq = (realloc)(q, sz + sizeof(track__block));
230 "(track) reallocated %lu at %p to %lu for %s in %s",
231 (unsigned long)osz, (void *)(q + 1),
232 (unsigned long)sz, (void *)(qq + 1),
233 qq->x.ctx, track__context->s);
236 qq->x.next = track__list;
239 qq->x.next->x.prev = qq;
241 track__used += sz - osz;
248 /* --- @track_used@ --- *
252 * Returns: A count of how much memory is used currently.
254 * Use: Returns the amount of memory which the @track_@-functions
255 * above have counted as being currently allocated.
258 unsigned long track_used(void)
260 return (track__used);
263 /* --- @track_list@ --- *
265 * Arguments: @unsigned int l@ = trace level to use
269 * Use: Traces a dump of the currently known blocks. Combined with
270 * a verbose dump of allocations and deallocations, and a
271 * good idea of which blocks were allocated where, this can
272 * be useful for locating memory leaks. It's not exactly a
276 void track_list(unsigned int l)
278 track__block *q = track__list;
280 if (!(tracing() & l))
283 trace(l, "(track dump) Dumping all blocks. Stand well back...");
285 trace(l, "(track dump) %p: %lu in %s",
286 (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
291 /*----- That's all, folks -------------------------------------------------*/