forked from sequitur-g2p/sequitur-g2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObstack.hh
243 lines (201 loc) · 5.34 KB
/
Obstack.hh
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* $Id: Obstack.hh 1667 2007-06-02 14:32:35Z max $
*
* Copyright (c) 2004-2005 RWTH Aachen University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 2 (June
* 1991) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you will find it at
* http://www.gnu.org/licenses/gpl.html, or write to the Free Software
* Foundation, Inc., 51 Franlin Street, Fifth Floor, Boston, MA 02110,
* USA.
*
* Should a provision of no. 9 and 10 of the GNU General Public License
* be invalid or become invalid, a valid provision is deemed to have been
* agreed upon which comes closest to what the parties intended
* commercially. In any case guarantee/warranty shall be limited to gross
* negligent actions or intended actions or fraudulent concealment.
*/
#ifndef _CORE_OBSTACK_HH
#define _CORE_OBSTACK_HH
#include <new>
#include <algorithm>
#include "Assertions.hh"
namespace Core {
template <class T>
class Obstack {
public:
typedef T Item;
private:
struct Chunk {
Chunk *succ ;
Item *tail, *end ;
Item data[1];
size_t size() const {
return tail - data ;
}
size_t room() const {
return end - tail;
}
void truncate(Item *i) {
while (i < tail) (--tail)->~Item();
ensure_(tail == i);
}
void clear() {
truncate(data) ;
ensure(size() == 0) ;
}
};
static const size_t defaultChunkSize_ = 4096 ;
size_t chunkCapacity_, chunkSize_ ;
void setChunkSize(size_t s) {
chunkSize_ = s;
chunkCapacity_ = (chunkSize_ - sizeof(Chunk)) / sizeof(Item) + 1;
}
void adjustChunkCapacity(size_t neededCapacity) {
while (chunkCapacity_ < neededCapacity)
setChunkSize(chunkSize_ * 2);
verify(chunkCapacity_ > 0) ;
verify(chunkSize_ > sizeof(Chunk)) ;
}
Chunk *newChunk(Item *begin, Item *end, size_t spareCapacity = 1);
void deleteChunk(Chunk *c) {
c->clear();
free(c);
}
Chunk *current_; /**< current chunk */
Item *begin_; /**< first item in current chunk */
void provide_(size_t n);
void provide(size_t n) {
if (current_->room() < n) provide_(n);
}
public:
Obstack(size_t chunkCapacity = 0);
void clear() {
Chunk *c, *cs;
for (c = current_ ; c ; c = cs) {
cs = c->succ;
deleteChunk(c);
}
current_ = newChunk(0, 0);
begin_ = 0;
}
~Obstack() {
Chunk *c, *cs;
for (c = current_ ; c ; c = cs) {
cs = c->succ;
deleteChunk(c);
}
}
void start() {
begin_ = current_->tail;
}
Item *currentBegin() const {
return begin_;
}
Item *currentEnd() const {
return current_->tail;
}
void grow() {
require(begin_);
provide(1);
new(current_->tail++) Item;
}
void grow(const Item &i) {
require(begin_);
provide(1);
new(current_->tail++) Item(i);
}
void grow(const Item &i, size_t n) {
require(begin_);
provide(n);
current_->tail = uninitialized_fill_n(current_->tail, n, i);
new(current_->tail++) Item(i);
}
void grow(const Item *begin, const Item *end) {
require(begin_);
require(begin <= end);
provide(end - begin);
current_->tail = uninitialized_copy(begin, end, current_->tail);
}
void grow0(const Item *begin, const Item *end) {
require(begin_);
require(begin <= end);
provide(end - begin + 1);
current_->tail = std::uninitialized_copy(begin, end, current_->tail);
*current_->tail++ = 0;
}
void finish() {
begin_ = 0;
}
Item *add(const Item &i) {
start();
grow(i);
Item *result = currentBegin();
finish();
return result;
}
Item *add(const Item *begin, const Item *end) {
start();
grow(begin, end);
Item *result = currentBegin();
finish();
return result;
}
Item *add0(const Item *begin, const Item *end) {
start();
grow0(begin, end);
Item *result = currentBegin();
finish();
return result;
}
};
template <class T>
typename Obstack<T>::Chunk *Obstack<T>::newChunk(Item *begin, Item *end, size_t spareCapacity) {
adjustChunkCapacity(end - begin + spareCapacity);
Chunk *c = (Chunk*) ::malloc(chunkSize_) ;
hope(c != NULL /* memory allocation failed */);
c->succ = 0 ;
c->end = c->data + chunkCapacity_ ;
c->tail = std::uninitialized_copy(begin, end, c->data);
ensure(c->room() >= spareCapacity);
return c ;
}
template <class T>
void Obstack<T>::provide_(size_t n) {
Chunk *nc = newChunk(begin_, current_->tail, n) ;
current_->truncate(begin_);
begin_ = nc->data;
if (current_->size() > 0) {
nc->succ = current_;
} else {
nc->succ = current_->succ;
deleteChunk(current_);
}
current_ = nc;
verify(current_->data <= begin_ &&
begin_ <= current_->tail &&
current_->tail < current_->end) ;
ensure(current_->room() >= n);
}
template <class T>
Obstack<T>::Obstack(size_t chunkCapacity) {
setChunkSize(defaultChunkSize_);
if (chunkCapacity) {
adjustChunkCapacity(chunkCapacity);
} else {
adjustChunkCapacity(1);
}
current_ = newChunk(0, 0);
begin_ = 0;
}
} // namespace Bliss
#endif // _CORE_OBSTACK_HH