[PATCH 2/6] perf thread: Allow tools to register a thread->priv destructor

From: Arnaldo Carvalho de Melo
Date: Wed Jul 19 2023 - 16:32:17 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

So that when thread__delete() runs it can be called and free stuff tools
stashed into thread->priv, like 'perf trace' does and will use this
new facility to plug some leaks.

Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
Cc: Ian Rogers <irogers@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/thread.c | 11 +++++++++++
tools/perf/util/thread.h | 2 ++
2 files changed, 13 insertions(+)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0b166404c5c365cf..35dd4e716e411da9 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -80,6 +80,13 @@ struct thread *thread__new(pid_t pid, pid_t tid)
return NULL;
}

+static void (*thread__priv_destructor)(void *priv);
+
+void thread__set_priv_destructor(void (*destructor)(void *priv))
+{
+ thread__priv_destructor = destructor;
+}
+
void thread__delete(struct thread *thread)
{
struct namespaces *namespaces, *tmp_namespaces;
@@ -112,6 +119,10 @@ void thread__delete(struct thread *thread)
exit_rwsem(thread__namespaces_lock(thread));
exit_rwsem(thread__comm_lock(thread));
thread__free_stitch_list(thread);
+
+ if (thread__priv_destructor)
+ thread__priv_destructor(thread__priv(thread));
+
RC_CHK_FREE(thread);
}

diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 9068a21ce0fa1b0f..e79225a0ea46b789 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -71,6 +71,8 @@ struct thread *thread__new(pid_t pid, pid_t tid);
int thread__init_maps(struct thread *thread, struct machine *machine);
void thread__delete(struct thread *thread);

+void thread__set_priv_destructor(void (*destructor)(void *priv));
+
struct thread *thread__get(struct thread *thread);
void thread__put(struct thread *thread);

--
2.41.0