]> git.cworth.org Git - apitrace/blobdiff - trace_parser.cpp
Fix memory usage in the on-demand-loading.
[apitrace] / trace_parser.cpp
index 5e253c96bf18a2b29e66ecc80c2cd8e385d1be48..66d56a66bbbb2abf2d524665f9f4e88e1f894071 100644 (file)
@@ -28,8 +28,8 @@
 #include <assert.h>
 #include <stdlib.h>
 
-#include <zlib.h>
-
+#include "trace_file.hpp"
+#include "trace_snappyfile.hpp"
 #include "trace_parser.hpp"
 
 
@@ -52,8 +52,14 @@ Parser::~Parser() {
 
 
 bool Parser::open(const char *filename) {
-    file = gzopen(filename, "rb");
-    if (!file) {
+    assert(!file);
+    if (File::isZLibCompressed(filename)) {
+        file = new ZLibFile;
+    } else {
+        file = new SnappyFile;
+    }
+
+    if (!file->open(filename, File::Read)) {
         return false;
     }
 
@@ -78,14 +84,16 @@ deleteAll(Iter begin, Iter end)
 
 template <typename Container>
 inline void
-deleteAll(const Container &c)
+deleteAll(Container &c)
 {
     deleteAll(c.begin(), c.end());
+    c.clear();
 }
 
 void Parser::close(void) {
     if (file) {
-        gzclose(file);
+        file->close();
+        delete file;
         file = NULL;
     }
 
@@ -138,20 +146,35 @@ void Parser::parse_enter(void) {
     size_t id = read_uint();
 
     FunctionSig *sig = lookup(functions, id);
-    if (!sig) {
-        sig = new FunctionSig;
-        sig->name = read_string();
-        sig->num_args = read_uint();
-        const char **arg_names = new const char *[sig->num_args];
-        for (unsigned i = 0; i < sig->num_args; ++i) {
-            arg_names[i] = read_string();
+    const File::Offset offset = file->currentOffset();
+    bool callWithSig = callWithSignature(offset);
+    if (!sig || callWithSig) {
+        if (!sig) {
+            sig = new FunctionSig;
+            sig->id = id;
+            sig->name = read_string();
+            sig->num_args = read_uint();
+            const char **arg_names = new const char *[sig->num_args];
+            for (unsigned i = 0; i < sig->num_args; ++i) {
+                arg_names[i] = read_string();
+            }
+            sig->arg_names = arg_names;
+            functions[id] = sig;
+            m_callSigOffsets.insert(offset);
+        } else {
+            /* skip over the signature */
+            read_string(); /* name */
+            int num_args = read_uint();
+            for (unsigned i = 0; i < num_args; ++i) {
+                 read_string(); /*arg_name*/
+            }
         }
-        sig->arg_names = arg_names;
-        functions[id] = sig;
     }
     assert(sig);
 
     Call *call = new Call(sig);
+
+
     call->no = next_call_no++;
 
     if (parse_call_details(call)) {
@@ -198,7 +221,8 @@ bool Parser::parse_call_details(Call *call) {
             call->ret = parse_value();
             break;
         default:
-            std::cerr << "error: unknown call detail " << c << "\n";
+            std::cerr << "error: ("<<call->name()<< ") unknown call detail "
+                      << c << "\n";
             exit(1);
         case -1:
             return false;
@@ -292,14 +316,14 @@ Value *Parser::parse_uint() {
 
 Value *Parser::parse_float() {
     float value;
-    gzread(file, &value, sizeof value);
+    file->read(&value, sizeof value);
     return new Float(value);
 }
 
 
 Value *Parser::parse_double() {
     double value;
-    gzread(file, &value, sizeof value);
+    file->read(&value, sizeof value);
     return new Float(value);
 }
 
@@ -312,14 +336,23 @@ Value *Parser::parse_string() {
 Value *Parser::parse_enum() {
     size_t id = read_uint();
     EnumSig *sig = lookup(enums, id);
-    if (!sig) {
-        sig = new EnumSig;
-        sig->id = id;
-        sig->name = read_string();
-        Value *value = parse_value();
-        sig->value = value->toSInt();
-        delete value;
-        enums[id] = sig;
+    const File::Offset offset = file->currentOffset();
+    bool enumWithSig = enumWithSignature(offset);
+    if (!sig || enumWithSig) {
+        if (!sig) {
+            sig = new EnumSig;
+            sig->id = id;
+            sig->name = read_string();
+            Value *value = parse_value();
+            sig->value = value->toSInt();
+            delete value;
+            enums[id] = sig;
+            m_enumSigOffsets.insert(offset);
+        } else {
+            read_string(); /*name*/
+            Value *value = parse_value();
+            delete value;
+        }
     }
     assert(sig);
     return new Enum(sig);
@@ -329,20 +362,31 @@ Value *Parser::parse_enum() {
 Value *Parser::parse_bitmask() {
     size_t id = read_uint();
     BitmaskSig *sig = lookup(bitmasks, id);
-    if (!sig) {
-        sig = new BitmaskSig;
-        sig->id = id;
-        sig->num_flags = read_uint();
-        BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
-        for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
-            it->name = read_string();
-            it->value = read_uint();
-            if (it->value == 0 && it != flags) {
-                std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
+    const File::Offset offset = file->currentOffset();
+    bool bitmaskWithSig = bitmaskWithSignature(offset);
+    if (!sig || bitmaskWithSig) {
+        if (!sig) {
+            sig = new BitmaskSig;
+            sig->id = id;
+            sig->num_flags = read_uint();
+            BitmaskFlag *flags = new BitmaskFlag[sig->num_flags];
+            for (BitmaskFlag *it = flags; it != flags + sig->num_flags; ++it) {
+                it->name = read_string();
+                it->value = read_uint();
+                if (it->value == 0 && it != flags) {
+                    std::cerr << "warning: bitmask " << it->name << " is zero but is not first flag\n";
+                }
+            }
+            sig->flags = flags;
+            bitmasks[id] = sig;
+            m_bitmaskSigOffsets.insert(offset);
+        } else {
+            int num_flags = read_uint();
+            for (int i = 0; i < num_flags; ++i) {
+                read_string(); /*name */
+                read_uint(); /* value */
             }
         }
-        sig->flags = flags;
-        bitmasks[id] = sig;
     }
     assert(sig);
 
@@ -366,7 +410,7 @@ Value *Parser::parse_blob(void) {
     size_t size = read_uint();
     Blob *blob = new Blob(size);
     if (size) {
-        gzread(file, blob->buf, (unsigned)size);
+        file->read(blob->buf, (unsigned)size);
     }
     return blob;
 }
@@ -376,17 +420,28 @@ Value *Parser::parse_struct() {
     size_t id = read_uint();
 
     StructSig *sig = lookup(structs, id);
-    if (!sig) {
-        sig = new StructSig;
-        sig->id = id;
-        sig->name = read_string();
-        sig->num_members = read_uint();
-        const char **member_names = new const char *[sig->num_members];
-        for (unsigned i = 0; i < sig->num_members; ++i) {
-            member_names[i] = read_string();
+    const File::Offset offset = file->currentOffset();
+    bool structWithSig = structWithSignature(offset);
+    if (!sig || structWithSig) {
+        if (!sig) {
+            sig = new StructSig;
+            sig->id = id;
+            sig->name = read_string();
+            sig->num_members = read_uint();
+            const char **member_names = new const char *[sig->num_members];
+            for (unsigned i = 0; i < sig->num_members; ++i) {
+                member_names[i] = read_string();
+            }
+            sig->member_names = member_names;
+            structs[id] = sig;
+            m_structSigOffsets.insert(offset);
+        } else {
+            read_string(); /* name */
+            unsigned num_members = read_uint();
+            for (unsigned i = 0; i < num_members; ++i) {
+                read_string(); /* member_name */
+            }
         }
-        sig->member_names = member_names;
-        structs[id] = sig;
     }
     assert(sig);
 
@@ -411,7 +466,7 @@ const char * Parser::read_string(void) {
     size_t len = read_uint();
     char * value = new char[len + 1];
     if (len) {
-        gzread(file, value, (unsigned)len);
+        file->read(value, (unsigned)len);
     }
     value[len] = 0;
 #if TRACE_VERBOSE
@@ -426,7 +481,7 @@ unsigned long long Parser::read_uint(void) {
     int c;
     unsigned shift = 0;
     do {
-        c = gzgetc(file);
+        c = file->getc();
         if (c == -1) {
             break;
         }
@@ -441,7 +496,7 @@ unsigned long long Parser::read_uint(void) {
 
 
 inline int Parser::read_byte(void) {
-    int c = gzgetc(file);
+    int c = file->getc();
 #if TRACE_VERBOSE
     if (c < 0)
         std::cerr << "\tEOF" << "\n";
@@ -452,4 +507,24 @@ inline int Parser::read_byte(void) {
 }
 
 
+inline bool Parser::callWithSignature(const File::Offset &offset) const
+{
+    return m_callSigOffsets.find(offset) != m_callSigOffsets.end();
+}
+
+inline bool Parser::structWithSignature(const File::Offset &offset) const
+{
+    return m_structSigOffsets.find(offset) != m_structSigOffsets.end();
+}
+
+inline bool Parser::enumWithSignature(const File::Offset &offset) const
+{
+    return m_enumSigOffsets.find(offset) != m_enumSigOffsets.end();
+}
+
+inline bool Parser::bitmaskWithSignature(const File::Offset &offset) const
+{
+    return m_bitmaskSigOffsets.find(offset) != m_bitmaskSigOffsets.end();
+}
+
 } /* namespace Trace */