27bc7e445e287b4b056c422cebd751598ccbce27
[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 "gslice.h"
39 #include "gmem.h"
40 #include "gthread.h"
41 #include "gmessages.h"
42 #include "gqsort.h"
43
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 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   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
222   g_atomic_int_inc (&rarray->ref_count);
223
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
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. If you want equal elements to keep their order &#8211; i.e.
621  * you want a stable sort &#8211; you can write a comparison function that,
622  * if two elements would otherwise compare equal, compares them by
623  * their addresses.
624  **/
625 void
626 g_array_sort (GArray       *farray,
627               GCompareFunc  compare_func)
628 {
629   GRealArray *array = (GRealArray*) farray;
630
631   g_return_if_fail (array != NULL);
632
633   qsort (array->data,
634          array->len,
635          array->elt_size,
636          compare_func);
637 }
638
639 /**
640  * g_array_sort_with_data:
641  * @array: a #GArray.
642  * @compare_func: comparison function.
643  * @user_data: data to pass to @compare_func.
644  *
645  * Like g_array_sort(), but the comparison function receives an extra
646  * user data argument.
647  **/
648 void
649 g_array_sort_with_data (GArray           *farray,
650                         GCompareDataFunc  compare_func,
651                         gpointer          user_data)
652 {
653   GRealArray *array = (GRealArray*) farray;
654
655   g_return_if_fail (array != NULL);
656
657   g_qsort_with_data (array->data,
658                      array->len,
659                      array->elt_size,
660                      compare_func,
661                      user_data);
662 }
663
664 /* Returns the smallest power of 2 greater than n, or n if
665  * such power does not fit in a guint
666  */
667 static guint
668 g_nearest_pow (gint num)
669 {
670   guint n = 1;
671
672   while (n < num && n > 0)
673     n <<= 1;
674
675   return n ? n : num;
676 }
677
678 static void
679 g_array_maybe_expand (GRealArray *array,
680                       gint        len)
681 {
682   guint want_alloc = g_array_elt_len (array, array->len + len + 
683                                       array->zero_terminated);
684
685   if (want_alloc > array->alloc)
686     {
687       want_alloc = g_nearest_pow (want_alloc);
688       want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
689
690       array->data = g_realloc (array->data, want_alloc);
691
692       if (G_UNLIKELY (g_mem_gc_friendly))
693         memset (array->data + array->alloc, 0, want_alloc - array->alloc);
694
695       array->alloc = want_alloc;
696     }
697 }
698
699 /**
700  * SECTION:arrays_pointer
701  * @title: Pointer Arrays
702  * @short_description: arrays of pointers to any type of data, which
703  *                     grow automatically as new elements are added
704  *
705  * Pointer Arrays are similar to Arrays but are used only for storing
706  * pointers.
707  *
708  * <note><para>If you remove elements from the array, elements at the
709  * end of the array are moved into the space previously occupied by the
710  * removed element. This means that you should not rely on the index of
711  * particular elements remaining the same. You should also be careful
712  * when deleting elements while iterating over the array.</para></note>
713  *
714  * To create a pointer array, use g_ptr_array_new().
715  *
716  * To add elements to a pointer array, use g_ptr_array_add().
717  *
718  * To remove elements from a pointer array, use g_ptr_array_remove(),
719  * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
720  *
721  * To access an element of a pointer array, use g_ptr_array_index().
722  *
723  * To set the size of a pointer array, use g_ptr_array_set_size().
724  *
725  * To free a pointer array, use g_ptr_array_free().
726  *
727  * <example>
728  *  <title>Using a #GPtrArray</title>
729  *  <programlisting>
730  *   GPtrArray *gparray;
731  *   gchar *string1 = "one", *string2 = "two", *string3 = "three";
732  *
733  *   gparray = g_ptr_array_new (<!-- -->);
734  *   g_ptr_array_add (gparray, (gpointer) string1);
735  *   g_ptr_array_add (gparray, (gpointer) string2);
736  *   g_ptr_array_add (gparray, (gpointer) string3);
737  *
738  *   if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
739  *     g_print ("ERROR: got &percnt;p instead of &percnt;p\n",
740  *              g_ptr_array_index (gparray, 0), string1);
741  *
742  *   g_ptr_array_free (gparray, TRUE);
743  *  </programlisting>
744  * </example>
745  **/
746
747 typedef struct _GRealPtrArray  GRealPtrArray;
748
749 /**
750  * GPtrArray:
751  * @pdata: points to the array of pointers, which may be moved when the
752  *         array grows.
753  * @len: number of pointers in the array.
754  *
755  * Contains the public fields of a pointer array.
756  **/
757 struct _GRealPtrArray
758 {
759   gpointer     *pdata;
760   guint         len;
761   guint         alloc;
762   gint          ref_count;
763   GDestroyNotify element_free_func;
764 };
765
766 /**
767  * g_ptr_array_index:
768  * @array: a #GPtrArray.
769  * @index_: the index of the pointer to return.
770  * @Returns: the pointer at the given index.
771  *
772  * Returns the pointer at the given index of the pointer array.
773  **/
774
775 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
776                                       gint           len);
777
778 /**
779  * g_ptr_array_new:
780  * @Returns: the new #GPtrArray.
781  *
782  * Creates a new #GPtrArray with a reference count of 1.
783  **/
784 GPtrArray*
785 g_ptr_array_new (void)
786 {
787   return g_ptr_array_sized_new (0);
788 }
789
790 /**
791  * g_ptr_array_sized_new:
792  * @reserved_size: number of pointers preallocated.
793  * @Returns: the new #GPtrArray.
794  *
795  * Creates a new #GPtrArray with @reserved_size pointers preallocated
796  * and a reference count of 1. This avoids frequent reallocation, if
797  * you are going to add many pointers to the array. Note however that
798  * the size of the array is still 0.
799  **/
800 GPtrArray*  
801 g_ptr_array_sized_new (guint reserved_size)
802 {
803   GRealPtrArray *array = g_slice_new (GRealPtrArray);
804
805   array->pdata = NULL;
806   array->len = 0;
807   array->alloc = 0;
808   array->ref_count = 1;
809   array->element_free_func = NULL;
810
811   if (reserved_size != 0)
812     g_ptr_array_maybe_expand (array, reserved_size);
813
814   return (GPtrArray*) array;  
815 }
816
817 /**
818  * g_ptr_array_new_with_free_func:
819  * @element_free_func: A function to free elements with destroy @array or %NULL.
820  *
821  * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
822  * for freeing each element when the array is destroyed either via
823  * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
824  * set to %TRUE or when removing elements.
825  *
826  * Returns: A new #GPtrArray.
827  *
828  * Since: 2.22
829  **/
830 GPtrArray *
831 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
832 {
833   GPtrArray *array;
834
835   array = g_ptr_array_new ();
836   g_ptr_array_set_free_func (array, element_free_func);
837   return array;
838 }
839
840 /**
841  * g_ptr_array_new_full:
842  * @reserved_size: number of pointers preallocated.
843  * @element_free_func: A function to free elements with destroy @array or %NULL.
844  *
845  * Creates a new #GPtrArray with @reserved_size pointers preallocated
846  * and a reference count of 1. This avoids frequent reallocation, if
847  * you are going to add many pointers to the array. Note however that
848  * the size of the array is still 0. It also set @element_free_func
849  * for freeing each element when the array is destroyed either via
850  * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
851  * set to %TRUE or when removing elements.
852  *
853  * Returns: A new #GPtrArray.
854  *
855  * Since: 2.30
856  **/
857 GPtrArray *
858 g_ptr_array_new_full (guint          reserved_size,
859                       GDestroyNotify element_free_func)
860 {
861   GPtrArray *array;
862
863   array = g_ptr_array_sized_new (reserved_size);
864   g_ptr_array_set_free_func (array, element_free_func);
865   return array;
866 }
867
868 /**
869  * g_ptr_array_set_free_func:
870  * @array: A #GPtrArray.
871  * @element_free_func: A function to free elements with destroy @array or %NULL.
872  *
873  * Sets a function for freeing each element when @array is destroyed
874  * either via g_ptr_array_unref(), when g_ptr_array_free() is called
875  * with @free_segment set to %TRUE or when removing elements.
876  *
877  * Since: 2.22
878  **/
879 void
880 g_ptr_array_set_free_func (GPtrArray        *array,
881                            GDestroyNotify    element_free_func)
882 {
883   GRealPtrArray* rarray = (GRealPtrArray*) array;
884
885   g_return_if_fail (array);
886
887   rarray->element_free_func = element_free_func;
888 }
889
890 /**
891  * g_ptr_array_ref:
892  * @array: A #GArray.
893  *
894  * Atomically increments the reference count of @array by one. This
895  * function is MT-safe and may be called from any thread.
896  *
897  * Returns: The passed in #GPtrArray.
898  *
899  * Since: 2.22
900  **/
901 GPtrArray *
902 g_ptr_array_ref (GPtrArray *array)
903 {
904   GRealPtrArray *rarray = (GRealPtrArray*) array;
905
906   g_return_val_if_fail (array, NULL);
907
908   g_atomic_int_inc (&rarray->ref_count);
909
910   return array;
911 }
912
913 /**
914  * g_ptr_array_unref:
915  * @array: A #GPtrArray.
916  *
917  * Atomically decrements the reference count of @array by one. If the
918  * reference count drops to 0, the effect is the same as calling
919  * g_ptr_array_free() with @free_segment set to %TRUE. This function
920  * is MT-safe and may be called from any thread.
921  *
922  * Since: 2.22
923  **/
924 void
925 g_ptr_array_unref (GPtrArray *array)
926 {
927   GRealPtrArray *rarray = (GRealPtrArray*) array;
928   g_return_if_fail (array);
929
930   if (g_atomic_int_dec_and_test (&rarray->ref_count))
931     g_ptr_array_free (array, TRUE);
932 }
933
934 /**
935  * g_ptr_array_free:
936  * @array: a #GPtrArray.
937  * @free_seg: if %TRUE the actual pointer array is freed as well.
938  * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
939  *           The pointer array should be freed using g_free().
940  *
941  * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
942  * it frees the memory block holding the elements as well. Pass %FALSE
943  * if you want to free the #GPtrArray wrapper but preserve the
944  * underlying array for use elsewhere. If the reference count of @array
945  * is greater than one, the #GPtrArray wrapper is preserved but the
946  * size of @array will be set to zero.
947  *
948  * <note><para>If array contents point to dynamically-allocated
949  * memory, they should be freed separately if @free_seg is %TRUE and no
950  * #GDestroyNotify function has been set for @array.</para></note>
951  **/
952 gpointer*
953 g_ptr_array_free (GPtrArray *farray,
954                   gboolean   free_segment)
955 {
956   GRealPtrArray *array = (GRealPtrArray*) farray;
957   gpointer* segment;
958   gboolean preserve_wrapper;
959
960   g_return_val_if_fail (array, NULL);
961
962   /* if others are holding a reference, preserve the wrapper but do free/return the data */
963   preserve_wrapper = FALSE;
964   if (g_atomic_int_get (&array->ref_count) > 1)
965     preserve_wrapper = TRUE;
966
967   if (free_segment)
968     {
969       if (array->element_free_func != NULL)
970         g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
971       g_free (array->pdata);
972       segment = NULL;
973     }
974   else
975     segment = array->pdata;
976
977   if (preserve_wrapper)
978     {
979       array->pdata = NULL;
980       array->len = 0;
981       array->alloc = 0;
982     }
983   else
984     {
985       g_slice_free1 (sizeof (GRealPtrArray), array);
986     }
987
988   return segment;
989 }
990
991 static void
992 g_ptr_array_maybe_expand (GRealPtrArray *array,
993                           gint           len)
994 {
995   if ((array->len + len) > array->alloc)
996     {
997       guint old_alloc = array->alloc;
998       array->alloc = g_nearest_pow (array->len + len);
999       array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1000       array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1001       if (G_UNLIKELY (g_mem_gc_friendly))
1002         for ( ; old_alloc < array->alloc; old_alloc++)
1003           array->pdata [old_alloc] = NULL;
1004     }
1005 }
1006
1007 /**
1008  * g_ptr_array_set_size:
1009  * @array: a #GPtrArray.
1010  * @length: the new length of the pointer array.
1011  *
1012  * Sets the size of the array. When making the array larger,
1013  * newly-added elements will be set to %NULL. When making it smaller,
1014  * if @array has a non-%NULL #GDestroyNotify function then it will be
1015  * called for the removed elements.
1016  **/
1017 void
1018 g_ptr_array_set_size  (GPtrArray *farray,
1019                        gint       length)
1020 {
1021   GRealPtrArray* array = (GRealPtrArray*) farray;
1022
1023   g_return_if_fail (array);
1024
1025   if (length > array->len)
1026     {
1027       int i;
1028       g_ptr_array_maybe_expand (array, (length - array->len));
1029       /* This is not 
1030        *     memset (array->pdata + array->len, 0,
1031        *            sizeof (gpointer) * (length - array->len));
1032        * to make it really portable. Remember (void*)NULL needn't be
1033        * bitwise zero. It of course is silly not to use memset (..,0,..).
1034        */
1035       for (i = array->len; i < length; i++)
1036         array->pdata[i] = NULL;
1037     }
1038   else if (length < array->len)
1039     g_ptr_array_remove_range (farray, length, array->len - length);
1040
1041   array->len = length;
1042 }
1043
1044 /**
1045  * g_ptr_array_remove_index:
1046  * @array: a #GPtrArray.
1047  * @index_: the index of the pointer to remove.
1048  * @Returns: the pointer which was removed.
1049  *
1050  * Removes the pointer at the given index from the pointer array. The
1051  * following elements are moved down one place. If @array has a
1052  * non-%NULL #GDestroyNotify function it is called for the removed
1053  * element.
1054  **/
1055 gpointer
1056 g_ptr_array_remove_index (GPtrArray *farray,
1057                           guint      index_)
1058 {
1059   GRealPtrArray* array = (GRealPtrArray*) farray;
1060   gpointer result;
1061
1062   g_return_val_if_fail (array, NULL);
1063
1064   g_return_val_if_fail (index_ < array->len, NULL);
1065
1066   result = array->pdata[index_];
1067   
1068   if (array->element_free_func != NULL)
1069     array->element_free_func (array->pdata[index_]);
1070
1071   if (index_ != array->len - 1)
1072     g_memmove (array->pdata + index_, array->pdata + index_ + 1, 
1073                sizeof (gpointer) * (array->len - index_ - 1));
1074   
1075   array->len -= 1;
1076
1077   if (G_UNLIKELY (g_mem_gc_friendly))
1078     array->pdata[array->len] = NULL;
1079
1080   return result;
1081 }
1082
1083 /**
1084  * g_ptr_array_remove_index_fast:
1085  * @array: a #GPtrArray.
1086  * @index_: the index of the pointer to remove.
1087  * @Returns: the pointer which was removed.
1088  *
1089  * Removes the pointer at the given index from the pointer array. The
1090  * last element in the array is used to fill in the space, so this
1091  * function does not preserve the order of the array. But it is faster
1092  * than g_ptr_array_remove_index(). If @array has a non-%NULL
1093  * #GDestroyNotify function it is called for the removed element.
1094  **/
1095 gpointer
1096 g_ptr_array_remove_index_fast (GPtrArray *farray,
1097                                guint      index_)
1098 {
1099   GRealPtrArray* array = (GRealPtrArray*) farray;
1100   gpointer result;
1101
1102   g_return_val_if_fail (array, NULL);
1103
1104   g_return_val_if_fail (index_ < array->len, NULL);
1105
1106   result = array->pdata[index_];
1107
1108   if (array->element_free_func != NULL)
1109     array->element_free_func (array->pdata[index_]);
1110
1111   if (index_ != array->len - 1)
1112     array->pdata[index_] = array->pdata[array->len - 1];
1113
1114   array->len -= 1;
1115
1116   if (G_UNLIKELY (g_mem_gc_friendly))
1117     array->pdata[array->len] = NULL;
1118
1119   return result;
1120 }
1121
1122 /**
1123  * g_ptr_array_remove_range:
1124  * @array: a @GPtrArray.
1125  * @index_: the index of the first pointer to remove.
1126  * @length: the number of pointers to remove.
1127  *
1128  * Removes the given number of pointers starting at the given index
1129  * from a #GPtrArray.  The following elements are moved to close the
1130  * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1131  * for the removed elements.
1132  *
1133  * Since: 2.4
1134  **/
1135 void
1136 g_ptr_array_remove_range (GPtrArray *farray,
1137                           guint      index_,
1138                           guint      length)
1139 {
1140   GRealPtrArray* array = (GRealPtrArray*) farray;
1141   guint n;
1142
1143   g_return_if_fail (array);
1144   g_return_if_fail (index_ < array->len);
1145   g_return_if_fail (index_ + length <= array->len);
1146
1147   if (array->element_free_func != NULL)
1148     {
1149       for (n = index_; n < index_ + length; n++)
1150         array->element_free_func (array->pdata[n]);
1151     }
1152
1153   if (index_ + length != array->len)
1154     {
1155       g_memmove (&array->pdata[index_],
1156                  &array->pdata[index_ + length], 
1157                  (array->len - (index_ + length)) * sizeof (gpointer));
1158     }
1159
1160   array->len -= length;
1161   if (G_UNLIKELY (g_mem_gc_friendly))
1162     {
1163       guint i;
1164       for (i = 0; i < length; i++)
1165         array->pdata[array->len + i] = NULL;
1166     }
1167 }
1168
1169 /**
1170  * g_ptr_array_remove:
1171  * @array: a #GPtrArray.
1172  * @data: the pointer to remove.
1173  * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1174  *           not found in the array.
1175  *
1176  * Removes the first occurrence of the given pointer from the pointer
1177  * array. The following elements are moved down one place. If @array
1178  * has a non-%NULL #GDestroyNotify function it is called for the
1179  * removed element.
1180  *
1181  * It returns %TRUE if the pointer was removed, or %FALSE if the
1182  * pointer was not found.
1183  **/
1184 gboolean
1185 g_ptr_array_remove (GPtrArray *farray,
1186                     gpointer   data)
1187 {
1188   GRealPtrArray* array = (GRealPtrArray*) farray;
1189   guint i;
1190
1191   g_return_val_if_fail (array, FALSE);
1192
1193   for (i = 0; i < array->len; i += 1)
1194     {
1195       if (array->pdata[i] == data)
1196         {
1197           g_ptr_array_remove_index (farray, i);
1198           return TRUE;
1199         }
1200     }
1201
1202   return FALSE;
1203 }
1204
1205 /**
1206  * g_ptr_array_remove_fast:
1207  * @array: a #GPtrArray.
1208  * @data: the pointer to remove.
1209  * @Returns: %TRUE if the pointer was found in the array.
1210  *
1211  * Removes the first occurrence of the given pointer from the pointer
1212  * array. The last element in the array is used to fill in the space,
1213  * so this function does not preserve the order of the array. But it is
1214  * faster than g_ptr_array_remove(). If @array has a non-%NULL
1215  * #GDestroyNotify function it is called for the removed element.
1216  *
1217  * It returns %TRUE if the pointer was removed, or %FALSE if the
1218  * pointer was not found.
1219  **/
1220 gboolean
1221 g_ptr_array_remove_fast (GPtrArray *farray,
1222                          gpointer   data)
1223 {
1224   GRealPtrArray* array = (GRealPtrArray*) farray;
1225   guint i;
1226
1227   g_return_val_if_fail (array, FALSE);
1228
1229   for (i = 0; i < array->len; i += 1)
1230     {
1231       if (array->pdata[i] == data)
1232         {
1233           g_ptr_array_remove_index_fast (farray, i);
1234           return TRUE;
1235         }
1236     }
1237
1238   return FALSE;
1239 }
1240
1241 /**
1242  * g_ptr_array_add:
1243  * @array: a #GPtrArray.
1244  * @data: the pointer to add.
1245  *
1246  * Adds a pointer to the end of the pointer array. The array will grow
1247  * in size automatically if necessary.
1248  **/
1249 void
1250 g_ptr_array_add (GPtrArray *farray,
1251                  gpointer   data)
1252 {
1253   GRealPtrArray* array = (GRealPtrArray*) farray;
1254
1255   g_return_if_fail (array);
1256
1257   g_ptr_array_maybe_expand (array, 1);
1258
1259   array->pdata[array->len++] = data;
1260 }
1261
1262 /**
1263  * g_ptr_array_sort:
1264  * @array: a #GPtrArray.
1265  * @compare_func: comparison function.
1266  *
1267  * Sorts the array, using @compare_func which should be a qsort()-style
1268  * comparison function (returns less than zero for first arg is less
1269  * than second arg, zero for equal, greater than zero if irst arg is
1270  * greater than second arg).
1271  *
1272  * If two array elements compare equal, their order in the sorted array
1273  * is undefined. If you want equal elements to keep their order &#8211; i.e.
1274  * you want a stable sort &#8211; you can write a comparison function that,
1275  * if two elements would otherwise compare equal, compares them by
1276  * their addresses.
1277  *
1278  * <note><para>The comparison function for g_ptr_array_sort() doesn't
1279  * take the pointers from the array as arguments, it takes pointers to
1280  * the pointers in the array.</para></note>
1281  **/
1282 void
1283 g_ptr_array_sort (GPtrArray    *array,
1284                   GCompareFunc  compare_func)
1285 {
1286   g_return_if_fail (array != NULL);
1287
1288   qsort (array->pdata,
1289          array->len,
1290          sizeof (gpointer),
1291          compare_func);
1292 }
1293
1294 /**
1295  * g_ptr_array_sort_with_data:
1296  * @array: a #GPtrArray.
1297  * @compare_func: comparison function.
1298  * @user_data: data to pass to @compare_func.
1299  *
1300  * Like g_ptr_array_sort(), but the comparison function has an extra
1301  * user data argument.
1302  *
1303  * <note><para>The comparison function for g_ptr_array_sort_with_data()
1304  * doesn't take the pointers from the array as arguments, it takes
1305  * pointers to the pointers in the array.</para></note>
1306  **/
1307 void
1308 g_ptr_array_sort_with_data (GPtrArray        *array,
1309                             GCompareDataFunc  compare_func,
1310                             gpointer          user_data)
1311 {
1312   g_return_if_fail (array != NULL);
1313
1314   g_qsort_with_data (array->pdata,
1315                      array->len,
1316                      sizeof (gpointer),
1317                      compare_func,
1318                      user_data);
1319 }
1320
1321 /**
1322  * g_ptr_array_foreach:
1323  * @array: a #GPtrArray
1324  * @func: the function to call for each array element
1325  * @user_data: user data to pass to the function
1326  * 
1327  * Calls a function for each element of a #GPtrArray.
1328  *
1329  * Since: 2.4
1330  **/
1331 void
1332 g_ptr_array_foreach (GPtrArray *array,
1333                      GFunc      func,
1334                      gpointer   user_data)
1335 {
1336   guint i;
1337
1338   g_return_if_fail (array);
1339
1340   for (i = 0; i < array->len; i++)
1341     (*func) (array->pdata[i], user_data);
1342 }
1343
1344 /**
1345  * SECTION:arrays_byte
1346  * @title: Byte Arrays
1347  * @short_description: arrays of bytes, which grow automatically as
1348  *                     elements are added
1349  *
1350  * #GByteArray is based on #GArray, to provide arrays of bytes which
1351  * grow automatically as elements are added.
1352  *
1353  * To create a new #GByteArray use g_byte_array_new().
1354  *
1355  * To add elements to a #GByteArray, use g_byte_array_append(), and
1356  * g_byte_array_prepend().
1357  *
1358  * To set the size of a #GByteArray, use g_byte_array_set_size().
1359  *
1360  * To free a #GByteArray, use g_byte_array_free().
1361  *
1362  * <example>
1363  *  <title>Using a #GByteArray</title>
1364  *  <programlisting>
1365  *   GByteArray *gbarray;
1366  *   gint i;
1367  *
1368  *   gbarray = g_byte_array_new (<!-- -->);
1369  *   for (i = 0; i &lt; 10000; i++)
1370  *     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1371  *
1372  *   for (i = 0; i &lt; 10000; i++)
1373  *     {
1374  *       g_assert (gbarray->data[4*i] == 'a');
1375  *       g_assert (gbarray->data[4*i+1] == 'b');
1376  *       g_assert (gbarray->data[4*i+2] == 'c');
1377  *       g_assert (gbarray->data[4*i+3] == 'd');
1378  *     }
1379  *
1380  *   g_byte_array_free (gbarray, TRUE);
1381  *  </programlisting>
1382  * </example>
1383  **/
1384
1385 /**
1386  * GByteArray:
1387  * @data: a pointer to the element data. The data may be moved as
1388  *        elements are added to the #GByteArray.
1389  * @len: the number of elements in the #GByteArray.
1390  *
1391  * The <structname>GByteArray</structname> struct allows access to the
1392  * public fields of a <structname>GByteArray</structname>.
1393  **/
1394
1395 /**
1396  * g_byte_array_new:
1397  * @Returns: the new #GByteArray.
1398  *
1399  * Creates a new #GByteArray with a reference count of 1.
1400  **/
1401 GByteArray* g_byte_array_new (void)
1402 {
1403   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1404 }
1405
1406 /**
1407  * g_byte_array_sized_new:
1408  * @reserved_size: number of bytes preallocated.
1409  * @Returns: the new #GByteArray.
1410  *
1411  * Creates a new #GByteArray with @reserved_size bytes preallocated.
1412  * This avoids frequent reallocation, if you are going to add many
1413  * bytes to the array. Note however that the size of the array is still
1414  * 0.
1415  **/
1416 GByteArray* g_byte_array_sized_new (guint reserved_size)
1417 {
1418   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1419 }
1420
1421 /**
1422  * g_byte_array_free:
1423  * @array: a #GByteArray.
1424  * @free_segment: if %TRUE the actual byte data is freed as well.
1425  * @Returns: the element data if @free_segment is %FALSE, otherwise
1426  *           %NULL.  The element data should be freed using g_free().
1427  *
1428  * Frees the memory allocated by the #GByteArray. If @free_segment is
1429  * %TRUE it frees the actual byte data. If the reference count of
1430  * @array is greater than one, the #GByteArray wrapper is preserved but
1431  * the size of @array will be set to zero.
1432  **/
1433 guint8*     g_byte_array_free     (GByteArray *array,
1434                                    gboolean    free_segment)
1435 {
1436   return (guint8*) g_array_free ((GArray*) array, free_segment);
1437 }
1438
1439 /**
1440  * g_byte_array_ref:
1441  * @array: A #GByteArray.
1442  *
1443  * Atomically increments the reference count of @array by one. This
1444  * function is MT-safe and may be called from any thread.
1445  *
1446  * Returns: The passed in #GByteArray.
1447  *
1448  * Since: 2.22
1449  **/
1450 GByteArray *
1451 g_byte_array_ref (GByteArray *array)
1452 {
1453   return (GByteArray *) g_array_ref ((GArray *) array);
1454 }
1455
1456 /**
1457  * g_byte_array_unref:
1458  * @array: A #GByteArray.
1459  *
1460  * Atomically decrements the reference count of @array by one. If the
1461  * reference count drops to 0, all memory allocated by the array is
1462  * released. This function is MT-safe and may be called from any
1463  * thread.
1464  *
1465  * Since: 2.22
1466  **/
1467 void
1468 g_byte_array_unref (GByteArray *array)
1469 {
1470   g_array_unref ((GArray *) array);
1471 }
1472
1473 /**
1474  * g_byte_array_append:
1475  * @array: a #GByteArray.
1476  * @data: the byte data to be added.
1477  * @len: the number of bytes to add.
1478  * @Returns: the #GByteArray.
1479  *
1480  * Adds the given bytes to the end of the #GByteArray. The array will
1481  * grow in size automatically if necessary.
1482  **/
1483 GByteArray* g_byte_array_append   (GByteArray   *array,
1484                                    const guint8 *data,
1485                                    guint         len)
1486 {
1487   g_array_append_vals ((GArray*) array, (guint8*)data, len);
1488
1489   return array;
1490 }
1491
1492 /**
1493  * g_byte_array_prepend:
1494  * @array: a #GByteArray.
1495  * @data: the byte data to be added.
1496  * @len: the number of bytes to add.
1497  * @Returns: the #GByteArray.
1498  *
1499  * Adds the given data to the start of the #GByteArray. The array will
1500  * grow in size automatically if necessary.
1501  **/
1502 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1503                                    const guint8 *data,
1504                                    guint         len)
1505 {
1506   g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1507
1508   return array;
1509 }
1510
1511 /**
1512  * g_byte_array_set_size:
1513  * @array: a #GByteArray.
1514  * @length: the new size of the #GByteArray.
1515  * @Returns: the #GByteArray.
1516  *
1517  * Sets the size of the #GByteArray, expanding it if necessary.
1518  **/
1519 GByteArray* g_byte_array_set_size (GByteArray *array,
1520                                    guint       length)
1521 {
1522   g_array_set_size ((GArray*) array, length);
1523
1524   return array;
1525 }
1526
1527 /**
1528  * g_byte_array_remove_index:
1529  * @array: a #GByteArray.
1530  * @index_: the index of the byte to remove.
1531  * @Returns: the #GByteArray.
1532  *
1533  * Removes the byte at the given index from a #GByteArray. The
1534  * following bytes are moved down one place.
1535  **/
1536 GByteArray* g_byte_array_remove_index (GByteArray *array,
1537                                        guint       index_)
1538 {
1539   g_array_remove_index ((GArray*) array, index_);
1540
1541   return array;
1542 }
1543
1544 /**
1545  * g_byte_array_remove_index_fast:
1546  * @array: a #GByteArray.
1547  * @index_: the index of the byte to remove.
1548  * @Returns: the #GByteArray.
1549  *
1550  * Removes the byte at the given index from a #GByteArray. The last
1551  * element in the array is used to fill in the space, so this function
1552  * does not preserve the order of the #GByteArray. But it is faster
1553  * than g_byte_array_remove_index().
1554  **/
1555 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1556                                             guint       index_)
1557 {
1558   g_array_remove_index_fast ((GArray*) array, index_);
1559
1560   return array;
1561 }
1562
1563 /**
1564  * g_byte_array_remove_range:
1565  * @array: a @GByteArray.
1566  * @index_: the index of the first byte to remove.
1567  * @length: the number of bytes to remove.
1568  * @Returns: the #GByteArray.
1569  *
1570  * Removes the given number of bytes starting at the given index from a
1571  * #GByteArray.  The following elements are moved to close the gap.
1572  *
1573  * Since: 2.4
1574  **/
1575 GByteArray*
1576 g_byte_array_remove_range (GByteArray *array,
1577                            guint       index_,
1578                            guint       length)
1579 {
1580   g_return_val_if_fail (array, NULL);
1581   g_return_val_if_fail (index_ < array->len, NULL);
1582   g_return_val_if_fail (index_ + length <= array->len, NULL);
1583
1584   return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1585 }
1586
1587 /**
1588  * g_byte_array_sort:
1589  * @array: a #GByteArray.
1590  * @compare_func: comparison function.
1591  *
1592  * Sorts a byte array, using @compare_func which should be a
1593  * qsort()-style comparison function (returns less than zero for first
1594  * arg is less than second arg, zero for equal, greater than zero if
1595  * first arg is greater than second arg).
1596  *
1597  * If two array elements compare equal, their order in the sorted array
1598  * is undefined. If you want equal elements to keep their order &#8211; i.e.
1599  * you want a stable sort &#8211; you can write a comparison function that,
1600  * if two elements would otherwise compare equal, compares them by
1601  * their addresses.
1602  **/
1603 void
1604 g_byte_array_sort (GByteArray   *array,
1605                    GCompareFunc  compare_func)
1606 {
1607   g_array_sort ((GArray *) array, compare_func);
1608 }
1609
1610 /**
1611  * g_byte_array_sort_with_data:
1612  * @array: a #GByteArray.
1613  * @compare_func: comparison function.
1614  * @user_data: data to pass to @compare_func.
1615  *
1616  * Like g_byte_array_sort(), but the comparison function takes an extra
1617  * user data argument.
1618  **/
1619 void
1620 g_byte_array_sort_with_data (GByteArray       *array,
1621                              GCompareDataFunc  compare_func,
1622                              gpointer          user_data)
1623 {
1624   g_array_sort_with_data ((GArray *) array, compare_func, user_data);
1625 }