]> git.cworth.org Git - apitrace/blobdiff - trace_snappyfile.cpp
Recognise glFrameTerminatorGREMEDY when retracing.
[apitrace] / trace_snappyfile.cpp
index c161b9d2b9017263c9c606daf2a681962cd62803..7e7072543426851c125257d2c9d0b5e0367e3e86 100644 (file)
@@ -74,6 +74,7 @@ SnappyFile::SnappyFile(const std::string &filename,
 SnappyFile::~SnappyFile()
 {
     delete [] m_compressedCache;
+    delete [] m_cache;
 }
 
 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
@@ -189,42 +190,51 @@ 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) {
-        if (m_stream.eof())
-            return;
         //assert(m_cachePtr == m_cache + m_cacheSize);
         size_t compressedLength;
         compressedLength = readCompressedLength();
-        m_stream.read((char*)m_compressedCache, compressedLength);
-        /*
-         * The reason we peek here is because the last read will
-         * read all the way until the last character, but that will not
-         * trigger m_stream.eof() to be set, so by calling peek
-         * we assure that if we in fact have read the entire stream
-         * then the m_stream.eof() is always set.
-         */
-        m_stream.peek();
-        ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
-                                        &m_cacheSize);
-        if (m_cache)
-            delete [] m_cache;
-        createCache(m_cacheSize);
-        ::snappy::RawUncompress(m_compressedCache, compressedLength,
-                                m_cache);
+
+        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);
+        }
     }
 }
 
 void SnappyFile::createCache(size_t size)
 {
-    m_cache = new char[size];
+    // TODO: only re-allocate if the current buffer is not big enough
+
+    if (m_cache) {
+        delete [] m_cache;
+    }
+
+    if (size) {
+        m_cache = new char[size];
+    } else {
+        m_cache = NULL;
+    }
+
     m_cachePtr = m_cache;
     m_cacheSize = size;
 }