perf tools: Introduce zfree

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.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent f5385650
...@@ -154,8 +154,7 @@ static int perf_session_env__lookup_binutils_path(struct perf_session_env *env, ...@@ -154,8 +154,7 @@ static int perf_session_env__lookup_binutils_path(struct perf_session_env *env,
} }
if (lookup_path(buf)) if (lookup_path(buf))
goto out; goto out;
free(buf); zfree(&buf);
buf = NULL;
} }
if (!strcmp(arch, "arm")) if (!strcmp(arch, "arm"))
......
...@@ -180,8 +180,7 @@ static void hists__find_annotations(struct hists *hists, ...@@ -180,8 +180,7 @@ static void hists__find_annotations(struct hists *hists,
* symbol, free he->ms.sym->src to signal we already * symbol, free he->ms.sym->src to signal we already
* processed this symbol. * processed this symbol.
*/ */
free(notes->src); zfree(&notes->src);
notes->src = NULL;
} }
} }
} }
......
...@@ -185,8 +185,7 @@ static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel) ...@@ -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) static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
{ {
free(evsel->priv); zfree(&evsel->priv);
evsel->priv = NULL;
} }
static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel) 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) ...@@ -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) static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel)
{ {
free(evsel->prev_raw_counts); zfree(&evsel->prev_raw_counts);
evsel->prev_raw_counts = NULL;
} }
static void perf_evlist__free_stats(struct perf_evlist *evlist) static void perf_evlist__free_stats(struct perf_evlist *evlist)
......
...@@ -488,8 +488,7 @@ static const char *cat_backtrace(union perf_event *event, ...@@ -488,8 +488,7 @@ static const char *cat_backtrace(union perf_event *event,
* It seems the callchain is corrupted. * It seems the callchain is corrupted.
* Discard all. * Discard all.
*/ */
free(p); zfree(&p);
p = NULL;
goto exit; goto exit;
} }
continue; continue;
......
...@@ -146,8 +146,7 @@ static int perf_evsel__init_tp_ptr_field(struct perf_evsel *evsel, ...@@ -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) static void perf_evsel__delete_priv(struct perf_evsel *evsel)
{ {
free(evsel->priv); zfree(&evsel->priv);
evsel->priv = NULL;
perf_evsel__delete(evsel); perf_evsel__delete(evsel);
} }
...@@ -165,8 +164,7 @@ static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler) ...@@ -165,8 +164,7 @@ static int perf_evsel__init_syscall_tp(struct perf_evsel *evsel, void *handler)
return -ENOMEM; return -ENOMEM;
out_delete: out_delete:
free(evsel->priv); zfree(&evsel->priv);
evsel->priv = NULL;
return -ENOENT; return -ENOENT;
} }
...@@ -1278,10 +1276,8 @@ static size_t syscall_arg__scnprintf_close_fd(char *bf, size_t size, ...@@ -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); size_t printed = syscall_arg__scnprintf_fd(bf, size, arg);
struct thread_trace *ttrace = arg->thread->priv; struct thread_trace *ttrace = arg->thread->priv;
if (ttrace && fd >= 0 && fd <= ttrace->paths.max) { if (ttrace && fd >= 0 && fd <= ttrace->paths.max)
free(ttrace->paths.table[fd]); zfree(&ttrace->paths.table[fd]);
ttrace->paths.table[fd] = NULL;
}
return printed; return printed;
} }
......
...@@ -256,8 +256,7 @@ int ui_browser__show(struct ui_browser *browser, const char *title, ...@@ -256,8 +256,7 @@ int ui_browser__show(struct ui_browser *browser, const char *title,
__ui_browser__show_title(browser, title); __ui_browser__show_title(browser, title);
browser->title = title; browser->title = title;
free(browser->helpline); zfree(&browser->helpline);
browser->helpline = NULL;
va_start(ap, helpline); va_start(ap, helpline);
err = vasprintf(&browser->helpline, helpline, ap); err = vasprintf(&browser->helpline, helpline, ap);
...@@ -272,8 +271,7 @@ void ui_browser__hide(struct ui_browser *browser) ...@@ -272,8 +271,7 @@ void ui_browser__hide(struct ui_browser *browser)
{ {
pthread_mutex_lock(&ui__lock); pthread_mutex_lock(&ui__lock);
ui_helpline__pop(); ui_helpline__pop();
free(browser->helpline); zfree(&browser->helpline);
browser->helpline = NULL;
pthread_mutex_unlock(&ui__lock); pthread_mutex_unlock(&ui__lock);
} }
......
...@@ -1267,10 +1267,8 @@ static inline void free_popup_options(char **options, int n) ...@@ -1267,10 +1267,8 @@ static inline void free_popup_options(char **options, int n)
{ {
int i; int i;
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i)
free(options[i]); zfree(&options[i]);
options[i] = NULL;
}
} }
/* Check whether the browser is for 'top' or 'report' */ /* Check whether the browser is for 'top' or 'report' */
......
...@@ -23,8 +23,7 @@ int perf_gtk__deactivate_context(struct perf_gtk_context **ctx) ...@@ -23,8 +23,7 @@ int perf_gtk__deactivate_context(struct perf_gtk_context **ctx)
if (!perf_gtk__is_active_context(*ctx)) if (!perf_gtk__is_active_context(*ctx))
return -1; return -1;
free(*ctx); zfree(ctx);
*ctx = NULL;
return 0; return 0;
} }
......
...@@ -55,8 +55,7 @@ int split_cmdline(char *cmdline, const char ***argv) ...@@ -55,8 +55,7 @@ int split_cmdline(char *cmdline, const char ***argv)
src++; src++;
c = cmdline[src]; c = cmdline[src];
if (!c) { if (!c) {
free(*argv); zfree(argv);
*argv = NULL;
return error("cmdline ends with \\"); return error("cmdline ends with \\");
} }
} }
...@@ -68,8 +67,7 @@ int split_cmdline(char *cmdline, const char ***argv) ...@@ -68,8 +67,7 @@ int split_cmdline(char *cmdline, const char ***argv)
cmdline[dst] = 0; cmdline[dst] = 0;
if (quoted) { if (quoted) {
free(*argv); zfree(argv);
*argv = NULL;
return error("unclosed quote"); return error("unclosed quote");
} }
......
...@@ -185,8 +185,7 @@ static int lock__parse(struct ins_operands *ops) ...@@ -185,8 +185,7 @@ static int lock__parse(struct ins_operands *ops)
return 0; return 0;
out_free_ops: out_free_ops:
free(ops->locked.ops); zfree(&ops->locked.ops);
ops->locked.ops = NULL;
return 0; return 0;
} }
...@@ -256,8 +255,7 @@ static int mov__parse(struct ins_operands *ops) ...@@ -256,8 +255,7 @@ static int mov__parse(struct ins_operands *ops)
return 0; return 0;
out_free_source: out_free_source:
free(ops->source.raw); zfree(&ops->source.raw);
ops->source.raw = NULL;
return -1; return -1;
} }
...@@ -560,8 +558,7 @@ static int disasm_line__parse(char *line, char **namep, char **rawp) ...@@ -560,8 +558,7 @@ static int disasm_line__parse(char *line, char **namep, char **rawp)
return 0; return 0;
out_free_name: out_free_name:
free(*namep); zfree(namep);
*namep = NULL;
return -1; return -1;
} }
...@@ -1113,8 +1110,7 @@ static void symbol__free_source_line(struct symbol *sym, int len) ...@@ -1113,8 +1110,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
src_line = (void *)src_line + sizeof_src_line; src_line = (void *)src_line + sizeof_src_line;
} }
free(notes->src->lines); zfree(&notes->src->lines);
notes->src->lines = NULL;
} }
/* Get the filename:line for the colored entries */ /* Get the filename:line for the colored entries */
......
...@@ -497,21 +497,18 @@ void dso__delete(struct dso *dso) ...@@ -497,21 +497,18 @@ void dso__delete(struct dso *dso)
symbols__delete(&dso->symbols[i]); symbols__delete(&dso->symbols[i]);
if (dso->short_name_allocated) { if (dso->short_name_allocated) {
free((char *)dso->short_name); zfree((char **)&dso->short_name);
dso->short_name = NULL;
dso->short_name_allocated = false; dso->short_name_allocated = false;
} }
if (dso->long_name_allocated) { if (dso->long_name_allocated) {
free((char *)dso->long_name); zfree((char **)&dso->long_name);
dso->long_name = NULL;
dso->long_name_allocated = false; dso->long_name_allocated = false;
} }
dso_cache__free(&dso->cache); dso_cache__free(&dso->cache);
dso__free_a2l(dso); dso__free_a2l(dso);
free(dso->symsrc_filename); zfree(&dso->symsrc_filename);
dso->symsrc_filename = NULL;
free(dso); free(dso);
} }
......
...@@ -101,10 +101,8 @@ static void perf_evlist__purge(struct perf_evlist *evlist) ...@@ -101,10 +101,8 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
void perf_evlist__exit(struct perf_evlist *evlist) void perf_evlist__exit(struct perf_evlist *evlist)
{ {
free(evlist->mmap); zfree(&evlist->mmap);
free(evlist->pollfd); zfree(&evlist->pollfd);
evlist->mmap = NULL;
evlist->pollfd = NULL;
} }
void perf_evlist__delete(struct perf_evlist *evlist) void perf_evlist__delete(struct perf_evlist *evlist)
...@@ -587,8 +585,7 @@ void perf_evlist__munmap(struct perf_evlist *evlist) ...@@ -587,8 +585,7 @@ void perf_evlist__munmap(struct perf_evlist *evlist)
for (i = 0; i < evlist->nr_mmaps; i++) for (i = 0; i < evlist->nr_mmaps; i++)
__perf_evlist__munmap(evlist, i); __perf_evlist__munmap(evlist, i);
free(evlist->mmap); zfree(&evlist->mmap);
evlist->mmap = NULL;
} }
static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
......
...@@ -750,8 +750,7 @@ void perf_evsel__free_id(struct perf_evsel *evsel) ...@@ -750,8 +750,7 @@ void perf_evsel__free_id(struct perf_evsel *evsel)
{ {
xyarray__delete(evsel->sample_id); xyarray__delete(evsel->sample_id);
evsel->sample_id = NULL; evsel->sample_id = NULL;
free(evsel->id); zfree(&evsel->id);
evsel->id = NULL;
} }
void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) 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, ...@@ -1960,8 +1959,7 @@ bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
evsel->attr.type = PERF_TYPE_SOFTWARE; evsel->attr.type = PERF_TYPE_SOFTWARE;
evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
free(evsel->name); zfree(&evsel->name);
evsel->name = NULL;
return true; return true;
} }
......
...@@ -1324,8 +1324,7 @@ read_event_desc(struct perf_header *ph, int fd) ...@@ -1324,8 +1324,7 @@ read_event_desc(struct perf_header *ph, int fd)
} }
} }
out: out:
if (buf) free(buf);
free(buf);
return events; return events;
error: error:
if (events) if (events)
......
...@@ -263,9 +263,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old) ...@@ -263,9 +263,8 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
for (i = 0; i < old->cnt; i++) for (i = 0; i < old->cnt; i++)
cmds->names[cmds->cnt++] = old->names[i]; cmds->names[cmds->cnt++] = old->names[i];
free(old->names); zfree(&old->names);
old->cnt = 0; old->cnt = 0;
old->names = NULL;
} }
const char *help_unknown_cmd(const char *cmd) const char *help_unknown_cmd(const char *cmd)
......
...@@ -102,8 +102,7 @@ void machine__exit(struct machine *machine) ...@@ -102,8 +102,7 @@ void machine__exit(struct machine *machine)
map_groups__exit(&machine->kmaps); map_groups__exit(&machine->kmaps);
dsos__delete(&machine->user_dsos); dsos__delete(&machine->user_dsos);
dsos__delete(&machine->kernel_dsos); dsos__delete(&machine->kernel_dsos);
free(machine->root_dir); zfree(&machine->root_dir);
machine->root_dir = NULL;
} }
void machine__delete(struct machine *machine) void machine__delete(struct machine *machine)
...@@ -562,11 +561,10 @@ void machine__destroy_kernel_maps(struct machine *machine) ...@@ -562,11 +561,10 @@ void machine__destroy_kernel_maps(struct machine *machine)
* on one of them. * on one of them.
*/ */
if (type == MAP__FUNCTION) { if (type == MAP__FUNCTION) {
free((char *)kmap->ref_reloc_sym->name); zfree((char **)&kmap->ref_reloc_sym->name);
kmap->ref_reloc_sym->name = NULL; zfree(&kmap->ref_reloc_sym);
free(kmap->ref_reloc_sym); } else
} kmap->ref_reloc_sym = NULL;
kmap->ref_reloc_sym = NULL;
} }
map__delete(machine->vmlinux_maps[type]); map__delete(machine->vmlinux_maps[type]);
......
...@@ -506,15 +506,13 @@ static int get_real_path(const char *raw_path, const char *comp_dir, ...@@ -506,15 +506,13 @@ static int get_real_path(const char *raw_path, const char *comp_dir,
case EFAULT: case EFAULT:
raw_path = strchr(++raw_path, '/'); raw_path = strchr(++raw_path, '/');
if (!raw_path) { if (!raw_path) {
free(*new_path); zfree(new_path);
*new_path = NULL;
return -ENOENT; return -ENOENT;
} }
continue; continue;
default: default:
free(*new_path); zfree(new_path);
*new_path = NULL;
return -errno; return -errno;
} }
} }
......
...@@ -226,10 +226,8 @@ struct debuginfo *debuginfo__new(const char *path) ...@@ -226,10 +226,8 @@ struct debuginfo *debuginfo__new(const char *path)
if (!dbg) if (!dbg)
return NULL; return NULL;
if (debuginfo__init_offline_dwarf(dbg, path) < 0) { if (debuginfo__init_offline_dwarf(dbg, path) < 0)
free(dbg); zfree(&dbg);
dbg = NULL;
}
return dbg; return dbg;
} }
...@@ -241,10 +239,8 @@ struct debuginfo *debuginfo__new_online_kernel(unsigned long addr) ...@@ -241,10 +239,8 @@ struct debuginfo *debuginfo__new_online_kernel(unsigned long addr)
if (!dbg) if (!dbg)
return NULL; return NULL;
if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0) { if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0)
free(dbg); zfree(&dbg);
dbg = NULL;
}
return dbg; return dbg;
} }
...@@ -1302,8 +1298,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg, ...@@ -1302,8 +1298,7 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
ret = debuginfo__find_probes(dbg, &tf.pf); ret = debuginfo__find_probes(dbg, &tf.pf);
if (ret < 0) { if (ret < 0) {
free(*tevs); zfree(tevs);
*tevs = NULL;
return ret; return ret;
} }
...@@ -1417,8 +1412,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg, ...@@ -1417,8 +1412,7 @@ int debuginfo__find_available_vars_at(struct debuginfo *dbg,
free(af.vls[af.nvls].point.symbol); free(af.vls[af.nvls].point.symbol);
strlist__delete(af.vls[af.nvls].vars); strlist__delete(af.vls[af.nvls].vars);
} }
free(af.vls); zfree(vls);
*vls = NULL;
return ret; return ret;
} }
...@@ -1522,8 +1516,7 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr, ...@@ -1522,8 +1516,7 @@ int debuginfo__find_probe_point(struct debuginfo *dbg, unsigned long addr,
if (fname) { if (fname) {
ppt->file = strdup(fname); ppt->file = strdup(fname);
if (ppt->file == NULL) { if (ppt->file == NULL) {
free(ppt->function); zfree(&ppt->function);
ppt->function = NULL;
ret = -ENOMEM; ret = -ENOMEM;
goto end; goto end;
} }
...@@ -1577,8 +1570,7 @@ static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf) ...@@ -1577,8 +1570,7 @@ static int find_line_range_by_line(Dwarf_Die *sp_die, struct line_finder *lf)
else else
ret = 0; /* Lines are not found */ ret = 0; /* Lines are not found */
else { else {
free(lf->lr->path); zfree(&lf->lr->path);
lf->lr->path = NULL;
} }
return ret; return ret;
} }
......
...@@ -1621,13 +1621,10 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map, ...@@ -1621,13 +1621,10 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map,
static void vmlinux_path__exit(void) static void vmlinux_path__exit(void)
{ {
while (--vmlinux_path__nr_entries >= 0) { while (--vmlinux_path__nr_entries >= 0)
free(vmlinux_path[vmlinux_path__nr_entries]); zfree(&vmlinux_path[vmlinux_path__nr_entries]);
vmlinux_path[vmlinux_path__nr_entries] = NULL;
}
free(vmlinux_path); zfree(&vmlinux_path);
vmlinux_path = NULL;
} }
static int vmlinux_path__init(void) static int vmlinux_path__init(void)
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "strlist.h" #include "strlist.h"
#include <string.h> #include <string.h>
#include "thread_map.h" #include "thread_map.h"
#include "util.h"
/* Skip "." and ".." directories */ /* Skip "." and ".." directories */
static int filter(const struct dirent *dir) static int filter(const struct dirent *dir)
...@@ -138,8 +139,7 @@ struct thread_map *thread_map__new_by_uid(uid_t uid) ...@@ -138,8 +139,7 @@ struct thread_map *thread_map__new_by_uid(uid_t uid)
free(namelist); free(namelist);
out_free_closedir: out_free_closedir:
free(threads); zfree(&threads);
threads = NULL;
goto out_closedir; goto out_closedir;
} }
...@@ -210,8 +210,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) ...@@ -210,8 +210,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
free(namelist); free(namelist);
out_free_threads: out_free_threads:
free(threads); zfree(&threads);
threads = NULL;
goto out; goto out;
} }
...@@ -262,8 +261,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) ...@@ -262,8 +261,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
return threads; return threads;
out_free_threads: out_free_threads:
free(threads); zfree(&threads);
threads = NULL;
goto out; goto out;
} }
......
...@@ -562,10 +562,8 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs, ...@@ -562,10 +562,8 @@ struct tracing_data *tracing_data_get(struct list_head *pattrs,
output_fd = fd; output_fd = fd;
} }
if (err) { if (err)
free(tdata); zfree(&tdata);
tdata = NULL;
}
put_tracepoints_path(tps); put_tracepoints_path(tps);
return tdata; return tdata;
......
...@@ -186,6 +186,8 @@ static inline void *zalloc(size_t size) ...@@ -186,6 +186,8 @@ static inline void *zalloc(size_t size)
return calloc(1, size); return calloc(1, size);
} }
#define zfree(ptr) ({ free(*ptr); *ptr = NULL; })
static inline int has_extension(const char *filename, const char *ext) static inline int has_extension(const char *filename, const char *ext)
{ {
size_t len = strlen(filename); size_t len = strlen(filename);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment