From 6bd1a1fe3ec12e177278303e6db0c79b1a14b631 Mon Sep 17 00:00:00 2001 From: Stewart Smith Date: Thu, 30 Nov 2017 18:43:56 +1100 Subject: fix high memory usage during any operation with '--buffer' The --buffer command line option was passed to setvbuf(3). Computers are fast enough and libc is good enough that we realistically do not need this. Hostboot's buildpnor.pl would pass --buffer 0x40000000 which resulted in a malloc(2GB), which would fail on systems with less than 2GB of memory because sometimes libc and the kernel are sane. So, we keep the command line option for backwards compatibility but completely ignore it. In the few places where it was used to allocate a buffer to do work in (and this was via a rather round-about way), we instead just allocate something the size of the PNOR image. Fixes: https://github.com/open-power/ffs/issues/7 Signed-off-by: Stewart Smith --- ffs/libffs.h | 7 ------- ffs/src/libffs.c | 49 ------------------------------------------------- 2 files changed, 56 deletions(-) (limited to 'ffs') diff --git a/ffs/libffs.h b/ffs/libffs.h index 2380a62..0b20baf 100644 --- a/ffs/libffs.h +++ b/ffs/libffs.h @@ -52,9 +52,6 @@ struct ffs { FILE * file; uint32_t count; - void * buf; // FILE* buffer - uint32_t buf_count; // multiple of hdr->block_size - bool dirty; }; @@ -76,7 +73,6 @@ typedef struct ffs_exception ffs_exception_t; #define FFS_INFO_ENTRY_COUNT 4 #define FFS_INFO_BLOCK_SIZE 5 #define FFS_INFO_BLOCK_COUNT 6 -#define FFS_INFO_BUFFER_COUNT 7 #define FFS_INFO_OFFSET 8 #define FFS_CHECK_PATH -3 @@ -109,9 +105,6 @@ extern ffs_t * __ffs_open(const char *, off_t) extern int __ffs_info(ffs_t *, int, uint32_t *) /*! @cond */ __nonnull ((1)) /*! @endcond */ ; -extern int __ffs_buffer(ffs_t *, size_t) -/*! @cond */ __nonnull ((1)) /*! @endcond */ ; - extern int __ffs_close(ffs_t *) /*! @cond */ __nonnull ((1)) /*! @endcond */ ; diff --git a/ffs/src/libffs.c b/ffs/src/libffs.c index 03ede55..707b0ee 100644 --- a/ffs/src/libffs.c +++ b/ffs/src/libffs.c @@ -583,25 +583,9 @@ ffs_t *__ffs_fopen(FILE * file, off_t offset) goto error; } - self->buf_count = 1; // default to 1 - - self->buf = malloc(self->buf_count * self->hdr->block_size); - if (self->hdr == NULL) { - ERRNO(errno); - goto error; - } - - if (setvbuf(self->file, self->buf, _IOFBF, - self->buf_count * self->hdr->block_size) != 0) { - ERRNO(errno); - goto error; - } - if (false) { error: if (self != NULL) { - if (self->buf != NULL) - free(self->buf), self->buf = NULL; if (self->hdr != NULL) free(self->hdr), self->hdr = NULL; @@ -670,9 +654,6 @@ int __ffs_info(ffs_t * self, int name, uint32_t *value) case FFS_INFO_BLOCK_COUNT: *value = self->hdr->block_count; break; - case FFS_INFO_BUFFER_COUNT: - *value = self->buf_count; - break; case FFS_INFO_OFFSET: *value = self->offset; break; @@ -684,34 +665,6 @@ int __ffs_info(ffs_t * self, int name, uint32_t *value) return 0; } -int __ffs_buffer(ffs_t * self, size_t size) -{ - assert(self != NULL); - - if (size == 0) - size = self->hdr->block_size; - - if (self->buf != NULL) { - free(self->buf); - self->buf_count = 0; - } - - self->buf_count = size / self->hdr->block_size; - size = self->buf_count * self->hdr->block_size; - - self->buf = malloc(size); - if (self->buf == NULL) { - ERRNO(errno); - return -1; - } - - if (setvbuf(self->file, self->buf, _IOFBF, size) < 0) { - ERRNO(errno); - return -1; - } - - return 0; -} int __ffs_fclose(ffs_t * self) { @@ -722,8 +675,6 @@ int __ffs_fclose(ffs_t * self) if (ffs_flush(self) < 0) return -1; - if (self->buf != NULL) - free(self->buf), self->buf = NULL; if (self->hdr != NULL) free(self->hdr), self->hdr = NULL; -- cgit v1.2.1