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