util: Add function debug_get_option_cached os_get_option_cached

This is used to fixes DEBUG_GET_ONCE_*_OPTION macros latter

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>
This commit is contained in:
Yonggang Luo
2022-11-06 04:26:38 +08:00
committed by Marge Bot
parent 397a5c1b2e
commit cdad035cfd
4 changed files with 77 additions and 1 deletions

View File

@@ -26,9 +26,12 @@
**************************************************************************/ **************************************************************************/
#include "hash_table.h"
#include "macros.h"
#include "os_misc.h" #include "os_misc.h"
#include "os_file.h" #include "os_file.h"
#include "macros.h" #include "ralloc.h"
#include "simple_mtx.h"
#include <stdarg.h> #include <stdarg.h>
@@ -182,6 +185,49 @@ os_get_option(const char *name)
return opt; return opt;
} }
static struct hash_table *options_tbl;
static simple_mtx_t options_tbl_mtx = SIMPLE_MTX_INITIALIZER;
/**
* NOTE: The strings that allocated with ralloc_strdup(options_tbl, ...)
* are freed by _mesa_hash_table_destroy automatically
*/
static void
options_tbl_fini(void)
{
_mesa_hash_table_destroy(options_tbl, NULL);
}
const char *
os_get_option_cached(const char *name)
{
char *opt = NULL;
simple_mtx_lock(&options_tbl_mtx);
if (!options_tbl) {
options_tbl = _mesa_hash_table_create(NULL, _mesa_hash_string,
_mesa_key_string_equal);
if (options_tbl == NULL) {
goto exit_mutex;
}
atexit(options_tbl_fini);
}
struct hash_entry *entry = _mesa_hash_table_search(options_tbl, name);
if (entry) {
opt = entry->data;
goto exit_mutex;
}
char *name_dup = ralloc_strdup(options_tbl, name);
if (name_dup == NULL) {
goto exit_mutex;
}
opt = ralloc_strdup(options_tbl, os_get_option(name));
_mesa_hash_table_insert(options_tbl, name_dup, (void *)opt);
exit_mutex:
simple_mtx_unlock(&options_tbl_mtx);
return opt;
}
/** /**
* Return the size of the total physical memory. * Return the size of the total physical memory.
* \param size returns the size of the total physical memory * \param size returns the size of the total physical memory

View File

@@ -90,6 +90,16 @@ os_log_message(const char *message);
const char * const char *
os_get_option(const char *name); os_get_option(const char *name);
/*
* Get an option. Should return NULL if specified option is not set.
* It's will save the option into hash table for the first time, and
* for latter calling, it's will return the value comes from hash table
* directly, and the returned value will always be valid before program exit
* The disadvantage is that setenv, unsetenv, putenv won't take effect
* after this function is called
*/
const char *
os_get_option_cached(const char *name);
/* /*
* Get the total amount of physical memory available on the system. * Get the total amount of physical memory available on the system.

View File

@@ -166,6 +166,23 @@ debug_get_option(const char *name, const char *dfault)
} }
const char *
debug_get_option_cached(const char *name, const char *dfault)
{
const char *result;
result = os_get_option_cached(name);
if (!result)
result = dfault;
if (debug_get_option_should_print())
debug_printf("%s: %s = %s\n", __FUNCTION__, name,
result ? result : "(null)");
return result;
}
/** /**
* Reads an environment variable and interprets its value as a boolean. * Reads an environment variable and interprets its value as a boolean.
* Recognizes 0/n/no/f/false case insensitive as false. * Recognizes 0/n/no/f/false case insensitive as false.

View File

@@ -355,6 +355,9 @@ comma_separated_list_contains(const char *list, const char *s);
const char * const char *
debug_get_option(const char *name, const char *dfault); debug_get_option(const char *name, const char *dfault);
const char *
debug_get_option_cached(const char *name, const char *dfault);
bool bool
debug_get_bool_option(const char *name, bool dfault); debug_get_bool_option(const char *name, bool dfault);