/[ascend]/trunk/base/generic/compiler/dump.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/dump.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 103 - (show annotations) (download) (as text)
Sun Dec 11 14:31:02 2005 UTC (13 years, 9 months ago) by jds
File MIME type: text/x-csrc
File size: 6960 byte(s)
Bug fixes & killing more compiler warnings:
- general/list.c - fixed AssertAllocatedMemory and AssertContainedMemory bugs.
- added #include of ascPanic.h where necessary to compile with ALLOCATED_TESTS defined.
- reworked utilities/ascDynaLoad to eliminate compiler warnings related to casting between data & function pointers.
- switched utilities/ascEnvVar.c over to ascstrdup (had static ascstringcopy, which did the same thing).

All regression tests passed on Windows and Linux (except pre-existing solver/slv_common issues).
1 /*
2 * Instance Garbage Dump
3 * by Tom Epperly
4 * 10/24/89
5 * Version: $Revision: 1.10 $
6 * Version control file: $RCSfile: dump.c,v $
7 * Date last modified: $Date: 1998/02/05 22:23:24 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU 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 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING.
28 *
29 * Implementation of instance garbage dump using hash tables.
30 */
31
32 #include "utilities/ascConfig.h"
33 #include "compiler/compiler.h"
34 #include "utilities/ascPanic.h"
35 #include "utilities/ascMalloc.h"
36 #include "general/list.h"
37 #include "compiler/instance_enum.h"
38 #include "compiler/cmpfunc.h"
39 #include "compiler/symtab.h"
40 #include "compiler/destroyinst.h"
41 #include "compiler/instquery.h"
42 #include "compiler/dump.h"
43
44 #ifndef lint
45 static CONST char GarbageDumpID[] = "$Id: dump.c,v 1.10 1998/02/05 22:23:24 ballan Exp $";
46 #endif
47
48 /*
49 * hash table keyed by type name pointer. chain is for some
50 * bizarre reason alphabetized within each bucket.
51 */
52 #define DUMPHASHSIZE 1024
53 /*
54 * macro for hashing pointers. shift, mask must match dump SIZE.
55 */
56 #define DUMPHASHINDEX(p) (((((long) (p))*1103515245) >> 20) & 1023)
57
58
59 #define DUMPLISTSIZE 10L
60
61 struct DumpRec {
62 struct DumpRec *next;
63 symchar *type;
64 struct gl_list_t *instances;
65 };
66
67 struct DumpRec *g_dump_ht[DUMPHASHSIZE];
68 unsigned long g_dump_type_count,g_dump_inst_count;
69
70 void InitDump(void)
71 {
72 register unsigned c;
73 for(c=0;c<DUMPHASHSIZE;g_dump_ht[c++]=NULL);
74 g_dump_type_count = 0;
75 g_dump_inst_count = 0;
76 }
77
78 void EmptyTrash(void)
79 {
80 register unsigned c;
81 register unsigned long i,len;
82 register struct DumpRec *p,*next;
83 register struct gl_list_t *l;
84 if (g_dump_inst_count==0) return;
85 for(c=0;c<DUMPHASHSIZE;c++) {
86 p = g_dump_ht[c];
87 g_dump_ht[c]=NULL;
88 while (p!=NULL) {
89 AssertAllocatedMemory(p,sizeof(struct DumpRec));
90 next = p->next;
91 if ((l=p->instances)!=NULL) {
92 len = gl_length(l);
93 for(i=1;i<=len;i++)
94 DestroyInstance((struct Instance *)gl_fetch(l,i),NULL);
95 gl_destroy(l);
96 }
97 ascfree((char *)p);
98 p = next;
99 }
100 }
101 g_dump_type_count = 0;
102 g_dump_inst_count = 0;
103 }
104
105 void TendTrash(void)
106 {
107 register unsigned c;
108 register unsigned long i;
109 register struct DumpRec *p;
110 register struct gl_list_t *l;
111 if (g_dump_inst_count <= MESSYTHRESH) return;
112 for(c=0;c<DUMPHASHSIZE;c++) {
113 p = g_dump_ht[c];
114 while(p!=NULL) {
115 if ((l=p->instances)!=NULL) {
116 for (i=gl_length(l);i>MESSYTHRESH;i--) {
117 DestroyInstance((struct Instance *)gl_fetch(l,i),NULL);
118 g_dump_inst_count--;
119 gl_delete(l,i,0);
120 }
121 }
122 p = p->next;
123 }
124 }
125 }
126
127 void TrashType(symchar *str)
128 {
129 register unsigned long c,len,bucket;
130 register struct DumpRec *p,*prev;
131 register struct gl_list_t *l;
132 register int cmp;
133 assert(AscFindSymbol(str)!=NULL);
134 if (*(SCP(str)) == '\0') return;
135 bucket = DUMPHASHINDEX(SCP(str));
136 if ((p = g_dump_ht[bucket])==NULL) return;
137 cmp = CmpSymchar(p->type,str);
138 if (cmp == 0)
139 g_dump_ht[bucket] = p->next;
140 else
141 if (cmp > 0) return;
142 else {
143 prev = p;
144 if ((p = p->next)==NULL) return;
145 while((cmp=CmpSymchar(p->type,str))<0) {
146 prev = p;
147 if ((p=p->next)==NULL) return;
148 }
149 if (cmp!=0) return;
150 /* remove from linked list */
151 prev->next = p->next;
152 }
153 if ((l=p->instances)!=NULL) {
154 len = gl_length(l);
155 for(c=1;c<=len;c++)
156 DestroyInstance((struct Instance *)gl_fetch(l,c),NULL);
157 gl_destroy(l);
158 g_dump_inst_count -= len;
159 }
160 g_dump_type_count--;
161 ascfree((char *)p);
162 }
163
164 struct Instance *FindInstance(symchar *str)
165 {
166 register struct DumpRec *p,*prev;
167 register int cmp;
168 register struct gl_list_t *l;
169 register struct Instance *result;
170 register unsigned long bucket;
171 if (*(SCP(str)) == '\0') return NULL;
172 bucket = DUMPHASHINDEX(SCP(str));
173 if ((p = g_dump_ht[bucket])==NULL) return NULL;
174 if ((cmp=CmpSymchar(p->type,str)) > 0) return NULL;
175 else {
176 if (cmp==0) {
177 l = p->instances;
178 result = (struct Instance *)gl_fetch(l,gl_length(l));
179 gl_delete(l,gl_length(l),0);
180 g_dump_inst_count--;
181 if (gl_length(l)==0) {
182 gl_destroy(l);
183 g_dump_ht[bucket] = p->next;
184 g_dump_type_count--;
185 ascfree((char *)p);
186 }
187 }
188 else {
189 prev = p;
190 if ((p = p->next)==NULL) return NULL;
191 while((cmp=CmpSymchar(p->type,str))<0) {
192 prev = p;
193 if ((p=p->next)==NULL) return NULL;
194 }
195 if (cmp!=0) return NULL;
196 /* pick out instance */
197 l = p->instances;
198 result = (struct Instance *)gl_fetch(l,gl_length(l));
199 gl_delete(l,gl_length(l),0);
200 g_dump_inst_count--;
201 if (gl_length(l)==0) {
202 gl_destroy(l);
203 prev->next = p->next;
204 g_dump_type_count--;
205 ascfree((char *)p);
206 }
207 }
208 return result;
209 }
210 }
211
212 void AddInstance(struct Instance *i)
213
214 /* add instance i to the trash dump */
215 {
216 register struct DumpRec *p,*prev;
217 register symchar *type;
218 register int cmp;
219 register unsigned long bucket;
220 type = InstanceType(i);
221 if ((i==NULL)||(type==NULL)) return;
222 bucket = DUMPHASHINDEX(SCP(type));
223 if ((p=g_dump_ht[bucket])==NULL) {
224 g_dump_type_count++;
225 g_dump_ht[bucket] = (struct DumpRec *)ascmalloc(sizeof(struct DumpRec));
226 p = g_dump_ht[bucket];
227 p->next = NULL;
228 }
229 else {
230 cmp = CmpSymchar(p->type,type);
231 if (cmp > 0) {
232 g_dump_type_count++;
233 p = (struct DumpRec *)ascmalloc(sizeof(struct DumpRec));
234 p->next = g_dump_ht[bucket];
235 g_dump_ht[bucket] = p;
236 }
237 else {
238 if (cmp == 0) {
239 gl_append_ptr(p->instances,(VOIDPTR)i);
240 g_dump_inst_count++;
241 return;
242 }
243 else {
244 prev = p;
245 if ((p = p->next)!=NULL)
246 while ((p!=NULL)&&(cmp=CmpSymchar(p->type,type)) <0) {
247 prev = p;
248 p = p->next;
249 }
250 if (cmp == 0) { /* found a match */
251 gl_append_ptr(p->instances,(VOIDPTR)i);
252 g_dump_inst_count++;
253 return;
254 }
255 /* insert between prev and p */
256 g_dump_type_count++;
257 prev->next = (struct DumpRec *)ascmalloc(sizeof(struct DumpRec));
258 prev->next->next = p;
259 p = prev->next;
260 }
261 }
262 }
263 p->type = type;
264 p->instances = gl_create(DUMPLISTSIZE);
265 g_dump_inst_count++;
266 gl_append_ptr(p->instances,(VOIDPTR)i);
267 }
268
269

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22