BonoboStreamMem changes



BonoboStreamMem isn't very well designed for writing. First, you have
to pass it a buffer which it doesn't use, and second, you have to know
in advance how much data is going to be written to the stream.

This patches changes a few things:

	- If you pass in a buffer, it will use it rather than making
	  an exact copy of it and using the copy.

	- If you don't pass in a buffer (and don't set read_only),
	  it will create its own buffer, and later on the write
	  and seek ops will realloc the buffer as needed to fit
	  whatever data is written to it.

	- There's now an accessor function for the buffer.


OK, the dubious part: there is a silent behavior change (the fact that
the buffer passed in to camel_stream_mem_create is now absorbed into
the stream rather than being copied and then ignored). However, I
could only find one use of BonoboStreamMem in the existing bonobo,
evolution, nautilus, and gnumeric code, and that was a read-only
stream, which isn't affected by the behavior change. Are there other
places I should check? I'm guessing that no one is using writable
BonoboStreamMems right now, because they're not very useful.

Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/bonobo/ChangeLog,v
retrieving revision 1.348
diff -u -r1.348 ChangeLog
--- ChangeLog	2000/04/14 14:51:33	1.348
+++ ChangeLog	2000/04/14 15:07:37
@@ -1,3 +1,12 @@
+2000-04-14  Dan Winship  <danw@helixcode.com>
+
+	* bonobo/bonobo-stream-memory.c (bonobo_stream_mem_create): Always
+	use the provided buffer, rather than copying it to a new buffer in
+	the !read_only case. Allow the caller to pass NULL for buffer,
+	meaning "allocate enough space to fit everything written to it".
+	(mem_write, mem_seek): expand buffer when needed.
+	(bonobo_stream_mem_get_buffer): new accessor.
+
 2000-04-14  Michael Meeks  <michael@helixcode.com>
 
 	* bonobo/bonobo-print.c (impl_render): kill thinko NULLing user_data pointer.
Index: bonobo/bonobo-stream-memory.c
===================================================================
RCS file: /cvs/gnome/bonobo/bonobo/bonobo-stream-memory.c,v
retrieving revision 1.20
diff -u -r1.20 bonobo-stream-memory.c
--- bonobo/bonobo-stream-memory.c	2000/03/05 08:30:52	1.20
+++ bonobo/bonobo-stream-memory.c	2000/04/14 15:07:37
@@ -52,8 +52,13 @@
 	}
 
 	if (smem->pos + len > smem->size){
-		mem_truncate (stream, smem->pos + len, ev);
-		g_warning ("Should check for an exception here");
+		if (smem->resizable){
+			smem->size = smem->pos + len;
+			smem->buffer = g_realloc (smem->buffer, smem->size);
+		} else {
+			mem_truncate (stream, smem->pos + len, ev);
+			g_warning ("Should check for an exception here");
+		}
 	}
 
 	if (smem->pos + len > smem->size)
@@ -113,7 +118,11 @@
 	}
 
 	if (pos > smem->size){
-		mem_truncate (stream, pos, ev);
+		if (smem->resizable){
+			smem->size = pos;
+			smem->buffer = g_realloc (smem->buffer, smem->size);
+		} else
+			mem_truncate (stream, pos, ev);
 	}
 	smem->pos = pos;
 	return pos;
@@ -245,7 +254,8 @@
 
 /**
  * bonobo_stream_mem_create:
- * @buffer: The memory buffer for which a BonoboStreamMem object is to be created.
+ * @buffer: The memory buffer for which a BonoboStreamMem object is to be
+ * created.
  * @size: The size in bytes of @buffer.
  * @read_only: Specifies whether or not the returned BonoboStreamMem
  * object should allow write() operations.
@@ -255,49 +265,60 @@
  * out of or written into the returned BonoboStream object,
  * the read() and write() operations operate on @buffer.
  *
- * Returns: the constructed BonoboStream object which operates on the specified memory buffer.
+ * If @buffer is #NULL, the stream will automatically allocate enough
+ * memory to fit any data written to it. In this case, @size is
+ * interpreted as a hint for a good initial size. It may be 0.
+ *
+ * Returns: the constructed BonoboStream object
  */
 BonoboStream *
 bonobo_stream_mem_create (char *buffer, size_t size, gboolean read_only)
 {
 	BonoboStreamMem *stream_mem;
 	Bonobo_Stream corba_stream;
-	char *copy;
-	
-	g_return_val_if_fail (buffer != NULL, NULL);
 
-	if (read_only)
-		copy = buffer;
-	else {
-		copy = g_malloc (size);
-		if (!copy)
-			return NULL;
-		memcpy (copy, buffer, size);
-	}
-	
+	g_return_val_if_fail (!read_only || buffer != NULL, NULL);
+
 	stream_mem = gtk_type_new (bonobo_stream_mem_get_type ());
-	if (stream_mem == NULL){
-		g_free (copy);
+	if (stream_mem == NULL)
 		return NULL;
-	}
+
+	if (buffer == NULL) {
+		stream_mem->resizable = TRUE;
+		buffer = g_malloc (size);
+		memset (buffer, 0, size);
+	} else
+		stream_mem->resizable = FALSE;
 
-	stream_mem->buffer = copy;
+	stream_mem->buffer = buffer;
 	stream_mem->size = size;
 	stream_mem->pos = 0;
 	stream_mem->read_only = read_only;
 	
-	corba_stream = create_bonobo_stream_mem (
-		BONOBO_OBJECT (stream_mem));
+	corba_stream = create_bonobo_stream_mem (BONOBO_OBJECT (stream_mem));
 
 	if (corba_stream == CORBA_OBJECT_NIL){
 		gtk_object_destroy (GTK_OBJECT (stream_mem));
 		return NULL;
 	}
 
-	bonobo_object_construct (
-		BONOBO_OBJECT (stream_mem), corba_stream);
+	bonobo_object_construct (BONOBO_OBJECT (stream_mem), corba_stream);
 	return BONOBO_STREAM (stream_mem);
 }
 
-
-
+/**
+ * bonobo_stream_mem_get_buffer:
+ * @stream_mem: a BonoboStreamMem
+ *
+ * Returns the buffer associated with a BonoboStreamMem. If the stream
+ * is set to automatically resize itself, this buffer is only guaranteed
+ * to stay valid until the next write operation on the stream.
+ *
+ * Return value: a buffer containing the data written to the stream (or
+ * the data the stream was initialized with if nothing has been written).
+ **/
+const char *
+bonobo_stream_mem_get_buffer (BonoboStreamMem *stream_mem)
+{
+	return stream_mem->buffer;
+}
Index: bonobo/bonobo-stream-memory.h
===================================================================
RCS file: /cvs/gnome/bonobo/bonobo/bonobo-stream-memory.h,v
retrieving revision 1.5
diff -u -r1.5 bonobo-stream-memory.h
--- bonobo/bonobo-stream-memory.h	2000/01/25 11:35:45	1.5
+++ bonobo/bonobo-stream-memory.h	2000/04/14 15:07:37
@@ -28,6 +28,7 @@
 	size_t       size;
 	long         pos;
 	gboolean     read_only;
+	gboolean     resizable;
 
 	BonoboStreamMemPrivate *priv;
 };
@@ -37,7 +38,8 @@
 } BonoboStreamMemClass;
 
 GtkType         bonobo_stream_mem_get_type     (void);
-BonoboStream    *bonobo_stream_mem_create       (char *buffer, size_t size, gboolean read_only);
+BonoboStream   *bonobo_stream_mem_create       (char *buffer, size_t size, gboolean read_only);
+const char     *bonobo_stream_mem_get_buffer   (BonoboStreamMem *stream_mem);
 
 END_GNOME_DECLS
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]