Add gtk-doc comments with Since tags to GMemoryOutputStream properties
[platform/upstream/glib.git] / gio / gmemoryoutputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Authors:
21  *   Christian Kellner <gicmo@gnome.org>
22  *   Krzysztof KosiƄski <tweenk.pl@gmail.com>
23  */
24
25 #include "config.h"
26 #include "gmemoryoutputstream.h"
27 #include "goutputstream.h"
28 #include "gseekable.h"
29 #include "gsimpleasyncresult.h"
30 #include "gioerror.h"
31 #include "string.h"
32 #include "glibintl.h"
33
34 #include "gioalias.h"
35
36 /**
37  * SECTION:gmemoryoutputstream
38  * @short_description: Streaming output operations on memory chunks
39  * @include: gio/gio.h
40  * @see_also: #GMemoryInputStream
41  *
42  * #GMemoryOutputStream is a class for using arbitrary
43  * memory chunks as output for GIO streaming output operations.
44  *
45  */
46
47 #define MIN_ARRAY_SIZE  16
48
49 enum {
50   PROP_0,
51   PROP_DATA,
52   PROP_SIZE,
53   PROP_DATA_SIZE,
54   PROP_REALLOC_FUNCTION,
55   PROP_DESTROY_FUNCTION
56 };
57
58 struct _GMemoryOutputStreamPrivate {
59   
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. */
65
66   GReallocFunc   realloc_fn;
67   GDestroyNotify destroy;
68 };
69
70 static void     g_memory_output_stream_set_property (GObject      *object,
71                                                      guint         prop_id,
72                                                      const GValue *value,
73                                                      GParamSpec   *pspec);
74 static void     g_memory_output_stream_get_property (GObject      *object,
75                                                      guint         prop_id,
76                                                      GValue       *value,
77                                                      GParamSpec   *pspec);
78 static void     g_memory_output_stream_finalize     (GObject      *object);
79
80 static gssize   g_memory_output_stream_write       (GOutputStream *stream,
81                                                     const void    *buffer,
82                                                     gsize          count,
83                                                     GCancellable  *cancellable,
84                                                     GError       **error);
85
86 static gboolean g_memory_output_stream_close       (GOutputStream  *stream,
87                                                     GCancellable   *cancellable,
88                                                     GError        **error);
89
90 static void     g_memory_output_stream_write_async  (GOutputStream        *stream,
91                                                      const void           *buffer,
92                                                      gsize                 count,
93                                                      int                   io_priority,
94                                                      GCancellable         *cancellable,
95                                                      GAsyncReadyCallback   callback,
96                                                      gpointer              data);
97 static gssize   g_memory_output_stream_write_finish (GOutputStream        *stream,
98                                                      GAsyncResult         *result,
99                                                      GError              **error);
100 static void     g_memory_output_stream_close_async  (GOutputStream        *stream,
101                                                      int                   io_priority,
102                                                      GCancellable         *cancellable,
103                                                      GAsyncReadyCallback   callback,
104                                                      gpointer              data);
105 static gboolean g_memory_output_stream_close_finish (GOutputStream        *stream,
106                                                      GAsyncResult         *result,
107                                                      GError              **error);
108
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,
113                                                            goffset          offset,
114                                                            GSeekType        type,
115                                                            GCancellable    *cancellable,
116                                                            GError         **error);
117 static gboolean g_memory_output_stream_can_truncate        (GSeekable       *seekable);
118 static gboolean g_memory_output_stream_truncate            (GSeekable       *seekable,
119                                                            goffset          offset,
120                                                            GCancellable    *cancellable,
121                                                            GError         **error);
122
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))
126
127
128 static void
129 g_memory_output_stream_class_init (GMemoryOutputStreamClass *klass)
130 {
131   GOutputStreamClass *ostream_class;
132   GObjectClass *gobject_class;
133
134   g_type_class_add_private (klass, sizeof (GMemoryOutputStreamPrivate));
135
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;
140
141   ostream_class = G_OUTPUT_STREAM_CLASS (klass);
142
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;
149
150   /**
151    * GMemoryOutputStream:data:
152    *
153    * Pointer to buffer where data will be written.
154    *
155    * Since: 2.24
156    **/
157   g_object_class_install_property (gobject_class,
158                                    PROP_DATA,
159                                    g_param_spec_pointer ("data",
160                                                          P_("Data Buffer"),
161                                                          P_("Pointer to buffer where data will be written."),
162                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
163                                                          G_PARAM_STATIC_STRINGS));
164
165   /**
166    * GMemoryOutputStream:size:
167    *
168    * Current size of the data buffer.
169    *
170    * Since: 2.24
171    **/
172   g_object_class_install_property (gobject_class,
173                                    PROP_SIZE,
174                                    g_param_spec_ulong ("size",
175                                                        P_("Data Buffer Size"),
176                                                        P_("Current size of the data buffer."),
177                                                        0, G_MAXULONG, 0,
178                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
179                                                        G_PARAM_STATIC_STRINGS));
180
181   /**
182    * GMemoryOutputStream:data-size:
183    *
184    * Size of data written to the buffer.
185    *
186    * Since: 2.24
187    **/
188   g_object_class_install_property (gobject_class,
189                                    PROP_DATA_SIZE,
190                                    g_param_spec_ulong ("data-size",
191                                                        P_("Data Size"),
192                                                        P_("Size of data written to the buffer."),
193                                                        0, G_MAXULONG, 0,
194                                                        G_PARAM_READABLE |
195                                                        G_PARAM_STATIC_STRINGS));
196
197   /**
198    * GMemoryOutputStream:realloc-function:
199    *
200    * Function with realloc semantics called to enlarge the buffer.
201    *
202    * Since: 2.24
203    **/
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));
211
212   /**
213    * GMemoryOutputStream:destroy-function:
214    *
215    * Function called with the buffer as argument when the stream is destroyed.
216    *
217    * Since: 2.24
218    **/
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));
226 }
227
228 static void
229 g_memory_output_stream_set_property (GObject      *object,
230                                      guint         prop_id,
231                                      const GValue *value,
232                                      GParamSpec   *pspec)
233 {
234   GMemoryOutputStream        *stream;
235   GMemoryOutputStreamPrivate *priv;
236
237   stream = G_MEMORY_OUTPUT_STREAM (object);
238   priv = stream->priv;
239
240   switch (prop_id)
241     {
242     case PROP_DATA:
243       priv->data = g_value_get_pointer (value);
244       break;
245     case PROP_SIZE:
246       priv->len = g_value_get_ulong (value);
247       break;
248     case PROP_REALLOC_FUNCTION:
249       priv->realloc_fn = g_value_get_pointer (value);
250       break;
251     case PROP_DESTROY_FUNCTION:
252       priv->destroy = g_value_get_pointer (value);
253       break;
254     default:
255       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256       break;
257     }
258 }
259
260 static void
261 g_memory_output_stream_get_property (GObject      *object,
262                                      guint         prop_id,
263                                      GValue       *value,
264                                      GParamSpec   *pspec)
265 {
266   GMemoryOutputStream        *stream;
267   GMemoryOutputStreamPrivate *priv;
268
269   stream = G_MEMORY_OUTPUT_STREAM (object);
270   priv = stream->priv;
271
272   switch (prop_id)
273     {
274     case PROP_DATA:
275       g_value_set_pointer (value, priv->data);
276       break;
277     case PROP_SIZE:
278       g_value_set_ulong (value, priv->len);
279       break;
280     case PROP_DATA_SIZE:
281       g_value_set_ulong (value, priv->valid_len);
282       break;
283     case PROP_REALLOC_FUNCTION:
284       g_value_set_pointer (value, priv->realloc_fn);
285       break;
286     case PROP_DESTROY_FUNCTION:
287       g_value_set_pointer (value, priv->destroy);
288       break;
289     default:
290       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291       break;
292     }
293 }
294
295 static void
296 g_memory_output_stream_finalize (GObject *object)
297 {
298   GMemoryOutputStream        *stream;
299   GMemoryOutputStreamPrivate *priv;
300
301   stream = G_MEMORY_OUTPUT_STREAM (object);
302   priv = stream->priv;
303   
304   if (priv->destroy)
305     priv->destroy (priv->data);
306
307   G_OBJECT_CLASS (g_memory_output_stream_parent_class)->finalize (object);
308 }
309
310 static void
311 g_memory_output_stream_seekable_iface_init (GSeekableIface *iface)
312 {
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;
318 }
319
320
321 static void
322 g_memory_output_stream_init (GMemoryOutputStream *stream)
323 {
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;
329 }
330
331 /**
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 to be called when
336  *     @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
339  *
340  * Creates a new #GMemoryOutputStream.
341  *
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.
346  * |[
347  * /&ast; a stream that can grow &ast;/
348  * stream = g_memory_output_stream_new (NULL, 0, realloc, free);
349  *
350  * /&ast; a fixed-size stream &ast;/
351  * data = malloc (200);
352  * stream2 = g_memory_output_stream_new (data, 200, NULL, free);
353  * ]|
354  *
355  * Return value: A newly created #GMemoryOutputStream object.
356  **/
357 GOutputStream *
358 g_memory_output_stream_new (gpointer       data,
359                             gsize          size,
360                             GReallocFunc   realloc_function,
361                             GDestroyNotify destroy_function)
362 {
363   GOutputStream *stream;
364
365   stream = g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
366                          "data", data,
367                          "size", size,
368                          "realloc-function", realloc_function,
369                          "destroy-function", destroy_function,
370                          NULL);
371
372   return stream;
373 }
374
375 /**
376  * g_memory_output_stream_get_data:
377  * @ostream: a #GMemoryOutputStream
378  *
379  * Gets any loaded data from the @ostream.
380  *
381  * Note that the returned pointer may become invalid on the next
382  * write or truncate operation on the stream.
383  *
384  * Returns: pointer to the stream's data
385  **/
386 gpointer
387 g_memory_output_stream_get_data (GMemoryOutputStream *ostream)
388 {
389   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), NULL);
390
391   return ostream->priv->data;
392 }
393
394 /**
395  * g_memory_output_stream_get_size:
396  * @ostream: a #GMemoryOutputStream
397  *
398  * Gets the size of the currently allocated data area (availible from
399  * g_memory_output_stream_get_data()). If the stream isn't
400  * growable (no realloc was passed to g_memory_output_stream_new()) then
401  * this is the maximum size of the stream and further writes
402  * will return %G_IO_ERROR_NO_SPACE.
403  *
404  * Note that for growable streams the returned size may become invalid on
405  * the next write or truncate operation on the stream.
406  *
407  * If you want the number of bytes currently written to the stream, use
408  * g_memory_output_stream_get_data_size().
409  *
410  * Returns: the number of bytes allocated for the data buffer
411  */
412 gsize
413 g_memory_output_stream_get_size (GMemoryOutputStream *ostream)
414 {
415   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
416
417   return ostream->priv->len;
418 }
419
420 /**
421  * g_memory_output_stream_get_data_size:
422  * @ostream: a #GMemoryOutputStream
423  *
424  * Returns the number of bytes from the start up
425  * to including the last byte written in the stream
426  * that has not been truncated away.
427  *
428  * Returns: the number of bytes written to the stream
429  *
430  * Since: 2.18
431  */
432 gsize
433 g_memory_output_stream_get_data_size (GMemoryOutputStream *ostream)
434 {
435   g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (ostream), 0);
436
437   return ostream->priv->valid_len;
438 }
439
440 static gboolean
441 array_resize (GMemoryOutputStream  *ostream,
442               gsize                 size,
443               gboolean              allow_partial,
444               GError              **error)
445 {
446   GMemoryOutputStreamPrivate *priv;
447   gpointer data;
448   gsize len;
449
450   priv = ostream->priv;
451
452   if (priv->len == size)
453     return TRUE;
454
455   if (!priv->realloc_fn)
456     {
457       if (allow_partial &&
458           priv->pos < priv->len)
459         return TRUE; /* Short write */
460
461       g_set_error_literal (error,
462                            G_IO_ERROR,
463                            G_IO_ERROR_NO_SPACE,
464                            _("Memory output stream not resizable"));
465       return FALSE;
466     }
467
468   len = priv->len;
469   data = priv->realloc_fn (priv->data, size);
470
471   if (size > 0 && !data)
472     {
473       if (allow_partial &&
474           priv->pos < priv->len)
475         return TRUE; /* Short write */
476
477       g_set_error_literal (error,
478                            G_IO_ERROR,
479                            G_IO_ERROR_NO_SPACE,
480                            _("Failed to resize memory output stream"));
481       return FALSE;
482     }
483
484   if (size > len)
485     memset ((guint8 *)data + len, 0, size - len);
486
487   priv->data = data;
488   priv->len = size;
489
490   if (priv->len < priv->valid_len)
491     priv->valid_len = priv->len;
492
493   return TRUE;
494 }
495
496 static gint
497 g_nearest_pow (gint num)
498 {
499   gint n = 1;
500
501   while (n < num)
502     n <<= 1;
503
504   return n;
505 }
506
507 static gssize
508 g_memory_output_stream_write (GOutputStream  *stream,
509                               const void     *buffer,
510                               gsize           count,
511                               GCancellable   *cancellable,
512                               GError        **error)
513 {
514   GMemoryOutputStream        *ostream;
515   GMemoryOutputStreamPrivate *priv;
516   guint8   *dest;
517   gsize new_size;
518
519   ostream = G_MEMORY_OUTPUT_STREAM (stream);
520   priv = ostream->priv;
521
522   if (count == 0)
523     return 0;
524
525   /* Check for address space overflow, but only if the buffer is resizable.
526      Otherwise we just do a short write and don't worry. */
527   if (priv->realloc_fn && priv->pos + count < priv->pos)
528     goto overflow;
529
530   if (priv->pos + count > priv->len)
531     {
532       /* At least enought to fit the write, rounded up
533              for greater than linear growth.
534          TODO: This wastes a lot of memory at large stream sizes.
535                Figure out a more rational allocation strategy. */
536       new_size = g_nearest_pow (priv->pos + count);
537       /* Check for overflow again. We have only checked if
538          pos + count > G_MAXSIZE, but it only catches the case of writing
539          more than 4GiB total on a 32-bit system. There's still the problem
540          of g_nearest_pow overflowing above 0x7fffffff, so we're
541          effectively limited to 2GiB. */
542       if (new_size < priv->len)
543         goto overflow;
544
545       new_size = MAX (new_size, MIN_ARRAY_SIZE);
546       if (!array_resize (ostream, new_size, TRUE, error))
547         return -1;
548     }
549
550   /* Make sure we handle short writes if the array_resize
551      only added part of the required memory */
552   count = MIN (count, priv->len - priv->pos);
553
554   dest = (guint8 *)priv->data + priv->pos;
555   memcpy (dest, buffer, count);
556   priv->pos += count;
557
558   if (priv->pos > priv->valid_len)
559     priv->valid_len = priv->pos;
560
561   return count;
562
563  overflow:
564   /* Overflow: buffer size would need to be bigger than G_MAXSIZE. */
565   g_set_error_literal (error,
566                        G_IO_ERROR,
567                        G_IO_ERROR_NO_SPACE,
568                        _("Amount of memory required to process the write is "
569                          "larger than available address space"));
570   return -1;
571 }
572
573 static gboolean
574 g_memory_output_stream_close (GOutputStream  *stream,
575                               GCancellable   *cancellable,
576                               GError        **error)
577 {
578   return TRUE;
579 }
580
581 static void
582 g_memory_output_stream_write_async (GOutputStream       *stream,
583                                     const void          *buffer,
584                                     gsize                count,
585                                     int                  io_priority,
586                                     GCancellable        *cancellable,
587                                     GAsyncReadyCallback  callback,
588                                     gpointer             data)
589 {
590   GSimpleAsyncResult *simple;
591   gssize nwritten;
592
593   nwritten = g_memory_output_stream_write (stream,
594                                            buffer,
595                                            count,
596                                            cancellable,
597                                            NULL);
598
599   simple = g_simple_async_result_new (G_OBJECT (stream),
600                                       callback,
601                                       data,
602                                       g_memory_output_stream_write_async);
603
604   g_simple_async_result_set_op_res_gssize (simple, nwritten);
605   g_simple_async_result_complete_in_idle (simple);
606   g_object_unref (simple);
607 }
608
609 static gssize
610 g_memory_output_stream_write_finish (GOutputStream  *stream,
611                                      GAsyncResult   *result,
612                                      GError        **error)
613 {
614   GSimpleAsyncResult *simple;
615   gssize nwritten;
616
617   simple = G_SIMPLE_ASYNC_RESULT (result);
618
619   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
620                   g_memory_output_stream_write_async);
621
622   nwritten = g_simple_async_result_get_op_res_gssize (simple);
623
624   return nwritten;
625 }
626
627 static void
628 g_memory_output_stream_close_async (GOutputStream       *stream,
629                                     int                  io_priority,
630                                     GCancellable        *cancellable,
631                                     GAsyncReadyCallback  callback,
632                                     gpointer             data)
633 {
634   GSimpleAsyncResult *simple;
635
636   simple = g_simple_async_result_new (G_OBJECT (stream),
637                                       callback,
638                                       data,
639                                       g_memory_output_stream_close_async);
640
641
642   /* will always return TRUE */
643   g_memory_output_stream_close (stream, cancellable, NULL);
644
645   g_simple_async_result_complete_in_idle (simple);
646   g_object_unref (simple);
647 }
648
649 static gboolean
650 g_memory_output_stream_close_finish (GOutputStream  *stream,
651                                      GAsyncResult   *result,
652                                      GError        **error)
653 {
654   GSimpleAsyncResult *simple;
655
656   simple = G_SIMPLE_ASYNC_RESULT (result);
657
658   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) ==
659                   g_memory_output_stream_close_async);
660
661   return TRUE;
662 }
663
664 static goffset
665 g_memory_output_stream_tell (GSeekable *seekable)
666 {
667   GMemoryOutputStream *stream;
668   GMemoryOutputStreamPrivate *priv;
669
670   stream = G_MEMORY_OUTPUT_STREAM (seekable);
671   priv = stream->priv;
672
673   return priv->pos;
674 }
675
676 static gboolean
677 g_memory_output_stream_can_seek (GSeekable *seekable)
678 {
679   return TRUE;
680 }
681
682 static gboolean
683 g_memory_output_stream_seek (GSeekable    *seekable,
684                             goffset        offset,
685                             GSeekType      type,
686                             GCancellable  *cancellable,
687                             GError       **error)
688 {
689   GMemoryOutputStream        *stream;
690   GMemoryOutputStreamPrivate *priv;
691   goffset absolute;
692
693   stream = G_MEMORY_OUTPUT_STREAM (seekable);
694   priv = stream->priv;
695
696   switch (type)
697     {
698     case G_SEEK_CUR:
699       absolute = priv->pos + offset;
700       break;
701
702     case G_SEEK_SET:
703       absolute = offset;
704       break;
705
706     case G_SEEK_END:
707       absolute = priv->len + offset;
708       break;
709
710     default:
711       g_set_error_literal (error,
712                            G_IO_ERROR,
713                            G_IO_ERROR_INVALID_ARGUMENT,
714                            _("Invalid GSeekType supplied"));
715
716       return FALSE;
717     }
718
719   if (absolute < 0)
720     {
721       g_set_error_literal (error,
722                            G_IO_ERROR,
723                            G_IO_ERROR_INVALID_ARGUMENT,
724                            _("Requested seek before the beginning of the stream"));
725       return FALSE;
726     }
727
728   if (absolute > priv->len)
729     {
730       g_set_error_literal (error,
731                            G_IO_ERROR,
732                            G_IO_ERROR_INVALID_ARGUMENT,
733                            _("Requested seek beyond the end of the stream"));
734       return FALSE;
735     }
736
737   priv->pos = absolute;
738
739   return TRUE;
740 }
741
742 static gboolean
743 g_memory_output_stream_can_truncate (GSeekable *seekable)
744 {
745   GMemoryOutputStream *ostream;
746   GMemoryOutputStreamPrivate *priv;
747
748   ostream = G_MEMORY_OUTPUT_STREAM (seekable);
749   priv = ostream->priv;
750
751   return priv->realloc_fn != NULL;
752 }
753
754 static gboolean
755 g_memory_output_stream_truncate (GSeekable     *seekable,
756                                  goffset        offset,
757                                  GCancellable  *cancellable,
758                                  GError       **error)
759 {
760   GMemoryOutputStream *ostream = G_MEMORY_OUTPUT_STREAM (seekable);
761
762   if (!array_resize (ostream, offset, FALSE, error))
763     return FALSE;
764
765   return TRUE;
766 }
767
768 #define __G_MEMORY_OUTPUT_STREAM_C__
769 #include "gioaliasdef.c"