-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgarbage.c
439 lines (407 loc) · 11.3 KB
/
garbage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*************************************************************************/
/* This module implements the garbage collector. */
/* It works by local interactions, propagating all the erase-operators */
/* in the graph. Each erase-operator terminates its travelling either by */
/* annihilating against another one, or being "absorbed" by a different */
/* operator, such as "lambdaunb", "uns_fan1", "uns_fan2". */
/* When the garbage collector is activated, it finds all erase operators */
/* looking into a list in which they are inserted when created. */
/* The following functions are external: */
/* - init_garbage: it initializes the erase-list inserting the first */
/* node. */
/* - ins_del(): insert a new erase operator at the head of a list */
/* to be scanned when the G.C. is activated. */
/* - clean(): it activates the G.C. by scanning the erase-list and by */
/* propagating them in the graph. It uses the local function */
/* "garbage()" which propagates a single node and */
/* inserts in the erases list new operators originated by */
/* duplication rules during travelling. */
/* - user(): it only calls the previous function and prints some data */
/* when the user digits the directive"#garbage". */
/* The following functions are internal: */
/* - garbage(): It performs the propagation of a single erase node by */
/* applicating garbage rules. */
/*************************************************************************/
/*************************************************************************/
/* 1. Inclusion of header files. */
/*************************************************************************/
#include "bohm.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
/*************************************************************************/
/* 2. Inclusion of declarations that are being imported. */
/*************************************************************************/
/*************************************************************************/
/* 3. Declaration of names strictly local to the module. */
/*************************************************************************/
/* constants concerning garbage */
#define EXISTENT 3
#define NOTEXISTENT 4
static long unsigned er_count; /* counter for erasing operations */
static long unsigned cl_count; /* counter for clean() calls */
static clock_t usr_garb_time;
static clock_t sys_garb_time;
static void garbage();
/*************************************************************************/
/* 4. Definitions of variables to be exported. */
/*************************************************************************/
FORM *del_head=NULL; /* head of erases list */
/*************************************************************************/
/* 5. Definitions of functions to be exported. */
/*************************************************************************/
/* The following function initializes the erase-list inserting */
/* the first node. */
void init_garbage()
{
del_head=(FORM *)malloc_da(sizeof(FORM));
del_head->nform[1]=NULL;
}
/* The following function insert a new erase operator at the */
/* head of a list to be scanned when the G.C. is activated. */
void ins_del(d)
FORM *d;
{
d->index=EXISTENT;
d->nform[1]=del_head->nform[1];
del_head->nform[1]=d;
}
/* The following function activates the G.C. by scanning the */
/* erase-list and by propagating them in the graph. It uses */
/* the local function "garbage()" which propagates a single */
/* node and inserts in the erases list new operators */
/* originated by duplication rules during travelling. */
void clean()
{
FORM *q;
struct tms partial_time, final_time;
if (seegarb)
times(&partial_time);
cl_count++;
while((q=del_head->nform[1])!=NULL) {
del_head->nform[1]=q->nform[1];
if((q->index)==NOTEXISTENT)
myfree(q);
else
garbage(q);
}
if (seegarb) {
times(&final_time);
usr_garb_time+=final_time.tms_utime - partial_time.tms_utime;
usr_garb_time+=final_time.tms_stime - partial_time.tms_stime;
}
}
/* The following function it only calls the previous function */
/* and prints some data when the user digits the directive */
/* "#garbage". */
void user()
{
printf("*****************************************************\n");
printf("Initial number of nodes %u\n",num_nodes);
printf("Please wait . . .\n");
clean();
printf("Final number of nodes %u\n",num_nodes);
printf("*****************************************************\n");
}
/************************************************ ************************/
/* 6. Definitions of functions strictly local to the module. */
/*************************************************************************/
/* The following function performs the propagation of a single */
/* erase node by applicating garbage rules. */
static void garbage(erase)
FORM *erase;
{
bool end=false;
FORM *form,*nextform,*newform;
int port,nextport;
int p1,p2;
nextform=erase->nform[0];
nextport=erase->nport[0];
myfree(erase);
while(!end) {
form=nextform;
port=nextport;
if(port<0) {
er_count++;
end=true;
}
else {
switch(form->name){
case TESTNIL1:
if(port==0) {
er_count++;
form->name=ERASE;
nextform=form->nform[2];
nextport=form->nport[2];
connect1(form,0,form->nform[1],form->nport[1]);
ins_del(form);
}
else {
if(port==1) {
if (form->nlevel[2]==0) {
connect1(form->nform[2],
form->nport[2],
form->nform[0],
form->nport[0]);
myfree(form);
}
else {
form->name=TRIANGLE;
connect1(form,1,form->nform[2],form->nport[2]);
form->nlevel[1] = form->nlevel[2];
form->index-=1;
}
}
else {
if (form->nlevel[1]==0) {
form->name=TESTNIL;
}
else {
form->name=TRIANGLE;
allocate_form(&newform,TESTNIL,form->index);
connect1(newform,0,form->nform[0],form->nport[0]);
connect(newform,1,form,0);
form->index-=1;
}
}
end=true;
}
break;
case CAR1:
if(port==0) {
er_count++;
form->name=ERASE;
nextform=form->nform[2];
nextport=form->nport[2];
connect1(form,0,form->nform[1],form->nport[1]);
ins_del(form);
}
else {
if(port==1) {
if (form->nlevel[2]==0) {
connect1(form->nform[2],
form->nport[2],
form->nform[0],
form->nport[0]);
myfree(form);
}
else {
form->name=TRIANGLE;
connect1(form,1,form->nform[2],form->nport[2]);
form->nlevel[1] = form->nlevel[2];
form->index-=1;
}
}
else {
if (form->nlevel[1]==0) {
form->name=CAR;
}
else {
form->name=TRIANGLE;
allocate_form(&newform,CAR,form->index);
connect1(newform,0,form->nform[0],form->nport[0]);
connect(newform,1,form,0);
form->index-=1;
}
}
end=true;
}
break;
case CDR1:
if(port==0) {
er_count++;
form->name=ERASE;
nextform=form->nform[2];
nextport=form->nport[2];
connect1(form,0,form->nform[1],form->nport[1]);
ins_del(form);
}
else {
if(port==1) {
if (form->nlevel[2]==0) {
connect1(form->nform[2],
form->nport[2],
form->nform[0],
form->nport[0]);
myfree(form);
}
else {
form->name=TRIANGLE;
connect1(form,1,form->nform[2],form->nport[2]);
form->nlevel[1] = form->nlevel[2];
form->index-=1;
}
}
else {
if (form->nlevel[1]==0) {
form->name=CDR;
}
else {
form->name=TRIANGLE;
allocate_form(&newform,CDR,form->index);
connect1(newform,0,form->nform[0],form->nport[0]);
connect(newform,1,form,0);
form->index-=1;
}
}
end=true;
}
break;
case CONS1:
if(port==0) {
er_count++;
nextform=form->nform[2];
nextport=form->nport[2];
form->name=ERASE;
connect1(form,0,form->nform[1],form->nport[1]);
ins_del(form);
}
else {
if(port==1) {
form->name=CDR;
connect1(form->nform[2],form->nport[2],form,1);
}
else
form->name=CAR;
end=true;
}
break;
case FAN:
if(port==0) {
er_count++;
nextform=form->nform[2];
nextport=form->nport[2];
form->name=ERASE;
connect1(form,0,form->nform[1],form->nport[1]);
ins_del(form);
}
else {
if(form->num_safe)
if (port==1){
form->name=TRIANGLE;
connect1(form,1,form->nform[2],form->nport[2]);
form->nlevel[1] = form->nlevel[2];
}
else
form->name=TRIANGLE;
else
if(port==1){
form->name=UNS_FAN1;
connect1(form,1,form->nform[2],form->nport[2]);
form->nlevel[1] = form->nlevel[2];
}
else
form->name=UNS_FAN2;
end=true;
}
break;
case TRIANGLE:
case LAMBDAUNB:
case TESTNIL:
case CAR:
case CDR:
case NOT:
case EQ1:
case NOTEQ1:
case LEQ1:
case MEQ1:
case MORE1:
case LESS1:
case ADD1:
case SUB1:
case PROD1:
case DIV1:
case MOD1:
case UNS_FAN1:
case UNS_FAN2:
er_count++;
if (port==0) {
nextform=form->nform[1];
nextport=form->nport[1];
}
else {
nextform=form->nform[0];
nextport=form->nport[0];
}
myfree(form);
break;
case APP:
case CONS:
case EQ:
case NOTEQ:
case LEQ:
case MEQ:
case MORE:
case LESS:
case ADD:
case SUB:
case PROD:
case DIV:
case MOD:
case AND:
case OR:
case IFELSE:
er_count++;
switch (port){
case 0:
p1=1;p2=2;
break;
case 1:
p1=0;p2=2;
break;
case 2:
p1=0;p2=1;
break;
}
nextform=form->nform[p2];
nextport=form->nport[p2];
form->name=ERASE;
connect1(form,0,form->nform[p1],form->nport[p1]);
ins_del(form);
break;
case LAMBDA:
if(port!=2){
er_count++;
nextform=form->nform[!port];
nextport=form->nport[!port];
form->name=ERASE;
connect1(form,0,form->nform[2],form->nport[2]);
ins_del(form);
}
else{
form->name=LAMBDAUNB;
end=true;
}
break;
case ERASE:
er_count++;
form->index=NOTEXISTENT;
end=true;
break;
default:
printf("Error: form %d\n",(erase->nform[0])->name);
exit(1);
break;
}
}
}
}
void reset_garbage()
{
er_count = 0;
cl_count = 0;
usr_garb_time = 0;
sys_garb_time = 0;
}
void show_garb_stat(seetime)
bool seetime;
{
printf("Total number of garbage calls %lu\n", cl_count);
printf("Total number of garbage operations %lu\n", er_count);
if (seetime)
{
printf("Garbage collection done in %.2f:usr %.2f:sys seconds\n",
(double)usr_garb_time / 60, (double)sys_garb_time / 60);
printf("*****************************************************\n");
}
}