From: José Fonseca Date: Sat, 3 Sep 2011 12:30:16 +0000 (+0100) Subject: Split flushCache into read/write versions. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=f3acd09b5367d91ae4e8168b55a5be53ae4e2386;p=apitrace Split flushCache into read/write versions. --- diff --git a/trace_file.hpp b/trace_file.hpp index c709050..60a8459 100644 --- a/trace_file.hpp +++ b/trace_file.hpp @@ -124,7 +124,9 @@ inline void File::close() inline void File::flush(void) { - rawFlush(); + if (m_mode == File::Write) { + rawFlush(); + } } inline int File::getc() diff --git a/trace_snappyfile.cpp b/trace_snappyfile.cpp index 7e70725..6a60441 100644 --- a/trace_snappyfile.cpp +++ b/trace_snappyfile.cpp @@ -97,7 +97,7 @@ bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode) m_stream >> byte2; assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2); - flushCache(); + flushReadCache(); } else if (m_stream.is_open() && mode == File::Write) { // write the snappy file identifier m_stream << SNAPPY_BYTE1; @@ -114,7 +114,7 @@ bool SnappyFile::rawWrite(const void *buffer, size_t length) } else if (freeCacheSize() == length) { memcpy(m_cachePtr, buffer, length); m_cachePtr += length; - flushCache(); + flushWriteCache(); } else { int sizeToWrite = length; @@ -124,7 +124,7 @@ bool SnappyFile::rawWrite(const void *buffer, size_t length) memcpy(m_cachePtr, (const char*)buffer + offset, endSize); sizeToWrite -= endSize; m_cachePtr += endSize; - flushCache(); + flushWriteCache(); } if (sizeToWrite) { int offset = length - sizeToWrite; @@ -154,10 +154,12 @@ bool SnappyFile::rawRead(void *buffer, size_t length) memcpy((char*)buffer + offset, m_cachePtr, chunkSize); m_cachePtr += chunkSize; sizeToRead -= chunkSize; - if (sizeToRead > 0) - flushCache(); - if (!m_cacheSize) + if (sizeToRead > 0) { + flushReadCache(); + } + if (!m_cacheSize) { break; + } } } @@ -174,7 +176,9 @@ int SnappyFile::rawGetc() void SnappyFile::rawClose() { - flushCache(); + if (m_mode == File::Write) { + flushWriteCache(); + } m_stream.close(); delete [] m_cache; m_cache = NULL; @@ -183,41 +187,43 @@ void SnappyFile::rawClose() void SnappyFile::rawFlush() { - flushCache(); + assert(m_mode == File::Write); + flushWriteCache(); m_stream.flush(); } -void SnappyFile::flushCache() +void SnappyFile::flushWriteCache() { - if (m_mode == File::Write) { - size_t inputLength = usedCacheSize(); + size_t inputLength = usedCacheSize(); - if (inputLength) { - size_t compressedLength; + if (inputLength) { + size_t compressedLength; - ::snappy::RawCompress(m_cache, inputLength, - m_compressedCache, &compressedLength); + ::snappy::RawCompress(m_cache, inputLength, + m_compressedCache, &compressedLength); - writeCompressedLength(compressedLength); - m_stream.write(m_compressedCache, compressedLength); - m_cachePtr = m_cache; - } - assert(m_cachePtr == m_cache); - } else if (m_mode == File::Read) { - //assert(m_cachePtr == m_cache + m_cacheSize); - size_t compressedLength; - compressedLength = readCompressedLength(); - - if (compressedLength) { - m_stream.read((char*)m_compressedCache, compressedLength); - ::snappy::GetUncompressedLength(m_compressedCache, compressedLength, - &m_cacheSize); - createCache(m_cacheSize); - ::snappy::RawUncompress(m_compressedCache, compressedLength, - m_cache); - } else { - createCache(0); - } + writeCompressedLength(compressedLength); + m_stream.write(m_compressedCache, compressedLength); + m_cachePtr = m_cache; + } + assert(m_cachePtr == m_cache); +} + +void SnappyFile::flushReadCache() +{ + //assert(m_cachePtr == m_cache + m_cacheSize); + size_t compressedLength; + compressedLength = readCompressedLength(); + + if (compressedLength) { + m_stream.read((char*)m_compressedCache, compressedLength); + ::snappy::GetUncompressedLength(m_compressedCache, compressedLength, + &m_cacheSize); + createCache(m_cacheSize); + ::snappy::RawUncompress(m_compressedCache, compressedLength, + m_cache); + } else { + createCache(0); } } diff --git a/trace_snappyfile.hpp b/trace_snappyfile.hpp index 61b7ab1..b807bfb 100644 --- a/trace_snappyfile.hpp +++ b/trace_snappyfile.hpp @@ -79,7 +79,8 @@ private: { return m_stream.eof() && freeCacheSize() == 0; } - void flushCache(); + void flushWriteCache(); + void flushReadCache(); void createCache(size_t size); void writeCompressedLength(size_t length); size_t readCompressedLength();