15ed7fa21659887c067475479e0fdcc059da26fb
3 * Tracing functions for debugging
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 /* --- ANSI headers --- */
38 /* --- Local headers --- */
43 /*----- Type definitions --------------------------------------------------*/
45 /* --- A track block --- *
47 * This gets prefixed to every block I manage.
52 union block
*next
; /* Link to previous block */
53 union block
*prev
; /* Link to next block */
54 size_t sz
; /* Size of the block */
55 const char *ctx
; /* Pointer to allocating context */
56 } x
; /* Main data area */
57 long double _ld
; /* Long double for alignment */
58 void *_p
; /* Void pointer for alignment */
61 /*----- Private state -----------------------------------------------------*/
63 /* --- Tracking memory usage --- */
65 static unsigned int used
= 0; /* Count of bytes occupied */
66 static block
*list
; /* List of allocated blocks */
68 /* --- Trace level for verbose messages --- */
70 static unsigned int vLevel
= 0;
72 /* --- Context tracking --- */
74 static track_ctx baseContext
= {
75 0, "[unknown context]"
78 static track_ctx
*context
= &baseContext
;
80 /*----- Functions provided ------------------------------------------------*/
82 /* --- @track_setLevel@ --- *
84 * Arguments: @unsigned int l@ = tracing level for allocation messages
88 * Use: Sets the trace level for allocation messages.
91 void track_setLevel(unsigned int l
)
96 /* --- @track_pushContext@ --- *
98 * Arguments: @track_ctx *ctx@ = context holder to push
102 * Use: Pushes the given context block onto the stack.
105 void track_pushContext(track_ctx
*ctx
)
111 /* --- @track_popContext@ --- *
113 * Arguments: @track_ctx *ctx@ = context holder to pop
117 * Use: Removes the given context block from the stack.
120 void track_popContext(track_ctx
*ctx
)
125 /* --- @track_malloc@ --- *
127 * Arguments: @size_t sz@ = size requested
129 * Returns: Pointer to allocated space, or null
131 * Use: Allocates memory, and tracks how much is allocated.
134 void *track_malloc(size_t sz
)
136 block
*q
= (malloc
)(sz
+ sizeof(block
));
140 trace(vLevel
, "(track) allocated %lu at %p in %s",
141 (unsigned long)sz
, (void *)(q
+ 1), context
->s
);
146 q
->x
.ctx
= context
->s
;
148 q
->x
.next
->x
.prev
= q
;
155 /* --- @track_free@ --- *
157 * Arguments: @void *p@ = pointer to an allocated block
161 * Use: Frees memory, and tracks how much is still allocated.
164 void track_free(void *p
)
172 trace(vLevel
, "(track) freed %lu at %p for %s in %s",
173 (unsigned long)q
->x
.sz
, (void *)(q
+ 1),
174 q
->x
.ctx
, context
->s
);
177 q
->x
.next
->x
.prev
= q
->x
.prev
;
179 q
->x
.prev
->x
.next
= q
->x
.next
;
186 /* --- @track_realloc@ --- *
188 * Arguments: @void *p@ = pointer to an allocated block
189 * @size_t sz@ = how big it wants to be
191 * Returns: Pointer to the new block.
193 * Use: Reallocates a block, tracking how much memory is still
197 void *track_realloc(void *p
, size_t sz
)
205 q
->x
.next
->x
.prev
= q
->x
.prev
;
207 q
->x
.prev
->x
.next
= q
->x
.next
;
214 qq
= (realloc
)(q
, sz
+ sizeof(block
));
218 "(track) reallocated %lu at %p to %lu for %s in %s",
219 (unsigned long)osz
, (void *)(q
+ 1),
220 (unsigned long)sz
, (void *)(qq
+ 1),
221 qq
->x
.ctx
, context
->s
);
227 qq
->x
.next
->x
.prev
= qq
;
236 /* --- @track_used@ --- *
240 * Returns: A count of how much memory is used currently.
242 * Use: Returns the amount of memory which the @track_@-functions
243 * above have counted as being currently allocated.
246 unsigned long track_used(void)
251 /* --- @track_list@ --- *
253 * Arguments: @unsigned int l@ = trace level to use
257 * Use: Traces a dump of the currently known blocks. Combined with
258 * a verbose dump of allocations and deallocations, and a
259 * good idea of which blocks were allocated where, this can
260 * be useful for locating memory leaks. It's not exactly a
264 void track_list(unsigned int l
)
268 if (!(tracing() & l
))
271 trace(l
, "(track dump) Dumping all blocks. Stand well back...");
273 trace(l
, "(track dump) %p: %lu in %s",
274 (void *)(q
+ 1), (unsigned long)q
->x
.sz
, q
->x
.ctx
);
279 /*----- That's all, folks -------------------------------------------------*/