1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Christian Kellner <gicmo@gnome.org>
22 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gpollableoutputstream.h"
29 #include "gseekable.h"
34 #include "gutilsprivate.h"
38 * SECTION:gmemoryoutputstream
39 * @short_description: Streaming output operations on memory chunks
41 * @see_also: #GMemoryInputStream
43 * #GMemoryOutputStream is a class for using arbitrary
44 * memory chunks as output for GIO streaming output operations.
46 * As of GLib 2.34, #GMemoryOutputStream trivially implements
47 * #GPollableOutputStream: it always polls as ready.
50 #define MIN_ARRAY_SIZE 16
57 PROP_REALLOC_FUNCTION,
61 struct _GMemoryOutputStreamPrivate
63 gpointer data; /* Write buffer */
64 gsize len; /* Current length of the data buffer. Can change with resizing. */
65 gsize valid_len; /* The part of data that has been written to */
66 gsize pos; /* Current position in the stream. Distinct from valid_len,
67 because the stream is seekable. */
69 GReallocFunc realloc_fn;
70 GDestroyNotify destroy;
73 static void g_memory_output_stream_set_property (GObject *object,
77 static void g_memory_output_stream_get_property (GObject *object,
81 static void g_memory_output_stream_finalize (GObject *object);
83 static gssize g_memory_output_stream_write (GOutputStream *stream,
86 GCancellable *cancellable,
89 static gboolean g_memory_output_stream_close (GOutputStream *stream,
90 GCancellable *cancellable,
93 static void g_memory_output_stream_close_async (GOutputStream *stream,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
98 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
102 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
103 static goffset g_memory_output_stream_tell (GSeekable *seekable);
104 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
105 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
108 GCancellable *cancellable,
110 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
111 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
113 GCancellable *cancellable,
116 static gboolean g_memory_output_stream_is_writable (GPollableOutputStream *stream);
117 static GSource *g_memory_output_stream_create_source (GPollableOutputStream *stream,
118 GCancellable *cancellable);
120 static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
122 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
123 G_ADD_PRIVATE (GMemoryOutputStream)
124 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
125 g_memory_output_stream_seekable_iface_init);
126 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
127 g_memory_output_stream_pollable_iface_init))
131 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
133 GOutputStreamClass *ostream_class;
134 GObjectClass *gobject_class;
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->close_async = g_memory_output_stream_close_async;
146 ostream_class->close_finish = g_memory_output_stream_close_finish;
149 * GMemoryOutputStream:data:
151 * Pointer to buffer where data will be written.
155 g_object_class_install_property (gobject_class,
157 g_param_spec_pointer ("data",
159 P_("Pointer to buffer where data will be written."),
160 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
161 G_PARAM_STATIC_STRINGS));
164 * GMemoryOutputStream:size:
166 * Current size of the data buffer.
170 g_object_class_install_property (gobject_class,
172 g_param_spec_ulong ("size",
173 P_("Data Buffer Size"),
174 P_("Current size of the data buffer."),
176 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
177 G_PARAM_STATIC_STRINGS));
180 * GMemoryOutputStream:data-size:
182 * Size of data written to the buffer.
186 g_object_class_install_property (gobject_class,
188 g_param_spec_ulong ("data-size",
190 P_("Size of data written to the buffer."),
193 G_PARAM_STATIC_STRINGS));
196 * GMemoryOutputStream:realloc-function: (skip)
198 * Function with realloc semantics called to enlarge the buffer.
202 g_object_class_install_property (gobject_class,
203 PROP_REALLOC_FUNCTION,
204 g_param_spec_pointer ("realloc-function",
205 P_("Memory Reallocation Function"),
206 P_("Function with realloc semantics called to enlarge the buffer."),
207 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
208 G_PARAM_STATIC_STRINGS));
211 * GMemoryOutputStream:destroy-function: (skip)
213 * Function called with the buffer as argument when the stream is destroyed.
217 g_object_class_install_property (gobject_class,
218 PROP_DESTROY_FUNCTION,
219 g_param_spec_pointer ("destroy-function",
220 P_("Destroy Notification Function"),
221 P_("Function called with the buffer as argument when the stream is destroyed."),
222 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
223 G_PARAM_STATIC_STRINGS));
227 g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
229 iface->is_writable = g_memory_output_stream_is_writable;
230 iface->create_source = g_memory_output_stream_create_source;
234 g_memory_output_stream_set_property (GObject *object,
239 GMemoryOutputStream *stream;
240 GMemoryOutputStreamPrivate *priv;
242 stream = G_MEMORY_OUTPUT_STREAM (object);
248 priv->data = g_value_get_pointer (value);
251 priv->len = g_value_get_ulong (value);
253 case PROP_REALLOC_FUNCTION:
254 priv->realloc_fn = g_value_get_pointer (value);
256 case PROP_DESTROY_FUNCTION:
257 priv->destroy = g_value_get_pointer (value);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 g_memory_output_stream_get_property (GObject *object,
271 GMemoryOutputStream *stream;
272 GMemoryOutputStreamPrivate *priv;
274 stream = G_MEMORY_OUTPUT_STREAM (object);
280 g_value_set_pointer (value, priv->data);
283 g_value_set_ulong (value, priv->len);
286 g_value_set_ulong (value, priv->valid_len);
288 case PROP_REALLOC_FUNCTION:
289 g_value_set_pointer (value, priv->realloc_fn);
291 case PROP_DESTROY_FUNCTION:
292 g_value_set_pointer (value, priv->destroy);
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 g_memory_output_stream_finalize (GObject *object)
303 GMemoryOutputStream *stream;
304 GMemoryOutputStreamPrivate *priv;
306 stream = G_MEMORY_OUTPUT_STREAM (object);
310 priv->destroy (priv->data);
312 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
316 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
318 iface->tell = g_memory_output_stream_tell;
319 iface->can_seek = g_memory_output_stream_can_seek;
320 iface->seek = g_memory_output_stream_seek;
321 iface->can_truncate = g_memory_output_stream_can_truncate;
322 iface->truncate_fn = g_memory_output_stream_truncate;
327 g_memory_output_stream_init (GMemoryOutputStream *stream)
329 stream->priv = g_memory_output_stream_get_instance_private (stream);
330 stream->priv->pos = 0;
331 stream->priv->valid_len = 0;
335 * g_memory_output_stream_new: (skip)
336 * @data: (nullable): pointer to a chunk of memory to use, or %NULL
337 * @size: the size of @data
338 * @realloc_function: (nullable): a function with realloc() semantics (like g_realloc())
339 * to be called when @data needs to be grown, or %NULL
340 * @destroy_function: (nullable): a function to be called on @data when the stream is
341 * finalized, or %NULL
343 * Creates a new #GMemoryOutputStream.
345 * In most cases this is not the function you want. See
346 * g_memory_output_stream_new_resizable() instead.
348 * If @data is non-%NULL, the stream will use that for its internal storage.
350 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
351 * storage when necessary and the stream will be considered resizable.
352 * In that case, the stream will start out being (conceptually) empty.
353 * @size is used only as a hint for how big @data is. Specifically,
354 * seeking to the end of a newly-created stream will seek to zero, not
355 * @size. Seeking past the end of the stream and then writing will
356 * introduce a zero-filled gap.
358 * If @realloc_fn is %NULL then the stream is fixed-sized. Seeking to
359 * the end will seek to @size exactly. Writing past the end will give
360 * an 'out of space' error. Attempting to seek past the end will fail.
361 * Unlike the resizable case, seeking to an offset within the stream and
362 * writing will preserve the bytes passed in as @data before that point
363 * and will return them as part of g_memory_output_stream_steal_data().
364 * If you intend to seek you should probably therefore ensure that @data
365 * is properly initialised.
367 * It is probably only meaningful to provide @data and @size in the case
368 * that you want a fixed-sized stream. Put another way: if @realloc_fn
369 * is non-%NULL then it makes most sense to give @data as %NULL and
370 * @size as 0 (allowing #GMemoryOutputStream to do the initial
371 * allocation for itself).
373 * |[<!-- language="C" -->
374 * // a stream that can grow
375 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
377 * // another stream that can grow
378 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
380 * // a fixed-size stream
381 * data = malloc (200);
382 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
385 * Returns: A newly created #GMemoryOutputStream object.
388 g_memory_output_stream_new (gpointer data,
390 GReallocFunc realloc_function,
391 GDestroyNotify destroy_function)
393 GOutputStream *stream;
395 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
398 "realloc-function", realloc_function,
399 "destroy-function", destroy_function,
406 * g_memory_output_stream_new_resizable:
408 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
409 * for memory allocation.
414 g_memory_output_stream_new_resizable (void)
416 return g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
420 * g_memory_output_stream_get_data:
421 * @ostream: a #GMemoryOutputStream
423 * Gets any loaded data from the @ostream.
425 * Note that the returned pointer may become invalid on the next
426 * write or truncate operation on the stream.
428 * Returns: (transfer none): pointer to the stream's data, or %NULL if the data
432 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
434 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
436 return ostream->priv->data;
440 * g_memory_output_stream_get_size:
441 * @ostream: a #GMemoryOutputStream
443 * Gets the size of the currently allocated data area (available from
444 * g_memory_output_stream_get_data()).
446 * You probably don't want to use this function on resizable streams.
447 * See g_memory_output_stream_get_data_size() instead. For resizable
448 * streams the size returned by this function is an implementation
449 * detail and may be change at any time in response to operations on the
452 * If the stream is fixed-sized (ie: no realloc was passed to
453 * g_memory_output_stream_new()) then this is the maximum size of the
454 * stream and further writes will return %G_IO_ERROR_NO_SPACE.
456 * In any case, if you want the number of bytes currently written to the
457 * stream, use g_memory_output_stream_get_data_size().
459 * Returns: the number of bytes allocated for the data buffer
462 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
464 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
466 return ostream->priv->len;
470 * g_memory_output_stream_get_data_size:
471 * @ostream: a #GMemoryOutputStream
473 * Returns the number of bytes from the start up to including the last
474 * byte written in the stream that has not been truncated away.
476 * Returns: the number of bytes written to the stream
481 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
483 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
485 return ostream->priv->valid_len;
489 * g_memory_output_stream_steal_data:
490 * @ostream: a #GMemoryOutputStream
492 * Gets any loaded data from the @ostream. Ownership of the data
493 * is transferred to the caller; when no longer needed it must be
494 * freed using the free function set in @ostream's
495 * #GMemoryOutputStream:destroy-function property.
497 * @ostream must be closed before calling this function.
499 * Returns: (transfer full): the stream's data, or %NULL if it has previously
505 g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
509 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
510 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
512 data = ostream->priv->data;
513 ostream->priv->data = NULL;
519 * g_memory_output_stream_steal_as_bytes:
520 * @ostream: a #GMemoryOutputStream
522 * Returns data from the @ostream as a #GBytes. @ostream must be
523 * closed before calling this function.
525 * Returns: (transfer full): the stream's data
530 g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream)
534 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
535 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
537 result = g_bytes_new_with_free_func (ostream->priv->data,
538 ostream->priv->valid_len,
539 ostream->priv->destroy,
540 ostream->priv->data);
541 ostream->priv->data = NULL;
547 array_resize (GMemoryOutputStream *ostream,
549 gboolean allow_partial,
552 GMemoryOutputStreamPrivate *priv;
556 priv = ostream->priv;
558 if (priv->len == size)
561 if (!priv->realloc_fn)
564 priv->pos < priv->len)
565 return TRUE; /* Short write */
567 g_set_error_literal (error,
570 _("Memory output stream not resizable"));
575 data = priv->realloc_fn (priv->data, size);
577 if (size > 0 && !data)
580 priv->pos < priv->len)
581 return TRUE; /* Short write */
583 g_set_error_literal (error,
586 _("Failed to resize memory output stream"));
591 memset ((guint8 *)data + len, 0, size - len);
596 if (priv->len < priv->valid_len)
597 priv->valid_len = priv->len;
603 g_memory_output_stream_write (GOutputStream *stream,
606 GCancellable *cancellable,
609 GMemoryOutputStream *ostream;
610 GMemoryOutputStreamPrivate *priv;
614 ostream = G_MEMORY_OUTPUT_STREAM (stream);
615 priv = ostream->priv;
620 /* Check for address space overflow, but only if the buffer is resizable.
621 Otherwise we just do a short write and don't worry. */
622 if (priv->realloc_fn && priv->pos + count < priv->pos)
625 if (priv->pos + count > priv->len)
627 /* At least enough to fit the write, rounded up for greater than
630 * Assuming that we're using something like realloc(), the kernel
631 * will overcommit memory to us, so doubling the size each time
632 * will keep the number of realloc calls low without wasting too
635 new_size = g_nearest_pow (priv->pos + count);
636 /* Check for overflow again. We have checked if
637 pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
642 new_size = MAX (new_size, MIN_ARRAY_SIZE);
643 if (!array_resize (ostream, new_size, TRUE, error))
647 /* Make sure we handle short writes if the array_resize
648 only added part of the required memory */
649 count = MIN (count, priv->len - priv->pos);
651 dest = (guint8 *)priv->data + priv->pos;
652 memcpy (dest, buffer, count);
655 if (priv->pos > priv->valid_len)
656 priv->valid_len = priv->pos;
661 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
662 g_set_error_literal (error,
665 _("Amount of memory required to process the write is "
666 "larger than available address space"));
671 g_memory_output_stream_close (GOutputStream *stream,
672 GCancellable *cancellable,
679 g_memory_output_stream_close_async (GOutputStream *stream,
681 GCancellable *cancellable,
682 GAsyncReadyCallback callback,
687 task = g_task_new (stream, cancellable, callback, data);
688 g_task_set_source_tag (task, g_memory_output_stream_close_async);
690 /* will always return TRUE */
691 g_memory_output_stream_close (stream, cancellable, NULL);
693 g_task_return_boolean (task, TRUE);
694 g_object_unref (task);
698 g_memory_output_stream_close_finish (GOutputStream *stream,
699 GAsyncResult *result,
702 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
704 return g_task_propagate_boolean (G_TASK (result), error);
708 g_memory_output_stream_tell (GSeekable *seekable)
710 GMemoryOutputStream *stream;
711 GMemoryOutputStreamPrivate *priv;
713 stream = G_MEMORY_OUTPUT_STREAM (seekable);
720 g_memory_output_stream_can_seek (GSeekable *seekable)
726 g_memory_output_stream_seek (GSeekable *seekable,
729 GCancellable *cancellable,
732 GMemoryOutputStream *stream;
733 GMemoryOutputStreamPrivate *priv;
736 stream = G_MEMORY_OUTPUT_STREAM (seekable);
742 absolute = priv->pos + offset;
750 /* For resizable streams, we consider the end to be the data
751 * length. For fixed-sized streams, we consider the end to be the
752 * size of the buffer.
754 if (priv->realloc_fn)
755 absolute = priv->valid_len + offset;
757 absolute = priv->len + offset;
761 g_set_error_literal (error,
763 G_IO_ERROR_INVALID_ARGUMENT,
764 _("Invalid GSeekType supplied"));
771 g_set_error_literal (error,
773 G_IO_ERROR_INVALID_ARGUMENT,
774 _("Requested seek before the beginning of the stream"));
778 /* Can't seek past the end of a fixed-size stream.
780 * Note: seeking to the non-existent byte at the end of a fixed-sized
781 * stream is valid (eg: a 1-byte fixed sized stream can have position
782 * 0 or 1). Therefore '>' is what we want.
784 if (priv->realloc_fn == NULL && (gsize) absolute > priv->len)
786 g_set_error_literal (error,
788 G_IO_ERROR_INVALID_ARGUMENT,
789 _("Requested seek beyond the end of the stream"));
793 priv->pos = absolute;
799 g_memory_output_stream_can_truncate (GSeekable *seekable)
801 GMemoryOutputStream *ostream;
802 GMemoryOutputStreamPrivate *priv;
804 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
805 priv = ostream->priv;
807 /* We do not allow truncation of fixed-sized streams */
808 return priv->realloc_fn != NULL;
812 g_memory_output_stream_truncate (GSeekable *seekable,
814 GCancellable *cancellable,
817 GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
819 if (!array_resize (ostream, offset, FALSE, error))
822 ostream->priv->valid_len = offset;
828 g_memory_output_stream_is_writable (GPollableOutputStream *stream)
834 g_memory_output_stream_create_source (GPollableOutputStream *stream,
835 GCancellable *cancellable)
837 GSource *base_source, *pollable_source;
839 base_source = g_timeout_source_new (0);
840 pollable_source = g_pollable_source_new_full (stream, base_source, cancellable);
841 g_source_unref (base_source);
843 return pollable_source;