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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
39 #include "gtestutils.h"
41 #include "gmessages.h"
48 * @short_description: arrays of arbitrary elements which grow
49 * automatically as elements are added
51 * Arrays are similar to standard C arrays, except that they grow
52 * automatically as elements are added.
54 * Array elements can be of any size (though all elements of one array
55 * are the same size), and the array can be automatically cleared to
56 * '0's and zero-terminated.
58 * To create a new array use g_array_new().
60 * To add elements to an array, use g_array_append_val(),
61 * g_array_append_vals(), g_array_prepend_val(), and
62 * g_array_prepend_vals().
64 * To access an element of an array, use g_array_index().
66 * To set the size of an array, use g_array_set_size().
68 * To free an array, use g_array_free().
71 * <title>Using a #GArray to store #gint values</title>
75 * /<!-- -->* We create a new array to store gint values.
76 * We don't want it zero-terminated or cleared to 0's. *<!-- -->/
77 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
78 * for (i = 0; i < 10000; i++)
79 * g_array_append_val (garray, i);
80 * for (i = 0; i < 10000; i++)
81 * if (g_array_index (garray, gint, i) != i)
82 * g_print ("ERROR: got %d instead of %d\n",
83 * g_array_index (garray, gint, i), i);
84 * g_array_free (garray, TRUE);
89 #define MIN_ARRAY_SIZE 16
91 typedef struct _GRealArray GRealArray;
95 * @data: a pointer to the element data. The data may be moved as
96 * elements are added to the #GArray.
97 * @len: the number of elements in the #GArray not including the
98 * possible terminating zero element.
100 * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
108 guint zero_terminated : 1;
111 GDestroyNotify clear_func;
117 * @t: the type of the elements.
118 * @i: the index of the element to return.
120 * Returns the element of a #GArray at the given index. The return
121 * value is cast to the given type.
124 * <title>Getting a pointer to an element in a #GArray</title>
126 * EDayViewEvent *event;
127 * /<!-- -->* This gets a pointer to the 4th element
128 * in the array of EDayViewEvent structs. *<!-- -->/
129 * event = &g_array_index (events, EDayViewEvent, 3);
133 * Returns: the element of the #GArray at the index given by @i.
136 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
137 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
138 #define g_array_elt_zero(array, pos, len) \
139 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
140 #define g_array_zero_terminate(array) G_STMT_START{ \
141 if ((array)->zero_terminated) \
142 g_array_elt_zero ((array), (array)->len, 1); \
145 static guint g_nearest_pow (gint num) G_GNUC_CONST;
146 static void g_array_maybe_expand (GRealArray *array,
151 * @zero_terminated: %TRUE if the array should have an extra element at
152 * the end which is set to 0.
153 * @clear_: %TRUE if #GArray elements should be automatically cleared
154 * to 0 when they are allocated.
155 * @element_size: the size of each element in bytes.
157 * Creates a new #GArray with a reference count of 1.
159 * Returns: the new #GArray.
162 g_array_new (gboolean zero_terminated,
166 g_return_val_if_fail (elt_size > 0, NULL);
168 return g_array_sized_new (zero_terminated, clear, elt_size, 0);
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
177 * @element_size: size of each element in the array.
178 * @reserved_size: number of elements preallocated.
180 * Creates a new #GArray with @reserved_size elements preallocated and
181 * a reference count of 1. This avoids frequent reallocation, if you
182 * are going to add many elements to the array. Note however that the
183 * size of the array is still 0.
185 * Returns: the new #GArray.
187 GArray* g_array_sized_new (gboolean zero_terminated,
194 g_return_val_if_fail (elt_size > 0, NULL);
196 array = g_slice_new (GRealArray);
201 array->zero_terminated = (zero_terminated ? 1 : 0);
202 array->clear = (clear ? 1 : 0);
203 array->elt_size = elt_size;
204 array->ref_count = 1;
205 array->clear_func = NULL;
207 if (array->zero_terminated || reserved_size != 0)
209 g_array_maybe_expand (array, reserved_size);
210 g_array_zero_terminate(array);
213 return (GArray*) array;
217 * g_array_set_clear_func:
219 * @clear_func: a function to clear an element of @array
221 * Sets a function to clear an element of @array.
223 * The @clear_func will be called when an element in the array
224 * data segment is removed and when the array is freed and data
225 * segment is deallocated as well.
227 * Note that in contrast with other uses of #GDestroyNotify
228 * functions, @clear_func is expected to clear the contents of
229 * the array element it is given, but not free the element itself.
234 g_array_set_clear_func (GArray *array,
235 GDestroyNotify clear_func)
237 GRealArray *rarray = (GRealArray *) array;
239 g_return_if_fail (array != NULL);
241 rarray->clear_func = clear_func;
248 * Atomically increments the reference count of @array by one. This
249 * function is MT-safe and may be called from any thread.
251 * Returns: The passed in #GArray.
256 g_array_ref (GArray *array)
258 GRealArray *rarray = (GRealArray*) array;
259 g_return_val_if_fail (array, NULL);
261 g_atomic_int_inc (&rarray->ref_count);
268 FREE_SEGMENT = 1 << 0,
269 PRESERVE_WRAPPER = 1 << 1
272 static gchar *array_free (GRealArray *, ArrayFreeFlags);
278 * Atomically decrements the reference count of @array by one. If the
279 * reference count drops to 0, all memory allocated by the array is
280 * released. This function is MT-safe and may be called from any
286 g_array_unref (GArray *array)
288 GRealArray *rarray = (GRealArray*) array;
289 g_return_if_fail (array);
291 if (g_atomic_int_dec_and_test (&rarray->ref_count))
292 array_free (rarray, FREE_SEGMENT);
296 * g_array_get_element_size:
299 * Gets the size of the elements in @array.
301 * Returns: Size of each element, in bytes.
306 g_array_get_element_size (GArray *array)
308 GRealArray *rarray = (GRealArray*) array;
310 g_return_val_if_fail (array, 0);
312 return rarray->elt_size;
318 * @free_segment: if %TRUE the actual element data is freed as well.
320 * Frees the memory allocated for the #GArray. If @free_segment is
321 * %TRUE it frees the memory block holding the elements as well and
322 * also each element if @array has a @element_free_func set. Pass
323 * %FALSE if you want to free the #GArray wrapper but preserve the
324 * underlying array for use elsewhere. If the reference count of @array
325 * is greater than one, the #GArray wrapper is preserved but the size
326 * of @array will be set to zero.
328 * <note><para>If array elements contain dynamically-allocated memory,
329 * they should be freed separately.</para></note>
331 * Returns: the element data if @free_segment is %FALSE, otherwise
332 * %NULL. The element data should be freed using g_free().
335 g_array_free (GArray *farray,
336 gboolean free_segment)
338 GRealArray *array = (GRealArray*) farray;
339 ArrayFreeFlags flags;
341 g_return_val_if_fail (array, NULL);
343 flags = (free_segment ? FREE_SEGMENT : 0);
345 /* if others are holding a reference, preserve the wrapper but do free/return the data */
346 if (!g_atomic_int_dec_and_test (&array->ref_count))
347 flags |= PRESERVE_WRAPPER;
349 return array_free (array, flags);
353 array_free (GRealArray *array,
354 ArrayFreeFlags flags)
358 if (flags & FREE_SEGMENT)
360 if (array->clear_func != NULL)
364 for (i = 0; i < array->len; i++)
365 array->clear_func (g_array_elt_pos (array, i));
368 g_free (array->data);
372 segment = (gchar*) array->data;
374 if (flags & PRESERVE_WRAPPER)
382 g_slice_free1 (sizeof (GRealArray), array);
389 * g_array_append_vals:
391 * @data: a pointer to the elements to append to the end of the array.
392 * @len: the number of elements to append.
394 * Adds @len elements onto the end of the array.
396 * Returns: the #GArray.
399 * g_array_append_val:
401 * @v: the value to append to the #GArray.
403 * Adds the value on to the end of the array. The array will grow in
404 * size automatically if necessary.
406 * <note><para>g_array_append_val() is a macro which uses a reference
407 * to the value parameter @v. This means that you cannot use it with
408 * literal values such as "27". You must use variables.</para></note>
410 * Returns: the #GArray.
413 g_array_append_vals (GArray *farray,
417 GRealArray *array = (GRealArray*) farray;
419 g_return_val_if_fail (array, NULL);
421 g_array_maybe_expand (array, len);
423 memcpy (g_array_elt_pos (array, array->len), data,
424 g_array_elt_len (array, len));
428 g_array_zero_terminate (array);
434 * g_array_prepend_vals:
436 * @data: a pointer to the elements to prepend to the start of the
438 * @len: the number of elements to prepend.
440 * Adds @len elements onto the start of the array.
442 * This operation is slower than g_array_append_vals() since the
443 * existing elements in the array have to be moved to make space for
446 * Returns: the #GArray.
449 * g_array_prepend_val:
451 * @v: the value to prepend to the #GArray.
453 * Adds the value on to the start of the array. The array will grow in
454 * size automatically if necessary.
456 * This operation is slower than g_array_append_val() since the
457 * existing elements in the array have to be moved to make space for
460 * <note><para>g_array_prepend_val() is a macro which uses a reference
461 * to the value parameter @v. This means that you cannot use it with
462 * literal values such as "27". You must use variables.</para></note>
464 * Returns: the #GArray.
467 g_array_prepend_vals (GArray *farray,
471 GRealArray *array = (GRealArray*) farray;
473 g_return_val_if_fail (array, NULL);
475 g_array_maybe_expand (array, len);
477 memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
478 g_array_elt_len (array, array->len));
480 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
484 g_array_zero_terminate (array);
490 * g_array_insert_vals:
492 * @index_: the index to place the elements at.
493 * @data: a pointer to the elements to insert.
494 * @len: the number of elements to insert.
496 * Inserts @len elements into a #GArray at the given index.
498 * Returns: the #GArray.
501 * g_array_insert_val:
503 * @i: the index to place the element at.
504 * @v: the value to insert into the array.
506 * Inserts an element into an array at the given index.
508 * <note><para>g_array_insert_val() is a macro which uses a reference
509 * to the value parameter @v. This means that you cannot use it with
510 * literal values such as "27". You must use variables.</para></note>
512 * Returns: the #GArray.
515 g_array_insert_vals (GArray *farray,
520 GRealArray *array = (GRealArray*) farray;
522 g_return_val_if_fail (array, NULL);
524 g_array_maybe_expand (array, len);
526 memmove (g_array_elt_pos (array, len + index_),
527 g_array_elt_pos (array, index_),
528 g_array_elt_len (array, array->len - index_));
530 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
534 g_array_zero_terminate (array);
542 * @length: the new size of the #GArray.
544 * Sets the size of the array, expanding it if necessary. If the array
545 * was created with @clear_ set to %TRUE, the new elements are set to 0.
547 * Returns: the #GArray.
550 g_array_set_size (GArray *farray,
553 GRealArray *array = (GRealArray*) farray;
555 g_return_val_if_fail (array, NULL);
557 if (length > array->len)
559 g_array_maybe_expand (array, length - array->len);
562 g_array_elt_zero (array, array->len, length - array->len);
564 else if (length < array->len)
565 g_array_remove_range (farray, length, array->len - length);
569 g_array_zero_terminate (array);
575 * g_array_remove_index:
577 * @index_: the index of the element to remove.
579 * Removes the element at the given index from a #GArray. The following
580 * elements are moved down one place.
582 * Returns: the #GArray.
585 g_array_remove_index (GArray *farray,
588 GRealArray* array = (GRealArray*) farray;
590 g_return_val_if_fail (array, NULL);
592 g_return_val_if_fail (index_ < array->len, NULL);
594 if (array->clear_func != NULL)
595 array->clear_func (g_array_elt_pos (array, index_));
597 if (index_ != array->len - 1)
598 memmove (g_array_elt_pos (array, index_),
599 g_array_elt_pos (array, index_ + 1),
600 g_array_elt_len (array, array->len - index_ - 1));
604 if (G_UNLIKELY (g_mem_gc_friendly))
605 g_array_elt_zero (array, array->len, 1);
607 g_array_zero_terminate (array);
613 * g_array_remove_index_fast:
615 * @index_: the index of the element to remove.
617 * Removes the element at the given index from a #GArray. The last
618 * element in the array is used to fill in the space, so this function
619 * does not preserve the order of the #GArray. But it is faster than
620 * g_array_remove_index().
622 * Returns: the #GArray.
625 g_array_remove_index_fast (GArray *farray,
628 GRealArray* array = (GRealArray*) farray;
630 g_return_val_if_fail (array, NULL);
632 g_return_val_if_fail (index_ < array->len, NULL);
634 if (array->clear_func != NULL)
635 array->clear_func (g_array_elt_pos (array, index_));
637 if (index_ != array->len - 1)
638 memcpy (g_array_elt_pos (array, index_),
639 g_array_elt_pos (array, array->len - 1),
640 g_array_elt_len (array, 1));
644 if (G_UNLIKELY (g_mem_gc_friendly))
645 g_array_elt_zero (array, array->len, 1);
647 g_array_zero_terminate (array);
653 * g_array_remove_range:
655 * @index_: the index of the first element to remove.
656 * @length: the number of elements to remove.
658 * Removes the given number of elements starting at the given index
659 * from a #GArray. The following elements are moved to close the gap.
661 * Returns: the #GArray.
666 g_array_remove_range (GArray *farray,
670 GRealArray *array = (GRealArray*) farray;
672 g_return_val_if_fail (array, NULL);
673 g_return_val_if_fail (index_ < array->len, NULL);
674 g_return_val_if_fail (index_ + length <= array->len, NULL);
676 if (array->clear_func != NULL)
680 for (i = 0; i < length; i++)
681 array->clear_func (g_array_elt_pos (array, index_ + i));
684 if (index_ + length != array->len)
685 memmove (g_array_elt_pos (array, index_),
686 g_array_elt_pos (array, index_ + length),
687 (array->len - (index_ + length)) * array->elt_size);
689 array->len -= length;
690 if (G_UNLIKELY (g_mem_gc_friendly))
691 g_array_elt_zero (array, array->len, length);
693 g_array_zero_terminate (array);
701 * @compare_func: comparison function.
703 * Sorts a #GArray using @compare_func which should be a qsort()-style
704 * comparison function (returns less than zero for first arg is less
705 * than second arg, zero for equal, greater zero if first arg is
706 * greater than second arg).
708 * This is guaranteed to be a stable sort since version 2.32.
711 g_array_sort (GArray *farray,
712 GCompareFunc compare_func)
714 GRealArray *array = (GRealArray*) farray;
716 g_return_if_fail (array != NULL);
718 /* Don't use qsort as we want a guaranteed stable sort */
719 g_qsort_with_data (array->data,
722 (GCompareDataFunc)compare_func,
727 * g_array_sort_with_data:
729 * @compare_func: comparison function.
730 * @user_data: data to pass to @compare_func.
732 * Like g_array_sort(), but the comparison function receives an extra
733 * user data argument.
735 * This is guaranteed to be a stable sort since version 2.32.
737 * There used to be a comment here about making the sort stable by
738 * using the addresses of the elements in the comparison function.
739 * This did not actually work, so any such code should be removed.
742 g_array_sort_with_data (GArray *farray,
743 GCompareDataFunc compare_func,
746 GRealArray *array = (GRealArray*) farray;
748 g_return_if_fail (array != NULL);
750 g_qsort_with_data (array->data,
757 /* Returns the smallest power of 2 greater than n, or n if
758 * such power does not fit in a guint
761 g_nearest_pow (gint num)
765 while (n < num && n > 0)
772 g_array_maybe_expand (GRealArray *array,
775 guint want_alloc = g_array_elt_len (array, array->len + len +
776 array->zero_terminated);
778 if (want_alloc > array->alloc)
780 want_alloc = g_nearest_pow (want_alloc);
781 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
783 array->data = g_realloc (array->data, want_alloc);
785 if (G_UNLIKELY (g_mem_gc_friendly))
786 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
788 array->alloc = want_alloc;
793 * SECTION:arrays_pointer
794 * @title: Pointer Arrays
795 * @short_description: arrays of pointers to any type of data, which
796 * grow automatically as new elements are added
798 * Pointer Arrays are similar to Arrays but are used only for storing
801 * <note><para>If you remove elements from the array, elements at the
802 * end of the array are moved into the space previously occupied by the
803 * removed element. This means that you should not rely on the index of
804 * particular elements remaining the same. You should also be careful
805 * when deleting elements while iterating over the array.</para></note>
807 * To create a pointer array, use g_ptr_array_new().
809 * To add elements to a pointer array, use g_ptr_array_add().
811 * To remove elements from a pointer array, use g_ptr_array_remove(),
812 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
814 * To access an element of a pointer array, use g_ptr_array_index().
816 * To set the size of a pointer array, use g_ptr_array_set_size().
818 * To free a pointer array, use g_ptr_array_free().
821 * <title>Using a #GPtrArray</title>
823 * GPtrArray *gparray;
824 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
826 * gparray = g_ptr_array_new (<!-- -->);
827 * g_ptr_array_add (gparray, (gpointer) string1);
828 * g_ptr_array_add (gparray, (gpointer) string2);
829 * g_ptr_array_add (gparray, (gpointer) string3);
831 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
832 * g_print ("ERROR: got %p instead of %p\n",
833 * g_ptr_array_index (gparray, 0), string1);
835 * g_ptr_array_free (gparray, TRUE);
840 typedef struct _GRealPtrArray GRealPtrArray;
844 * @pdata: points to the array of pointers, which may be moved when the
846 * @len: number of pointers in the array.
848 * Contains the public fields of a pointer array.
850 struct _GRealPtrArray
856 GDestroyNotify element_free_func;
861 * @array: a #GPtrArray.
862 * @index_: the index of the pointer to return.
864 * Returns the pointer at the given index of the pointer array.
867 * This does not perform bounds checking on the given @index_, so you are
868 * responsible for checking it against the array length.</para></note>
870 * Returns: the pointer at the given index.
873 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
879 * Creates a new #GPtrArray with a reference count of 1.
881 * Returns: the new #GPtrArray.
884 g_ptr_array_new (void)
886 return g_ptr_array_sized_new (0);
890 * g_ptr_array_sized_new:
891 * @reserved_size: number of pointers preallocated.
893 * Creates a new #GPtrArray with @reserved_size pointers preallocated
894 * and a reference count of 1. This avoids frequent reallocation, if
895 * you are going to add many pointers to the array. Note however that
896 * the size of the array is still 0.
898 * Returns: the new #GPtrArray.
901 g_ptr_array_sized_new (guint reserved_size)
903 GRealPtrArray *array = g_slice_new (GRealPtrArray);
908 array->ref_count = 1;
909 array->element_free_func = NULL;
911 if (reserved_size != 0)
912 g_ptr_array_maybe_expand (array, reserved_size);
914 return (GPtrArray*) array;
918 * g_ptr_array_new_with_free_func:
919 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
921 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
922 * for freeing each element when the array is destroyed either via
923 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
924 * set to %TRUE or when removing elements.
926 * Returns: A new #GPtrArray.
931 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
935 array = g_ptr_array_new ();
936 g_ptr_array_set_free_func (array, element_free_func);
941 * g_ptr_array_new_full:
942 * @reserved_size: number of pointers preallocated.
943 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
945 * Creates a new #GPtrArray with @reserved_size pointers preallocated
946 * and a reference count of 1. This avoids frequent reallocation, if
947 * you are going to add many pointers to the array. Note however that
948 * the size of the array is still 0. It also set @element_free_func
949 * for freeing each element when the array is destroyed either via
950 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
951 * set to %TRUE or when removing elements.
953 * Returns: A new #GPtrArray.
958 g_ptr_array_new_full (guint reserved_size,
959 GDestroyNotify element_free_func)
963 array = g_ptr_array_sized_new (reserved_size);
964 g_ptr_array_set_free_func (array, element_free_func);
969 * g_ptr_array_set_free_func:
970 * @array: A #GPtrArray.
971 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
973 * Sets a function for freeing each element when @array is destroyed
974 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
975 * with @free_segment set to %TRUE or when removing elements.
980 g_ptr_array_set_free_func (GPtrArray *array,
981 GDestroyNotify element_free_func)
983 GRealPtrArray* rarray = (GRealPtrArray*) array;
985 g_return_if_fail (array);
987 rarray->element_free_func = element_free_func;
992 * @array: a #GPtrArray
994 * Atomically increments the reference count of @array by one.
995 * This function is thread-safe and may be called from any thread.
997 * Returns: The passed in #GPtrArray
1002 g_ptr_array_ref (GPtrArray *array)
1004 GRealPtrArray *rarray = (GRealPtrArray*) array;
1006 g_return_val_if_fail (array, NULL);
1008 g_atomic_int_inc (&rarray->ref_count);
1013 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
1016 * g_ptr_array_unref:
1017 * @array: A #GPtrArray.
1019 * Atomically decrements the reference count of @array by one. If the
1020 * reference count drops to 0, the effect is the same as calling
1021 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1022 * is MT-safe and may be called from any thread.
1027 g_ptr_array_unref (GPtrArray *array)
1029 GRealPtrArray *rarray = (GRealPtrArray*) array;
1030 g_return_if_fail (array);
1032 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1033 ptr_array_free (array, FREE_SEGMENT);
1038 * @array: a #GPtrArray.
1039 * @free_seg: if %TRUE the actual pointer array is freed as well.
1041 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1042 * it frees the memory block holding the elements as well. Pass %FALSE
1043 * if you want to free the #GPtrArray wrapper but preserve the
1044 * underlying array for use elsewhere. If the reference count of @array
1045 * is greater than one, the #GPtrArray wrapper is preserved but the
1046 * size of @array will be set to zero.
1048 * <note><para>If array contents point to dynamically-allocated
1049 * memory, they should be freed separately if @free_seg is %TRUE and no
1050 * #GDestroyNotify function has been set for @array.</para></note>
1052 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1053 * The pointer array should be freed using g_free().
1056 g_ptr_array_free (GPtrArray *farray,
1057 gboolean free_segment)
1059 GRealPtrArray *array = (GRealPtrArray*) farray;
1060 ArrayFreeFlags flags;
1062 g_return_val_if_fail (array, NULL);
1064 flags = (free_segment ? FREE_SEGMENT : 0);
1066 /* if others are holding a reference, preserve the wrapper but do free/return the data */
1067 if (!g_atomic_int_dec_and_test (&array->ref_count))
1068 flags |= PRESERVE_WRAPPER;
1070 return ptr_array_free (farray, flags);
1074 ptr_array_free (GPtrArray *farray,
1075 ArrayFreeFlags flags)
1077 GRealPtrArray *array = (GRealPtrArray*) farray;
1080 if (flags & FREE_SEGMENT)
1082 if (array->element_free_func != NULL)
1083 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1084 g_free (array->pdata);
1088 segment = array->pdata;
1090 if (flags & PRESERVE_WRAPPER)
1092 array->pdata = NULL;
1098 g_slice_free1 (sizeof (GRealPtrArray), array);
1105 g_ptr_array_maybe_expand (GRealPtrArray *array,
1108 if ((array->len + len) > array->alloc)
1110 guint old_alloc = array->alloc;
1111 array->alloc = g_nearest_pow (array->len + len);
1112 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1113 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1114 if (G_UNLIKELY (g_mem_gc_friendly))
1115 for ( ; old_alloc < array->alloc; old_alloc++)
1116 array->pdata [old_alloc] = NULL;
1121 * g_ptr_array_set_size:
1122 * @array: a #GPtrArray.
1123 * @length: the new length of the pointer array.
1125 * Sets the size of the array. When making the array larger,
1126 * newly-added elements will be set to %NULL. When making it smaller,
1127 * if @array has a non-%NULL #GDestroyNotify function then it will be
1128 * called for the removed elements.
1131 g_ptr_array_set_size (GPtrArray *farray,
1134 GRealPtrArray* array = (GRealPtrArray*) farray;
1136 g_return_if_fail (array);
1138 if (length > array->len)
1141 g_ptr_array_maybe_expand (array, (length - array->len));
1143 * memset (array->pdata + array->len, 0,
1144 * sizeof (gpointer) * (length - array->len));
1145 * to make it really portable. Remember (void*)NULL needn't be
1146 * bitwise zero. It of course is silly not to use memset (..,0,..).
1148 for (i = array->len; i < length; i++)
1149 array->pdata[i] = NULL;
1151 else if (length < array->len)
1152 g_ptr_array_remove_range (farray, length, array->len - length);
1154 array->len = length;
1158 * g_ptr_array_remove_index:
1159 * @array: a #GPtrArray.
1160 * @index_: the index of the pointer to remove.
1162 * Removes the pointer at the given index from the pointer array. The
1163 * following elements are moved down one place. If @array has a
1164 * non-%NULL #GDestroyNotify function it is called for the removed
1167 * Returns: the pointer which was removed.
1170 g_ptr_array_remove_index (GPtrArray *farray,
1173 GRealPtrArray* array = (GRealPtrArray*) farray;
1176 g_return_val_if_fail (array, NULL);
1178 g_return_val_if_fail (index_ < array->len, NULL);
1180 result = array->pdata[index_];
1182 if (array->element_free_func != NULL)
1183 array->element_free_func (array->pdata[index_]);
1185 if (index_ != array->len - 1)
1186 memmove (array->pdata + index_, array->pdata + index_ + 1,
1187 sizeof (gpointer) * (array->len - index_ - 1));
1191 if (G_UNLIKELY (g_mem_gc_friendly))
1192 array->pdata[array->len] = NULL;
1198 * g_ptr_array_remove_index_fast:
1199 * @array: a #GPtrArray.
1200 * @index_: the index of the pointer to remove.
1202 * Removes the pointer at the given index from the pointer array. The
1203 * last element in the array is used to fill in the space, so this
1204 * function does not preserve the order of the array. But it is faster
1205 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1206 * #GDestroyNotify function it is called for the removed element.
1208 * Returns: the pointer which was removed.
1211 g_ptr_array_remove_index_fast (GPtrArray *farray,
1214 GRealPtrArray* array = (GRealPtrArray*) farray;
1217 g_return_val_if_fail (array, NULL);
1219 g_return_val_if_fail (index_ < array->len, NULL);
1221 result = array->pdata[index_];
1223 if (array->element_free_func != NULL)
1224 array->element_free_func (array->pdata[index_]);
1226 if (index_ != array->len - 1)
1227 array->pdata[index_] = array->pdata[array->len - 1];
1231 if (G_UNLIKELY (g_mem_gc_friendly))
1232 array->pdata[array->len] = NULL;
1238 * g_ptr_array_remove_range:
1239 * @array: a @GPtrArray
1240 * @index_: the index of the first pointer to remove
1241 * @length: the number of pointers to remove
1243 * Removes the given number of pointers starting at the given index
1244 * from a #GPtrArray. The following elements are moved to close the
1245 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1246 * for the removed elements.
1248 * Returns: the @array
1253 g_ptr_array_remove_range (GPtrArray *farray,
1257 GRealPtrArray* array = (GRealPtrArray*) farray;
1260 g_return_val_if_fail (array != NULL, NULL);
1261 g_return_val_if_fail (index_ < array->len, NULL);
1262 g_return_val_if_fail (index_ + length <= array->len, NULL);
1264 if (array->element_free_func != NULL)
1266 for (n = index_; n < index_ + length; n++)
1267 array->element_free_func (array->pdata[n]);
1270 if (index_ + length != array->len)
1272 memmove (&array->pdata[index_],
1273 &array->pdata[index_ + length],
1274 (array->len - (index_ + length)) * sizeof (gpointer));
1277 array->len -= length;
1278 if (G_UNLIKELY (g_mem_gc_friendly))
1281 for (i = 0; i < length; i++)
1282 array->pdata[array->len + i] = NULL;
1289 * g_ptr_array_remove:
1290 * @array: a #GPtrArray.
1291 * @data: the pointer to remove.
1293 * Removes the first occurrence of the given pointer from the pointer
1294 * array. The following elements are moved down one place. If @array
1295 * has a non-%NULL #GDestroyNotify function it is called for the
1298 * It returns %TRUE if the pointer was removed, or %FALSE if the
1299 * pointer was not found.
1301 * Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1302 * not found in the array.
1305 g_ptr_array_remove (GPtrArray *farray,
1308 GRealPtrArray* array = (GRealPtrArray*) farray;
1311 g_return_val_if_fail (array, FALSE);
1313 for (i = 0; i < array->len; i += 1)
1315 if (array->pdata[i] == data)
1317 g_ptr_array_remove_index (farray, i);
1326 * g_ptr_array_remove_fast:
1327 * @array: a #GPtrArray.
1328 * @data: the pointer to remove.
1330 * Removes the first occurrence of the given pointer from the pointer
1331 * array. The last element in the array is used to fill in the space,
1332 * so this function does not preserve the order of the array. But it is
1333 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1334 * #GDestroyNotify function it is called for the removed element.
1336 * It returns %TRUE if the pointer was removed, or %FALSE if the
1337 * pointer was not found.
1339 * Returns: %TRUE if the pointer was found in the array.
1342 g_ptr_array_remove_fast (GPtrArray *farray,
1345 GRealPtrArray* array = (GRealPtrArray*) farray;
1348 g_return_val_if_fail (array, FALSE);
1350 for (i = 0; i < array->len; i += 1)
1352 if (array->pdata[i] == data)
1354 g_ptr_array_remove_index_fast (farray, i);
1364 * @array: a #GPtrArray.
1365 * @data: the pointer to add.
1367 * Adds a pointer to the end of the pointer array. The array will grow
1368 * in size automatically if necessary.
1371 g_ptr_array_add (GPtrArray *farray,
1374 GRealPtrArray* array = (GRealPtrArray*) farray;
1376 g_return_if_fail (array);
1378 g_ptr_array_maybe_expand (array, 1);
1380 array->pdata[array->len++] = data;
1384 * g_ptr_array_insert:
1385 * @array: a #GPtrArray.
1386 * @index_: the index to place the new element at, or -1 to append.
1387 * @data: the pointer to add.
1389 * Inserts an element into the pointer array at the given index. The
1390 * array will grow in size automatically if necessary.
1395 g_ptr_array_insert (GPtrArray *farray,
1399 GRealPtrArray* array = (GRealPtrArray*) farray;
1401 g_return_if_fail (array);
1402 g_return_if_fail (index_ >= -1);
1403 g_return_if_fail (index_ <= (gint)array->len);
1405 g_ptr_array_maybe_expand (array, 1);
1408 index_ = array->len;
1410 if (index_ < array->len)
1411 memmove (&(array->pdata[index_ + 1]),
1412 &(array->pdata[index_]),
1413 (array->len - index_) * sizeof (gpointer));
1416 array->pdata[index_] = data;
1421 * @array: a #GPtrArray.
1422 * @compare_func: comparison function.
1424 * Sorts the array, using @compare_func which should be a qsort()-style
1425 * comparison function (returns less than zero for first arg is less
1426 * than second arg, zero for equal, greater than zero if irst arg is
1427 * greater than second arg).
1429 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1430 * take the pointers from the array as arguments, it takes pointers to
1431 * the pointers in the array.</para></note>
1433 * This is guaranteed to be a stable sort since version 2.32.
1436 g_ptr_array_sort (GPtrArray *array,
1437 GCompareFunc compare_func)
1439 g_return_if_fail (array != NULL);
1441 /* Don't use qsort as we want a guaranteed stable sort */
1442 g_qsort_with_data (array->pdata,
1445 (GCompareDataFunc)compare_func,
1450 * g_ptr_array_sort_with_data:
1451 * @array: a #GPtrArray.
1452 * @compare_func: comparison function.
1453 * @user_data: data to pass to @compare_func.
1455 * Like g_ptr_array_sort(), but the comparison function has an extra
1456 * user data argument.
1458 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1459 * doesn't take the pointers from the array as arguments, it takes
1460 * pointers to the pointers in the array.</para></note>
1462 * This is guaranteed to be a stable sort since version 2.32.
1465 g_ptr_array_sort_with_data (GPtrArray *array,
1466 GCompareDataFunc compare_func,
1469 g_return_if_fail (array != NULL);
1471 g_qsort_with_data (array->pdata,
1479 * g_ptr_array_foreach:
1480 * @array: a #GPtrArray
1481 * @func: the function to call for each array element
1482 * @user_data: user data to pass to the function
1484 * Calls a function for each element of a #GPtrArray.
1489 g_ptr_array_foreach (GPtrArray *array,
1495 g_return_if_fail (array);
1497 for (i = 0; i < array->len; i++)
1498 (*func) (array->pdata[i], user_data);
1502 * SECTION:arrays_byte
1503 * @title: Byte Arrays
1504 * @short_description: arrays of bytes
1506 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1507 * of bytes which grow automatically as elements are added.
1509 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1510 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1512 * To set the size of a #GByteArray, use g_byte_array_set_size().
1514 * To free a #GByteArray, use g_byte_array_free().
1517 * <title>Using a #GByteArray</title>
1519 * GByteArray *gbarray;
1522 * gbarray = g_byte_array_new (<!-- -->);
1523 * for (i = 0; i < 10000; i++)
1524 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1526 * for (i = 0; i < 10000; i++)
1528 * g_assert (gbarray->data[4*i] == 'a');
1529 * g_assert (gbarray->data[4*i+1] == 'b');
1530 * g_assert (gbarray->data[4*i+2] == 'c');
1531 * g_assert (gbarray->data[4*i+3] == 'd');
1534 * g_byte_array_free (gbarray, TRUE);
1538 * See #GBytes if you are interested in an immutable object representing a
1539 * sequence of bytes.
1544 * @data: a pointer to the element data. The data may be moved as
1545 * elements are added to the #GByteArray.
1546 * @len: the number of elements in the #GByteArray.
1548 * The #GByteArray-struct allows access to the public fields of
1555 * Creates a new #GByteArray with a reference count of 1.
1557 * Returns: (transfer full): the new #GByteArray.
1559 GByteArray* g_byte_array_new (void)
1561 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1565 * g_byte_array_new_take:
1566 * @data: (transfer full) (array length=len): byte data for the array
1567 * @len: length of @data
1569 * Create byte array containing the data. The data will be owned by the array
1570 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1574 * Returns: (transfer full): a new #GByteArray
1577 g_byte_array_new_take (guint8 *data,
1583 array = g_byte_array_new ();
1584 real = (GRealArray *)array;
1585 g_assert (real->data == NULL);
1586 g_assert (real->len == 0);
1595 * g_byte_array_sized_new:
1596 * @reserved_size: number of bytes preallocated.
1598 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1599 * This avoids frequent reallocation, if you are going to add many
1600 * bytes to the array. Note however that the size of the array is still
1603 * Returns: the new #GByteArray.
1605 GByteArray* g_byte_array_sized_new (guint reserved_size)
1607 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1611 * g_byte_array_free:
1612 * @array: a #GByteArray.
1613 * @free_segment: if %TRUE the actual byte data is freed as well.
1615 * Frees the memory allocated by the #GByteArray. If @free_segment is
1616 * %TRUE it frees the actual byte data. If the reference count of
1617 * @array is greater than one, the #GByteArray wrapper is preserved but
1618 * the size of @array will be set to zero.
1620 * Returns: the element data if @free_segment is %FALSE, otherwise
1621 * %NULL. The element data should be freed using g_free().
1623 guint8* g_byte_array_free (GByteArray *array,
1624 gboolean free_segment)
1626 return (guint8*) g_array_free ((GArray*) array, free_segment);
1630 * g_byte_array_free_to_bytes:
1631 * @array: (transfer full): a #GByteArray
1633 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1635 * The #GByteArray is freed unless the reference count of @array is greater
1636 * than one, the #GByteArray wrapper is preserved but the size of @array
1637 * will be set to zero.
1639 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1644 * Returns: (transfer full): a new immutable #GBytes representing same byte
1645 * data that was in the array
1648 g_byte_array_free_to_bytes (GByteArray *array)
1652 g_return_val_if_fail (array != NULL, NULL);
1654 length = array->len;
1655 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1660 * @array: A #GByteArray.
1662 * Atomically increments the reference count of @array by one. This
1663 * function is MT-safe and may be called from any thread.
1665 * Returns: The passed in #GByteArray.
1670 g_byte_array_ref (GByteArray *array)
1672 return (GByteArray *) g_array_ref ((GArray *) array);
1676 * g_byte_array_unref:
1677 * @array: A #GByteArray.
1679 * Atomically decrements the reference count of @array by one. If the
1680 * reference count drops to 0, all memory allocated by the array is
1681 * released. This function is MT-safe and may be called from any
1687 g_byte_array_unref (GByteArray *array)
1689 g_array_unref ((GArray *) array);
1693 * g_byte_array_append:
1694 * @array: a #GByteArray.
1695 * @data: the byte data to be added.
1696 * @len: the number of bytes to add.
1698 * Adds the given bytes to the end of the #GByteArray. The array will
1699 * grow in size automatically if necessary.
1701 * Returns: the #GByteArray.
1703 GByteArray* g_byte_array_append (GByteArray *array,
1707 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1713 * g_byte_array_prepend:
1714 * @array: a #GByteArray.
1715 * @data: the byte data to be added.
1716 * @len: the number of bytes to add.
1718 * Adds the given data to the start of the #GByteArray. The array will
1719 * grow in size automatically if necessary.
1721 * Returns: the #GByteArray.
1723 GByteArray* g_byte_array_prepend (GByteArray *array,
1727 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1733 * g_byte_array_set_size:
1734 * @array: a #GByteArray.
1735 * @length: the new size of the #GByteArray.
1737 * Sets the size of the #GByteArray, expanding it if necessary.
1739 * Returns: the #GByteArray.
1741 GByteArray* g_byte_array_set_size (GByteArray *array,
1744 g_array_set_size ((GArray*) array, length);
1750 * g_byte_array_remove_index:
1751 * @array: a #GByteArray.
1752 * @index_: the index of the byte to remove.
1754 * Removes the byte at the given index from a #GByteArray. The
1755 * following bytes are moved down one place.
1757 * Returns: the #GByteArray.
1759 GByteArray* g_byte_array_remove_index (GByteArray *array,
1762 g_array_remove_index ((GArray*) array, index_);
1768 * g_byte_array_remove_index_fast:
1769 * @array: a #GByteArray.
1770 * @index_: the index of the byte to remove.
1772 * Removes the byte at the given index from a #GByteArray. The last
1773 * element in the array is used to fill in the space, so this function
1774 * does not preserve the order of the #GByteArray. But it is faster
1775 * than g_byte_array_remove_index().
1777 * Returns: the #GByteArray.
1779 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1782 g_array_remove_index_fast ((GArray*) array, index_);
1788 * g_byte_array_remove_range:
1789 * @array: a @GByteArray.
1790 * @index_: the index of the first byte to remove.
1791 * @length: the number of bytes to remove.
1793 * Removes the given number of bytes starting at the given index from a
1794 * #GByteArray. The following elements are moved to close the gap.
1796 * Returns: the #GByteArray.
1801 g_byte_array_remove_range (GByteArray *array,
1805 g_return_val_if_fail (array, NULL);
1806 g_return_val_if_fail (index_ < array->len, NULL);
1807 g_return_val_if_fail (index_ + length <= array->len, NULL);
1809 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1813 * g_byte_array_sort:
1814 * @array: a #GByteArray.
1815 * @compare_func: comparison function.
1817 * Sorts a byte array, using @compare_func which should be a
1818 * qsort()-style comparison function (returns less than zero for first
1819 * arg is less than second arg, zero for equal, greater than zero if
1820 * first arg is greater than second arg).
1822 * If two array elements compare equal, their order in the sorted array
1823 * is undefined. If you want equal elements to keep their order (i.e.
1824 * you want a stable sort) you can write a comparison function that,
1825 * if two elements would otherwise compare equal, compares them by
1829 g_byte_array_sort (GByteArray *array,
1830 GCompareFunc compare_func)
1832 g_array_sort ((GArray *) array, compare_func);
1836 * g_byte_array_sort_with_data:
1837 * @array: a #GByteArray.
1838 * @compare_func: comparison function.
1839 * @user_data: data to pass to @compare_func.
1841 * Like g_byte_array_sort(), but the comparison function takes an extra
1842 * user data argument.
1845 g_byte_array_sort_with_data (GByteArray *array,
1846 GCompareDataFunc compare_func,
1849 g_array_sort_with_data ((GArray *) array, compare_func, user_data);