From 2b2de01e951c4b9db01c93c640d3ad0cc0b43af6 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Thu, 28 May 2009 12:26:16 +0000 Subject: [PATCH] *! Minimize the number of realloc()s done when reading zip cdir. This should presumably make reading of large zip archives much faster. --- src/libzip/zip_open.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/libzip/zip_open.c b/src/libzip/zip_open.c index 51430513..8b0cb4b2 100644 --- a/src/libzip/zip_open.c +++ b/src/libzip/zip_open.c @@ -232,14 +232,16 @@ _zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen, } // Instead of using nentry, just read all entries until one can't be read. - // And we're not gonna use + + int allocated = nentry; for (i=0; ; i++) { - if ( i >= nentry ) + if ( i >= allocated ) { // The original array won't hold -- reallocate - cd->entry = realloc( cd->entry, ( i + 1 ) * sizeof(*(cd->entry)) ); + allocated += 65536; + cd->entry = realloc( cd->entry, allocated * sizeof(*(cd->entry)) ); } if ((_zip_dirent_read(cd->entry+i, fp, bufp, eocd-cdp, 0, @@ -248,6 +250,11 @@ _zip_readcdir(FILE *fp, unsigned char *buf, unsigned char *eocd, int buflen, } } + // Deallocate the unneeded memory + + if ( allocated != i ) + cd->entry = realloc( cd->entry, i * sizeof(*(cd->entry)) ); + cd->nentry = i; return cd;