From 236e846fae12e6e70ab3800aad70877bb61dd4cf Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 18 Aug 2016 17:07:48 -0400 Subject: [PATCH 32/44] FIXME: input.c: introduce class sources_cache --- gcc/input.c | 81 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 24 deletions(-) diff --git a/gcc/input.c b/gcc/input.c index 9b26378..96580cc 100644 --- a/gcc/input.c +++ b/gcc/input.c @@ -126,8 +126,28 @@ struct line_maps *line_table; struct line_maps *saved_line_table; -static fcache *fcache_tab; -static const size_t fcache_tab_size = 16; +/* FIXME. */ + +class sources_cache +{ + public: + fcache *lookup_or_add_file (const char *file_path); + void forcibly_evict_file (const char *file_path); + + private: + fcache *evicted_cache_tab_entry (unsigned *highest_use_count); + fcache *add_file (const char *file_path); + fcache *lookup_file (const char *file_path); + + private: + static const size_t fcache_tab_size = 16; + fcache fcache_tab[fcache_tab_size]; +}; + +/* FIXME. */ + +static sources_cache *g_sources_cache; + static const size_t fcache_buffer_size = 4 * 1024; static const size_t fcache_line_record_size = 100; @@ -195,8 +215,8 @@ expand_location_1 (source_location loc, static void diagnostic_file_cache_init (void) { - if (fcache_tab == NULL) - fcache_tab = new fcache[fcache_tab_size]; + if (g_sources_cache == NULL) + g_sources_cache = new sources_cache (); } /* Free the resources used by the set of cache used for files accessed @@ -205,10 +225,10 @@ diagnostic_file_cache_init (void) void diagnostic_file_cache_fini (void) { - if (fcache_tab) + if (g_sources_cache) { - delete [] (fcache_tab); - fcache_tab = NULL; + delete g_sources_cache; + g_sources_cache = NULL; } } @@ -235,13 +255,10 @@ total_lines_num (const char *file_path) caret diagnostic. Return the found cached file, or NULL if no cached file was found. */ -static fcache* -lookup_file_in_cache_tab (const char *file_path) +fcache * +sources_cache::lookup_file (const char *file_path) { - if (file_path == NULL) - return NULL; - - diagnostic_file_cache_init (); + gcc_assert (file_path); /* This will contain the found cached file. */ fcache *r = NULL; @@ -270,7 +287,18 @@ diagnostics_file_cache_forcibly_evict_file (const char *file_path) { gcc_assert (file_path); - fcache *r = lookup_file_in_cache_tab (file_path); + if (!g_sources_cache) + return; + + g_sources_cache->forcibly_evict_file (file_path); +} + +void +sources_cache::forcibly_evict_file (const char *file_path) +{ + gcc_assert (file_path); + + fcache *r = lookup_file (file_path); if (!r) /* Not found. */ return; @@ -293,8 +321,8 @@ diagnostics_file_cache_forcibly_evict_file (const char *file_path) *HIGHEST_USE_COUNT is set to the highest use count of the entries in the cache table. */ -static fcache* -evicted_cache_tab_entry (unsigned *highest_use_count) +fcache* +sources_cache::evicted_cache_tab_entry (unsigned *highest_use_count) { diagnostic_file_cache_init (); @@ -332,8 +360,8 @@ evicted_cache_tab_entry (unsigned *highest_use_count) function returns the created cache. Note that only the last fcache_tab_size files are cached. */ -static fcache* -add_file_to_cache_tab (const char *file_path) +fcache* +sources_cache::add_file (const char *file_path) { FILE *fp = fopen (file_path, "r"); @@ -364,12 +392,12 @@ add_file_to_cache_tab (const char *file_path) for this file, add it to the array of cached file and return it. */ -static fcache* -lookup_or_add_file_to_cache_tab (const char *file_path) +fcache* +sources_cache::lookup_or_add_file (const char *file_path) { - fcache *r = lookup_file_in_cache_tab (file_path); + fcache *r = lookup_file (file_path); if (r == NULL) - r = add_file_to_cache_tab (file_path); + r = add_file (file_path); return r; } @@ -754,7 +782,12 @@ location_get_source_line (const char *file_path, int line, if (line == 0) return NULL; - fcache *c = lookup_or_add_file_to_cache_tab (file_path); + if (file_path == NULL) + return NULL; + + diagnostic_file_cache_init (); + + fcache *c = g_sources_cache->lookup_or_add_file (file_path); if (c == NULL) return NULL; @@ -773,7 +806,7 @@ location_get_source_line (const char *file_path, int line, bool location_missing_trailing_newline (const char *file_path) { - fcache *c = lookup_or_add_file_to_cache_tab (file_path); + fcache *c = g_sources_cache->lookup_or_add_file (file_path); if (c == NULL) return false; -- 1.8.5.3