From: Timo Sirainen Date: Fri, 27 Oct 2017 17:45:41 +0000 (+0300) Subject: lib: Add i_stream_try_alloc_avoid_compress() X-Git-Tag: 2.3.0.rc1~640 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51d379cbc50242a13462d0fded50e013eb00cc07;p=thirdparty%2Fdovecot%2Fcore.git lib: Add i_stream_try_alloc_avoid_compress() --- diff --git a/src/lib/istream-private.h b/src/lib/istream-private.h index 14b57df9ce..b86676b3aa 100644 --- a/src/lib/istream-private.h +++ b/src/lib/istream-private.h @@ -77,6 +77,12 @@ void i_stream_grow_buffer(struct istream_private *stream, size_t bytes); bool ATTR_NOWARN_UNUSED_RESULT i_stream_try_alloc(struct istream_private *stream, size_t wanted_size, size_t *size_r); +/* Like i_stream_try_alloc(), but compress only if it's the only way to get + more space. This can be useful when stream is marked with + i_stream_seek_mark() */ +bool ATTR_NOWARN_UNUSED_RESULT +i_stream_try_alloc_avoid_compress(struct istream_private *stream, + size_t wanted_size, size_t *size_r); void *i_stream_alloc(struct istream_private *stream, size_t size); /* Free memory allocated by i_stream_*alloc() */ void i_stream_free_buffer(struct istream_private *stream); diff --git a/src/lib/istream.c b/src/lib/istream.c index 56711082ce..dc0d3c74d5 100644 --- a/src/lib/istream.c +++ b/src/lib/istream.c @@ -783,6 +783,22 @@ bool i_stream_try_alloc(struct istream_private *stream, return *size_r > 0; } +bool ATTR_NOWARN_UNUSED_RESULT +i_stream_try_alloc_avoid_compress(struct istream_private *stream, + size_t wanted_size, size_t *size_r) +{ + size_t old_skip = stream->skip; + + /* try first with skip=0, so no compression is done */ + stream->skip = 0; + bool ret = i_stream_try_alloc(stream, wanted_size, size_r); + stream->skip = old_skip; + if (ret || old_skip == 0) + return ret; + /* it's full. try with compression. */ + return i_stream_try_alloc(stream, wanted_size, size_r); +} + void *i_stream_alloc(struct istream_private *stream, size_t size) { size_t old_size, avail_size;