-
Notifications
You must be signed in to change notification settings - Fork 13
/
hybridstream.cpp
236 lines (209 loc) · 5.48 KB
/
hybridstream.cpp
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
/*
This file is part of the Fairytale project
Copyright (C) 2018 Márcio Pais
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hybridstream.h"
void HybridStream::freeMemory() {
if (memory) {
delete memory;
memory = nullptr;
}
}
void HybridStream::freeFile() {
if (file) {
file->close();
delete file;
file = nullptr;
}
}
void HybridStream::allocate() {
assert(sizeLimit > 0);
assert((int64_t)memLimit <= sizeLimit);
if (memLimit > 0)
memory = new Array<uint8_t>(memLimit);
else {
file = new FileStream();
if (!file->getTempFile()) {
freeFile();
throw ExhaustedStorageException();
}
}
}
HybridStream::HybridStream(const size_t maxMemUsage, const int64_t maxSize) :
file(nullptr), memory(nullptr), filepos(0), filesize(0), previousSize(0), sizeLimit(maxSize), memLimit(MEM_LIMIT(maxMemUsage)), refCount(1), priority(STREAM_PRIORITY_NORMAL), keepAlive(false) {
allocate();
}
HybridStream::~HybridStream() {
close();
}
void HybridStream::close() {
freeMemory();
freeFile();
if (previousSize == 0)
previousSize = filesize;
filepos = 0, filesize = 0;
}
void HybridStream::restore(const size_t maxMemUsage, const int64_t maxSize) {
assert(memory == nullptr);
assert(file == nullptr);
assert(filepos == 0);
assert(filesize == 0);
memLimit = MEM_LIMIT(maxMemUsage);
sizeLimit = maxSize;
allocate();
}
bool HybridStream::commitToFile() {
assert(file == nullptr);
assert(memory != nullptr);
file = new FileStream();
if (!file->getTempFile()) {
// panic: we failed to get physical storage
close();
return false;
}
if (filesize > 0)
file->blockWrite(&((*memory)[0]), size_t(filesize));
file->setPos(filepos);
freeMemory();
return true;
}
int HybridStream::getChar() {
if (filepos >= (off_t)filesize)
return EOF;
if (memory)
return (*memory)[filepos++];
else {
assert(file != nullptr);
return filepos++, file->getChar();
}
}
void HybridStream::putChar(const uint8_t c) {
if (memory) {
if (filepos < (off_t)memLimit) {
(*memory)[filepos] = c;
if (filepos == (off_t)filesize)
filesize++;
filepos++;
return;
}
else {
if (!commitToFile())
throw ExhaustedStorageException();
}
}
if (filepos < (off_t)sizeLimit) {
assert(file != nullptr);
file->putChar(c);
if (filepos == (off_t)filesize)
filesize++;
filepos++;
}
else {
close();
throw ExhaustedStorageException();
}
}
size_t HybridStream::blockRead(void *ptr, const size_t count) {
if (memory) {
if (count > 0) {
uint64_t total = filesize - filepos;
if (total > uint64_t(count))
total = uint64_t(count);
memcpy(ptr, &((*memory)[filepos]), size_t(total));
filepos += off_t(total);
return size_t(total);
}
else
return 0;
}
else {
assert(file != nullptr);
size_t total = file->blockRead(ptr, count);
filepos += off_t(total);
return total;
}
}
void HybridStream::blockWrite(void *ptr, const size_t count) {
if (memory) {
if (filepos + count <= memLimit) {
if (count > 0) {
memcpy(&((*memory)[filepos]), ptr, count);
filepos += off_t(count);
if (filepos > (off_t)filesize)
filesize = filepos;
}
return;
}
else {
if (!commitToFile())
throw ExhaustedStorageException();
}
}
if ((int64_t)(filepos + count) <= sizeLimit) {
assert(file != nullptr);
file->blockWrite(ptr, count);
filepos += off_t(count);
if (filepos > (off_t)filesize)
filesize = filepos;
}
else {
close();
throw ExhaustedStorageException();
}
}
void HybridStream::setPos(const off_t newpos) {
if (memory) {
filepos = newpos;
if (filepos > (off_t)filesize && !commitToFile())
throw ExhaustedStorageException();
else
return;
}
if (newpos < (off_t)sizeLimit) {
assert(file != nullptr);
file->setPos(newpos);
filepos = newpos;
}
}
void HybridStream::setEnd() {
filepos = off_t(filesize);
if (file != nullptr)
file->setEnd();
}
off_t HybridStream::curPos() {
return (memory) ? filepos : (assert(file != nullptr), file->curPos());
}
void HybridStream::incRefCount() {
refCount++;
}
void HybridStream::decRefCount() {
refCount -= (refCount > 0);
}
int HybridStream::getPriority() {
return priority;
}
void HybridStream::setPriority(const int p) {
priority = p;
}
void HybridStream::setPurgeStatus(const bool allowed) {
keepAlive = !allowed;
}
size_t HybridStream::getMemUsage() {
return memLimit;
}
int64_t HybridStream::getSize() {
return filesize;
}
int64_t HybridStream::getPreviousSize() {
return previousSize;
}