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