[3e4acc]: libkwave / SwapFile.cpp Maximize Restore History

Download this file

SwapFile.cpp    299 lines (246 with data), 8.9 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/***************************************************************************
SwapFile.cpp - Provides virtual memory in a swap file
-------------------
begin : Fri Aug 17 2001
copyright : (C) 2000 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <unistd.h> // for unlink() and getpagesize()
#include "SwapFile.h"
// just for debugging: number of open swapfiles
static unsigned int g_instances = 0;
/** minimum swap file size: 16 MB */
#define MINIMUM_SIZE (16 << 20)
/** size increment when resizing: 4 MB */
#define BLOCK_SIZE (4 << 20)
//***************************************************************************
SwapFile::SwapFile(const QString &name)
:m_file(name), m_address(0), m_size(0), m_pagesize(0), m_map_count(0)
{
// determine the system's native page size
#if defined(HAVE_GETPAGESIZE)
if (!m_pagesize) m_pagesize = getpagesize();
#endif
#if defined(HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
if (!m_pagesize) m_pagesize = sysconf(_SC_PAGESIZE);
#endif
// fallback: assume 4kB pagesize
if (static_cast<int>(m_pagesize) <= 0) {
qWarning("SwapFile: unable to determine page size, using fallback");
m_pagesize = (4 << 10);
}
g_instances++;
}
//***************************************************************************
SwapFile::~SwapFile()
{
close();
g_instances--;
}
//***************************************************************************
static inline unsigned int round_up(unsigned int size, unsigned int units)
{
if (size < MINIMUM_SIZE) return MINIMUM_SIZE;
unsigned int modulo = (size % units);
if (modulo) size += (units - modulo);
return size;
}
//***************************************************************************
bool SwapFile::allocate(size_t size)
{
Q_ASSERT(!m_address); // MUST NOT be mapped !
if (m_address) return false;
if (m_size) close();
qDebug("SwapFile::allocate(%u), instances: %u",
static_cast<unsigned int>(size), g_instances);
// try to create the temporary file
if (!m_file.open()) {
qWarning("SwapFile(%s) -> open failed", m_file.fileName().toLocal8Bit().data());
return false;
}
// when it is created, also try to unlink it so that it will always
// be removed, even if the application crashes !
#ifdef HAVE_UNLINK
unlink(m_file.fileName().toLocal8Bit().data());
#endif /* HAVE_UNLINK */
// round up the new size to a full page
size_t rounded = round_up(size, m_pagesize);
// touch each new page in order to *really* allocate the disk space
size_t offset = 0;
while (offset < rounded) {
// qDebug("SwapFile: touching at offset 0x%08X",
// static_cast<unsigned int>(offset));
m_file.seek(offset);
m_file.putChar(0);
m_file.flush();
if (m_file.pos() != static_cast<qint64>(offset + 1)) {
qWarning("SwapFile::allocate(): seek failed. DISK FULL !?");
return false;
}
offset += m_pagesize;
}
m_file.seek(rounded - 1);
m_file.putChar(0);
m_file.flush();
if (m_file.pos() + 1 < static_cast<qint64>(size)) {
qWarning("SwapFile::allocate(%d MB) failed, DISK FULL ?",
static_cast<unsigned int>(size >> 20));
m_size = 0;
return false;
}
m_file.putChar(0);
// now the size is valid
m_size = size;
// qDebug("SwapFile::allocate(%d kB)",size >> 10);
return true;
}
//***************************************************************************
bool SwapFile::resize(size_t size)
{
Q_ASSERT(!m_address); // MUST NOT be mappped !
Q_ASSERT(!m_map_count);
if (m_address || m_map_count) return false;
if (size == m_size) return true; // nothing to do
// special case: shutdown
if (size == 0) {
close();
return true;
}
// this file seems to be a growing one:
// round up the new size to a full block
size_t rounded = round_up(size, BLOCK_SIZE);
// optimization: if rounded size already matches -> done
if (rounded == m_size) {
// qDebug("SwapFile::resize(%u MB) -> skipped, already big enough",
// static_cast<unsigned int>(size >> 20));
return true;
}
// do not shrink below minimum size
if ((size < m_size) && (size < MINIMUM_SIZE)) {
// qDebug("SwapFile::resize(%u MB) -> skipped, limited by min size",
// static_cast<unsigned int>(size >> 20));
return true;
}
// resize the file
// qDebug("SwapFile::resize(%u)", size);
// touch each new page in order to *really* allocate the disk space
size_t offset = static_cast<size_t>((m_size + m_pagesize - 1) / m_pagesize);
while (offset < rounded) {
// qDebug("SwapFile: touching at offset 0x%08X",
// static_cast<unsigned int>(offset));
m_file.seek(offset);
m_file.putChar(0);
m_file.flush();
if (m_file.pos() != static_cast<qint64>(offset + 1)) {
qWarning("SwapFile::resize(): seek failed. DISK FULL !?");
return false;
}
offset += m_pagesize;
}
m_file.seek(rounded - 1);
if (m_file.pos() == static_cast<qint64>(rounded - 1)) {
if (rounded > m_size) {
// growing: mark the new "last byte"
m_file.putChar(0);
} else {
// shrinking: only truncate the file
m_file.flush();
int res = ftruncate(m_file.handle(), rounded);
if (res) perror("ftruncate failed");
}
m_size = rounded;
} else {
qWarning("SwapFile::resize(): seek failed. DISK FULL !?");
return false;
}
// qDebug("SwapFile::resize() to size=%u", m_size);
return true;
}
//***************************************************************************
void SwapFile::close()
{
Q_ASSERT(!m_map_count);
if (m_address) m_file.unmap(static_cast<uchar *>(m_address));
m_address = 0;
m_size = 0;
m_file.resize(0);
if (m_file.isOpen()) m_file.close();
if (m_file.exists(m_file.fileName())) {
if (!m_file.remove()) {
qWarning("SwapFile(%s) -> remove FAILED",
m_file.fileName().toLocal8Bit().data());
}
}
}
//***************************************************************************
void *SwapFile::map()
{
// qDebug(" SwapFile::map() - m_size=%u", m_size);
// shortcut if already mapped
if (m_map_count) {
m_map_count++;
Q_ASSERT(m_address);
return m_address;
}
m_file.flush();
m_address = m_file.map(0, m_size, QFile::NoOptions);
// map -1 to null pointer
if (m_address == reinterpret_cast<void *>(-1)) m_address = 0;
// if succeeded, increase map reference counter
if (m_address) {
m_map_count++;
} else {
qWarning("SwapFile(%s) -> map FAILED",
m_file.fileName().toLocal8Bit().data());
}
return m_address;
}
//***************************************************************************
int SwapFile::unmap()
{
Q_ASSERT(m_address);
Q_ASSERT(m_size);
Q_ASSERT(m_map_count);
// first decrease refcount and do nothing if not zero
if (!m_map_count) return 0;
if (--m_map_count) return m_map_count;
// really do the unmap
if (m_size && m_address) {
// qDebug(" --- SwapFile::unmap() (%p)", this);
if (!m_file.unmap(static_cast<uchar *>(m_address))) {
qWarning("SwapFile(%s) -> unmap FAILED",
m_file.fileName().toLocal8Bit().data());
}
}
m_address = 0;
return m_map_count;
}
//***************************************************************************
int SwapFile::read(unsigned int offset, void *buffer, unsigned int length)
{
// seek to the given offset
if (!m_file.seek(offset)) return -1;
// read into the buffer
m_file.flush();
return m_file.read(reinterpret_cast<char *>(buffer), length);
}
//***************************************************************************
int SwapFile::write(unsigned int offset, const void *buffer,
unsigned int length)
{
// seek to the given offset
if (!m_file.seek(offset)) return -1;
// write data from the buffer
return m_file.write(reinterpret_cast<const char *>(buffer), length);
}
//***************************************************************************
//***************************************************************************