From: José Fonseca Date: Thu, 1 Sep 2011 12:47:14 +0000 (+0100) Subject: Add a usedCacheSize() method. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=42dbb827fc8340e7d1d12ab317b3841275c187e9;p=apitrace Add a usedCacheSize() method. Simplifies things a little, and makes the code slightly more robust, as I trying to figure out why sometimes I get traces with trailing garbagge. --- diff --git a/trace_snappyfile.cpp b/trace_snappyfile.cpp index 7678401..7e70725 100644 --- a/trace_snappyfile.cpp +++ b/trace_snappyfile.cpp @@ -190,14 +190,19 @@ void SnappyFile::rawFlush() void SnappyFile::flushCache() { if (m_mode == File::Write) { - size_t compressedLength; + size_t inputLength = usedCacheSize(); + + if (inputLength) { + size_t compressedLength; - ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(), - m_compressedCache, &compressedLength); + ::snappy::RawCompress(m_cache, inputLength, + m_compressedCache, &compressedLength); - writeCompressedLength(compressedLength); - m_stream.write(m_compressedCache, compressedLength); - m_cachePtr = m_cache; + 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; diff --git a/trace_snappyfile.hpp b/trace_snappyfile.hpp index 393fe02..61b7ab1 100644 --- a/trace_snappyfile.hpp +++ b/trace_snappyfile.hpp @@ -27,6 +27,8 @@ #ifndef TRACE_SNAPPYFILE_HPP #define TRACE_SNAPPYFILE_HPP +#include + #include "trace_file.hpp" #include @@ -59,12 +61,19 @@ protected: virtual void rawFlush(); private: + inline size_t usedCacheSize() const + { + assert(m_cachePtr >= m_cache); + return m_cachePtr - m_cache; + } inline size_t freeCacheSize() const { - if (m_cacheSize > 0) - return m_cacheSize - (m_cachePtr - m_cache); - else + assert(m_cacheSize >= usedCacheSize()); + if (m_cacheSize > 0) { + return m_cacheSize - usedCacheSize(); + } else { return 0; + } } inline bool endOfData() const {