Commit 9d7e8c3a authored by Jiri Olsa's avatar Jiri Olsa Committed by Arnaldo Carvalho de Melo

perf tools: Add thread_map__(alloc|realloc) helpers

In order to have 'struct thread_map' allocation on single place and can
change it easily in following patch.

Using alloc|realloc for static helpers, because thread_map__new is
already used in public interface.
Signed-off-by: default avatarJiri Olsa <jolsa@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1434269985-521-3-git-send-email-jolsa@kernel.orgSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent b45f65e8
...@@ -20,6 +20,15 @@ static int filter(const struct dirent *dir) ...@@ -20,6 +20,15 @@ static int filter(const struct dirent *dir)
return 1; return 1;
} }
static struct thread_map *thread_map__realloc(struct thread_map *map, int nr)
{
size_t size = sizeof(*map) + sizeof(pid_t) * nr;
return realloc(map, size);
}
#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
struct thread_map *thread_map__new_by_pid(pid_t pid) struct thread_map *thread_map__new_by_pid(pid_t pid)
{ {
struct thread_map *threads; struct thread_map *threads;
...@@ -33,7 +42,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid) ...@@ -33,7 +42,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid)
if (items <= 0) if (items <= 0)
return NULL; return NULL;
threads = malloc(sizeof(*threads) + sizeof(pid_t) * items); threads = thread_map__alloc(items);
if (threads != NULL) { if (threads != NULL) {
for (i = 0; i < items; i++) for (i = 0; i < items; i++)
threads->map[i] = atoi(namelist[i]->d_name); threads->map[i] = atoi(namelist[i]->d_name);
...@@ -49,7 +58,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid) ...@@ -49,7 +58,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid)
struct thread_map *thread_map__new_by_tid(pid_t tid) struct thread_map *thread_map__new_by_tid(pid_t tid)
{ {
struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t)); struct thread_map *threads = thread_map__alloc(1);
if (threads != NULL) { if (threads != NULL) {
threads->map[0] = tid; threads->map[0] = tid;
...@@ -65,8 +74,8 @@ struct thread_map *thread_map__new_by_uid(uid_t uid) ...@@ -65,8 +74,8 @@ struct thread_map *thread_map__new_by_uid(uid_t uid)
int max_threads = 32, items, i; int max_threads = 32, items, i;
char path[256]; char path[256];
struct dirent dirent, *next, **namelist = NULL; struct dirent dirent, *next, **namelist = NULL;
struct thread_map *threads = malloc(sizeof(*threads) + struct thread_map *threads = thread_map__alloc(max_threads);
max_threads * sizeof(pid_t));
if (threads == NULL) if (threads == NULL)
goto out; goto out;
...@@ -185,8 +194,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) ...@@ -185,8 +194,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
goto out_free_threads; goto out_free_threads;
total_tasks += items; total_tasks += items;
nt = realloc(threads, (sizeof(*threads) + nt = thread_map__realloc(threads, total_tasks);
sizeof(pid_t) * total_tasks));
if (nt == NULL) if (nt == NULL)
goto out_free_namelist; goto out_free_namelist;
...@@ -216,7 +224,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) ...@@ -216,7 +224,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str)
struct thread_map *thread_map__new_dummy(void) struct thread_map *thread_map__new_dummy(void)
{ {
struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t)); struct thread_map *threads = thread_map__alloc(1);
if (threads != NULL) { if (threads != NULL) {
threads->map[0] = -1; threads->map[0] = -1;
...@@ -253,7 +261,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) ...@@ -253,7 +261,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str)
continue; continue;
ntasks++; ntasks++;
nt = realloc(threads, sizeof(*threads) + sizeof(pid_t) * ntasks); nt = thread_map__realloc(threads, ntasks);
if (nt == NULL) if (nt == NULL)
goto out_free_threads; goto out_free_threads;
......
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