Move deprecated GRel to deprecated/
[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_new_full:
841  * @reserved_size: number of pointers preallocated.
842  * @element_free_func: A function to free elements with destroy @array or %NULL.
843  *
844  * Creates a new #GPtrArray with @reserved_size pointers preallocated
845  * and a reference count of 1. This avoids frequent reallocation, if
846  * you are going to add many pointers to the array. Note however that
847  * the size of the array is still 0. It also set @element_free_func
848  * for freeing each element when the array is destroyed either via
849  * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
850  * set to %TRUE or when removing elements.
851  *
852  * Returns: A new #GPtrArray.
853  *
854  * Since: 2.30
855  **/
856 GPtrArray *
857 g_ptr_array_new_full (guint          reserved_size,
858                       GDestroyNotify element_free_func)
859 {
860   GPtrArray *array;
861
862   array = g_ptr_array_sized_new (reserved_size);
863   g_ptr_array_set_free_func (array, element_free_func);
864   return array;
865 }
866
867 /**
868  * g_ptr_array_set_free_func:
869  * @array: A #GPtrArray.
870  * @element_free_func: A function to free elements with destroy @array or %NULL.
871  *
872  * Sets a function for freeing each element when @array is destroyed
873  * either via g_ptr_array_unref(), when g_ptr_array_free() is called
874  * with @free_segment set to %TRUE or when removing elements.
875  *
876  * Since: 2.22
877  **/
878 void
879 g_ptr_array_set_free_func (GPtrArray        *array,
880                            GDestroyNotify    element_free_func)
881 {
882   GRealPtrArray* rarray = (GRealPtrArray*) array;
883
884   g_return_if_fail (array);
885
886   rarray->element_free_func = element_free_func;
887 }
888
889 /**
890  * g_ptr_array_ref:
891  * @array: A #GArray.
892  *
893  * Atomically increments the reference count of @array by one. This
894  * function is MT-safe and may be called from any thread.
895  *
896  * Returns: The passed in #GPtrArray.
897  *
898  * Since: 2.22
899  **/
900 GPtrArray *
901 g_ptr_array_ref (GPtrArray *array)
902 {
903   GRealPtrArray *rarray = (GRealPtrArray*) array;
904
905   g_return_val_if_fail (array, NULL);
906
907   g_atomic_int_inc (&rarray->ref_count);
908
909   return array;
910 }
911
912 /**
913  * g_ptr_array_unref:
914  * @array: A #GPtrArray.
915  *
916  * Atomically decrements the reference count of @array by one. If the
917  * reference count drops to 0, the effect is the same as calling
918  * g_ptr_array_free() with @free_segment set to %TRUE. This function
919  * is MT-safe and may be called from any thread.
920  *
921  * Since: 2.22
922  **/
923 void
924 g_ptr_array_unref (GPtrArray *array)
925 {
926   GRealPtrArray *rarray = (GRealPtrArray*) array;
927   g_return_if_fail (array);
928
929   if (g_atomic_int_dec_and_test (&rarray->ref_count))
930     g_ptr_array_free (array, TRUE);
931 }
932
933 /**
934  * g_ptr_array_free:
935  * @array: a #GPtrArray.
936  * @free_seg: if %TRUE the actual pointer array is freed as well.
937  * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
938  *           The pointer array should be freed using g_free().
939  *
940  * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
941  * it frees the memory block holding the elements as well. Pass %FALSE
942  * if you want to free the #GPtrArray wrapper but preserve the
943  * underlying array for use elsewhere. If the reference count of @array
944  * is greater than one, the #GPtrArray wrapper is preserved but the
945  * size of @array will be set to zero.
946  *
947  * <note><para>If array contents point to dynamically-allocated
948  * memory, they should be freed separately if @free_seg is %TRUE and no
949  * #GDestroyNotify function has been set for @array.</para></note>
950  **/
951 gpointer*
952 g_ptr_array_free (GPtrArray *farray,
953                   gboolean   free_segment)
954 {
955   GRealPtrArray *array = (GRealPtrArray*) farray;
956   gpointer* segment;
957   gboolean preserve_wrapper;
958
959   g_return_val_if_fail (array, NULL);
960
961   /* if others are holding a reference, preserve the wrapper but do free/return the data */
962   preserve_wrapper = FALSE;
963   if (g_atomic_int_get (&array->ref_count) > 1)
964     preserve_wrapper = TRUE;
965
966   if (free_segment)
967     {
968       if (array->element_free_func != NULL)
969         g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
970       g_free (array->pdata);
971       segment = NULL;
972     }
973   else
974     segment = array->pdata;
975
976   if (preserve_wrapper)
977     {
978       array->pdata = NULL;
979       array->len = 0;
980       array->alloc = 0;
981     }
982   else
983     {
984       g_slice_free1 (sizeof (GRealPtrArray), array);
985     }
986
987   return segment;
988 }
989
990 static void
991 g_ptr_array_maybe_expand (GRealPtrArray *array,
992                           gint           len)
993 {
994   if ((array->len + len) > array->alloc)
995     {
996       guint old_alloc = array->alloc;
997       array->alloc = g_nearest_pow (array->len + len);
998       array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
999       array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1000       if (G_UNLIKELY (g_mem_gc_friendly))
1001         for ( ; old_alloc < array->alloc; old_alloc++)
1002           array->pdata [old_alloc] = NULL;
1003     }
1004 }
1005
1006 /**
1007  * g_ptr_array_set_size:
1008  * @array: a #GPtrArray.
1009  * @length: the new length of the pointer array.
1010  *
1011  * Sets the size of the array. When making the array larger,
1012  * newly-added elements will be set to %NULL. When making it smaller,
1013  * if @array has a non-%NULL #GDestroyNotify function then it will be
1014  * called for the removed elements.
1015  **/
1016 void
1017 g_ptr_array_set_size  (GPtrArray *farray,
1018                        gint       length)
1019 {
1020   GRealPtrArray* array = (GRealPtrArray*) farray;
1021
1022   g_return_if_fail (array);
1023
1024   if (length > array->len)
1025     {
1026       int i;
1027       g_ptr_array_maybe_expand (array, (length - array->len));
1028       /* This is not 
1029        *     memset (array->pdata + array->len, 0,
1030        *            sizeof (gpointer) * (length - array->len));
1031        * to make it really portable. Remember (void*)NULL needn't be
1032        * bitwise zero. It of course is silly not to use memset (..,0,..).
1033        */
1034       for (i = array->len; i < length; i++)
1035         array->pdata[i] = NULL;
1036     }
1037   else if (length < array->len)
1038     g_ptr_array_remove_range (farray, length, array->len - length);
1039
1040   array->len = length;
1041 }
1042
1043 /**
1044  * g_ptr_array_remove_index:
1045  * @array: a #GPtrArray.
1046  * @index_: the index of the pointer to remove.
1047  * @Returns: the pointer which was removed.
1048  *
1049  * Removes the pointer at the given index from the pointer array. The
1050  * following elements are moved down one place. If @array has a
1051  * non-%NULL #GDestroyNotify function it is called for the removed
1052  * element.
1053  **/
1054 gpointer
1055 g_ptr_array_remove_index (GPtrArray *farray,
1056                           guint      index_)
1057 {
1058   GRealPtrArray* array = (GRealPtrArray*) farray;
1059   gpointer result;
1060
1061   g_return_val_if_fail (array, NULL);
1062
1063   g_return_val_if_fail (index_ < array->len, NULL);
1064
1065   result = array->pdata[index_];
1066   
1067   if (array->element_free_func != NULL)
1068     array->element_free_func (array->pdata[index_]);
1069
1070   if (index_ != array->len - 1)
1071     g_memmove (array->pdata + index_, array->pdata + index_ + 1, 
1072                sizeof (gpointer) * (array->len - index_ - 1));
1073   
1074   array->len -= 1;
1075
1076   if (G_UNLIKELY (g_mem_gc_friendly))
1077     array->pdata[array->len] = NULL;
1078
1079   return result;
1080 }
1081
1082 /**
1083  * g_ptr_array_remove_index_fast:
1084  * @array: a #GPtrArray.
1085  * @index_: the index of the pointer to remove.
1086  * @Returns: the pointer which was removed.
1087  *
1088  * Removes the pointer at the given index from the pointer array. The
1089  * last element in the array is used to fill in the space, so this
1090  * function does not preserve the order of the array. But it is faster
1091  * than g_ptr_array_remove_index(). If @array has a non-%NULL
1092  * #GDestroyNotify function it is called for the removed element.
1093  **/
1094 gpointer
1095 g_ptr_array_remove_index_fast (GPtrArray *farray,
1096                                guint      index_)
1097 {
1098   GRealPtrArray* array = (GRealPtrArray*) farray;
1099   gpointer result;
1100
1101   g_return_val_if_fail (array, NULL);
1102
1103   g_return_val_if_fail (index_ < array->len, NULL);
1104
1105   result = array->pdata[index_];
1106
1107   if (array->element_free_func != NULL)
1108     array->element_free_func (array->pdata[index_]);
1109
1110   if (index_ != array->len - 1)
1111     array->pdata[index_] = array->pdata[array->len - 1];
1112
1113   array->len -= 1;
1114
1115   if (G_UNLIKELY (g_mem_gc_friendly))
1116     array->pdata[array->len] = NULL;
1117
1118   return result;
1119 }
1120
1121 /**
1122  * g_ptr_array_remove_range:
1123  * @array: a @GPtrArray.
1124  * @index_: the index of the first pointer to remove.
1125  * @length: the number of pointers to remove.
1126  *
1127  * Removes the given number of pointers starting at the given index
1128  * from a #GPtrArray.  The following elements are moved to close the
1129  * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1130  * for the removed elements.
1131  *
1132  * Since: 2.4
1133  **/
1134 void
1135 g_ptr_array_remove_range (GPtrArray *farray,
1136                           guint      index_,
1137                           guint      length)
1138 {
1139   GRealPtrArray* array = (GRealPtrArray*) farray;
1140   guint n;
1141
1142   g_return_if_fail (array);
1143   g_return_if_fail (index_ < array->len);
1144   g_return_if_fail (index_ + length <= array->len);
1145
1146   if (array->element_free_func != NULL)
1147     {
1148       for (n = index_; n < index_ + length; n++)
1149         array->element_free_func (array->pdata[n]);
1150     }
1151
1152   if (index_ + length != array->len)
1153     {
1154       g_memmove (&array->pdata[index_],
1155                  &array->pdata[index_ + length], 
1156                  (array->len - (index_ + length)) * sizeof (gpointer));
1157     }
1158
1159   array->len -= length;
1160   if (G_UNLIKELY (g_mem_gc_friendly))
1161     {
1162       guint i;
1163       for (i = 0; i < length; i++)
1164         array->pdata[array->len + i] = NULL;
1165     }
1166 }
1167
1168 /**
1169  * g_ptr_array_remove:
1170  * @array: a #GPtrArray.
1171  * @data: the pointer to remove.
1172  * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1173  *           not found in the array.
1174  *
1175  * Removes the first occurrence of the given pointer from the pointer
1176  * array. The following elements are moved down one place. If @array
1177  * has a non-%NULL #GDestroyNotify function it is called for the
1178  * removed element.
1179  *
1180  * It returns %TRUE if the pointer was removed, or %FALSE if the
1181  * pointer was not found.
1182  **/
1183 gboolean
1184 g_ptr_array_remove (GPtrArray *farray,
1185                     gpointer   data)
1186 {
1187   GRealPtrArray* array = (GRealPtrArray*) farray;
1188   guint i;
1189
1190   g_return_val_if_fail (array, FALSE);
1191
1192   for (i = 0; i < array->len; i += 1)
1193     {
1194       if (array->pdata[i] == data)
1195         {
1196           g_ptr_array_remove_index (farray, i);
1197           return TRUE;
1198         }
1199     }
1200
1201   return FALSE;
1202 }
1203
1204 /**
1205  * g_ptr_array_remove_fast:
1206  * @array: a #GPtrArray.
1207  * @data: the pointer to remove.
1208  * @Returns: %TRUE if the pointer was found in the array.
1209  *
1210  * Removes the first occurrence of the given pointer from the pointer
1211  * array. The last element in the array is used to fill in the space,
1212  * so this function does not preserve the order of the array. But it is
1213  * faster than g_ptr_array_remove(). If @array has a non-%NULL
1214  * #GDestroyNotify function it is called for the removed element.
1215  *
1216  * It returns %TRUE if the pointer was removed, or %FALSE if the
1217  * pointer was not found.
1218  **/
1219 gboolean
1220 g_ptr_array_remove_fast (GPtrArray *farray,
1221                          gpointer   data)
1222 {
1223   GRealPtrArray* array = (GRealPtrArray*) farray;
1224   guint i;
1225
1226   g_return_val_if_fail (array, FALSE);
1227
1228   for (i = 0; i < array->len; i += 1)
1229     {
1230       if (array->pdata[i] == data)
1231         {
1232           g_ptr_array_remove_index_fast (farray, i);
1233           return TRUE;
1234         }
1235     }
1236
1237   return FALSE;
1238 }
1239
1240 /**
1241  * g_ptr_array_add:
1242  * @array: a #GPtrArray.
1243  * @data: the pointer to add.
1244  *
1245  * Adds a pointer to the end of the pointer array. The array will grow
1246  * in size automatically if necessary.
1247  **/
1248 void
1249 g_ptr_array_add (GPtrArray *farray,
1250                  gpointer   data)
1251 {
1252   GRealPtrArray* array = (GRealPtrArray*) farray;
1253
1254   g_return_if_fail (array);
1255
1256   g_ptr_array_maybe_expand (array, 1);
1257
1258   array->pdata[array->len++] = data;
1259 }
1260
1261 /**
1262  * g_ptr_array_sort:
1263  * @array: a #GPtrArray.
1264  * @compare_func: comparison function.
1265  *
1266  * Sorts the array, using @compare_func which should be a qsort()-style
1267  * comparison function (returns less than zero for first arg is less
1268  * than second arg, zero for equal, greater than zero if irst arg is
1269  * greater than second arg).
1270  *
1271  * If two array elements compare equal, their order in the sorted array
1272  * is undefined. If you want equal elements to keep their order &#8211; i.e.
1273  * you want a stable sort &#8211; you can write a comparison function that,
1274  * if two elements would otherwise compare equal, compares them by
1275  * their addresses.
1276  *
1277  * <note><para>The comparison function for g_ptr_array_sort() doesn't
1278  * take the pointers from the array as arguments, it takes pointers to
1279  * the pointers in the array.</para></note>
1280  **/
1281 void
1282 g_ptr_array_sort (GPtrArray    *array,
1283                   GCompareFunc  compare_func)
1284 {
1285   g_return_if_fail (array != NULL);
1286
1287   qsort (array->pdata,
1288          array->len,
1289          sizeof (gpointer),
1290          compare_func);
1291 }
1292
1293 /**
1294  * g_ptr_array_sort_with_data:
1295  * @array: a #GPtrArray.
1296  * @compare_func: comparison function.
1297  * @user_data: data to pass to @compare_func.
1298  *
1299  * Like g_ptr_array_sort(), but the comparison function has an extra
1300  * user data argument.
1301  *
1302  * <note><para>The comparison function for g_ptr_array_sort_with_data()
1303  * doesn't take the pointers from the array as arguments, it takes
1304  * pointers to the pointers in the array.</para></note>
1305  **/
1306 void
1307 g_ptr_array_sort_with_data (GPtrArray        *array,
1308                             GCompareDataFunc  compare_func,
1309                             gpointer          user_data)
1310 {
1311   g_return_if_fail (array != NULL);
1312
1313   g_qsort_with_data (array->pdata,
1314                      array->len,
1315                      sizeof (gpointer),
1316                      compare_func,
1317                      user_data);
1318 }
1319
1320 /**
1321  * g_ptr_array_foreach:
1322  * @array: a #GPtrArray
1323  * @func: the function to call for each array element
1324  * @user_data: user data to pass to the function
1325  * 
1326  * Calls a function for each element of a #GPtrArray.
1327  *
1328  * Since: 2.4
1329  **/
1330 void
1331 g_ptr_array_foreach (GPtrArray *array,
1332                      GFunc      func,
1333                      gpointer   user_data)
1334 {
1335   guint i;
1336
1337   g_return_if_fail (array);
1338
1339   for (i = 0; i < array->len; i++)
1340     (*func) (array->pdata[i], user_data);
1341 }
1342
1343 /**
1344  * SECTION:arrays_byte
1345  * @title: Byte Arrays
1346  * @short_description: arrays of bytes, which grow automatically as
1347  *                     elements are added
1348  *
1349  * #GByteArray is based on #GArray, to provide arrays of bytes which
1350  * grow automatically as elements are added.
1351  *
1352  * To create a new #GByteArray use g_byte_array_new().
1353  *
1354  * To add elements to a #GByteArray, use g_byte_array_append(), and
1355  * g_byte_array_prepend().
1356  *
1357  * To set the size of a #GByteArray, use g_byte_array_set_size().
1358  *
1359  * To free a #GByteArray, use g_byte_array_free().
1360  *
1361  * <example>
1362  *  <title>Using a #GByteArray</title>
1363  *  <programlisting>
1364  *   GByteArray *gbarray;
1365  *   gint i;
1366  *
1367  *   gbarray = g_byte_array_new (<!-- -->);
1368  *   for (i = 0; i &lt; 10000; i++)
1369  *     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1370  *
1371  *   for (i = 0; i &lt; 10000; i++)
1372  *     {
1373  *       g_assert (gbarray->data[4*i] == 'a');
1374  *       g_assert (gbarray->data[4*i+1] == 'b');
1375  *       g_assert (gbarray->data[4*i+2] == 'c');
1376  *       g_assert (gbarray->data[4*i+3] == 'd');
1377  *     }
1378  *
1379  *   g_byte_array_free (gbarray, TRUE);
1380  *  </programlisting>
1381  * </example>
1382  **/
1383
1384 /**
1385  * GByteArray:
1386  * @data: a pointer to the element data. The data may be moved as
1387  *        elements are added to the #GByteArray.
1388  * @len: the number of elements in the #GByteArray.
1389  *
1390  * The <structname>GByteArray</structname> struct allows access to the
1391  * public fields of a <structname>GByteArray</structname>.
1392  **/
1393
1394 /**
1395  * g_byte_array_new:
1396  * @Returns: the new #GByteArray.
1397  *
1398  * Creates a new #GByteArray with a reference count of 1.
1399  **/
1400 GByteArray* g_byte_array_new (void)
1401 {
1402   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1403 }
1404
1405 /**
1406  * g_byte_array_sized_new:
1407  * @reserved_size: number of bytes preallocated.
1408  * @Returns: the new #GByteArray.
1409  *
1410  * Creates a new #GByteArray with @reserved_size bytes preallocated.
1411  * This avoids frequent reallocation, if you are going to add many
1412  * bytes to the array. Note however that the size of the array is still
1413  * 0.
1414  **/
1415 GByteArray* g_byte_array_sized_new (guint reserved_size)
1416 {
1417   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1418 }
1419
1420 /**
1421  * g_byte_array_free:
1422  * @array: a #GByteArray.
1423  * @free_segment: if %TRUE the actual byte data is freed as well.
1424  * @Returns: the element data if @free_segment is %FALSE, otherwise
1425  *           %NULL.  The element data should be freed using g_free().
1426  *
1427  * Frees the memory allocated by the #GByteArray. If @free_segment is
1428  * %TRUE it frees the actual byte data. If the reference count of
1429  * @array is greater than one, the #GByteArray wrapper is preserved but
1430  * the size of @array will be set to zero.
1431  **/
1432 guint8*     g_byte_array_free     (GByteArray *array,
1433                                    gboolean    free_segment)
1434 {
1435   return (guint8*) g_array_free ((GArray*) array, free_segment);
1436 }
1437
1438 /**
1439  * g_byte_array_ref:
1440  * @array: A #GByteArray.
1441  *
1442  * Atomically increments the reference count of @array by one. This
1443  * function is MT-safe and may be called from any thread.
1444  *
1445  * Returns: The passed in #GByteArray.
1446  *
1447  * Since: 2.22
1448  **/
1449 GByteArray *
1450 g_byte_array_ref (GByteArray *array)
1451 {
1452   return (GByteArray *) g_array_ref ((GArray *) array);
1453 }
1454
1455 /**
1456  * g_byte_array_unref:
1457  * @array: A #GByteArray.
1458  *
1459  * Atomically decrements the reference count of @array by one. If the
1460  * reference count drops to 0, all memory allocated by the array is
1461  * released. This function is MT-safe and may be called from any
1462  * thread.
1463  *
1464  * Since: 2.22
1465  **/
1466 void
1467 g_byte_array_unref (GByteArray *array)
1468 {
1469   g_array_unref ((GArray *) array);
1470 }
1471
1472 /**
1473  * g_byte_array_append:
1474  * @array: a #GByteArray.
1475  * @data: the byte data to be added.
1476  * @len: the number of bytes to add.
1477  * @Returns: the #GByteArray.
1478  *
1479  * Adds the given bytes to the end of the #GByteArray. The array will
1480  * grow in size automatically if necessary.
1481  **/
1482 GByteArray* g_byte_array_append   (GByteArray   *array,
1483                                    const guint8 *data,
1484                                    guint         len)
1485 {
1486   g_array_append_vals ((GArray*) array, (guint8*)data, len);
1487
1488   return array;
1489 }
1490
1491 /**
1492  * g_byte_array_prepend:
1493  * @array: a #GByteArray.
1494  * @data: the byte data to be added.
1495  * @len: the number of bytes to add.
1496  * @Returns: the #GByteArray.
1497  *
1498  * Adds the given data to the start of the #GByteArray. The array will
1499  * grow in size automatically if necessary.
1500  **/
1501 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1502                                    const guint8 *data,
1503                                    guint         len)
1504 {
1505   g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1506
1507   return array;
1508 }
1509
1510 /**
1511  * g_byte_array_set_size:
1512  * @array: a #GByteArray.
1513  * @length: the new size of the #GByteArray.
1514  * @Returns: the #GByteArray.
1515  *
1516  * Sets the size of the #GByteArray, expanding it if necessary.
1517  **/
1518 GByteArray* g_byte_array_set_size (GByteArray *array,
1519                                    guint       length)
1520 {
1521   g_array_set_size ((GArray*) array, length);
1522
1523   return array;
1524 }
1525
1526 /**
1527  * g_byte_array_remove_index:
1528  * @array: a #GByteArray.
1529  * @index_: the index of the byte to remove.
1530  * @Returns: the #GByteArray.
1531  *
1532  * Removes the byte at the given index from a #GByteArray. The
1533  * following bytes are moved down one place.
1534  **/
1535 GByteArray* g_byte_array_remove_index (GByteArray *array,
1536                                        guint       index_)
1537 {
1538   g_array_remove_index ((GArray*) array, index_);
1539
1540   return array;
1541 }
1542
1543 /**
1544  * g_byte_array_remove_index_fast:
1545  * @array: a #GByteArray.
1546  * @index_: the index of the byte to remove.
1547  * @Returns: the #GByteArray.
1548  *
1549  * Removes the byte at the given index from a #GByteArray. The last
1550  * element in the array is used to fill in the space, so this function
1551  * does not preserve the order of the #GByteArray. But it is faster
1552  * than g_byte_array_remove_index().
1553  **/
1554 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1555                                             guint       index_)
1556 {
1557   g_array_remove_index_fast ((GArray*) array, index_);
1558
1559   return array;
1560 }
1561
1562 /**
1563  * g_byte_array_remove_range:
1564  * @array: a @GByteArray.
1565  * @index_: the index of the first byte to remove.
1566  * @length: the number of bytes to remove.
1567  * @Returns: the #GByteArray.
1568  *
1569  * Removes the given number of bytes starting at the given index from a
1570  * #GByteArray.  The following elements are moved to close the gap.
1571  *
1572  * Since: 2.4
1573  **/
1574 GByteArray*
1575 g_byte_array_remove_range (GByteArray *array,
1576                            guint       index_,
1577                            guint       length)
1578 {
1579   g_return_val_if_fail (array, NULL);
1580   g_return_val_if_fail (index_ < array->len, NULL);
1581   g_return_val_if_fail (index_ + length <= array->len, NULL);
1582
1583   return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1584 }
1585
1586 /**
1587  * g_byte_array_sort:
1588  * @array: a #GByteArray.
1589  * @compare_func: comparison function.
1590  *
1591  * Sorts a byte array, using @compare_func which should be a
1592  * qsort()-style comparison function (returns less than zero for first
1593  * arg is less than second arg, zero for equal, greater than zero if
1594  * first arg is greater than second arg).
1595  *
1596  * If two array elements compare equal, their order in the sorted array
1597  * is undefined. If you want equal elements to keep their order &#8211; i.e.
1598  * you want a stable sort &#8211; you can write a comparison function that,
1599  * if two elements would otherwise compare equal, compares them by
1600  * their addresses.
1601  **/
1602 void
1603 g_byte_array_sort (GByteArray   *array,
1604                    GCompareFunc  compare_func)
1605 {
1606   g_array_sort ((GArray *) array, compare_func);
1607 }
1608
1609 /**
1610  * g_byte_array_sort_with_data:
1611  * @array: a #GByteArray.
1612  * @compare_func: comparison function.
1613  * @user_data: data to pass to @compare_func.
1614  *
1615  * Like g_byte_array_sort(), but the comparison function takes an extra
1616  * user data argument.
1617  **/
1618 void
1619 g_byte_array_sort_with_data (GByteArray       *array,
1620                              GCompareDataFunc  compare_func,
1621                              gpointer          user_data)
1622 {
1623   g_array_sort_with_data ((GArray *) array, compare_func, user_data);
1624 }