Skip to content

Commit d86342e

Browse files
committed
Use small intrusive pointer in irep
1 parent 985f1a7 commit d86342e

File tree

4 files changed

+254
-319
lines changed

4 files changed

+254
-319
lines changed

src/util/irep.cpp

Lines changed: 10 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ Author: Daniel Kroening, [email protected]
2222
#include <iostream>
2323
#endif
2424

25-
irept nil_rep_storage;
26-
2725
#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+
}
2933
#endif
3034

3135
/*******************************************************************\
@@ -75,177 +79,13 @@ Function: get_nil_irep
7579

7680
const irept &get_nil_irep()
7781
{
82+
static irept nil_rep_storage;
7883
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)
11184
{
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);
24686
}
87+
return nil_rep_storage;
24788
}
248-
#endif
24989

25090
/*******************************************************************\
25191
@@ -261,9 +101,6 @@ Function: irept::move_to_named_sub
261101

262102
void irept::move_to_named_sub(const irep_namet &name, irept &irep)
263103
{
264-
#ifdef SHARING
265-
detach();
266-
#endif
267104
add(name).swap(irep);
268105
irep.clear();
269106
}
@@ -282,9 +119,6 @@ Function: irept::move_to_sub
282119

283120
void irept::move_to_sub(irept &irep)
284121
{
285-
#ifdef SHARING
286-
detach();
287-
#endif
288122
get_sub().push_back(get_nil_irep());
289123
get_sub().back().swap(irep);
290124
}

0 commit comments

Comments
 (0)