dovecot-2.2: lib: Added o_stream_add_destroy_callback()

dovecot at dovecot.org dovecot at dovecot.org
Sat Jan 17 02:16:10 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/4723cecff76c
changeset: 18164:4723cecff76c
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Jan 17 04:13:13 2015 +0200
description:
lib: Added o_stream_add_destroy_callback()

diffstat:

 src/lib/iostream-private.h |   4 ++++
 src/lib/iostream.c         |  28 ++++++++++++++++++++++++++++
 src/lib/istream.c          |  24 ++++--------------------
 src/lib/ostream.c          |  15 +++++++++++++++
 src/lib/ostream.h          |  12 ++++++++++++
 5 files changed, 63 insertions(+), 20 deletions(-)

diffs (145 lines):

diff -r 20acc7cc5b11 -r 4723cecff76c src/lib/iostream-private.h
--- a/src/lib/iostream-private.h	Sat Jan 17 02:40:11 2015 +0200
+++ b/src/lib/iostream-private.h	Sat Jan 17 04:13:13 2015 +0200
@@ -27,6 +27,10 @@
 void io_stream_close(struct iostream_private *stream, bool close_parent);
 void io_stream_set_max_buffer_size(struct iostream_private *stream,
 				   size_t max_size);
+void io_stream_add_destroy_callback(struct iostream_private *stream,
+				    void (*callback)(void *), void *context);
+void io_stream_remove_destroy_callback(struct iostream_private *stream,
+				       void (*callback)(void *));
 /* Set a specific error for the stream. This shouldn't be used for regular
    syscall errors where stream's errno is enough, since it's used by default.
    The stream errno must always be set even if the error string is also set.
diff -r 20acc7cc5b11 -r 4723cecff76c src/lib/iostream.c
--- a/src/lib/iostream.c	Sat Jan 17 02:40:11 2015 +0200
+++ b/src/lib/iostream.c	Sat Jan 17 04:13:13 2015 +0200
@@ -62,6 +62,34 @@
 	stream->set_max_buffer_size(stream, max_size);
 }
 
+void io_stream_add_destroy_callback(struct iostream_private *stream,
+				    void (*callback)(void *), void *context)
+{
+	struct iostream_destroy_callback *dc;
+
+	if (!array_is_created(&stream->destroy_callbacks))
+		i_array_init(&stream->destroy_callbacks, 2);
+	dc = array_append_space(&stream->destroy_callbacks);
+	dc->callback = callback;
+	dc->context = context;
+}
+
+void io_stream_remove_destroy_callback(struct iostream_private *stream,
+				       void (*callback)(void *))
+{
+	const struct iostream_destroy_callback *dcs;
+	unsigned int i, count;
+
+	dcs = array_get(&stream->destroy_callbacks, &count);
+	for (i = 0; i < count; i++) {
+		if (dcs[i].callback == callback) {
+			array_delete(&stream->destroy_callbacks, i, 1);
+			return;
+		}
+	}
+	i_unreached();
+}
+
 void io_stream_set_error(struct iostream_private *stream,
 			 const char *fmt, ...)
 {
diff -r 20acc7cc5b11 -r 4723cecff76c src/lib/istream.c
--- a/src/lib/istream.c	Sat Jan 17 02:40:11 2015 +0200
+++ b/src/lib/istream.c	Sat Jan 17 04:13:13 2015 +0200
@@ -58,31 +58,15 @@
 void i_stream_add_destroy_callback(struct istream *stream,
 				   istream_callback_t *callback, void *context)
 {
-	struct iostream_private *iostream = &stream->real_stream->iostream;
-	struct iostream_destroy_callback *dc;
-
-	if (!array_is_created(&iostream->destroy_callbacks))
-		i_array_init(&iostream->destroy_callbacks, 2);
-	dc = array_append_space(&iostream->destroy_callbacks);
-	dc->callback = callback;
-	dc->context = context;
+	io_stream_add_destroy_callback(&stream->real_stream->iostream,
+				       callback, context);
 }
 
 void i_stream_remove_destroy_callback(struct istream *stream,
 				      void (*callback)())
 {
-	struct iostream_private *iostream = &stream->real_stream->iostream;
-	const struct iostream_destroy_callback *dcs;
-	unsigned int i, count;
-
-	dcs = array_get(&iostream->destroy_callbacks, &count);
-	for (i = 0; i < count; i++) {
-		if (dcs[i].callback == (istream_callback_t *)callback) {
-			array_delete(&iostream->destroy_callbacks, i, 1);
-			return;
-		}
-	}
-	i_unreached();
+	io_stream_remove_destroy_callback(&stream->real_stream->iostream,
+					  callback);
 }
 
 int i_stream_get_fd(struct istream *stream)
diff -r 20acc7cc5b11 -r 4723cecff76c src/lib/ostream.c
--- a/src/lib/ostream.c	Sat Jan 17 02:40:11 2015 +0200
+++ b/src/lib/ostream.c	Sat Jan 17 04:13:13 2015 +0200
@@ -89,6 +89,21 @@
 	*_stream = NULL;
 }
 
+#undef o_stream_add_destroy_callback
+void o_stream_add_destroy_callback(struct ostream *stream,
+				   ostream_callback_t *callback, void *context)
+{
+	io_stream_add_destroy_callback(&stream->real_stream->iostream,
+				       callback, context);
+}
+
+void o_stream_remove_destroy_callback(struct ostream *stream,
+				      void (*callback)())
+{
+	io_stream_remove_destroy_callback(&stream->real_stream->iostream,
+					  callback);
+}
+
 void o_stream_close(struct ostream *stream)
 {
 	o_stream_close_full(stream, TRUE);
diff -r 20acc7cc5b11 -r 4723cecff76c src/lib/ostream.h
--- a/src/lib/ostream.h	Sat Jan 17 02:40:11 2015 +0200
+++ b/src/lib/ostream.h	Sat Jan 17 04:13:13 2015 +0200
@@ -25,6 +25,7 @@
    Pretty much the only real reason to return 0 is if you wish to send more
    data to client which isn't buffered, eg. o_stream_send_istream(). */
 typedef int stream_flush_callback_t(void *context);
+typedef void ostream_callback_t(void *context);
 
 /* Create new output stream from given file descriptor.
    If max_buffer_size is 0, an "optimal" buffer size is used (max 128kB). */
@@ -63,6 +64,17 @@
 void o_stream_ref(struct ostream *stream);
 /* Unreferences the stream and sets stream pointer to NULL. */
 void o_stream_unref(struct ostream **stream);
+/* Call the given callback function when stream is destroyed. */
+void o_stream_add_destroy_callback(struct ostream *stream,
+				   ostream_callback_t *callback, void *context)
+	ATTR_NULL(3);
+#define o_stream_add_destroy_callback(stream, callback, context) \
+	o_stream_add_destroy_callback(stream + \
+		CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \
+		(ostream_callback_t *)callback, context)
+/* Remove the destroy callback. */
+void o_stream_remove_destroy_callback(struct ostream *stream,
+				      void (*callback)());
 
 /* Mark the stream and all of its parent streams closed. Nothing will be
    sent after this call. */


More information about the dovecot-cvs mailing list