As preparation for using header records in pipe mode, replace
int fd with struct feat_fd fd in process and read functions for
all header record types.

To reduce the boiler plate, define and use the FEAT_PROCESS_STR_FUN
macro for the common case of header records that are a simple string.

Signed-off-by: David Carrillo-Cisneros <davi...@google.com>
---
 tools/perf/util/header.c | 405 ++++++++++++++++++-----------------------------
 tools/perf/util/header.h |   1 +
 2 files changed, 156 insertions(+), 250 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index e28770d12c1c..981f5e9685e2 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -141,27 +141,67 @@ static int do_write_string(struct feat_fd *fd, const char 
*str)
        return write_padded(fd, str, olen, len);
 }
 
-static char *do_read_string(int fd, struct perf_header *ph)
+static int __do_read(struct feat_fd *fd, void *addr, ssize_t size)
+{
+       if (!fd->buf) {
+               ssize_t ret = readn(fd->fd, addr, size);
+
+               if (ret != (ssize_t)size)
+                       return ret < 0 ? (int)ret : -1;
+               return 0;
+       }
+
+       assert((ssize_t)fd->size > fd->offset);
+       if (size > (ssize_t)fd->size - fd->offset)
+               return -1;
+
+       memcpy(addr, fd->buf + fd->offset, size);
+       fd->offset += size;
+
+       return 0;
+}
+
+static int do_read_u32(struct feat_fd *fd, u32 *addr)
+{
+       int ret;
+
+       ret = __do_read(fd, addr, sizeof(*addr));
+       if (ret)
+               return ret;
+
+       if (fd->ph->needs_swap)
+               *addr = bswap_32(*addr);
+       return 0;
+}
+
+static int do_read_u64(struct feat_fd *fd, u64 *addr)
+{
+       int ret;
+
+       ret = __do_read(fd, addr, sizeof(*addr));
+       if (ret)
+               return ret;
+
+       if (fd->ph->needs_swap)
+               *addr = bswap_64(*addr);
+       return 0;
+}
+
+static char *do_read_string(struct feat_fd *fd)
 {
-       ssize_t sz, ret;
        u32 len;
        char *buf;
 
-       sz = readn(fd, &len, sizeof(len));
-       if (sz < (ssize_t)sizeof(len))
+       if (do_read_u32(fd, &len))
                return NULL;
 
-       if (ph->needs_swap)
-               len = bswap_32(len);
-
        buf = malloc(len);
        if (!buf)
                return NULL;
 
-       ret = readn(fd, buf, len);
-       if (ret == (ssize_t)len) {
+       if (!__do_read(fd, buf, len)) {
                /*
-                * strings are padded by zeroes
+                * note that strings are padded by zeroes
                 * thus the actual strlen of buf
                 * may be less than len
                 */
@@ -1207,8 +1247,7 @@ static void free_event_desc(struct perf_evsel *events)
        free(events);
 }
 
-static struct perf_evsel *
-read_event_desc(struct perf_header *ph, int fd)
+static struct perf_evsel *read_event_desc(struct feat_fd *fd)
 {
        struct perf_evsel *evsel, *events = NULL;
        u64 *id;
@@ -1218,20 +1257,12 @@ read_event_desc(struct perf_header *ph, int fd)
        size_t msz;
 
        /* number of events */
-       ret = readn(fd, &nre, sizeof(nre));
-       if (ret != (ssize_t)sizeof(nre))
+       if (do_read_u32(fd, &nre))
                goto error;
 
-       if (ph->needs_swap)
-               nre = bswap_32(nre);
-
-       ret = readn(fd, &sz, sizeof(sz));
-       if (ret != (ssize_t)sizeof(sz))
+       if (do_read_u32(fd, &sz))
                goto error;
 
-       if (ph->needs_swap)
-               sz = bswap_32(sz);
-
        /* buffer to hold on file attr struct */
        buf = malloc(sz);
        if (!buf)
@@ -1253,25 +1284,22 @@ read_event_desc(struct perf_header *ph, int fd)
                 * must read entire on-file attr struct to
                 * sync up with layout.
                 */
-               ret = readn(fd, buf, sz);
+               ret = __do_read(fd, buf, sz);
                if (ret != (ssize_t)sz)
                        goto error;
 
-               if (ph->needs_swap)
+               if (fd->ph->needs_swap)
                        perf_event__attr_swap(buf);
 
                memcpy(&evsel->attr, buf, msz);
 
-               ret = readn(fd, &nr, sizeof(nr));
-               if (ret != (ssize_t)sizeof(nr))
+               if (do_read_u32(fd, &nr))
                        goto error;
 
-               if (ph->needs_swap) {
-                       nr = bswap_32(nr);
+               if (fd->ph->needs_swap)
                        evsel->needs_swap = true;
-               }
 
-               evsel->name = do_read_string(fd, ph);
+               evsel->name = do_read_string(fd);
 
                if (!nr)
                        continue;
@@ -1283,11 +1311,8 @@ read_event_desc(struct perf_header *ph, int fd)
                evsel->id = id;
 
                for (j = 0 ; j < nr; j++) {
-                       ret = readn(fd, id, sizeof(*id));
-                       if (ret != (ssize_t)sizeof(*id))
+                       if (do_read_u64(fd, id))
                                goto error;
-                       if (ph->needs_swap)
-                               *id = bswap_64(*id);
                        id++;
                }
        }
@@ -1308,7 +1333,7 @@ static int __desc_attr__fprintf(FILE *fp, const char 
*name, const char *val,
 
 static void print_event_desc(struct feat_fd *fd, FILE *fp)
 {
-       struct perf_evsel *evsel, *events = read_event_desc(fd->ph, fd->fd);
+       struct perf_evsel *evsel, *events = read_event_desc(fd);
        u32 j;
        u64 *id;
 
@@ -1598,113 +1623,61 @@ static int perf_header__read_build_ids(struct 
perf_header *header,
        return err;
 }
 
-static int process_tracing_data(struct perf_file_section *section 
__maybe_unused,
-                               struct perf_header *ph __maybe_unused,
-                               int fd, void *data)
-{
-       ssize_t ret = trace_report(fd, data, false);
-       return ret < 0 ? -1 : 0;
+/* Macro for features that simply need to read and store a string. */
+#define FEAT_PROCESS_STR_FUN(__feat, __feat_env) \
+static int process_##__feat(struct feat_fd *fd, void *data __maybe_unused) \
+{\
+       fd->ph->env.__feat_env = do_read_string(fd); \
+       return fd->ph->env.__feat_env ? 0 : -ENOMEM; \
 }
 
-static int process_build_id(struct perf_file_section *section,
-                           struct perf_header *ph, int fd,
-                           void *data __maybe_unused)
+FEAT_PROCESS_STR_FUN(hostname, hostname);
+FEAT_PROCESS_STR_FUN(osrelease, os_release);
+FEAT_PROCESS_STR_FUN(version, version);
+FEAT_PROCESS_STR_FUN(arch, arch);
+FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc);
+FEAT_PROCESS_STR_FUN(cpuid, cpuid);
+
+static int process_build_id(struct feat_fd *fd, void *data __maybe_unused)
 {
-       if (perf_header__read_build_ids(ph, fd, section->offset, section->size))
+       if (perf_header__read_build_ids(fd->ph, fd->fd, fd->offset, fd->size))
                pr_debug("Failed to read buildids, continuing...\n");
        return 0;
 }
 
-static int process_hostname(struct perf_file_section *section __maybe_unused,
-                           struct perf_header *ph, int fd,
-                           void *data __maybe_unused)
-{
-       ph->env.hostname = do_read_string(fd, ph);
-       return ph->env.hostname ? 0 : -ENOMEM;
-}
-
-static int process_osrelease(struct perf_file_section *section __maybe_unused,
-                            struct perf_header *ph, int fd,
-                            void *data __maybe_unused)
-{
-       ph->env.os_release = do_read_string(fd, ph);
-       return ph->env.os_release ? 0 : -ENOMEM;
-}
-
-static int process_version(struct perf_file_section *section __maybe_unused,
-                          struct perf_header *ph, int fd,
-                          void *data __maybe_unused)
+static int process_tracing_data(struct feat_fd *fd, void *data)
 {
-       ph->env.version = do_read_string(fd, ph);
-       return ph->env.version ? 0 : -ENOMEM;
-}
+       ssize_t ret = trace_report(fd->fd, data, false);
 
-static int process_arch(struct perf_file_section *section __maybe_unused,
-                       struct perf_header *ph, int fd,
-                       void *data __maybe_unused)
-{
-       ph->env.arch = do_read_string(fd, ph);
-       return ph->env.arch ? 0 : -ENOMEM;
+       return ret < 0 ? -1 : 0;
 }
 
-static int process_nrcpus(struct perf_file_section *section __maybe_unused,
-                         struct perf_header *ph, int fd,
-                         void *data __maybe_unused)
+static int process_nrcpus(struct feat_fd *fd, void *data __maybe_unused)
 {
-       ssize_t ret;
-       u32 nr;
-
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
-               return -1;
-
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
-
-       ph->env.nr_cpus_avail = nr;
-
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
-               return -1;
+       int ret;
+       u32 nr_cpus_avail, nr_cpus_online;
 
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
+       ret = do_read_u32(fd, &nr_cpus_avail);
+       if (ret)
+               return ret;
 
-       ph->env.nr_cpus_online = nr;
+       ret = do_read_u32(fd, &nr_cpus_online);
+       if (ret)
+               return ret;
+       fd->ph->env.nr_cpus_avail = (int)nr_cpus_avail;
+       fd->ph->env.nr_cpus_online = (int)nr_cpus_online;
        return 0;
 }
 
-static int process_cpudesc(struct perf_file_section *section __maybe_unused,
-                          struct perf_header *ph, int fd,
-                          void *data __maybe_unused)
-{
-       ph->env.cpu_desc = do_read_string(fd, ph);
-       return ph->env.cpu_desc ? 0 : -ENOMEM;
-}
-
-static int process_cpuid(struct perf_file_section *section __maybe_unused,
-                        struct perf_header *ph,  int fd,
-                        void *data __maybe_unused)
-{
-       ph->env.cpuid = do_read_string(fd, ph);
-       return ph->env.cpuid ? 0 : -ENOMEM;
-}
-
-static int process_total_mem(struct perf_file_section *section __maybe_unused,
-                            struct perf_header *ph, int fd,
-                            void *data __maybe_unused)
+static int process_total_mem(struct feat_fd *fd, void *data __maybe_unused)
 {
-       uint64_t mem;
-       ssize_t ret;
+       u64 total_mem;
+       int ret;
 
-       ret = readn(fd, &mem, sizeof(mem));
-       if (ret != sizeof(mem))
+       ret = do_read_u64(fd, &total_mem);
+       if (ret)
                return -1;
-
-       if (ph->needs_swap)
-               mem = bswap_64(mem);
-
-       ph->env.total_mem = mem;
+       fd->ph->env.total_mem = (unsigned long long)total_mem;
        return 0;
 }
 
@@ -1741,17 +1714,15 @@ perf_evlist__set_event_name(struct perf_evlist *evlist,
 }
 
 static int
-process_event_desc(struct perf_file_section *section __maybe_unused,
-                  struct perf_header *header, int fd,
-                  void *data __maybe_unused)
+process_event_desc(struct feat_fd *fd, void *data __maybe_unused)
 {
        struct perf_session *session;
-       struct perf_evsel *evsel, *events = read_event_desc(header, fd);
+       struct perf_evsel *evsel, *events = read_event_desc(fd);
 
        if (!events)
                return 0;
 
-       session = container_of(header, struct perf_session, header);
+       session = container_of(fd->ph, struct perf_session, header);
        for (evsel = events; evsel->attr.size; evsel++)
                perf_evlist__set_event_name(session->evlist, evsel);
 
@@ -1760,24 +1731,17 @@ process_event_desc(struct perf_file_section *section 
__maybe_unused,
        return 0;
 }
 
-static int process_cmdline(struct perf_file_section *section,
-                          struct perf_header *ph, int fd,
-                          void *data __maybe_unused)
+static int process_cmdline(struct feat_fd *fd, void *data __maybe_unused)
 {
-       ssize_t ret;
        char *str, *cmdline = NULL, **argv = NULL;
        u32 nr, i, len = 0;
 
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
+       if (do_read_u32(fd, &nr))
                return -1;
 
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
-
-       ph->env.nr_cmdline = nr;
+       fd->ph->env.nr_cmdline = nr;
 
-       cmdline = zalloc(section->size + nr + 1);
+       cmdline = zalloc(fd->size + nr + 1);
        if (!cmdline)
                return -1;
 
@@ -1786,7 +1750,7 @@ static int process_cmdline(struct perf_file_section 
*section,
                goto error;
 
        for (i = 0; i < nr; i++) {
-               str = do_read_string(fd, ph);
+               str = do_read_string(fd);
                if (!str)
                        goto error;
 
@@ -1795,8 +1759,8 @@ static int process_cmdline(struct perf_file_section 
*section,
                len += strlen(str) + 1;
                free(str);
        }
-       ph->env.cmdline = cmdline;
-       ph->env.cmdline_argv = (const char **) argv;
+       fd->ph->env.cmdline = cmdline;
+       fd->ph->env.cmdline_argv = (const char **) argv;
        return 0;
 
 error:
@@ -1805,65 +1769,51 @@ static int process_cmdline(struct perf_file_section 
*section,
        return -1;
 }
 
-static int process_cpu_topology(struct perf_file_section *section,
-                               struct perf_header *ph, int fd,
-                               void *data __maybe_unused)
+static int process_cpu_topology(struct feat_fd *fd, void *data __maybe_unused)
 {
-       ssize_t ret;
        u32 nr, i;
        char *str;
        struct strbuf sb;
-       int cpu_nr = ph->env.nr_cpus_avail;
-       u64 size = 0;
+       int cpu_nr = fd->ph->env.nr_cpus_avail;
+       struct perf_header *ph = fd->ph;
+       u64 start_offset = fd->offset;
 
        ph->env.cpu = calloc(cpu_nr, sizeof(*ph->env.cpu));
        if (!ph->env.cpu)
                return -1;
 
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
+       if (do_read_u32(fd, &nr))
                goto free_cpu;
 
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
-
        ph->env.nr_sibling_cores = nr;
-       size += sizeof(u32);
        if (strbuf_init(&sb, 128) < 0)
                goto free_cpu;
 
        for (i = 0; i < nr; i++) {
-               str = do_read_string(fd, ph);
+               str = do_read_string(fd);
                if (!str)
                        goto error;
 
                /* include a NULL character at the end */
                if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
                        goto error;
-               size += string_size(str);
                free(str);
        }
        ph->env.sibling_cores = strbuf_detach(&sb, NULL);
 
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
+       if (do_read_u32(fd, &nr))
                return -1;
 
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
-
        ph->env.nr_sibling_threads = nr;
-       size += sizeof(u32);
 
        for (i = 0; i < nr; i++) {
-               str = do_read_string(fd, ph);
+               str = do_read_string(fd);
                if (!str)
                        goto error;
 
                /* include a NULL character at the end */
                if (strbuf_add(&sb, str, strlen(str) + 1) < 0)
                        goto error;
-               size += string_size(str);
                free(str);
        }
        ph->env.sibling_threads = strbuf_detach(&sb, NULL);
@@ -1872,28 +1822,20 @@ static int process_cpu_topology(struct 
perf_file_section *section,
         * The header may be from old perf,
         * which doesn't include core id and socket id information.
         */
-       if (section->size <= size) {
+       if (fd->size <= fd->offset - start_offset) {
                zfree(&ph->env.cpu);
                return 0;
        }
 
        for (i = 0; i < (u32)cpu_nr; i++) {
-               ret = readn(fd, &nr, sizeof(nr));
-               if (ret != sizeof(nr))
+               if (do_read_u32(fd, &nr))
                        goto free_cpu;
 
-               if (ph->needs_swap)
-                       nr = bswap_32(nr);
-
                ph->env.cpu[i].core_id = nr;
 
-               ret = readn(fd, &nr, sizeof(nr));
-               if (ret != sizeof(nr))
+               if (do_read_u32(fd, &nr))
                        goto free_cpu;
 
-               if (ph->needs_swap)
-                       nr = bswap_32(nr);
-
                if (nr != (u32)-1 && nr > (u32)cpu_nr) {
                        pr_debug("socket_id number is too big."
                                 "You may need to upgrade the perf tool.\n");
@@ -1912,23 +1854,17 @@ static int process_cpu_topology(struct 
perf_file_section *section,
        return -1;
 }
 
-static int process_numa_topology(struct perf_file_section *section 
__maybe_unused,
-                                struct perf_header *ph, int fd,
-                                void *data __maybe_unused)
+static int process_numa_topology(struct feat_fd *fd, void *data __maybe_unused)
 {
+       struct perf_header *ph = fd->ph;
        struct numa_node *nodes, *n;
-       ssize_t ret;
        u32 nr, i;
        char *str;
 
        /* nr nodes */
-       ret = readn(fd, &nr, sizeof(nr));
-       if (ret != sizeof(nr))
+       if (do_read_u32(fd, &nr))
                return -1;
 
-       if (ph->needs_swap)
-               nr = bswap_32(nr);
-
        nodes = zalloc(sizeof(*nodes) * nr);
        if (!nodes)
                return -ENOMEM;
@@ -1937,25 +1873,16 @@ static int process_numa_topology(struct 
perf_file_section *section __maybe_unuse
                n = &nodes[i];
 
                /* node number */
-               ret = readn(fd, &n->node, sizeof(u32));
-               if (ret != sizeof(n->node))
+               if (do_read_u32(fd, &n->node))
                        goto error;
 
-               ret = readn(fd, &n->mem_total, sizeof(u64));
-               if (ret != sizeof(u64))
+               if (do_read_u64(fd, &n->mem_total))
                        goto error;
 
-               ret = readn(fd, &n->mem_free, sizeof(u64));
-               if (ret != sizeof(u64))
+               if (do_read_u64(fd, &n->mem_free))
                        goto error;
 
-               if (ph->needs_swap) {
-                       n->node      = bswap_32(n->node);
-                       n->mem_total = bswap_64(n->mem_total);
-                       n->mem_free  = bswap_64(n->mem_free);
-               }
-
-               str = do_read_string(fd, ph);
+               str = do_read_string(fd);
                if (!str)
                        goto error;
 
@@ -1974,23 +1901,17 @@ static int process_numa_topology(struct 
perf_file_section *section __maybe_unuse
        return -1;
 }
 
-static int process_pmu_mappings(struct perf_file_section *section 
__maybe_unused,
-                               struct perf_header *ph, int fd,
-                               void *data __maybe_unused)
+static int process_pmu_mappings(struct feat_fd *fd, void *data __maybe_unused)
 {
-       ssize_t ret;
+       struct perf_header *ph = fd->ph;
        char *name;
        u32 pmu_num;
        u32 type;
        struct strbuf sb;
 
-       ret = readn(fd, &pmu_num, sizeof(pmu_num));
-       if (ret != sizeof(pmu_num))
+       if (do_read_u32(fd, &pmu_num))
                return -1;
 
-       if (ph->needs_swap)
-               pmu_num = bswap_32(pmu_num);
-
        if (!pmu_num) {
                pr_debug("pmu mappings not available\n");
                return 0;
@@ -2001,12 +1922,10 @@ static int process_pmu_mappings(struct 
perf_file_section *section __maybe_unused
                return -1;
 
        while (pmu_num) {
-               if (readn(fd, &type, sizeof(type)) != sizeof(type))
+               if (do_read_u32(fd, &type))
                        goto error;
-               if (ph->needs_swap)
-                       type = bswap_32(type);
 
-               name = do_read_string(fd, ph);
+               name = do_read_string(fd);
                if (!name)
                        goto error;
 
@@ -2030,10 +1949,9 @@ static int process_pmu_mappings(struct perf_file_section 
*section __maybe_unused
        return -1;
 }
 
-static int process_group_desc(struct perf_file_section *section __maybe_unused,
-                             struct perf_header *ph, int fd,
-                             void *data __maybe_unused)
+static int process_group_desc(struct feat_fd *fd, void *data __maybe_unused)
 {
+       struct perf_header *ph = fd->ph;
        size_t ret = -1;
        u32 i, nr, nr_groups;
        struct perf_session *session;
@@ -2044,12 +1962,9 @@ static int process_group_desc(struct perf_file_section 
*section __maybe_unused,
                u32 nr_members;
        } *desc;
 
-       if (readn(fd, &nr_groups, sizeof(nr_groups)) != sizeof(nr_groups))
+       if (do_read_u32(fd, &nr_groups))
                return -1;
 
-       if (ph->needs_swap)
-               nr_groups = bswap_32(nr_groups);
-
        ph->env.nr_groups = nr_groups;
        if (!nr_groups) {
                pr_debug("group desc not available\n");
@@ -2061,20 +1976,15 @@ static int process_group_desc(struct perf_file_section 
*section __maybe_unused,
                return -1;
 
        for (i = 0; i < nr_groups; i++) {
-               desc[i].name = do_read_string(fd, ph);
+               desc[i].name = do_read_string(fd);
                if (!desc[i].name)
                        goto out_free;
 
-               if (readn(fd, &desc[i].leader_idx, sizeof(u32)) != sizeof(u32))
+               if (do_read_u32(fd, &desc[i].leader_idx))
                        goto out_free;
 
-               if (readn(fd, &desc[i].nr_members, sizeof(u32)) != sizeof(u32))
+               if (do_read_u32(fd, &desc[i].nr_members))
                        goto out_free;
-
-               if (ph->needs_swap) {
-                       desc[i].leader_idx = bswap_32(desc[i].leader_idx);
-                       desc[i].nr_members = bswap_32(desc[i].nr_members);
-               }
        }
 
        /*
@@ -2124,44 +2034,35 @@ static int process_group_desc(struct perf_file_section 
*section __maybe_unused,
        return ret;
 }
 
-static int process_auxtrace(struct perf_file_section *section,
-                           struct perf_header *ph, int fd,
-                           void *data __maybe_unused)
+static int process_auxtrace(struct feat_fd *fd, void *data __maybe_unused)
 {
        struct perf_session *session;
        int err;
 
-       session = container_of(ph, struct perf_session, header);
+       session = container_of(fd->ph, struct perf_session, header);
 
-       err = auxtrace_index__process(fd, section->size, session,
-                                     ph->needs_swap);
+       err = auxtrace_index__process(fd->fd, fd->size, session,
+                                     fd->ph->needs_swap);
        if (err < 0)
                pr_err("Failed to process auxtrace index\n");
        return err;
 }
 
-static int process_cache(struct perf_file_section *section __maybe_unused,
-                        struct perf_header *ph __maybe_unused, int fd 
__maybe_unused,
-                        void *data __maybe_unused)
+static int process_cache(struct feat_fd *fd, void *data __maybe_unused)
 {
        struct cpu_cache_level *caches;
        u32 cnt, i, version;
 
-       if (readn(fd, &version, sizeof(version)) != sizeof(version))
+       pr_err("process_cache\n");
+       if (do_read_u32(fd, &version))
                return -1;
 
-       if (ph->needs_swap)
-               version = bswap_32(version);
-
        if (version != 1)
                return -1;
 
-       if (readn(fd, &cnt, sizeof(cnt)) != sizeof(cnt))
+       if (do_read_u32(fd, &cnt))
                return -1;
 
-       if (ph->needs_swap)
-               cnt = bswap_32(cnt);
-
        caches = zalloc(sizeof(*caches) * cnt);
        if (!caches)
                return -1;
@@ -2170,10 +2071,8 @@ static int process_cache(struct perf_file_section 
*section __maybe_unused,
                struct cpu_cache_level c;
 
                #define _R(v)                                           \
-                       if (readn(fd, &c.v, sizeof(u32)) != sizeof(u32))\
+                       if (do_read_u32(fd, &c.v))\
                                goto out_free_caches;                   \
-                       if (ph->needs_swap)                             \
-                               c.v = bswap_32(c.v);                    \
 
                _R(level)
                _R(line_size)
@@ -2182,7 +2081,7 @@ static int process_cache(struct perf_file_section 
*section __maybe_unused,
                #undef _R
 
                #define _R(v)                           \
-                       c.v = do_read_string(fd, ph);   \
+                       c.v = do_read_string(fd);       \
                        if (!c.v)                       \
                                goto out_free_caches;
 
@@ -2194,8 +2093,8 @@ static int process_cache(struct perf_file_section 
*section __maybe_unused,
                caches[i] = c;
        }
 
-       ph->env.caches = caches;
-       ph->env.caches_cnt = cnt;
+       fd->ph->env.caches = caches;
+       fd->ph->env.caches_cnt = cnt;
        return 0;
 out_free_caches:
        free(caches);
@@ -2205,8 +2104,7 @@ static int process_cache(struct perf_file_section 
*section __maybe_unused,
 struct feature_ops {
        int (*write)(struct feat_fd *fd, struct perf_evlist *evlist);
        void (*print)(struct feat_fd *fd, FILE *fp);
-       int (*process)(struct perf_file_section *section,
-                      struct perf_header *h, int fd, void *data);
+       int (*process)(struct feat_fd *fd, void *data);
        const char *name;
        bool full_only;
 };
@@ -2747,6 +2645,13 @@ static int perf_file_section__process(struct 
perf_file_section *section,
                                      struct perf_header *ph,
                                      int feat, int fd, void *data)
 {
+       struct feat_fd fdd = {
+               .fd = fd,
+               .ph = ph,
+               .size = section->size,
+               .offset = section->offset,
+       };
+
        if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) {
                pr_debug("Failed to lseek to %" PRIu64 " offset for feature "
                          "%d, continuing...\n", section->offset, feat);
@@ -2761,7 +2666,7 @@ static int perf_file_section__process(struct 
perf_file_section *section,
        if (!feat_ops[feat].process)
                return 0;
 
-       return feat_ops[feat].process(section, ph, fd, data);
+       return feat_ops[feat].process(&fdd, data);
 }
 
 static int perf_file_header__read_pipe(struct perf_pipe_file_header *header,
diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h
index 9d8dcd5eb727..86ab723cbee4 100644
--- a/tools/perf/util/header.h
+++ b/tools/perf/util/header.h
@@ -78,6 +78,7 @@ struct perf_header {
        struct perf_env         env;
 };
 
+struct feat_fd;
 struct perf_evlist;
 struct perf_session;
 
-- 
2.13.0.303.g4ebf302169-goog

Reply via email to