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