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