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