3 * $Id: track.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (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 General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with mLib; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.1 1998/06/17 23:44:42 mdw
37 /*----- Header files ------------------------------------------------------*/
39 /* --- ANSI headers --- */
47 /* --- Local headers --- */
52 /*----- Type definitions --------------------------------------------------*/
54 /* --- A track block --- *
56 * This gets prefixed to every block I manage.
59 typedef union track__block {
61 union track__block *next; /* Link to previous block */
62 union track__block *prev; /* Link to next block */
63 size_t sz; /* Size of the block */
64 const char *ctx; /* Pointer to allocating context */
65 } x; /* Main data area */
66 long double _ld; /* Long double for alignment */
67 void *_p; /* Void pointer for alignment */
70 /*----- Private state -----------------------------------------------------*/
72 /* --- Tracking memory usage --- */
74 static unsigned int track__used = 0; /* Count of bytes occupied */
75 static track__block *track__list; /* List of allocated blocks */
77 /* --- Trace level for verbose messages --- */
79 static unsigned int track__vLevel = 0;
81 /* --- Context tracking --- */
83 static track_ctx track__baseContext = {
84 0, "[unknown context]"
87 static track_ctx *track__context = &track__baseContext;
89 /*----- Functions provided ------------------------------------------------*/
91 /* --- @track_setLevel@ --- *
93 * Arguments: @unsigned int l@ = tracing level for allocation messages
97 * Use: Sets the trace level for allocation messages.
100 void track_setLevel(unsigned int l)
105 /* --- @track_pushContext@ --- *
107 * Arguments: @track_ctx *ctx@ = context holder to push
111 * Use: Pushes the given context block onto the stack.
114 void track_pushContext(track_ctx *ctx)
116 ctx->next = track__context;
117 track__context = ctx;
120 /* --- @track_popContext@ --- *
122 * Arguments: @track_ctx *ctx@ = context holder to pop
126 * Use: Removes the given context block from the stack.
129 void track_popContext(track_ctx *ctx)
131 track__context = ctx->next;
134 /* --- @track_malloc@ --- *
136 * Arguments: @size_t sz@ = size requested
138 * Returns: Pointer to allocated space, or null
140 * Use: Allocates memory, and tracks how much is allocated.
143 void *track_malloc(size_t sz)
145 track__block *q = (malloc)(sz + sizeof(track__block));
149 trace(track__vLevel, "(track) allocated %lu at %p in %s",
150 (unsigned long)sz, (void *)(q + 1), track__context->s);
153 q->x.next = track__list;
155 q->x.ctx = track__context->s;
157 q->x.next->x.prev = q;
164 /* --- @track_free@ --- *
166 * Arguments: @void *p@ = pointer to an allocated block
170 * Use: Frees memory, and tracks how much is still allocated.
173 void track_free(void *p)
179 q = (track__block *)p - 1;
181 trace(track__vLevel, "(track) freed %lu at %p for %s in %s",
182 (unsigned long)q->x.sz, (void *)(q + 1),
183 q->x.ctx, track__context->s);
186 q->x.next->x.prev = q->x.prev;
188 q->x.prev->x.next = q->x.next;
190 track__list = q->x.next;
191 track__used -= q->x.sz;
195 /* --- @track_realloc@ --- *
197 * Arguments: @void *p@ = pointer to an allocated block
198 * @size_t sz@ = how big it wants to be
200 * Returns: Pointer to the new block.
202 * Use: Reallocates a block, tracking how much memory is still
206 void *track_realloc(void *p, size_t sz)
209 track__block *q, *qq;
211 q = (track__block *)p - 1;
214 q->x.next->x.prev = q->x.prev;
216 q->x.prev->x.next = q->x.next;
218 track__list = q->x.next;
223 qq = (realloc)(q, sz + sizeof(track__block));
227 "(track) reallocated %lu at %p to %lu for %s in %s",
228 (unsigned long)osz, (void *)(q + 1),
229 (unsigned long)sz, (void *)(qq + 1),
230 qq->x.ctx, track__context->s);
233 qq->x.next = track__list;
236 qq->x.next->x.prev = qq;
238 track__used += sz - osz;
245 /* --- @track_used@ --- *
249 * Returns: A count of how much memory is used currently.
251 * Use: Returns the amount of memory which the @track_@-functions
252 * above have counted as being currently allocated.
255 unsigned long track_used(void)
257 return (track__used);
260 /* --- @track_list@ --- *
262 * Arguments: @unsigned int l@ = trace level to use
266 * Use: Traces a dump of the currently known blocks. Combined with
267 * a verbose dump of allocations and deallocations, and a
268 * good idea of which blocks were allocated where, this can
269 * be useful for locating memory leaks. It's not exactly a
273 void track_list(unsigned int l)
275 track__block *q = track__list;
277 if (!(tracing() & l))
280 trace(l, "(track dump) Dumping all blocks. Stand well back...");
282 trace(l, "(track dump) %p: %lu in %s",
283 (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
288 /*----- That's all, folks -------------------------------------------------*/