Repository navigation
Expand file tree
/
Copy pathcuddauxAddCamlTable.c
More file actions
272 lines (227 loc) · 7.87 KB
/
Copy pathcuddauxAddCamlTable.c
File metadata and controls
272 lines (227 loc) · 7.87 KB
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
/**CFile***********************************************************************
FileName [cuddauxTable.c]
PackageName [cuddaux]
Synopsis [Allows to put pointers in constant ADD node]
Description [Miscellaneous operations..]
External procedures included in this module:
<ul>
<li>
</ul>
Internal procedures included in this module:
<ul>
<li>
</ul>
]
Author [Bertrand Jeannet]
Copyright []
******************************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "cuddInt.h"
#include "util.h"
#include "st.h"
#include "cuddaux.h"
#include "caml/memory.h"
extern intnat caml_stat_compactions;
static intnat old_compactions = -1;
typedef union myhack {
value value;
unsigned int bits[2];
} myhack;
/*---------------------------------------------------------------------------*/
/* Definition of exported functions */
/*---------------------------------------------------------------------------*/
/**Function********************************************************************
Synopsis [Checks the unique table for the existence of a constant node.]
Description [Copy of cuddUniqueConst.
Checks the unique table for the existence of a constant node.
If it does not exist, it creates a new one. Does not
modify the reference count of whatever is returned. A newly created
internal node comes back with a reference count 0. Returns a
pointer to the new node.]
SideEffects [None]
***************************************************************************/
DdNode *
Cuddaux_addCamlConst(DdManager * unique,
value value)
{
CAMLparam1(value);
int pos;
DdNodePtr *nodelist;
DdNode *looking;
myhack split;
if (
value==((struct CuddauxDdNode*)unique->one)->type.value ||
value==((struct CuddauxDdNode*)unique->zero)->type.value ||
value==((struct CuddauxDdNode*)unique->plusinfinity)->type.value ||
value==((struct CuddauxDdNode*)unique->minusinfinity)->type.value
){
fprintf(stderr,"\ncuddaux/mlcuddidl: big problem: CAML value assimilated to ZERO, ONE, PLUS_INFINITY or MINUS_INFINITY double value\nContact Bertrand Jeannet\n");
abort();
}
#ifdef DD_UNIQUE_PROFILE
unique->uniqueLookUps++;
#endif
if (unique->constants.keys > unique->constants.maxKeys) {
if (unique->gcEnabled && ((unique->dead > unique->minDead) ||
(10 * unique->constants.dead > 9 * unique->constants.keys))) { /* too many dead */
(void) cuddGarbageCollect(unique,1);
} else {
cuddauxAddCamlConstRehash(unique,1);
old_compactions = caml_stat_compactions;
}
}
if (0<=old_compactions && old_compactions < caml_stat_compactions){
cuddauxAddCamlConstRehash(unique,0);
}
old_compactions = caml_stat_compactions;
split.bits[0] = split.bits[1] = 0;
split.value = value >> 2;
pos = ddHash(split.bits[0], split.bits[1], unique->constants.shift);
nodelist = unique->constants.nodelist;
looking = nodelist[pos];
while (looking != NULL) {
if ( ((struct CuddauxDdNode*)looking)->type.value == value) {
if (looking->ref == 0) {
cuddReclaim(unique,looking);
}
goto Cuddaux_addCamlConst_exit;
}
looking = looking->next;
#ifdef DD_UNIQUE_PROFILE
unique->uniqueLinks++;
#endif
}
unique->keys++;
unique->constants.keys++;
looking = cuddAllocNode(unique);
if (looking == NULL) goto Cuddaux_addCamlConst_exit;
looking->index = CUDD_CONST_INDEX;
((struct CuddauxDdNode*)looking)->type.value = value;
if (Is_block(value))
caml_register_global_root(&((struct CuddauxDdNode*)looking)->type.value);
looking->next = nodelist[pos];
nodelist[pos] = looking;
Cuddaux_addCamlConst_exit:
if (0){
printf("Adding value %ld, %f, pos=%d, node=%p\n",
value, Double_val(value),pos,looking
);
}
CAMLreturnT(DdNode*,looking);
} /* end of cuddaux_addCamlConst */
/**Function********************************************************************
Synopsis [Free ADD OCaml value for enabling garbage collection.]
Description []
SideEffects [None]
SeeAlso []
******************************************************************************/
int Cuddaux_addCamlPreGC(DdManager* unique, const char* s, void* data)
{
DdNodePtr *nodelist;
int j;
DdNode *node;
int slots;
if (unique->constants.dead == 0) return 1;
nodelist = unique->constants.nodelist;
slots = unique->constants.slots;
for (j = 0; j < slots; j++) {
node = nodelist[j];
while (node != NULL) {
if (node->ref == 0) {
value value = ((struct CuddauxDdNode *)node)->type.value;
if (0){
printf("Removing value %ld, %f, pos=%d, node=%p\n",
value, Double_val(value),j,node
);
}
if (value==((struct CuddauxDdNode*)unique->one)->type.value){
fprintf(stderr,"\ncuddaux/mlcuddidl: big problem: CAML value assimilated to ONE double value\nContact Bertrand Jeannet\n");
abort();
}
if (value==((struct CuddauxDdNode*)unique->zero)->type.value){
fprintf(stderr,"\ncuddaux/mlcuddidl: big problem: CAML value assimilated to ZERO double value\nContact Bertrand Jeannet\n");
abort();
}
if (value==((struct CuddauxDdNode*)unique->plusinfinity)->type.value){
fprintf(stderr,"\ncuddaux/mlcuddidl: big problem: CAML value assimilated to PLUS_INFINITY double value\nContact Bertrand Jeannet\n");
abort();
}
if (value==((struct CuddauxDdNode*)unique->minusinfinity)->type.value){
fprintf(stderr,"\ncuddaux/mlcuddidl: big problem: CAML value assimilated to MINUS_INFINITY double value\nContact Bertrand Jeannet\n");
abort();
}
if (Is_block(value))
caml_remove_global_root(&((struct CuddauxDdNode*)node)->type.value);
}
node = node->next;
}
}
return 1;
}
/*---------------------------------------------------------------------------*/
/* Definition of internal functions */
/*---------------------------------------------------------------------------*/
void cuddauxAddCamlConstRehash(DdManager* unique, int offset)
{
unsigned int slots, oldslots;
int shift, oldshift;
int j, pos;
DdNodePtr *nodelist, *oldnodelist;
DdNode *node, *next;
myhack split;
extern DD_OOMFP MMoutOfMemory;
DD_OOMFP saveHandler;
oldslots = unique->constants.slots;
oldshift = unique->constants.shift;
oldnodelist = unique->constants.nodelist;
/* The constant subtable is never subjected to reordering.
** Therefore, when it is resized, it is because it has just
** reached the maximum load. We can safely just double the size,
** with no need for the loop we use for the other tables.
*/
slots = oldslots << offset;
shift = oldshift - offset;
saveHandler = MMoutOfMemory;
MMoutOfMemory = Cudd_OutOfMem;
nodelist = ALLOC(DdNodePtr, slots);
MMoutOfMemory = saveHandler;
if (nodelist == NULL) {
int j;
(void) fprintf(unique->err,
"Unable to resize constant subtable for lack of memory\n");
(void) cuddGarbageCollect(unique,1);
for (j = 0; j < unique->size; j++) {
unique->subtables[j].maxKeys <<= 1;
}
unique->constants.maxKeys <<= 1;
return;
}
unique->constants.slots = slots;
unique->constants.shift = shift;
unique->constants.maxKeys = slots * DD_MAX_SUBTABLE_DENSITY;
unique->constants.nodelist = nodelist;
for (j = 0; (unsigned) j < slots; j++) {
nodelist[j] = NULL;
}
for (j = 0; (unsigned) j < oldslots; j++) {
node = oldnodelist[j];
while (node != NULL) {
next = node->next;
split.bits[0] = split.bits[1] = 0;
split.value = cuddauxCamlV(node) >> 2;
pos = ddHash(split.bits[0], split.bits[1], shift);
node->next = nodelist[pos];
nodelist[pos] = node;
node = next;
}
}
FREE(oldnodelist);
#ifdef DD_VERBOSE
(void) fprintf(unique->err,
"rehashing constants: keys %d dead %d new size %d\n",
unique->constants.keys,unique->constants.dead,slots);
#endif
}