Fix doc typos
[platform/upstream/glib.git] / glib / garray.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "garray.h"
37
38 #include "gmem.h"
39 #include "gthread.h"
40 #include "gmessages.h"
41 #include "gqsort.h"
42
43
44 /**
45  * SECTION:arrays
46  * @title: Arrays
47  * @short_description: arrays of arbitrary elements which grow
48  *                     automatically as elements are added
49  *
50  * Arrays are similar to standard C arrays, except that they grow
51  * automatically as elements are added.
52  *
53  * Array elements can be of any size (though all elements of one array
54  * are the same size), and the array can be automatically cleared to
55  * '0's and zero-terminated.
56  *
57  * To create a new array use g_array_new().
58  *
59  * To add elements to an array, use g_array_append_val(),
60  * g_array_append_vals(), g_array_prepend_val(), and
61  * g_array_prepend_vals().
62  *
63  * To access an element of an array, use g_array_index().
64  *
65  * To set the size of an array, use g_array_set_size().
66  *
67  * To free an array, use g_array_free().
68  *
69  * <example>
70  *  <title>Using a #GArray to store #gint values</title>
71  *  <programlisting>
72  *   GArray *garray;
73  *   gint i;
74  *   /<!-- -->* We create a new array to store gint values.
75  *      We don't want it zero-terminated or cleared to 0's. *<!-- -->/
76  *   garray = g_array_new (FALSE, FALSE, sizeof (gint));
77  *   for (i = 0; i &lt; 10000; i++)
78  *     g_array_append_val (garray, i);
79  *   for (i = 0; i &lt; 10000; i++)
80  *     if (g_array_index (garray, gint, i) != i)
81  *       g_print ("ERROR: got &percnt;d instead of &percnt;d\n",
82  *                g_array_index (garray, gint, i), i);
83  *   g_array_free (garray, TRUE);
84  *  </programlisting>
85  * </example>
86  **/
87
88 #define MIN_ARRAY_SIZE  16
89
90 typedef struct _GRealArray  GRealArray;
91
92 /**
93  * GArray:
94  * @data: a pointer to the element data. The data may be moved as
95  *        elements are added to the #GArray.
96  * @len: the number of elements in the #GArray not including the
97  *       possible terminating zero element.
98  *
99  * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
100  **/
101 struct _GRealArray
102 {
103   guint8 *data;
104   guint   len;
105   guint   alloc;
106   guint   elt_size;
107   guint   zero_terminated : 1;
108   guint   clear : 1;
109   gint    ref_count;
110 };
111
112 /**
113  * g_array_index:
114  * @a: a #GArray.
115  * @t: the type of the elements.
116  * @i: the index of the element to return.
117  * @Returns: the element of the #GArray at the index given by @i.
118  *
119  * Returns the element of a #GArray at the given index. The return
120  * value is cast to the given type.
121  *
122  * <example>
123  *  <title>Getting a pointer to an element in a #GArray</title>
124  *  <programlisting>
125  *   EDayViewEvent *event;
126  *   /<!-- -->* This gets a pointer to the 4th element
127  *      in the array of EDayViewEvent structs. *<!-- -->/
128  *   event = &amp;g_array_index (events, EDayViewEvent, 3);
129  *  </programlisting>
130  * </example>
131  **/
132
133 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
134 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
135 #define g_array_elt_zero(array, pos, len)                               \
136   (memset (g_array_elt_pos ((array), pos), 0,  g_array_elt_len ((array), len)))
137 #define g_array_zero_terminate(array) G_STMT_START{                     \
138   if ((array)->zero_terminated)                                         \
139     g_array_elt_zero ((array), (array)->len, 1);                        \
140 }G_STMT_END
141
142 static guint g_nearest_pow        (gint        num) G_GNUC_CONST;
143 static void  g_array_maybe_expand (GRealArray *array,
144                                    gint        len);
145
146 /**
147  * g_array_new:
148  * @zero_terminated: %TRUE if the array should have an extra element at
149  *                   the end which is set to 0.
150  * @clear_: %TRUE if #GArray elements should be automatically cleared
151  *          to 0 when they are allocated.
152  * @element_size: the size of each element in bytes.
153  * @Returns: the new #GArray.
154  *
155  * Creates a new #GArray with a reference count of 1.
156  **/
157 GArray*
158 g_array_new (gboolean zero_terminated,
159              gboolean clear,
160              guint    elt_size)
161 {
162   return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
163 }
164
165 /**
166  * g_array_sized_new:
167  * @zero_terminated: %TRUE if the array should have an extra element at
168  *                   the end with all bits cleared.
169  * @clear_: %TRUE if all bits in the array should be cleared to 0 on
170  *          allocation.
171  * @element_size: size of each element in the array.
172  * @reserved_size: number of elements preallocated.
173  * @Returns: the new #GArray.
174  *
175  * Creates a new #GArray with @reserved_size elements preallocated and
176  * a reference count of 1. This avoids frequent reallocation, if you
177  * are going to add many elements to the array. Note however that the
178  * size of the array is still 0.
179  **/
180 GArray* g_array_sized_new (gboolean zero_terminated,
181                            gboolean clear,
182                            guint    elt_size,
183                            guint    reserved_size)
184 {
185   GRealArray *array = g_slice_new (GRealArray);
186
187   array->data            = NULL;
188   array->len             = 0;
189   array->alloc           = 0;
190   array->zero_terminated = (zero_terminated ? 1 : 0);
191   array->clear           = (clear ? 1 : 0);
192   array->elt_size        = elt_size;
193   array->ref_count       = 1;
194
195   if (array->zero_terminated || reserved_size != 0)
196     {
197       g_array_maybe_expand (array, reserved_size);
198       g_array_zero_terminate(array);
199     }
200
201   return (GArray*) array;
202 }
203
204 /**
205  * g_array_ref:
206  * @array: A #GArray.
207  *
208  * Atomically increments the reference count of @array by one. This
209  * function is MT-safe and may be called from any thread.
210  *
211  * Returns: The passed in #GArray.
212  *
213  * Since: 2.22
214  **/
215 GArray *
216 g_array_ref (GArray *array)
217 {
218   GRealArray *rarray = (GRealArray*) array;
219   g_return_val_if_fail (array, NULL);
220
221   g_atomic_int_inc (&rarray->ref_count);
222
223   return array;
224 }
225
226 /**
227  * g_array_unref:
228  * @array: A #GArray.
229  *
230  * Atomically decrements the reference count of @array by one. If the
231  * reference count drops to 0, all memory allocated by the array is
232  * released. This function is MT-safe and may be called from any
233  * thread.
234  *
235  * Since: 2.22
236  **/
237 void
238 g_array_unref (GArray *array)
239 {
240   GRealArray *rarray = (GRealArray*) array;
241   g_return_if_fail (array);
242
243   if (g_atomic_int_dec_and_test (&rarray->ref_count))
244     g_array_free (array, TRUE);
245 }
246
247 /**
248  * g_array_get_element_size:
249  * @array: A #GArray.
250  *
251  * Gets the size of the elements in @array.
252  *
253  * Returns: Size of each element, in bytes.
254  *
255  * Since: 2.22
256  **/
257 guint
258 g_array_get_element_size (GArray *array)
259 {
260   GRealArray *rarray = (GRealArray*) array;
261
262   g_return_val_if_fail (array, 0);
263
264   return rarray->elt_size;
265 }
266
267 /**
268  * g_array_free:
269  * @array: a #GArray.
270  * @free_segment: if %TRUE the actual element data is freed as well.
271  * @Returns: the element data if @free_segment is %FALSE, otherwise
272  *           %NULL.  The element data should be freed using g_free().
273  *
274  * Frees the memory allocated for the #GArray. If @free_segment is
275  * %TRUE it frees the memory block holding the elements as well and
276  * also each element if @array has a @element_free_func set. Pass
277  * %FALSE if you want to free the #GArray wrapper but preserve the
278  * underlying array for use elsewhere. If the reference count of @array
279  * is greater than one, the #GArray wrapper is preserved but the size
280  * of @array will be set to zero.
281  *
282  * <note><para>If array elements contain dynamically-allocated memory,
283  * they should be freed separately.</para></note>
284  **/
285 gchar*
286 g_array_free (GArray   *farray,
287               gboolean  free_segment)
288 {
289   GRealArray *array = (GRealArray*) farray;
290   gchar* segment;
291   gboolean preserve_wrapper;
292
293   g_return_val_if_fail (array, NULL);
294
295   /* if others are holding a reference, preserve the wrapper but do free/return the data */
296   preserve_wrapper = FALSE;
297   if (g_atomic_int_get (&array->ref_count) > 1)
298     preserve_wrapper = TRUE;
299
300   if (free_segment)
301     {
302       g_free (array->data);
303       segment = NULL;
304     }
305   else
306     segment = (gchar*) array->data;
307
308   if (preserve_wrapper)
309     {
310       array->data            = NULL;
311       array->len             = 0;
312       array->alloc           = 0;
313     }
314   else
315     {
316       g_slice_free1 (sizeof (GRealArray), array);
317     }
318
319   return segment;
320 }
321
322 /**
323  * g_array_append_vals:
324  * @array: a #GArray.
325  * @data: a pointer to the elements to append to the end of the array.
326  * @len: the number of elements to append.
327  * @Returns: the #GArray.
328  *
329  * Adds @len elements onto the end of the array.
330  **/
331 /**
332  * g_array_append_val:
333  * @a: a #GArray.
334  * @v: the value to append to the #GArray.
335  * @Returns: the #GArray.
336  *
337  * Adds the value on to the end of the array. The array will grow in
338  * size automatically if necessary.
339  *
340  * <note><para>g_array_append_val() is a macro which uses a reference
341  * to the value parameter @v. This means that you cannot use it with
342  * literal values such as "27". You must use variables.</para></note>
343  **/
344 GArray*
345 g_array_append_vals (GArray       *farray,
346                      gconstpointer data,
347                      guint         len)
348 {
349   GRealArray *array = (GRealArray*) farray;
350
351   g_return_val_if_fail (array, NULL);
352
353   g_array_maybe_expand (array, len);
354
355   memcpy (g_array_elt_pos (array, array->len), data, 
356           g_array_elt_len (array, len));
357
358   array->len += len;
359
360   g_array_zero_terminate (array);
361
362   return farray;
363 }
364
365 /**
366  * g_array_prepend_vals:
367  * @array: a #GArray.
368  * @data: a pointer to the elements to prepend to the start of the
369  *        array.
370  * @len: the number of elements to prepend.
371  * @Returns: the #GArray.
372  *
373  * Adds @len elements onto the start of the array.
374  *
375  * This operation is slower than g_array_append_vals() since the
376  * existing elements in the array have to be moved to make space for
377  * the new elements.
378  **/
379 /**
380  * g_array_prepend_val:
381  * @a: a #GArray.
382  * @v: the value to prepend to the #GArray.
383  * @Returns: the #GArray.
384  *
385  * Adds the value on to the start of the array. The array will grow in
386  * size automatically if necessary.
387  *
388  * This operation is slower than g_array_append_val() since the
389  * existing elements in the array have to be moved to make space for
390  * the new element.
391  *
392  * <note><para>g_array_prepend_val() is a macro which uses a reference
393  * to the value parameter @v. This means that you cannot use it with
394  * literal values such as "27". You must use variables.</para></note>
395  **/
396 GArray*
397 g_array_prepend_vals (GArray        *farray,
398                       gconstpointer  data,
399                       guint          len)
400 {
401   GRealArray *array = (GRealArray*) farray;
402
403   g_return_val_if_fail (array, NULL);
404
405   g_array_maybe_expand (array, len);
406
407   g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0), 
408              g_array_elt_len (array, array->len));
409
410   memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
411
412   array->len += len;
413
414   g_array_zero_terminate (array);
415
416   return farray;
417 }
418
419 /**
420  * g_array_insert_vals:
421  * @array: a #GArray.
422  * @index_: the index to place the elements at.
423  * @data: a pointer to the elements to insert.
424  * @len: the number of elements to insert.
425  * @Returns: the #GArray.
426  *
427  * Inserts @len elements into a #GArray at the given index.
428  **/
429 /**
430  * g_array_insert_val:
431  * @a: a #GArray.
432  * @i: the index to place the element at.
433  * @v: the value to insert into the array.
434  * @Returns: the #GArray.
435  *
436  * Inserts an element into an array at the given index.
437  *
438  * <note><para>g_array_insert_val() is a macro which uses a reference
439  * to the value parameter @v. This means that you cannot use it with
440  * literal values such as "27". You must use variables.</para></note>
441  **/
442 GArray*
443 g_array_insert_vals (GArray        *farray,
444                      guint          index_,
445                      gconstpointer  data,
446                      guint          len)
447 {
448   GRealArray *array = (GRealArray*) farray;
449
450   g_return_val_if_fail (array, NULL);
451
452   g_array_maybe_expand (array, len);
453
454   g_memmove (g_array_elt_pos (array, len + index_), 
455              g_array_elt_pos (array, index_), 
456              g_array_elt_len (array, array->len - index_));
457
458   memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
459
460   array->len += len;
461
462   g_array_zero_terminate (array);
463
464   return farray;
465 }
466
467 /**
468  * g_array_set_size:
469  * @array: a #GArray.
470  * @length: the new size of the #GArray.
471  * @Returns: the #GArray.
472  *
473  * Sets the size of the array, expanding it if necessary. If the array
474  * was created with @clear_ set to %TRUE, the new elements are set to 0.
475  **/
476 GArray*
477 g_array_set_size (GArray *farray,
478                   guint   length)
479 {
480   GRealArray *array = (GRealArray*) farray;
481
482   g_return_val_if_fail (array, NULL);
483
484   if (length > array->len)
485     {
486       g_array_maybe_expand (array, length - array->len);
487       
488       if (array->clear)
489         g_array_elt_zero (array, array->len, length - array->len);
490     }
491   else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
492     g_array_elt_zero (array, length, array->len - length);
493   
494   array->len = length;
495   
496   g_array_zero_terminate (array);
497   
498   return farray;
499 }
500
501 /**
502  * g_array_remove_index:
503  * @array: a #GArray.
504  * @index_: the index of the element to remove.
505  * @Returns: the #GArray.
506  *
507  * Removes the element at the given index from a #GArray. The following
508  * elements are moved down one place.
509  **/
510 GArray*
511 g_array_remove_index (GArray *farray,
512                       guint   index_)
513 {
514   GRealArray* array = (GRealArray*) farray;
515
516   g_return_val_if_fail (array, NULL);
517
518   g_return_val_if_fail (index_ < array->len, NULL);
519
520   if (index_ != array->len - 1)
521     g_memmove (g_array_elt_pos (array, index_),
522                g_array_elt_pos (array, index_ + 1),
523                g_array_elt_len (array, array->len - index_ - 1));
524   
525   array->len -= 1;
526
527   if (G_UNLIKELY (g_mem_gc_friendly))
528     g_array_elt_zero (array, array->len, 1);
529   else
530     g_array_zero_terminate (array);
531
532   return farray;
533 }
534
535 /**
536  * g_array_remove_index_fast:
537  * @array: a @GArray.
538  * @index_: the index of the element to remove.
539  * @Returns: the #GArray.
540  *
541  * Removes the element at the given index from a #GArray. The last
542  * element in the array is used to fill in the space, so this function
543  * does not preserve the order of the #GArray. But it is faster than
544  * g_array_remove_index().
545  **/
546 GArray*
547 g_array_remove_index_fast (GArray *farray,
548                            guint   index_)
549 {
550   GRealArray* array = (GRealArray*) farray;
551
552   g_return_val_if_fail (array, NULL);
553
554   g_return_val_if_fail (index_ < array->len, NULL);
555
556   if (index_ != array->len - 1)
557     memcpy (g_array_elt_pos (array, index_), 
558             g_array_elt_pos (array, array->len - 1),
559             g_array_elt_len (array, 1));
560   
561   array->len -= 1;
562
563   if (G_UNLIKELY (g_mem_gc_friendly))
564     g_array_elt_zero (array, array->len, 1);
565   else
566     g_array_zero_terminate (array);
567
568   return farray;
569 }
570
571 /**
572  * g_array_remove_range:
573  * @array: a @GArray.
574  * @index_: the index of the first element to remove.
575  * @length: the number of elements to remove.
576  * @Returns: the #GArray.
577  *
578  * Removes the given number of elements starting at the given index
579  * from a #GArray.  The following elements are moved to close the gap.
580  *
581  * Since: 2.4
582  **/
583 GArray*
584 g_array_remove_range (GArray *farray,
585                       guint   index_,
586                       guint   length)
587 {
588   GRealArray *array = (GRealArray*) farray;
589
590   g_return_val_if_fail (array, NULL);
591   g_return_val_if_fail (index_ < array->len, NULL);
592   g_return_val_if_fail (index_ + length <= array->len, NULL);
593
594   if (index_ + length != array->len)
595     g_memmove (g_array_elt_pos (array, index_), 
596                g_array_elt_pos (array, index_ + length), 
597                (array->len - (index_ + length)) * array->elt_size);
598
599   array->len -= length;
600   if (G_UNLIKELY (g_mem_gc_friendly))
601     g_array_elt_zero (array, array->len, length);
602   else
603     g_array_zero_terminate (array);
604
605   return farray;
606 }
607
608 /**
609  * g_array_sort:
610  * @array: a #GArray.
611  * @compare_func: comparison function.
612  *
613  * Sorts a #GArray using @compare_func which should be a qsort()-style
614  * comparison function (returns less than zero for first arg is less
615  * than second arg, zero for equal, greater zero if first arg is
616  * greater than second arg).
617  *
618  * If two array elements compare equal, their order in the sorted array
619  * is undefined. If you want equal elements to keep their order &#8211; i.e.
620  * you want a stable sort &#8211; you can write a comparison function that,
621  * if two elements would otherwise compare equal, compares them by
622  * their addresses.
623  **/
624 void
625 g_array_sort (GArray       *farray,
626               GCompareFunc  compare_func)
627 {
628   GRealArray *array = (GRealArray*) farray;
629
630   g_return_if_fail (array != NULL);
631
632   qsort (array->data,
633          array->len,
634          array->elt_size,
635          compare_func);
636 }
637
638 /**
639  * g_array_sort_with_data:
640  * @array: a #GArray.
641  * @compare_func: comparison function.
642  * @user_data: data to pass to @compare_func.
643  *
644  * Like g_array_sort(), but the comparison function receives an extra
645  * user data argument.
646  **/
647 void
648 g_array_sort_with_data (GArray           *farray,
649                         GCompareDataFunc  compare_func,
650                         gpointer          user_data)
651 {
652   GRealArray *array = (GRealArray*) farray;
653
654   g_return_if_fail (array != NULL);
655
656   g_qsort_with_data (array->data,
657                      array->len,
658                      array->elt_size,
659                      compare_func,
660                      user_data);
661 }
662
663 /* Returns the smallest power of 2 greater than n, or n if
664  * such power does not fit in a guint
665  */
666 static guint
667 g_nearest_pow (gint num)
668 {
669   guint n = 1;
670
671   while (n < num && n > 0)
672     n <<= 1;
673
674   return n ? n : num;
675 }
676
677 static void
678 g_array_maybe_expand (GRealArray *array,
679                       gint        len)
680 {
681   guint want_alloc = g_array_elt_len (array, array->len + len + 
682                                       array->zero_terminated);
683
684   if (want_alloc > array->alloc)
685     {
686       want_alloc = g_nearest_pow (want_alloc);
687       want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
688
689       array->data = g_realloc (array->data, want_alloc);
690
691       if (G_UNLIKELY (g_mem_gc_friendly))
692         memset (array->data + array->alloc, 0, want_alloc - array->alloc);
693
694       array->alloc = want_alloc;
695     }
696 }
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   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
879   g_atomic_int_inc (&rarray->ref_count);
880
881   return array;
882 }
883
884 /**
885  * g_ptr_array_unref:
886  * @array: A #GPtrArray.
887  *
888  * Atomically decrements the reference count of @array by one. If the
889  * reference count drops to 0, the effect is the same as calling
890  * g_ptr_array_free() with @free_segment set to %TRUE. This function
891  * is MT-safe and may be called from any thread.
892  *
893  * Since: 2.22
894  **/
895 void
896 g_ptr_array_unref (GPtrArray *array)
897 {
898   GRealPtrArray *rarray = (GRealPtrArray*) array;
899   g_return_if_fail (array);
900
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 (array->element_free_func != NULL)
1080     array->element_free_func (array->pdata[index_]);
1081
1082   if (index_ != array->len - 1)
1083     array->pdata[index_] = array->pdata[array->len - 1];
1084
1085   array->len -= 1;
1086
1087   if (G_UNLIKELY (g_mem_gc_friendly))
1088     array->pdata[array->len] = NULL;
1089
1090   return result;
1091 }
1092
1093 /**
1094  * g_ptr_array_remove_range:
1095  * @array: a @GPtrArray.
1096  * @index_: the index of the first pointer to remove.
1097  * @length: the number of pointers to remove.
1098  *
1099  * Removes the given number of pointers starting at the given index
1100  * from a #GPtrArray.  The following elements are moved to close the
1101  * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1102  * for the removed elements.
1103  *
1104  * Since: 2.4
1105  **/
1106 void
1107 g_ptr_array_remove_range (GPtrArray *farray,
1108                           guint      index_,
1109                           guint      length)
1110 {
1111   GRealPtrArray* array = (GRealPtrArray*) farray;
1112   guint n;
1113
1114   g_return_if_fail (array);
1115   g_return_if_fail (index_ < array->len);
1116   g_return_if_fail (index_ + length <= array->len);
1117
1118   if (array->element_free_func != NULL)
1119     {
1120       for (n = index_; n < index_ + length; n++)
1121         array->element_free_func (array->pdata[n]);
1122     }
1123
1124   if (index_ + length != array->len)
1125     {
1126       g_memmove (&array->pdata[index_],
1127                  &array->pdata[index_ + length], 
1128                  (array->len - (index_ + length)) * sizeof (gpointer));
1129     }
1130
1131   array->len -= length;
1132   if (G_UNLIKELY (g_mem_gc_friendly))
1133     {
1134       guint i;
1135       for (i = 0; i < length; i++)
1136         array->pdata[array->len + i] = NULL;
1137     }
1138 }
1139
1140 /**
1141  * g_ptr_array_remove:
1142  * @array: a #GPtrArray.
1143  * @data: the pointer to remove.
1144  * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1145  *           not found in the array.
1146  *
1147  * Removes the first occurrence of the given pointer from the pointer
1148  * array. The following elements are moved down one place. If @array
1149  * has a non-%NULL #GDestroyNotify function it is called for the
1150  * removed element.
1151  *
1152  * It returns %TRUE if the pointer was removed, or %FALSE if the
1153  * pointer was not found.
1154  **/
1155 gboolean
1156 g_ptr_array_remove (GPtrArray *farray,
1157                     gpointer   data)
1158 {
1159   GRealPtrArray* array = (GRealPtrArray*) farray;
1160   guint i;
1161
1162   g_return_val_if_fail (array, FALSE);
1163
1164   for (i = 0; i < array->len; i += 1)
1165     {
1166       if (array->pdata[i] == data)
1167         {
1168           g_ptr_array_remove_index (farray, i);
1169           return TRUE;
1170         }
1171     }
1172
1173   return FALSE;
1174 }
1175
1176 /**
1177  * g_ptr_array_remove_fast:
1178  * @array: a #GPtrArray.
1179  * @data: the pointer to remove.
1180  * @Returns: %TRUE if the pointer was found in the array.
1181  *
1182  * Removes the first occurrence of the given pointer from the pointer
1183  * array. The last element in the array is used to fill in the space,
1184  * so this function does not preserve the order of the array. But it is
1185  * faster than g_ptr_array_remove(). If @array has a non-%NULL
1186  * #GDestroyNotify function it is called for the removed element.
1187  *
1188  * It returns %TRUE if the pointer was removed, or %FALSE if the
1189  * pointer was not found.
1190  **/
1191 gboolean
1192 g_ptr_array_remove_fast (GPtrArray *farray,
1193                          gpointer   data)
1194 {
1195   GRealPtrArray* array = (GRealPtrArray*) farray;
1196   guint i;
1197
1198   g_return_val_if_fail (array, FALSE);
1199
1200   for (i = 0; i < array->len; i += 1)
1201     {
1202       if (array->pdata[i] == data)
1203         {
1204           g_ptr_array_remove_index_fast (farray, i);
1205           return TRUE;
1206         }
1207     }
1208
1209   return FALSE;
1210 }
1211
1212 /**
1213  * g_ptr_array_add:
1214  * @array: a #GPtrArray.
1215  * @data: the pointer to add.
1216  *
1217  * Adds a pointer to the end of the pointer array. The array will grow
1218  * in size automatically if necessary.
1219  **/
1220 void
1221 g_ptr_array_add (GPtrArray *farray,
1222                  gpointer   data)
1223 {
1224   GRealPtrArray* array = (GRealPtrArray*) farray;
1225
1226   g_return_if_fail (array);
1227
1228   g_ptr_array_maybe_expand (array, 1);
1229
1230   array->pdata[array->len++] = data;
1231 }
1232
1233 /**
1234  * g_ptr_array_sort:
1235  * @array: a #GPtrArray.
1236  * @compare_func: comparison function.
1237  *
1238  * Sorts the array, using @compare_func which should be a qsort()-style
1239  * comparison function (returns less than zero for first arg is less
1240  * than second arg, zero for equal, greater than zero if irst arg is
1241  * greater than second arg).
1242  *
1243  * If two array elements compare equal, their order in the sorted array
1244  * is undefined. If you want equal elements to keep their order &#8211; i.e.
1245  * you want a stable sort &#8211; you can write a comparison function that,
1246  * if two elements would otherwise compare equal, compares them by
1247  * their addresses.
1248  *
1249  * <note><para>The comparison function for g_ptr_array_sort() doesn't
1250  * take the pointers from the array as arguments, it takes pointers to
1251  * the pointers in the array.</para></note>
1252  **/
1253 void
1254 g_ptr_array_sort (GPtrArray    *array,
1255                   GCompareFunc  compare_func)
1256 {
1257   g_return_if_fail (array != NULL);
1258
1259   qsort (array->pdata,
1260          array->len,
1261          sizeof (gpointer),
1262          compare_func);
1263 }
1264
1265 /**
1266  * g_ptr_array_sort_with_data:
1267  * @array: a #GPtrArray.
1268  * @compare_func: comparison function.
1269  * @user_data: data to pass to @compare_func.
1270  *
1271  * Like g_ptr_array_sort(), but the comparison function has an extra
1272  * user data argument.
1273  *
1274  * <note><para>The comparison function for g_ptr_array_sort_with_data()
1275  * doesn't take the pointers from the array as arguments, it takes
1276  * pointers to the pointers in the array.</para></note>
1277  **/
1278 void
1279 g_ptr_array_sort_with_data (GPtrArray        *array,
1280                             GCompareDataFunc  compare_func,
1281                             gpointer          user_data)
1282 {
1283   g_return_if_fail (array != NULL);
1284
1285   g_qsort_with_data (array->pdata,
1286                      array->len,
1287                      sizeof (gpointer),
1288                      compare_func,
1289                      user_data);
1290 }
1291
1292 /**
1293  * g_ptr_array_foreach:
1294  * @array: a #GPtrArray
1295  * @func: the function to call for each array element
1296  * @user_data: user data to pass to the function
1297  * 
1298  * Calls a function for each element of a #GPtrArray.
1299  *
1300  * Since: 2.4
1301  **/
1302 void
1303 g_ptr_array_foreach (GPtrArray *array,
1304                      GFunc      func,
1305                      gpointer   user_data)
1306 {
1307   guint i;
1308
1309   g_return_if_fail (array);
1310
1311   for (i = 0; i < array->len; i++)
1312     (*func) (array->pdata[i], user_data);
1313 }
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. If you want equal elements to keep their order &#8211; i.e.
1570  * you want a stable sort &#8211; you can write a comparison function that,
1571  * if two elements would otherwise compare equal, compares them by
1572  * their addresses.
1573  **/
1574 void
1575 g_byte_array_sort (GByteArray   *array,
1576                    GCompareFunc  compare_func)
1577 {
1578   g_array_sort ((GArray *) array, compare_func);
1579 }
1580
1581 /**
1582  * g_byte_array_sort_with_data:
1583  * @array: a #GByteArray.
1584  * @compare_func: comparison function.
1585  * @user_data: data to pass to @compare_func.
1586  *
1587  * Like g_byte_array_sort(), but the comparison function takes an extra
1588  * user data argument.
1589  **/
1590 void
1591 g_byte_array_sort_with_data (GByteArray       *array,
1592                              GCompareDataFunc  compare_func,
1593                              gpointer          user_data)
1594 {
1595   g_array_sort_with_data ((GArray *) array, compare_func, user_data);
1596 }