changeset 19835:c2999b133d37

lib: Added iostream_temp_create_sized() to specify the max in-memory size
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 23 Feb 2016 23:41:28 +0200
parents 785dec1e0a0e
children 0090a7192286
files src/lib/iostream-temp.c src/lib/iostream-temp.h
diffstat 2 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/iostream-temp.c	Tue Feb 23 18:41:35 2016 +0200
+++ b/src/lib/iostream-temp.c	Tue Feb 23 23:41:28 2016 +0200
@@ -11,13 +11,14 @@
 
 #include <unistd.h>
 
-#define IOSTREAM_TEMP_MAX_BUF_SIZE (1024*128)
+#define IOSTREAM_TEMP_MAX_BUF_SIZE_DEFAULT (1024*128)
 
 struct temp_ostream {
 	struct ostream_private ostream;
 
 	char *temp_path_prefix;
 	enum iostream_temp_flags flags;
+	size_t max_mem_size;
 
 	struct istream *dupstream;
 	uoff_t dupstream_offset, dupstream_start_offset;
@@ -105,7 +106,7 @@
 		return o_stream_temp_fd_sendv(tstream, iov, iov_count);
 
 	for (i = 0; i < iov_count; i++) {
-		if (tstream->buf->used + iov[i].iov_len > IOSTREAM_TEMP_MAX_BUF_SIZE) {
+		if (tstream->buf->used + iov[i].iov_len > tstream->max_mem_size) {
 			if (o_stream_temp_move_to_fd(tstream) == 0) {
 				return o_stream_temp_fd_sendv(tstream, iov+i,
 							      iov_count-i);
@@ -222,6 +223,15 @@
 					   enum iostream_temp_flags flags,
 					   const char *name)
 {
+	return iostream_temp_create_sized(temp_path_prefix, flags, name,
+					  IOSTREAM_TEMP_MAX_BUF_SIZE_DEFAULT);
+}
+
+struct ostream *iostream_temp_create_sized(const char *temp_path_prefix,
+					   enum iostream_temp_flags flags,
+					   const char *name,
+					   size_t max_mem_size)
+{
 	struct temp_ostream *tstream;
 	struct ostream *output;
 
@@ -232,6 +242,7 @@
 	tstream->ostream.iostream.close = o_stream_temp_close;
 	tstream->temp_path_prefix = i_strdup(temp_path_prefix);
 	tstream->flags = flags;
+	tstream->max_mem_size = max_mem_size;
 	tstream->buf = buffer_create_dynamic(default_pool, 8192);
 	tstream->fd = -1;
 
--- a/src/lib/iostream-temp.h	Tue Feb 23 18:41:35 2016 +0200
+++ b/src/lib/iostream-temp.h	Tue Feb 23 23:41:28 2016 +0200
@@ -15,8 +15,13 @@
 struct ostream *iostream_temp_create_named(const char *temp_path_prefix,
 					   enum iostream_temp_flags flags,
 					   const char *name);
+struct ostream *iostream_temp_create_sized(const char *temp_path_prefix,
+					   enum iostream_temp_flags flags,
+					   const char *name,
+					   size_t max_mem_size);
 /* Finished writing to stream. Return input stream for it and free the
-   output stream. */
+   output stream. (It's also possible to abort iostream-temp by simply
+   destroying the ostream.) */
 struct istream *iostream_temp_finish(struct ostream **output,
 				     size_t max_buffer_size);