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