perf tools: Factor out copy_config_terms() and free_config_terms()
authorAdrian Hunter <adrian.hunter@intel.com>
Thu, 9 Sep 2021 12:55:07 +0000 (15:55 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Sat, 11 Sep 2021 19:00:13 +0000 (16:00 -0300)
Factor out copy_config_terms() and free_config_terms() so that they can
be reused.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Link: https //lore.kernel.org/r/20210909125508.28693-2-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/evsel.c
tools/perf/util/evsel.h
tools/perf/util/parse-events.c

index 54d251327b5ba04488b2c3733f1a17de7988333f..dbfeceb2546c6ef164a14a755753c2de382f13e0 100644 (file)
@@ -333,11 +333,11 @@ error_free:
        goto out;
 }
 
-static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
+int copy_config_terms(struct list_head *dst, struct list_head *src)
 {
        struct evsel_config_term *pos, *tmp;
 
-       list_for_each_entry(pos, &src->config_terms, list) {
+       list_for_each_entry(pos, src, list) {
                tmp = malloc(sizeof(*tmp));
                if (tmp == NULL)
                        return -ENOMEM;
@@ -350,11 +350,16 @@ static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
                                return -ENOMEM;
                        }
                }
-               list_add_tail(&tmp->list, &dst->config_terms);
+               list_add_tail(&tmp->list, dst);
        }
        return 0;
 }
 
+static int evsel__copy_config_terms(struct evsel *dst, struct evsel *src)
+{
+       return copy_config_terms(&dst->config_terms, &src->config_terms);
+}
+
 /**
  * evsel__clone - create a new evsel copied from @orig
  * @orig: original evsel
@@ -1385,11 +1390,11 @@ int evsel__disable(struct evsel *evsel)
        return err;
 }
 
-static void evsel__free_config_terms(struct evsel *evsel)
+void free_config_terms(struct list_head *config_terms)
 {
        struct evsel_config_term *term, *h;
 
-       list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
+       list_for_each_entry_safe(term, h, config_terms, list) {
                list_del_init(&term->list);
                if (term->free_str)
                        zfree(&term->val.str);
@@ -1397,6 +1402,11 @@ static void evsel__free_config_terms(struct evsel *evsel)
        }
 }
 
+static void evsel__free_config_terms(struct evsel *evsel)
+{
+       free_config_terms(&evsel->config_terms);
+}
+
 void evsel__exit(struct evsel *evsel)
 {
        assert(list_empty(&evsel->core.node));
index 1b3eeab5f1887be0458e6d0c783e280b235840c5..1f7edfa8568a6156d4f96f9ea76d045805d6bdb6 100644 (file)
@@ -213,6 +213,9 @@ static inline struct evsel *evsel__new(struct perf_event_attr *attr)
 struct evsel *evsel__clone(struct evsel *orig);
 struct evsel *evsel__newtp_idx(const char *sys, const char *name, int idx);
 
+int copy_config_terms(struct list_head *dst, struct list_head *src);
+void free_config_terms(struct list_head *config_terms);
+
 /*
  * Returns pointer with encoded error via <linux/err.h> interface.
  */
index e5eae23cfceba746ec67a594b0b6dfb239777da3..ded5808798f96eb3da6d5ae5d34fbdac32cd9ebf 100644 (file)
@@ -1608,14 +1608,7 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
        }
 
        if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
-               struct evsel_config_term *pos, *tmp;
-
-               list_for_each_entry_safe(pos, tmp, &config_terms, list) {
-                       list_del_init(&pos->list);
-                       if (pos->free_str)
-                               zfree(&pos->val.str);
-                       free(pos);
-               }
+               free_config_terms(&config_terms);
                return -EINVAL;
        }