util: Improve prototype of debug_get_num_option and debug_get_flags_option
authorYonggang Luo <luoyonggang@gmail.com>
Wed, 2 Nov 2022 06:46:16 +0000 (14:46 +0800)
committerMarge Bot <emma+marge@anholt.net>
Fri, 16 Dec 2022 19:30:19 +0000 (19:30 +0000)
Getting debug_get_num_option to return int64_t, as long under 64 bit Linux are 64 bit size,
 so using fixed int64_t for cross platform consistence, as long under win32 is 32 bit size.

Getting DEBUG_GET_ONCE_FLAGS_OPTION to return uint64_t to getting it to be
consistence with debug_get_flags_option.

DEBUG_GET_ONCE_NUM_OPTION is not accessed in codebase, so add unittest for it, it maybe
used in future, remove it is not consistence

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
Acked-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19554>

src/util/tests/u_debug_test.cpp
src/util/u_debug.c
src/util/u_debug.h

index 4dcac24..5b3e74c 100644 (file)
@@ -183,3 +183,34 @@ TEST(u_debug, debug_get_num_option)
       EXPECT_EQ(debug_get_num_option("MESA_UNIT_TEST_NUM_VARIABLE_2", 100), 100);
    }
 }
+
+DEBUG_GET_ONCE_NUM_OPTION(num_once_test_0, "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_0", -33)
+
+DEBUG_GET_ONCE_NUM_OPTION(num_once_test_1, "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_1", 0)
+
+DEBUG_GET_ONCE_NUM_OPTION(num_once_test_2, "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_2", 0)
+
+TEST(u_debug, DEBUG_GET_ONCE_NUM_OPTION_Macro)
+{
+   {
+      EXPECT_EQ(debug_get_option_num_once_test_0(), -33);
+   }
+
+   {
+      static char env_str[] = "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_1=9223372036854775807";
+      putenv(env_str);
+      EXPECT_EQ(debug_get_option_num_once_test_1(), INT64_MAX);
+   }
+
+   {
+      static char env_str[] = "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_1=9223372036854775806";
+      putenv(env_str);
+      EXPECT_EQ(debug_get_option_num_once_test_1(), INT64_MAX);
+   }
+
+   {
+      static char env_str[] = "MESA_UNIT_TEST_DEBUG_GET_ONCE_NUM_VARIABLE_2=-9223372036854775808";
+      putenv(env_str);
+      EXPECT_EQ(debug_get_option_num_once_test_2(), INT64_MIN);
+   }
+}
index 0c40337..d7ae2f9 100644 (file)
@@ -184,10 +184,10 @@ debug_get_bool_option(const char *name, bool dfault)
 }
 
 
-long
-debug_get_num_option(const char *name, long dfault)
+int64_t
+debug_get_num_option(const char *name, int64_t dfault)
 {
-   long result;
+   int64_t result;
    const char *str;
 
    str = os_get_option(name);
@@ -196,7 +196,7 @@ debug_get_num_option(const char *name, long dfault)
    } else {
       char *endptr;
 
-      result = strtol(str, &endptr, 0);
+      result = strtoll(str, &endptr, 0);
       if (str == endptr) {
          /* Restore the default value when no digits were found. */
          result = dfault;
@@ -204,7 +204,7 @@ debug_get_num_option(const char *name, long dfault)
    }
 
    if (debug_get_option_should_print())
-      debug_printf("%s: %s = %li\n", __func__, name, result);
+      debug_printf("%s: %s = %"PRId64"\n", __func__, name, result);
 
    return result;
 }
@@ -326,7 +326,7 @@ debug_get_flags_option(const char *name,
 
 const char *
 debug_dump_enum(const struct debug_named_value *names,
-                unsigned long value)
+                uint64_t value)
 {
    static char rest[64];
 
@@ -336,13 +336,13 @@ debug_dump_enum(const struct debug_named_value *names,
       ++names;
    }
 
-   snprintf(rest, sizeof(rest), "0x%08lx", value);
+   snprintf(rest, sizeof(rest), "0x%08"PRIx64, value);
    return rest;
 }
 
 
 const char *
-debug_dump_flags(const struct debug_named_value *names, unsigned long value)
+debug_dump_flags(const struct debug_named_value *names, uint64_t value)
 {
    static char output[4096];
    static char rest[256];
@@ -369,7 +369,7 @@ debug_dump_flags(const struct debug_named_value *names, unsigned long value)
       else
          first = 0;
 
-      snprintf(rest, sizeof(rest), "0x%08lx", value);
+      snprintf(rest, sizeof(rest), "0x%08"PRIx64, value);
       strncat(output, rest, sizeof(output) - strlen(output) - 1);
       output[sizeof(output) - 1] = '\0';
    }
index a3858fd..ab65ef2 100644 (file)
@@ -187,9 +187,6 @@ debug_disable_win32_error_dialogs(void);
 #endif /* !DEBUG */
 
 
-long
-debug_get_num_option(const char *name, long dfault);
-
 void
 debug_get_version_option(const char *name, unsigned *major, unsigned *minor);
 
@@ -310,8 +307,8 @@ struct debug_named_value
  *    ...
  * @endcode
  */
-#define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (unsigned long)__symbol, NULL}
-#define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (unsigned long)__symbol, __desc}
+#define DEBUG_NAMED_VALUE(__symbol) {#__symbol, (uint64_t)__symbol, NULL}
+#define DEBUG_NAMED_VALUE_WITH_DESCRIPTION(__symbol, __desc) {#__symbol, (uint64_t)__symbol, __desc}
 #define DEBUG_NAMED_VALUE_END {NULL, 0, NULL}
 
 
@@ -320,14 +317,14 @@ struct debug_named_value
  */
 const char *
 debug_dump_enum(const struct debug_named_value *names,
-                unsigned long value);
+                uint64_t value);
 
 /**
  * Convert binary flags value to a string.
  */
 const char *
 debug_dump_flags(const struct debug_named_value *names,
-                 unsigned long value);
+                 uint64_t value);
 
 
 struct debug_control {
@@ -361,8 +358,8 @@ debug_get_option(const char *name, const char *dfault);
 bool
 debug_get_bool_option(const char *name, bool dfault);
 
-long
-debug_get_num_option(const char *name, long dfault);
+int64_t
+debug_get_num_option(const char *name, int64_t dfault);
 
 uint64_t
 debug_get_flags_option(const char *name,
@@ -406,11 +403,11 @@ debug_get_option_ ## sufix (void) \
 }
 
 #define DEBUG_GET_ONCE_NUM_OPTION(sufix, name, dfault) \
-static long \
+static int64_t \
 debug_get_option_ ## sufix (void) \
 { \
    static bool initialized = false; \
-   static long value; \
+   static int64_t value; \
    if (unlikely(!p_atomic_read_relaxed(&initialized))) { \
       value = debug_get_num_option(name, dfault); \
       p_atomic_set(&initialized, true); \
@@ -419,11 +416,11 @@ debug_get_option_ ## sufix (void) \
 }
 
 #define DEBUG_GET_ONCE_FLAGS_OPTION(sufix, name, flags, dfault) \
-static unsigned long \
+static uint64_t \
 debug_get_option_ ## sufix (void) \
 { \
    static bool initialized = false; \
-   static unsigned long value; \
+   static uint64_t value; \
    if (unlikely(!p_atomic_read_relaxed(&initialized))) { \
       value = debug_get_flags_option(name, flags, dfault); \
       p_atomic_set(&initialized, true); \