6
6
7
7
\*******************************************************************/
8
8
9
- #include < cassert>
10
- #include < ostream>
11
-
12
- #include " string2int.h"
13
9
#include " irep.h"
14
- #include " string_hash.h"
15
10
#include " irep_hash.h"
11
+ #include " string2int.h"
12
+ #include " string_hash.h"
13
+
14
+ #include < cassert>
15
+ #include < ostream>
16
16
17
17
#ifdef SUB_IS_LIST
18
18
#include < algorithm>
19
19
#endif
20
20
21
- #ifdef IREP_DEBUG
22
- #include < iostream>
23
- #endif
24
-
25
- irept nil_rep_storage;
26
-
27
21
#ifdef SHARING
28
- irept::dt irept::empty_d ;
22
+ std::shared_ptr< irept::dt> irept::empty=std::make_shared<dt>() ;
29
23
#endif
30
24
31
25
/* ******************************************************************\
@@ -75,180 +69,14 @@ Function: get_nil_irep
75
69
76
70
const irept &get_nil_irep ()
77
71
{
72
+ static irept nil_rep_storage;
78
73
if (nil_rep_storage.id ().empty ()) // initialized?
79
74
nil_rep_storage.id (ID_nil);
80
75
return nil_rep_storage;
81
76
}
82
77
83
78
/* ******************************************************************\
84
79
85
- Function: irept::detach
86
-
87
- Inputs:
88
-
89
- Outputs:
90
-
91
- Purpose:
92
-
93
- \*******************************************************************/
94
-
95
- #ifdef SHARING
96
- void irept::detach ()
97
- {
98
- #ifdef IREP_DEBUG
99
- std::cout << " DETACH1: " << data << std::endl;
100
- #endif
101
-
102
- if (data==&empty_d)
103
- {
104
- data=new dt;
105
-
106
- #ifdef IREP_DEBUG
107
- std::cout << " ALLOCATED " << data << std::endl;
108
- #endif
109
- }
110
- else if (data->ref_count >1 )
111
- {
112
- dt *old_data (data);
113
- data=new dt (*old_data);
114
-
115
- #ifdef IREP_DEBUG
116
- std::cout << " ALLOCATED " << data << std::endl;
117
- #endif
118
-
119
- data->ref_count =1 ;
120
- remove_ref (old_data);
121
- }
122
-
123
- assert (data->ref_count ==1 );
124
-
125
- #ifdef IREP_DEBUG
126
- std::cout << " DETACH2: " << data << std::endl;
127
- #endif
128
- }
129
- #endif
130
-
131
- /* ******************************************************************\
132
-
133
- Function: irept::remove_ref
134
-
135
- Inputs:
136
-
137
- Outputs:
138
-
139
- Purpose:
140
-
141
- \*******************************************************************/
142
-
143
- #ifdef SHARING
144
- void irept::remove_ref (dt *old_data)
145
- {
146
- if (old_data==&empty_d)
147
- return ;
148
-
149
- #if 0
150
- nonrecursive_destructor(old_data);
151
- #else
152
-
153
- assert (old_data->ref_count !=0 );
154
-
155
- #ifdef IREP_DEBUG
156
- std::cout << " R: " << old_data << " " << old_data->ref_count << std::endl;
157
- #endif
158
-
159
- old_data->ref_count --;
160
- if (old_data->ref_count ==0 )
161
- {
162
- #ifdef IREP_DEBUG
163
- std::cout << " D: " << pretty () << std::endl;
164
- std::cout << " DELETING " << old_data->data
165
- << " " << old_data << std::endl;
166
- old_data->clear ();
167
- std::cout << " DEALLOCATING " << old_data << " \n " ;
168
- #endif
169
-
170
- // may cause recursive call
171
- delete old_data;
172
-
173
- #ifdef IREP_DEBUG
174
- std::cout << " DONE\n " ;
175
- #endif
176
- }
177
- #endif
178
- }
179
- #endif
180
-
181
- /* ******************************************************************\
182
-
183
- Function: irept::nonrecursive_destructor
184
-
185
- Inputs:
186
-
187
- Outputs:
188
-
189
- Purpose: Does the same as remove_ref, but
190
- using an explicit stack instead of recursion.
191
-
192
- \*******************************************************************/
193
-
194
- #ifdef SHARING
195
- void irept::nonrecursive_destructor (dt *old_data)
196
- {
197
- std::vector<dt *> stack (1 , old_data);
198
-
199
- while (!stack.empty ())
200
- {
201
- dt *d=stack.back ();
202
- stack.erase (--stack.end ());
203
- if (d==&empty_d)
204
- continue ;
205
-
206
- assert (d->ref_count !=0 );
207
- d->ref_count --;
208
-
209
- if (d->ref_count ==0 )
210
- {
211
- stack.reserve (stack.size ()+
212
- d->named_sub .size ()+
213
- d->comments .size ()+
214
- d->sub .size ());
215
-
216
- for (named_subt::iterator
217
- it=d->named_sub .begin ();
218
- it!=d->named_sub .end ();
219
- it++)
220
- {
221
- stack.push_back (it->second .data );
222
- it->second .data =&empty_d;
223
- }
224
-
225
- for (named_subt::iterator
226
- it=d->comments .begin ();
227
- it!=d->comments .end ();
228
- it++)
229
- {
230
- stack.push_back (it->second .data );
231
- it->second .data =&empty_d;
232
- }
233
-
234
- for (subt::iterator
235
- it=d->sub .begin ();
236
- it!=d->sub .end ();
237
- it++)
238
- {
239
- stack.push_back (it->data );
240
- it->data =&empty_d;
241
- }
242
-
243
- // now delete, won't do recursion
244
- delete d;
245
- }
246
- }
247
- }
248
- #endif
249
-
250
- /* ******************************************************************\
251
-
252
80
Function: irept::move_to_named_sub
253
81
254
82
Inputs:
@@ -261,9 +89,6 @@ Function: irept::move_to_named_sub
261
89
262
90
void irept::move_to_named_sub (const irep_namet &name, irept &irep)
263
91
{
264
- #ifdef SHARING
265
- detach ();
266
- #endif
267
92
add (name).swap (irep);
268
93
irep.clear ();
269
94
}
@@ -282,9 +107,6 @@ Function: irept::move_to_sub
282
107
283
108
void irept::move_to_sub (irept &irep)
284
109
{
285
- #ifdef SHARING
286
- detach ();
287
- #endif
288
110
get_sub ().push_back (get_nil_irep ());
289
111
get_sub ().back ().swap (irep);
290
112
}
0 commit comments