3 * $Id: track.c,v 1.5 2004/04/08 01:36:13 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
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 /* --- ANSI headers --- */
40 /* --- Local headers --- */
45 /*----- Type definitions --------------------------------------------------*/
47 /* --- A track block --- *
49 * This gets prefixed to every block I manage.
54 union block *next; /* Link to previous block */
55 union block *prev; /* Link to next block */
56 size_t sz; /* Size of the block */
57 const char *ctx; /* Pointer to allocating context */
58 } x; /* Main data area */
59 long double _ld; /* Long double for alignment */
60 void *_p; /* Void pointer for alignment */
63 /*----- Private state -----------------------------------------------------*/
65 /* --- Tracking memory usage --- */
67 static unsigned int used = 0; /* Count of bytes occupied */
68 static block *list; /* List of allocated blocks */
70 /* --- Trace level for verbose messages --- */
72 static unsigned int vLevel = 0;
74 /* --- Context tracking --- */
76 static track_ctx baseContext = {
77 0, "[unknown context]"
80 static track_ctx *context = &baseContext;
82 /*----- Functions provided ------------------------------------------------*/
84 /* --- @track_setLevel@ --- *
86 * Arguments: @unsigned int l@ = tracing level for allocation messages
90 * Use: Sets the trace level for allocation messages.
93 void track_setLevel(unsigned int l)
98 /* --- @track_pushContext@ --- *
100 * Arguments: @track_ctx *ctx@ = context holder to push
104 * Use: Pushes the given context block onto the stack.
107 void track_pushContext(track_ctx *ctx)
113 /* --- @track_popContext@ --- *
115 * Arguments: @track_ctx *ctx@ = context holder to pop
119 * Use: Removes the given context block from the stack.
122 void track_popContext(track_ctx *ctx)
127 /* --- @track_malloc@ --- *
129 * Arguments: @size_t sz@ = size requested
131 * Returns: Pointer to allocated space, or null
133 * Use: Allocates memory, and tracks how much is allocated.
136 void *track_malloc(size_t sz)
138 block *q = (malloc)(sz + sizeof(block));
142 trace(vLevel, "(track) allocated %lu at %p in %s",
143 (unsigned long)sz, (void *)(q + 1), context->s);
148 q->x.ctx = context->s;
150 q->x.next->x.prev = q;
157 /* --- @track_free@ --- *
159 * Arguments: @void *p@ = pointer to an allocated block
163 * Use: Frees memory, and tracks how much is still allocated.
166 void track_free(void *p)
174 trace(vLevel, "(track) freed %lu at %p for %s in %s",
175 (unsigned long)q->x.sz, (void *)(q + 1),
176 q->x.ctx, context->s);
179 q->x.next->x.prev = q->x.prev;
181 q->x.prev->x.next = q->x.next;
188 /* --- @track_realloc@ --- *
190 * Arguments: @void *p@ = pointer to an allocated block
191 * @size_t sz@ = how big it wants to be
193 * Returns: Pointer to the new block.
195 * Use: Reallocates a block, tracking how much memory is still
199 void *track_realloc(void *p, size_t sz)
207 q->x.next->x.prev = q->x.prev;
209 q->x.prev->x.next = q->x.next;
216 qq = (realloc)(q, sz + sizeof(block));
220 "(track) reallocated %lu at %p to %lu for %s in %s",
221 (unsigned long)osz, (void *)(q + 1),
222 (unsigned long)sz, (void *)(qq + 1),
223 qq->x.ctx, context->s);
229 qq->x.next->x.prev = qq;
238 /* --- @track_used@ --- *
242 * Returns: A count of how much memory is used currently.
244 * Use: Returns the amount of memory which the @track_@-functions
245 * above have counted as being currently allocated.
248 unsigned long track_used(void)
253 /* --- @track_list@ --- *
255 * Arguments: @unsigned int l@ = trace level to use
259 * Use: Traces a dump of the currently known blocks. Combined with
260 * a verbose dump of allocations and deallocations, and a
261 * good idea of which blocks were allocated where, this can
262 * be useful for locating memory leaks. It's not exactly a
266 void track_list(unsigned int l)
270 if (!(tracing() & l))
273 trace(l, "(track dump) Dumping all blocks. Stand well back...");
275 trace(l, "(track dump) %p: %lu in %s",
276 (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
281 /*----- That's all, folks -------------------------------------------------*/