本文整理汇总了C++中AsyncFio::read方法的典型用法代码示例。如果您正苦于以下问题:C++ AsyncFio::read方法的具体用法?C++ AsyncFio::read怎么用?C++ AsyncFio::read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AsyncFio
的用法示例。
在下文中一共展示了AsyncFio::read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
void Ps2File::cacheReadAhead() {
if (_cacheOpRunning) {
// there's already some cache read running
if (fio.poll(_fd)) // did it finish?
cacheReadSync(); // yes.
}
if ((!_cacheOpRunning) && ((_readBytesBlock >= CACHE_READ_THRESHOLD) || _stream) && fio.fioAvail()) {
// the engine seems to do sequential reads and there are no other I/Os going on. read ahead.
uint32 cachePosEnd = _cachePos + _bytesInCache;
if (_cachePos > _filePos)
return; // there was a seek in the meantime, don't cache.
if (cachePosEnd - _filePos >= CACHE_FILL_MIN)
return; // cache is full enough.
if (cachePosEnd == _fileSize)
return; // can't read beyond EOF.
assert(cachePosEnd < _fileSize);
if (_cachePos + _bytesInCache <= _filePos) {
_cacheOfs = _bytesInCache = 0;
_cachePos = cachePosEnd = _filePos & ~READ_ALIGN_MASK;
assert(_filePos == _physFilePos);
} else {
uint32 cacheDiff = _filePos - _cachePos;
assert(_bytesInCache >= cacheDiff);
cacheDiff &= ~READ_ALIGN_MASK;
_bytesInCache -= cacheDiff;
_cachePos += cacheDiff;
_cacheOfs = (_cacheOfs + cacheDiff) % CACHE_SIZE;
}
if (_physFilePos != cachePosEnd) {
sioprintf("unexpected _physFilePos %d cache %d %d\n", _physFilePos, _cacheOfs, _bytesInCache);
// assert(!(cachePosEnd & READ_ALIGN_MASK)); // romeo
_physFilePos = fio.seek(_fd, cachePosEnd, SEEK_SET);
if (_physFilePos != cachePosEnd) {
sioprintf("cache seek error: seek to %d instead of %d, fs = %d\n", _physFilePos, cachePosEnd, _fileSize);
return;
}
}
uint32 cacheDest = (_cacheOfs + _bytesInCache) % CACHE_SIZE;
uint32 cacheRead = CACHE_SIZE - _bytesInCache;
if (cacheDest + cacheRead > CACHE_SIZE)
cacheRead = CACHE_SIZE - cacheDest;
if (cacheRead > MAX_READ_STEP)
cacheRead = MAX_READ_STEP;
assert((!(cacheRead & READ_ALIGN_MASK)) && cacheRead);
_cacheOpRunning = true;
fio.read(_fd, _cacheBuf + cacheDest, cacheRead);
}
}
示例2:
uint32 Ps2ReadFile::read(void *dest, uint32 len) {
WaitSema(_sema);
uint8 *destBuf = (uint8*)dest;
if ((_filePos < _cachePos) || (_filePos + len > _cachePos + _bytesInCache))
cacheReadSync(); // we have to read from CD, sync cache.
while (len && (_filePos != _fileSize)) {
if ((_filePos >= _cachePos) && (_filePos < _cachePos + _bytesInCache)) { // read from cache
uint32 staPos = (_cacheOfs + (_filePos - _cachePos)) % CACHE_SIZE;
uint32 cpyLen = _bytesInCache - (_filePos - _cachePos);
if (cpyLen > len)
cpyLen = len;
if (staPos + cpyLen > CACHE_SIZE)
cpyLen = CACHE_SIZE - staPos;
assert(cpyLen);
memcpy(destBuf, _cacheBuf + staPos, cpyLen);
_filePos += cpyLen;
destBuf += cpyLen;
_readBytesBlock += len;
len -= cpyLen;
} else { // cache miss
assert(!_cacheOpRunning);
if (_physFilePos != _filePos) {
if ((_filePos < _physFilePos) || (_filePos > _physFilePos + (CACHE_SIZE / 2)))
_readBytesBlock = 0; // reset cache hit count
_physFilePos = _filePos & ~READ_ALIGN_MASK;
if (fio.seek(_fd, _physFilePos, SEEK_SET) != (int)_physFilePos)
break; // read beyond EOF
}
int doRead = len + (_filePos - _physFilePos);
doRead = (doRead + READ_ALIGN_MASK) & ~READ_ALIGN_MASK;
if (doRead > MAX_READ_STEP)
doRead = MAX_READ_STEP;
if (doRead < 2048)
doRead = 2048;
fio.read(_fd, _cacheBuf, doRead);
_cachePos = _physFilePos;
_cacheOfs = 0;
_bytesInCache = fio.sync(_fd);
_physFilePos += _bytesInCache;
if (!_bytesInCache)
break; // EOF
}
}
cacheReadAhead();
SignalSema(_sema);
return destBuf - (uint8*)dest;
}
示例3: if
bool Ps2File::open(const char *name, int mode) {
#if 1
_fd = fio.open(name, mode);
dbg_printf("open %s [%d]\n", name, _fd);
if (_fd >= 0) {
_mode = mode;
_filePos = 0;
if (_mode == O_RDONLY) {
_fileSize = fio.seek(_fd, 0, SEEK_END);
fio.seek(_fd, 0, SEEK_SET);
}
else
_fileSize = 0;
dbg_printf(" _mode = %x\n", _mode);
dbg_printf(" _fileSize = %d\n", _fileSize);
// dbg_printf(" _filePos = %d\n", _filePos);
return true;
}
return false;
#else
uint32 r;
// hack: FIO does not reports size for RW (?)
_fd = fio.open(name, O_RDONLY);
if (_fd >= 0) {
_fileSize = fio.seek(_fd, 0, SEEK_END);
fio.seek(_fd, 0, SEEK_SET); /* rewind ! */
if (_fileSize && mode != O_RDONLY) {
fio.read(_fd, _cacheBuf, _fileSize);
r = fio.sync(_fd);
dbg_printf(" sz=%d, read=%d\n", _fileSize, r);
assert(r == _fileSize);
}
fio.close(_fd);
}
else
_fileSize = 0; /* new file */
_fd = fio.open(name, mode);
dbg_printf("open %s [%d]\n", name, _fd);
if (_fd >= 0) {
_mode = mode;
_filePos = 0;
if (_fileSize) { /* existing data */
if (mode == O_RDONLY) {
/* DANGER: for w* modes it will truncate your fine files */
fio.seek(_fd, 0, SEEK_SET);
}
else if (_mode & O_APPEND) {
fio.seek(_fd, 0, _fileSize);
_filePos = _fileSize;
}
#if 0 /* file already trunc'd when opened as w* -> moved up */
if (mode != O_RDONLY) {
fio.read(_fd, _cacheBuf, _fileSize);
r = fio.sync(_fd);
dbg_printf(" sz=%d, read=%d\n", _fileSize, r);
assert(r == _fileSize);
// _fileSize = fio.seek(_fd, 0, SEEK_END);
}
#endif
}
dbg_printf(" _mode = %x\n", _mode);
dbg_printf(" _fileSize = %d\n", _fileSize);
dbg_printf(" _filePos = %d\n", _filePos);
return true;
} else
return false;
#endif
}