gallium: Switch u_debug_stack/symbol.c to util/hash_table.h
[mesa.git] / src / gallium / auxiliary / util / u_debug_symbol.c
index cfd354a6fe63073ae5b2082344e5f76ac9ed58cd..73225e9e49544ed443c7696f1c5da2099f633207 100644 (file)
 
 #include "pipe/p_compiler.h"
 #include "os/os_thread.h"
-#include "u_string.h"
+#include "util/u_string.h"
 
-#include "u_debug.h"
+#include "util/u_debug.h"
 #include "u_debug_symbol.h"
-#include "u_hash_table.h"
+#include "util/hash_table.h"
 
 
 #if defined(PIPE_OS_WINDOWS)
@@ -191,9 +191,9 @@ debug_symbol_name_dbghelp(const void *addr, char* buf, unsigned size)
       if (GetModuleFileNameA(hModule, buffer, sizeof buffer) == sizeof buffer) {
          return FALSE;
       }
-      util_snprintf(buf, size, "%p at %s+0x%lx",
-                    addr, buffer,
-                    (unsigned long)((uintptr_t)addr - (uintptr_t)hModule));
+      snprintf(buf, size, "%p at %s+0x%lx",
+               addr, buffer,
+               (unsigned long)((uintptr_t)addr - (uintptr_t)hModule));
 
       return TRUE;
    }
@@ -208,9 +208,9 @@ debug_symbol_name_dbghelp(const void *addr, char* buf, unsigned size)
    }
 
    if (Line.FileName) {
-      util_snprintf(buf, size, "%s at %s:%lu", pSymbol->Name, Line.FileName, Line.LineNumber);
+      snprintf(buf, size, "%s at %s:%lu", pSymbol->Name, Line.FileName, Line.LineNumber);
    } else {
-      util_snprintf(buf, size, "%s", pSymbol->Name);
+      snprintf(buf, size, "%s", pSymbol->Name);
    }
 
    return TRUE;
@@ -219,7 +219,7 @@ debug_symbol_name_dbghelp(const void *addr, char* buf, unsigned size)
 #endif /* PIPE_OS_WINDOWS */
 
 
-#if defined(__GLIBC__) && !defined(__UCLIBC__)
+#if defined(HAVE_EXECINFO_H)
 
 #include <execinfo.h>
 
@@ -234,13 +234,13 @@ debug_symbol_name_glibc(const void *addr, char* buf, unsigned size)
    if (!syms) {
       return FALSE;
    }
-   strncpy(buf, syms[0], size);
+   strncpy(buf, syms[0], size - 1);
    buf[size - 1] = 0;
    free(syms);
    return TRUE;
 }
 
-#endif /* defined(__GLIBC__) && !defined(__UCLIBC__) */
+#endif /* defined(HAVE_EXECINFO_H) */
 
 
 void
@@ -252,13 +252,13 @@ debug_symbol_name(const void *addr, char* buf, unsigned size)
    }
 #endif
 
-#if defined(__GLIBC__) && !defined(__UCLIBC__)
+#if defined(HAVE_EXECINFO_H)
    if (debug_symbol_name_glibc(addr, buf, size)) {
        return;
    }
-#endif
+#endif /* defined(HAVE_EXECINFO_H) */
 
-   util_snprintf(buf, size, "%p", addr);
+   snprintf(buf, size, "%p", addr);
    buf[size - 1] = 0;
 }
 
@@ -270,49 +270,37 @@ debug_symbol_print(const void *addr)
    debug_printf("\t%s\n", buf);
 }
 
-struct util_hash_table* symbols_hash;
+static struct hash_table* symbols_hash;
+#ifdef PIPE_OS_WINDOWS
+static mtx_t symbols_mutex;
+#else
 static mtx_t symbols_mutex = _MTX_INITIALIZER_NP;
-
-static unsigned hash_ptr(void* p)
-{
-   return (unsigned)(uintptr_t)p;
-}
-
-static int compare_ptr(void* a, void* b)
-{
-   if(a == b)
-      return 0;
-   else if(a < b)
-      return -1;
-   else
-      return 1;
-}
+#endif
 
 const char*
 debug_symbol_name_cached(const void *addr)
 {
    const char* name;
-#ifdef PIPE_SUBSYSTEM_WINDOWS_USER
+#ifdef PIPE_OS_WINDOWS
    static boolean first = TRUE;
 
    if (first) {
-      pipe_mutex_init(symbols_mutex);
+      (void) mtx_init(&symbols_mutex, mtx_plain);
       first = FALSE;
    }
 #endif
 
-   pipe_mutex_lock(symbols_mutex);
+   mtx_lock(&symbols_mutex);
    if(!symbols_hash)
-      symbols_hash = util_hash_table_create(hash_ptr, compare_ptr);
-   name = util_hash_table_get(symbols_hash, (void*)addr);
-   if(!name)
-   {
+      symbols_hash = _mesa_pointer_hash_table_create(NULL);
+   struct hash_entry *entry = _mesa_hash_table_search(symbols_hash, addr);
+   if (!entry) {
       char buf[1024];
       debug_symbol_name(addr, buf, sizeof(buf));
       name = strdup(buf);
 
-      util_hash_table_set(symbols_hash, (void*)addr, (void*)name);
+      entry = _mesa_hash_table_insert(symbols_hash, addr, (void*)name);
    }
-   pipe_mutex_unlock(symbols_mutex);
-   return name;
+   mtx_unlock(&symbols_mutex);
+   return entry->data;
 }