From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 32850 invoked by alias); 26 Oct 2017 08:12:48 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 32757 invoked by uid 89); 26 Oct 2017 08:12:47 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_SOFTFAIL autolearn=ham version=3.3.2 spammy=Reverse X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (208.118.235.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 26 Oct 2017 08:12:44 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e7dHN-0004tj-Cl for gcc-patches@gcc.gnu.org; Thu, 26 Oct 2017 04:12:43 -0400 Received: from mx2.suse.de ([195.135.220.15]:55237) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e7dHN-0004t8-2y for gcc-patches@gcc.gnu.org; Thu, 26 Oct 2017 04:12:41 -0400 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 37098AD95 for ; Thu, 26 Oct 2017 08:12:37 +0000 (UTC) Resent-From: =?UTF-8?Q?Martin_Li=c5=a1ka?= Resent-To: GCC Patches Resent-Date: Thu, 26 Oct 2017 10:12:36 +0200 Resent-Message-ID: <977afdf1-0650-a437-cb21-2af59cb2e30e@suse.cz> Resent-User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 Message-Id: In-Reply-To: References: From: marxin Date: Thu, 26 Oct 2017 08:12:00 -0000 Subject: [PATCH 5/7] GCOV: std::vector refactoring. To: gcc-patches@gcc.gnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 195.135.220.15 X-IsSubscribed: yes X-SW-Source: 2017-10/txt/msg01891.txt.bz2 gcc/ChangeLog: 2017-10-26 Martin Liska * gcov.c (struct source_info): Remove typedef. (source_info::source_info): Add proper ctor. (accumulate_line_counts): Use struct, not it's typedef. (output_gcov_file): Likewise. (output_lines): Likewise. (main): Do not allocate an array. (output_intermediate_file): Use size of vector container. (process_file): Resize the vector. (generate_results): Do not preallocate, use newly added vector lines. (release_structures): Do not release sources. (find_source): Use vector methods. (add_line_counts): Do not use typedef. --- gcc/gcov.c | 89 +++++++++++++++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/gcc/gcov.c b/gcc/gcov.c index 8ba63f002d8..e2d33edb984 100644 --- a/gcc/gcov.c +++ b/gcc/gcov.c @@ -272,22 +272,29 @@ line_t::has_block (block_t *needle) /* Describes a file mentioned in the block graph. Contains an array of line info. */ -typedef struct source_info +struct source_info { + /* Default constructor. */ + source_info (); + /* Canonical name of source file. */ char *name; time_t file_time; - /* Array of line information. */ - line_t *lines; - unsigned num_lines; + /* Vector of line information. */ + vector lines; coverage_t coverage; /* Functions in this source file. These are in ascending line number order. */ function_t *functions; -} source_t; +}; + +source_info::source_info (): name (NULL), file_time (), lines (), + coverage (), functions (NULL) +{ +} typedef struct name_map { @@ -300,9 +307,8 @@ typedef struct name_map static function_t *functions; static function_t **fn_end = &functions; -static source_t *sources; /* Array of source files */ -static unsigned n_sources; /* Number of sources */ -static unsigned a_sources; /* Allocated sources */ +/* Vector of source files. */ +static vector sources; static name_map_t *names; /* Mapping of file names to sources */ static unsigned n_names; /* Number of names */ @@ -448,10 +454,10 @@ static void add_line_counts (coverage_t *, function_t *); static void executed_summary (unsigned, unsigned); static void function_summary (const coverage_t *, const char *); static const char *format_gcov (gcov_type, gcov_type, int); -static void accumulate_line_counts (source_t *); -static void output_gcov_file (const char *, source_t *); +static void accumulate_line_counts (source_info *); +static void output_gcov_file (const char *, source_info *); static int output_branch_count (FILE *, int, const arc_t *); -static void output_lines (FILE *, const source_t *); +static void output_lines (FILE *, const source_info *); static char *make_gcov_file_name (const char *, const char *); static char *mangle_name (const char *, char *); static void release_structures (void); @@ -668,8 +674,6 @@ main (int argc, char **argv) a_names = 10; names = XNEWVEC (name_map_t, a_names); - a_sources = 10; - sources = XNEWVEC (source_t, a_sources); argno = process_args (argc, argv); if (optind == argc) @@ -868,7 +872,7 @@ included. Instead the intermediate format here outputs only a single file 'foo.cc.gcov' similar to the above example. */ static void -output_intermediate_file (FILE *gcov_file, source_t *src) +output_intermediate_file (FILE *gcov_file, source_info *src) { unsigned line_num; /* current line number. */ const line_t *line; /* current line info ptr. */ @@ -885,7 +889,7 @@ output_intermediate_file (FILE *gcov_file, source_t *src) } for (line_num = 1, line = &src->lines[line_num]; - line_num < src->num_lines; + line_num < src->lines.size (); line_num++, line++) { arc_t *arc; @@ -967,8 +971,8 @@ process_file (const char *file_name) { unsigned last_line = block->locations[i].lines.back () + 1; - if (last_line > sources[s].num_lines) - sources[s].num_lines = last_line; + if (last_line > sources[s].lines.size ()) + sources[s].lines.resize (last_line); } } } @@ -987,7 +991,7 @@ process_file (const char *file_name) } static void -output_gcov_file (const char *file_name, source_t *src) +output_gcov_file (const char *file_name, source_info *src) { char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name); @@ -1020,14 +1024,8 @@ output_gcov_file (const char *file_name, source_t *src) static void generate_results (const char *file_name) { - unsigned ix; - source_t *src; function_t *fn; - for (ix = n_sources, src = sources; ix--; src++) - if (src->num_lines) - src->lines = XCNEWVEC (line_t, src->num_lines); - for (fn = functions; fn; fn = fn->next) { coverage_t coverage; @@ -1052,8 +1050,10 @@ generate_results (const char *file_name) file_name = canonicalize_name (file_name); } - for (ix = n_sources, src = sources; ix--; src++) + for (vector::iterator it = sources.begin (); + it != sources.end (); it++) { + source_info *src = &(*it); if (flag_relative_only) { /* Ignore this source, if it is an absolute path (after @@ -1091,10 +1091,6 @@ release_structures (void) unsigned ix; function_t *fn; - for (ix = n_sources; ix--;) - free (sources[ix].lines); - free (sources); - for (ix = n_names; ix--;) free (names[ix].name); free (names); @@ -1239,25 +1235,15 @@ find_source (const char *file_name) if (!name_map) { /* Not found with canonical name, create a new source. */ - source_t *src; - - if (n_sources == a_sources) - { - a_sources *= 2; - src = XNEWVEC (source_t, a_sources); - memcpy (src, sources, n_sources * sizeof (*sources)); - free (sources); - sources = src; - } - - idx = n_sources; + source_info *src; + idx = sources.size (); name_map = &names[n_names++]; name_map->name = canon; name_map->src = idx; - src = &sources[n_sources++]; - memset (src, 0, sizeof (*src)); + sources.push_back (source_info ()); + src = &sources.back (); src->name = canon; src->coverage.name = src->name; if (source_length @@ -2293,7 +2279,7 @@ add_line_counts (coverage_t *coverage, function_t *fn) fn->blocks_executed++; for (unsigned i = 0; i < block->locations.size (); i++) { - const source_t *src = &sources[block->locations[i].source_file_idx]; + source_info *src = &sources[block->locations[i].source_file_idx]; vector &lines = block->locations[i].lines; for (unsigned j = 0; j < lines.size (); j++) @@ -2349,11 +2335,10 @@ add_line_counts (coverage_t *coverage, function_t *fn) /* Accumulate the line counts of a file. */ static void -accumulate_line_counts (source_t *src) +accumulate_line_counts (source_info *src) { - line_t *line; function_t *fn, *fn_p, *fn_n; - unsigned ix; + unsigned ix = 0; /* Reverse the function order. */ for (fn = src->functions, fn_p = NULL; fn; fn_p = fn, fn = fn_n) @@ -2363,8 +2348,10 @@ accumulate_line_counts (source_t *src) } src->functions = fn_p; - for (ix = src->num_lines, line = src->lines; ix--; line++) + for (vector::reverse_iterator it = src->lines.rbegin (); + it != src->lines.rend (); it++) { + line_t *line = &(*it); if (line->blocks) { /* The user expects the line count to be the number of times @@ -2417,6 +2404,8 @@ accumulate_line_counts (source_t *src) if (line->count) src->coverage.lines_executed++; } + + ix++; } } @@ -2578,7 +2567,7 @@ output_line_beginning (FILE *f, bool exists, bool unexceptional, information. */ static void -output_lines (FILE *gcov_file, const source_t *src) +output_lines (FILE *gcov_file, const source_info *src) { #define DEFAULT_LINE_START " -: 0:" @@ -2611,7 +2600,7 @@ output_lines (FILE *gcov_file, const source_t *src) fn = src->functions; for (line_num = 1, line = &src->lines[line_num]; - line_num < src->num_lines; line_num++, line++) + line_num < src->lines.size (); line_num++, line++) { for (; fn && fn->line == line_num; fn = fn->next_file_fn) { -- 2.14.2