Flush output before and after writing memory tracking information.
[become] / src / utils.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
88c62e21 3 * $Id: utils.c,v 1.5 1997/09/17 10:24:47 mdw Exp $
c4f2d992 4 *
5 * Miscellaneous useful bits of code.
6 *
7 * (c) 1997 Mark Wooding
8 */
9
03f996bd 10/*----- Licensing notice --------------------------------------------------*
c4f2d992 11 *
12 * This file is part of `become'
13 *
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * `Become' 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 General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
03f996bd 25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
c4f2d992 27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: utils.c,v $
88c62e21 32 * Revision 1.5 1997/09/17 10:24:47 mdw
33 * Flush output before and after writing memory tracking information.
34 *
35 * Revision 1.4 1997/09/08 13:43:54 mdw
d0168b65 36 * Flush tracedump file after each `interesting' write.
37 *
38 * Revision 1.3 1997/08/20 16:25:37 mdw
deab2ee8 39 * Add some simple `malloc' tracking.
40 *
03f996bd 41 * Revision 1.2 1997/08/04 10:24:26 mdw
42 * Sources placed under CVS control.
43 *
44 * Revision 1.1 1997/07/21 13:47:42 mdw
c4f2d992 45 * Initial revision
46 *
47 */
48
49/*----- Header files ------------------------------------------------------*/
50
51/* --- ANSI headers --- */
52
03f996bd 53#include <ctype.h>
c4f2d992 54#include <stdarg.h>
55#include <stdio.h>
56#include <stdlib.h>
03f996bd 57#include <string.h>
c4f2d992 58
59/* --- Local headers --- */
60
61#include "config.h"
62#include "utils.h"
63
03f996bd 64/*----- Program name handling ---------------------------------------------*/
c4f2d992 65
03f996bd 66/* --- Static data --- */
c4f2d992 67
03f996bd 68static const char *myname = 0; /* What's my name? */
c4f2d992 69
70/* --- @quis@ --- *
71 *
72 * Arguments: ---
73 *
74 * Returns: Pointer to the program name.
75 *
76 * Use: Returns the program name.
77 */
78
79const char *quis(void)
80{
81 return (myname);
82}
83
84/* --- @ego@ --- *
85 *
86 * Arguments: @const char *p@ = pointer to program name
87 *
88 * Returns: ---
89 *
90 * Use: Tells the utils library what the program's name is.
91 */
92
93#ifndef PATHSEP
94# if defined(__riscos)
95# define PATHSEP '.'
96# elif defined(__unix) || defined(unix)
97# define PATHSEP '/'
98# else
99# define PATHSEP '\\'
100# endif
101#endif
102
103void ego(const char *p)
104{
105 const char *q = p;
106 while (*q) {
107 if (*q++ == PATHSEP)
108 p = q;
109 }
110 myname = p;
111}
112
113/*----- Error reporting ---------------------------------------------------*/
114
115/* --- @moan@ --- *
116 *
117 * Arguments: @const char *f@ = a @printf@-style format string
118 * @...@ = other arguments
119 *
120 * Returns: ---
121 *
122 * Use: Reports an error.
123 */
124
125void moan(const char *f, ...)
126{
127 va_list ap;
128 va_start(ap, f);
129 fprintf(stderr, "%s: ", myname);
130 vfprintf(stderr, f, ap);
131 va_end(ap);
132 putc('\n', stderr);
133}
134
135/* --- @die@ --- *
136 *
137 * Arguments: @const char *f@ = a @printf@-style format string
138 * @...@ = other arguments
139 *
140 * Returns: Never.
141 *
142 * Use: Reports an error and hari-kiris. Like @moan@ above, only
143 * more permanent.
144 */
145
146void die(const char *f, ...)
147{
148 va_list ap;
149 va_start(ap, f);
150 fprintf(stderr, "%s: ", myname);
151 vfprintf(stderr, f, ap);
152 va_end(ap);
153 putc('\n', stderr);
154 exit(EXIT_FAILURE);
155}
156
03f996bd 157/*----- Trace messages ----------------------------------------------------*/
158
159#if defined(TRACING) || !defined(NDEBUG)
160
161/* --- Static data --- */
162
163static FILE *tracefp = 0; /* Where does debugging go? */
164static unsigned int tracelvl = 0; /* How much tracing gets done? */
165
166/* --- @trace@ --- *
167 *
168 * Arguments: @unsigned int lvl@ = trace level for output
169 * @const char *f@ = a @printf@-style format string
170 * @...@ = other arguments
171 *
172 * Returns: ---
173 *
174 * Use: Reports a message to the trace output.
175 */
176
177void trace(unsigned int lvl, const char *f, ...)
178{
179 va_list ap;
180 if ((lvl & tracing()) == 0)
181 return;
182 va_start(ap, f);
183 fprintf(tracefp, "*** %s: ", myname);
184 vfprintf(tracefp, f, ap);
185 va_end(ap);
186 putc('\n', tracefp);
d0168b65 187 fflush(tracefp);
03f996bd 188}
189
190/* --- @traceblk@ --- *
191 *
192 * Arguments: @unsigned int lvl@ = trace level for output
193 * @const char *hdr@ = some header string to write
194 * @const void *blk@ = pointer to a block of memory to dump
195 * @size_t sz@ = size of the block of memory
196 *
197 * Returns: ---
198 *
199 * Use: Dumps the contents of a block to the trace output.
200 */
201
202void traceblk(unsigned int lvl, const char *hdr, const void *blk, size_t sz)
203{
204 const unsigned char *p = blk;
205 size_t i;
206 unsigned long o = 0;
207 size_t c;
208
209 /* --- Skip if the trace level is too high --- */
210
211 if ((lvl & tracing()) == 0)
212 return;
213
214 /* --- Now start work --- */
215
216 fprintf(tracefp, "*** %s: %s\n", myname, hdr);
217
218 while (sz) {
219 fprintf(tracefp, "*** %s: %08lu : ", myname, o);
220 for (i = 0; i < 8; i++) {
221 if (i < sz)
222 fprintf(tracefp, "%02x ", p[i]);
223 else
224 fputs("** ", tracefp);
225 }
226 fputs(": ", tracefp);
227 for (i = 0; i < 8; i++) {
228 if (i < sz)
229 fputc(isprint(p[i]) ? p[i] : '.', tracefp);
230 else
231 fputc('*', tracefp);
232 }
233 fputc('\n', tracefp);
234 c = (sz >= 8) ? 8 : sz;
235 sz -= c, p += c, o += c;
236 }
d0168b65 237 fflush(tracefp);
03f996bd 238}
239
03f996bd 240/* --- @traceon@ --- *
241 *
242 * Arguments: @FILE *fp@ = a file to trace on
243 * @unsigned int lvl@ = trace level to set
244 *
245 * Returns: ---
246 *
247 * Use: Enables tracing to a file.
248 */
249
250void traceon(FILE *fp, unsigned int lvl)
251{
252 tracefp = fp;
253 if (!tracelvl)
254 tracelvl = lvl;
255}
256
257/* --- @tracesetlvl@ --- *
258 *
259 * Arguments: @unsigned int lvl@ = trace level to set
260 *
261 * Returns: ---
262 *
263 * Use: Sets the tracing level.
264 */
265
266void tracesetlvl(unsigned int lvl) { tracelvl = lvl; }
267
268/* --- @tracing@ --- *
269 *
270 * Arguments: ---
271 *
272 * Returns: Zero if not tracing, tracing level if tracing.
273 *
274 * Use: Informs the caller whether tracing is enabled.
275 */
276
277unsigned int tracing(void) { return (tracefp ? tracelvl : 0u); }
278
279#endif
280
c4f2d992 281/*----- Memory management functions ---------------------------------------*/
282
283/* --- @xmalloc@ --- *
284 *
285 * Arguments: @size_t sz@ = size of block to allocate
286 *
287 * Returns: Pointer to allocated block.
288 *
289 * Use: Allocates memory. If the memory isn't available, we don't
290 * hang aroung long enough for a normal function return.
291 */
292
293void *xmalloc(size_t sz)
294{
295 void *p = malloc(sz);
296 if (!p)
297 die("not enough memory");
298 return (p);
299}
300
301/* --- @xstrdup@ --- *
302 *
303 * Arguments: @const char *s@ = pointer to a string
304 *
305 * Returns: Pointer to a copy of the string.
306 *
307 * Use: Copies a string (like @strdup@ would, if it existed).
308 */
309
310char *xstrdup(const char *s)
311{
312 size_t sz = strlen(s) + 1;
313 char *p = xmalloc(sz);
314 memcpy(p, s, sz);
315 return (p);
316}
317
318/* --- @xrealloc@ --- *
319 *
320 * Arguments: @void *p@ = pointer to a block of memory
321 * @size_t sz@ = new size desired for the block
322 *
323 * Returns: Pointer to the resized memory block (which is almost
324 * certainly not in the same place any more).
325 *
326 * Use: Resizes a memory block.
327 */
328
329void *xrealloc(void *p, size_t sz)
330{
331 p = realloc(p, sz);
332 if (!p)
333 die("not enough memory");
334 return (p);
335}
336
deab2ee8 337/*----- Simple memory use tracking ----------------------------------------*/
338
339#ifdef TRACK_MALLOC
340
341/*#define TRACK_VERBOSE*/
342
343/* --- A type to record a size and have a nice alignment --- */
344
345typedef union szblock {
346 struct {
347 union szblock *next;
348 union szblock *prev;
349 size_t sz;
350 } x;
351 long double _ld;
352 void *_p;
353} szblock;
354
355/* --- Static data --- */
356
357static unsigned int memused = 0;
358static szblock *memlist;
359
360/* --- @track_malloc@ --- *
361 *
362 * Arguments: @size_t sz@ = size requested
363 *
364 * Returns: Pointer to allocated space, or null
365 *
366 * Use: Allocates memory, and tracks how much is allocated.
367 */
368
369void *track_malloc(size_t sz)
370{
371 szblock *q = (malloc)(sz + sizeof(szblock));
372 if (q) {
373 memused += sz;
374#ifdef TRACK_VERBOSE
88c62e21 375 fflush(0);
deab2ee8 376 printf("[%p] allocated %lu\n", (void *)(q + 1), (unsigned long)sz);
88c62e21 377 fflush(stdout);
deab2ee8 378#endif
379 q->x.sz = sz;
380 q->x.next = memlist;
381 q->x.prev = 0;
382 if (q->x.next)
383 q->x.next->x.prev = q;
384 memlist = q;
385 return (q + 1);
386 }
387 return (0);
388}
389
390/* --- @track_free@ --- *
391 *
392 * Arguments: @void *p@ = pointer to an allocated block
393 *
394 * Returns: ---
395 *
396 * Use: Frees memory, and tracks how much is still allocated.
397 */
398
399void track_free(void *p)
400{
401 szblock *q;
402
403 if (!p)
404 return;
405 q = (szblock *)p - 1;
406#ifdef TRACK_VERBOSE
88c62e21 407 fflush(0);
deab2ee8 408 printf("[%p] freed %lu\n", (void *)(q + 1), (unsigned long)q->x.sz);
88c62e21 409 fflush(stdout);
deab2ee8 410#endif
411 if (q->x.next)
412 q->x.next->x.prev = q->x.prev;
413 if (q->x.prev)
414 q->x.prev->x.next = q->x.next;
415 else
416 memlist = q->x.next;
417 memused -= q->x.sz;
418 (free)(q);
419}
420
421/* --- @track_realloc@ --- *
422 *
423 * Arguments: @void *p@ = pointer to an allocated block
424 * @size_t sz@ = how big it wants to be
425 *
426 * Returns: Pointer to the new block.
427 *
428 * Use: Reallocates a block, tracking how much memory is still
429 * available.
430 */
431
432void *track_realloc(void *p, size_t sz)
433{
434 size_t osz;
435 szblock *q, *qq;
436 if (p) {
437 q = (szblock *)p - 1;
438 osz = q->x.sz;
439 if (q->x.next)
440 q->x.next->x.prev = q->x.prev;
441 if (q->x.prev)
442 q->x.prev->x.next = q->x.next;
443 else
444 memlist = q->x.next;
445 } else {
446 q = 0;
447 osz = 0;
448 }
449 qq = (realloc)(q, sz + sizeof(szblock));
450 if (qq) {
451#ifdef TRACK_VERBOSE
88c62e21 452 fflush(0);
deab2ee8 453 printf("[%p->%p] reallocated %lu -> %lu\n",
454 (void *)(q + 1), (void *)(qq + 1),
455 (unsigned long)osz, (unsigned long)sz);
88c62e21 456 fflush(stdout);
deab2ee8 457#endif
458 qq->x.sz = sz;
459 qq->x.next = memlist;
460 qq->x.prev = 0;
461 if (qq->x.next)
462 qq->x.next->x.prev = qq;
463 memlist = qq;
464 memused += sz - osz;
465 qq->x.sz = sz;
466 return (qq + 1);
467 }
468 return (0);
469}
470
471/* --- @track_memused@ --- *
472 *
473 * Arguments: ---
474 *
475 * Returns: A count of how much memory is used currently.
476 *
477 * Use: Returns the amount of memory which the @track_@-functions
478 * above have counted as being currently allocated.
479 */
480
481unsigned long track_memused(void)
482{
483 return (memused);
484}
485
486/* --- @track_memlist@ --- *
487 *
488 * Arguments: ---
489 *
490 * Returns: ---
491 *
492 * Use: Dumps a list of allocated blocks to standard output.
493 */
494
495void track_memlist(void)
496{
497 szblock *q = memlist;
88c62e21 498 fflush(0);
deab2ee8 499 printf("listing blocks:\n");
500 while (q) {
501 printf("... [%p] %lu\n", (void *)(q + 1), (unsigned long)q->x.sz);
502 q = q->x.next;
503 }
504 printf("done\n");
88c62e21 505 fflush(stdout);
deab2ee8 506}
507
508#endif
509
c4f2d992 510/*----- That's all, folks -------------------------------------------------*/