-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaystack.h
228 lines (183 loc) · 5.92 KB
/
haystack.h
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
#ifndef LACE__HAYSTACK_H
#define LACE__HAYSTACK_H
#include "do_not_copy.h"
#include "try.h"
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <utility>
#include <obstack.h>
namespace lace {
class haystack : public lace::do_not_copy {
public:
haystack() { obstack_specify_allocation(&_, 0, 0, xmalloc, xfree); }
~haystack() { obstack_free(&_, NULL); }
bool empty() const { return obstack_empty_p(&_); }
long & chunk_size() { return obstack_chunk_size(&_); }
int & alignment_mask() { return obstack_alignment_mask(&_); }
int const & alignment_mask() const { return obstack_alignment_mask(&_); }
int memory_used() { return obstack_memory_used(&_); }
template <typename T> void align() { alignment_mask() = alignof_mask<T>(); }
template <typename T> bool aligned() const { return alignof_mask<T>() == (alignment_mask() & alignof_mask<T>()); }
void clear() {
long cs = chunk_size();
int am = alignment_mask();
deallocate(NULL);
obstack_specify_allocation(&_, cs, am, xmalloc, xfree);
}
void* allocate(::size_t s) { return obstack_alloc(&_, s); }
template <typename T>
T* allocate() {
assert(aligned<T>());
return reinterpret_cast<T*>(allocate(sizeof(T)));
}
template <typename T, typename ...As>
T* make(As&&... as) {
align<T>();
auto t = allocate<T>();
if (t) try {
new (t) T(std::forward<As&&>(as)...);
} catch(...) {
deallocate(t);
throw;
}
return t;
}
void deallocate(void* p) { return obstack_free(&_, p); }
void* copy(const void* a, ::size_t s) { return obstack_copy(&_, a, s); }
template <typename T>
T* copy(const T &t) {
assert(aligned<T>());
return reinterpret_cast<T*>(copy(&t, sizeof(t)));
}
void* copy0(const void* a, ::size_t s) { return obstack_copy0(&_, a, s); }
template <typename T>
T* copy0(const T &t) { return reinterpret_cast<T*>(copy0(&t, sizeof(t))); }
void* base() const { return obstack_base(&_); }
int size() const { return obstack_object_size(&_); }
void* next() const { return obstack_next_free(&_); }
int room() const { return obstack_room(&_); }
void* finish() { return obstack_finish(&_); }
void abort() { obstack_free(&_, finish()); }
char* string() {
int s = size();
if (s <= 0 || reinterpret_cast<char*>(base())[s-1])
zero();
return reinterpret_cast<char*>(finish());
}
haystack& want(::size_t w) {
obstack_make_room(&_, w);
return *this;
}
void* blank(::size_t s = 0) {
obstack_blank(&_, s);
return obstack_next_free(&_) - s;
}
template <typename T>
T* blank() {
assert(aligned<T>());
return reinterpret_cast<T*>(blank(sizeof(T)));
}
haystack&
shrink(::size_t s) {
assert(s <= size());
obstack_blank_fast(&_, -::ssize_t(s));
return *this;
}
template <typename T>
haystack&
grow(const T* a, ::size_t s) {
obstack_grow(&_, (const void*)a, s);
return *this;
}
haystack& grow(const char * s) { return grow((void*)s, strlen(s)); }
haystack&
grow(const char c) {
obstack_1grow(&_, c);
return *this;
}
template <typename T>
haystack& grow(const T &t) { return grow(&t, sizeof(t)); }
haystack& pad(::size_t s) { blank(s); return *this; }
haystack& zero() { return grow('\0'); }
haystack& pointer(const void* p) { obstack_ptr_grow(&_, p); return *this; }
haystack&
grow0(void* a, ::size_t s) {
obstack_grow0(&_, a, s);
return *this;
}
haystack& grow0(const char * s) { return grow0((void*)s, strlen(s)); }
template <typename T>
haystack& grow0(const T &t) { return grow0(&t, sizeof(t)); }
haystack&
printf(const char *f, ...) __attribute__ ((format (printf, 2, 3))) {
va_list a;
va_start(a, f);
try {
TRY(obstack_vprintf, &_, f, a);
va_end(a);
} catch(...) {
va_end(a);
throw;
}
return *this;
}
haystack&
vprintf(const char *f, va_list a) {
TRY(obstack_vprintf, &_, f, a);
return *this;
}
haystack& append(const char *s) { return append(s, strlen(s)); }
haystack&
append(const char *s, ::size_t l) {
memcpy(blank(l), s, l);
return *this;
}
haystack& quote(const char *s) { return quote(s, strlen(s)); }
haystack&
quote(const char *s, ::size_t l) {
if (!l || !*s)
return append("''");
::size_t i = 0;
::size_t w = 0;
while (i < l && s[i]) {
if (::size_t o = strcspn(&s[i], "'")) {
w += 2;
i += o;
}
if (::size_t o = strspn(&s[i], "'")) {
w += 2;
i += o;
}
}
want(w + i);
while (i > 0) {
if (::size_t o = strcspn(s, "'")) {
assert(room() >= o + 2);
obstack_1grow_fast(&_, '\'');
memcpy(next(), s, o);
obstack_blank_fast(&_, o);
obstack_1grow_fast(&_, '\'');
i -= o;
s += o;
}
if (::size_t o = strspn(s, "'")) {
assert(room() >= o + 2);
obstack_1grow_fast(&_, '"');
memcpy(next(), s, o);
obstack_blank_fast(&_, o);
obstack_1grow_fast(&_, '"');
i -= o;
s += o;
}
}
return *this;
}
private:
struct ::obstack _;
static void * xmalloc(::size_t s) { return operator new(s); }
static void xfree(void* p) { operator delete(p); }
template <typename T> static ::size_t alignof_mask() { return (1<<(alignof(T)-1))-1; }
};
} // namespace lace
#endif//LACE__HAYSTACK_H