GSettings docs: clarify what is a good path
[glib.git] / glib / garray.c
blob0cb3713a5a454fc668e6f28a5266dd276277e377
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 /*
28 * MT safe
31 #include "config.h"
33 #include <string.h>
34 #include <stdlib.h>
36 #include "garray.h"
38 #include "gbytes.h"
39 #include "gslice.h"
40 #include "gmem.h"
41 #include "gtestutils.h"
42 #include "gthread.h"
43 #include "gmessages.h"
44 #include "gqsort.h"
47 /**
48 * SECTION:arrays
49 * @title: Arrays
50 * @short_description: arrays of arbitrary elements which grow
51 * automatically as elements are added
53 * Arrays are similar to standard C arrays, except that they grow
54 * automatically as elements are added.
56 * Array elements can be of any size (though all elements of one array
57 * are the same size), and the array can be automatically cleared to
58 * '0's and zero-terminated.
60 * To create a new array use g_array_new().
62 * To add elements to an array, use g_array_append_val(),
63 * g_array_append_vals(), g_array_prepend_val(), and
64 * g_array_prepend_vals().
66 * To access an element of an array, use g_array_index().
68 * To set the size of an array, use g_array_set_size().
70 * To free an array, use g_array_free().
72 * <example>
73 * <title>Using a #GArray to store #gint values</title>
74 * <programlisting>
75 * GArray *garray;
76 * gint i;
77 * /<!-- -->* We create a new array to store gint values.
78 * We don't want it zero-terminated or cleared to 0's. *<!-- -->/
79 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
80 * for (i = 0; i &lt; 10000; i++)
81 * g_array_append_val (garray, i);
82 * for (i = 0; i &lt; 10000; i++)
83 * if (g_array_index (garray, gint, i) != i)
84 * g_print ("ERROR: got &percnt;d instead of &percnt;d\n",
85 * g_array_index (garray, gint, i), i);
86 * g_array_free (garray, TRUE);
87 * </programlisting>
88 * </example>
89 **/
91 #define MIN_ARRAY_SIZE 16
93 typedef struct _GRealArray GRealArray;
95 /**
96 * GArray:
97 * @data: a pointer to the element data. The data may be moved as
98 * elements are added to the #GArray.
99 * @len: the number of elements in the #GArray not including the
100 * possible terminating zero element.
102 * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
104 struct _GRealArray
106 guint8 *data;
107 guint len;
108 guint alloc;
109 guint elt_size;
110 guint zero_terminated : 1;
111 guint clear : 1;
112 gint ref_count;
113 GDestroyNotify clear_func;
117 * g_array_index:
118 * @a: a #GArray.
119 * @t: the type of the elements.
120 * @i: the index of the element to return.
121 * @Returns: the element of the #GArray at the index given by @i.
123 * Returns the element of a #GArray at the given index. The return
124 * value is cast to the given type.
126 * <example>
127 * <title>Getting a pointer to an element in a #GArray</title>
128 * <programlisting>
129 * EDayViewEvent *event;
130 * /<!-- -->* This gets a pointer to the 4th element
131 * in the array of EDayViewEvent structs. *<!-- -->/
132 * event = &amp;g_array_index (events, EDayViewEvent, 3);
133 * </programlisting>
134 * </example>
137 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
138 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
139 #define g_array_elt_zero(array, pos, len) \
140 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
141 #define g_array_zero_terminate(array) G_STMT_START{ \
142 if ((array)->zero_terminated) \
143 g_array_elt_zero ((array), (array)->len, 1); \
144 }G_STMT_END
146 static guint g_nearest_pow (gint num) G_GNUC_CONST;
147 static void g_array_maybe_expand (GRealArray *array,
148 gint len);
151 * g_array_new:
152 * @zero_terminated: %TRUE if the array should have an extra element at
153 * the end which is set to 0.
154 * @clear_: %TRUE if #GArray elements should be automatically cleared
155 * to 0 when they are allocated.
156 * @element_size: the size of each element in bytes.
157 * @Returns: the new #GArray.
159 * Creates a new #GArray with a reference count of 1.
161 GArray*
162 g_array_new (gboolean zero_terminated,
163 gboolean clear,
164 guint elt_size)
166 g_return_val_if_fail (elt_size > 0, NULL);
168 return g_array_sized_new (zero_terminated, clear, elt_size, 0);
172 * g_array_sized_new:
173 * @zero_terminated: %TRUE if the array should have an extra element at
174 * the end with all bits cleared.
175 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
176 * allocation.
177 * @element_size: size of each element in the array.
178 * @reserved_size: number of elements preallocated.
179 * @Returns: the new #GArray.
181 * Creates a new #GArray with @reserved_size elements preallocated and
182 * a reference count of 1. This avoids frequent reallocation, if you
183 * are going to add many elements to the array. Note however that the
184 * size of the array is still 0.
186 GArray* g_array_sized_new (gboolean zero_terminated,
187 gboolean clear,
188 guint elt_size,
189 guint reserved_size)
191 GRealArray *array;
193 g_return_val_if_fail (elt_size > 0, NULL);
195 array = g_slice_new (GRealArray);
197 array->data = NULL;
198 array->len = 0;
199 array->alloc = 0;
200 array->zero_terminated = (zero_terminated ? 1 : 0);
201 array->clear = (clear ? 1 : 0);
202 array->elt_size = elt_size;
203 array->ref_count = 1;
204 array->clear_func = NULL;
206 if (array->zero_terminated || reserved_size != 0)
208 g_array_maybe_expand (array, reserved_size);
209 g_array_zero_terminate(array);
212 return (GArray*) array;
216 * g_array_set_clear_func:
217 * @array: A #GArray
218 * @clear_func: a function to clear an element of @array
220 * Sets a function to clear an element of @array.
222 * The @clear_func will be called when an element in the array
223 * data segment is removed and when the array is freed and data
224 * segment is deallocated as well.
226 * Note that in contrast with other uses of #GDestroyNotify
227 * functions, @clear_func is expected to clear the contents of
228 * the array element it is given, but not free the element itself.
230 * Since: 2.32
232 void
233 g_array_set_clear_func (GArray *array,
234 GDestroyNotify clear_func)
236 GRealArray *rarray = (GRealArray *) array;
238 g_return_if_fail (array != NULL);
240 rarray->clear_func = clear_func;
244 * g_array_ref:
245 * @array: A #GArray.
247 * Atomically increments the reference count of @array by one. This
248 * function is MT-safe and may be called from any thread.
250 * Returns: The passed in #GArray.
252 * Since: 2.22
254 GArray *
255 g_array_ref (GArray *array)
257 GRealArray *rarray = (GRealArray*) array;
258 g_return_val_if_fail (array, NULL);
260 g_atomic_int_inc (&rarray->ref_count);
262 return array;
265 typedef enum
267 FREE_SEGMENT = 1 << 0,
268 PRESERVE_WRAPPER = 1 << 1
269 } ArrayFreeFlags;
271 static gchar *array_free (GRealArray *, ArrayFreeFlags);
274 * g_array_unref:
275 * @array: A #GArray.
277 * Atomically decrements the reference count of @array by one. If the
278 * reference count drops to 0, all memory allocated by the array is
279 * released. This function is MT-safe and may be called from any
280 * thread.
282 * Since: 2.22
284 void
285 g_array_unref (GArray *array)
287 GRealArray *rarray = (GRealArray*) array;
288 g_return_if_fail (array);
290 if (g_atomic_int_dec_and_test (&rarray->ref_count))
291 array_free (rarray, FREE_SEGMENT);
295 * g_array_get_element_size:
296 * @array: A #GArray.
298 * Gets the size of the elements in @array.
300 * Returns: Size of each element, in bytes.
302 * Since: 2.22
304 guint
305 g_array_get_element_size (GArray *array)
307 GRealArray *rarray = (GRealArray*) array;
309 g_return_val_if_fail (array, 0);
311 return rarray->elt_size;
315 * g_array_free:
316 * @array: a #GArray.
317 * @free_segment: if %TRUE the actual element data is freed as well.
318 * @Returns: the element data if @free_segment is %FALSE, otherwise
319 * %NULL. The element data should be freed using g_free().
321 * Frees the memory allocated for the #GArray. If @free_segment is
322 * %TRUE it frees the memory block holding the elements as well and
323 * also each element if @array has a @element_free_func set. Pass
324 * %FALSE if you want to free the #GArray wrapper but preserve the
325 * underlying array for use elsewhere. If the reference count of @array
326 * is greater than one, the #GArray wrapper is preserved but the size
327 * of @array will be set to zero.
329 * <note><para>If array elements contain dynamically-allocated memory,
330 * they should be freed separately.</para></note>
332 gchar*
333 g_array_free (GArray *farray,
334 gboolean free_segment)
336 GRealArray *array = (GRealArray*) farray;
337 ArrayFreeFlags flags;
339 g_return_val_if_fail (array, NULL);
341 flags = (free_segment ? FREE_SEGMENT : 0);
343 /* if others are holding a reference, preserve the wrapper but do free/return the data */
344 if (!g_atomic_int_dec_and_test (&array->ref_count))
345 flags |= PRESERVE_WRAPPER;
347 return array_free (array, flags);
350 static gchar *
351 array_free (GRealArray *array,
352 ArrayFreeFlags flags)
354 gchar *segment;
356 if (flags & FREE_SEGMENT)
358 if (array->clear_func != NULL)
360 guint i;
362 for (i = 0; i < array->len; i++)
363 array->clear_func (g_array_elt_pos (array, i));
366 g_free (array->data);
367 segment = NULL;
369 else
370 segment = (gchar*) array->data;
372 if (flags & PRESERVE_WRAPPER)
374 array->data = NULL;
375 array->len = 0;
376 array->alloc = 0;
378 else
380 g_slice_free1 (sizeof (GRealArray), array);
383 return segment;
387 * g_array_append_vals:
388 * @array: a #GArray.
389 * @data: a pointer to the elements to append to the end of the array.
390 * @len: the number of elements to append.
391 * @Returns: the #GArray.
393 * Adds @len elements onto the end of the array.
396 * g_array_append_val:
397 * @a: a #GArray.
398 * @v: the value to append to the #GArray.
399 * @Returns: the #GArray.
401 * Adds the value on to the end of the array. The array will grow in
402 * size automatically if necessary.
404 * <note><para>g_array_append_val() is a macro which uses a reference
405 * to the value parameter @v. This means that you cannot use it with
406 * literal values such as "27". You must use variables.</para></note>
408 GArray*
409 g_array_append_vals (GArray *farray,
410 gconstpointer data,
411 guint len)
413 GRealArray *array = (GRealArray*) farray;
415 g_return_val_if_fail (array, NULL);
417 g_array_maybe_expand (array, len);
419 memcpy (g_array_elt_pos (array, array->len), data,
420 g_array_elt_len (array, len));
422 array->len += len;
424 g_array_zero_terminate (array);
426 return farray;
430 * g_array_prepend_vals:
431 * @array: a #GArray.
432 * @data: a pointer to the elements to prepend to the start of the
433 * array.
434 * @len: the number of elements to prepend.
435 * @Returns: the #GArray.
437 * Adds @len elements onto the start of the array.
439 * This operation is slower than g_array_append_vals() since the
440 * existing elements in the array have to be moved to make space for
441 * the new elements.
444 * g_array_prepend_val:
445 * @a: a #GArray.
446 * @v: the value to prepend to the #GArray.
447 * @Returns: the #GArray.
449 * Adds the value on to the start of the array. The array will grow in
450 * size automatically if necessary.
452 * This operation is slower than g_array_append_val() since the
453 * existing elements in the array have to be moved to make space for
454 * the new element.
456 * <note><para>g_array_prepend_val() is a macro which uses a reference
457 * to the value parameter @v. This means that you cannot use it with
458 * literal values such as "27". You must use variables.</para></note>
460 GArray*
461 g_array_prepend_vals (GArray *farray,
462 gconstpointer data,
463 guint len)
465 GRealArray *array = (GRealArray*) farray;
467 g_return_val_if_fail (array, NULL);
469 g_array_maybe_expand (array, len);
471 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
472 g_array_elt_len (array, array->len));
474 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
476 array->len += len;
478 g_array_zero_terminate (array);
480 return farray;
484 * g_array_insert_vals:
485 * @array: a #GArray.
486 * @index_: the index to place the elements at.
487 * @data: a pointer to the elements to insert.
488 * @len: the number of elements to insert.
489 * @Returns: the #GArray.
491 * Inserts @len elements into a #GArray at the given index.
494 * g_array_insert_val:
495 * @a: a #GArray.
496 * @i: the index to place the element at.
497 * @v: the value to insert into the array.
498 * @Returns: the #GArray.
500 * Inserts an element into an array at the given index.
502 * <note><para>g_array_insert_val() is a macro which uses a reference
503 * to the value parameter @v. This means that you cannot use it with
504 * literal values such as "27". You must use variables.</para></note>
506 GArray*
507 g_array_insert_vals (GArray *farray,
508 guint index_,
509 gconstpointer data,
510 guint len)
512 GRealArray *array = (GRealArray*) farray;
514 g_return_val_if_fail (array, NULL);
516 g_array_maybe_expand (array, len);
518 g_memmove (g_array_elt_pos (array, len + index_),
519 g_array_elt_pos (array, index_),
520 g_array_elt_len (array, array->len - index_));
522 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
524 array->len += len;
526 g_array_zero_terminate (array);
528 return farray;
532 * g_array_set_size:
533 * @array: a #GArray.
534 * @length: the new size of the #GArray.
535 * @Returns: the #GArray.
537 * Sets the size of the array, expanding it if necessary. If the array
538 * was created with @clear_ set to %TRUE, the new elements are set to 0.
540 GArray*
541 g_array_set_size (GArray *farray,
542 guint length)
544 GRealArray *array = (GRealArray*) farray;
546 g_return_val_if_fail (array, NULL);
548 if (length > array->len)
550 g_array_maybe_expand (array, length - array->len);
552 if (array->clear)
553 g_array_elt_zero (array, array->len, length - array->len);
555 else if (length < array->len)
556 g_array_remove_range (farray, length, array->len - length);
558 array->len = length;
560 g_array_zero_terminate (array);
562 return farray;
566 * g_array_remove_index:
567 * @array: a #GArray.
568 * @index_: the index of the element to remove.
569 * @Returns: the #GArray.
571 * Removes the element at the given index from a #GArray. The following
572 * elements are moved down one place.
574 GArray*
575 g_array_remove_index (GArray *farray,
576 guint index_)
578 GRealArray* array = (GRealArray*) farray;
580 g_return_val_if_fail (array, NULL);
582 g_return_val_if_fail (index_ < array->len, NULL);
584 if (array->clear_func != NULL)
585 array->clear_func (g_array_elt_pos (array, index_));
587 if (index_ != array->len - 1)
588 g_memmove (g_array_elt_pos (array, index_),
589 g_array_elt_pos (array, index_ + 1),
590 g_array_elt_len (array, array->len - index_ - 1));
592 array->len -= 1;
594 if (G_UNLIKELY (g_mem_gc_friendly))
595 g_array_elt_zero (array, array->len, 1);
596 else
597 g_array_zero_terminate (array);
599 return farray;
603 * g_array_remove_index_fast:
604 * @array: a @GArray.
605 * @index_: the index of the element to remove.
606 * @Returns: the #GArray.
608 * Removes the element at the given index from a #GArray. The last
609 * element in the array is used to fill in the space, so this function
610 * does not preserve the order of the #GArray. But it is faster than
611 * g_array_remove_index().
613 GArray*
614 g_array_remove_index_fast (GArray *farray,
615 guint index_)
617 GRealArray* array = (GRealArray*) farray;
619 g_return_val_if_fail (array, NULL);
621 g_return_val_if_fail (index_ < array->len, NULL);
623 if (array->clear_func != NULL)
624 array->clear_func (g_array_elt_pos (array, index_));
626 if (index_ != array->len - 1)
627 memcpy (g_array_elt_pos (array, index_),
628 g_array_elt_pos (array, array->len - 1),
629 g_array_elt_len (array, 1));
631 array->len -= 1;
633 if (G_UNLIKELY (g_mem_gc_friendly))
634 g_array_elt_zero (array, array->len, 1);
635 else
636 g_array_zero_terminate (array);
638 return farray;
642 * g_array_remove_range:
643 * @array: a @GArray.
644 * @index_: the index of the first element to remove.
645 * @length: the number of elements to remove.
646 * @Returns: the #GArray.
648 * Removes the given number of elements starting at the given index
649 * from a #GArray. The following elements are moved to close the gap.
651 * Since: 2.4
653 GArray*
654 g_array_remove_range (GArray *farray,
655 guint index_,
656 guint length)
658 GRealArray *array = (GRealArray*) farray;
660 g_return_val_if_fail (array, NULL);
661 g_return_val_if_fail (index_ < array->len, NULL);
662 g_return_val_if_fail (index_ + length <= array->len, NULL);
664 if (array->clear_func != NULL)
666 guint i;
668 for (i = 0; i < length; i++)
669 array->clear_func (g_array_elt_pos (array, index_ + i));
672 if (index_ + length != array->len)
673 g_memmove (g_array_elt_pos (array, index_),
674 g_array_elt_pos (array, index_ + length),
675 (array->len - (index_ + length)) * array->elt_size);
677 array->len -= length;
678 if (G_UNLIKELY (g_mem_gc_friendly))
679 g_array_elt_zero (array, array->len, length);
680 else
681 g_array_zero_terminate (array);
683 return farray;
687 * g_array_sort:
688 * @array: a #GArray.
689 * @compare_func: comparison function.
691 * Sorts a #GArray using @compare_func which should be a qsort()-style
692 * comparison function (returns less than zero for first arg is less
693 * than second arg, zero for equal, greater zero if first arg is
694 * greater than second arg).
696 * This is guaranteed to be a stable sort since version 2.32.
698 void
699 g_array_sort (GArray *farray,
700 GCompareFunc compare_func)
702 GRealArray *array = (GRealArray*) farray;
704 g_return_if_fail (array != NULL);
706 /* Don't use qsort as we want a guaranteed stable sort */
707 g_qsort_with_data (array->data,
708 array->len,
709 array->elt_size,
710 (GCompareDataFunc)compare_func,
711 NULL);
715 * g_array_sort_with_data:
716 * @array: a #GArray.
717 * @compare_func: comparison function.
718 * @user_data: data to pass to @compare_func.
720 * Like g_array_sort(), but the comparison function receives an extra
721 * user data argument.
723 * This is guaranteed to be a stable sort since version 2.32.
725 * There used to be a comment here about making the sort stable by
726 * using the addresses of the elements in the comparison function.
727 * This did not actually work, so any such code should be removed.
729 void
730 g_array_sort_with_data (GArray *farray,
731 GCompareDataFunc compare_func,
732 gpointer user_data)
734 GRealArray *array = (GRealArray*) farray;
736 g_return_if_fail (array != NULL);
738 g_qsort_with_data (array->data,
739 array->len,
740 array->elt_size,
741 compare_func,
742 user_data);
745 /* Returns the smallest power of 2 greater than n, or n if
746 * such power does not fit in a guint
748 static guint
749 g_nearest_pow (gint num)
751 guint n = 1;
753 while (n < num && n > 0)
754 n <<= 1;
756 return n ? n : num;
759 static void
760 g_array_maybe_expand (GRealArray *array,
761 gint len)
763 guint want_alloc = g_array_elt_len (array, array->len + len +
764 array->zero_terminated);
766 if (want_alloc > array->alloc)
768 want_alloc = g_nearest_pow (want_alloc);
769 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
771 array->data = g_realloc (array->data, want_alloc);
773 if (G_UNLIKELY (g_mem_gc_friendly))
774 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
776 array->alloc = want_alloc;
781 * SECTION:arrays_pointer
782 * @title: Pointer Arrays
783 * @short_description: arrays of pointers to any type of data, which
784 * grow automatically as new elements are added
786 * Pointer Arrays are similar to Arrays but are used only for storing
787 * pointers.
789 * <note><para>If you remove elements from the array, elements at the
790 * end of the array are moved into the space previously occupied by the
791 * removed element. This means that you should not rely on the index of
792 * particular elements remaining the same. You should also be careful
793 * when deleting elements while iterating over the array.</para></note>
795 * To create a pointer array, use g_ptr_array_new().
797 * To add elements to a pointer array, use g_ptr_array_add().
799 * To remove elements from a pointer array, use g_ptr_array_remove(),
800 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
802 * To access an element of a pointer array, use g_ptr_array_index().
804 * To set the size of a pointer array, use g_ptr_array_set_size().
806 * To free a pointer array, use g_ptr_array_free().
808 * <example>
809 * <title>Using a #GPtrArray</title>
810 * <programlisting>
811 * GPtrArray *gparray;
812 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
814 * gparray = g_ptr_array_new (<!-- -->);
815 * g_ptr_array_add (gparray, (gpointer) string1);
816 * g_ptr_array_add (gparray, (gpointer) string2);
817 * g_ptr_array_add (gparray, (gpointer) string3);
819 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
820 * g_print ("ERROR: got &percnt;p instead of &percnt;p\n",
821 * g_ptr_array_index (gparray, 0), string1);
823 * g_ptr_array_free (gparray, TRUE);
824 * </programlisting>
825 * </example>
828 typedef struct _GRealPtrArray GRealPtrArray;
831 * GPtrArray:
832 * @pdata: points to the array of pointers, which may be moved when the
833 * array grows.
834 * @len: number of pointers in the array.
836 * Contains the public fields of a pointer array.
838 struct _GRealPtrArray
840 gpointer *pdata;
841 guint len;
842 guint alloc;
843 gint ref_count;
844 GDestroyNotify element_free_func;
848 * g_ptr_array_index:
849 * @array: a #GPtrArray.
850 * @index_: the index of the pointer to return.
851 * @Returns: the pointer at the given index.
853 * Returns the pointer at the given index of the pointer array.
856 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
857 gint len);
860 * g_ptr_array_new:
861 * @Returns: the new #GPtrArray.
863 * Creates a new #GPtrArray with a reference count of 1.
865 GPtrArray*
866 g_ptr_array_new (void)
868 return g_ptr_array_sized_new (0);
872 * g_ptr_array_sized_new:
873 * @reserved_size: number of pointers preallocated.
874 * @Returns: the new #GPtrArray.
876 * Creates a new #GPtrArray with @reserved_size pointers preallocated
877 * and a reference count of 1. This avoids frequent reallocation, if
878 * you are going to add many pointers to the array. Note however that
879 * the size of the array is still 0.
881 GPtrArray*
882 g_ptr_array_sized_new (guint reserved_size)
884 GRealPtrArray *array = g_slice_new (GRealPtrArray);
886 array->pdata = NULL;
887 array->len = 0;
888 array->alloc = 0;
889 array->ref_count = 1;
890 array->element_free_func = NULL;
892 if (reserved_size != 0)
893 g_ptr_array_maybe_expand (array, reserved_size);
895 return (GPtrArray*) array;
899 * g_ptr_array_new_with_free_func:
900 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
902 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
903 * for freeing each element when the array is destroyed either via
904 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
905 * set to %TRUE or when removing elements.
907 * Returns: A new #GPtrArray.
909 * Since: 2.22
911 GPtrArray *
912 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
914 GPtrArray *array;
916 array = g_ptr_array_new ();
917 g_ptr_array_set_free_func (array, element_free_func);
918 return array;
922 * g_ptr_array_new_full:
923 * @reserved_size: number of pointers preallocated.
924 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
926 * Creates a new #GPtrArray with @reserved_size pointers preallocated
927 * and a reference count of 1. This avoids frequent reallocation, if
928 * you are going to add many pointers to the array. Note however that
929 * the size of the array is still 0. It also set @element_free_func
930 * for freeing each element when the array is destroyed either via
931 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
932 * set to %TRUE or when removing elements.
934 * Returns: A new #GPtrArray.
936 * Since: 2.30
938 GPtrArray *
939 g_ptr_array_new_full (guint reserved_size,
940 GDestroyNotify element_free_func)
942 GPtrArray *array;
944 array = g_ptr_array_sized_new (reserved_size);
945 g_ptr_array_set_free_func (array, element_free_func);
946 return array;
950 * g_ptr_array_set_free_func:
951 * @array: A #GPtrArray.
952 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
954 * Sets a function for freeing each element when @array is destroyed
955 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
956 * with @free_segment set to %TRUE or when removing elements.
958 * Since: 2.22
960 void
961 g_ptr_array_set_free_func (GPtrArray *array,
962 GDestroyNotify element_free_func)
964 GRealPtrArray* rarray = (GRealPtrArray*) array;
966 g_return_if_fail (array);
968 rarray->element_free_func = element_free_func;
972 * g_ptr_array_ref:
973 * @array: A #GArray.
975 * Atomically increments the reference count of @array by one. This
976 * function is MT-safe and may be called from any thread.
978 * Returns: The passed in #GPtrArray.
980 * Since: 2.22
982 GPtrArray *
983 g_ptr_array_ref (GPtrArray *array)
985 GRealPtrArray *rarray = (GRealPtrArray*) array;
987 g_return_val_if_fail (array, NULL);
989 g_atomic_int_inc (&rarray->ref_count);
991 return array;
994 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
997 * g_ptr_array_unref:
998 * @array: A #GPtrArray.
1000 * Atomically decrements the reference count of @array by one. If the
1001 * reference count drops to 0, the effect is the same as calling
1002 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1003 * is MT-safe and may be called from any thread.
1005 * Since: 2.22
1007 void
1008 g_ptr_array_unref (GPtrArray *array)
1010 GRealPtrArray *rarray = (GRealPtrArray*) array;
1011 g_return_if_fail (array);
1013 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1014 ptr_array_free (array, FREE_SEGMENT);
1018 * g_ptr_array_free:
1019 * @array: a #GPtrArray.
1020 * @free_seg: if %TRUE the actual pointer array is freed as well.
1021 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1022 * The pointer array should be freed using g_free().
1024 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1025 * it frees the memory block holding the elements as well. Pass %FALSE
1026 * if you want to free the #GPtrArray wrapper but preserve the
1027 * underlying array for use elsewhere. If the reference count of @array
1028 * is greater than one, the #GPtrArray wrapper is preserved but the
1029 * size of @array will be set to zero.
1031 * <note><para>If array contents point to dynamically-allocated
1032 * memory, they should be freed separately if @free_seg is %TRUE and no
1033 * #GDestroyNotify function has been set for @array.</para></note>
1035 gpointer*
1036 g_ptr_array_free (GPtrArray *farray,
1037 gboolean free_segment)
1039 GRealPtrArray *array = (GRealPtrArray*) farray;
1040 ArrayFreeFlags flags;
1042 g_return_val_if_fail (array, NULL);
1044 flags = (free_segment ? FREE_SEGMENT : 0);
1046 /* if others are holding a reference, preserve the wrapper but do free/return the data */
1047 if (!g_atomic_int_dec_and_test (&array->ref_count))
1048 flags |= PRESERVE_WRAPPER;
1050 return ptr_array_free (farray, flags);
1053 static gpointer *
1054 ptr_array_free (GPtrArray *farray,
1055 ArrayFreeFlags flags)
1057 GRealPtrArray *array = (GRealPtrArray*) farray;
1058 gpointer *segment;
1060 if (flags & FREE_SEGMENT)
1062 if (array->element_free_func != NULL)
1063 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1064 g_free (array->pdata);
1065 segment = NULL;
1067 else
1068 segment = array->pdata;
1070 if (flags & PRESERVE_WRAPPER)
1072 array->pdata = NULL;
1073 array->len = 0;
1074 array->alloc = 0;
1076 else
1078 g_slice_free1 (sizeof (GRealPtrArray), array);
1081 return segment;
1084 static void
1085 g_ptr_array_maybe_expand (GRealPtrArray *array,
1086 gint len)
1088 if ((array->len + len) > array->alloc)
1090 guint old_alloc = array->alloc;
1091 array->alloc = g_nearest_pow (array->len + len);
1092 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1093 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1094 if (G_UNLIKELY (g_mem_gc_friendly))
1095 for ( ; old_alloc < array->alloc; old_alloc++)
1096 array->pdata [old_alloc] = NULL;
1101 * g_ptr_array_set_size:
1102 * @array: a #GPtrArray.
1103 * @length: the new length of the pointer array.
1105 * Sets the size of the array. When making the array larger,
1106 * newly-added elements will be set to %NULL. When making it smaller,
1107 * if @array has a non-%NULL #GDestroyNotify function then it will be
1108 * called for the removed elements.
1110 void
1111 g_ptr_array_set_size (GPtrArray *farray,
1112 gint length)
1114 GRealPtrArray* array = (GRealPtrArray*) farray;
1116 g_return_if_fail (array);
1118 if (length > array->len)
1120 int i;
1121 g_ptr_array_maybe_expand (array, (length - array->len));
1122 /* This is not
1123 * memset (array->pdata + array->len, 0,
1124 * sizeof (gpointer) * (length - array->len));
1125 * to make it really portable. Remember (void*)NULL needn't be
1126 * bitwise zero. It of course is silly not to use memset (..,0,..).
1128 for (i = array->len; i < length; i++)
1129 array->pdata[i] = NULL;
1131 else if (length < array->len)
1132 g_ptr_array_remove_range (farray, length, array->len - length);
1134 array->len = length;
1138 * g_ptr_array_remove_index:
1139 * @array: a #GPtrArray.
1140 * @index_: the index of the pointer to remove.
1141 * @Returns: the pointer which was removed.
1143 * Removes the pointer at the given index from the pointer array. The
1144 * following elements are moved down one place. If @array has a
1145 * non-%NULL #GDestroyNotify function it is called for the removed
1146 * element.
1148 gpointer
1149 g_ptr_array_remove_index (GPtrArray *farray,
1150 guint index_)
1152 GRealPtrArray* array = (GRealPtrArray*) farray;
1153 gpointer result;
1155 g_return_val_if_fail (array, NULL);
1157 g_return_val_if_fail (index_ < array->len, NULL);
1159 result = array->pdata[index_];
1161 if (array->element_free_func != NULL)
1162 array->element_free_func (array->pdata[index_]);
1164 if (index_ != array->len - 1)
1165 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1166 sizeof (gpointer) * (array->len - index_ - 1));
1168 array->len -= 1;
1170 if (G_UNLIKELY (g_mem_gc_friendly))
1171 array->pdata[array->len] = NULL;
1173 return result;
1177 * g_ptr_array_remove_index_fast:
1178 * @array: a #GPtrArray.
1179 * @index_: the index of the pointer to remove.
1180 * @Returns: the pointer which was removed.
1182 * Removes the pointer at the given index from the pointer array. The
1183 * last element in the array is used to fill in the space, so this
1184 * function does not preserve the order of the array. But it is faster
1185 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1186 * #GDestroyNotify function it is called for the removed element.
1188 gpointer
1189 g_ptr_array_remove_index_fast (GPtrArray *farray,
1190 guint index_)
1192 GRealPtrArray* array = (GRealPtrArray*) farray;
1193 gpointer result;
1195 g_return_val_if_fail (array, NULL);
1197 g_return_val_if_fail (index_ < array->len, NULL);
1199 result = array->pdata[index_];
1201 if (array->element_free_func != NULL)
1202 array->element_free_func (array->pdata[index_]);
1204 if (index_ != array->len - 1)
1205 array->pdata[index_] = array->pdata[array->len - 1];
1207 array->len -= 1;
1209 if (G_UNLIKELY (g_mem_gc_friendly))
1210 array->pdata[array->len] = NULL;
1212 return result;
1216 * g_ptr_array_remove_range:
1217 * @array: a @GPtrArray.
1218 * @index_: the index of the first pointer to remove.
1219 * @length: the number of pointers to remove.
1221 * Removes the given number of pointers starting at the given index
1222 * from a #GPtrArray. The following elements are moved to close the
1223 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1224 * for the removed elements.
1226 * Since: 2.4
1228 void
1229 g_ptr_array_remove_range (GPtrArray *farray,
1230 guint index_,
1231 guint length)
1233 GRealPtrArray* array = (GRealPtrArray*) farray;
1234 guint n;
1236 g_return_if_fail (array);
1237 g_return_if_fail (index_ < array->len);
1238 g_return_if_fail (index_ + length <= array->len);
1240 if (array->element_free_func != NULL)
1242 for (n = index_; n < index_ + length; n++)
1243 array->element_free_func (array->pdata[n]);
1246 if (index_ + length != array->len)
1248 g_memmove (&array->pdata[index_],
1249 &array->pdata[index_ + length],
1250 (array->len - (index_ + length)) * sizeof (gpointer));
1253 array->len -= length;
1254 if (G_UNLIKELY (g_mem_gc_friendly))
1256 guint i;
1257 for (i = 0; i < length; i++)
1258 array->pdata[array->len + i] = NULL;
1263 * g_ptr_array_remove:
1264 * @array: a #GPtrArray.
1265 * @data: the pointer to remove.
1266 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1267 * not found in the array.
1269 * Removes the first occurrence of the given pointer from the pointer
1270 * array. The following elements are moved down one place. If @array
1271 * has a non-%NULL #GDestroyNotify function it is called for the
1272 * removed element.
1274 * It returns %TRUE if the pointer was removed, or %FALSE if the
1275 * pointer was not found.
1277 gboolean
1278 g_ptr_array_remove (GPtrArray *farray,
1279 gpointer data)
1281 GRealPtrArray* array = (GRealPtrArray*) farray;
1282 guint i;
1284 g_return_val_if_fail (array, FALSE);
1286 for (i = 0; i < array->len; i += 1)
1288 if (array->pdata[i] == data)
1290 g_ptr_array_remove_index (farray, i);
1291 return TRUE;
1295 return FALSE;
1299 * g_ptr_array_remove_fast:
1300 * @array: a #GPtrArray.
1301 * @data: the pointer to remove.
1302 * @Returns: %TRUE if the pointer was found in the array.
1304 * Removes the first occurrence of the given pointer from the pointer
1305 * array. The last element in the array is used to fill in the space,
1306 * so this function does not preserve the order of the array. But it is
1307 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1308 * #GDestroyNotify function it is called for the removed element.
1310 * It returns %TRUE if the pointer was removed, or %FALSE if the
1311 * pointer was not found.
1313 gboolean
1314 g_ptr_array_remove_fast (GPtrArray *farray,
1315 gpointer data)
1317 GRealPtrArray* array = (GRealPtrArray*) farray;
1318 guint i;
1320 g_return_val_if_fail (array, FALSE);
1322 for (i = 0; i < array->len; i += 1)
1324 if (array->pdata[i] == data)
1326 g_ptr_array_remove_index_fast (farray, i);
1327 return TRUE;
1331 return FALSE;
1335 * g_ptr_array_add:
1336 * @array: a #GPtrArray.
1337 * @data: the pointer to add.
1339 * Adds a pointer to the end of the pointer array. The array will grow
1340 * in size automatically if necessary.
1342 void
1343 g_ptr_array_add (GPtrArray *farray,
1344 gpointer data)
1346 GRealPtrArray* array = (GRealPtrArray*) farray;
1348 g_return_if_fail (array);
1350 g_ptr_array_maybe_expand (array, 1);
1352 array->pdata[array->len++] = data;
1356 * g_ptr_array_sort:
1357 * @array: a #GPtrArray.
1358 * @compare_func: comparison function.
1360 * Sorts the array, using @compare_func which should be a qsort()-style
1361 * comparison function (returns less than zero for first arg is less
1362 * than second arg, zero for equal, greater than zero if irst arg is
1363 * greater than second arg).
1365 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1366 * take the pointers from the array as arguments, it takes pointers to
1367 * the pointers in the array.</para></note>
1369 * This is guaranteed to be a stable sort since version 2.32.
1371 void
1372 g_ptr_array_sort (GPtrArray *array,
1373 GCompareFunc compare_func)
1375 g_return_if_fail (array != NULL);
1377 /* Don't use qsort as we want a guaranteed stable sort */
1378 g_qsort_with_data (array->pdata,
1379 array->len,
1380 sizeof (gpointer),
1381 (GCompareDataFunc)compare_func,
1382 NULL);
1386 * g_ptr_array_sort_with_data:
1387 * @array: a #GPtrArray.
1388 * @compare_func: comparison function.
1389 * @user_data: data to pass to @compare_func.
1391 * Like g_ptr_array_sort(), but the comparison function has an extra
1392 * user data argument.
1394 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1395 * doesn't take the pointers from the array as arguments, it takes
1396 * pointers to the pointers in the array.</para></note>
1398 * This is guaranteed to be a stable sort since version 2.32.
1400 void
1401 g_ptr_array_sort_with_data (GPtrArray *array,
1402 GCompareDataFunc compare_func,
1403 gpointer user_data)
1405 g_return_if_fail (array != NULL);
1407 g_qsort_with_data (array->pdata,
1408 array->len,
1409 sizeof (gpointer),
1410 compare_func,
1411 user_data);
1415 * g_ptr_array_foreach:
1416 * @array: a #GPtrArray
1417 * @func: the function to call for each array element
1418 * @user_data: user data to pass to the function
1420 * Calls a function for each element of a #GPtrArray.
1422 * Since: 2.4
1424 void
1425 g_ptr_array_foreach (GPtrArray *array,
1426 GFunc func,
1427 gpointer user_data)
1429 guint i;
1431 g_return_if_fail (array);
1433 for (i = 0; i < array->len; i++)
1434 (*func) (array->pdata[i], user_data);
1438 * SECTION:arrays_byte
1439 * @title: Byte Arrays
1440 * @short_description: arrays of bytes
1442 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1443 * of bytes which grow automatically as elements are added.
1445 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1446 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1448 * To set the size of a #GByteArray, use g_byte_array_set_size().
1450 * To free a #GByteArray, use g_byte_array_free().
1452 * <example>
1453 * <title>Using a #GByteArray</title>
1454 * <programlisting>
1455 * GByteArray *gbarray;
1456 * gint i;
1458 * gbarray = g_byte_array_new (<!-- -->);
1459 * for (i = 0; i &lt; 10000; i++)
1460 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1462 * for (i = 0; i &lt; 10000; i++)
1464 * g_assert (gbarray->data[4*i] == 'a');
1465 * g_assert (gbarray->data[4*i+1] == 'b');
1466 * g_assert (gbarray->data[4*i+2] == 'c');
1467 * g_assert (gbarray->data[4*i+3] == 'd');
1470 * g_byte_array_free (gbarray, TRUE);
1471 * </programlisting>
1472 * </example>
1474 * See #GBytes if you are interested in an immutable object representing a
1475 * sequence of bytes.
1479 * GByteArray:
1480 * @data: a pointer to the element data. The data may be moved as
1481 * elements are added to the #GByteArray.
1482 * @len: the number of elements in the #GByteArray.
1484 * The <structname>GByteArray</structname> struct allows access to the
1485 * public fields of a <structname>GByteArray</structname>.
1489 * g_byte_array_new:
1490 * @Returns: the new #GByteArray.
1492 * Creates a new #GByteArray with a reference count of 1.
1494 GByteArray* g_byte_array_new (void)
1496 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1500 * g_byte_array_new_take:
1501 * @data: (array length=len): byte data for the array
1502 * @len: length of @data
1504 * Create byte array containing the data. The data will be owned by the array
1505 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1507 * Since: 2.32
1509 * Returns: (transfer full): a new #GByteArray
1511 GByteArray *
1512 g_byte_array_new_take (guint8 *data,
1513 gsize len)
1515 GByteArray *array;
1516 GRealArray *real;
1518 array = g_byte_array_new ();
1519 real = (GRealArray *)array;
1520 g_assert (real->data == NULL);
1521 g_assert (real->len == 0);
1523 real->data = data;
1524 real->len = len;
1526 return array;
1530 * g_byte_array_sized_new:
1531 * @reserved_size: number of bytes preallocated.
1532 * @Returns: the new #GByteArray.
1534 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1535 * This avoids frequent reallocation, if you are going to add many
1536 * bytes to the array. Note however that the size of the array is still
1537 * 0.
1539 GByteArray* g_byte_array_sized_new (guint reserved_size)
1541 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1545 * g_byte_array_free:
1546 * @array: a #GByteArray.
1547 * @free_segment: if %TRUE the actual byte data is freed as well.
1548 * @Returns: the element data if @free_segment is %FALSE, otherwise
1549 * %NULL. The element data should be freed using g_free().
1551 * Frees the memory allocated by the #GByteArray. If @free_segment is
1552 * %TRUE it frees the actual byte data. If the reference count of
1553 * @array is greater than one, the #GByteArray wrapper is preserved but
1554 * the size of @array will be set to zero.
1556 guint8* g_byte_array_free (GByteArray *array,
1557 gboolean free_segment)
1559 return (guint8*) g_array_free ((GArray*) array, free_segment);
1563 * g_byte_array_free_to_bytes:
1564 * @array: (transfer full): a #GByteArray
1566 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1568 * The #GByteArray is freed unless the reference count of @array is greater
1569 * than one, the #GByteArray wrapper is preserved but the size of @array
1570 * will be set to zero.
1572 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1573 * together.
1575 * Since: 2.32
1577 * Returns: (transfer full): a new immutable #GBytes representing same byte
1578 * data that was in the array
1580 GBytes *
1581 g_byte_array_free_to_bytes (GByteArray *array)
1583 gsize length;
1585 g_return_val_if_fail (array != NULL, NULL);
1587 length = array->len;
1588 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1592 * g_byte_array_ref:
1593 * @array: A #GByteArray.
1595 * Atomically increments the reference count of @array by one. This
1596 * function is MT-safe and may be called from any thread.
1598 * Returns: The passed in #GByteArray.
1600 * Since: 2.22
1602 GByteArray *
1603 g_byte_array_ref (GByteArray *array)
1605 return (GByteArray *) g_array_ref ((GArray *) array);
1609 * g_byte_array_unref:
1610 * @array: A #GByteArray.
1612 * Atomically decrements the reference count of @array by one. If the
1613 * reference count drops to 0, all memory allocated by the array is
1614 * released. This function is MT-safe and may be called from any
1615 * thread.
1617 * Since: 2.22
1619 void
1620 g_byte_array_unref (GByteArray *array)
1622 g_array_unref ((GArray *) array);
1626 * g_byte_array_append:
1627 * @array: a #GByteArray.
1628 * @data: the byte data to be added.
1629 * @len: the number of bytes to add.
1630 * @Returns: the #GByteArray.
1632 * Adds the given bytes to the end of the #GByteArray. The array will
1633 * grow in size automatically if necessary.
1635 GByteArray* g_byte_array_append (GByteArray *array,
1636 const guint8 *data,
1637 guint len)
1639 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1641 return array;
1645 * g_byte_array_prepend:
1646 * @array: a #GByteArray.
1647 * @data: the byte data to be added.
1648 * @len: the number of bytes to add.
1649 * @Returns: the #GByteArray.
1651 * Adds the given data to the start of the #GByteArray. The array will
1652 * grow in size automatically if necessary.
1654 GByteArray* g_byte_array_prepend (GByteArray *array,
1655 const guint8 *data,
1656 guint len)
1658 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1660 return array;
1664 * g_byte_array_set_size:
1665 * @array: a #GByteArray.
1666 * @length: the new size of the #GByteArray.
1667 * @Returns: the #GByteArray.
1669 * Sets the size of the #GByteArray, expanding it if necessary.
1671 GByteArray* g_byte_array_set_size (GByteArray *array,
1672 guint length)
1674 g_array_set_size ((GArray*) array, length);
1676 return array;
1680 * g_byte_array_remove_index:
1681 * @array: a #GByteArray.
1682 * @index_: the index of the byte to remove.
1683 * @Returns: the #GByteArray.
1685 * Removes the byte at the given index from a #GByteArray. The
1686 * following bytes are moved down one place.
1688 GByteArray* g_byte_array_remove_index (GByteArray *array,
1689 guint index_)
1691 g_array_remove_index ((GArray*) array, index_);
1693 return array;
1697 * g_byte_array_remove_index_fast:
1698 * @array: a #GByteArray.
1699 * @index_: the index of the byte to remove.
1700 * @Returns: the #GByteArray.
1702 * Removes the byte at the given index from a #GByteArray. The last
1703 * element in the array is used to fill in the space, so this function
1704 * does not preserve the order of the #GByteArray. But it is faster
1705 * than g_byte_array_remove_index().
1707 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1708 guint index_)
1710 g_array_remove_index_fast ((GArray*) array, index_);
1712 return array;
1716 * g_byte_array_remove_range:
1717 * @array: a @GByteArray.
1718 * @index_: the index of the first byte to remove.
1719 * @length: the number of bytes to remove.
1720 * @Returns: the #GByteArray.
1722 * Removes the given number of bytes starting at the given index from a
1723 * #GByteArray. The following elements are moved to close the gap.
1725 * Since: 2.4
1727 GByteArray*
1728 g_byte_array_remove_range (GByteArray *array,
1729 guint index_,
1730 guint length)
1732 g_return_val_if_fail (array, NULL);
1733 g_return_val_if_fail (index_ < array->len, NULL);
1734 g_return_val_if_fail (index_ + length <= array->len, NULL);
1736 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1740 * g_byte_array_sort:
1741 * @array: a #GByteArray.
1742 * @compare_func: comparison function.
1744 * Sorts a byte array, using @compare_func which should be a
1745 * qsort()-style comparison function (returns less than zero for first
1746 * arg is less than second arg, zero for equal, greater than zero if
1747 * first arg is greater than second arg).
1749 * If two array elements compare equal, their order in the sorted array
1750 * is undefined. If you want equal elements to keep their order (i.e.
1751 * you want a stable sort) you can write a comparison function that,
1752 * if two elements would otherwise compare equal, compares them by
1753 * their addresses.
1755 void
1756 g_byte_array_sort (GByteArray *array,
1757 GCompareFunc compare_func)
1759 g_array_sort ((GArray *) array, compare_func);
1763 * g_byte_array_sort_with_data:
1764 * @array: a #GByteArray.
1765 * @compare_func: comparison function.
1766 * @user_data: data to pass to @compare_func.
1768 * Like g_byte_array_sort(), but the comparison function takes an extra
1769 * user data argument.
1771 void
1772 g_byte_array_sort_with_data (GByteArray *array,
1773 GCompareDataFunc compare_func,
1774 gpointer user_data)
1776 g_array_sort_with_data ((GArray *) array, compare_func, user_data);