22
22
#include < iostream>
23
23
#endif
24
24
25
- irept nil_rep_storage;
26
-
27
25
#ifdef SHARING
28
- irept::dt irept::empty_d;
26
+ small_shared_ptrt<irept::dt>& irept::get_empty_dt ()
27
+ {
28
+ // Putting this in a function makes sure that it exists the first time that
29
+ // the function is called, which solves undefined-order-of-static-init issues.
30
+ static small_shared_ptrt<dt> empty=make_small_shared_ptr<dt>();
31
+ return empty;
32
+ }
29
33
#endif
30
34
31
35
/* ******************************************************************\
@@ -75,177 +79,13 @@ Function: get_nil_irep
75
79
76
80
const irept &get_nil_irep ()
77
81
{
82
+ static irept nil_rep_storage;
78
83
if (nil_rep_storage.id ().empty ()) // initialized?
79
- nil_rep_storage.id (ID_nil);
80
- return nil_rep_storage;
81
- }
82
-
83
- /* ******************************************************************\
84
-
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
84
{
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
- }
85
+ nil_rep_storage.id (ID_nil);
246
86
}
87
+ return nil_rep_storage;
247
88
}
248
- #endif
249
89
250
90
/* ******************************************************************\
251
91
@@ -261,9 +101,6 @@ Function: irept::move_to_named_sub
261
101
262
102
void irept::move_to_named_sub (const irep_namet &name, irept &irep)
263
103
{
264
- #ifdef SHARING
265
- detach ();
266
- #endif
267
104
add (name).swap (irep);
268
105
irep.clear ();
269
106
}
@@ -282,9 +119,6 @@ Function: irept::move_to_sub
282
119
283
120
void irept::move_to_sub (irept &irep)
284
121
{
285
- #ifdef SHARING
286
- detach ();
287
- #endif
288
122
get_sub ().push_back (get_nil_irep ());
289
123
get_sub ().back ().swap (irep);
290
124
}
0 commit comments