chiark / gitweb /
New hex encoding stuff. Rename test programs.
[mLib] / track.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
ff0f0220 3 * $Id: track.c,v 1.4 1999/05/19 20:27:11 mdw Exp $
0875b58f 4 *
5 * Tracing functions for debugging
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
c846879c 10/*----- Licensing notice --------------------------------------------------*
0875b58f 11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
c846879c 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.
18 *
0875b58f 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
c846879c 22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
0bd98442 25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
0875b58f 28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: track.c,v $
ff0f0220 33 * Revision 1.4 1999/05/19 20:27:11 mdw
34 * Change naming to match newer mLib conventions.
35 *
0bd98442 36 * Revision 1.3 1999/05/06 19:51:36 mdw
37 * Reformatted the LGPL notice a little bit.
38 *
c846879c 39 * Revision 1.2 1999/05/05 18:50:31 mdw
40 * Change licensing conditions to LGPL.
41 *
42 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
43 * Initial version of mLib
0875b58f 44 *
45 */
46
47/*----- Header files ------------------------------------------------------*/
48
49/* --- ANSI headers --- */
50
51#include <ctype.h>
52#include <stdarg.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56
57/* --- Local headers --- */
58
59#include "trace.h"
60#include "track.h"
61
62/*----- Type definitions --------------------------------------------------*/
63
64/* --- A track block --- *
65 *
66 * This gets prefixed to every block I manage.
67 */
68
ff0f0220 69typedef union block {
0875b58f 70 struct {
ff0f0220 71 union block *next; /* Link to previous block */
72 union block *prev; /* Link to next block */
0875b58f 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 */
ff0f0220 78} block;
0875b58f 79
80/*----- Private state -----------------------------------------------------*/
81
82/* --- Tracking memory usage --- */
83
ff0f0220 84static unsigned int used = 0; /* Count of bytes occupied */
85static block *list; /* List of allocated blocks */
0875b58f 86
87/* --- Trace level for verbose messages --- */
88
ff0f0220 89static unsigned int vLevel = 0;
0875b58f 90
91/* --- Context tracking --- */
92
ff0f0220 93static track_ctx baseContext = {
0875b58f 94 0, "[unknown context]"
95};
96
ff0f0220 97static track_ctx *context = &baseContext;
0875b58f 98
99/*----- Functions provided ------------------------------------------------*/
100
101/* --- @track_setLevel@ --- *
102 *
103 * Arguments: @unsigned int l@ = tracing level for allocation messages
104 *
105 * Returns: ---
106 *
107 * Use: Sets the trace level for allocation messages.
108 */
109
110void track_setLevel(unsigned int l)
111{
ff0f0220 112 vLevel = l;
0875b58f 113}
114
115/* --- @track_pushContext@ --- *
116 *
117 * Arguments: @track_ctx *ctx@ = context holder to push
118 *
119 * Returns: ---
120 *
121 * Use: Pushes the given context block onto the stack.
122 */
123
124void track_pushContext(track_ctx *ctx)
125{
ff0f0220 126 ctx->next = context;
127 context = ctx;
0875b58f 128}
129
130/* --- @track_popContext@ --- *
131 *
132 * Arguments: @track_ctx *ctx@ = context holder to pop
133 *
134 * Returns: ---
135 *
136 * Use: Removes the given context block from the stack.
137 */
138
139void track_popContext(track_ctx *ctx)
140{
ff0f0220 141 context = ctx->next;
0875b58f 142}
143
144/* --- @track_malloc@ --- *
145 *
146 * Arguments: @size_t sz@ = size requested
147 *
148 * Returns: Pointer to allocated space, or null
149 *
150 * Use: Allocates memory, and tracks how much is allocated.
151 */
152
153void *track_malloc(size_t sz)
154{
ff0f0220 155 block *q = (malloc)(sz + sizeof(block));
0875b58f 156 if (q) {
ff0f0220 157 used += sz;
158 if (vLevel) {
159 trace(vLevel, "(track) allocated %lu at %p in %s",
160 (unsigned long)sz, (void *)(q + 1), context->s);
0875b58f 161 }
162 q->x.sz = sz;
ff0f0220 163 q->x.next = list;
0875b58f 164 q->x.prev = 0;
ff0f0220 165 q->x.ctx = context->s;
0875b58f 166 if (q->x.next)
167 q->x.next->x.prev = q;
ff0f0220 168 list = q;
0875b58f 169 return (q + 1);
170 }
171 return (0);
172}
173
174/* --- @track_free@ --- *
175 *
176 * Arguments: @void *p@ = pointer to an allocated block
177 *
178 * Returns: ---
179 *
180 * Use: Frees memory, and tracks how much is still allocated.
181 */
182
183void track_free(void *p)
184{
ff0f0220 185 block *q;
0875b58f 186
187 if (!p)
188 return;
ff0f0220 189 q = (block *)p - 1;
190 if (vLevel) {
191 trace(vLevel, "(track) freed %lu at %p for %s in %s",
0875b58f 192 (unsigned long)q->x.sz, (void *)(q + 1),
ff0f0220 193 q->x.ctx, context->s);
0875b58f 194 }
195 if (q->x.next)
196 q->x.next->x.prev = q->x.prev;
197 if (q->x.prev)
198 q->x.prev->x.next = q->x.next;
199 else
ff0f0220 200 list = q->x.next;
201 used -= q->x.sz;
0875b58f 202 (free)(q);
203}
204
205/* --- @track_realloc@ --- *
206 *
207 * Arguments: @void *p@ = pointer to an allocated block
208 * @size_t sz@ = how big it wants to be
209 *
210 * Returns: Pointer to the new block.
211 *
212 * Use: Reallocates a block, tracking how much memory is still
213 * available.
214 */
215
216void *track_realloc(void *p, size_t sz)
217{
218 size_t osz;
ff0f0220 219 block *q, *qq;
0875b58f 220 if (p) {
ff0f0220 221 q = (block *)p - 1;
0875b58f 222 osz = q->x.sz;
223 if (q->x.next)
224 q->x.next->x.prev = q->x.prev;
225 if (q->x.prev)
226 q->x.prev->x.next = q->x.next;
227 else
ff0f0220 228 list = q->x.next;
0875b58f 229 } else {
230 q = 0;
231 osz = 0;
232 }
ff0f0220 233 qq = (realloc)(q, sz + sizeof(block));
0875b58f 234 if (qq) {
ff0f0220 235 if (vLevel) {
236 trace(vLevel,
0875b58f 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),
ff0f0220 240 qq->x.ctx, context->s);
0875b58f 241 }
242 qq->x.sz = sz;
ff0f0220 243 qq->x.next = list;
0875b58f 244 qq->x.prev = 0;
245 if (qq->x.next)
246 qq->x.next->x.prev = qq;
ff0f0220 247 list = qq;
248 used += sz - osz;
0875b58f 249 qq->x.sz = sz;
250 return (qq + 1);
251 }
252 return (0);
253}
254
255/* --- @track_used@ --- *
256 *
257 * Arguments: ---
258 *
259 * Returns: A count of how much memory is used currently.
260 *
261 * Use: Returns the amount of memory which the @track_@-functions
262 * above have counted as being currently allocated.
263 */
264
265unsigned long track_used(void)
266{
ff0f0220 267 return (used);
0875b58f 268}
269
270/* --- @track_list@ --- *
271 *
272 * Arguments: @unsigned int l@ = trace level to use
273 *
274 * Returns: ---
275 *
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
280 * picnic, though.
281 */
282
283void track_list(unsigned int l)
284{
ff0f0220 285 block *q = list;
0875b58f 286
287 if (!(tracing() & l))
288 return;
289
290 trace(l, "(track dump) Dumping all blocks. Stand well back...");
291 while (q) {
292 trace(l, "(track dump) %p: %lu in %s",
293 (void *)(q + 1), (unsigned long)q->x.sz, q->x.ctx);
294 q = q->x.next;
295 }
296}
297
298/*----- That's all, folks -------------------------------------------------*/