X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=common%2Ftrace_file_snappy.cpp;h=c0727cd28e86df4e54573e7094e7125cd91053a2;hb=bcb5850afb381e4814d247917311494d7edd2e91;hp=60f003bd828c4f1b43a389ae54d58bcdf54f61f4;hpb=4159a6196b23dae0be22dde17a85783fe4036517;p=apitrace diff --git a/common/trace_file_snappy.cpp b/common/trace_file_snappy.cpp index 60f003b..c0727cd 100644 --- a/common/trace_file_snappy.cpp +++ b/common/trace_file_snappy.cpp @@ -67,7 +67,7 @@ #define SNAPPY_BYTE2 't' -using namespace Trace; +using namespace trace; class SnappyFile : public File { @@ -82,7 +82,7 @@ public: protected: virtual bool rawOpen(const std::string &filename, File::Mode mode); virtual bool rawWrite(const void *buffer, size_t length); - virtual bool rawRead(void *buffer, size_t length); + virtual size_t rawRead(void *buffer, size_t length); virtual int rawGetc(); virtual void rawClose(); virtual void rawFlush(); @@ -115,9 +115,10 @@ private: size_t readCompressedLength(); private: std::fstream m_stream; + size_t m_cacheMaxSize; + size_t m_cacheSize; char *m_cache; char *m_cachePtr; - size_t m_cacheSize; char *m_compressedCache; @@ -128,9 +129,10 @@ private: SnappyFile::SnappyFile(const std::string &filename, File::Mode mode) : File(), - m_cache(0), - m_cachePtr(0), - m_cacheSize(0) + m_cacheMaxSize(SNAPPY_CHUNK_SIZE), + m_cacheSize(m_cacheMaxSize), + m_cache(new char [m_cacheMaxSize]), + m_cachePtr(m_cache) { size_t maxCompressedLength = snappy::MaxCompressedLength(SNAPPY_CHUNK_SIZE); @@ -139,6 +141,7 @@ SnappyFile::SnappyFile(const std::string &filename, SnappyFile::~SnappyFile() { + close(); delete [] m_compressedCache; delete [] m_cache; } @@ -206,10 +209,10 @@ bool SnappyFile::rawWrite(const void *buffer, size_t length) return true; } -bool SnappyFile::rawRead(void *buffer, size_t length) +size_t SnappyFile::rawRead(void *buffer, size_t length) { if (endOfData()) { - return false; + return 0; } if (freeCacheSize() >= length) { @@ -228,18 +231,18 @@ bool SnappyFile::rawRead(void *buffer, size_t length) flushReadCache(); } if (!m_cacheSize) { - break; + return length - sizeToRead; } } } - return true; + return length; } int SnappyFile::rawGetc() { - int c = 0; - if (!rawRead(&c, 1)) + unsigned char c = 0; + if (rawRead(&c, 1) != 1) return -1; return c; } @@ -302,16 +305,14 @@ void SnappyFile::flushReadCache(size_t skipLength) void SnappyFile::createCache(size_t size) { - // TODO: only re-allocate if the current buffer is not big enough + if (size > m_cacheMaxSize) { + do { + m_cacheMaxSize <<= 1; + } while (size > m_cacheMaxSize); - if (m_cache) { delete [] m_cache; - } - - if (size) { m_cache = new char[size]; - } else { - m_cache = NULL; + m_cacheMaxSize = size; } m_cachePtr = m_cache;