1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
21 * Christian Kellner <gicmo@gnome.org>
22 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
37 * SECTION:gmemoryoutputstream
38 * @short_description: Streaming output operations on memory chunks
40 * @see_also: #GMemoryInputStream
42 * #GMemoryOutputStream is a class for using arbitrary
43 * memory chunks as output for GIO streaming output operations.
47 #define MIN_ARRAY_SIZE 16
54 PROP_REALLOC_FUNCTION,
58 struct _GMemoryOutputStreamPrivate {
60 gpointer data; /* Write buffer */
61 gsize len; /* Current length of the data buffer. Can change with resizing. */
62 gsize valid_len; /* The part of data that has been written to */
63 gsize pos; /* Current position in the stream. Distinct from valid_len,
64 because the stream is seekable. */
66 GReallocFunc realloc_fn;
67 GDestroyNotify destroy;
70 static void g_memory_output_stream_set_property (GObject *object,
74 static void g_memory_output_stream_get_property (GObject *object,
78 static void g_memory_output_stream_finalize (GObject *object);
80 static gssize g_memory_output_stream_write (GOutputStream *stream,
83 GCancellable *cancellable,
86 static gboolean g_memory_output_stream_close (GOutputStream *stream,
87 GCancellable *cancellable,
90 static void g_memory_output_stream_write_async (GOutputStream *stream,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
97 static gssize g_memory_output_stream_write_finish (GOutputStream *stream,
100 static void g_memory_output_stream_close_async (GOutputStream *stream,
102 GCancellable *cancellable,
103 GAsyncReadyCallback callback,
105 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
106 GAsyncResult *result,
109 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
110 static goffset g_memory_output_stream_tell (GSeekable *seekable);
111 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
112 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
115 GCancellable *cancellable,
117 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
118 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
120 GCancellable *cancellable,
123 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
124 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
125 g_memory_output_stream_seekable_iface_init))
129 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
131 GOutputStreamClass *ostream_class;
132 GObjectClass *gobject_class;
134 g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
136 gobject_class = G_OBJECT_CLASS (klass);
137 gobject_class->set_property = g_memory_output_stream_set_property;
138 gobject_class->get_property = g_memory_output_stream_get_property;
139 gobject_class->finalize = g_memory_output_stream_finalize;
141 ostream_class = G_OUTPUT_STREAM_CLASS (klass);
143 ostream_class->write_fn = g_memory_output_stream_write;
144 ostream_class->close_fn = g_memory_output_stream_close;
145 ostream_class->write_async = g_memory_output_stream_write_async;
146 ostream_class->write_finish = g_memory_output_stream_write_finish;
147 ostream_class->close_async = g_memory_output_stream_close_async;
148 ostream_class->close_finish = g_memory_output_stream_close_finish;
151 * GMemoryOutputStream:data:
153 * Pointer to buffer where data will be written.
157 g_object_class_install_property (gobject_class,
159 g_param_spec_pointer ("data",
161 P_("Pointer to buffer where data will be written."),
162 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
163 G_PARAM_STATIC_STRINGS));
166 * GMemoryOutputStream:size:
168 * Current size of the data buffer.
172 g_object_class_install_property (gobject_class,
174 g_param_spec_ulong ("size",
175 P_("Data Buffer Size"),
176 P_("Current size of the data buffer."),
178 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
179 G_PARAM_STATIC_STRINGS));
182 * GMemoryOutputStream:data-size:
184 * Size of data written to the buffer.
188 g_object_class_install_property (gobject_class,
190 g_param_spec_ulong ("data-size",
192 P_("Size of data written to the buffer."),
195 G_PARAM_STATIC_STRINGS));
198 * GMemoryOutputStream:realloc-function:
200 * Function with realloc semantics called to enlarge the buffer.
204 g_object_class_install_property (gobject_class,
205 PROP_REALLOC_FUNCTION,
206 g_param_spec_pointer ("realloc-function",
207 P_("Memory Reallocation Function"),
208 P_("Function with realloc semantics called to enlarge the buffer."),
209 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
210 G_PARAM_STATIC_STRINGS));
213 * GMemoryOutputStream:destroy-function:
215 * Function called with the buffer as argument when the stream is destroyed.
219 g_object_class_install_property (gobject_class,
220 PROP_DESTROY_FUNCTION,
221 g_param_spec_pointer ("destroy-function",
222 P_("Destroy Notification Function"),
223 P_("Function called with the buffer as argument when the stream is destroyed."),
224 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
225 G_PARAM_STATIC_STRINGS));
229 g_memory_output_stream_set_property (GObject *object,
234 GMemoryOutputStream *stream;
235 GMemoryOutputStreamPrivate *priv;
237 stream = G_MEMORY_OUTPUT_STREAM (object);
243 priv->data = g_value_get_pointer (value);
246 priv->len = g_value_get_ulong (value);
248 case PROP_REALLOC_FUNCTION:
249 priv->realloc_fn = g_value_get_pointer (value);
251 case PROP_DESTROY_FUNCTION:
252 priv->destroy = g_value_get_pointer (value);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 g_memory_output_stream_get_property (GObject *object,
266 GMemoryOutputStream *stream;
267 GMemoryOutputStreamPrivate *priv;
269 stream = G_MEMORY_OUTPUT_STREAM (object);
275 g_value_set_pointer (value, priv->data);
278 g_value_set_ulong (value, priv->len);
281 g_value_set_ulong (value, priv->valid_len);
283 case PROP_REALLOC_FUNCTION:
284 g_value_set_pointer (value, priv->realloc_fn);
286 case PROP_DESTROY_FUNCTION:
287 g_value_set_pointer (value, priv->destroy);
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 g_memory_output_stream_finalize (GObject *object)
298 GMemoryOutputStream *stream;
299 GMemoryOutputStreamPrivate *priv;
301 stream = G_MEMORY_OUTPUT_STREAM (object);
305 priv->destroy (priv->data);
307 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
311 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
313 iface->tell = g_memory_output_stream_tell;
314 iface->can_seek = g_memory_output_stream_can_seek;
315 iface->seek = g_memory_output_stream_seek;
316 iface->can_truncate = g_memory_output_stream_can_truncate;
317 iface->truncate_fn = g_memory_output_stream_truncate;
322 g_memory_output_stream_init (GMemoryOutputStream *stream)
324 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
325 G_TYPE_MEMORY_OUTPUT_STREAM,
326 GMemoryOutputStreamPrivate);
327 stream->priv->pos = 0;
328 stream->priv->valid_len = 0;
332 * g_memory_output_stream_new:
333 * @data: pointer to a chunk of memory to use, or %NULL
334 * @size: the size of @data
335 * @realloc_function: a function with realloc() semantics (like g_realloc())
336 * to be called when @data needs to be grown, or %NULL
337 * @destroy_function: a function to be called on @data when the stream is
338 * finalized, or %NULL
340 * Creates a new #GMemoryOutputStream.
342 * If @data is non-%NULL, the stream will use that for its internal storage.
343 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
344 * storage when necessary. To construct a fixed-size output stream,
345 * pass %NULL as @realloc_fn.
348 * /* a stream that can grow */
349 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
351 * /* another stream that can grow */
352 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
354 * /* a fixed-size stream */
355 * data = malloc (200);
356 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
359 * Return value: A newly created #GMemoryOutputStream object.
362 g_memory_output_stream_new (gpointer data,
364 GReallocFunc realloc_function,
365 GDestroyNotify destroy_function)
367 GOutputStream *stream;
369 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
372 "realloc-function", realloc_function,
373 "destroy-function", destroy_function,
380 * g_memory_output_stream_get_data:
381 * @ostream: a #GMemoryOutputStream
383 * Gets any loaded data from the @ostream.
385 * Note that the returned pointer may become invalid on the next
386 * write or truncate operation on the stream.
388 * Returns: pointer to the stream's data
391 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
393 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
395 return ostream->priv->data;
399 * g_memory_output_stream_get_size:
400 * @ostream: a #GMemoryOutputStream
402 * Gets the size of the currently allocated data area (availible from
403 * g_memory_output_stream_get_data()). If the stream isn't
404 * growable (no realloc was passed to g_memory_output_stream_new()) then
405 * this is the maximum size of the stream and further writes
406 * will return %G_IO_ERROR_NO_SPACE.
408 * Note that for growable streams the returned size may become invalid on
409 * the next write or truncate operation on the stream.
411 * If you want the number of bytes currently written to the stream, use
412 * g_memory_output_stream_get_data_size().
414 * Returns: the number of bytes allocated for the data buffer
417 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
419 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
421 return ostream->priv->len;
425 * g_memory_output_stream_get_data_size:
426 * @ostream: a #GMemoryOutputStream
428 * Returns the number of bytes from the start up
429 * to including the last byte written in the stream
430 * that has not been truncated away.
432 * Returns: the number of bytes written to the stream
437 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
439 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
441 return ostream->priv->valid_len;
445 array_resize (GMemoryOutputStream *ostream,
447 gboolean allow_partial,
450 GMemoryOutputStreamPrivate *priv;
454 priv = ostream->priv;
456 if (priv->len == size)
459 if (!priv->realloc_fn)
462 priv->pos < priv->len)
463 return TRUE; /* Short write */
465 g_set_error_literal (error,
468 _("Memory output stream not resizable"));
473 data = priv->realloc_fn (priv->data, size);
475 if (size > 0 && !data)
478 priv->pos < priv->len)
479 return TRUE; /* Short write */
481 g_set_error_literal (error,
484 _("Failed to resize memory output stream"));
489 memset ((guint8 *)data + len, 0, size - len);
494 if (priv->len < priv->valid_len)
495 priv->valid_len = priv->len;
501 g_nearest_pow (gint num)
512 g_memory_output_stream_write (GOutputStream *stream,
515 GCancellable *cancellable,
518 GMemoryOutputStream *ostream;
519 GMemoryOutputStreamPrivate *priv;
523 ostream = G_MEMORY_OUTPUT_STREAM (stream);
524 priv = ostream->priv;
529 /* Check for address space overflow, but only if the buffer is resizable.
530 Otherwise we just do a short write and don't worry. */
531 if (priv->realloc_fn && priv->pos + count < priv->pos)
534 if (priv->pos + count > priv->len)
536 /* At least enought to fit the write, rounded up
537 for greater than linear growth.
538 TODO: This wastes a lot of memory at large stream sizes.
539 Figure out a more rational allocation strategy. */
540 new_size = g_nearest_pow (priv->pos + count);
541 /* Check for overflow again. We have only checked if
542 pos + count > G_MAXSIZE, but it only catches the case of writing
543 more than 4GiB total on a 32-bit system. There's still the problem
544 of g_nearest_pow overflowing above 0x7fffffff, so we're
545 effectively limited to 2GiB. */
546 if (new_size < priv->len)
549 new_size = MAX (new_size, MIN_ARRAY_SIZE);
550 if (!array_resize (ostream, new_size, TRUE, error))
554 /* Make sure we handle short writes if the array_resize
555 only added part of the required memory */
556 count = MIN (count, priv->len - priv->pos);
558 dest = (guint8 *)priv->data + priv->pos;
559 memcpy (dest, buffer, count);
562 if (priv->pos > priv->valid_len)
563 priv->valid_len = priv->pos;
568 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
569 g_set_error_literal (error,
572 _("Amount of memory required to process the write is "
573 "larger than available address space"));
578 g_memory_output_stream_close (GOutputStream *stream,
579 GCancellable *cancellable,
586 g_memory_output_stream_write_async (GOutputStream *stream,
590 GCancellable *cancellable,
591 GAsyncReadyCallback callback,
594 GSimpleAsyncResult *simple;
597 nwritten = g_memory_output_stream_write (stream,
603 simple = g_simple_async_result_new (G_OBJECT (stream),
606 g_memory_output_stream_write_async);
608 g_simple_async_result_set_op_res_gssize (simple, nwritten);
609 g_simple_async_result_complete_in_idle (simple);
610 g_object_unref (simple);
614 g_memory_output_stream_write_finish (GOutputStream *stream,
615 GAsyncResult *result,
618 GSimpleAsyncResult *simple;
621 simple = G_SIMPLE_ASYNC_RESULT (result);
623 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
624 g_memory_output_stream_write_async);
626 nwritten = g_simple_async_result_get_op_res_gssize (simple);
632 g_memory_output_stream_close_async (GOutputStream *stream,
634 GCancellable *cancellable,
635 GAsyncReadyCallback callback,
638 GSimpleAsyncResult *simple;
640 simple = g_simple_async_result_new (G_OBJECT (stream),
643 g_memory_output_stream_close_async);
646 /* will always return TRUE */
647 g_memory_output_stream_close (stream, cancellable, NULL);
649 g_simple_async_result_complete_in_idle (simple);
650 g_object_unref (simple);
654 g_memory_output_stream_close_finish (GOutputStream *stream,
655 GAsyncResult *result,
658 GSimpleAsyncResult *simple;
660 simple = G_SIMPLE_ASYNC_RESULT (result);
662 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
663 g_memory_output_stream_close_async);
669 g_memory_output_stream_tell (GSeekable *seekable)
671 GMemoryOutputStream *stream;
672 GMemoryOutputStreamPrivate *priv;
674 stream = G_MEMORY_OUTPUT_STREAM (seekable);
681 g_memory_output_stream_can_seek (GSeekable *seekable)
687 g_memory_output_stream_seek (GSeekable *seekable,
690 GCancellable *cancellable,
693 GMemoryOutputStream *stream;
694 GMemoryOutputStreamPrivate *priv;
697 stream = G_MEMORY_OUTPUT_STREAM (seekable);
703 absolute = priv->pos + offset;
711 absolute = priv->len + offset;
715 g_set_error_literal (error,
717 G_IO_ERROR_INVALID_ARGUMENT,
718 _("Invalid GSeekType supplied"));
725 g_set_error_literal (error,
727 G_IO_ERROR_INVALID_ARGUMENT,
728 _("Requested seek before the beginning of the stream"));
732 if (absolute > priv->len)
734 g_set_error_literal (error,
736 G_IO_ERROR_INVALID_ARGUMENT,
737 _("Requested seek beyond the end of the stream"));
741 priv->pos = absolute;
747 g_memory_output_stream_can_truncate (GSeekable *seekable)
749 GMemoryOutputStream *ostream;
750 GMemoryOutputStreamPrivate *priv;
752 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
753 priv = ostream->priv;
755 return priv->realloc_fn != NULL;
759 g_memory_output_stream_truncate (GSeekable *seekable,
761 GCancellable *cancellable,
764 GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
766 if (!array_resize (ostream, offset, FALSE, error))
772 #define __G_MEMORY_OUTPUT_STREAM_C__
773 #include "gioaliasdef.c"