-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem.c
246 lines (216 loc) · 6.23 KB
/
mem.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
/***** ltl2ba : mem.c *****/
/* Written by Denis Oddoux, LIAFA, France */
/* Copyright (c) 2001 Denis Oddoux */
/* Modified by Paul Gastin, LSV, France */
/* Copyright (c) 2007 Paul Gastin */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/
/* */
/* Based on the translation algorithm by Gastin and Oddoux, */
/* presented at the 13th International Conference on Computer Aided */
/* Verification, CAV 2001, Paris, France. */
/* Proceedings - LNCS 2102, pp. 53-65 */
/* */
/* Send bug-reports and/or questions to Paul Gastin */
/* http://www.lsv.ens-cachan.fr/~gastin */
/* */
/* Some of the code in this file was taken from the Spin software */
/* Written by Gerard J. Holzmann, Bell Laboratories, U.S.A. */
#include "config.h"
#include "ltl2ba.h"
#if 1
#define log(e, u, d) event[e][(int) u] += (long) d;
#else
#define log(e, u, d)
#endif
#define A_LARGE 80
#define A_USER 0x55000000
#define NOTOOBIG 32768
#define POOL 0
#define ALLOC 1
#define FREE 2
#define NREVENT 3
extern unsigned long All_Mem;
extern int tl_verbose;
ATrans *atrans_list = (ATrans *)0;
GTrans *gtrans_list = (GTrans *)0;
BTrans *btrans_list = (BTrans *)0;
int aallocs = 0, afrees = 0, apool = 0;
int gallocs = 0, gfrees = 0, gpool = 0;
int ballocs = 0, bfrees = 0, bpool = 0;
union M {
long size;
union M *link;
};
static union M *freelist[A_LARGE];
static long req[A_LARGE];
static long event[NREVENT][A_LARGE];
void *
tl_emalloc(int U)
{ union M *m;
long r, u;
void *rp;
u = (long) ((U-1)/sizeof(union M) + 2);
if (u >= A_LARGE)
{ log(ALLOC, 0, 1);
if (tl_verbose)
printf("tl_spin: memalloc %ld bytes\n", u);
m = (union M *) emalloc((int) u*sizeof(union M));
All_Mem += (unsigned long) u*sizeof(union M);
} else
{ if (!freelist[u])
{ r = req[u] += req[u] ? req[u] : 1;
if (r >= NOTOOBIG)
r = req[u] = NOTOOBIG;
log(POOL, u, r);
freelist[u] = (union M *)
emalloc((int) r*u*sizeof(union M));
All_Mem += (unsigned long) r*u*sizeof(union M);
m = freelist[u] + (r-2)*u;
for ( ; m >= freelist[u]; m -= u)
m->link = m+u;
}
log(ALLOC, u, 1);
m = freelist[u];
freelist[u] = m->link;
}
m->size = (u|A_USER);
for (r = 1; r < u; )
(&m->size)[r++] = 0;
rp = (void *) (m+1);
memset(rp, 0, U);
return rp;
}
void
tfree(void *v)
{ union M *m = (union M *) v;
long u;
--m;
if ((m->size&0xFF000000) != A_USER)
Fatal("releasing a free block", (char *)0);
u = (m->size &= 0xFFFFFF);
if (u >= A_LARGE)
{ log(FREE, 0, 1);
/* free(m); */
} else
{ log(FREE, u, 1);
m->link = freelist[u];
freelist[u] = m;
}
}
ATrans* emalloc_atrans() {
ATrans *result;
if(!atrans_list) {
result = (ATrans *)tl_emalloc(sizeof(GTrans));
result->pos = new_set(1);
result->neg = new_set(1);
result->to = new_set(0);
apool++;
}
else {
result = atrans_list;
atrans_list = atrans_list->nxt;
result->nxt = (ATrans *)0;
}
aallocs++;
return result;
}
void free_atrans(ATrans *t, int rec) {
if(!t) return;
if(rec) free_atrans(t->nxt, rec);
t->nxt = atrans_list;
atrans_list = t;
afrees++;
}
void free_all_atrans() {
ATrans *t;
while(atrans_list) {
t = atrans_list;
atrans_list = t->nxt;
tfree(t->to);
tfree(t->pos);
tfree(t->neg);
tfree(t);
}
}
GTrans* emalloc_gtrans() {
GTrans *result;
if(!gtrans_list) {
result = (GTrans *)tl_emalloc(sizeof(GTrans));
result->pos = new_set(1);
result->neg = new_set(1);
result->final = new_set(0);
gpool++;
}
else {
result = gtrans_list;
gtrans_list = gtrans_list->nxt;
}
gallocs++;
return result;
}
void free_gtrans(GTrans *t, GTrans *sentinel, int fly) {
gfrees++;
if(sentinel && (t != sentinel)) {
free_gtrans(t->nxt, sentinel, fly);
if(fly) t->to->incoming--;
}
t->nxt = gtrans_list;
gtrans_list = t;
}
BTrans* emalloc_btrans() {
BTrans *result;
if(!btrans_list) {
result = (BTrans *)tl_emalloc(sizeof(BTrans));
result->pos = new_set(1);
result->neg = new_set(1);
bpool++;
}
else {
result = btrans_list;
btrans_list = btrans_list->nxt;
}
ballocs++;
return result;
}
void free_btrans(BTrans *t, BTrans *sentinel, int fly) {
bfrees++;
if(sentinel && (t != sentinel)) {
free_btrans(t->nxt, sentinel, fly);
if(fly) t->to->incoming--;
}
t->nxt = btrans_list;
btrans_list = t;
}
void
a_stats(void)
{ long p, a, f;
int i;
printf(" size\t pool\tallocs\t frees\n");
for (i = 0; i < A_LARGE; i++)
{ p = event[POOL][i];
a = event[ALLOC][i];
f = event[FREE][i];
if(p|a|f)
printf("%5d\t%6ld\t%6ld\t%6ld\n",
i, p, a, f);
}
printf("atrans\t%6d\t%6d\t%6d\n",
apool, aallocs, afrees);
printf("gtrans\t%6d\t%6d\t%6d\n",
gpool, gallocs, gfrees);
printf("btrans\t%6d\t%6d\t%6d\n",
bpool, ballocs, bfrees);
}