Skip to content

Commit c568f89

Browse files
committed
Use Region instead of Heap where possible
1 parent 6f467e3 commit c568f89

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

gecode/int/extensional/dfa.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ namespace Gecode {
152152
DFA::init(int start, Transition t_spec[], int f_spec[], bool minimize) {
153153
using namespace Int;
154154
using namespace Extensional;
155+
Region region;
156+
155157
// Compute number of states and transitions
156158
int n_states = start;
157159
int n_trans = 0;
@@ -165,12 +167,12 @@ namespace Gecode {
165167
n_states++;
166168

167169
// Temporary structure for transitions
168-
Transition* trans = heap.alloc<Transition>(n_trans);
170+
Transition* trans = region.alloc<Transition>(n_trans);
169171
for (int i = n_trans; i--; )
170172
trans[i] = t_spec[i];
171173
// Temporary structures for finals
172-
int* final = heap.alloc<int>(n_states+1);
173-
bool* is_final = heap.alloc<bool>(n_states+1);
174+
int* final = region.alloc<int>(n_states+1);
175+
bool* is_final = region.alloc<bool>(n_states+1);
174176
int n_finals = 0;
175177
for (int i = n_states+1; i--; )
176178
is_final[i] = false;
@@ -182,7 +184,7 @@ namespace Gecode {
182184
if (minimize) {
183185
// Sort transitions by symbol and i_state
184186
TransBySymbolI_State::sort(trans, n_trans);
185-
Transition** idx = heap.alloc<Transition*>(n_trans+1);
187+
Transition** idx = region.alloc<Transition*>(n_trans+1);
186188
// idx[i]...idx[i+1]-1 gives where transitions for symbol i start
187189
int n_symbols = 0;
188190
{
@@ -197,9 +199,9 @@ namespace Gecode {
197199
assert(j == n_trans);
198200
}
199201
// Map states to groups
200-
int* s2g = heap.alloc<int>(n_states+1);
201-
StateGroup* part = heap.alloc<StateGroup>(n_states+1);
202-
GroupStates* g2s = heap.alloc<GroupStates>(n_states+1);
202+
int* s2g = region.alloc<int>(n_states+1);
203+
StateGroup* part = region.alloc<StateGroup>(n_states+1);
204+
GroupStates* g2s = region.alloc<GroupStates>(n_states+1);
203205
// Initialize: final states is group one, all other group zero
204206
for (int i = n_states+1; i--; ) {
205207
part[i].state = i;
@@ -287,7 +289,7 @@ namespace Gecode {
287289
break;
288290
}
289291
// Compute representatives
290-
int* s2r = heap.alloc<int>(n_states+1);
292+
int* s2r = region.alloc<int>(n_states+1);
291293
for (int i = n_states+1; i--; )
292294
s2r[i] = -1;
293295
for (int g = n_groups; g--; )
@@ -303,21 +305,16 @@ namespace Gecode {
303305
}
304306
n_trans = j;
305307
n_states = n_groups;
306-
heap.free<int>(s2r,n_states+1);
307308
}
308-
heap.free<GroupStates>(g2s,n_states+1);
309-
heap.free<StateGroup>(part,n_states+1);
310-
heap.free<int>(s2g,n_states+1);
311-
heap.free<Transition*>(idx,n_trans+1);
312309
}
313310

314311
// Do a reachability analysis for all states starting from start state
315-
Gecode::Support::StaticStack<int,Heap> visit(heap,n_states);
316-
int* state = heap.alloc<int>(n_states);
312+
Gecode::Support::StaticStack<int,Region> visit(region,n_states);
313+
int* state = region.alloc<int>(n_states);
317314
for (int i=n_states; i--; )
318315
state[i] = SI_NONE;
319316

320-
Transition** idx = heap.alloc<Transition*>(n_states+1);
317+
Transition** idx = region.alloc<Transition*>(n_states+1);
321318
{
322319
// Sort all transitions according to i_state and create index structure
323320
// idx[i]...idx[i+1]-1 gives where transitions for state i start
@@ -374,12 +371,9 @@ namespace Gecode {
374371
}
375372
}
376373
}
377-
heap.free<Transition*>(idx,n_states+1);
378-
heap.free<int>(final,n_states+1);
379-
heap.free<bool>(is_final,n_states+1);
380374

381375
// Now all reachable states are known (also the final ones)
382-
int* re = heap.alloc<int>(n_states);
376+
int* re = region.alloc<int>(n_states);
383377
for (int i = n_states; i--; )
384378
re[i] = -1;
385379

@@ -440,7 +434,7 @@ namespace Gecode {
440434
{
441435
// Compute maximal degree
442436
unsigned int max_degree = 0;
443-
unsigned int* deg = heap.alloc<unsigned int>(m_states);
437+
unsigned int* deg = region.alloc<unsigned int>(m_states);
444438

445439
// Compute in-degree per state
446440
for (int i = m_states; i--; )
@@ -458,8 +452,6 @@ namespace Gecode {
458452
for (int i = m_states; i--; )
459453
max_degree = std::max(max_degree,deg[i]);
460454

461-
heap.free<unsigned int>(deg,m_states);
462-
463455
// Compute transitions per symbol
464456
{
465457
int i=0;
@@ -477,9 +469,6 @@ namespace Gecode {
477469

478470
d->fill();
479471
object(d);
480-
heap.free<int>(re,n_states);
481-
heap.free<int>(state,n_states);
482-
heap.free<Transition>(trans,n_trans);
483472
}
484473

485474
DFA::DFA(int start, Transition t_spec[], int f_spec[], bool minimize) {

gecode/minimodel/reg.cpp

100644100755
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace Gecode {
4141
/**
4242
* \brief Allocator for position sets
4343
*/
44-
typedef Support::BlockAllocator<PosSet,Heap> PosSetAllocator;
44+
typedef Support::BlockAllocator<PosSet,Region> PosSetAllocator;
4545

4646
class NodeInfo;
4747
class PosInfo;
@@ -113,7 +113,8 @@ namespace Gecode {
113113

114114
void
115115
REG::Exp::dispose(void) {
116-
Support::DynamicStack<Exp*,Heap> todo(heap);
116+
Region region;
117+
Support::DynamicStack<Exp*,Region> todo(region);
117118
todo.push(this);
118119
while (!todo.empty()) {
119120
Exp* e = todo.pop();
@@ -256,7 +257,8 @@ namespace Gecode {
256257
int n = x.size();
257258
if (n < 1)
258259
throw MiniModel::TooFewArguments("REG");
259-
Exp** a = heap.alloc<Exp*>(n);
260+
Region region;
261+
Exp** a = region.alloc<Exp*>(n);
260262
// Initialize with symbols
261263
for (int i=n; i--; ) {
262264
a[i] = new Exp();
@@ -292,7 +294,6 @@ namespace Gecode {
292294
}
293295
}
294296
e = a[0];
295-
heap.free<Exp*>(a,n);
296297
}
297298

298299
REG
@@ -447,7 +448,7 @@ namespace Gecode {
447448
/**
448449
* \brief Sets of positions
449450
*/
450-
class PosSet : public Support::BlockClient<PosSet,Heap> {
451+
class PosSet : public Support::BlockClient<PosSet,Region> {
451452
// Maintain sets of positions in inverse order
452453
// This makes the check whether the last position is included
453454
// more efficient.
@@ -566,8 +567,10 @@ namespace Gecode {
566567
using MiniModel::NodeInfo;
567568
using MiniModel::ExpInfo;
568569

569-
Support::DynamicStack<ExpInfo,Heap> todo(heap);
570-
Support::DynamicStack<NodeInfo,Heap> done(heap);
570+
Region region;
571+
572+
Support::DynamicStack<ExpInfo,Region> todo(region);
573+
Support::DynamicStack<NodeInfo,Region> done(region);
571574

572575
// Start with first expression to be processed
573576
todo.push(ExpInfo(this));
@@ -837,19 +840,20 @@ namespace Gecode {
837840

838841
using MiniModel::SymbolsInc;
839842

840-
PosSetAllocator psm(heap);
843+
Region region;
844+
PosSetAllocator psm(region);
841845
StatePoolAllocator spm(heap);
842846
REG r = *this + REG(Int::Limits::max+1);
843847
int n_pos = REG::Exp::n_pos(r.e);
844848

845-
PosInfo* pi = heap.alloc<PosInfo>(n_pos);
849+
PosInfo* pi = region.alloc<PosInfo>(n_pos);
846850
for (int i=n_pos; i--; )
847851
pi[i].followpos = NULL;
848852

849853
PosSet* firstpos = r.e->followpos(psm,&pi[0]);
850854

851855
// Compute symbols
852-
int* symbols = heap.alloc<int>(n_pos);
856+
int* symbols = region.alloc<int>(n_pos);
853857
for (int i=n_pos; i--; )
854858
symbols[i] = pi[i].symbol;
855859

@@ -882,8 +886,6 @@ namespace Gecode {
882886
fb.add(n->state);
883887
fb.finish();
884888

885-
heap.free<PosInfo>(pi,n_pos);
886-
heap.free<int>(symbols,n_pos);
887889
return DFA(0,tb.transitions(),fb.finals(),true);
888890
}
889891

0 commit comments

Comments
 (0)