]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add i_stream_try_alloc_avoid_compress()
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 27 Oct 2017 17:45:41 +0000 (20:45 +0300)
committerTimo Sirainen <tss@dovecot.fi>
Wed, 1 Nov 2017 11:45:51 +0000 (13:45 +0200)
src/lib/istream-private.h
src/lib/istream.c

index 14b57df9cee561064ea7175f0a51af275735888d..b86676b3aa75544f45c036e63d8b26d16afaed14 100644 (file)
@@ -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);
index 56711082ced3f940c47e473b4337fa40c5efaabe..dc0d3c74d5092564f006ba5f80f261940acde610 100644 (file)
@@ -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;