Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+56 -103
+1 -2
tools/perf/arch/common.c
··· 154 154 } 155 155 if (lookup_path(buf)) 156 156 goto out; 157 - free(buf); 158 - buf = NULL; 157 + zfree(&buf); 159 158 } 160 159 161 160 if (!strcmp(arch, "arm"))
+1 -2
tools/perf/builtin-annotate.c
··· 180 180 * symbol, free he->ms.sym->src to signal we already 181 181 * processed this symbol. 182 182 */ 183 - free(notes->src); 184 - notes->src = NULL; 183 + zfree(&notes->src); 185 184 } 186 185 } 187 186 }
+2 -4
tools/perf/builtin-stat.c
··· 185 185 186 186 static void perf_evsel__free_stat_priv(struct perf_evsel *evsel) 187 187 { 188 - free(evsel->priv); 189 - evsel->priv = NULL; 188 + zfree(&evsel->priv); 190 189 } 191 190 192 191 static int perf_evsel__alloc_prev_raw_counts(struct perf_evsel *evsel) ··· 207 208 208 209 static void perf_evsel__free_prev_raw_counts(struct perf_evsel *evsel) 209 210 { 210 - free(evsel->prev_raw_counts); 211 - evsel->prev_raw_counts = NULL; 211 + zfree(&evsel->prev_raw_counts); 212 212 } 213 213 214 214 static void perf_evlist__free_stats(struct perf_evlist *evlist)
+1 -2
tools/perf/builtin-timechart.c
··· 488 488 * It seems the callchain is corrupted. 489 489 * Discard all. 490 490 */ 491 - free(p); 492 - p = NULL; 491 + zfree(&p); 493 492 goto exit; 494 493 } 495 494 continue;
+4 -8
tools/perf/builtin-trace.c
··· 146 146 147 147 static void perf_evsel__delete_priv(struct perf_evsel *evsel) 148 148 { 149 - free(evsel->priv); 150 - evsel->priv = NULL; 149 + zfree(&evsel->priv); 151 150 perf_evsel__delete(evsel); 152 151 } 153 152 ··· 164 165 return -ENOMEM; 165 166 166 167 out_delete: 167 - free(evsel->priv); 168 - evsel->priv = NULL; 168 + zfree(&evsel->priv); 169 169 return -ENOENT; 170 170 } 171 171 ··· 1276 1278 size_t printed = syscall_arg__scnprintf_fd(bf, size, arg); 1277 1279 struct thread_trace *ttrace = arg->thread->priv; 1278 1280 1279 - if (ttrace && fd >= 0 && fd <= ttrace->paths.max) { 1280 - free(ttrace->paths.table[fd]); 1281 - ttrace->paths.table[fd] = NULL; 1282 - } 1281 + if (ttrace && fd >= 0 && fd <= ttrace->paths.max) 1282 + zfree(&ttrace->paths.table[fd]); 1283 1283 1284 1284 return printed; 1285 1285 }
+2 -4
tools/perf/ui/browser.c
··· 256 256 __ui_browser__show_title(browser, title); 257 257 258 258 browser->title = title; 259 - free(browser->helpline); 260 - browser->helpline = NULL; 259 + zfree(&browser->helpline); 261 260 262 261 va_start(ap, helpline); 263 262 err = vasprintf(&browser->helpline, helpline, ap); ··· 271 272 { 272 273 pthread_mutex_lock(&ui__lock); 273 274 ui_helpline__pop(); 274 - free(browser->helpline); 275 - browser->helpline = NULL; 275 + zfree(&browser->helpline); 276 276 pthread_mutex_unlock(&ui__lock); 277 277 } 278 278
+2 -4
tools/perf/ui/browsers/hists.c
··· 1267 1267 { 1268 1268 int i; 1269 1269 1270 - for (i = 0; i < n; ++i) { 1271 - free(options[i]); 1272 - options[i] = NULL; 1273 - } 1270 + for (i = 0; i < n; ++i) 1271 + zfree(&options[i]); 1274 1272 } 1275 1273 1276 1274 /* Check whether the browser is for 'top' or 'report' */
+1 -2
tools/perf/ui/gtk/util.c
··· 23 23 if (!perf_gtk__is_active_context(*ctx)) 24 24 return -1; 25 25 26 - free(*ctx); 27 - *ctx = NULL; 26 + zfree(ctx); 28 27 return 0; 29 28 } 30 29
+2 -4
tools/perf/util/alias.c
··· 55 55 src++; 56 56 c = cmdline[src]; 57 57 if (!c) { 58 - free(*argv); 59 - *argv = NULL; 58 + zfree(argv); 60 59 return error("cmdline ends with \\"); 61 60 } 62 61 } ··· 67 68 cmdline[dst] = 0; 68 69 69 70 if (quoted) { 70 - free(*argv); 71 - *argv = NULL; 71 + zfree(argv); 72 72 return error("unclosed quote"); 73 73 } 74 74
+4 -8
tools/perf/util/annotate.c
··· 185 185 return 0; 186 186 187 187 out_free_ops: 188 - free(ops->locked.ops); 189 - ops->locked.ops = NULL; 188 + zfree(&ops->locked.ops); 190 189 return 0; 191 190 } 192 191 ··· 255 256 return 0; 256 257 257 258 out_free_source: 258 - free(ops->source.raw); 259 - ops->source.raw = NULL; 259 + zfree(&ops->source.raw); 260 260 return -1; 261 261 } 262 262 ··· 558 560 return 0; 559 561 560 562 out_free_name: 561 - free(*namep); 562 - *namep = NULL; 563 + zfree(namep); 563 564 return -1; 564 565 } 565 566 ··· 1110 1113 src_line = (void *)src_line + sizeof_src_line; 1111 1114 } 1112 1115 1113 - free(notes->src->lines); 1114 - notes->src->lines = NULL; 1116 + zfree(&notes->src->lines); 1115 1117 } 1116 1118 1117 1119 /* Get the filename:line for the colored entries */
+3 -6
tools/perf/util/dso.c
··· 497 497 symbols__delete(&dso->symbols[i]); 498 498 499 499 if (dso->short_name_allocated) { 500 - free((char *)dso->short_name); 501 - dso->short_name = NULL; 500 + zfree((char **)&dso->short_name); 502 501 dso->short_name_allocated = false; 503 502 } 504 503 505 504 if (dso->long_name_allocated) { 506 - free((char *)dso->long_name); 507 - dso->long_name = NULL; 505 + zfree((char **)&dso->long_name); 508 506 dso->long_name_allocated = false; 509 507 } 510 508 511 509 dso_cache__free(&dso->cache); 512 510 dso__free_a2l(dso); 513 - free(dso->symsrc_filename); 514 - dso->symsrc_filename = NULL; 511 + zfree(&dso->symsrc_filename); 515 512 free(dso); 516 513 } 517 514
+3 -6
tools/perf/util/evlist.c
··· 101 101 102 102 void perf_evlist__exit(struct perf_evlist *evlist) 103 103 { 104 - free(evlist->mmap); 105 - free(evlist->pollfd); 106 - evlist->mmap = NULL; 107 - evlist->pollfd = NULL; 104 + zfree(&evlist->mmap); 105 + zfree(&evlist->pollfd); 108 106 } 109 107 110 108 void perf_evlist__delete(struct perf_evlist *evlist) ··· 585 587 for (i = 0; i < evlist->nr_mmaps; i++) 586 588 __perf_evlist__munmap(evlist, i); 587 589 588 - free(evlist->mmap); 589 - evlist->mmap = NULL; 590 + zfree(&evlist->mmap); 590 591 } 591 592 592 593 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
+2 -4
tools/perf/util/evsel.c
··· 750 750 { 751 751 xyarray__delete(evsel->sample_id); 752 752 evsel->sample_id = NULL; 753 - free(evsel->id); 754 - evsel->id = NULL; 753 + zfree(&evsel->id); 755 754 } 756 755 757 756 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) ··· 1959 1960 evsel->attr.type = PERF_TYPE_SOFTWARE; 1960 1961 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK; 1961 1962 1962 - free(evsel->name); 1963 - evsel->name = NULL; 1963 + zfree(&evsel->name); 1964 1964 return true; 1965 1965 } 1966 1966
+1 -2
tools/perf/util/header.c
··· 1324 1324 } 1325 1325 } 1326 1326 out: 1327 - if (buf) 1328 - free(buf); 1327 + free(buf); 1329 1328 return events; 1330 1329 error: 1331 1330 if (events)
+1 -2
tools/perf/util/help.c
··· 263 263 264 264 for (i = 0; i < old->cnt; i++) 265 265 cmds->names[cmds->cnt++] = old->names[i]; 266 - free(old->names); 266 + zfree(&old->names); 267 267 old->cnt = 0; 268 - old->names = NULL; 269 268 } 270 269 271 270 const char *help_unknown_cmd(const char *cmd)
+5 -7
tools/perf/util/machine.c
··· 102 102 map_groups__exit(&machine->kmaps); 103 103 dsos__delete(&machine->user_dsos); 104 104 dsos__delete(&machine->kernel_dsos); 105 - free(machine->root_dir); 106 - machine->root_dir = NULL; 105 + zfree(&machine->root_dir); 107 106 } 108 107 109 108 void machine__delete(struct machine *machine) ··· 561 562 * on one of them. 562 563 */ 563 564 if (type == MAP__FUNCTION) { 564 - free((char *)kmap->ref_reloc_sym->name); 565 - kmap->ref_reloc_sym->name = NULL; 566 - free(kmap->ref_reloc_sym); 567 - } 568 - kmap->ref_reloc_sym = NULL; 565 + zfree((char **)&kmap->ref_reloc_sym->name); 566 + zfree(&kmap->ref_reloc_sym); 567 + } else 568 + kmap->ref_reloc_sym = NULL; 569 569 } 570 570 571 571 map__delete(machine->vmlinux_maps[type]);
+2 -4
tools/perf/util/probe-event.c
··· 506 506 case EFAULT: 507 507 raw_path = strchr(++raw_path, '/'); 508 508 if (!raw_path) { 509 - free(*new_path); 510 - *new_path = NULL; 509 + zfree(new_path); 511 510 return -ENOENT; 512 511 } 513 512 continue; 514 513 515 514 default: 516 - free(*new_path); 517 - *new_path = NULL; 515 + zfree(new_path); 518 516 return -errno; 519 517 } 520 518 }
+8 -16
tools/perf/util/probe-finder.c
··· 226 226 if (!dbg) 227 227 return NULL; 228 228 229 - if (debuginfo__init_offline_dwarf(dbg, path) < 0) { 230 - free(dbg); 231 - dbg = NULL; 232 - } 229 + if (debuginfo__init_offline_dwarf(dbg, path) < 0) 230 + zfree(&dbg); 233 231 234 232 return dbg; 235 233 } ··· 239 241 if (!dbg) 240 242 return NULL; 241 243 242 - if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0) { 243 - free(dbg); 244 - dbg = NULL; 245 - } 244 + if (debuginfo__init_online_kernel_dwarf(dbg, (Dwarf_Addr)addr) < 0) 245 + zfree(&dbg); 246 246 247 247 return dbg; 248 248 } ··· 1298 1302 1299 1303 ret = debuginfo__find_probes(dbg, &tf.pf); 1300 1304 if (ret < 0) { 1301 - free(*tevs); 1302 - *tevs = NULL; 1305 + zfree(tevs); 1303 1306 return ret; 1304 1307 } 1305 1308 ··· 1412 1417 free(af.vls[af.nvls].point.symbol); 1413 1418 strlist__delete(af.vls[af.nvls].vars); 1414 1419 } 1415 - free(af.vls); 1416 - *vls = NULL; 1420 + zfree(vls); 1417 1421 return ret; 1418 1422 } 1419 1423 ··· 1516 1522 if (fname) { 1517 1523 ppt->file = strdup(fname); 1518 1524 if (ppt->file == NULL) { 1519 - free(ppt->function); 1520 - ppt->function = NULL; 1525 + zfree(&ppt->function); 1521 1526 ret = -ENOMEM; 1522 1527 goto end; 1523 1528 } ··· 1570 1577 else 1571 1578 ret = 0; /* Lines are not found */ 1572 1579 else { 1573 - free(lf->lr->path); 1574 - lf->lr->path = NULL; 1580 + zfree(&lf->lr->path); 1575 1581 } 1576 1582 return ret; 1577 1583 }
+3 -6
tools/perf/util/symbol.c
··· 1621 1621 1622 1622 static void vmlinux_path__exit(void) 1623 1623 { 1624 - while (--vmlinux_path__nr_entries >= 0) { 1625 - free(vmlinux_path[vmlinux_path__nr_entries]); 1626 - vmlinux_path[vmlinux_path__nr_entries] = NULL; 1627 - } 1624 + while (--vmlinux_path__nr_entries >= 0) 1625 + zfree(&vmlinux_path[vmlinux_path__nr_entries]); 1628 1626 1629 - free(vmlinux_path); 1630 - vmlinux_path = NULL; 1627 + zfree(&vmlinux_path); 1631 1628 } 1632 1629 1633 1630 static int vmlinux_path__init(void)
+4 -6
tools/perf/util/thread_map.c
··· 9 9 #include "strlist.h" 10 10 #include <string.h> 11 11 #include "thread_map.h" 12 + #include "util.h" 12 13 13 14 /* Skip "." and ".." directories */ 14 15 static int filter(const struct dirent *dir) ··· 139 138 free(namelist); 140 139 141 140 out_free_closedir: 142 - free(threads); 143 - threads = NULL; 141 + zfree(&threads); 144 142 goto out_closedir; 145 143 } 146 144 ··· 210 210 free(namelist); 211 211 212 212 out_free_threads: 213 - free(threads); 214 - threads = NULL; 213 + zfree(&threads); 215 214 goto out; 216 215 } 217 216 ··· 261 262 return threads; 262 263 263 264 out_free_threads: 264 - free(threads); 265 - threads = NULL; 265 + zfree(&threads); 266 266 goto out; 267 267 } 268 268
+2 -4
tools/perf/util/trace-event-info.c
··· 562 562 output_fd = fd; 563 563 } 564 564 565 - if (err) { 566 - free(tdata); 567 - tdata = NULL; 568 - } 565 + if (err) 566 + zfree(&tdata); 569 567 570 568 put_tracepoints_path(tps); 571 569 return tdata;
+2
tools/perf/util/util.h
··· 186 186 return calloc(1, size); 187 187 } 188 188 189 + #define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) 190 + 189 191 static inline int has_extension(const char *filename, const char *ext) 190 192 { 191 193 size_t len = strlen(filename);