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;
150 g_object_class_install_property (gobject_class,
152 g_param_spec_pointer ("data",
154 P_("Pointer to buffer where data will be written."),
155 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
156 G_PARAM_STATIC_STRINGS));
158 g_object_class_install_property (gobject_class,
160 g_param_spec_ulong ("size",
161 P_("Data Buffer Size"),
162 P_("Current size of the data buffer."),
164 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
165 G_PARAM_STATIC_STRINGS));
167 g_object_class_install_property (gobject_class,
169 g_param_spec_ulong ("data-size",
171 P_("Size of data written to the buffer."),
174 G_PARAM_STATIC_STRINGS));
176 g_object_class_install_property (gobject_class,
177 PROP_REALLOC_FUNCTION,
178 g_param_spec_pointer ("realloc-function",
179 P_("Memory Reallocation Function"),
180 P_("Function with realloc semantics called to enlarge the buffer."),
181 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
182 G_PARAM_STATIC_STRINGS));
184 g_object_class_install_property (gobject_class,
185 PROP_DESTROY_FUNCTION,
186 g_param_spec_pointer ("destroy-function",
187 P_("Destroy Notification Function"),
188 P_("Function called with the buffer as argument when the stream is destroyed."),
189 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
190 G_PARAM_STATIC_STRINGS));
194 g_memory_output_stream_set_property (GObject *object,
199 GMemoryOutputStream *stream;
200 GMemoryOutputStreamPrivate *priv;
202 stream = G_MEMORY_OUTPUT_STREAM (object);
208 priv->data = g_value_get_pointer (value);
211 priv->len = g_value_get_ulong (value);
213 case PROP_REALLOC_FUNCTION:
214 priv->realloc_fn = g_value_get_pointer (value);
216 case PROP_DESTROY_FUNCTION:
217 priv->destroy = g_value_get_pointer (value);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 g_memory_output_stream_get_property (GObject *object,
231 GMemoryOutputStream *stream;
232 GMemoryOutputStreamPrivate *priv;
234 stream = G_MEMORY_OUTPUT_STREAM (object);
240 g_value_set_pointer (value, priv->data);
243 g_value_set_ulong (value, priv->len);
246 g_value_set_ulong (value, priv->valid_len);
248 case PROP_REALLOC_FUNCTION:
249 g_value_set_pointer (value, priv->realloc_fn);
251 case PROP_DESTROY_FUNCTION:
252 g_value_set_pointer (value, priv->destroy);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 g_memory_output_stream_finalize (GObject *object)
263 GMemoryOutputStream *stream;
264 GMemoryOutputStreamPrivate *priv;
266 stream = G_MEMORY_OUTPUT_STREAM (object);
270 priv->destroy (priv->data);
272 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
276 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
278 iface->tell = g_memory_output_stream_tell;
279 iface->can_seek = g_memory_output_stream_can_seek;
280 iface->seek = g_memory_output_stream_seek;
281 iface->can_truncate = g_memory_output_stream_can_truncate;
282 iface->truncate_fn = g_memory_output_stream_truncate;
287 g_memory_output_stream_init (GMemoryOutputStream *stream)
289 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
290 G_TYPE_MEMORY_OUTPUT_STREAM,
291 GMemoryOutputStreamPrivate);
292 stream->priv->pos = 0;
293 stream->priv->valid_len = 0;
297 * g_memory_output_stream_new:
298 * @data: pointer to a chunk of memory to use, or %NULL
299 * @size: the size of @data
300 * @realloc_function: a function with realloc() semantics to be called when
301 * @data needs to be grown, or %NULL
302 * @destroy_function: a function to be called on @data when the stream is
303 * finalized, or %NULL
305 * Creates a new #GMemoryOutputStream.
307 * If @data is non-%NULL, the stream will use that for its internal storage.
308 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
309 * storage when necessary. To construct a fixed-size output stream,
310 * pass %NULL as @realloc_fn.
312 * /* a stream that can grow */
313 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
315 * /* a fixed-size stream */
316 * data = malloc (200);
317 * stream2 = g_memory_output_stream_new (data, 200, NULL, free);
320 * Return value: A newly created #GMemoryOutputStream object.
323 g_memory_output_stream_new (gpointer data,
325 GReallocFunc realloc_function,
326 GDestroyNotify destroy_function)
328 GOutputStream *stream;
330 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
333 "realloc-function", realloc_function,
334 "destroy-function", destroy_function,
341 * g_memory_output_stream_get_data:
342 * @ostream: a #GMemoryOutputStream
344 * Gets any loaded data from the @ostream.
346 * Note that the returned pointer may become invalid on the next
347 * write or truncate operation on the stream.
349 * Returns: pointer to the stream's data
352 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
354 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
356 return ostream->priv->data;
360 * g_memory_output_stream_get_size:
361 * @ostream: a #GMemoryOutputStream
363 * Gets the size of the currently allocated data area (availible from
364 * g_memory_output_stream_get_data()). If the stream isn't
365 * growable (no realloc was passed to g_memory_output_stream_new()) then
366 * this is the maximum size of the stream and further writes
367 * will return %G_IO_ERROR_NO_SPACE.
369 * Note that for growable streams the returned size may become invalid on
370 * the next write or truncate operation on the stream.
372 * If you want the number of bytes currently written to the stream, use
373 * g_memory_output_stream_get_data_size().
375 * Returns: the number of bytes allocated for the data buffer
378 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
380 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
382 return ostream->priv->len;
386 * g_memory_output_stream_get_data_size:
387 * @ostream: a #GMemoryOutputStream
389 * Returns the number of bytes from the start up
390 * to including the last byte written in the stream
391 * that has not been truncated away.
393 * Returns: the number of bytes written to the stream
398 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
400 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
402 return ostream->priv->valid_len;
406 array_resize (GMemoryOutputStream *ostream,
408 gboolean allow_partial,
411 GMemoryOutputStreamPrivate *priv;
415 priv = ostream->priv;
417 if (priv->len == size)
420 if (!priv->realloc_fn)
423 priv->pos < priv->len)
424 return TRUE; /* Short write */
426 g_set_error_literal (error,
429 _("Memory output stream not resizable"));
434 data = priv->realloc_fn (priv->data, size);
436 if (size > 0 && !data)
439 priv->pos < priv->len)
440 return TRUE; /* Short write */
442 g_set_error_literal (error,
445 _("Failed to resize memory output stream"));
450 memset ((guint8 *)data + len, 0, size - len);
455 if (priv->len < priv->valid_len)
456 priv->valid_len = priv->len;
462 g_nearest_pow (gint num)
473 g_memory_output_stream_write (GOutputStream *stream,
476 GCancellable *cancellable,
479 GMemoryOutputStream *ostream;
480 GMemoryOutputStreamPrivate *priv;
484 ostream = G_MEMORY_OUTPUT_STREAM (stream);
485 priv = ostream->priv;
490 /* Check for address space overflow, but only if the buffer is resizable.
491 Otherwise we just do a short write and don't worry. */
492 if (priv->realloc_fn && priv->pos + count < priv->pos)
495 if (priv->pos + count > priv->len)
497 /* At least enought to fit the write, rounded up
498 for greater than linear growth.
499 TODO: This wastes a lot of memory at large stream sizes.
500 Figure out a more rational allocation strategy. */
501 new_size = g_nearest_pow (priv->pos + count);
502 /* Check for overflow again. We have only checked if
503 pos + count > G_MAXSIZE, but it only catches the case of writing
504 more than 4GiB total on a 32-bit system. There's still the problem
505 of g_nearest_pow overflowing above 0x7fffffff, so we're
506 effectively limited to 2GiB. */
507 if (new_size < priv->len)
510 new_size = MAX (new_size, MIN_ARRAY_SIZE);
511 if (!array_resize (ostream, new_size, TRUE, error))
515 /* Make sure we handle short writes if the array_resize
516 only added part of the required memory */
517 count = MIN (count, priv->len - priv->pos);
519 dest = (guint8 *)priv->data + priv->pos;
520 memcpy (dest, buffer, count);
523 if (priv->pos > priv->valid_len)
524 priv->valid_len = priv->pos;
529 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
530 g_set_error_literal (error,
533 _("Amount of memory required to process the write is "
534 "larger than available address space"));
539 g_memory_output_stream_close (GOutputStream *stream,
540 GCancellable *cancellable,
547 g_memory_output_stream_write_async (GOutputStream *stream,
551 GCancellable *cancellable,
552 GAsyncReadyCallback callback,
555 GSimpleAsyncResult *simple;
558 nwritten = g_memory_output_stream_write (stream,
564 simple = g_simple_async_result_new (G_OBJECT (stream),
567 g_memory_output_stream_write_async);
569 g_simple_async_result_set_op_res_gssize (simple, nwritten);
570 g_simple_async_result_complete_in_idle (simple);
571 g_object_unref (simple);
575 g_memory_output_stream_write_finish (GOutputStream *stream,
576 GAsyncResult *result,
579 GSimpleAsyncResult *simple;
582 simple = G_SIMPLE_ASYNC_RESULT (result);
584 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
585 g_memory_output_stream_write_async);
587 nwritten = g_simple_async_result_get_op_res_gssize (simple);
593 g_memory_output_stream_close_async (GOutputStream *stream,
595 GCancellable *cancellable,
596 GAsyncReadyCallback callback,
599 GSimpleAsyncResult *simple;
601 simple = g_simple_async_result_new (G_OBJECT (stream),
604 g_memory_output_stream_close_async);
607 /* will always return TRUE */
608 g_memory_output_stream_close (stream, cancellable, NULL);
610 g_simple_async_result_complete_in_idle (simple);
611 g_object_unref (simple);
615 g_memory_output_stream_close_finish (GOutputStream *stream,
616 GAsyncResult *result,
619 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_close_async);
630 g_memory_output_stream_tell (GSeekable *seekable)
632 GMemoryOutputStream *stream;
633 GMemoryOutputStreamPrivate *priv;
635 stream = G_MEMORY_OUTPUT_STREAM (seekable);
642 g_memory_output_stream_can_seek (GSeekable *seekable)
648 g_memory_output_stream_seek (GSeekable *seekable,
651 GCancellable *cancellable,
654 GMemoryOutputStream *stream;
655 GMemoryOutputStreamPrivate *priv;
658 stream = G_MEMORY_OUTPUT_STREAM (seekable);
664 absolute = priv->pos + offset;
672 absolute = priv->len + offset;
676 g_set_error_literal (error,
678 G_IO_ERROR_INVALID_ARGUMENT,
679 _("Invalid GSeekType supplied"));
686 g_set_error_literal (error,
688 G_IO_ERROR_INVALID_ARGUMENT,
689 _("Requested seek before the beginning of the stream"));
693 if (absolute > priv->len)
695 g_set_error_literal (error,
697 G_IO_ERROR_INVALID_ARGUMENT,
698 _("Requested seek beyond the end of the stream"));
702 priv->pos = absolute;
708 g_memory_output_stream_can_truncate (GSeekable *seekable)
710 GMemoryOutputStream *ostream;
711 GMemoryOutputStreamPrivate *priv;
713 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
714 priv = ostream->priv;
716 return priv->realloc_fn != NULL;
720 g_memory_output_stream_truncate (GSeekable *seekable,
722 GCancellable *cancellable,
725 GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
727 if (!array_resize (ostream, offset, FALSE, error))
733 #define __G_MEMORY_OUTPUT_STREAM_C__
734 #include "gioaliasdef.c"