diff options
| author | Jiri Olsa <jolsa@kernel.org> | 2015-06-14 04:19:17 -0400 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-06-16 09:34:40 -0400 |
| commit | 9d7e8c3a96e5a903a4e0951e08f7fa6957170bef (patch) | |
| tree | 09f148c7d9c01b9619dc5dc9ac52279a899f93ef | |
| parent | b45f65e8fddc89ac6b46388908d3f6ac728be372 (diff) | |
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: Jiri 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.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
| -rw-r--r-- | tools/perf/util/thread_map.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/tools/perf/util/thread_map.c b/tools/perf/util/thread_map.c index f93b9734735b..f4822bd03709 100644 --- a/tools/perf/util/thread_map.c +++ b/tools/perf/util/thread_map.c | |||
| @@ -20,6 +20,15 @@ static int filter(const struct dirent *dir) | |||
| 20 | return 1; | 20 | return 1; |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | static struct thread_map *thread_map__realloc(struct thread_map *map, int nr) | ||
| 24 | { | ||
| 25 | size_t size = sizeof(*map) + sizeof(pid_t) * nr; | ||
| 26 | |||
| 27 | return realloc(map, size); | ||
| 28 | } | ||
| 29 | |||
| 30 | #define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr) | ||
| 31 | |||
| 23 | struct thread_map *thread_map__new_by_pid(pid_t pid) | 32 | struct thread_map *thread_map__new_by_pid(pid_t pid) |
| 24 | { | 33 | { |
| 25 | struct thread_map *threads; | 34 | struct thread_map *threads; |
| @@ -33,7 +42,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid) | |||
| 33 | if (items <= 0) | 42 | if (items <= 0) |
| 34 | return NULL; | 43 | return NULL; |
| 35 | 44 | ||
| 36 | threads = malloc(sizeof(*threads) + sizeof(pid_t) * items); | 45 | threads = thread_map__alloc(items); |
| 37 | if (threads != NULL) { | 46 | if (threads != NULL) { |
| 38 | for (i = 0; i < items; i++) | 47 | for (i = 0; i < items; i++) |
| 39 | threads->map[i] = atoi(namelist[i]->d_name); | 48 | threads->map[i] = atoi(namelist[i]->d_name); |
| @@ -49,7 +58,7 @@ struct thread_map *thread_map__new_by_pid(pid_t pid) | |||
| 49 | 58 | ||
| 50 | struct thread_map *thread_map__new_by_tid(pid_t tid) | 59 | struct thread_map *thread_map__new_by_tid(pid_t tid) |
| 51 | { | 60 | { |
| 52 | struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t)); | 61 | struct thread_map *threads = thread_map__alloc(1); |
| 53 | 62 | ||
| 54 | if (threads != NULL) { | 63 | if (threads != NULL) { |
| 55 | threads->map[0] = tid; | 64 | threads->map[0] = tid; |
| @@ -65,8 +74,8 @@ struct thread_map *thread_map__new_by_uid(uid_t uid) | |||
| 65 | int max_threads = 32, items, i; | 74 | int max_threads = 32, items, i; |
| 66 | char path[256]; | 75 | char path[256]; |
| 67 | struct dirent dirent, *next, **namelist = NULL; | 76 | struct dirent dirent, *next, **namelist = NULL; |
| 68 | struct thread_map *threads = malloc(sizeof(*threads) + | 77 | struct thread_map *threads = thread_map__alloc(max_threads); |
| 69 | max_threads * sizeof(pid_t)); | 78 | |
| 70 | if (threads == NULL) | 79 | if (threads == NULL) |
| 71 | goto out; | 80 | goto out; |
| 72 | 81 | ||
| @@ -185,8 +194,7 @@ static struct thread_map *thread_map__new_by_pid_str(const char *pid_str) | |||
| 185 | goto out_free_threads; | 194 | goto out_free_threads; |
| 186 | 195 | ||
| 187 | total_tasks += items; | 196 | total_tasks += items; |
| 188 | nt = realloc(threads, (sizeof(*threads) + | 197 | nt = thread_map__realloc(threads, total_tasks); |
| 189 | sizeof(pid_t) * total_tasks)); | ||
| 190 | if (nt == NULL) | 198 | if (nt == NULL) |
| 191 | goto out_free_namelist; | 199 | goto out_free_namelist; |
| 192 | 200 | ||
| @@ -216,7 +224,7 @@ out_free_threads: | |||
| 216 | 224 | ||
| 217 | struct thread_map *thread_map__new_dummy(void) | 225 | struct thread_map *thread_map__new_dummy(void) |
| 218 | { | 226 | { |
| 219 | struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t)); | 227 | struct thread_map *threads = thread_map__alloc(1); |
| 220 | 228 | ||
| 221 | if (threads != NULL) { | 229 | if (threads != NULL) { |
| 222 | threads->map[0] = -1; | 230 | threads->map[0] = -1; |
| @@ -253,7 +261,7 @@ static struct thread_map *thread_map__new_by_tid_str(const char *tid_str) | |||
| 253 | continue; | 261 | continue; |
| 254 | 262 | ||
| 255 | ntasks++; | 263 | ntasks++; |
| 256 | nt = realloc(threads, sizeof(*threads) + sizeof(pid_t) * ntasks); | 264 | nt = thread_map__realloc(threads, ntasks); |
| 257 | 265 | ||
| 258 | if (nt == NULL) | 266 | if (nt == NULL) |
| 259 | goto out_free_threads; | 267 | goto out_free_threads; |
