[Mesa-dev] [PATCH 08/14] util: env_var_as_unsigned() helper

Eric Engestrom eric.engestrom at intel.com
Thu Nov 15 17:11:49 UTC 2018


On Monday, 2018-11-12 10:50:52 -0500, Rob Clark wrote:
> So I can drop env2u() helper from freedreno_util.h and get rid of one
> small ir3 dependency on gallium/freedreno
> 
> Signed-off-by: Rob Clark <robdclark at gmail.com>
> ---
>  src/util/debug.c | 12 ++++++++++++
>  src/util/debug.h |  2 ++
>  2 files changed, 14 insertions(+)
> 
> diff --git a/src/util/debug.c b/src/util/debug.c
> index 98b1853325d..6142801ef60 100644
> --- a/src/util/debug.c
> +++ b/src/util/debug.c
> @@ -76,3 +76,15 @@ env_var_as_boolean(const char *var_name, bool default_value)
>        return default_value;
>     }
>  }
> +
> +/**
> + * Reads an environment variable and interprets its value as a unsigned.
> + */
> +unsigned
> +env_var_as_unsigned(const char *var_name, unsigned default_value)
> +{
> +   char *str = getenv(var_name);
> +   if (str)
> +      return strtoul(str, NULL, 0);
> +   return default_value;

FOO=bar will return 0; I think returning `default_value` might be a more
predictable behaviour here for users. How about this?

   char *str = getenv(var_name);
   if (str) {
      char *end;
      unsigned long result;

      errno = 0;
      result = strtoul(str, &end, 0);
      if (errno == 0 && end != str && *end == '\0')
        return result;
   }
   return default_value;

The check added here is essentially that strtoul didn't set an error,
had something to read, and read all the way to the end of the string. If
those aren't true, the `default_value` is returned.

With that:
Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>

> +}
> diff --git a/src/util/debug.h b/src/util/debug.h
> index 75ebc2ebffb..2e34ebe3421 100644
> --- a/src/util/debug.h
> +++ b/src/util/debug.h
> @@ -41,6 +41,8 @@ parse_debug_string(const char *debug,
>                     const struct debug_control *control);
>  bool
>  env_var_as_boolean(const char *var_name, bool default_value);
> +unsigned
> +env_var_as_unsigned(const char *var_name, unsigned default_value);
>  
>  #ifdef __cplusplus
>  } /* extern C */
> -- 
> 2.19.1
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list