-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrail.cpp
More file actions
161 lines (151 loc) · 3.19 KB
/
trail.cpp
File metadata and controls
161 lines (151 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#include "trail.hpp"
#include <assert.h>
#include <iostream>
#include "commandList.hpp"
#define TSIZE (1 << 28)
Trailer::Trailer()
: _magic(-1)
{
_block = (char*)malloc(TSIZE);
_bsz = TSIZE;
_btop = 0;
_lastNode = 0;
_enabled = false;
}
Trailer::~Trailer()
{
free(_block);
}
void Trailer::resize()
{
std::cout << "Trailer::resize to: " << (_bsz << 1) << "\n";
char* nb = (char*)realloc(_block,_bsz << 1);
if (nb != _block) {
std::stack<Entry*> ns;
while(!_trail.empty()) {
Entry* e = _trail.top();
Entry* ne = (Entry*)((char*)e - _block + nb);
ne->relocate(nb - _block);
ns.push(ne);
_trail.pop();
}
while(!ns.empty()) {
Entry* e = ns.top();
_trail.push(e);
ns.pop();
}
}
_bsz <<= 1;
_block = nb;
}
long Trailer::push()
{
++_magic;
long rv = ++_lastNode;
_tops.emplace(std::make_tuple(_trail.size(),_btop,rv));
return rv;
}
void Trailer::push(long nodeID)
{
++_magic;
_tops.emplace(std::make_tuple(_trail.size(),_btop,nodeID));
}
void Trailer::popToNode(long node)
{
while (true) {
int to;
std::size_t mem;
long nodeId;
std::tie(to,mem,nodeId) = _tops.top();
pop();
if (nodeId == node)
break;
}
}
void Trailer::pop()
{
unsigned to;
std::size_t mem;
long node;
std::tie(to,mem,node) = _tops.top();
_tops.pop();
while (_trail.size() != to) {
assert(_trail.size() >= to);
Entry* entry = _trail.top();
entry->restore();
_trail.pop();
entry->Entry::~Entry();
}
_btop = mem;
}
void Trailer::clear()
{
while (_tops.size() > 0)
pop();
}
void Trailer::saveState()
{
push();
}
void Trailer::restoreState()
{
pop();
}
void Trailer::withNewState(const std::function<void(void)>& body)
{
long lvl = push();
body();
popToNode(lvl);
}
//MemoryTrail::MemoryTrail()
//{
//}
//MemoryTrail::~MemoryTrail()
//{
//}
//void MemoryTrail::clear()
//{
// return;
//}
//void MemoryTrail::saveState()
//{
// return;
//}
//void MemoryTrail::restoreState()
//{
// return;
//}
//void MemoryTrail::withNewState(const std::function<void(void)>& body)
//{
// return;
//}
//unsigned int MemoryTrail::trailSize()
//{
// return _trail.size();
//}
//void MemoryTrail::comply(MemoryTrail* other, CommandList* list)
//{
// unsigned int from = list->memoryFrom();
// unsigned int to = list->memoryTo();
// for (auto i = from; i < to; i++) {
// assert(other->at(i) != nullptr);
// _trail.push_back(other->at(i));
// }
//}
//void* MemoryTrail::at(unsigned int index) {
// return _trail[index];
//}