gfileutils: Add missing g_free() in error path
[platform/upstream/glib.git] / glib / garray.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 /* 
26  * MT safe
27  */
28
29 #include "config.h"
30
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "garray.h"
35
36 #include "gbytes.h"
37 #include "gslice.h"
38 #include "gmem.h"
39 #include "gtestutils.h"
40 #include "gthread.h"
41 #include "gmessages.h"
42 #include "gqsort.h"
43
44
45 /**
46  * SECTION:arrays
47  * @title: Arrays
48  * @short_description: arrays of arbitrary elements which grow
49  *     automatically as elements are added
50  *
51  * Arrays are similar to standard C arrays, except that they grow
52  * automatically as elements are added.
53  *
54  * Array elements can be of any size (though all elements of one array
55  * are the same size), and the array can be automatically cleared to
56  * '0's and zero-terminated.
57  *
58  * To create a new array use g_array_new().
59  *
60  * To add elements to an array, use g_array_append_val(),
61  * g_array_append_vals(), g_array_prepend_val(), and
62  * g_array_prepend_vals().
63  *
64  * To access an element of an array, use g_array_index().
65  *
66  * To set the size of an array, use g_array_set_size().
67  *
68  * To free an array, use g_array_free().
69  *
70  * Here is an example that stores integers in a #GArray:
71  * |[<!-- language="C" -->
72  *   GArray *garray;
73  *   gint i;
74  *   // We create a new array to store gint values.
75  *   // We don't want it zero-terminated or cleared to 0's.
76  *   garray = g_array_new (FALSE, FALSE, sizeof (gint));
77  *   for (i = 0; i < 10000; i++)
78  *     g_array_append_val (garray, i);
79  *   for (i = 0; i < 10000; i++)
80  *     if (g_array_index (garray, gint, i) != i)
81  *       g_print ("ERROR: got %d instead of %d\n",
82  *                g_array_index (garray, gint, i), i);
83  *   g_array_free (garray, TRUE);
84  * ]|
85  */
86
87 #define MIN_ARRAY_SIZE  16
88
89 typedef struct _GRealArray  GRealArray;
90
91 /**
92  * GArray:
93  * @data: a pointer to the element data. The data may be moved as
94  *     elements are added to the #GArray.
95  * @len: the number of elements in the #GArray not including the
96  *     possible terminating zero element.
97  *
98  * Contains the public fields of a GArray.
99  */
100 struct _GRealArray
101 {
102   guint8 *data;
103   guint   len;
104   guint   alloc;
105   guint   elt_size;
106   guint   zero_terminated : 1;
107   guint   clear : 1;
108   gint    ref_count;
109   GDestroyNotify clear_func;
110 };
111
112 /**
113  * g_array_index:
114  * @a: a #GArray
115  * @t: the type of the elements
116  * @i: the index of the element to return
117  *
118  * Returns the element of a #GArray at the given index. The return
119  * value is cast to the given type.
120  *
121  * This example gets a pointer to an element in a #GArray:
122  * |[<!-- language="C" -->
123  *   EDayViewEvent *event;
124  *   // This gets a pointer to the 4th element in the array of
125  *   // EDayViewEvent structs.
126  *   event = &g_array_index (events, EDayViewEvent, 3);
127  * ]|
128  *
129  * Returns: the element of the #GArray at the index given by @i
130  */
131
132 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
133 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
134 #define g_array_elt_zero(array, pos, len)                               \
135   (memset (g_array_elt_pos ((array), pos), 0,  g_array_elt_len ((array), len)))
136 #define g_array_zero_terminate(array) G_STMT_START{                     \
137   if ((array)->zero_terminated)                                         \
138     g_array_elt_zero ((array), (array)->len, 1);                        \
139 }G_STMT_END
140
141 static guint g_nearest_pow        (gint        num) G_GNUC_CONST;
142 static void  g_array_maybe_expand (GRealArray *array,
143                                    gint        len);
144
145 /**
146  * g_array_new:
147  * @zero_terminated: %TRUE if the array should have an extra element at
148  *     the end which is set to 0
149  * @clear_: %TRUE if #GArray elements should be automatically cleared
150  *     to 0 when they are allocated
151  * @element_size: the size of each element in bytes
152  *
153  * Creates a new #GArray with a reference count of 1.
154  *
155  * Returns: the new #GArray
156  */
157 GArray*
158 g_array_new (gboolean zero_terminated,
159              gboolean clear,
160              guint    elt_size)
161 {
162   g_return_val_if_fail (elt_size > 0, NULL);
163
164   return g_array_sized_new (zero_terminated, clear, elt_size, 0);
165 }
166
167 /**
168  * g_array_sized_new:
169  * @zero_terminated: %TRUE if the array should have an extra element at
170  *     the end with all bits cleared
171  * @clear_: %TRUE if all bits in the array should be cleared to 0 on
172  *     allocation
173  * @element_size: size of each element in the array
174  * @reserved_size: number of elements preallocated
175  *
176  * Creates a new #GArray with @reserved_size elements preallocated and
177  * a reference count of 1. This avoids frequent reallocation, if you
178  * are going to add many elements to the array. Note however that the
179  * size of the array is still 0.
180  *
181  * Returns: the new #GArray
182  */
183 GArray*
184 g_array_sized_new (gboolean zero_terminated,
185                    gboolean clear,
186                    guint    elt_size,
187                    guint    reserved_size)
188 {
189   GRealArray *array;
190   
191   g_return_val_if_fail (elt_size > 0, NULL);
192
193   array = g_slice_new (GRealArray);
194
195   array->data            = NULL;
196   array->len             = 0;
197   array->alloc           = 0;
198   array->zero_terminated = (zero_terminated ? 1 : 0);
199   array->clear           = (clear ? 1 : 0);
200   array->elt_size        = elt_size;
201   array->ref_count       = 1;
202   array->clear_func      = NULL;
203
204   if (array->zero_terminated || reserved_size != 0)
205     {
206       g_array_maybe_expand (array, reserved_size);
207       g_array_zero_terminate(array);
208     }
209
210   return (GArray*) array;
211 }
212
213 /**
214  * g_array_set_clear_func:
215  * @array: A #GArray
216  * @clear_func: a function to clear an element of @array
217  *
218  * Sets a function to clear an element of @array.
219  *
220  * The @clear_func will be called when an element in the array
221  * data segment is removed and when the array is freed and data
222  * segment is deallocated as well.
223  *
224  * Note that in contrast with other uses of #GDestroyNotify
225  * functions, @clear_func is expected to clear the contents of
226  * the array element it is given, but not free the element itself.
227  *
228  * Since: 2.32
229  */
230 void
231 g_array_set_clear_func (GArray         *array,
232                         GDestroyNotify  clear_func)
233 {
234   GRealArray *rarray = (GRealArray *) array;
235
236   g_return_if_fail (array != NULL);
237
238   rarray->clear_func = clear_func;
239 }
240
241 /**
242  * g_array_ref:
243  * @array: A #GArray
244  *
245  * Atomically increments the reference count of @array by one.
246  * This function is MT-safe and may be called from any thread.
247  *
248  * Returns: The passed in #GArray
249  *
250  * Since: 2.22
251  */
252 GArray *
253 g_array_ref (GArray *array)
254 {
255   GRealArray *rarray = (GRealArray*) array;
256   g_return_val_if_fail (array, NULL);
257
258   g_atomic_int_inc (&rarray->ref_count);
259
260   return array;
261 }
262
263 typedef enum
264 {
265   FREE_SEGMENT = 1 << 0,
266   PRESERVE_WRAPPER = 1 << 1
267 } ArrayFreeFlags;
268
269 static gchar *array_free (GRealArray *, ArrayFreeFlags);
270
271 /**
272  * g_array_unref:
273  * @array: A #GArray
274  *
275  * Atomically decrements the reference count of @array by one. If the
276  * reference count drops to 0, all memory allocated by the array is
277  * released. This function is MT-safe and may be called from any
278  * thread.
279  *
280  * Since: 2.22
281  */
282 void
283 g_array_unref (GArray *array)
284 {
285   GRealArray *rarray = (GRealArray*) array;
286   g_return_if_fail (array);
287
288   if (g_atomic_int_dec_and_test (&rarray->ref_count))
289     array_free (rarray, FREE_SEGMENT);
290 }
291
292 /**
293  * g_array_get_element_size:
294  * @array: A #GArray
295  *
296  * Gets the size of the elements in @array.
297  *
298  * Returns: Size of each element, in bytes
299  *
300  * Since: 2.22
301  */
302 guint
303 g_array_get_element_size (GArray *array)
304 {
305   GRealArray *rarray = (GRealArray*) array;
306
307   g_return_val_if_fail (array, 0);
308
309   return rarray->elt_size;
310 }
311
312 /**
313  * g_array_free:
314  * @array: a #GArray
315  * @free_segment: if %TRUE the actual element data is freed as well
316  *
317  * Frees the memory allocated for the #GArray. If @free_segment is
318  * %TRUE it frees the memory block holding the elements as well and
319  * also each element if @array has a @element_free_func set. Pass
320  * %FALSE if you want to free the #GArray wrapper but preserve the
321  * underlying array for use elsewhere. If the reference count of @array
322  * is greater than one, the #GArray wrapper is preserved but the size
323  * of @array will be set to zero.
324  *
325  * If array elements contain dynamically-allocated memory, they should
326  * be freed separately.
327  *
328  * Returns: the element data if @free_segment is %FALSE, otherwise
329  *     %NULL. The element data should be freed using g_free().
330  */
331 gchar*
332 g_array_free (GArray   *farray,
333               gboolean  free_segment)
334 {
335   GRealArray *array = (GRealArray*) farray;
336   ArrayFreeFlags flags;
337
338   g_return_val_if_fail (array, NULL);
339
340   flags = (free_segment ? FREE_SEGMENT : 0);
341
342   /* if others are holding a reference, preserve the wrapper but do free/return the data */
343   if (!g_atomic_int_dec_and_test (&array->ref_count))
344     flags |= PRESERVE_WRAPPER;
345
346   return array_free (array, flags);
347 }
348
349 static gchar *
350 array_free (GRealArray     *array,
351             ArrayFreeFlags  flags)
352 {
353   gchar *segment;
354
355   if (flags & FREE_SEGMENT)
356     {
357       if (array->clear_func != NULL)
358         {
359           guint i;
360
361           for (i = 0; i < array->len; i++)
362             array->clear_func (g_array_elt_pos (array, i));
363         }
364
365       g_free (array->data);
366       segment = NULL;
367     }
368   else
369     segment = (gchar*) array->data;
370
371   if (flags & PRESERVE_WRAPPER)
372     {
373       array->data            = NULL;
374       array->len             = 0;
375       array->alloc           = 0;
376     }
377   else
378     {
379       g_slice_free1 (sizeof (GRealArray), array);
380     }
381
382   return segment;
383 }
384
385 /**
386  * g_array_append_vals:
387  * @array: a #GArray
388  * @data: a pointer to the elements to append to the end of the array
389  * @len: the number of elements to append
390  *
391  * Adds @len elements onto the end of the array.
392  *
393  * Returns: the #GArray
394  */
395 /**
396  * g_array_append_val:
397  * @a: a #GArray
398  * @v: the value to append to the #GArray
399  *
400  * Adds the value on to the end of the array. The array will grow in
401  * size automatically if necessary.
402  *
403  * g_array_append_val() is a macro which uses a reference to the value
404  * parameter @v. This means that you cannot use it with literal values
405  * such as "27". You must use variables.
406  *
407  * Returns: the #GArray
408  */
409 GArray*
410 g_array_append_vals (GArray       *farray,
411                      gconstpointer data,
412                      guint         len)
413 {
414   GRealArray *array = (GRealArray*) farray;
415
416   g_return_val_if_fail (array, NULL);
417
418   g_array_maybe_expand (array, len);
419
420   memcpy (g_array_elt_pos (array, array->len), data, 
421           g_array_elt_len (array, len));
422
423   array->len += len;
424
425   g_array_zero_terminate (array);
426
427   return farray;
428 }
429
430 /**
431  * g_array_prepend_vals:
432  * @array: a #GArray
433  * @data: a pointer to the elements to prepend to the start of the array
434  * @len: the number of elements to prepend
435  *
436  * Adds @len elements onto the start of the array.
437  *
438  * This operation is slower than g_array_append_vals() since the
439  * existing elements in the array have to be moved to make space for
440  * the new elements.
441  *
442  * Returns: the #GArray
443  */
444 /**
445  * g_array_prepend_val:
446  * @a: a #GArray
447  * @v: the value to prepend to the #GArray
448  *
449  * Adds the value on to the start of the array. The array will grow in
450  * size automatically if necessary.
451  *
452  * This operation is slower than g_array_append_val() since the
453  * existing elements in the array have to be moved to make space for
454  * the new element.
455  *
456  * g_array_prepend_val() is a macro which uses a reference to the value
457  * parameter @v. This means that you cannot use it with literal values
458  * such as "27". You must use variables.
459  *
460  * Returns: the #GArray
461  */
462 GArray*
463 g_array_prepend_vals (GArray        *farray,
464                       gconstpointer  data,
465                       guint          len)
466 {
467   GRealArray *array = (GRealArray*) farray;
468
469   g_return_val_if_fail (array, NULL);
470
471   g_array_maybe_expand (array, len);
472
473   memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
474            g_array_elt_len (array, array->len));
475
476   memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
477
478   array->len += len;
479
480   g_array_zero_terminate (array);
481
482   return farray;
483 }
484
485 /**
486  * g_array_insert_vals:
487  * @array: a #GArray
488  * @index_: the index to place the elements at
489  * @data: a pointer to the elements to insert
490  * @len: the number of elements to insert
491  *
492  * Inserts @len elements into a #GArray at the given index.
493  *
494  * Returns: the #GArray
495  */
496 /**
497  * g_array_insert_val:
498  * @a: a #GArray
499  * @i: the index to place the element at
500  * @v: the value to insert into the array
501  *
502  * Inserts an element into an array at the given index.
503  *
504  * g_array_insert_val() is a macro which uses a reference to the value
505  * parameter @v. This means that you cannot use it with literal values
506  * such as "27". You must use variables.
507  *
508  * Returns: the #GArray
509  */
510 GArray*
511 g_array_insert_vals (GArray        *farray,
512                      guint          index_,
513                      gconstpointer  data,
514                      guint          len)
515 {
516   GRealArray *array = (GRealArray*) farray;
517
518   g_return_val_if_fail (array, NULL);
519
520   g_array_maybe_expand (array, len);
521
522   memmove (g_array_elt_pos (array, len + index_),
523            g_array_elt_pos (array, index_),
524            g_array_elt_len (array, array->len - index_));
525
526   memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
527
528   array->len += len;
529
530   g_array_zero_terminate (array);
531
532   return farray;
533 }
534
535 /**
536  * g_array_set_size:
537  * @array: a #GArray
538  * @length: the new size of the #GArray
539  *
540  * Sets the size of the array, expanding it if necessary. If the array
541  * was created with @clear_ set to %TRUE, the new elements are set to 0.
542  *
543  * Returns: the #GArray
544  */
545 GArray*
546 g_array_set_size (GArray *farray,
547                   guint   length)
548 {
549   GRealArray *array = (GRealArray*) farray;
550
551   g_return_val_if_fail (array, NULL);
552
553   if (length > array->len)
554     {
555       g_array_maybe_expand (array, length - array->len);
556       
557       if (array->clear)
558         g_array_elt_zero (array, array->len, length - array->len);
559     }
560   else if (length < array->len)
561     g_array_remove_range (farray, length, array->len - length);
562   
563   array->len = length;
564   
565   g_array_zero_terminate (array);
566   
567   return farray;
568 }
569
570 /**
571  * g_array_remove_index:
572  * @array: a #GArray
573  * @index_: the index of the element to remove
574  *
575  * Removes the element at the given index from a #GArray. The following
576  * elements are moved down one place.
577  *
578  * Returns: the #GArray
579  */
580 GArray*
581 g_array_remove_index (GArray *farray,
582                       guint   index_)
583 {
584   GRealArray* array = (GRealArray*) farray;
585
586   g_return_val_if_fail (array, NULL);
587
588   g_return_val_if_fail (index_ < array->len, NULL);
589
590   if (array->clear_func != NULL)
591     array->clear_func (g_array_elt_pos (array, index_));
592
593   if (index_ != array->len - 1)
594     memmove (g_array_elt_pos (array, index_),
595              g_array_elt_pos (array, index_ + 1),
596              g_array_elt_len (array, array->len - index_ - 1));
597
598   array->len -= 1;
599
600   if (G_UNLIKELY (g_mem_gc_friendly))
601     g_array_elt_zero (array, array->len, 1);
602   else
603     g_array_zero_terminate (array);
604
605   return farray;
606 }
607
608 /**
609  * g_array_remove_index_fast:
610  * @array: a @GArray
611  * @index_: the index of the element to remove
612  *
613  * Removes the element at the given index from a #GArray. The last
614  * element in the array is used to fill in the space, so this function
615  * does not preserve the order of the #GArray. But it is faster than
616  * g_array_remove_index().
617  *
618  * Returns: the #GArray
619  */
620 GArray*
621 g_array_remove_index_fast (GArray *farray,
622                            guint   index_)
623 {
624   GRealArray* array = (GRealArray*) farray;
625
626   g_return_val_if_fail (array, NULL);
627
628   g_return_val_if_fail (index_ < array->len, NULL);
629
630   if (array->clear_func != NULL)
631     array->clear_func (g_array_elt_pos (array, index_));
632
633   if (index_ != array->len - 1)
634     memcpy (g_array_elt_pos (array, index_),
635             g_array_elt_pos (array, array->len - 1),
636             g_array_elt_len (array, 1));
637   
638   array->len -= 1;
639
640   if (G_UNLIKELY (g_mem_gc_friendly))
641     g_array_elt_zero (array, array->len, 1);
642   else
643     g_array_zero_terminate (array);
644
645   return farray;
646 }
647
648 /**
649  * g_array_remove_range:
650  * @array: a @GArray
651  * @index_: the index of the first element to remove
652  * @length: the number of elements to remove
653  *
654  * Removes the given number of elements starting at the given index
655  * from a #GArray.  The following elements are moved to close the gap.
656  *
657  * Returns: the #GArray
658  *
659  * Since: 2.4
660  */
661 GArray*
662 g_array_remove_range (GArray *farray,
663                       guint   index_,
664                       guint   length)
665 {
666   GRealArray *array = (GRealArray*) farray;
667
668   g_return_val_if_fail (array, NULL);
669   g_return_val_if_fail (index_ < array->len, NULL);
670   g_return_val_if_fail (index_ + length <= array->len, NULL);
671
672   if (array->clear_func != NULL)
673     {
674       guint i;
675
676       for (i = 0; i < length; i++)
677         array->clear_func (g_array_elt_pos (array, index_ + i));
678     }
679
680   if (index_ + length != array->len)
681     memmove (g_array_elt_pos (array, index_),
682              g_array_elt_pos (array, index_ + length),
683              (array->len - (index_ + length)) * array->elt_size);
684
685   array->len -= length;
686   if (G_UNLIKELY (g_mem_gc_friendly))
687     g_array_elt_zero (array, array->len, length);
688   else
689     g_array_zero_terminate (array);
690
691   return farray;
692 }
693
694 /**
695  * g_array_sort:
696  * @array: a #GArray
697  * @compare_func: comparison function
698  *
699  * Sorts a #GArray using @compare_func which should be a qsort()-style
700  * comparison function (returns less than zero for first arg is less
701  * than second arg, zero for equal, greater zero if first arg is
702  * greater than second arg).
703  *
704  * This is guaranteed to be a stable sort since version 2.32.
705  */
706 void
707 g_array_sort (GArray       *farray,
708               GCompareFunc  compare_func)
709 {
710   GRealArray *array = (GRealArray*) farray;
711
712   g_return_if_fail (array != NULL);
713
714   /* Don't use qsort as we want a guaranteed stable sort */
715   g_qsort_with_data (array->data,
716                      array->len,
717                      array->elt_size,
718                      (GCompareDataFunc)compare_func,
719                      NULL);
720 }
721
722 /**
723  * g_array_sort_with_data:
724  * @array: a #GArray
725  * @compare_func: comparison function
726  * @user_data: data to pass to @compare_func
727  *
728  * Like g_array_sort(), but the comparison function receives an extra
729  * user data argument.
730  *
731  * This is guaranteed to be a stable sort since version 2.32.
732  *
733  * There used to be a comment here about making the sort stable by
734  * using the addresses of the elements in the comparison function.
735  * This did not actually work, so any such code should be removed.
736  */
737 void
738 g_array_sort_with_data (GArray           *farray,
739                         GCompareDataFunc  compare_func,
740                         gpointer          user_data)
741 {
742   GRealArray *array = (GRealArray*) farray;
743
744   g_return_if_fail (array != NULL);
745
746   g_qsort_with_data (array->data,
747                      array->len,
748                      array->elt_size,
749                      compare_func,
750                      user_data);
751 }
752
753 /* Returns the smallest power of 2 greater than n, or n if
754  * such power does not fit in a guint
755  */
756 static guint
757 g_nearest_pow (gint num)
758 {
759   guint n = 1;
760
761   while (n < num && n > 0)
762     n <<= 1;
763
764   return n ? n : num;
765 }
766
767 static void
768 g_array_maybe_expand (GRealArray *array,
769                       gint        len)
770 {
771   guint want_alloc = g_array_elt_len (array, array->len + len + 
772                                       array->zero_terminated);
773
774   if (want_alloc > array->alloc)
775     {
776       want_alloc = g_nearest_pow (want_alloc);
777       want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
778
779       array->data = g_realloc (array->data, want_alloc);
780
781       if (G_UNLIKELY (g_mem_gc_friendly))
782         memset (array->data + array->alloc, 0, want_alloc - array->alloc);
783
784       array->alloc = want_alloc;
785     }
786 }
787
788 /**
789  * SECTION:arrays_pointer
790  * @title: Pointer Arrays
791  * @short_description: arrays of pointers to any type of data, which
792  *     grow automatically as new elements are added
793  *
794  * Pointer Arrays are similar to Arrays but are used only for storing
795  * pointers.
796  *
797  * If you remove elements from the array, elements at the end of the
798  * array are moved into the space previously occupied by the removed
799  * element. This means that you should not rely on the index of particular
800  * elements remaining the same. You should also be careful when deleting
801  * elements while iterating over the array.
802  *
803  * To create a pointer array, use g_ptr_array_new().
804  *
805  * To add elements to a pointer array, use g_ptr_array_add().
806  *
807  * To remove elements from a pointer array, use g_ptr_array_remove(),
808  * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
809  *
810  * To access an element of a pointer array, use g_ptr_array_index().
811  *
812  * To set the size of a pointer array, use g_ptr_array_set_size().
813  *
814  * To free a pointer array, use g_ptr_array_free().
815  *
816  * An example using a #GPtrArray:
817  * |[<!-- language="C" -->
818  *   GPtrArray *array;
819  *   gchar *string1 = "one", *string2 = "two", *string3 = "three";
820  *
821  *   gparray = g_ptr_array_new ();
822  *   g_ptr_array_add (array, (gpointer) string1);
823  *   g_ptr_array_add (array, (gpointer) string2);
824  *   g_ptr_array_add (array, (gpointer) string3);
825  *
826  *   if (g_ptr_array_index (array, 0) != (gpointer) string1)
827  *     g_print ("ERROR: got %p instead of %p\n",
828  *              g_ptr_array_index (array, 0), string1);
829  *
830  *   g_ptr_array_free (array, TRUE);
831  * ]|
832  */
833
834 typedef struct _GRealPtrArray  GRealPtrArray;
835
836 /**
837  * GPtrArray:
838  * @pdata: points to the array of pointers, which may be moved when the
839  *     array grows
840  * @len: number of pointers in the array
841  *
842  * Contains the public fields of a pointer array.
843  */
844 struct _GRealPtrArray
845 {
846   gpointer       *pdata;
847   guint           len;
848   guint           alloc;
849   gint            ref_count;
850   GDestroyNotify  element_free_func;
851 };
852
853 /**
854  * g_ptr_array_index:
855  * @array: a #GPtrArray
856  * @index_: the index of the pointer to return
857  *
858  * Returns the pointer at the given index of the pointer array.
859  *
860  * This does not perform bounds checking on the given @index_,
861  * so you are responsible for checking it against the array length.
862  *
863  * Returns: the pointer at the given index
864  */
865
866 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
867                                       gint           len);
868
869 /**
870  * g_ptr_array_new:
871  *
872  * Creates a new #GPtrArray with a reference count of 1.
873  *
874  * Returns: the new #GPtrArray
875  */
876 GPtrArray*
877 g_ptr_array_new (void)
878 {
879   return g_ptr_array_sized_new (0);
880 }
881
882 /**
883  * g_ptr_array_sized_new:
884  * @reserved_size: number of pointers preallocated
885  *
886  * Creates a new #GPtrArray with @reserved_size pointers preallocated
887  * and a reference count of 1. This avoids frequent reallocation, if
888  * you are going to add many pointers to the array. Note however that
889  * the size of the array is still 0.
890  *
891  * Returns: the new #GPtrArray
892  */
893 GPtrArray*  
894 g_ptr_array_sized_new (guint reserved_size)
895 {
896   GRealPtrArray *array;
897
898   array = g_slice_new (GRealPtrArray);
899
900   array->pdata = NULL;
901   array->len = 0;
902   array->alloc = 0;
903   array->ref_count = 1;
904   array->element_free_func = NULL;
905
906   if (reserved_size != 0)
907     g_ptr_array_maybe_expand (array, reserved_size);
908
909   return (GPtrArray*) array;  
910 }
911
912 /**
913  * g_ptr_array_new_with_free_func:
914  * @element_free_func: (allow-none): A function to free elements with
915  *     destroy @array or %NULL
916  *
917  * Creates a new #GPtrArray with a reference count of 1 and use
918  * @element_free_func for freeing each element when the array is destroyed
919  * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
920  * @free_segment set to %TRUE or when removing elements.
921  *
922  * Returns: A new #GPtrArray
923  *
924  * Since: 2.22
925  */
926 GPtrArray*
927 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
928 {
929   GPtrArray *array;
930
931   array = g_ptr_array_new ();
932   g_ptr_array_set_free_func (array, element_free_func);
933
934   return array;
935 }
936
937 /**
938  * g_ptr_array_new_full:
939  * @reserved_size: number of pointers preallocated
940  * @element_free_func: (allow-none): A function to free elements with
941  *     destroy @array or %NULL
942  *
943  * Creates a new #GPtrArray with @reserved_size pointers preallocated
944  * and a reference count of 1. This avoids frequent reallocation, if
945  * you are going to add many pointers to the array. Note however that
946  * the size of the array is still 0. It also set @element_free_func
947  * for freeing each element when the array is destroyed either via
948  * g_ptr_array_unref(), when g_ptr_array_free() is called with
949  * @free_segment set to %TRUE or when removing elements.
950  *
951  * Returns: A new #GPtrArray
952  *
953  * Since: 2.30
954  */
955 GPtrArray*
956 g_ptr_array_new_full (guint          reserved_size,
957                       GDestroyNotify element_free_func)
958 {
959   GPtrArray *array;
960
961   array = g_ptr_array_sized_new (reserved_size);
962   g_ptr_array_set_free_func (array, element_free_func);
963
964   return array;
965 }
966
967 /**
968  * g_ptr_array_set_free_func:
969  * @array: A #GPtrArray
970  * @element_free_func: (allow-none): A function to free elements with
971  *     destroy @array or %NULL
972  *
973  * Sets a function for freeing each element when @array is destroyed
974  * either via g_ptr_array_unref(), when g_ptr_array_free() is called
975  * with @free_segment set to %TRUE or when removing elements.
976  *
977  * Since: 2.22
978  */
979 void
980 g_ptr_array_set_free_func (GPtrArray      *array,
981                            GDestroyNotify  element_free_func)
982 {
983   GRealPtrArray *rarray = (GRealPtrArray *)array;
984
985   g_return_if_fail (array);
986
987   rarray->element_free_func = element_free_func;
988 }
989
990 /**
991  * g_ptr_array_ref:
992  * @array: a #GPtrArray
993  *
994  * Atomically increments the reference count of @array by one.
995  * This function is thread-safe and may be called from any thread.
996  *
997  * Returns: The passed in #GPtrArray
998  *
999  * Since: 2.22
1000  */
1001 GPtrArray*
1002 g_ptr_array_ref (GPtrArray *array)
1003 {
1004   GRealPtrArray *rarray = (GRealPtrArray *)array;
1005
1006   g_return_val_if_fail (array, NULL);
1007
1008   g_atomic_int_inc (&rarray->ref_count);
1009
1010   return array;
1011 }
1012
1013 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
1014
1015 /**
1016  * g_ptr_array_unref:
1017  * @array: A #GPtrArray
1018  *
1019  * Atomically decrements the reference count of @array by one. If the
1020  * reference count drops to 0, the effect is the same as calling
1021  * g_ptr_array_free() with @free_segment set to %TRUE. This function
1022  * is MT-safe and may be called from any thread.
1023  *
1024  * Since: 2.22
1025  */
1026 void
1027 g_ptr_array_unref (GPtrArray *array)
1028 {
1029   GRealPtrArray *rarray = (GRealPtrArray *)array;
1030
1031   g_return_if_fail (array);
1032
1033   if (g_atomic_int_dec_and_test (&rarray->ref_count))
1034     ptr_array_free (array, FREE_SEGMENT);
1035 }
1036
1037 /**
1038  * g_ptr_array_free:
1039  * @array: a #GPtrArray
1040  * @free_seg: if %TRUE the actual pointer array is freed as well
1041  *
1042  * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1043  * it frees the memory block holding the elements as well. Pass %FALSE
1044  * if you want to free the #GPtrArray wrapper but preserve the
1045  * underlying array for use elsewhere. If the reference count of @array
1046  * is greater than one, the #GPtrArray wrapper is preserved but the
1047  * size of @array will be set to zero.
1048  *
1049  * If array contents point to dynamically-allocated memory, they should
1050  * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
1051  * function has been set for @array.
1052  *
1053  * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1054  *     The pointer array should be freed using g_free().
1055  */
1056 gpointer*
1057 g_ptr_array_free (GPtrArray *array,
1058                   gboolean   free_segment)
1059 {
1060   GRealPtrArray *rarray = (GRealPtrArray *)array;
1061   ArrayFreeFlags flags;
1062
1063   g_return_val_if_fail (rarray, NULL);
1064
1065   flags = (free_segment ? FREE_SEGMENT : 0);
1066
1067   /* if others are holding a reference, preserve the wrapper but
1068    * do free/return the data
1069    */
1070   if (!g_atomic_int_dec_and_test (&rarray->ref_count))
1071     flags |= PRESERVE_WRAPPER;
1072
1073   return ptr_array_free (array, flags);
1074 }
1075
1076 static gpointer *
1077 ptr_array_free (GPtrArray      *array,
1078                 ArrayFreeFlags  flags)
1079 {
1080   GRealPtrArray *rarray = (GRealPtrArray *)array;
1081   gpointer *segment;
1082
1083   if (flags & FREE_SEGMENT)
1084     {
1085       if (rarray->element_free_func != NULL)
1086         g_ptr_array_foreach (array, (GFunc) rarray->element_free_func, NULL);
1087       g_free (rarray->pdata);
1088       segment = NULL;
1089     }
1090   else
1091     segment = rarray->pdata;
1092
1093   if (flags & PRESERVE_WRAPPER)
1094     {
1095       rarray->pdata = NULL;
1096       rarray->len = 0;
1097       rarray->alloc = 0;
1098     }
1099   else
1100     {
1101       g_slice_free1 (sizeof (GRealPtrArray), rarray);
1102     }
1103
1104   return segment;
1105 }
1106
1107 static void
1108 g_ptr_array_maybe_expand (GRealPtrArray *array,
1109                           gint           len)
1110 {
1111   if ((array->len + len) > array->alloc)
1112     {
1113       guint old_alloc = array->alloc;
1114       array->alloc = g_nearest_pow (array->len + len);
1115       array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1116       array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1117       if (G_UNLIKELY (g_mem_gc_friendly))
1118         for ( ; old_alloc < array->alloc; old_alloc++)
1119           array->pdata [old_alloc] = NULL;
1120     }
1121 }
1122
1123 /**
1124  * g_ptr_array_set_size:
1125  * @array: a #GPtrArray
1126  * @length: the new length of the pointer array
1127  *
1128  * Sets the size of the array. When making the array larger,
1129  * newly-added elements will be set to %NULL. When making it smaller,
1130  * if @array has a non-%NULL #GDestroyNotify function then it will be
1131  * called for the removed elements.
1132  */
1133 void
1134 g_ptr_array_set_size  (GPtrArray *array,
1135                        gint       length)
1136 {
1137   GRealPtrArray *rarray = (GRealPtrArray *)array;
1138
1139   g_return_if_fail (rarray);
1140
1141   if (length > rarray->len)
1142     {
1143       int i;
1144       g_ptr_array_maybe_expand (rarray, (length - rarray->len));
1145       /* This is not 
1146        *     memset (array->pdata + array->len, 0,
1147        *            sizeof (gpointer) * (length - array->len));
1148        * to make it really portable. Remember (void*)NULL needn't be
1149        * bitwise zero. It of course is silly not to use memset (..,0,..).
1150        */
1151       for (i = rarray->len; i < length; i++)
1152         rarray->pdata[i] = NULL;
1153     }
1154   else if (length < rarray->len)
1155     g_ptr_array_remove_range (array, length, rarray->len - length);
1156
1157   rarray->len = length;
1158 }
1159
1160 /**
1161  * g_ptr_array_remove_index:
1162  * @array: a #GPtrArray
1163  * @index_: the index of the pointer to remove
1164  *
1165  * Removes the pointer at the given index from the pointer array.
1166  * The following elements are moved down one place. If @array has
1167  * a non-%NULL #GDestroyNotify function it is called for the removed
1168  * element.
1169  *
1170  * Returns: the pointer which was removed
1171  */
1172 gpointer
1173 g_ptr_array_remove_index (GPtrArray *array,
1174                           guint      index_)
1175 {
1176   GRealPtrArray *rarray = (GRealPtrArray *)array;
1177   gpointer result;
1178
1179   g_return_val_if_fail (rarray, NULL);
1180
1181   g_return_val_if_fail (index_ < rarray->len, NULL);
1182
1183   result = rarray->pdata[index_];
1184   
1185   if (rarray->element_free_func != NULL)
1186     rarray->element_free_func (rarray->pdata[index_]);
1187
1188   if (index_ != rarray->len - 1)
1189     memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
1190              sizeof (gpointer) * (rarray->len - index_ - 1));
1191   
1192   rarray->len -= 1;
1193
1194   if (G_UNLIKELY (g_mem_gc_friendly))
1195     rarray->pdata[rarray->len] = NULL;
1196
1197   return result;
1198 }
1199
1200 /**
1201  * g_ptr_array_remove_index_fast:
1202  * @array: a #GPtrArray
1203  * @index_: the index of the pointer to remove
1204  *
1205  * Removes the pointer at the given index from the pointer array.
1206  * The last element in the array is used to fill in the space, so
1207  * this function does not preserve the order of the array. But it
1208  * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
1209  * #GDestroyNotify function it is called for the removed element.
1210  *
1211  * Returns: the pointer which was removed
1212  */
1213 gpointer
1214 g_ptr_array_remove_index_fast (GPtrArray *array,
1215                                guint      index_)
1216 {
1217   GRealPtrArray *rarray = (GRealPtrArray *)array;
1218   gpointer result;
1219
1220   g_return_val_if_fail (rarray, NULL);
1221
1222   g_return_val_if_fail (index_ < rarray->len, NULL);
1223
1224   result = rarray->pdata[index_];
1225
1226   if (rarray->element_free_func != NULL)
1227     rarray->element_free_func (rarray->pdata[index_]);
1228
1229   if (index_ != rarray->len - 1)
1230     rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
1231
1232   rarray->len -= 1;
1233
1234   if (G_UNLIKELY (g_mem_gc_friendly))
1235     rarray->pdata[rarray->len] = NULL;
1236
1237   return result;
1238 }
1239
1240 /**
1241  * g_ptr_array_remove_range:
1242  * @array: a @GPtrArray
1243  * @index_: the index of the first pointer to remove
1244  * @length: the number of pointers to remove
1245  *
1246  * Removes the given number of pointers starting at the given index
1247  * from a #GPtrArray. The following elements are moved to close the
1248  * gap. If @array has a non-%NULL #GDestroyNotify function it is
1249  * called for the removed elements.
1250  *
1251  * Returns: the @array
1252  *
1253  * Since: 2.4
1254  */
1255 GPtrArray*
1256 g_ptr_array_remove_range (GPtrArray *array,
1257                           guint      index_,
1258                           guint      length)
1259 {
1260   GRealPtrArray *rarray = (GRealPtrArray *)array;
1261   guint n;
1262
1263   g_return_val_if_fail (rarray != NULL, NULL);
1264   g_return_val_if_fail (index_ < rarray->len, NULL);
1265   g_return_val_if_fail (index_ + length <= rarray->len, NULL);
1266
1267   if (rarray->element_free_func != NULL)
1268     {
1269       for (n = index_; n < index_ + length; n++)
1270         rarray->element_free_func (rarray->pdata[n]);
1271     }
1272
1273   if (index_ + length != rarray->len)
1274     {
1275       memmove (&rarray->pdata[index_],
1276                &rarray->pdata[index_ + length],
1277                (rarray->len - (index_ + length)) * sizeof (gpointer));
1278     }
1279
1280   rarray->len -= length;
1281   if (G_UNLIKELY (g_mem_gc_friendly))
1282     {
1283       guint i;
1284       for (i = 0; i < length; i++)
1285         rarray->pdata[rarray->len + i] = NULL;
1286     }
1287
1288   return array;
1289 }
1290
1291 /**
1292  * g_ptr_array_remove:
1293  * @array: a #GPtrArray
1294  * @data: the pointer to remove
1295  *
1296  * Removes the first occurrence of the given pointer from the pointer
1297  * array. The following elements are moved down one place. If @array
1298  * has a non-%NULL #GDestroyNotify function it is called for the
1299  * removed element.
1300  *
1301  * It returns %TRUE if the pointer was removed, or %FALSE if the
1302  * pointer was not found.
1303  *
1304  * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
1305  *     is not found in the array
1306  */
1307 gboolean
1308 g_ptr_array_remove (GPtrArray *array,
1309                     gpointer   data)
1310 {
1311   guint i;
1312
1313   g_return_val_if_fail (array, FALSE);
1314
1315   for (i = 0; i < array->len; i += 1)
1316     {
1317       if (array->pdata[i] == data)
1318         {
1319           g_ptr_array_remove_index (array, i);
1320           return TRUE;
1321         }
1322     }
1323
1324   return FALSE;
1325 }
1326
1327 /**
1328  * g_ptr_array_remove_fast:
1329  * @array: a #GPtrArray
1330  * @data: the pointer to remove
1331  *
1332  * Removes the first occurrence of the given pointer from the pointer
1333  * array. The last element in the array is used to fill in the space,
1334  * so this function does not preserve the order of the array. But it
1335  * is faster than g_ptr_array_remove(). If @array has a non-%NULL
1336  * #GDestroyNotify function it is called for the removed element.
1337  *
1338  * It returns %TRUE if the pointer was removed, or %FALSE if the
1339  * pointer was not found.
1340  *
1341  * Returns: %TRUE if the pointer was found in the array
1342  */
1343 gboolean
1344 g_ptr_array_remove_fast (GPtrArray *array,
1345                          gpointer   data)
1346 {
1347   GRealPtrArray *rarray = (GRealPtrArray *)array;
1348   guint i;
1349
1350   g_return_val_if_fail (rarray, FALSE);
1351
1352   for (i = 0; i < rarray->len; i += 1)
1353     {
1354       if (rarray->pdata[i] == data)
1355         {
1356           g_ptr_array_remove_index_fast (array, i);
1357           return TRUE;
1358         }
1359     }
1360
1361   return FALSE;
1362 }
1363
1364 /**
1365  * g_ptr_array_add:
1366  * @array: a #GPtrArray
1367  * @data: the pointer to add
1368  *
1369  * Adds a pointer to the end of the pointer array. The array will grow
1370  * in size automatically if necessary.
1371  */
1372 void
1373 g_ptr_array_add (GPtrArray *array,
1374                  gpointer   data)
1375 {
1376   GRealPtrArray *rarray = (GRealPtrArray *)array;
1377
1378   g_return_if_fail (rarray);
1379
1380   g_ptr_array_maybe_expand (rarray, 1);
1381
1382   rarray->pdata[rarray->len++] = data;
1383 }
1384
1385 /**
1386  * g_ptr_array_insert:
1387  * @array: a #GPtrArray
1388  * @index_: the index to place the new element at, or -1 to append
1389  * @data: the pointer to add.
1390  *
1391  * Inserts an element into the pointer array at the given index. The 
1392  * array will grow in size automatically if necessary.
1393  *
1394  * Since: 2.40
1395  */
1396 void
1397 g_ptr_array_insert (GPtrArray *array,
1398                     gint       index_,
1399                     gpointer   data)
1400 {
1401   GRealPtrArray *rarray = (GRealPtrArray *)array;
1402
1403   g_return_if_fail (rarray);
1404   g_return_if_fail (index_ >= -1);
1405   g_return_if_fail (index_ <= (gint)rarray->len);
1406
1407   g_ptr_array_maybe_expand (rarray, 1);
1408
1409   if (index_ < 0)
1410     index_ = rarray->len;
1411
1412   if (index_ < rarray->len)
1413     memmove (&(rarray->pdata[index_ + 1]),
1414              &(rarray->pdata[index_]),
1415              (rarray->len - index_) * sizeof (gpointer));
1416
1417   rarray->len++;
1418   rarray->pdata[index_] = data;
1419 }
1420
1421 /**
1422  * g_ptr_array_sort:
1423  * @array: a #GPtrArray
1424  * @compare_func: comparison function
1425  *
1426  * Sorts the array, using @compare_func which should be a qsort()-style
1427  * comparison function (returns less than zero for first arg is less
1428  * than second arg, zero for equal, greater than zero if irst arg is
1429  * greater than second arg).
1430  *
1431  * Note that the comparison function for g_ptr_array_sort() doesn't
1432  * take the pointers from the array as arguments, it takes pointers to
1433  * the pointers in the array.
1434  *
1435  * This is guaranteed to be a stable sort since version 2.32.
1436  */
1437 void
1438 g_ptr_array_sort (GPtrArray    *array,
1439                   GCompareFunc  compare_func)
1440 {
1441   g_return_if_fail (array != NULL);
1442
1443   /* Don't use qsort as we want a guaranteed stable sort */
1444   g_qsort_with_data (array->pdata,
1445                      array->len,
1446                      sizeof (gpointer),
1447                      (GCompareDataFunc)compare_func,
1448                      NULL);
1449 }
1450
1451 /**
1452  * g_ptr_array_sort_with_data:
1453  * @array: a #GPtrArray
1454  * @compare_func: comparison function
1455  * @user_data: data to pass to @compare_func
1456  *
1457  * Like g_ptr_array_sort(), but the comparison function has an extra
1458  * user data argument.
1459  *
1460  * Note that the comparison function for g_ptr_array_sort_with_data()
1461  * doesn't take the pointers from the array as arguments, it takes
1462  * pointers to the pointers in the array.
1463  *
1464  * This is guaranteed to be a stable sort since version 2.32.
1465  */
1466 void
1467 g_ptr_array_sort_with_data (GPtrArray        *array,
1468                             GCompareDataFunc  compare_func,
1469                             gpointer          user_data)
1470 {
1471   g_return_if_fail (array != NULL);
1472
1473   g_qsort_with_data (array->pdata,
1474                      array->len,
1475                      sizeof (gpointer),
1476                      compare_func,
1477                      user_data);
1478 }
1479
1480 /**
1481  * g_ptr_array_foreach:
1482  * @array: a #GPtrArray
1483  * @func: the function to call for each array element
1484  * @user_data: user data to pass to the function
1485  * 
1486  * Calls a function for each element of a #GPtrArray.
1487  *
1488  * Since: 2.4
1489  */
1490 void
1491 g_ptr_array_foreach (GPtrArray *array,
1492                      GFunc      func,
1493                      gpointer   user_data)
1494 {
1495   guint i;
1496
1497   g_return_if_fail (array);
1498
1499   for (i = 0; i < array->len; i++)
1500     (*func) (array->pdata[i], user_data);
1501 }
1502
1503 /**
1504  * SECTION:arrays_byte
1505  * @title: Byte Arrays
1506  * @short_description: arrays of bytes
1507  *
1508  * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1509  * of bytes which grow automatically as elements are added.
1510  *
1511  * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1512  * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1513  *
1514  * To set the size of a #GByteArray, use g_byte_array_set_size().
1515  *
1516  * To free a #GByteArray, use g_byte_array_free().
1517  *
1518  * An example for using a #GByteArray:
1519  * |[<!-- language="C" -->
1520  *   GByteArray *gbarray;
1521  *   gint i;
1522  *
1523  *   gbarray = g_byte_array_new ();
1524  *   for (i = 0; i < 10000; i++)
1525  *     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1526  *
1527  *   for (i = 0; i < 10000; i++)
1528  *     {
1529  *       g_assert (gbarray->data[4*i] == 'a');
1530  *       g_assert (gbarray->data[4*i+1] == 'b');
1531  *       g_assert (gbarray->data[4*i+2] == 'c');
1532  *       g_assert (gbarray->data[4*i+3] == 'd');
1533  *     }
1534  *
1535  *   g_byte_array_free (gbarray, TRUE);
1536  * ]|
1537  *
1538  * See #GBytes if you are interested in an immutable object representing a
1539  * sequence of bytes.
1540  */
1541
1542 /**
1543  * GByteArray:
1544  * @data: a pointer to the element data. The data may be moved as
1545  *     elements are added to the #GByteArray
1546  * @len: the number of elements in the #GByteArray
1547  *
1548  * Contains the public fields of a GByteArray.
1549  */
1550
1551 /**
1552  * g_byte_array_new:
1553  *
1554  * Creates a new #GByteArray with a reference count of 1.
1555  *
1556  * Returns: (transfer full): the new #GByteArray
1557  */
1558 GByteArray*
1559 g_byte_array_new (void)
1560 {
1561   return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
1562 }
1563
1564 /**
1565  * g_byte_array_new_take:
1566  * @data: (transfer full) (array length=len): byte data for the array
1567  * @len: length of @data
1568  *
1569  * Create byte array containing the data. The data will be owned by the array
1570  * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1571  *
1572  * Since: 2.32
1573  *
1574  * Returns: (transfer full): a new #GByteArray
1575  */
1576 GByteArray*
1577 g_byte_array_new_take (guint8 *data,
1578                        gsize   len)
1579 {
1580   GByteArray *array;
1581   GRealArray *real;
1582
1583   array = g_byte_array_new ();
1584   real = (GRealArray *)array;
1585   g_assert (real->data == NULL);
1586   g_assert (real->len == 0);
1587
1588   real->data = data;
1589   real->len = len;
1590
1591   return array;
1592 }
1593
1594 /**
1595  * g_byte_array_sized_new:
1596  * @reserved_size: number of bytes preallocated
1597  *
1598  * Creates a new #GByteArray with @reserved_size bytes preallocated.
1599  * This avoids frequent reallocation, if you are going to add many
1600  * bytes to the array. Note however that the size of the array is still
1601  * 0.
1602  *
1603  * Returns: the new #GByteArray
1604  */
1605 GByteArray*
1606 g_byte_array_sized_new (guint reserved_size)
1607 {
1608   return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1609 }
1610
1611 /**
1612  * g_byte_array_free:
1613  * @array: a #GByteArray
1614  * @free_segment: if %TRUE the actual byte data is freed as well
1615  *
1616  * Frees the memory allocated by the #GByteArray. If @free_segment is
1617  * %TRUE it frees the actual byte data. If the reference count of
1618  * @array is greater than one, the #GByteArray wrapper is preserved but
1619  * the size of @array will be set to zero.
1620  *
1621  * Returns: the element data if @free_segment is %FALSE, otherwise
1622  *          %NULL.  The element data should be freed using g_free().
1623  */
1624 guint8*
1625 g_byte_array_free (GByteArray *array,
1626                    gboolean    free_segment)
1627 {
1628   return (guint8 *)g_array_free ((GArray *)array, free_segment);
1629 }
1630
1631 /**
1632  * g_byte_array_free_to_bytes:
1633  * @array: (transfer full): a #GByteArray
1634  *
1635  * Transfers the data from the #GByteArray into a new immutable #GBytes.
1636  *
1637  * The #GByteArray is freed unless the reference count of @array is greater
1638  * than one, the #GByteArray wrapper is preserved but the size of @array
1639  * will be set to zero.
1640  *
1641  * This is identical to using g_bytes_new_take() and g_byte_array_free()
1642  * together.
1643  *
1644  * Since: 2.32
1645  *
1646  * Returns: (transfer full): a new immutable #GBytes representing same
1647  *     byte data that was in the array
1648  */
1649 GBytes*
1650 g_byte_array_free_to_bytes (GByteArray *array)
1651 {
1652   gsize length;
1653
1654   g_return_val_if_fail (array != NULL, NULL);
1655
1656   length = array->len;
1657   return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1658 }
1659
1660 /**
1661  * g_byte_array_ref:
1662  * @array: A #GByteArray
1663  *
1664  * Atomically increments the reference count of @array by one.
1665  * This function is thread-safe and may be called from any thread.
1666  *
1667  * Returns: The passed in #GByteArray
1668  *
1669  * Since: 2.22
1670  */
1671 GByteArray*
1672 g_byte_array_ref (GByteArray *array)
1673 {
1674   return (GByteArray *)g_array_ref ((GArray *)array);
1675 }
1676
1677 /**
1678  * g_byte_array_unref:
1679  * @array: A #GByteArray
1680  *
1681  * Atomically decrements the reference count of @array by one. If the
1682  * reference count drops to 0, all memory allocated by the array is
1683  * released. This function is thread-safe and may be called from any
1684  * thread.
1685  *
1686  * Since: 2.22
1687  */
1688 void
1689 g_byte_array_unref (GByteArray *array)
1690 {
1691   g_array_unref ((GArray *)array);
1692 }
1693
1694 /**
1695  * g_byte_array_append:
1696  * @array: a #GByteArray
1697  * @data: the byte data to be added
1698  * @len: the number of bytes to add
1699  *
1700  * Adds the given bytes to the end of the #GByteArray.
1701  * The array will grow in size automatically if necessary.
1702  *
1703  * Returns: the #GByteArray
1704  */
1705 GByteArray*
1706 g_byte_array_append (GByteArray   *array,
1707                      const guint8 *data,
1708                      guint         len)
1709 {
1710   g_array_append_vals ((GArray *)array, (guint8 *)data, len);
1711
1712   return array;
1713 }
1714
1715 /**
1716  * g_byte_array_prepend:
1717  * @array: a #GByteArray
1718  * @data: the byte data to be added
1719  * @len: the number of bytes to add
1720  *
1721  * Adds the given data to the start of the #GByteArray.
1722  * The array will grow in size automatically if necessary.
1723  *
1724  * Returns: the #GByteArray
1725  */
1726 GByteArray*
1727 g_byte_array_prepend (GByteArray   *array,
1728                       const guint8 *data,
1729                       guint         len)
1730 {
1731   g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
1732
1733   return array;
1734 }
1735
1736 /**
1737  * g_byte_array_set_size:
1738  * @array: a #GByteArray
1739  * @length: the new size of the #GByteArray
1740  *
1741  * Sets the size of the #GByteArray, expanding it if necessary.
1742  *
1743  * Returns: the #GByteArray
1744  */
1745 GByteArray*
1746 g_byte_array_set_size (GByteArray *array,
1747                        guint       length)
1748 {
1749   g_array_set_size ((GArray *)array, length);
1750
1751   return array;
1752 }
1753
1754 /**
1755  * g_byte_array_remove_index:
1756  * @array: a #GByteArray
1757  * @index_: the index of the byte to remove
1758  *
1759  * Removes the byte at the given index from a #GByteArray.
1760  * The following bytes are moved down one place.
1761  *
1762  * Returns: the #GByteArray
1763  **/
1764 GByteArray*
1765 g_byte_array_remove_index (GByteArray *array,
1766                            guint       index_)
1767 {
1768   g_array_remove_index ((GArray *)array, index_);
1769
1770   return array;
1771 }
1772
1773 /**
1774  * g_byte_array_remove_index_fast:
1775  * @array: a #GByteArray
1776  * @index_: the index of the byte to remove
1777  *
1778  * Removes the byte at the given index from a #GByteArray. The last
1779  * element in the array is used to fill in the space, so this function
1780  * does not preserve the order of the #GByteArray. But it is faster
1781  * than g_byte_array_remove_index().
1782  *
1783  * Returns: the #GByteArray
1784  */
1785 GByteArray*
1786 g_byte_array_remove_index_fast (GByteArray *array,
1787                                 guint       index_)
1788 {
1789   g_array_remove_index_fast ((GArray *)array, index_);
1790
1791   return array;
1792 }
1793
1794 /**
1795  * g_byte_array_remove_range:
1796  * @array: a @GByteArray
1797  * @index_: the index of the first byte to remove
1798  * @length: the number of bytes to remove
1799  *
1800  * Removes the given number of bytes starting at the given index from a
1801  * #GByteArray.  The following elements are moved to close the gap.
1802  *
1803  * Returns: the #GByteArray
1804  *
1805  * Since: 2.4
1806  */
1807 GByteArray*
1808 g_byte_array_remove_range (GByteArray *array,
1809                            guint       index_,
1810                            guint       length)
1811 {
1812   g_return_val_if_fail (array, NULL);
1813   g_return_val_if_fail (index_ < array->len, NULL);
1814   g_return_val_if_fail (index_ + length <= array->len, NULL);
1815
1816   return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
1817 }
1818
1819 /**
1820  * g_byte_array_sort:
1821  * @array: a #GByteArray
1822  * @compare_func: comparison function
1823  *
1824  * Sorts a byte array, using @compare_func which should be a
1825  * qsort()-style comparison function (returns less than zero for first
1826  * arg is less than second arg, zero for equal, greater than zero if
1827  * first arg is greater than second arg).
1828  *
1829  * If two array elements compare equal, their order in the sorted array
1830  * is undefined. If you want equal elements to keep their order (i.e.
1831  * you want a stable sort) you can write a comparison function that,
1832  * if two elements would otherwise compare equal, compares them by
1833  * their addresses.
1834  */
1835 void
1836 g_byte_array_sort (GByteArray   *array,
1837                    GCompareFunc  compare_func)
1838 {
1839   g_array_sort ((GArray *)array, compare_func);
1840 }
1841
1842 /**
1843  * g_byte_array_sort_with_data:
1844  * @array: a #GByteArray
1845  * @compare_func: comparison function
1846  * @user_data: data to pass to @compare_func
1847  *
1848  * Like g_byte_array_sort(), but the comparison function takes an extra
1849  * user data argument.
1850  */
1851 void
1852 g_byte_array_sort_with_data (GByteArray       *array,
1853                              GCompareDataFunc  compare_func,
1854                              gpointer          user_data)
1855 {
1856   g_array_sort_with_data ((GArray *)array, compare_func, user_data);
1857 }