From: Nick Alcock Date: Mon, 25 Mar 2013 16:45:52 +0000 (+0000) Subject: ctf: split the absolute-file-name caching machinery out of type_id() X-Git-Tag: v4.1.12-92~313^2~86 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=870b7cdd3bd3109fa0a74ea4983ea510f1728a74;p=users%2Fjedix%2Flinux-maple.git ctf: split the absolute-file-name caching machinery out of type_id() We will be needing similar caching elsewhere in a forthcoming commit. Signed-off-by: Nick Alcock --- diff --git a/scripts/dwarf2ctf/dwarf2ctf.c b/scripts/dwarf2ctf/dwarf2ctf.c index beca89222abf..224280dc1022 100644 --- a/scripts/dwarf2ctf/dwarf2ctf.c +++ b/scripts/dwarf2ctf/dwarf2ctf.c @@ -608,6 +608,11 @@ static char *xstrdup(const char *s) __attribute__((__nonnull__, */ static char *fn_to_module(const char *file_name); +/* + * Determine, and cache, absolute filenames. + */ +static const char *abs_file_name(const char *file_name); + /* * Stub libdwfl callback, use only the ELF handle passed in. */ @@ -1138,40 +1143,19 @@ static char *type_id(Dwarf_Die *die, void (*fun)(Dwarf_Die *die, id = type_id(private_dwarf_type(die, &type_die), fun, data); /* - * Location information. Here in particular we value speed over - * clarity, caching the result of realpath() to minimize system - * calls, and going to some length to keep the number of calls - * to str_append*() down. + * Location information. We use cached realpath() results, and + * call str_appendn() only once, minimizing the number of + * strlen()s. */ if (id == NULL) { - static GHashTable *abs_file_names; - - if (abs_file_names == NULL) - abs_file_names = g_hash_table_new_full(g_str_hash, - g_str_equal, - free, free); - const char *decl_file_name = dwarf_decl_file(die); int decl_line_num; const char *fname = ""; - char line_num[21] = ""; /* bigger than 2^64's digit count */ + char line_num[21] = ""; /* bigger than 2^64's digit count */ no_type_id = 1; if (decl_file_name != NULL) { - - fname = g_hash_table_lookup(abs_file_names, - decl_file_name); - - if (fname == NULL) { - char abspath[PATH_MAX] = ""; - if (realpath(decl_file_name, abspath) == NULL) - strcpy(abspath, decl_file_name); - g_hash_table_replace(abs_file_names, - xstrdup(decl_file_name), - xstrdup(abspath)); - fname = g_hash_table_lookup(abs_file_names, - decl_file_name); - } + fname = abs_file_name(decl_file_name); } if (dwarf_decl_line(die, &decl_line_num) >= 0) { @@ -3436,6 +3420,35 @@ static char *fn_to_module(const char *file_name) return module_name; } +/* + * Determine, and cache, absolute filenames. This is called in very hot + * paths, notably type_id(), and must be kept fast. + */ +static const char *abs_file_name(const char *file_name) +{ + static GHashTable *abs_file_names; + const char *abs_name; + + if (abs_file_names == NULL) + abs_file_names = g_hash_table_new_full(g_str_hash, g_str_equal, + free, free); + + abs_name = g_hash_table_lookup(abs_file_names, file_name); + + if (abs_name == NULL) { + char abspath[PATH_MAX] = ""; + + if (realpath(file_name, abspath) == NULL) + strcpy(abspath, file_name); + g_hash_table_replace(abs_file_names, + xstrdup(file_name), xstrdup(abspath)); + + abs_name = g_hash_table_lookup(abs_file_names, file_name); + } + + return abs_name; +} + /* * Given a type encoding table, and a size, return the CTF encoding for that * type, or 0 if none.