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