It is 'registered', not 'registred'
[glib.git] / gio / gmemoryoutputstream.c
blob378bf3cfeabcb95fcf1424dc6fe9e9937b2bc1ea
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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.
20 * Authors:
21 * Christian Kellner <gicmo@gnome.org>
22 * Krzysztof KosiƄski <tweenk.pl@gmail.com>
25 #include "config.h"
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gpollableoutputstream.h"
29 #include "gseekable.h"
30 #include "gsimpleasyncresult.h"
31 #include "gioerror.h"
32 #include "string.h"
33 #include "glibintl.h"
36 /**
37 * SECTION:gmemoryoutputstream
38 * @short_description: Streaming output operations on memory chunks
39 * @include: gio/gio.h
40 * @see_also: #GMemoryInputStream
42 * #GMemoryOutputStream is a class for using arbitrary
43 * memory chunks as output for GIO streaming output operations.
45 * As of GLib 2.34, #GMemoryOutputStream implements
46 * #GPollableOutputStream.
49 #define MIN_ARRAY_SIZE 16
51 enum {
52 PROP_0,
53 PROP_DATA,
54 PROP_SIZE,
55 PROP_DATA_SIZE,
56 PROP_REALLOC_FUNCTION,
57 PROP_DESTROY_FUNCTION
60 struct _GMemoryOutputStreamPrivate {
62 gpointer data; /* Write buffer */
63 gsize len; /* Current length of the data buffer. Can change with resizing. */
64 gsize valid_len; /* The part of data that has been written to */
65 gsize pos; /* Current position in the stream. Distinct from valid_len,
66 because the stream is seekable. */
68 GReallocFunc realloc_fn;
69 GDestroyNotify destroy;
72 static void g_memory_output_stream_set_property (GObject *object,
73 guint prop_id,
74 const GValue *value,
75 GParamSpec *pspec);
76 static void g_memory_output_stream_get_property (GObject *object,
77 guint prop_id,
78 GValue *value,
79 GParamSpec *pspec);
80 static void g_memory_output_stream_finalize (GObject *object);
82 static gssize g_memory_output_stream_write (GOutputStream *stream,
83 const void *buffer,
84 gsize count,
85 GCancellable *cancellable,
86 GError **error);
88 static gboolean g_memory_output_stream_close (GOutputStream *stream,
89 GCancellable *cancellable,
90 GError **error);
92 static void g_memory_output_stream_close_async (GOutputStream *stream,
93 int io_priority,
94 GCancellable *cancellable,
95 GAsyncReadyCallback callback,
96 gpointer data);
97 static gboolean g_memory_output_stream_close_finish (GOutputStream *stream,
98 GAsyncResult *result,
99 GError **error);
101 static void g_memory_output_stream_seekable_iface_init (GSeekableIface *iface);
102 static goffset g_memory_output_stream_tell (GSeekable *seekable);
103 static gboolean g_memory_output_stream_can_seek (GSeekable *seekable);
104 static gboolean g_memory_output_stream_seek (GSeekable *seekable,
105 goffset offset,
106 GSeekType type,
107 GCancellable *cancellable,
108 GError **error);
109 static gboolean g_memory_output_stream_can_truncate (GSeekable *seekable);
110 static gboolean g_memory_output_stream_truncate (GSeekable *seekable,
111 goffset offset,
112 GCancellable *cancellable,
113 GError **error);
115 static gboolean g_memory_output_stream_is_writable (GPollableOutputStream *stream);
116 static GSource *g_memory_output_stream_create_source (GPollableOutputStream *stream,
117 GCancellable *cancellable);
119 static void g_memory_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
121 G_DEFINE_TYPE_WITH_CODE (GMemoryOutputStream, g_memory_output_stream, G_TYPE_OUTPUT_STREAM,
122 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
123 g_memory_output_stream_seekable_iface_init);
124 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
125 g_memory_output_stream_pollable_iface_init))
128 static void
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->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.
153 * Since: 2.24
155 g_object_class_install_property (gobject_class,
156 PROP_DATA,
157 g_param_spec_pointer ("data",
158 P_("Data Buffer"),
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.
168 * Since: 2.24
170 g_object_class_install_property (gobject_class,
171 PROP_SIZE,
172 g_param_spec_ulong ("size",
173 P_("Data Buffer Size"),
174 P_("Current size of the data buffer."),
175 0, G_MAXULONG, 0,
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.
184 * Since: 2.24
186 g_object_class_install_property (gobject_class,
187 PROP_DATA_SIZE,
188 g_param_spec_ulong ("data-size",
189 P_("Data Size"),
190 P_("Size of data written to the buffer."),
191 0, G_MAXULONG, 0,
192 G_PARAM_READABLE |
193 G_PARAM_STATIC_STRINGS));
196 * GMemoryOutputStream:realloc-function: (skip)
198 * Function with realloc semantics called to enlarge the buffer.
200 * Since: 2.24
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.
215 * Since: 2.24
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));
226 static void
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;
233 static void
234 g_memory_output_stream_set_property (GObject *object,
235 guint prop_id,
236 const GValue *value,
237 GParamSpec *pspec)
239 GMemoryOutputStream *stream;
240 GMemoryOutputStreamPrivate *priv;
242 stream = G_MEMORY_OUTPUT_STREAM (object);
243 priv = stream->priv;
245 switch (prop_id)
247 case PROP_DATA:
248 priv->data = g_value_get_pointer (value);
249 break;
250 case PROP_SIZE:
251 priv->len = g_value_get_ulong (value);
252 break;
253 case PROP_REALLOC_FUNCTION:
254 priv->realloc_fn = g_value_get_pointer (value);
255 break;
256 case PROP_DESTROY_FUNCTION:
257 priv->destroy = g_value_get_pointer (value);
258 break;
259 default:
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 break;
265 static void
266 g_memory_output_stream_get_property (GObject *object,
267 guint prop_id,
268 GValue *value,
269 GParamSpec *pspec)
271 GMemoryOutputStream *stream;
272 GMemoryOutputStreamPrivate *priv;
274 stream = G_MEMORY_OUTPUT_STREAM (object);
275 priv = stream->priv;
277 switch (prop_id)
279 case PROP_DATA:
280 g_value_set_pointer (value, priv->data);
281 break;
282 case PROP_SIZE:
283 g_value_set_ulong (value, priv->len);
284 break;
285 case PROP_DATA_SIZE:
286 g_value_set_ulong (value, priv->valid_len);
287 break;
288 case PROP_REALLOC_FUNCTION:
289 g_value_set_pointer (value, priv->realloc_fn);
290 break;
291 case PROP_DESTROY_FUNCTION:
292 g_value_set_pointer (value, priv->destroy);
293 break;
294 default:
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 break;
300 static void
301 g_memory_output_stream_finalize (GObject *object)
303 GMemoryOutputStream *stream;
304 GMemoryOutputStreamPrivate *priv;
306 stream = G_MEMORY_OUTPUT_STREAM (object);
307 priv = stream->priv;
309 if (priv->destroy)
310 priv->destroy (priv->data);
312 G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
315 static void
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;
326 static void
327 g_memory_output_stream_init (GMemoryOutputStream *stream)
329 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
330 G_TYPE_MEMORY_OUTPUT_STREAM,
331 GMemoryOutputStreamPrivate);
332 stream->priv->pos = 0;
333 stream->priv->valid_len = 0;
337 * g_memory_output_stream_new: (skip)
338 * @data: (allow-none): pointer to a chunk of memory to use, or %NULL
339 * @size: the size of @data
340 * @realloc_function: (allow-none): a function with realloc() semantics (like g_realloc())
341 * to be called when @data needs to be grown, or %NULL
342 * @destroy_function: (allow-none): a function to be called on @data when the stream is
343 * finalized, or %NULL
345 * Creates a new #GMemoryOutputStream.
347 * If @data is non-%NULL, the stream will use that for its internal storage.
348 * If @realloc_fn is non-%NULL, it will be used for resizing the internal
349 * storage when necessary. To construct a fixed-size output stream,
350 * pass %NULL as @realloc_fn.
352 * |[
353 * /&ast; a stream that can grow &ast;/
354 * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
356 * /&ast; another stream that can grow &ast;/
357 * stream2 = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
359 * /&ast; a fixed-size stream &ast;/
360 * data = malloc (200);
361 * stream3 = g_memory_output_stream_new (data, 200, NULL, free);
362 * ]|
364 * Return value: A newly created #GMemoryOutputStream object.
366 GOutputStream *
367 g_memory_output_stream_new (gpointer data,
368 gsize size,
369 GReallocFunc realloc_function,
370 GDestroyNotify destroy_function)
372 GOutputStream *stream;
374 stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
375 "data", data,
376 "size", size,
377 "realloc-function", realloc_function,
378 "destroy-function", destroy_function,
379 NULL);
381 return stream;
385 * g_memory_output_stream_get_data:
386 * @ostream: a #GMemoryOutputStream
388 * Gets any loaded data from the @ostream.
390 * Note that the returned pointer may become invalid on the next
391 * write or truncate operation on the stream.
393 * Returns: (transfer none): pointer to the stream's data
395 gpointer
396 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
398 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
400 return ostream->priv->data;
404 * g_memory_output_stream_get_size:
405 * @ostream: a #GMemoryOutputStream
407 * Gets the size of the currently allocated data area (available from
408 * g_memory_output_stream_get_data()). If the stream isn't
409 * growable (no realloc was passed to g_memory_output_stream_new()) then
410 * this is the maximum size of the stream and further writes
411 * will return %G_IO_ERROR_NO_SPACE.
413 * Note that for growable streams the returned size may become invalid on
414 * the next write or truncate operation on the stream.
416 * If you want the number of bytes currently written to the stream, use
417 * g_memory_output_stream_get_data_size().
419 * Returns: the number of bytes allocated for the data buffer
421 gsize
422 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
424 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
426 return ostream->priv->len;
430 * g_memory_output_stream_get_data_size:
431 * @ostream: a #GMemoryOutputStream
433 * Returns the number of bytes from the start up
434 * to including the last byte written in the stream
435 * that has not been truncated away.
437 * Returns: the number of bytes written to the stream
439 * Since: 2.18
441 gsize
442 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
444 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
446 return ostream->priv->valid_len;
450 * g_memory_output_stream_steal_data:
451 * @ostream: a #GMemoryOutputStream
453 * Gets any loaded data from the @ostream. Ownership of the data
454 * is transferred to the caller; when no longer needed it must be
455 * freed using the free function set in @ostream's
456 * #GMemoryOutputStream:destroy-function property.
458 * @ostream must be closed before calling this function.
460 * Returns: (transfer full): the stream's data
462 * Since: 2.26
464 gpointer
465 g_memory_output_stream_steal_data (GMemoryOutputStream *ostream)
467 gpointer data;
469 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
470 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
472 data = ostream->priv->data;
473 ostream->priv->data = NULL;
475 return data;
479 * g_memory_output_stream_steal_as_bytes:
480 * @ostream: a #GMemoryOutputStream
482 * Returns data from the @ostream as a #GBytes. @ostream must be
483 * closed before calling this function.
485 * Returns: (transfer full): the stream's data
487 * Since: 2.34
489 GBytes *
490 g_memory_output_stream_steal_as_bytes (GMemoryOutputStream *ostream)
492 GBytes *result;
494 g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
495 g_return_val_if_fail (g_output_stream_is_closed (G_OUTPUT_STREAM (ostream)), NULL);
497 result = g_bytes_new_with_free_func (ostream->priv->data,
498 ostream->priv->valid_len,
499 ostream->priv->destroy,
500 ostream->priv->data);
501 ostream->priv->data = NULL;
503 return result;
506 static gboolean
507 array_resize (GMemoryOutputStream *ostream,
508 gsize size,
509 gboolean allow_partial,
510 GError **error)
512 GMemoryOutputStreamPrivate *priv;
513 gpointer data;
514 gsize len;
516 priv = ostream->priv;
518 if (priv->len == size)
519 return TRUE;
521 if (!priv->realloc_fn)
523 if (allow_partial &&
524 priv->pos < priv->len)
525 return TRUE; /* Short write */
527 g_set_error_literal (error,
528 G_IO_ERROR,
529 G_IO_ERROR_NO_SPACE,
530 _("Memory output stream not resizable"));
531 return FALSE;
534 len = priv->len;
535 data = priv->realloc_fn (priv->data, size);
537 if (size > 0 && !data)
539 if (allow_partial &&
540 priv->pos < priv->len)
541 return TRUE; /* Short write */
543 g_set_error_literal (error,
544 G_IO_ERROR,
545 G_IO_ERROR_NO_SPACE,
546 _("Failed to resize memory output stream"));
547 return FALSE;
550 if (size > len)
551 memset ((guint8 *)data + len, 0, size - len);
553 priv->data = data;
554 priv->len = size;
556 if (priv->len < priv->valid_len)
557 priv->valid_len = priv->len;
559 return TRUE;
562 static gint
563 g_nearest_pow (gint num)
565 gint n = 1;
567 while (n < num)
568 n <<= 1;
570 return n;
573 static gssize
574 g_memory_output_stream_write (GOutputStream *stream,
575 const void *buffer,
576 gsize count,
577 GCancellable *cancellable,
578 GError **error)
580 GMemoryOutputStream *ostream;
581 GMemoryOutputStreamPrivate *priv;
582 guint8 *dest;
583 gsize new_size;
585 ostream = G_MEMORY_OUTPUT_STREAM (stream);
586 priv = ostream->priv;
588 if (count == 0)
589 return 0;
591 /* Check for address space overflow, but only if the buffer is resizable.
592 Otherwise we just do a short write and don't worry. */
593 if (priv->realloc_fn && priv->pos + count < priv->pos)
594 goto overflow;
596 if (priv->pos + count > priv->len)
598 /* At least enought to fit the write, rounded up
599 for greater than linear growth.
600 TODO: This wastes a lot of memory at large stream sizes.
601 Figure out a more rational allocation strategy. */
602 new_size = g_nearest_pow (priv->pos + count);
603 /* Check for overflow again. We have only checked if
604 pos + count > G_MAXSIZE, but it only catches the case of writing
605 more than 4GiB total on a 32-bit system. There's still the problem
606 of g_nearest_pow overflowing above 0x7fffffff, so we're
607 effectively limited to 2GiB. */
608 if (new_size < priv->len)
609 goto overflow;
611 new_size = MAX (new_size, MIN_ARRAY_SIZE);
612 if (!array_resize (ostream, new_size, TRUE, error))
613 return -1;
616 /* Make sure we handle short writes if the array_resize
617 only added part of the required memory */
618 count = MIN (count, priv->len - priv->pos);
620 dest = (guint8 *)priv->data + priv->pos;
621 memcpy (dest, buffer, count);
622 priv->pos += count;
624 if (priv->pos > priv->valid_len)
625 priv->valid_len = priv->pos;
627 return count;
629 overflow:
630 /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
631 g_set_error_literal (error,
632 G_IO_ERROR,
633 G_IO_ERROR_NO_SPACE,
634 _("Amount of memory required to process the write is "
635 "larger than available address space"));
636 return -1;
639 static gboolean
640 g_memory_output_stream_close (GOutputStream *stream,
641 GCancellable *cancellable,
642 GError **error)
644 return TRUE;
647 static void
648 g_memory_output_stream_close_async (GOutputStream *stream,
649 int io_priority,
650 GCancellable *cancellable,
651 GAsyncReadyCallback callback,
652 gpointer data)
654 GSimpleAsyncResult *simple;
656 simple = g_simple_async_result_new (G_OBJECT (stream),
657 callback,
658 data,
659 g_memory_output_stream_close_async);
662 /* will always return TRUE */
663 g_memory_output_stream_close (stream, cancellable, NULL);
665 g_simple_async_result_complete_in_idle (simple);
666 g_object_unref (simple);
669 static gboolean
670 g_memory_output_stream_close_finish (GOutputStream *stream,
671 GAsyncResult *result,
672 GError **error)
674 GSimpleAsyncResult *simple;
676 simple = G_SIMPLE_ASYNC_RESULT (result);
678 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
679 g_memory_output_stream_close_async);
681 return TRUE;
684 static goffset
685 g_memory_output_stream_tell (GSeekable *seekable)
687 GMemoryOutputStream *stream;
688 GMemoryOutputStreamPrivate *priv;
690 stream = G_MEMORY_OUTPUT_STREAM (seekable);
691 priv = stream->priv;
693 return priv->pos;
696 static gboolean
697 g_memory_output_stream_can_seek (GSeekable *seekable)
699 return TRUE;
702 static gboolean
703 g_memory_output_stream_seek (GSeekable *seekable,
704 goffset offset,
705 GSeekType type,
706 GCancellable *cancellable,
707 GError **error)
709 GMemoryOutputStream *stream;
710 GMemoryOutputStreamPrivate *priv;
711 goffset absolute;
713 stream = G_MEMORY_OUTPUT_STREAM (seekable);
714 priv = stream->priv;
716 switch (type)
718 case G_SEEK_CUR:
719 absolute = priv->pos + offset;
720 break;
722 case G_SEEK_SET:
723 absolute = offset;
724 break;
726 case G_SEEK_END:
727 absolute = priv->len + offset;
728 break;
730 default:
731 g_set_error_literal (error,
732 G_IO_ERROR,
733 G_IO_ERROR_INVALID_ARGUMENT,
734 _("Invalid GSeekType supplied"));
736 return FALSE;
739 if (absolute < 0)
741 g_set_error_literal (error,
742 G_IO_ERROR,
743 G_IO_ERROR_INVALID_ARGUMENT,
744 _("Requested seek before the beginning of the stream"));
745 return FALSE;
748 if (absolute > priv->len)
750 g_set_error_literal (error,
751 G_IO_ERROR,
752 G_IO_ERROR_INVALID_ARGUMENT,
753 _("Requested seek beyond the end of the stream"));
754 return FALSE;
757 priv->pos = absolute;
759 return TRUE;
762 static gboolean
763 g_memory_output_stream_can_truncate (GSeekable *seekable)
765 GMemoryOutputStream *ostream;
766 GMemoryOutputStreamPrivate *priv;
768 ostream = G_MEMORY_OUTPUT_STREAM (seekable);
769 priv = ostream->priv;
771 return priv->realloc_fn != NULL;
774 static gboolean
775 g_memory_output_stream_truncate (GSeekable *seekable,
776 goffset offset,
777 GCancellable *cancellable,
778 GError **error)
780 GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
782 if (!array_resize (ostream, offset, FALSE, error))
783 return FALSE;
785 return TRUE;
788 static gboolean
789 g_memory_output_stream_is_writable (GPollableOutputStream *stream)
791 return TRUE;
794 static GSource *
795 g_memory_output_stream_create_source (GPollableOutputStream *stream,
796 GCancellable *cancellable)
798 GSource *base_source, *pollable_source;
800 base_source = g_timeout_source_new (0);
801 pollable_source = g_pollable_source_new_full (stream, base_source,
802 cancellable);
803 g_source_unref (base_source);
805 return pollable_source;