emergency commit
[cl-cudd.git] / distr / mnemosyne / mnemalyse.c
blob60d2b914e31f23c80e863318e2d23c1ba9661766
1 /************************************************************************
2 * *
3 * Copyright (c) 1985 by *
4 * Digital Equipment Corporation, Maynard, MA *
5 * All rights reserved. *
6 * *
7 * The information in this software is subject to change without *
8 * notice and should not be construed as a commitment by Digital *
9 * Equipment Corporation. *
10 * *
11 * Digital assumes no responsibility for the use or reliability *
12 * of its software on equipment which is not supplied by Digital. *
13 * *
14 * Redistribution and use in source and binary forms are permitted *
15 * provided that the above copyright notice and this paragraph are *
16 * duplicated in all such forms and that any documentation, *
17 * advertising materials, and other materials related to such *
18 * distribution and use acknowledge that the software was developed *
19 * by Digital Equipment Corporation. The name of Digital Equipment *
20 * Corporation may not be used to endorse or promote products derived *
21 * from this software without specific prior written permission. *
22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR *
23 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED *
24 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.*
25 * Do not take internally. In case of accidental ingestion, contact *
26 * your physician immediately. *
27 * *
28 ************************************************************************/
30 /* DO NOT INCLUDE "mnemosyne.h" !!! */
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/file.h>
36 static char rcsid[] = "/fats/tools/hsv/mnemosyne/mnemalyse.c,v 1.1.1.1 1995/06/06 18:18:28 fabio Exp";
38 #include "mnemconf.h"
40 extern char *index();
43 post-processor to interpret memory allocation maps and search for
44 pointers that were allocated but never freed.
46 Marcus J. Ranum, 1990. (mjr@decuac.dec.com)
51 simple and braindead, read in the ".lines" file, and store it in a
52 table by number. then read the pointer map, and crossref any unfreed
53 pointers. simple as dereferencing NULL...
55 this could use some cleaning and buffing, but it's damn effective as
56 it is. again, fancier symbol table routines would make this faster,
57 but who gives a damn? it only has to be faster than finding memory
58 leaks by hand...
61 struct xsym {
62 char *dat;
63 int lnum;
64 int map;
65 struct xsym *nxt;
70 main()
72 register struct xsym *sp;
73 register struct xsym *zp;
74 struct ptr p;
75 struct xsym *shash[HASHSIZ];
76 char inbuf[BUFSIZ];
77 FILE *lp;
78 int fd;
80 /* statistics */
81 int ptrcnt = 0;
82 int ptrbad = 0;
83 int ptrlos = 0;
85 /* to chop up lines */
86 char *cpmap;
87 char *cpcalls;
88 char *cpave;
89 char *cplnum;
90 char *cpfnam;
92 for(fd = 0; fd < HASHSIZ; fd++)
93 shash[fd] = (struct xsym *)0;
95 if((lp = fopen(LINESFILE,"r")) == (FILE *)0) {
96 perror(LINESFILE);
97 exit(1);
100 if((fd = open(PTRFILE,O_RDONLY|O_RDWR)) < 0) {
101 perror(PTRFILE);
102 exit(1);
105 /* this is ugly, but I refuse to trust !@(#&U!@#&! sscanf() */
106 while((cpmap = fgets(inbuf,sizeof(inbuf),lp)) != (char *)0) {
107 if(inbuf[0] == '#')
108 continue;
110 sp = (struct xsym *)malloc(sizeof(struct xsym));
111 if(sp == (struct xsym *)0) {
112 perror("malloc");
113 exit(1);
115 sp->lnum = sp->map = 0;
117 if((cpcalls = index(cpmap,'\t')) != (char *)0)
118 *cpcalls++ = '\0';
120 if((cpave = index(cpcalls,'\t')) != (char *)0)
121 *cpave++ = '\0';
123 if((cplnum = index(cpave,'\t')) != (char *)0)
124 *cplnum++ = '\0';
126 if((cpfnam = index(cplnum,'\t')) != (char *)0)
127 *cpfnam++ = '\0';
129 /* setup symbol */
130 sp->map = atoi(cpmap);
132 if(cplnum == (char *)0)
133 sp->lnum = -1;
134 else
135 sp->lnum = atoi(cplnum);
137 if(cpfnam != (char *)0) {
138 char *x;
139 if((x = index(cpfnam,'\n')) != (char *)0)
140 *x = '\0';
142 sp->dat = malloc((unsigned)(strlen(cpfnam) + 1));
143 if(sp->dat == (char *)0) {
144 perror("malloc");
145 exit(1);
147 (void)strcpy(sp->dat,cpfnam);
148 } else
149 sp->dat = "unknown";
151 /* check to make sure it is not already in table */
152 zp = shash[sp->map % HASHSIZ];
153 while(zp != (struct xsym *)0) {
154 if(zp->map == sp->map) {
155 (void)fprintf(stderr,
156 "mnemalyse: duplicate map entry ignored");
157 (void)fprintf(stderr,
158 " (point at both %s and %s)\n",sp->dat,zp->dat);
159 (void)free(sp);
161 /* can't free dat - may not be malloced! */
162 sp = (struct xsym *)0;
163 break;
165 zp = zp->nxt;
168 /* shrug, link it in */
169 if(sp != (struct xsym *)0) {
170 sp->nxt = shash[sp->map % HASHSIZ];
171 shash[sp->map % HASHSIZ] = sp;
174 (void)fclose(lp);
176 while(read(fd,(char *)&(p.dsk),sizeof(p.dsk)) == sizeof(p.dsk)) {
178 /* if the pointer was not deallocated, note it */
179 if(p.dsk.siz != 0) {
180 zp = shash[p.dsk.smap % HASHSIZ];
181 while(zp != (struct xsym *)0) {
182 if(zp->map == p.dsk.smap) {
183 printf("%d bytes missing %s line:%d\n",
184 p.dsk.siz,zp->dat,zp->lnum);
186 zp = zp->nxt;
188 ptrbad++;
189 ptrlos += p.dsk.siz;
191 ptrcnt++;
194 printf("%d pointers, %d lost totalling %d bytes\n",
195 ptrcnt,ptrbad,ptrlos);
196 exit(0);