From b7e01457d12b64d98b0396f95e46489c02209982 Mon Sep 17 00:00:00 2001 From: Carl-Daniel Hailfinger Date: Wed, 25 Nov 2009 16:58:17 +0000 Subject: Reduce realloc syscall overhead for FT2232 and bitbang FT2232 ran realloc() for every executed command. Start with a big enough buffer and don't touch buffer size unless it needs to grow. Bitbang was slightly better: It only ran realloc() if buffer size changed. Still, the solution above improves performance and reliability. Corresponding to flashrom svn r780. Signed-off-by: Carl-Daniel Hailfinger Acked-by: Sean Nelson --- bitbang_spi.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'bitbang_spi.c') diff --git a/bitbang_spi.c b/bitbang_spi.c index 0f1f7ed..abf5530 100644 --- a/bitbang_spi.c +++ b/bitbang_spi.c @@ -87,14 +87,16 @@ int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, static unsigned char *bufout = NULL; static unsigned char *bufin = NULL; static int oldbufsize = 0; - int bufsize = max(writecnt + readcnt, 260); + int bufsize; int i; /* Arbitrary size limitation here. We're only constrained by memory. */ if (writecnt > 65536 || readcnt > 65536) return SPI_INVALID_LENGTH; - if (bufsize != oldbufsize) { + bufsize = max(writecnt + readcnt, 260); + /* Never shrink. realloc() calls are expensive. */ + if (bufsize > oldbufsize) { bufout = realloc(bufout, bufsize); if (!bufout) { fprintf(stderr, "Out of memory!\n"); @@ -109,6 +111,7 @@ int bitbang_spi_send_command(unsigned int writecnt, unsigned int readcnt, free(bufout); exit(1); } + oldbufsize = bufsize; } memcpy(bufout, writearr, writecnt); -- cgit v1.1