Re: [PATCH v2 10/13] perf header: add a buffer to struct feat_fd
From: Jiri Olsa
Date: Thu May 25 2017 - 04:09:59 EST
On Tue, May 23, 2017 at 12:48:50AM -0700, David Carrillo-Cisneros wrote:
SNIP
> @@ -82,11 +83,27 @@ bool perf_header__has_feat(const struct perf_header *header, int feat)
> /* Return: 0 if succeded, -ERR if failed. */
> int do_write(struct feat_fd *fd, const void *buf, size_t size)
> {
> - ssize_t ret;
> + void *addr;
>
> - ret = writen(fd->fd, buf, size);
> - if (ret != (ssize_t)size)
> - return ret < 0 ? (int)ret: -1;
> + if (!fd->buf) {
> + ssize_t ret = writen(fd->fd, buf, size);
> +
> + if (ret != (ssize_t)size)
> + return ret < 0 ? (int)ret : -1;
> + return 0;
> + }
> +retry:
> + if (size > (fd->size - fd->offset)) {
> + addr = realloc(fd->buf, fd->size << 1);
> + if (!addr)
> + return -ENOSPC;
> + fd->buf = addr;
> + fd->size <<= 1;
> + goto retry;
> + }
> +
> + memcpy(fd->buf + fd->offset, buf, size);
> + fd->offset += size;
>
> return 0;
please put those 2 cases in separate functions
> }
> @@ -126,10 +143,21 @@ static int do_write_string(struct feat_fd *fd, const char *str)
>
> static int __do_read(struct feat_fd *fd, void *addr, ssize_t size)
> {
> - ssize_t ret = readn(fd->fd, addr, 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;
>
> - if (ret != (ssize_t)size)
> - return ret < 0 ? (int)ret : -1;
> return 0;
> }
same for the read
thanks,
jirka