]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/commitdiff
perf tools: Introduce zfree
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 26 Dec 2013 20:41:15 +0000 (17:41 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 27 Dec 2013 18:17:00 +0000 (15:17 -0300)
For the frequent idiom of:

   free(ptr);
   ptr = NULL;

Make it expect a pointer to the pointer being freed, so that it becomes
clear at first sight that the variable being freed is being modified.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-pfw02ezuab37kha18wlut7ir@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
22 files changed:
tools/perf/arch/common.c
tools/perf/builtin-annotate.c
tools/perf/builtin-stat.c
tools/perf/builtin-timechart.c
tools/perf/builtin-trace.c
tools/perf/ui/browser.c
tools/perf/ui/browsers/hists.c
tools/perf/ui/gtk/util.c
tools/perf/util/alias.c
tools/perf/util/annotate.c
tools/perf/util/dso.c
tools/perf/util/evlist.c
tools/perf/util/evsel.c
tools/perf/util/header.c
tools/perf/util/help.c
tools/perf/util/machine.c
tools/perf/util/probe-event.c
tools/perf/util/probe-finder.c
tools/perf/util/symbol.c
tools/perf/util/thread_map.c
tools/perf/util/trace-event-info.c
tools/perf/util/util.h

index aacef07ebf31525227b372e6c40e9a3039c5d580..42faf369211c853f21f87cd037df5e9f4683b4f7 100644 (file)
@@ -154,8 +154,7 @@ static int perf_session_env__lookup_binutils_path(struct perf_session_env *env,
                }
                if (lookup_path(buf))
                        goto out;
-               free(buf);
-               buf = NULL;
+               zfree(&buf);
        }
 
        if (!strcmp(arch, "arm"))
index 4136f9970fd5395b1af96231a94380f45e4684ec..ab65057a03173985cc9603ff846d4df2812b7015 100644 (file)
@@ -180,8 +180,7 @@ find_next:
                         * symbol, free he->ms.sym->src to signal we already
                         * processed this symbol.
                         */
-                       free(notes->src);
-                       notes->src = NULL;
+                       zfree(&notes->src);
                }
        }
 }
index dab98b50c9feb800eac1652d4c66d2cf4c691c97..106a5e5b7842434a659a0e597019cdbb78ae7bbf 100644 (file)
@@ -185,8 +185,7 @@ static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
 
 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
 {
-       free(evsel->priv);
-       evsel->priv = NULL;
+       zfree(&evsel->priv);
 }
 
 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
@@ -208,8 +207,7 @@ static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel)
 
 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
 {
-       free(evsel->prev_raw_counts);
-       evsel->prev_raw_counts = NULL;
+       zfree(&evsel->prev_raw_counts);
 }
 
 static void perf_evlist__free_stats(struct perf_evlist *evlist)
index 20d4212fa33773ec88f150d027781374a0f50713..652af0b66a625305deac34b0f18ec2e5ccf1d8f0 100644 (file)
@@ -488,8 +488,7 @@ static const char *cat_backtrace(union perf_event *event,
                                 * It seems the callchain is corrupted.
                                 * Discard all.
                                 */
-                               free(p);
-                               p = NULL;
+                               zfree(&p);
                                goto exit;
                        }
                        continue;
index f64b5b0aa8b1650daecb2ecee7ba5cdbd726e5f3..c5b4bc51175cb63975af8cbb1ffc182f2a4f2e91 100644 (file)
@@ -146,8 +146,7 @@ static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel,
 
 static void perf_evsel__delete_priv(struct perf_evsel *evsel)
 {
-       free(evsel->priv);
-       evsel->priv = NULL;
+       zfree(&evsel->priv);
        perf_evsel__delete(evsel);
 }
 
@@ -165,8 +164,7 @@ static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler)
        return -ENOMEM;
 
 out_delete:
-       free(evsel->priv);
-       evsel->priv = NULL;
+       zfree(&evsel->priv);
        return -ENOENT;
 }
 
@@ -1278,10 +1276,8 @@ static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size,
        size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
        struct thread_trace *ttrace = arg->thread->priv;
 
-       if (ttrace && fd >= 0 && fd <= ttrace->paths.max) {
-               free(ttrace->paths.table[fd]);
-               ttrace->paths.table[fd] = NULL;
-       }
+       if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
+               zfree(&ttrace->paths.table[fd]);
 
        return printed;
 }
index 94223d404f4312d6f09a67f3143fa6930f9a0c53..d11541d4d7d7ffcddad8d093b2e79f697967292a 100644 (file)
@@ -256,8 +256,7 @@ int ui_browser__show(struct ui_browser *browser, const char *title,
        __ui_browser__show_title(browser, title);
 
        browser->title = title;
-       free(browser->helpline);
-       browser->helpline = NULL;
+       zfree(&browser->helpline);
 
        va_start(ap, helpline);
        err = vasprintf(&browser->helpline, helpline, ap);
@@ -272,8 +271,7 @@ void ui_browser__hide(struct ui_browser *browser)
 {
        pthread_mutex_lock(&ui__lock);
        ui_helpline__pop();
-       free(browser->helpline);
-       browser->helpline = NULL;
+       zfree(&browser->helpline);
        pthread_mutex_unlock(&ui__lock);
 }
 
index 0d9dd99507ee5030ae397e20b46e0f3609181222..022d1731b80128d594299e7e44eefb8ecf3c91e4 100644 (file)
@@ -1267,10 +1267,8 @@ static inline void free_popup_options(char **options, int n)
 {
        int i;
 
-       for (i = 0; i < n; ++i) {
-               free(options[i]);
-               options[i] = NULL;
-       }
+       for (i = 0; i < n; ++i)
+               zfree(&options[i]);
 }
 
 /* Check whether the browser is for 'top' or 'report' */
index 696c1fbe42482db03b4fb460b488860435a4edf5..52e7fc48af9f488088785b8e216e05f6b22546fc 100644 (file)
@@ -23,8 +23,7 @@ int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
        if (!perf_gtk__is_active_context(*ctx))
                return -1;
 
-       free(*ctx);
-       *ctx = NULL;
+       zfree(ctx);
        return 0;
 }
 
index e6d134773d0a498d2b62e18391d0de5c746dd149..c0b43ee40d95674fb5a5602c6f287ba2d8d8c5f5 100644 (file)
@@ -55,8 +55,7 @@ int split_cmdline(char *cmdline, const char ***argv)
                                src++;
                                c = cmdline[src];
                                if (!c) {
-                                       free(*argv);
-                                       *argv = NULL;
+                                       zfree(argv);
                                        return error("cmdline ends with \\");
                                }
                        }
@@ -68,8 +67,7 @@ int split_cmdline(char *cmdline, const char ***argv)
        cmdline[dst] = 0;
 
        if (quoted) {
-               free(*argv);
-               *argv = NULL;
+               zfree(argv);
                return error("unclosed quote");
        }
 
index 27ab7b59dbf439680d683f5b4473e1653caa1dae..a78721d14694eb51bae5763faac0cd6c570c7b13 100644 (file)
@@ -185,8 +185,7 @@ static int lock__parse(struct ins_operands *ops)
        return 0;
 
 out_free_ops:
-       free(ops->locked.ops);
-       ops->locked.ops = NULL;
+       zfree(&ops->locked.ops);
        return 0;
 }
 
@@ -256,8 +255,7 @@ static int mov__parse(struct ins_operands *ops)
        return 0;
 
 out_free_source:
-       free(ops->source.raw);
-       ops->source.raw = NULL;
+       zfree(&ops->source.raw);
        return -1;
 }
 
@@ -560,8 +558,7 @@ static int disasm_line__parse(char *line, char **namep, char **rawp)
        return 0;
 
 out_free_name:
-       free(*namep);
-       *namep = NULL;
+       zfree(namep);
        return -1;
 }
 
@@ -1113,8 +1110,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
                src_line = (void *)src_line + sizeof_src_line;
        }
 
-       free(notes->src->lines);
-       notes->src->lines = NULL;
+       zfree(&notes->src->lines);
 }
 
 /* Get the filename:line for the colored entries */
index 4ddeecb9ff85b4fb92b4851028b6b6c9026b90b8..4045d086d9d957823af08d79db4d8407fb9a460f 100644 (file)
@@ -497,21 +497,18 @@ void dso__delete(struct dso *dso)
                symbols__delete(&dso->symbols[i]);
 
        if (dso->short_name_allocated) {
-               free((char *)dso->short_name);
-               dso->short_name           = NULL;
+               zfree((char **)&dso->short_name);
                dso->short_name_allocated = false;
        }
 
        if (dso->long_name_allocated) {
-               free((char *)dso->long_name);
-               dso->long_name           = NULL;
+               zfree((char **)&dso->long_name);
                dso->long_name_allocated = false;
        }
 
        dso_cache__free(&dso->cache);
        dso__free_a2l(dso);
-       free(dso->symsrc_filename);
-       dso->symsrc_filename = NULL;
+       zfree(&dso->symsrc_filename);
        free(dso);
 }
 
index da318291498470d8e53041579c62b2954cf87630..b08a7ecdcea180da880b6a57a199e6c89c9bd728 100644 (file)
@@ -101,10 +101,8 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
 
 void perf_evlist__exit(struct perf_evlist *evlist)
 {
-       free(evlist->mmap);
-       free(evlist->pollfd);
-       evlist->mmap = NULL;
-       evlist->pollfd = NULL;
+       zfree(&evlist->mmap);
+       zfree(&evlist->pollfd);
 }
 
 void perf_evlist__delete(struct perf_evlist *evlist)
@@ -587,8 +585,7 @@ void perf_evlist__munmap(struct perf_evlist *evlist)
        for (i = 0; i < evlist->nr_mmaps; i++)
                __perf_evlist__munmap(evlist, i);
 
-       free(evlist->mmap);
-       evlist->mmap = NULL;
+       zfree(&evlist->mmap);
 }
 
 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
index 6874e0485693ebebb2e251587cfd36ff934c0212..93b6031d5459f244062d37ec7ebb01f093b3b313 100644 (file)
@@ -750,8 +750,7 @@ void perf_evsel__free_id(struct perf_evsel *evsel)
 {
        xyarray__delete(evsel->sample_id);
        evsel->sample_id = NULL;
-       free(evsel->id);
-       evsel->id = NULL;
+       zfree(&evsel->id);
 }
 
 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
@@ -1960,8 +1959,7 @@ bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
                evsel->attr.type   = PERF_TYPE_SOFTWARE;
                evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
 
-               free(evsel->name);
-               evsel->name = NULL;
+               zfree(&evsel->name);
                return true;
        }
 
index 10730b0af804422220972d174b100d8027fcd7cb..20f3a9c97bd886cecc2ddc40d4715f7e93f0d9b5 100644 (file)
@@ -1324,8 +1324,7 @@ read_event_desc(struct perf_header *ph, int fd)
                }
        }
 out:
-       if (buf)
-               free(buf);
+       free(buf);
        return events;
 error:
        if (events)
index 8b1f6e891b8a8f3857bbf98e97b7f1f9c84f9987..7b68978e50d24d98c7da185c606ca8e8b9be8a3a 100644 (file)
@@ -263,9 +263,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 
        for (i = 0; i < old->cnt; i++)
                cmds->names[cmds->cnt++] = old->names[i];
-       free(old->names);
+       zfree(&old->names);
        old->cnt = 0;
-       old->names = NULL;
 }
 
 const char *help_unknown_cmd(const char *cmd)
index c78cc84f433ea413d07e7697f92b0ac91b8801e1..a98538dc465a79ba9fbb077e1c43437f35f49f69 100644 (file)
@@ -102,8 +102,7 @@ void machine__exit(struct machine *machine)
        map_groups__exit(&machine->kmaps);
        dsos__delete(&machine->user_dsos);
        dsos__delete(&machine->kernel_dsos);
-       free(machine->root_dir);
-       machine->root_dir = NULL;
+       zfree(&machine->root_dir);
 }
 
 void machine__delete(struct machine *machine)
@@ -562,11 +561,10 @@ void machine__destroy_kernel_maps(struct machine *machine)
                         * on one of them.
                         */
                        if (type == MAP__FUNCTION) {
-                               free((char *)kmap->ref_reloc_sym->name);
-                               kmap->ref_reloc_sym->name = NULL;
-                               free(kmap->ref_reloc_sym);
-                       }
-                       kmap->ref_reloc_sym = NULL;
+                               zfree((char **)&kmap->ref_reloc_sym->name);
+                               zfree(&kmap->ref_reloc_sym);
+                       } else
+                               kmap->ref_reloc_sym = NULL;
                }
 
                map__delete(machine->vmlinux_maps[type]);
index 095a98ec7444aa183d820c606639c272c66c79a8..4d3cd1a0278a1eb938d19ce470cbe52ad5574002 100644 (file)
@@ -506,15 +506,13 @@ static int get_real_path(const char *raw_path, const char *comp_dir,
                case EFAULT:
                        raw_path = strchr(++raw_path, '/');
                        if (!raw_path) {
-                               free(*new_path);
-                               *new_path = NULL;
+                               zfree(new_path);
                                return -ENOENT;
                        }
                        continue;
 
                default:
-                       free(*new_path);
-                       *new_path = NULL;
+                       zfree(new_path);
                        return -errno;
                }
        }
index 8c087359b7cefb5a4cb15a981be012aa194416b8..6d8796e38d7f2ff6b333b29291bba3062719fe5d 100644 (file)
@@ -226,10 +226,8 @@ struct debuginfo *debuginfo__new(const char *path)
        if (!dbg)
                return NULL;
 
-       if (debuginfo__init_offline_dwarf(dbg, path) < 0) {
-               free(dbg);
-               dbg = NULL;
-       }
+       if (debuginfo__init_offline_dwarf(dbg, path) < 0)
+               zfree(&dbg);
 
        return dbg;
 }
@@ -241,10 +239,8 @@ struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
        if (!dbg)
                return NULL;
 
-       if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0) {
-               free(dbg);
-               dbg = NULL;
-       }
+       if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0)
+               zfree(&dbg);
 
        return dbg;
 }
@@ -1302,8 +1298,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
 
        ret = debuginfo__find_probes(dbg, &tf.pf);
        if (ret < 0) {
-               free(*tevs);
-               *tevs = NULL;
+               zfree(tevs);
                return ret;
        }
 
@@ -1417,8 +1412,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
                        free(af.vls[af.nvls].point.symbol);
                        strlist__delete(af.vls[af.nvls].vars);
                }
-               free(af.vls);
-               *vls = NULL;
+               zfree(vls);
                return ret;
        }
 
@@ -1522,8 +1516,7 @@ post:
        if (fname) {
                ppt->file = strdup(fname);
                if (ppt->file == NULL) {
-                       free(ppt->function);
-                       ppt->function = NULL;
+                       zfree(&ppt->function);
                        ret = -ENOMEM;
                        goto end;
                }
@@ -1577,8 +1570,7 @@ static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
                else
                        ret = 0;        /* Lines are not found */
        else {
-               free(lf->lr->path);
-               lf->lr->path = NULL;
+               zfree(&lf->lr->path);
        }
        return ret;
 }
index 923d00040bbfd21eee501ac000a669b08da6a484..fd9e1a4fad1669038fe0568c38b417b459afc71f 100644 (file)
@@ -1621,13 +1621,10 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
 
 static void vmlinux_path__exit(void)
 {
-       while (--vmlinux_path__nr_entries >= 0) {
-               free(vmlinux_path[vmlinux_path__nr_entries]);
-               vmlinux_path[vmlinux_path__nr_entries] = NULL;
-       }
+       while (--vmlinux_path__nr_entries >= 0)
+               zfree(&vmlinux_path[vmlinux_path__nr_entries]);
 
-       free(vmlinux_path);
-       vmlinux_path = NULL;
+       zfree(&vmlinux_path);
 }
 
 static int vmlinux_path__init(void)
index 9b5f856cc28096b865d1c34c12073a23e5eb5a9c..cf44644a4058fd8a8bac0fedfbc8dd0207523d89 100644 (file)
@@ -9,6 +9,7 @@
 #include "strlist.h"
 #include <string.h>
 #include "thread_map.h"
+#include "util.h"
 
 /* Skip "." and ".." directories */
 static int filter(const struct dirent *dir)
@@ -138,8 +139,7 @@ out_free_namelist:
        free(namelist);
 
 out_free_closedir:
-       free(threads);
-       threads = NULL;
+       zfree(&threads);
        goto out_closedir;
 }
 
@@ -210,8 +210,7 @@ out_free_namelist:
        free(namelist);
 
 out_free_threads:
-       free(threads);
-       threads = NULL;
+       zfree(&threads);
        goto out;
 }
 
@@ -262,8 +261,7 @@ out:
        return threads;
 
 out_free_threads:
-       free(threads);
-       threads = NULL;
+       zfree(&threads);
        goto out;
 }
 
index c354b95a234864845a602081225e98381df69a77..9f73bf43862c5e73d4f22b519b25ef7156f97576 100644 (file)
@@ -562,10 +562,8 @@ out:
                output_fd = fd;
        }
 
-       if (err) {
-               free(tdata);
-               tdata = NULL;
-       }
+       if (err)
+               zfree(&tdata);
 
        put_tracepoints_path(tps);
        return tdata;
index 9a2c268ad718a5e6c25ad74255fae6ec791f1829..6995d66f225c786a7a90ee3e6884f628474f7cec 100644 (file)
@@ -186,6 +186,8 @@ static inline void *zalloc(size_t size)
        return calloc(1, size);
 }
 
+#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
+
 static inline int has_extension(const char *filename, const char *ext)
 {
        size_t len = strlen(filename);