From a27aa33c254a0054af72147a5ce05d106df15251 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Fri, 15 Aug 2008 16:57:09 +0200 Subject: Simplify (and possibly reduce performance) of fz_readall(). --- stream/stm_misc.c | 88 ++++++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 66 deletions(-) (limited to 'stream/stm_misc.c') diff --git a/stream/stm_misc.c b/stream/stm_misc.c index b91b6c7e..70c1c315 100644 --- a/stream/stm_misc.c +++ b/stream/stm_misc.c @@ -59,83 +59,39 @@ fz_readline(fz_stream *stm, char *mem, int n) /* * Utility function to consume all the contents of an input stream into - * a freshly allocated buffer; realloced and trimmed to size. + * a freshly allocated buffer. */ -enum { CHUNKSIZE = 1024 * 4 }; - fz_error * -fz_readall(fz_buffer **bufp, fz_stream *stm) +fz_readall(fz_buffer **bufp, fz_stream *stm, int sizehint) { fz_error *error; - fz_buffer *real; - unsigned char *newbuf; - unsigned char *buf; - int len; - int pos; - int n; + fz_buffer *buf; + int c; - len = 0; - pos = 0; - buf = nil; + if (sizehint == 0) + sizehint = 4 * 1024; - while (1) - { - if (len - pos == 0) - { - if (len == 0) - len = CHUNKSIZE; - else - len = len * 2; - newbuf = fz_realloc(buf, len); - if (!newbuf) - { - fz_free(buf); - return fz_throw("outofmem: scratch buffer"); - } - buf = newbuf; - } + error = fz_newbuffer(&buf, sizehint); + if (error) + return fz_rethrow(error, "cannot create scratch buffer"); - error = fz_read(&n, stm, buf + pos, len - pos); - if (error) + for (c = fz_readbyte(stm); c != EOF; c = fz_readbyte(stm)) + { + if (buf->wp == buf->ep) { - fz_free(buf); - return fz_rethrow(error, "cannot read data"); + error = fz_growbuffer(buf); + if (error) + { + fz_dropbuffer(buf); + return fz_rethrow(error, "cannot resize scratch buffer"); + } } - pos += n; - - if (n < CHUNKSIZE) - { - if (pos > 0) - { - newbuf = fz_realloc(buf, pos); - if (!newbuf) - { - fz_free(buf); - return fz_throw("outofmem: scratch buffer"); - } - } - else newbuf = buf; - - real = fz_malloc(sizeof(fz_buffer)); - if (!real) - { - fz_free(newbuf); - return fz_throw("outofmem: buffer struct"); - } - - real->refs = 1; - real->ownsdata = 1; - real->bp = newbuf; - real->rp = newbuf; - real->wp = newbuf + pos; - real->ep = newbuf + pos; - real->eof = 1; - - *bufp = real; - return fz_okay; - } + *buf->wp++ = c; } + + *bufp = buf; + return fz_okay; } -- cgit v1.2.3