Skip to content

Commit bbeea25

Browse files
committed
Use shared_ptr in irept
1 parent 2a95e59 commit bbeea25

File tree

3 files changed

+88
-367
lines changed

3 files changed

+88
-367
lines changed

src/util/irep.cpp

Lines changed: 7 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
#include <cassert>
10-
#include <ostream>
11-
12-
#include "string2int.h"
139
#include "irep.h"
14-
#include "string_hash.h"
1510
#include "irep_hash.h"
11+
#include "string2int.h"
12+
#include "string_hash.h"
13+
14+
#include <cassert>
15+
#include <ostream>
1616

1717
#ifdef SUB_IS_LIST
1818
#include <algorithm>
1919
#endif
2020

21-
#ifdef IREP_DEBUG
22-
#include <iostream>
23-
#endif
24-
25-
irept nil_rep_storage;
26-
2721
#ifdef SHARING
28-
irept::dt irept::empty_d;
22+
std::shared_ptr<irept::dt> irept::empty=std::make_shared<dt>();
2923
#endif
3024

3125
/*******************************************************************\
@@ -75,180 +69,14 @@ Function: get_nil_irep
7569

7670
const irept &get_nil_irep()
7771
{
72+
static irept nil_rep_storage;
7873
if(nil_rep_storage.id().empty()) // initialized?
7974
nil_rep_storage.id(ID_nil);
8075
return nil_rep_storage;
8176
}
8277

8378
/*******************************************************************\
8479
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-
25280
Function: irept::move_to_named_sub
25381
25482
Inputs:
@@ -261,9 +89,6 @@ Function: irept::move_to_named_sub
26189

26290
void irept::move_to_named_sub(const irep_namet &name, irept &irep)
26391
{
264-
#ifdef SHARING
265-
detach();
266-
#endif
26792
add(name).swap(irep);
26893
irep.clear();
26994
}
@@ -282,9 +107,6 @@ Function: irept::move_to_sub
282107

283108
void irept::move_to_sub(irept &irep)
284109
{
285-
#ifdef SHARING
286-
detach();
287-
#endif
288110
get_sub().push_back(get_nil_irep());
289111
get_sub().back().swap(irep);
290112
}

0 commit comments

Comments
 (0)