[PATCH v2 3/6] perf parse-events: Make term's config const

From: Ian Rogers
Date: Thu Aug 24 2023 - 22:41:23 EST


This avoids casts in tests. Use zfree in a few places to avoid
warnings about a freeing a const pointer.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/tests/pmu.c | 18 +++++++++---------
tools/perf/util/parse-events.c | 10 +++++-----
tools/perf/util/parse-events.h | 4 ++--
tools/perf/util/pmu.c | 2 +-
4 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/perf/tests/pmu.c b/tools/perf/tests/pmu.c
index c204ed1f1a8b..eb60e5f66859 100644
--- a/tools/perf/tests/pmu.c
+++ b/tools/perf/tests/pmu.c
@@ -28,55 +28,55 @@ static struct test_format {
/* Simulated users input. */
static struct parse_events_term test_terms[] = {
{
- .config = (char *) "krava01",
+ .config = "krava01",
.val.num = 15,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava02",
+ .config = "krava02",
.val.num = 170,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava03",
+ .config = "krava03",
.val.num = 1,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava11",
+ .config = "krava11",
.val.num = 27,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava12",
+ .config = "krava12",
.val.num = 1,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava13",
+ .config = "krava13",
.val.num = 2,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava21",
+ .config = "krava21",
.val.num = 119,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava22",
+ .config = "krava22",
.val.num = 11,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
},
{
- .config = (char *) "krava23",
+ .config = "krava23",
.val.num = 2,
.type_val = PARSE_EVENTS__TERM_TYPE_NUM,
.type_term = PARSE_EVENTS__TERM_TYPE_USER,
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 621bdd157a0f..4c812fbe0cf9 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -199,7 +199,7 @@ static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu)
continue;

if (perf_pmu__have_event(pmu, term->val.str)) {
- free(term->config);
+ zfree(&term->config);
term->config = term->val.str;
term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
@@ -208,7 +208,7 @@ static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu)
continue;
}

- free(term->config);
+ zfree(&term->config);
term->config = strdup("config");
errno = 0;
num = strtoull(term->val.str + 1, NULL, 16);
@@ -1416,7 +1416,7 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
struct perf_pmu *pmu = NULL;
YYLTYPE *loc = loc_;
int ok = 0;
- char *config;
+ const char *config;

*listp = NULL;

@@ -1435,7 +1435,7 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
PARSE_EVENTS__TERM_TYPE_USER,
config, 1, false, NULL,
NULL) < 0) {
- free(config);
+ zfree(&config);
goto out_err;
}
list_add_tail(&term->list, head);
@@ -2378,7 +2378,7 @@ static int new_term(struct parse_events_term **_term,
}

int parse_events_term__num(struct parse_events_term **term,
- int type_term, char *config, u64 num,
+ int type_term, const char *config, u64 num,
bool no_value,
void *loc_term_, void *loc_val_)
{
diff --git a/tools/perf/util/parse-events.h b/tools/perf/util/parse-events.h
index c4e8d8e94b28..6d75d853ce00 100644
--- a/tools/perf/util/parse-events.h
+++ b/tools/perf/util/parse-events.h
@@ -83,7 +83,7 @@ enum {
};

struct parse_events_term {
- char *config;
+ const char *config;
union {
char *str;
u64 num;
@@ -138,7 +138,7 @@ bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
void parse_events__shrink_config_terms(void);
int parse_events__is_hardcoded_term(struct parse_events_term *term);
int parse_events_term__num(struct parse_events_term **term,
- int type_term, char *config, u64 num,
+ int type_term, const char *config, u64 num,
bool novalue,
void *loc_term, void *loc_val);
int parse_events_term__str(struct parse_events_term **term,
diff --git a/tools/perf/util/pmu.c b/tools/perf/util/pmu.c
index a96ccc5c3b51..eb42f9830be7 100644
--- a/tools/perf/util/pmu.c
+++ b/tools/perf/util/pmu.c
@@ -1418,7 +1418,7 @@ static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu,
struct parse_events_term *term)
{
struct perf_pmu_alias *alias;
- char *name;
+ const char *name;

if (parse_events__is_hardcoded_term(term))
return NULL;
--
2.42.0.rc2.253.gd59a3bf2b4-goog