]> git.cworth.org Git - apitrace/blobdiff - trace_write.cpp
Fix makefile patterns.
[apitrace] / trace_write.cpp
index 912824e012d936db5ef8bc0b478771c7608788d0..854db104b1755d7e39101ce2bf42e390c50d8076 100644 (file)
@@ -30,7 +30,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include <map>
+#include <vector>
 
 #include <zlib.h>
 
@@ -141,22 +141,6 @@ WriteString(const char *str) {
    Write(str, len);
 }
 
-typedef std::map<const char *, size_t> namemap;
-static namemap names;
-
-static inline void 
-WriteName(const char *name) {
-   namemap::iterator it = names.find(name);
-   if (it == names.end()) {
-       size_t name_id = names.size();
-       WriteUInt(name_id);
-       WriteString(name);
-       names[name] = name_id;
-   } else {
-       WriteUInt(it->second);
-   }
-}
-
 void Open(void) {
     if (!g_gzFile) {
         _Open("trace");
@@ -170,11 +154,34 @@ void Close(void) {
 
 static unsigned call_no = 0;
 
-unsigned BeginEnter(const char *function) {
+inline bool lookup(std::vector<bool> &map, size_t index) {
+   if (index >= map.size()) {
+      map.resize(index + 1);
+      return false;
+   } else {
+      return map[index];
+   }
+}
+
+static std::vector<bool> functions;
+static std::vector<bool> structs;
+static std::vector<bool> enums;
+static std::vector<bool> bitmasks;
+
+
+unsigned BeginEnter(const FunctionSig &function) {
    OS::AcquireMutex();
    Open();
    WriteByte(Trace::EVENT_ENTER);
-   WriteName(function);
+   WriteUInt(function.id);
+   if (!lookup(functions, function.id)) {
+      WriteString(function.name);
+      WriteUInt(function.num_args);
+      for (unsigned i = 0; i < function.num_args; ++i) {
+         WriteString(function.args[i]);
+      }
+      functions[function.id] = true;
+   }
    return call_no++;
 }
 
@@ -196,10 +203,9 @@ void EndLeave(void) {
    OS::ReleaseMutex();
 }
 
-void BeginArg(unsigned index, const char *name) {
+void BeginArg(unsigned index) {
    WriteByte(Trace::CALL_ARG);
    WriteUInt(index);
-   WriteName(name);
 }
 
 void BeginReturn(void) {
@@ -211,21 +217,17 @@ void BeginArray(size_t length) {
    WriteUInt(length);
 }
 
-void BeginStruct(size_t length) {
+void BeginStruct(const StructSig *sig) {
    WriteByte(Trace::TYPE_STRUCT);
-   WriteUInt(length);
-}
-
-void BeginMember(const char *name) {
-   WriteName(name);
-}
-
-void BeginBitmask(void) {
-   WriteByte(Trace::TYPE_BITMASK);
-}
-
-void EndBitmask(void) {
-   WriteByte(Trace::TYPE_NULL);
+   WriteUInt(sig->id);
+   if (!lookup(structs, sig->id)) {
+      WriteString(sig->name);
+      WriteUInt(sig->num_members);
+      for (unsigned i = 0; i < sig->num_members; ++i) {
+         WriteString(sig->members[i]);
+      }
+      structs[sig->id] = true;
+   }
 }
 
 void LiteralBool(bool value) {
@@ -297,18 +299,20 @@ void LiteralBlob(const void *data, size_t size) {
    }
 }
 
-void LiteralNamedConstant(const char *name, long long value) {
-   WriteByte(Trace::TYPE_CONST);
-   WriteName(name);
-   LiteralSInt(value);
+void LiteralEnum(const EnumSig *sig) {
+   WriteByte(Trace::TYPE_ENUM);
+   WriteUInt(sig->id);
+   if (!lookup(enums, sig->id)) {
+      WriteString(sig->name);
+      LiteralSInt(sig->value);
+      enums[sig->id] = true;
+   }
 }
 
-static std::map<Id, bool> bitmasks;
-
 void LiteralBitmask(const BitmaskSig &bitmask, unsigned long long value) {
    WriteByte(Trace::TYPE_BITMASK);
    WriteUInt(bitmask.id);
-   if (!bitmasks[bitmask.id]) {
+   if (!lookup(bitmasks, bitmask.id)) {
       WriteUInt(bitmask.count);
       for (unsigned i = 0; i < bitmask.count; ++i) {
          WriteString(bitmask.values[i].name);
@@ -338,3 +342,33 @@ void Abort(void) {
 }
 
 } /* namespace Trace */
+
+
+#ifdef WIN32
+
+#if 0
+BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
+    switch(fdwReason) {
+    case DLL_PROCESS_ATTACH:
+    case DLL_THREAD_ATTACH:
+        return TRUE;
+    case DLL_THREAD_DETACH:
+        return TRUE;
+    case DLL_PROCESS_DETACH:
+        Trace::Close();
+        return TRUE;
+    }
+    (void)hinstDLL;
+    (void)lpvReserved;
+    return TRUE;
+}
+#endif
+
+#else
+
+static void _uninit(void) __attribute__((destructor));
+static void _uninit(void) {
+    Trace::Close();
+}
+
+#endif