3 * $Id: track.c,v 1.4 1999/05/19 20:27:11 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.4 1999/05/19 20:27:11 mdw
34 * Change naming to match newer mLib conventions.
36 * Revision 1.3 1999/05/06 19:51:36 mdw
37 * Reformatted the LGPL notice a little bit.
39 * Revision 1.2 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
42 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
43 * Initial version of mLib
47 /*----- Header files ------------------------------------------------------*/
49 /* --- ANSI headers --- */
57 /* --- Local headers --- */
62 /*----- Type definitions --------------------------------------------------*/
64 /* --- A track block --- *
66 * This gets prefixed to every block I manage.
71 union block
*next
; /* Link to previous block */
72 union block
*prev
; /* Link to next block */
73 size_t sz
; /* Size of the block */
74 const char *ctx
; /* Pointer to allocating context */
75 } x
; /* Main data area */
76 long double _ld
; /* Long double for alignment */
77 void *_p
; /* Void pointer for alignment */
80 /*----- Private state -----------------------------------------------------*/
82 /* --- Tracking memory usage --- */
84 static unsigned int used
= 0; /* Count of bytes occupied */
85 static block
*list
; /* List of allocated blocks */
87 /* --- Trace level for verbose messages --- */
89 static unsigned int vLevel
= 0;
91 /* --- Context tracking --- */
93 static track_ctx baseContext
= {
94 0, "[unknown context]"
97 static track_ctx
*context
= &baseContext
;
99 /*----- Functions provided ------------------------------------------------*/
101 /* --- @track_setLevel@ --- *
103 * Arguments: @unsigned int l@ = tracing level for allocation messages
107 * Use: Sets the trace level for allocation messages.
110 void track_setLevel(unsigned int l
)
115 /* --- @track_pushContext@ --- *
117 * Arguments: @track_ctx *ctx@ = context holder to push
121 * Use: Pushes the given context block onto the stack.
124 void track_pushContext(track_ctx
*ctx
)
130 /* --- @track_popContext@ --- *
132 * Arguments: @track_ctx *ctx@ = context holder to pop
136 * Use: Removes the given context block from the stack.
139 void track_popContext(track_ctx
*ctx
)
144 /* --- @track_malloc@ --- *
146 * Arguments: @size_t sz@ = size requested
148 * Returns: Pointer to allocated space, or null
150 * Use: Allocates memory, and tracks how much is allocated.
153 void *track_malloc(size_t sz
)
155 block
*q
= (malloc
)(sz
+ sizeof(block
));
159 trace(vLevel
, "(track) allocated %lu at %p in %s",
160 (unsigned long)sz
, (void *)(q
+ 1), context
->s
);
165 q
->x
.ctx
= context
->s
;
167 q
->x
.next
->x
.prev
= q
;
174 /* --- @track_free@ --- *
176 * Arguments: @void *p@ = pointer to an allocated block
180 * Use: Frees memory, and tracks how much is still allocated.
183 void track_free(void *p
)
191 trace(vLevel
, "(track) freed %lu at %p for %s in %s",
192 (unsigned long)q
->x
.sz
, (void *)(q
+ 1),
193 q
->x
.ctx
, context
->s
);
196 q
->x
.next
->x
.prev
= q
->x
.prev
;
198 q
->x
.prev
->x
.next
= q
->x
.next
;
205 /* --- @track_realloc@ --- *
207 * Arguments: @void *p@ = pointer to an allocated block
208 * @size_t sz@ = how big it wants to be
210 * Returns: Pointer to the new block.
212 * Use: Reallocates a block, tracking how much memory is still
216 void *track_realloc(void *p
, size_t sz
)
224 q
->x
.next
->x
.prev
= q
->x
.prev
;
226 q
->x
.prev
->x
.next
= q
->x
.next
;
233 qq
= (realloc
)(q
, sz
+ sizeof(block
));
237 "(track) reallocated %lu at %p to %lu for %s in %s",
238 (unsigned long)osz
, (void *)(q
+ 1),
239 (unsigned long)sz
, (void *)(qq
+ 1),
240 qq
->x
.ctx
, context
->s
);
246 qq
->x
.next
->x
.prev
= qq
;
255 /* --- @track_used@ --- *
259 * Returns: A count of how much memory is used currently.
261 * Use: Returns the amount of memory which the @track_@-functions
262 * above have counted as being currently allocated.
265 unsigned long track_used(void)
270 /* --- @track_list@ --- *
272 * Arguments: @unsigned int l@ = trace level to use
276 * Use: Traces a dump of the currently known blocks. Combined with
277 * a verbose dump of allocations and deallocations, and a
278 * good idea of which blocks were allocated where, this can
279 * be useful for locating memory leaks. It's not exactly a
283 void track_list(unsigned int l
)
287 if (!(tracing() & l
))
290 trace(l
, "(track dump) Dumping all blocks. Stand well back...");
292 trace(l
, "(track dump) %p: %lu in %s",
293 (void *)(q
+ 1), (unsigned long)q
->x
.sz
, q
->x
.ctx
);
298 /*----- That's all, folks -------------------------------------------------*/