3 * $Id: track.c,v 1.3 1999/05/06 19:51:36 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.3 1999/05/06 19:51:36 mdw
34 * Reformatted the LGPL notice a little bit.
36 * Revision 1.2 1999/05/05 18:50:31 mdw
37 * Change licensing conditions to LGPL.
39 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
40 * Initial version of mLib
44 /*----- Header files ------------------------------------------------------*/
46 /* --- ANSI headers --- */
54 /* --- Local headers --- */
59 /*----- Type definitions --------------------------------------------------*/
61 /* --- A track block --- *
63 * This gets prefixed to every block I manage.
66 typedef union track__block
{
68 union track__block
*next
; /* Link to previous block */
69 union track__block
*prev
; /* Link to next block */
70 size_t sz
; /* Size of the block */
71 const char *ctx
; /* Pointer to allocating context */
72 } x
; /* Main data area */
73 long double _ld
; /* Long double for alignment */
74 void *_p
; /* Void pointer for alignment */
77 /*----- Private state -----------------------------------------------------*/
79 /* --- Tracking memory usage --- */
81 static unsigned int track__used
= 0; /* Count of bytes occupied */
82 static track__block
*track__list
; /* List of allocated blocks */
84 /* --- Trace level for verbose messages --- */
86 static unsigned int track__vLevel
= 0;
88 /* --- Context tracking --- */
90 static track_ctx track__baseContext
= {
91 0, "[unknown context]"
94 static track_ctx
*track__context
= &track__baseContext
;
96 /*----- Functions provided ------------------------------------------------*/
98 /* --- @track_setLevel@ --- *
100 * Arguments: @unsigned int l@ = tracing level for allocation messages
104 * Use: Sets the trace level for allocation messages.
107 void track_setLevel(unsigned int l
)
112 /* --- @track_pushContext@ --- *
114 * Arguments: @track_ctx *ctx@ = context holder to push
118 * Use: Pushes the given context block onto the stack.
121 void track_pushContext(track_ctx
*ctx
)
123 ctx
->next
= track__context
;
124 track__context
= ctx
;
127 /* --- @track_popContext@ --- *
129 * Arguments: @track_ctx *ctx@ = context holder to pop
133 * Use: Removes the given context block from the stack.
136 void track_popContext(track_ctx
*ctx
)
138 track__context
= ctx
->next
;
141 /* --- @track_malloc@ --- *
143 * Arguments: @size_t sz@ = size requested
145 * Returns: Pointer to allocated space, or null
147 * Use: Allocates memory, and tracks how much is allocated.
150 void *track_malloc(size_t sz
)
152 track__block
*q
= (malloc
)(sz
+ sizeof(track__block
));
156 trace(track__vLevel
, "(track) allocated %lu at %p in %s",
157 (unsigned long)sz
, (void *)(q
+ 1), track__context
->s
);
160 q
->x
.next
= track__list
;
162 q
->x
.ctx
= track__context
->s
;
164 q
->x
.next
->x
.prev
= q
;
171 /* --- @track_free@ --- *
173 * Arguments: @void *p@ = pointer to an allocated block
177 * Use: Frees memory, and tracks how much is still allocated.
180 void track_free(void *p
)
186 q
= (track__block
*)p
- 1;
188 trace(track__vLevel
, "(track) freed %lu at %p for %s in %s",
189 (unsigned long)q
->x
.sz
, (void *)(q
+ 1),
190 q
->x
.ctx
, track__context
->s
);
193 q
->x
.next
->x
.prev
= q
->x
.prev
;
195 q
->x
.prev
->x
.next
= q
->x
.next
;
197 track__list
= q
->x
.next
;
198 track__used
-= q
->x
.sz
;
202 /* --- @track_realloc@ --- *
204 * Arguments: @void *p@ = pointer to an allocated block
205 * @size_t sz@ = how big it wants to be
207 * Returns: Pointer to the new block.
209 * Use: Reallocates a block, tracking how much memory is still
213 void *track_realloc(void *p
, size_t sz
)
216 track__block
*q
, *qq
;
218 q
= (track__block
*)p
- 1;
221 q
->x
.next
->x
.prev
= q
->x
.prev
;
223 q
->x
.prev
->x
.next
= q
->x
.next
;
225 track__list
= q
->x
.next
;
230 qq
= (realloc
)(q
, sz
+ sizeof(track__block
));
234 "(track) reallocated %lu at %p to %lu for %s in %s",
235 (unsigned long)osz
, (void *)(q
+ 1),
236 (unsigned long)sz
, (void *)(qq
+ 1),
237 qq
->x
.ctx
, track__context
->s
);
240 qq
->x
.next
= track__list
;
243 qq
->x
.next
->x
.prev
= qq
;
245 track__used
+= sz
- osz
;
252 /* --- @track_used@ --- *
256 * Returns: A count of how much memory is used currently.
258 * Use: Returns the amount of memory which the @track_@-functions
259 * above have counted as being currently allocated.
262 unsigned long track_used(void)
264 return (track__used
);
267 /* --- @track_list@ --- *
269 * Arguments: @unsigned int l@ = trace level to use
273 * Use: Traces a dump of the currently known blocks. Combined with
274 * a verbose dump of allocations and deallocations, and a
275 * good idea of which blocks were allocated where, this can
276 * be useful for locating memory leaks. It's not exactly a
280 void track_list(unsigned int l
)
282 track__block
*q
= track__list
;
284 if (!(tracing() & l
))
287 trace(l
, "(track dump) Dumping all blocks. Stand well back...");
289 trace(l
, "(track dump) %p: %lu in %s",
290 (void *)(q
+ 1), (unsigned long)q
->x
.sz
, q
->x
.ctx
);
295 /*----- That's all, folks -------------------------------------------------*/