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.1 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, see <http://www.gnu.org/licenses/>.
19 * Christian Kellner <gicmo@gnome.org>
20 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
24 #include "gmemoryoutputstream.h"
25 #include "goutputstream.h"
26 #include "gpollableoutputstream.h"
27 #include "gseekable.h"
35 * SECTION:gmemoryoutputstream
36 * @short_description: Streaming output operations on memory chunks
38 * @see_also: #GMemoryInputStream
40 * #GMemoryOutputStream is a class for using arbitrary
41 * memory chunks as output for GIO streaming output operations.
43 * As of GLib 2.34, #GMemoryOutputStream trivially implements
44 * #GPollableOutputStream: it always polls as ready.
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_close_async (GOutputStream
*stream
,
92 GCancellable
*cancellable
,
93 GAsyncReadyCallback callback
,
95 static gboolean
g_memory_output_stream_close_finish (GOutputStream
*stream
,
99 static void g_memory_output_stream_seekable_iface_init (GSeekableIface
*iface
);
100 static goffset
g_memory_output_stream_tell (GSeekable
*seekable
);
101 static gboolean
g_memory_output_stream_can_seek (GSeekable
*seekable
);
102 static gboolean
g_memory_output_stream_seek (GSeekable
*seekable
,
105 GCancellable
*cancellable
,
107 static gboolean
g_memory_output_stream_can_truncate (GSeekable
*seekable
);
108 static gboolean
g_memory_output_stream_truncate (GSeekable
*seekable
,
110 GCancellable
*cancellable
,
113 static gboolean
g_memory_output_stream_is_writable (GPollableOutputStream
*stream
);
114 static GSource
*g_memory_output_stream_create_source (GPollableOutputStream
*stream
,
115 GCancellable
*cancellable
);
117 static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface
*iface
);
119 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream
, g_memory_output_stream
, G_TYPE_OUTPUT_STREAM
,
120 G_ADD_PRIVATE (GMemoryOutputStream
)
121 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE
,
122 g_memory_output_stream_seekable_iface_init
);
123 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM
,
124 g_memory_output_stream_pollable_iface_init
))
128 g_memory_output_stream_class_init (GMemoryOutputStreamClass
*klass
)
130 GOutputStreamClass
*ostream_class
;
131 GObjectClass
*gobject_class
;
133 gobject_class
= G_OBJECT_CLASS (klass
);
134 gobject_class
->set_property
= g_memory_output_stream_set_property
;
135 gobject_class
->get_property
= g_memory_output_stream_get_property
;
136 gobject_class
->finalize
= g_memory_output_stream_finalize
;
138 ostream_class
= G_OUTPUT_STREAM_CLASS (klass
);
140 ostream_class
->write_fn
= g_memory_output_stream_write
;
141 ostream_class
->close_fn
= g_memory_output_stream_close
;
142 ostream_class
->close_async
= g_memory_output_stream_close_async
;
143 ostream_class
->close_finish
= g_memory_output_stream_close_finish
;
146 * GMemoryOutputStream:data:
148 * Pointer to buffer where data will be written.
152 g_object_class_install_property (gobject_class
,
154 g_param_spec_pointer ("data",
156 P_("Pointer to buffer where data will be written."),
157 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
158 G_PARAM_STATIC_STRINGS
));
161 * GMemoryOutputStream:size:
163 * Current size of the data buffer.
167 g_object_class_install_property (gobject_class
,
169 g_param_spec_ulong ("size",
170 P_("Data Buffer Size"),
171 P_("Current size of the data buffer."),
173 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
174 G_PARAM_STATIC_STRINGS
));
177 * GMemoryOutputStream:data-size:
179 * Size of data written to the buffer.
183 g_object_class_install_property (gobject_class
,
185 g_param_spec_ulong ("data-size",
187 P_("Size of data written to the buffer."),
190 G_PARAM_STATIC_STRINGS
));
193 * GMemoryOutputStream:realloc-function: (skip)
195 * Function with realloc semantics called to enlarge the buffer.
199 g_object_class_install_property (gobject_class
,
200 PROP_REALLOC_FUNCTION
,
201 g_param_spec_pointer ("realloc-function",
202 P_("Memory Reallocation Function"),
203 P_("Function with realloc semantics called to enlarge the buffer."),
204 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
205 G_PARAM_STATIC_STRINGS
));
208 * GMemoryOutputStream:destroy-function: (skip)
210 * Function called with the buffer as argument when the stream is destroyed.
214 g_object_class_install_property (gobject_class
,
215 PROP_DESTROY_FUNCTION
,
216 g_param_spec_pointer ("destroy-function",
217 P_("Destroy Notification Function"),
218 P_("Function called with the buffer as argument when the stream is destroyed."),
219 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
|
220 G_PARAM_STATIC_STRINGS
));
224 g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface
*iface
)
226 iface
->is_writable
= g_memory_output_stream_is_writable
;
227 iface
->create_source
= g_memory_output_stream_create_source
;
231 g_memory_output_stream_set_property (GObject
*object
,
236 GMemoryOutputStream
*stream
;
237 GMemoryOutputStreamPrivate
*priv
;
239 stream
= G_MEMORY_OUTPUT_STREAM (object
);
245 priv
->data
= g_value_get_pointer (value
);
248 priv
->len
= g_value_get_ulong (value
);
250 case PROP_REALLOC_FUNCTION
:
251 priv
->realloc_fn
= g_value_get_pointer (value
);
253 case PROP_DESTROY_FUNCTION
:
254 priv
->destroy
= g_value_get_pointer (value
);
257 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
263 g_memory_output_stream_get_property (GObject
*object
,
268 GMemoryOutputStream
*stream
;
269 GMemoryOutputStreamPrivate
*priv
;
271 stream
= G_MEMORY_OUTPUT_STREAM (object
);
277 g_value_set_pointer (value
, priv
->data
);
280 g_value_set_ulong (value
, priv
->len
);
283 g_value_set_ulong (value
, priv
->valid_len
);
285 case PROP_REALLOC_FUNCTION
:
286 g_value_set_pointer (value
, priv
->realloc_fn
);
288 case PROP_DESTROY_FUNCTION
:
289 g_value_set_pointer (value
, priv
->destroy
);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
298 g_memory_output_stream_finalize (GObject
*object
)
300 GMemoryOutputStream
*stream
;
301 GMemoryOutputStreamPrivate
*priv
;
303 stream
= G_MEMORY_OUTPUT_STREAM (object
);
307 priv
->destroy (priv
->data
);
309 G_OBJECT_CLASS (g_memory_output_stream_parent_class
)->finalize (object
);
313 g_memory_output_stream_seekable_iface_init (GSeekableIface
*iface
)
315 iface
->tell
= g_memory_output_stream_tell
;
316 iface
->can_seek
= g_memory_output_stream_can_seek
;
317 iface
->seek
= g_memory_output_stream_seek
;
318 iface
->can_truncate
= g_memory_output_stream_can_truncate
;
319 iface
->truncate_fn
= g_memory_output_stream_truncate
;
324 g_memory_output_stream_init (GMemoryOutputStream
*stream
)
326 stream
->priv
= g_memory_output_stream_get_instance_private (stream
);
327 stream
->priv
->pos
= 0;
328 stream
->priv
->valid_len
= 0;
332 * g_memory_output_stream_new: (skip)
333 * @data: (nullable): pointer to a chunk of memory to use, or %NULL
334 * @size: the size of @data
335 * @realloc_function: (nullable): a function with realloc() semantics (like g_realloc())
336 * to be called when @data needs to be grown, or %NULL
337 * @destroy_function: (nullable): a function to be called on @data when the stream is
338 * finalized, or %NULL
340 * Creates a new #GMemoryOutputStream.
342 * In most cases this is not the function you want. See
343 * g_memory_output_stream_new_resizable() instead.
345 * If @data is non-%NULL, the stream will use that for its internal storage.
347 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
348 * storage when necessary and the stream will be considered resizable.
349 * In that case, the stream will start out being (conceptually) empty.
350 * @size is used only as a hint for how big @data is. Specifically,
351 * seeking to the end of a newly-created stream will seek to zero, not
352 * @size. Seeking past the end of the stream and then writing will
353 * introduce a zero-filled gap.
355 * If @realloc_fn is %NULL then the stream is fixed-sized. Seeking to
356 * the end will seek to @size exactly. Writing past the end will give
357 * an 'out of space' error. Attempting to seek past the end will fail.
358 * Unlike the resizable case, seeking to an offset within the stream and
359 * writing will preserve the bytes passed in as @data before that point
360 * and will return them as part of g_memory_output_stream_steal_data().
361 * If you intend to seek you should probably therefore ensure that @data
362 * is properly initialised.
364 * It is probably only meaningful to provide @data and @size in the case
365 * that you want a fixed-sized stream. Put another way: if @realloc_fn
366 * is non-%NULL then it makes most sense to give @data as %NULL and
367 * @size as 0 (allowing #GMemoryOutputStream to do the initial
368 * allocation for itself).
370 * |[<!-- language="C" -->
371 * // a stream that can grow
372 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
374 * // another stream that can grow
375 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
377 * // a fixed-size stream
378 * data = malloc (200);
379 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
382 * Returns: A newly created #GMemoryOutputStream object.
385 g_memory_output_stream_new (gpointer data
,
387 GReallocFunc realloc_function
,
388 GDestroyNotify destroy_function
)
390 GOutputStream
*stream
;
392 stream
= g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM
,
395 "realloc-function", realloc_function
,
396 "destroy-function", destroy_function
,
403 * g_memory_output_stream_new_resizable:
405 * Creates a new #GMemoryOutputStream, using g_realloc() and g_free()
406 * for memory allocation.
411 g_memory_output_stream_new_resizable (void)
413 return g_memory_output_stream_new (NULL
, 0, g_realloc
, g_free
);
417 * g_memory_output_stream_get_data:
418 * @ostream: a #GMemoryOutputStream
420 * Gets any loaded data from the @ostream.
422 * Note that the returned pointer may become invalid on the next
423 * write or truncate operation on the stream.
425 * Returns: (transfer none): pointer to the stream's data, or %NULL if the data
429 g_memory_output_stream_get_data (GMemoryOutputStream
*ostream
)
431 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
433 return ostream
->priv
->data
;
437 * g_memory_output_stream_get_size:
438 * @ostream: a #GMemoryOutputStream
440 * Gets the size of the currently allocated data area (available from
441 * g_memory_output_stream_get_data()).
443 * You probably don't want to use this function on resizable streams.
444 * See g_memory_output_stream_get_data_size() instead. For resizable
445 * streams the size returned by this function is an implementation
446 * detail and may be change at any time in response to operations on the
449 * If the stream is fixed-sized (ie: no realloc was passed to
450 * g_memory_output_stream_new()) then this is the maximum size of the
451 * stream and further writes will return %G_IO_ERROR_NO_SPACE.
453 * In any case, if you want the number of bytes currently written to the
454 * stream, use g_memory_output_stream_get_data_size().
456 * Returns: the number of bytes allocated for the data buffer
459 g_memory_output_stream_get_size (GMemoryOutputStream
*ostream
)
461 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), 0);
463 return ostream
->priv
->len
;
467 * g_memory_output_stream_get_data_size:
468 * @ostream: a #GMemoryOutputStream
470 * Returns the number of bytes from the start up to including the last
471 * byte written in the stream that has not been truncated away.
473 * Returns: the number of bytes written to the stream
478 g_memory_output_stream_get_data_size (GMemoryOutputStream
*ostream
)
480 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), 0);
482 return ostream
->priv
->valid_len
;
486 * g_memory_output_stream_steal_data:
487 * @ostream: a #GMemoryOutputStream
489 * Gets any loaded data from the @ostream. Ownership of the data
490 * is transferred to the caller; when no longer needed it must be
491 * freed using the free function set in @ostream's
492 * #GMemoryOutputStream:destroy-function property.
494 * @ostream must be closed before calling this function.
496 * Returns: (transfer full): the stream's data, or %NULL if it has previously
502 g_memory_output_stream_steal_data (GMemoryOutputStream
*ostream
)
506 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
507 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream
)), NULL
);
509 data
= ostream
->priv
->data
;
510 ostream
->priv
->data
= NULL
;
516 * g_memory_output_stream_steal_as_bytes:
517 * @ostream: a #GMemoryOutputStream
519 * Returns data from the @ostream as a #GBytes. @ostream must be
520 * closed before calling this function.
522 * Returns: (transfer full): the stream's data
527 g_memory_output_stream_steal_as_bytes (GMemoryOutputStream
*ostream
)
531 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream
), NULL
);
532 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream
)), NULL
);
534 result
= g_bytes_new_with_free_func (ostream
->priv
->data
,
535 ostream
->priv
->valid_len
,
536 ostream
->priv
->destroy
,
537 ostream
->priv
->data
);
538 ostream
->priv
->data
= NULL
;
544 array_resize (GMemoryOutputStream
*ostream
,
546 gboolean allow_partial
,
549 GMemoryOutputStreamPrivate
*priv
;
553 priv
= ostream
->priv
;
555 if (priv
->len
== size
)
558 if (!priv
->realloc_fn
)
561 priv
->pos
< priv
->len
)
562 return TRUE
; /* Short write */
564 g_set_error_literal (error
,
567 _("Memory output stream not resizable"));
572 data
= priv
->realloc_fn (priv
->data
, size
);
574 if (size
> 0 && !data
)
577 priv
->pos
< priv
->len
)
578 return TRUE
; /* Short write */
580 g_set_error_literal (error
,
583 _("Failed to resize memory output stream"));
588 memset ((guint8
*)data
+ len
, 0, size
- len
);
593 if (priv
->len
< priv
->valid_len
)
594 priv
->valid_len
= priv
->len
;
600 g_nearest_pow (gsize num
)
604 while (n
< num
&& n
> 0)
611 g_memory_output_stream_write (GOutputStream
*stream
,
614 GCancellable
*cancellable
,
617 GMemoryOutputStream
*ostream
;
618 GMemoryOutputStreamPrivate
*priv
;
622 ostream
= G_MEMORY_OUTPUT_STREAM (stream
);
623 priv
= ostream
->priv
;
628 /* Check for address space overflow, but only if the buffer is resizable.
629 Otherwise we just do a short write and don't worry. */
630 if (priv
->realloc_fn
&& priv
->pos
+ count
< priv
->pos
)
633 if (priv
->pos
+ count
> priv
->len
)
635 /* At least enough to fit the write, rounded up for greater than
638 * Assuming that we're using something like realloc(), the kernel
639 * will overcommit memory to us, so doubling the size each time
640 * will keep the number of realloc calls low without wasting too
643 new_size
= g_nearest_pow (priv
->pos
+ count
);
644 /* Check for overflow again. We have checked if
645 pos + count > G_MAXSIZE, but now check if g_nearest_pow () has
650 new_size
= MAX (new_size
, MIN_ARRAY_SIZE
);
651 if (!array_resize (ostream
, new_size
, TRUE
, error
))
655 /* Make sure we handle short writes if the array_resize
656 only added part of the required memory */
657 count
= MIN (count
, priv
->len
- priv
->pos
);
659 dest
= (guint8
*)priv
->data
+ priv
->pos
;
660 memcpy (dest
, buffer
, count
);
663 if (priv
->pos
> priv
->valid_len
)
664 priv
->valid_len
= priv
->pos
;
669 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
670 g_set_error_literal (error
,
673 _("Amount of memory required to process the write is "
674 "larger than available address space"));
679 g_memory_output_stream_close (GOutputStream
*stream
,
680 GCancellable
*cancellable
,
687 g_memory_output_stream_close_async (GOutputStream
*stream
,
689 GCancellable
*cancellable
,
690 GAsyncReadyCallback callback
,
695 task
= g_task_new (stream
, cancellable
, callback
, data
);
696 g_task_set_source_tag (task
, g_memory_output_stream_close_async
);
698 /* will always return TRUE */
699 g_memory_output_stream_close (stream
, cancellable
, NULL
);
701 g_task_return_boolean (task
, TRUE
);
702 g_object_unref (task
);
706 g_memory_output_stream_close_finish (GOutputStream
*stream
,
707 GAsyncResult
*result
,
710 g_return_val_if_fail (g_task_is_valid (result
, stream
), FALSE
);
712 return g_task_propagate_boolean (G_TASK (result
), error
);
716 g_memory_output_stream_tell (GSeekable
*seekable
)
718 GMemoryOutputStream
*stream
;
719 GMemoryOutputStreamPrivate
*priv
;
721 stream
= G_MEMORY_OUTPUT_STREAM (seekable
);
728 g_memory_output_stream_can_seek (GSeekable
*seekable
)
734 g_memory_output_stream_seek (GSeekable
*seekable
,
737 GCancellable
*cancellable
,
740 GMemoryOutputStream
*stream
;
741 GMemoryOutputStreamPrivate
*priv
;
744 stream
= G_MEMORY_OUTPUT_STREAM (seekable
);
750 absolute
= priv
->pos
+ offset
;
758 /* For resizable streams, we consider the end to be the data
759 * length. For fixed-sized streams, we consider the end to be the
760 * size of the buffer.
762 if (priv
->realloc_fn
)
763 absolute
= priv
->valid_len
+ offset
;
765 absolute
= priv
->len
+ offset
;
769 g_set_error_literal (error
,
771 G_IO_ERROR_INVALID_ARGUMENT
,
772 _("Invalid GSeekType supplied"));
779 g_set_error_literal (error
,
781 G_IO_ERROR_INVALID_ARGUMENT
,
782 _("Requested seek before the beginning of the stream"));
786 /* Can't seek past the end of a fixed-size stream.
788 * Note: seeking to the non-existent byte at the end of a fixed-sized
789 * stream is valid (eg: a 1-byte fixed sized stream can have position
790 * 0 or 1). Therefore '>' is what we want.
792 if (priv
->realloc_fn
== NULL
&& absolute
> priv
->len
)
794 g_set_error_literal (error
,
796 G_IO_ERROR_INVALID_ARGUMENT
,
797 _("Requested seek beyond the end of the stream"));
801 priv
->pos
= absolute
;
807 g_memory_output_stream_can_truncate (GSeekable
*seekable
)
809 GMemoryOutputStream
*ostream
;
810 GMemoryOutputStreamPrivate
*priv
;
812 ostream
= G_MEMORY_OUTPUT_STREAM (seekable
);
813 priv
= ostream
->priv
;
815 /* We do not allow truncation of fixed-sized streams */
816 return priv
->realloc_fn
!= NULL
;
820 g_memory_output_stream_truncate (GSeekable
*seekable
,
822 GCancellable
*cancellable
,
825 GMemoryOutputStream
*ostream
= G_MEMORY_OUTPUT_STREAM (seekable
);
827 if (!array_resize (ostream
, offset
, FALSE
, error
))
830 ostream
->priv
->valid_len
= offset
;
836 g_memory_output_stream_is_writable (GPollableOutputStream
*stream
)
842 g_memory_output_stream_create_source (GPollableOutputStream
*stream
,
843 GCancellable
*cancellable
)
845 GSource
*base_source
, *pollable_source
;
847 base_source
= g_timeout_source_new (0);
848 pollable_source
= g_pollable_source_new_full (stream
, base_source
, cancellable
);
849 g_source_unref (base_source
);
851 return pollable_source
;