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