summaryrefslogtreecommitdiffstats
path: root/ft2232_spi.c
diff options
context:
space:
mode:
authorCarl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>2009-11-25 16:58:17 +0000
committerCarl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>2009-11-25 16:58:17 +0000
commit9f59a7b92c5fe33bfd34620d48868344be2ae69f (patch)
tree341176081702d03d5bfc3fc5eafbeea6618f5e0c /ft2232_spi.c
parent2ce45373bf00450356c2062df59264aca4c993ac (diff)
downloadflashrom-9f59a7b92c5fe33bfd34620d48868344be2ae69f.zip
flashrom-9f59a7b92c5fe33bfd34620d48868344be2ae69f.tar.gz
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 <c-d.hailfinger.devel.2006@gmx.net> Acked-by: Sean Nelson <audiohacked@gmail.com>
Diffstat (limited to 'ft2232_spi.c')
-rw-r--r--ft2232_spi.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/ft2232_spi.c b/ft2232_spi.c
index aa00753..d565a6f 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -200,14 +200,22 @@ int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
static unsigned char *buf = NULL;
/* failed is special. We use bitwise ops, but it is essentially bool. */
int i = 0, ret = 0, failed = 0;
+ int bufsize;
+ static int oldbufsize = 0;
if (writecnt > 65536 || readcnt > 65536)
return SPI_INVALID_LENGTH;
- buf = realloc(buf, writecnt + readcnt + 100);
- if (!buf) {
- fprintf(stderr, "Out of memory!\n");
- exit(1); // -1
+ /* buf is not used for the response from the chip. */
+ bufsize = max(writecnt + 9, 260 + 9);
+ /* Never shrink. realloc() calls are expensive. */
+ if (bufsize > oldbufsize) {
+ buf = realloc(buf, bufsize);
+ if (!buf) {
+ fprintf(stderr, "Out of memory!\n");
+ exit(1);
+ }
+ oldbufsize = bufsize;
}
/*
OpenPOWER on IntegriCloud