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/.
41 #include "gtestutils.h"
43 #include "gmessages.h"
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().
73 * <title>Using a #GArray to store #gint values</title>
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 < 10000; i++)
81 * g_array_append_val (garray, i);
82 * for (i = 0; i < 10000; i++)
83 * if (g_array_index (garray, gint, i) != i)
84 * g_print ("ERROR: got %d instead of %d\n",
85 * g_array_index (garray, gint, i), i);
86 * g_array_free (garray, TRUE);
91 #define MIN_ARRAY_SIZE 16
93 typedef struct _GRealArray GRealArray;
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>.
110 guint zero_terminated : 1;
113 GDestroyNotify clear_func;
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.
127 * <title>Getting a pointer to an element in a #GArray</title>
129 * EDayViewEvent *event;
130 * /<!-- -->* This gets a pointer to the 4th element
131 * in the array of EDayViewEvent structs. *<!-- -->/
132 * event = &g_array_index (events, EDayViewEvent, 3);
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); \
146 static guint g_nearest_pow (gint num) G_GNUC_CONST;
147 static void g_array_maybe_expand (GRealArray *array,
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.
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.
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,
193 g_return_val_if_fail (elt_size > 0, NULL);
195 array = g_slice_new (GRealArray);
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:
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.
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;
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.
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);
267 FREE_SEGMENT = 1 << 0,
268 PRESERVE_WRAPPER = 1 << 1
271 static gchar *array_free (GRealArray *, ArrayFreeFlags);
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
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:
298 * Gets the size of the elements in @array.
300 * Returns: Size of each element, in bytes.
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;
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>
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);
351 array_free (GRealArray *array,
352 ArrayFreeFlags flags)
356 if (flags & FREE_SEGMENT)
358 if (array->clear_func != NULL)
362 for (i = 0; i < array->len; i++)
363 array->clear_func (g_array_elt_pos (array, i));
366 g_free (array->data);
370 segment = (gchar*) array->data;
372 if (flags & PRESERVE_WRAPPER)
380 g_slice_free1 (sizeof (GRealArray), array);
387 * g_array_append_vals:
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:
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>
409 g_array_append_vals (GArray *farray,
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));
424 g_array_zero_terminate (array);
430 * g_array_prepend_vals:
432 * @data: a pointer to the elements to prepend to the start of the
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
444 * g_array_prepend_val:
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
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>
461 g_array_prepend_vals (GArray *farray,
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));
478 g_array_zero_terminate (array);
484 * g_array_insert_vals:
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:
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>
507 g_array_insert_vals (GArray *farray,
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));
526 g_array_zero_terminate (array);
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.
541 g_array_set_size (GArray *farray,
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);
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);
560 g_array_zero_terminate (array);
566 * g_array_remove_index:
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.
575 g_array_remove_index (GArray *farray,
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));
594 if (G_UNLIKELY (g_mem_gc_friendly))
595 g_array_elt_zero (array, array->len, 1);
597 g_array_zero_terminate (array);
603 * g_array_remove_index_fast:
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().
614 g_array_remove_index_fast (GArray *farray,
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));
633 if (G_UNLIKELY (g_mem_gc_friendly))
634 g_array_elt_zero (array, array->len, 1);
636 g_array_zero_terminate (array);
642 * g_array_remove_range:
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.
654 g_array_remove_range (GArray *farray,
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)
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);
681 g_array_zero_terminate (array);
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 * If two array elements compare equal, their order in the sorted array
697 * is undefined. If you want equal elements to keep their order (i.e.
698 * you want a stable sort) you can write a comparison function that,
699 * if two elements would otherwise compare equal, compares them by
703 g_array_sort (GArray *farray,
704 GCompareFunc compare_func)
706 GRealArray *array = (GRealArray*) farray;
708 g_return_if_fail (array != NULL);
717 * g_array_sort_with_data:
719 * @compare_func: comparison function.
720 * @user_data: data to pass to @compare_func.
722 * Like g_array_sort(), but the comparison function receives an extra
723 * user data argument.
726 g_array_sort_with_data (GArray *farray,
727 GCompareDataFunc compare_func,
730 GRealArray *array = (GRealArray*) farray;
732 g_return_if_fail (array != NULL);
734 g_qsort_with_data (array->data,
741 /* Returns the smallest power of 2 greater than n, or n if
742 * such power does not fit in a guint
745 g_nearest_pow (gint num)
749 while (n < num && n > 0)
756 g_array_maybe_expand (GRealArray *array,
759 guint want_alloc = g_array_elt_len (array, array->len + len +
760 array->zero_terminated);
762 if (want_alloc > array->alloc)
764 want_alloc = g_nearest_pow (want_alloc);
765 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
767 array->data = g_realloc (array->data, want_alloc);
769 if (G_UNLIKELY (g_mem_gc_friendly))
770 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
772 array->alloc = want_alloc;
777 * SECTION:arrays_pointer
778 * @title: Pointer Arrays
779 * @short_description: arrays of pointers to any type of data, which
780 * grow automatically as new elements are added
782 * Pointer Arrays are similar to Arrays but are used only for storing
785 * <note><para>If you remove elements from the array, elements at the
786 * end of the array are moved into the space previously occupied by the
787 * removed element. This means that you should not rely on the index of
788 * particular elements remaining the same. You should also be careful
789 * when deleting elements while iterating over the array.</para></note>
791 * To create a pointer array, use g_ptr_array_new().
793 * To add elements to a pointer array, use g_ptr_array_add().
795 * To remove elements from a pointer array, use g_ptr_array_remove(),
796 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
798 * To access an element of a pointer array, use g_ptr_array_index().
800 * To set the size of a pointer array, use g_ptr_array_set_size().
802 * To free a pointer array, use g_ptr_array_free().
805 * <title>Using a #GPtrArray</title>
807 * GPtrArray *gparray;
808 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
810 * gparray = g_ptr_array_new (<!-- -->);
811 * g_ptr_array_add (gparray, (gpointer) string1);
812 * g_ptr_array_add (gparray, (gpointer) string2);
813 * g_ptr_array_add (gparray, (gpointer) string3);
815 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
816 * g_print ("ERROR: got %p instead of %p\n",
817 * g_ptr_array_index (gparray, 0), string1);
819 * g_ptr_array_free (gparray, TRUE);
824 typedef struct _GRealPtrArray GRealPtrArray;
828 * @pdata: points to the array of pointers, which may be moved when the
830 * @len: number of pointers in the array.
832 * Contains the public fields of a pointer array.
834 struct _GRealPtrArray
840 GDestroyNotify element_free_func;
845 * @array: a #GPtrArray.
846 * @index_: the index of the pointer to return.
847 * @Returns: the pointer at the given index.
849 * Returns the pointer at the given index of the pointer array.
852 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
857 * @Returns: the new #GPtrArray.
859 * Creates a new #GPtrArray with a reference count of 1.
862 g_ptr_array_new (void)
864 return g_ptr_array_sized_new (0);
868 * g_ptr_array_sized_new:
869 * @reserved_size: number of pointers preallocated.
870 * @Returns: the new #GPtrArray.
872 * Creates a new #GPtrArray with @reserved_size pointers preallocated
873 * and a reference count of 1. This avoids frequent reallocation, if
874 * you are going to add many pointers to the array. Note however that
875 * the size of the array is still 0.
878 g_ptr_array_sized_new (guint reserved_size)
880 GRealPtrArray *array = g_slice_new (GRealPtrArray);
885 array->ref_count = 1;
886 array->element_free_func = NULL;
888 if (reserved_size != 0)
889 g_ptr_array_maybe_expand (array, reserved_size);
891 return (GPtrArray*) array;
895 * g_ptr_array_new_with_free_func:
896 * @element_free_func: A function to free elements with destroy @array or %NULL.
898 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
899 * for freeing each element when the array is destroyed either via
900 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
901 * set to %TRUE or when removing elements.
903 * Returns: A new #GPtrArray.
908 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
912 array = g_ptr_array_new ();
913 g_ptr_array_set_free_func (array, element_free_func);
918 * g_ptr_array_new_full:
919 * @reserved_size: number of pointers preallocated.
920 * @element_free_func: A function to free elements with destroy @array or %NULL.
922 * Creates a new #GPtrArray with @reserved_size pointers preallocated
923 * and a reference count of 1. This avoids frequent reallocation, if
924 * you are going to add many pointers to the array. Note however that
925 * the size of the array is still 0. It also set @element_free_func
926 * for freeing each element when the array is destroyed either via
927 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
928 * set to %TRUE or when removing elements.
930 * Returns: A new #GPtrArray.
935 g_ptr_array_new_full (guint reserved_size,
936 GDestroyNotify element_free_func)
940 array = g_ptr_array_sized_new (reserved_size);
941 g_ptr_array_set_free_func (array, element_free_func);
946 * g_ptr_array_set_free_func:
947 * @array: A #GPtrArray.
948 * @element_free_func: A function to free elements with destroy @array or %NULL.
950 * Sets a function for freeing each element when @array is destroyed
951 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
952 * with @free_segment set to %TRUE or when removing elements.
957 g_ptr_array_set_free_func (GPtrArray *array,
958 GDestroyNotify element_free_func)
960 GRealPtrArray* rarray = (GRealPtrArray*) array;
962 g_return_if_fail (array);
964 rarray->element_free_func = element_free_func;
971 * Atomically increments the reference count of @array by one. This
972 * function is MT-safe and may be called from any thread.
974 * Returns: The passed in #GPtrArray.
979 g_ptr_array_ref (GPtrArray *array)
981 GRealPtrArray *rarray = (GRealPtrArray*) array;
983 g_return_val_if_fail (array, NULL);
985 g_atomic_int_inc (&rarray->ref_count);
990 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
994 * @array: A #GPtrArray.
996 * Atomically decrements the reference count of @array by one. If the
997 * reference count drops to 0, the effect is the same as calling
998 * g_ptr_array_free() with @free_segment set to %TRUE. This function
999 * is MT-safe and may be called from any thread.
1004 g_ptr_array_unref (GPtrArray *array)
1006 GRealPtrArray *rarray = (GRealPtrArray*) array;
1007 g_return_if_fail (array);
1009 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1010 ptr_array_free (array, FREE_SEGMENT);
1015 * @array: a #GPtrArray.
1016 * @free_seg: if %TRUE the actual pointer array is freed as well.
1017 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1018 * The pointer array should be freed using g_free().
1020 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1021 * it frees the memory block holding the elements as well. Pass %FALSE
1022 * if you want to free the #GPtrArray wrapper but preserve the
1023 * underlying array for use elsewhere. If the reference count of @array
1024 * is greater than one, the #GPtrArray wrapper is preserved but the
1025 * size of @array will be set to zero.
1027 * <note><para>If array contents point to dynamically-allocated
1028 * memory, they should be freed separately if @free_seg is %TRUE and no
1029 * #GDestroyNotify function has been set for @array.</para></note>
1032 g_ptr_array_free (GPtrArray *farray,
1033 gboolean free_segment)
1035 GRealPtrArray *array = (GRealPtrArray*) farray;
1036 ArrayFreeFlags flags;
1038 g_return_val_if_fail (array, NULL);
1040 flags = (free_segment ? FREE_SEGMENT : 0);
1042 /* if others are holding a reference, preserve the wrapper but do free/return the data */
1043 if (!g_atomic_int_dec_and_test (&array->ref_count))
1044 flags |= PRESERVE_WRAPPER;
1046 return ptr_array_free (farray, flags);
1050 ptr_array_free (GPtrArray *farray,
1051 ArrayFreeFlags flags)
1053 GRealPtrArray *array = (GRealPtrArray*) farray;
1056 if (flags & FREE_SEGMENT)
1058 if (array->element_free_func != NULL)
1059 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1060 g_free (array->pdata);
1064 segment = array->pdata;
1066 if (flags & PRESERVE_WRAPPER)
1068 array->pdata = NULL;
1074 g_slice_free1 (sizeof (GRealPtrArray), array);
1081 g_ptr_array_maybe_expand (GRealPtrArray *array,
1084 if ((array->len + len) > array->alloc)
1086 guint old_alloc = array->alloc;
1087 array->alloc = g_nearest_pow (array->len + len);
1088 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1089 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1090 if (G_UNLIKELY (g_mem_gc_friendly))
1091 for ( ; old_alloc < array->alloc; old_alloc++)
1092 array->pdata [old_alloc] = NULL;
1097 * g_ptr_array_set_size:
1098 * @array: a #GPtrArray.
1099 * @length: the new length of the pointer array.
1101 * Sets the size of the array. When making the array larger,
1102 * newly-added elements will be set to %NULL. When making it smaller,
1103 * if @array has a non-%NULL #GDestroyNotify function then it will be
1104 * called for the removed elements.
1107 g_ptr_array_set_size (GPtrArray *farray,
1110 GRealPtrArray* array = (GRealPtrArray*) farray;
1112 g_return_if_fail (array);
1114 if (length > array->len)
1117 g_ptr_array_maybe_expand (array, (length - array->len));
1119 * memset (array->pdata + array->len, 0,
1120 * sizeof (gpointer) * (length - array->len));
1121 * to make it really portable. Remember (void*)NULL needn't be
1122 * bitwise zero. It of course is silly not to use memset (..,0,..).
1124 for (i = array->len; i < length; i++)
1125 array->pdata[i] = NULL;
1127 else if (length < array->len)
1128 g_ptr_array_remove_range (farray, length, array->len - length);
1130 array->len = length;
1134 * g_ptr_array_remove_index:
1135 * @array: a #GPtrArray.
1136 * @index_: the index of the pointer to remove.
1137 * @Returns: the pointer which was removed.
1139 * Removes the pointer at the given index from the pointer array. The
1140 * following elements are moved down one place. If @array has a
1141 * non-%NULL #GDestroyNotify function it is called for the removed
1145 g_ptr_array_remove_index (GPtrArray *farray,
1148 GRealPtrArray* array = (GRealPtrArray*) farray;
1151 g_return_val_if_fail (array, NULL);
1153 g_return_val_if_fail (index_ < array->len, NULL);
1155 result = array->pdata[index_];
1157 if (array->element_free_func != NULL)
1158 array->element_free_func (array->pdata[index_]);
1160 if (index_ != array->len - 1)
1161 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1162 sizeof (gpointer) * (array->len - index_ - 1));
1166 if (G_UNLIKELY (g_mem_gc_friendly))
1167 array->pdata[array->len] = NULL;
1173 * g_ptr_array_remove_index_fast:
1174 * @array: a #GPtrArray.
1175 * @index_: the index of the pointer to remove.
1176 * @Returns: the pointer which was removed.
1178 * Removes the pointer at the given index from the pointer array. The
1179 * last element in the array is used to fill in the space, so this
1180 * function does not preserve the order of the array. But it is faster
1181 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1182 * #GDestroyNotify function it is called for the removed element.
1185 g_ptr_array_remove_index_fast (GPtrArray *farray,
1188 GRealPtrArray* array = (GRealPtrArray*) farray;
1191 g_return_val_if_fail (array, NULL);
1193 g_return_val_if_fail (index_ < array->len, NULL);
1195 result = array->pdata[index_];
1197 if (array->element_free_func != NULL)
1198 array->element_free_func (array->pdata[index_]);
1200 if (index_ != array->len - 1)
1201 array->pdata[index_] = array->pdata[array->len - 1];
1205 if (G_UNLIKELY (g_mem_gc_friendly))
1206 array->pdata[array->len] = NULL;
1212 * g_ptr_array_remove_range:
1213 * @array: a @GPtrArray.
1214 * @index_: the index of the first pointer to remove.
1215 * @length: the number of pointers to remove.
1217 * Removes the given number of pointers starting at the given index
1218 * from a #GPtrArray. The following elements are moved to close the
1219 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1220 * for the removed elements.
1225 g_ptr_array_remove_range (GPtrArray *farray,
1229 GRealPtrArray* array = (GRealPtrArray*) farray;
1232 g_return_if_fail (array);
1233 g_return_if_fail (index_ < array->len);
1234 g_return_if_fail (index_ + length <= array->len);
1236 if (array->element_free_func != NULL)
1238 for (n = index_; n < index_ + length; n++)
1239 array->element_free_func (array->pdata[n]);
1242 if (index_ + length != array->len)
1244 g_memmove (&array->pdata[index_],
1245 &array->pdata[index_ + length],
1246 (array->len - (index_ + length)) * sizeof (gpointer));
1249 array->len -= length;
1250 if (G_UNLIKELY (g_mem_gc_friendly))
1253 for (i = 0; i < length; i++)
1254 array->pdata[array->len + i] = NULL;
1259 * g_ptr_array_remove:
1260 * @array: a #GPtrArray.
1261 * @data: the pointer to remove.
1262 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1263 * not found in the array.
1265 * Removes the first occurrence of the given pointer from the pointer
1266 * array. The following elements are moved down one place. If @array
1267 * has a non-%NULL #GDestroyNotify function it is called for the
1270 * It returns %TRUE if the pointer was removed, or %FALSE if the
1271 * pointer was not found.
1274 g_ptr_array_remove (GPtrArray *farray,
1277 GRealPtrArray* array = (GRealPtrArray*) farray;
1280 g_return_val_if_fail (array, FALSE);
1282 for (i = 0; i < array->len; i += 1)
1284 if (array->pdata[i] == data)
1286 g_ptr_array_remove_index (farray, i);
1295 * g_ptr_array_remove_fast:
1296 * @array: a #GPtrArray.
1297 * @data: the pointer to remove.
1298 * @Returns: %TRUE if the pointer was found in the array.
1300 * Removes the first occurrence of the given pointer from the pointer
1301 * array. The last element in the array is used to fill in the space,
1302 * so this function does not preserve the order of the array. But it is
1303 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1304 * #GDestroyNotify function it is called for the removed element.
1306 * It returns %TRUE if the pointer was removed, or %FALSE if the
1307 * pointer was not found.
1310 g_ptr_array_remove_fast (GPtrArray *farray,
1313 GRealPtrArray* array = (GRealPtrArray*) farray;
1316 g_return_val_if_fail (array, FALSE);
1318 for (i = 0; i < array->len; i += 1)
1320 if (array->pdata[i] == data)
1322 g_ptr_array_remove_index_fast (farray, i);
1332 * @array: a #GPtrArray.
1333 * @data: the pointer to add.
1335 * Adds a pointer to the end of the pointer array. The array will grow
1336 * in size automatically if necessary.
1339 g_ptr_array_add (GPtrArray *farray,
1342 GRealPtrArray* array = (GRealPtrArray*) farray;
1344 g_return_if_fail (array);
1346 g_ptr_array_maybe_expand (array, 1);
1348 array->pdata[array->len++] = data;
1353 * @array: a #GPtrArray.
1354 * @compare_func: comparison function.
1356 * Sorts the array, using @compare_func which should be a qsort()-style
1357 * comparison function (returns less than zero for first arg is less
1358 * than second arg, zero for equal, greater than zero if irst arg is
1359 * greater than second arg).
1361 * If two array elements compare equal, their order in the sorted array
1362 * is undefined. If you want equal elements to keep their order (i.e.
1363 * you want a stable sort) you can write a comparison function that,
1364 * if two elements would otherwise compare equal, compares them by
1367 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1368 * take the pointers from the array as arguments, it takes pointers to
1369 * the pointers in the array.</para></note>
1372 g_ptr_array_sort (GPtrArray *array,
1373 GCompareFunc compare_func)
1375 g_return_if_fail (array != NULL);
1377 qsort (array->pdata,
1384 * g_ptr_array_sort_with_data:
1385 * @array: a #GPtrArray.
1386 * @compare_func: comparison function.
1387 * @user_data: data to pass to @compare_func.
1389 * Like g_ptr_array_sort(), but the comparison function has an extra
1390 * user data argument.
1392 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1393 * doesn't take the pointers from the array as arguments, it takes
1394 * pointers to the pointers in the array.</para></note>
1397 g_ptr_array_sort_with_data (GPtrArray *array,
1398 GCompareDataFunc compare_func,
1401 g_return_if_fail (array != NULL);
1403 g_qsort_with_data (array->pdata,
1411 * g_ptr_array_foreach:
1412 * @array: a #GPtrArray
1413 * @func: the function to call for each array element
1414 * @user_data: user data to pass to the function
1416 * Calls a function for each element of a #GPtrArray.
1421 g_ptr_array_foreach (GPtrArray *array,
1427 g_return_if_fail (array);
1429 for (i = 0; i < array->len; i++)
1430 (*func) (array->pdata[i], user_data);
1434 * SECTION:arrays_byte
1435 * @title: Byte Arrays
1436 * @short_description: arrays of bytes
1438 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1439 * of bytes which grow automatically as elements are added.
1441 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1442 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1444 * To set the size of a #GByteArray, use g_byte_array_set_size().
1446 * To free a #GByteArray, use g_byte_array_free().
1449 * <title>Using a #GByteArray</title>
1451 * GByteArray *gbarray;
1454 * gbarray = g_byte_array_new (<!-- -->);
1455 * for (i = 0; i < 10000; i++)
1456 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1458 * for (i = 0; i < 10000; i++)
1460 * g_assert (gbarray->data[4*i] == 'a');
1461 * g_assert (gbarray->data[4*i+1] == 'b');
1462 * g_assert (gbarray->data[4*i+2] == 'c');
1463 * g_assert (gbarray->data[4*i+3] == 'd');
1466 * g_byte_array_free (gbarray, TRUE);
1470 * See #GBytes if you are interested in an immutable object representing a
1471 * sequence of bytes.
1476 * @data: a pointer to the element data. The data may be moved as
1477 * elements are added to the #GByteArray.
1478 * @len: the number of elements in the #GByteArray.
1480 * The <structname>GByteArray</structname> struct allows access to the
1481 * public fields of a <structname>GByteArray</structname>.
1486 * @Returns: the new #GByteArray.
1488 * Creates a new #GByteArray with a reference count of 1.
1490 GByteArray* g_byte_array_new (void)
1492 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1496 * g_byte_array_new_take:
1497 * @data: (array length=len): byte data for the array
1498 * @len: length of @data
1500 * Create byte array containing the data. The data will be owned by the array
1501 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1505 * Returns: (transfer full): a new #GByteArray
1508 g_byte_array_new_take (guint8 *data,
1514 array = g_byte_array_new ();
1515 real = (GRealArray *)array;
1516 g_assert (real->data == NULL);
1517 g_assert (real->len == 0);
1526 * g_byte_array_sized_new:
1527 * @reserved_size: number of bytes preallocated.
1528 * @Returns: the new #GByteArray.
1530 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1531 * This avoids frequent reallocation, if you are going to add many
1532 * bytes to the array. Note however that the size of the array is still
1535 GByteArray* g_byte_array_sized_new (guint reserved_size)
1537 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1541 * g_byte_array_free:
1542 * @array: a #GByteArray.
1543 * @free_segment: if %TRUE the actual byte data is freed as well.
1544 * @Returns: the element data if @free_segment is %FALSE, otherwise
1545 * %NULL. The element data should be freed using g_free().
1547 * Frees the memory allocated by the #GByteArray. If @free_segment is
1548 * %TRUE it frees the actual byte data. If the reference count of
1549 * @array is greater than one, the #GByteArray wrapper is preserved but
1550 * the size of @array will be set to zero.
1552 guint8* g_byte_array_free (GByteArray *array,
1553 gboolean free_segment)
1555 return (guint8*) g_array_free ((GArray*) array, free_segment);
1559 * g_byte_array_free_to_bytes:
1560 * @array: (transfer full): a #GByteArray
1562 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1564 * The #GByteArray is freed unless the reference count of @array is greater
1565 * than one, the #GByteArray wrapper is preserved but the size of @array
1566 * will be set to zero.
1568 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1573 * Returns: (transfer full): a new immutable #GBytes representing same byte
1574 * data that was in the array
1577 g_byte_array_free_to_bytes (GByteArray *array)
1581 g_return_val_if_fail (array != NULL, NULL);
1583 length = array->len;
1584 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1589 * @array: A #GByteArray.
1591 * Atomically increments the reference count of @array by one. This
1592 * function is MT-safe and may be called from any thread.
1594 * Returns: The passed in #GByteArray.
1599 g_byte_array_ref (GByteArray *array)
1601 return (GByteArray *) g_array_ref ((GArray *) array);
1605 * g_byte_array_unref:
1606 * @array: A #GByteArray.
1608 * Atomically decrements the reference count of @array by one. If the
1609 * reference count drops to 0, all memory allocated by the array is
1610 * released. This function is MT-safe and may be called from any
1616 g_byte_array_unref (GByteArray *array)
1618 g_array_unref ((GArray *) array);
1622 * g_byte_array_append:
1623 * @array: a #GByteArray.
1624 * @data: the byte data to be added.
1625 * @len: the number of bytes to add.
1626 * @Returns: the #GByteArray.
1628 * Adds the given bytes to the end of the #GByteArray. The array will
1629 * grow in size automatically if necessary.
1631 GByteArray* g_byte_array_append (GByteArray *array,
1635 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1641 * g_byte_array_prepend:
1642 * @array: a #GByteArray.
1643 * @data: the byte data to be added.
1644 * @len: the number of bytes to add.
1645 * @Returns: the #GByteArray.
1647 * Adds the given data to the start of the #GByteArray. The array will
1648 * grow in size automatically if necessary.
1650 GByteArray* g_byte_array_prepend (GByteArray *array,
1654 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1660 * g_byte_array_set_size:
1661 * @array: a #GByteArray.
1662 * @length: the new size of the #GByteArray.
1663 * @Returns: the #GByteArray.
1665 * Sets the size of the #GByteArray, expanding it if necessary.
1667 GByteArray* g_byte_array_set_size (GByteArray *array,
1670 g_array_set_size ((GArray*) array, length);
1676 * g_byte_array_remove_index:
1677 * @array: a #GByteArray.
1678 * @index_: the index of the byte to remove.
1679 * @Returns: the #GByteArray.
1681 * Removes the byte at the given index from a #GByteArray. The
1682 * following bytes are moved down one place.
1684 GByteArray* g_byte_array_remove_index (GByteArray *array,
1687 g_array_remove_index ((GArray*) array, index_);
1693 * g_byte_array_remove_index_fast:
1694 * @array: a #GByteArray.
1695 * @index_: the index of the byte to remove.
1696 * @Returns: the #GByteArray.
1698 * Removes the byte at the given index from a #GByteArray. The last
1699 * element in the array is used to fill in the space, so this function
1700 * does not preserve the order of the #GByteArray. But it is faster
1701 * than g_byte_array_remove_index().
1703 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1706 g_array_remove_index_fast ((GArray*) array, index_);
1712 * g_byte_array_remove_range:
1713 * @array: a @GByteArray.
1714 * @index_: the index of the first byte to remove.
1715 * @length: the number of bytes to remove.
1716 * @Returns: the #GByteArray.
1718 * Removes the given number of bytes starting at the given index from a
1719 * #GByteArray. The following elements are moved to close the gap.
1724 g_byte_array_remove_range (GByteArray *array,
1728 g_return_val_if_fail (array, NULL);
1729 g_return_val_if_fail (index_ < array->len, NULL);
1730 g_return_val_if_fail (index_ + length <= array->len, NULL);
1732 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1736 * g_byte_array_sort:
1737 * @array: a #GByteArray.
1738 * @compare_func: comparison function.
1740 * Sorts a byte array, using @compare_func which should be a
1741 * qsort()-style comparison function (returns less than zero for first
1742 * arg is less than second arg, zero for equal, greater than zero if
1743 * first arg is greater than second arg).
1745 * If two array elements compare equal, their order in the sorted array
1746 * is undefined. If you want equal elements to keep their order (i.e.
1747 * you want a stable sort) you can write a comparison function that,
1748 * if two elements would otherwise compare equal, compares them by
1752 g_byte_array_sort (GByteArray *array,
1753 GCompareFunc compare_func)
1755 g_array_sort ((GArray *) array, compare_func);
1759 * g_byte_array_sort_with_data:
1760 * @array: a #GByteArray.
1761 * @compare_func: comparison function.
1762 * @user_data: data to pass to @compare_func.
1764 * Like g_byte_array_sort(), but the comparison function takes an extra
1765 * user data argument.
1768 g_byte_array_sort_with_data (GByteArray *array,
1769 GCompareDataFunc compare_func,
1772 g_array_sort_with_data ((GArray *) array, compare_func, user_data);