#include <stdlib.h>
 #include <linux/refcount.h>
 #include <internal/threadmap.h>
+#include <string.h>
+
+static void perf_thread_map__reset(struct perf_thread_map *map, int start, int nr)
+{
+       size_t size = (nr - start) * sizeof(map->map[0]);
+
+       memset(&map->map[start], 0, size);
+       map->err_thread = -1;
+}
+
+struct perf_thread_map *perf_thread_map__realloc(struct perf_thread_map *map, int nr)
+{
+       size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
+       int start = map ? map->nr : 0;
+
+       map = realloc(map, size);
+       /*
+        * We only realloc to add more items, let's reset new items.
+        */
+       if (map)
+               perf_thread_map__reset(map, start, nr);
+
+       return map;
+}
+
+#define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
+
+void perf_thread_map__set_pid(struct perf_thread_map *map, int thread, pid_t pid)
+{
+       map->map[thread].pid = pid;
+}
+
+struct perf_thread_map *perf_thread_map__new_dummy(void)
+{
+       struct perf_thread_map *threads = thread_map__alloc(1);
+
+       if (threads != NULL) {
+               perf_thread_map__set_pid(threads, 0, -1);
+               threads->nr = 1;
+               refcount_set(&threads->refcnt, 1);
+       }
+       return threads;
+}
 
                return 1;
 }
 
-static void thread_map__reset(struct perf_thread_map *map, int start, int nr)
-{
-       size_t size = (nr - start) * sizeof(map->map[0]);
-
-       memset(&map->map[start], 0, size);
-       map->err_thread = -1;
-}
-
-static struct perf_thread_map *thread_map__realloc(struct perf_thread_map *map, int nr)
-{
-       size_t size = sizeof(*map) + sizeof(map->map[0]) * nr;
-       int start = map ? map->nr : 0;
-
-       map = realloc(map, size);
-       /*
-        * We only realloc to add more items, let's reset new items.
-        */
-       if (map)
-               thread_map__reset(map, start, nr);
-
-       return map;
-}
-
-#define thread_map__alloc(__nr) thread_map__realloc(NULL, __nr)
+#define thread_map__alloc(__nr) perf_thread_map__realloc(NULL, __nr)
 
 struct perf_thread_map *thread_map__new_by_pid(pid_t pid)
 {
        threads = thread_map__alloc(items);
        if (threads != NULL) {
                for (i = 0; i < items; i++)
-                       thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
+                       perf_thread_map__set_pid(threads, i, atoi(namelist[i]->d_name));
                threads->nr = items;
                refcount_set(&threads->refcnt, 1);
        }
        struct perf_thread_map *threads = thread_map__alloc(1);
 
        if (threads != NULL) {
-               thread_map__set_pid(threads, 0, tid);
+               perf_thread_map__set_pid(threads, 0, tid);
                threads->nr = 1;
                refcount_set(&threads->refcnt, 1);
        }
                if (grow) {
                        struct perf_thread_map *tmp;
 
-                       tmp = thread_map__realloc(threads, max_threads);
+                       tmp = perf_thread_map__realloc(threads, max_threads);
                        if (tmp == NULL)
                                goto out_free_namelist;
 
                }
 
                for (i = 0; i < items; i++) {
-                       thread_map__set_pid(threads, threads->nr + i,
-                                           atoi(namelist[i]->d_name));
+                       perf_thread_map__set_pid(threads, threads->nr + i,
+                                                   atoi(namelist[i]->d_name));
                }
 
                for (i = 0; i < items; i++)
                        goto out_free_threads;
 
                total_tasks += items;
-               nt = thread_map__realloc(threads, total_tasks);
+               nt = perf_thread_map__realloc(threads, total_tasks);
                if (nt == NULL)
                        goto out_free_namelist;
 
                threads = nt;
 
                for (i = 0; i < items; i++) {
-                       thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
+                       perf_thread_map__set_pid(threads, j++, atoi(namelist[i]->d_name));
                        zfree(&namelist[i]);
                }
                threads->nr = total_tasks;
        goto out;
 }
 
-struct perf_thread_map *thread_map__new_dummy(void)
-{
-       struct perf_thread_map *threads = thread_map__alloc(1);
-
-       if (threads != NULL) {
-               thread_map__set_pid(threads, 0, -1);
-               threads->nr = 1;
-               refcount_set(&threads->refcnt, 1);
-       }
-       return threads;
-}
-
 struct perf_thread_map *thread_map__new_by_tid_str(const char *tid_str)
 {
        struct perf_thread_map *threads = NULL, *nt;
 
        /* perf-stat expects threads to be generated even if tid not given */
        if (!tid_str)
-               return thread_map__new_dummy();
+               return perf_thread_map__new_dummy();
 
        slist = strlist__new(tid_str, &slist_config);
        if (!slist)
                        continue;
 
                ntasks++;
-               nt = thread_map__realloc(threads, ntasks);
+               nt = perf_thread_map__realloc(threads, ntasks);
 
                if (nt == NULL)
                        goto out_free_threads;
 
                threads = nt;
-               thread_map__set_pid(threads, ntasks - 1, tid);
+               perf_thread_map__set_pid(threads, ntasks - 1, tid);
                threads->nr = ntasks;
        }
 out:
        threads->nr = (int) event->nr;
 
        for (i = 0; i < event->nr; i++) {
-               thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
+               perf_thread_map__set_pid(threads, i, (pid_t) event->entries[i].pid);
                threads->map[i].comm = strndup(event->entries[i].comm, 16);
        }