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;
205 if (array->zero_terminated || reserved_size != 0)
207 g_array_maybe_expand (array, reserved_size);
208 g_array_zero_terminate(array);
211 return (GArray*) array;
215 * g_array_set_clear_func:
217 * @clear_func: a function to clear an element of @array
219 * Sets a function to clear an element of @array.
221 * The @clear_func will be called when an element in the array
222 * data segment is removed and when the array is freed and data
223 * segment is deallocated as well.
225 * Note that in contrast with other uses of #GDestroyNotify
226 * functions, @clear_func is expected to clear the contents of
227 * the array element it is given, but not free the element itself.
232 g_array_set_clear_func (GArray *array,
233 GDestroyNotify clear_func)
235 GRealArray *rarray = (GRealArray *) array;
237 g_return_if_fail (array != NULL);
239 rarray->clear_func = clear_func;
246 * Atomically increments the reference count of @array by one. This
247 * function is MT-safe and may be called from any thread.
249 * Returns: The passed in #GArray.
254 g_array_ref (GArray *array)
256 GRealArray *rarray = (GRealArray*) array;
257 g_return_val_if_fail (array, NULL);
259 g_atomic_int_inc (&rarray->ref_count);
266 FREE_SEGMENT = 1 << 0,
267 PRESERVE_WRAPPER = 1 << 1
270 static gchar *array_free (GRealArray *, ArrayFreeFlags);
276 * Atomically decrements the reference count of @array by one. If the
277 * reference count drops to 0, all memory allocated by the array is
278 * released. This function is MT-safe and may be called from any
284 g_array_unref (GArray *array)
286 GRealArray *rarray = (GRealArray*) array;
287 g_return_if_fail (array);
289 if (g_atomic_int_dec_and_test (&rarray->ref_count))
290 array_free (rarray, FREE_SEGMENT);
294 * g_array_get_element_size:
297 * Gets the size of the elements in @array.
299 * Returns: Size of each element, in bytes.
304 g_array_get_element_size (GArray *array)
306 GRealArray *rarray = (GRealArray*) array;
308 g_return_val_if_fail (array, 0);
310 return rarray->elt_size;
316 * @free_segment: if %TRUE the actual element data is freed as well.
317 * @Returns: the element data if @free_segment is %FALSE, otherwise
318 * %NULL. The element data should be freed using g_free().
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>
332 g_array_free (GArray *farray,
333 gboolean free_segment)
335 GRealArray *array = (GRealArray*) farray;
336 ArrayFreeFlags flags;
338 g_return_val_if_fail (array, NULL);
340 flags = (free_segment ? FREE_SEGMENT : 0);
342 /* if others are holding a reference, preserve the wrapper but do free/return the data */
343 if (!g_atomic_int_dec_and_test (&array->ref_count))
344 flags |= PRESERVE_WRAPPER;
346 return array_free (array, flags);
350 array_free (GRealArray *array,
351 ArrayFreeFlags flags)
355 if (flags & FREE_SEGMENT)
357 if (array->clear_func != NULL)
361 for (i = 0; i < array->len; i++)
362 array->clear_func (g_array_elt_pos (array, i));
365 g_free (array->data);
369 segment = (gchar*) array->data;
371 if (flags & PRESERVE_WRAPPER)
379 g_slice_free1 (sizeof (GRealArray), array);
386 * g_array_append_vals:
388 * @data: a pointer to the elements to append to the end of the array.
389 * @len: the number of elements to append.
390 * @Returns: the #GArray.
392 * Adds @len elements onto the end of the array.
395 * g_array_append_val:
397 * @v: the value to append to the #GArray.
398 * @Returns: the #GArray.
400 * Adds the value on to the end of the array. The array will grow in
401 * size automatically if necessary.
403 * <note><para>g_array_append_val() is a macro which uses a reference
404 * to the value parameter @v. This means that you cannot use it with
405 * literal values such as "27". You must use variables.</para></note>
408 g_array_append_vals (GArray *farray,
412 GRealArray *array = (GRealArray*) farray;
414 g_return_val_if_fail (array, NULL);
416 g_array_maybe_expand (array, len);
418 memcpy (g_array_elt_pos (array, array->len), data,
419 g_array_elt_len (array, len));
423 g_array_zero_terminate (array);
429 * g_array_prepend_vals:
431 * @data: a pointer to the elements to prepend to the start of the
433 * @len: the number of elements to prepend.
434 * @Returns: the #GArray.
436 * Adds @len elements onto the start of the array.
438 * This operation is slower than g_array_append_vals() since the
439 * existing elements in the array have to be moved to make space for
443 * g_array_prepend_val:
445 * @v: the value to prepend to the #GArray.
446 * @Returns: the #GArray.
448 * Adds the value on to the start of the array. The array will grow in
449 * size automatically if necessary.
451 * This operation is slower than g_array_append_val() since the
452 * existing elements in the array have to be moved to make space for
455 * <note><para>g_array_prepend_val() is a macro which uses a reference
456 * to the value parameter @v. This means that you cannot use it with
457 * literal values such as "27". You must use variables.</para></note>
460 g_array_prepend_vals (GArray *farray,
464 GRealArray *array = (GRealArray*) farray;
466 g_return_val_if_fail (array, NULL);
468 g_array_maybe_expand (array, len);
470 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
471 g_array_elt_len (array, array->len));
473 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
477 g_array_zero_terminate (array);
483 * g_array_insert_vals:
485 * @index_: the index to place the elements at.
486 * @data: a pointer to the elements to insert.
487 * @len: the number of elements to insert.
488 * @Returns: the #GArray.
490 * Inserts @len elements into a #GArray at the given index.
493 * g_array_insert_val:
495 * @i: the index to place the element at.
496 * @v: the value to insert into the array.
497 * @Returns: the #GArray.
499 * Inserts an element into an array at the given index.
501 * <note><para>g_array_insert_val() is a macro which uses a reference
502 * to the value parameter @v. This means that you cannot use it with
503 * literal values such as "27". You must use variables.</para></note>
506 g_array_insert_vals (GArray *farray,
511 GRealArray *array = (GRealArray*) farray;
513 g_return_val_if_fail (array, NULL);
515 g_array_maybe_expand (array, len);
517 g_memmove (g_array_elt_pos (array, len + index_),
518 g_array_elt_pos (array, index_),
519 g_array_elt_len (array, array->len - index_));
521 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
525 g_array_zero_terminate (array);
533 * @length: the new size of the #GArray.
534 * @Returns: the #GArray.
536 * Sets the size of the array, expanding it if necessary. If the array
537 * was created with @clear_ set to %TRUE, the new elements are set to 0.
540 g_array_set_size (GArray *farray,
543 GRealArray *array = (GRealArray*) farray;
545 g_return_val_if_fail (array, NULL);
547 if (length > array->len)
549 g_array_maybe_expand (array, length - array->len);
552 g_array_elt_zero (array, array->len, length - array->len);
554 else if (length < array->len)
555 g_array_remove_range (farray, length, array->len - length);
559 g_array_zero_terminate (array);
565 * g_array_remove_index:
567 * @index_: the index of the element to remove.
568 * @Returns: the #GArray.
570 * Removes the element at the given index from a #GArray. The following
571 * elements are moved down one place.
574 g_array_remove_index (GArray *farray,
577 GRealArray* array = (GRealArray*) farray;
579 g_return_val_if_fail (array, NULL);
581 g_return_val_if_fail (index_ < array->len, NULL);
583 if (array->clear_func != NULL)
584 array->clear_func (g_array_elt_pos (array, index_));
586 if (index_ != array->len - 1)
587 g_memmove (g_array_elt_pos (array, index_),
588 g_array_elt_pos (array, index_ + 1),
589 g_array_elt_len (array, array->len - index_ - 1));
593 if (G_UNLIKELY (g_mem_gc_friendly))
594 g_array_elt_zero (array, array->len, 1);
596 g_array_zero_terminate (array);
602 * g_array_remove_index_fast:
604 * @index_: the index of the element to remove.
605 * @Returns: the #GArray.
607 * Removes the element at the given index from a #GArray. The last
608 * element in the array is used to fill in the space, so this function
609 * does not preserve the order of the #GArray. But it is faster than
610 * g_array_remove_index().
613 g_array_remove_index_fast (GArray *farray,
616 GRealArray* array = (GRealArray*) farray;
618 g_return_val_if_fail (array, NULL);
620 g_return_val_if_fail (index_ < array->len, NULL);
622 if (array->clear_func != NULL)
623 array->clear_func (g_array_elt_pos (array, index_));
625 if (index_ != array->len - 1)
626 memcpy (g_array_elt_pos (array, index_),
627 g_array_elt_pos (array, array->len - 1),
628 g_array_elt_len (array, 1));
632 if (G_UNLIKELY (g_mem_gc_friendly))
633 g_array_elt_zero (array, array->len, 1);
635 g_array_zero_terminate (array);
641 * g_array_remove_range:
643 * @index_: the index of the first element to remove.
644 * @length: the number of elements to remove.
645 * @Returns: the #GArray.
647 * Removes the given number of elements starting at the given index
648 * from a #GArray. The following elements are moved to close the gap.
653 g_array_remove_range (GArray *farray,
657 GRealArray *array = (GRealArray*) farray;
659 g_return_val_if_fail (array, NULL);
660 g_return_val_if_fail (index_ < array->len, NULL);
661 g_return_val_if_fail (index_ + length <= array->len, NULL);
663 if (array->clear_func != NULL)
667 for (i = 0; i < length; i++)
668 array->clear_func (g_array_elt_pos (array, index_ + i));
671 if (index_ + length != array->len)
672 g_memmove (g_array_elt_pos (array, index_),
673 g_array_elt_pos (array, index_ + length),
674 (array->len - (index_ + length)) * array->elt_size);
676 array->len -= length;
677 if (G_UNLIKELY (g_mem_gc_friendly))
678 g_array_elt_zero (array, array->len, length);
680 g_array_zero_terminate (array);
688 * @compare_func: comparison function.
690 * Sorts a #GArray using @compare_func which should be a qsort()-style
691 * comparison function (returns less than zero for first arg is less
692 * than second arg, zero for equal, greater zero if first arg is
693 * greater than second arg).
695 * If two array elements compare equal, their order in the sorted array
696 * is undefined. If you want equal elements to keep their order (i.e.
697 * you want a stable sort) you can write a comparison function that,
698 * if two elements would otherwise compare equal, compares them by
702 g_array_sort (GArray *farray,
703 GCompareFunc compare_func)
705 GRealArray *array = (GRealArray*) farray;
707 g_return_if_fail (array != NULL);
716 * g_array_sort_with_data:
718 * @compare_func: comparison function.
719 * @user_data: data to pass to @compare_func.
721 * Like g_array_sort(), but the comparison function receives an extra
722 * user data argument.
725 g_array_sort_with_data (GArray *farray,
726 GCompareDataFunc compare_func,
729 GRealArray *array = (GRealArray*) farray;
731 g_return_if_fail (array != NULL);
733 g_qsort_with_data (array->data,
740 /* Returns the smallest power of 2 greater than n, or n if
741 * such power does not fit in a guint
744 g_nearest_pow (gint num)
748 while (n < num && n > 0)
755 g_array_maybe_expand (GRealArray *array,
758 guint want_alloc = g_array_elt_len (array, array->len + len +
759 array->zero_terminated);
761 if (want_alloc > array->alloc)
763 want_alloc = g_nearest_pow (want_alloc);
764 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
766 array->data = g_realloc (array->data, want_alloc);
768 if (G_UNLIKELY (g_mem_gc_friendly))
769 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
771 array->alloc = want_alloc;
776 * SECTION:arrays_pointer
777 * @title: Pointer Arrays
778 * @short_description: arrays of pointers to any type of data, which
779 * grow automatically as new elements are added
781 * Pointer Arrays are similar to Arrays but are used only for storing
784 * <note><para>If you remove elements from the array, elements at the
785 * end of the array are moved into the space previously occupied by the
786 * removed element. This means that you should not rely on the index of
787 * particular elements remaining the same. You should also be careful
788 * when deleting elements while iterating over the array.</para></note>
790 * To create a pointer array, use g_ptr_array_new().
792 * To add elements to a pointer array, use g_ptr_array_add().
794 * To remove elements from a pointer array, use g_ptr_array_remove(),
795 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
797 * To access an element of a pointer array, use g_ptr_array_index().
799 * To set the size of a pointer array, use g_ptr_array_set_size().
801 * To free a pointer array, use g_ptr_array_free().
804 * <title>Using a #GPtrArray</title>
806 * GPtrArray *gparray;
807 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
809 * gparray = g_ptr_array_new (<!-- -->);
810 * g_ptr_array_add (gparray, (gpointer) string1);
811 * g_ptr_array_add (gparray, (gpointer) string2);
812 * g_ptr_array_add (gparray, (gpointer) string3);
814 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
815 * g_print ("ERROR: got %p instead of %p\n",
816 * g_ptr_array_index (gparray, 0), string1);
818 * g_ptr_array_free (gparray, TRUE);
823 typedef struct _GRealPtrArray GRealPtrArray;
827 * @pdata: points to the array of pointers, which may be moved when the
829 * @len: number of pointers in the array.
831 * Contains the public fields of a pointer array.
833 struct _GRealPtrArray
839 GDestroyNotify element_free_func;
844 * @array: a #GPtrArray.
845 * @index_: the index of the pointer to return.
846 * @Returns: the pointer at the given index.
848 * Returns the pointer at the given index of the pointer array.
851 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
856 * @Returns: the new #GPtrArray.
858 * Creates a new #GPtrArray with a reference count of 1.
861 g_ptr_array_new (void)
863 return g_ptr_array_sized_new (0);
867 * g_ptr_array_sized_new:
868 * @reserved_size: number of pointers preallocated.
869 * @Returns: the new #GPtrArray.
871 * Creates a new #GPtrArray with @reserved_size pointers preallocated
872 * and a reference count of 1. This avoids frequent reallocation, if
873 * you are going to add many pointers to the array. Note however that
874 * the size of the array is still 0.
877 g_ptr_array_sized_new (guint reserved_size)
879 GRealPtrArray *array = g_slice_new (GRealPtrArray);
884 array->ref_count = 1;
885 array->element_free_func = NULL;
887 if (reserved_size != 0)
888 g_ptr_array_maybe_expand (array, reserved_size);
890 return (GPtrArray*) array;
894 * g_ptr_array_new_with_free_func:
895 * @element_free_func: A function to free elements with destroy @array or %NULL.
897 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
898 * for freeing each element when the array is destroyed either via
899 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
900 * set to %TRUE or when removing elements.
902 * Returns: A new #GPtrArray.
907 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
911 array = g_ptr_array_new ();
912 g_ptr_array_set_free_func (array, element_free_func);
917 * g_ptr_array_new_full:
918 * @reserved_size: number of pointers preallocated.
919 * @element_free_func: A function to free elements with destroy @array or %NULL.
921 * Creates a new #GPtrArray with @reserved_size pointers preallocated
922 * and a reference count of 1. This avoids frequent reallocation, if
923 * you are going to add many pointers to the array. Note however that
924 * the size of the array is still 0. It also set @element_free_func
925 * for freeing each element when the array is destroyed either via
926 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
927 * set to %TRUE or when removing elements.
929 * Returns: A new #GPtrArray.
934 g_ptr_array_new_full (guint reserved_size,
935 GDestroyNotify element_free_func)
939 array = g_ptr_array_sized_new (reserved_size);
940 g_ptr_array_set_free_func (array, element_free_func);
945 * g_ptr_array_set_free_func:
946 * @array: A #GPtrArray.
947 * @element_free_func: A function to free elements with destroy @array or %NULL.
949 * Sets a function for freeing each element when @array is destroyed
950 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
951 * with @free_segment set to %TRUE or when removing elements.
956 g_ptr_array_set_free_func (GPtrArray *array,
957 GDestroyNotify element_free_func)
959 GRealPtrArray* rarray = (GRealPtrArray*) array;
961 g_return_if_fail (array);
963 rarray->element_free_func = element_free_func;
970 * Atomically increments the reference count of @array by one. This
971 * function is MT-safe and may be called from any thread.
973 * Returns: The passed in #GPtrArray.
978 g_ptr_array_ref (GPtrArray *array)
980 GRealPtrArray *rarray = (GRealPtrArray*) array;
982 g_return_val_if_fail (array, NULL);
984 g_atomic_int_inc (&rarray->ref_count);
989 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
993 * @array: A #GPtrArray.
995 * Atomically decrements the reference count of @array by one. If the
996 * reference count drops to 0, the effect is the same as calling
997 * g_ptr_array_free() with @free_segment set to %TRUE. This function
998 * is MT-safe and may be called from any thread.
1003 g_ptr_array_unref (GPtrArray *array)
1005 GRealPtrArray *rarray = (GRealPtrArray*) array;
1006 g_return_if_fail (array);
1008 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1009 ptr_array_free (array, FREE_SEGMENT);
1014 * @array: a #GPtrArray.
1015 * @free_seg: if %TRUE the actual pointer array is freed as well.
1016 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1017 * The pointer array should be freed using g_free().
1019 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1020 * it frees the memory block holding the elements as well. Pass %FALSE
1021 * if you want to free the #GPtrArray wrapper but preserve the
1022 * underlying array for use elsewhere. If the reference count of @array
1023 * is greater than one, the #GPtrArray wrapper is preserved but the
1024 * size of @array will be set to zero.
1026 * <note><para>If array contents point to dynamically-allocated
1027 * memory, they should be freed separately if @free_seg is %TRUE and no
1028 * #GDestroyNotify function has been set for @array.</para></note>
1031 g_ptr_array_free (GPtrArray *farray,
1032 gboolean free_segment)
1034 GRealPtrArray *array = (GRealPtrArray*) farray;
1035 ArrayFreeFlags flags;
1037 g_return_val_if_fail (array, NULL);
1039 flags = (free_segment ? FREE_SEGMENT : 0);
1041 /* if others are holding a reference, preserve the wrapper but do free/return the data */
1042 if (!g_atomic_int_dec_and_test (&array->ref_count))
1043 flags |= PRESERVE_WRAPPER;
1045 return ptr_array_free (farray, flags);
1049 ptr_array_free (GPtrArray *farray,
1050 ArrayFreeFlags flags)
1052 GRealPtrArray *array = (GRealPtrArray*) farray;
1055 if (flags & FREE_SEGMENT)
1057 if (array->element_free_func != NULL)
1058 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1059 g_free (array->pdata);
1063 segment = array->pdata;
1065 if (flags & PRESERVE_WRAPPER)
1067 array->pdata = NULL;
1073 g_slice_free1 (sizeof (GRealPtrArray), array);
1080 g_ptr_array_maybe_expand (GRealPtrArray *array,
1083 if ((array->len + len) > array->alloc)
1085 guint old_alloc = array->alloc;
1086 array->alloc = g_nearest_pow (array->len + len);
1087 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1088 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1089 if (G_UNLIKELY (g_mem_gc_friendly))
1090 for ( ; old_alloc < array->alloc; old_alloc++)
1091 array->pdata [old_alloc] = NULL;
1096 * g_ptr_array_set_size:
1097 * @array: a #GPtrArray.
1098 * @length: the new length of the pointer array.
1100 * Sets the size of the array. When making the array larger,
1101 * newly-added elements will be set to %NULL. When making it smaller,
1102 * if @array has a non-%NULL #GDestroyNotify function then it will be
1103 * called for the removed elements.
1106 g_ptr_array_set_size (GPtrArray *farray,
1109 GRealPtrArray* array = (GRealPtrArray*) farray;
1111 g_return_if_fail (array);
1113 if (length > array->len)
1116 g_ptr_array_maybe_expand (array, (length - array->len));
1118 * memset (array->pdata + array->len, 0,
1119 * sizeof (gpointer) * (length - array->len));
1120 * to make it really portable. Remember (void*)NULL needn't be
1121 * bitwise zero. It of course is silly not to use memset (..,0,..).
1123 for (i = array->len; i < length; i++)
1124 array->pdata[i] = NULL;
1126 else if (length < array->len)
1127 g_ptr_array_remove_range (farray, length, array->len - length);
1129 array->len = length;
1133 * g_ptr_array_remove_index:
1134 * @array: a #GPtrArray.
1135 * @index_: the index of the pointer to remove.
1136 * @Returns: the pointer which was removed.
1138 * Removes the pointer at the given index from the pointer array. The
1139 * following elements are moved down one place. If @array has a
1140 * non-%NULL #GDestroyNotify function it is called for the removed
1144 g_ptr_array_remove_index (GPtrArray *farray,
1147 GRealPtrArray* array = (GRealPtrArray*) farray;
1150 g_return_val_if_fail (array, NULL);
1152 g_return_val_if_fail (index_ < array->len, NULL);
1154 result = array->pdata[index_];
1156 if (array->element_free_func != NULL)
1157 array->element_free_func (array->pdata[index_]);
1159 if (index_ != array->len - 1)
1160 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1161 sizeof (gpointer) * (array->len - index_ - 1));
1165 if (G_UNLIKELY (g_mem_gc_friendly))
1166 array->pdata[array->len] = NULL;
1172 * g_ptr_array_remove_index_fast:
1173 * @array: a #GPtrArray.
1174 * @index_: the index of the pointer to remove.
1175 * @Returns: the pointer which was removed.
1177 * Removes the pointer at the given index from the pointer array. The
1178 * last element in the array is used to fill in the space, so this
1179 * function does not preserve the order of the array. But it is faster
1180 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1181 * #GDestroyNotify function it is called for the removed element.
1184 g_ptr_array_remove_index_fast (GPtrArray *farray,
1187 GRealPtrArray* array = (GRealPtrArray*) farray;
1190 g_return_val_if_fail (array, NULL);
1192 g_return_val_if_fail (index_ < array->len, NULL);
1194 result = array->pdata[index_];
1196 if (array->element_free_func != NULL)
1197 array->element_free_func (array->pdata[index_]);
1199 if (index_ != array->len - 1)
1200 array->pdata[index_] = array->pdata[array->len - 1];
1204 if (G_UNLIKELY (g_mem_gc_friendly))
1205 array->pdata[array->len] = NULL;
1211 * g_ptr_array_remove_range:
1212 * @array: a @GPtrArray.
1213 * @index_: the index of the first pointer to remove.
1214 * @length: the number of pointers to remove.
1216 * Removes the given number of pointers starting at the given index
1217 * from a #GPtrArray. The following elements are moved to close the
1218 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1219 * for the removed elements.
1224 g_ptr_array_remove_range (GPtrArray *farray,
1228 GRealPtrArray* array = (GRealPtrArray*) farray;
1231 g_return_if_fail (array);
1232 g_return_if_fail (index_ < array->len);
1233 g_return_if_fail (index_ + length <= array->len);
1235 if (array->element_free_func != NULL)
1237 for (n = index_; n < index_ + length; n++)
1238 array->element_free_func (array->pdata[n]);
1241 if (index_ + length != array->len)
1243 g_memmove (&array->pdata[index_],
1244 &array->pdata[index_ + length],
1245 (array->len - (index_ + length)) * sizeof (gpointer));
1248 array->len -= length;
1249 if (G_UNLIKELY (g_mem_gc_friendly))
1252 for (i = 0; i < length; i++)
1253 array->pdata[array->len + i] = NULL;
1258 * g_ptr_array_remove:
1259 * @array: a #GPtrArray.
1260 * @data: the pointer to remove.
1261 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1262 * not found in the array.
1264 * Removes the first occurrence of the given pointer from the pointer
1265 * array. The following elements are moved down one place. If @array
1266 * has a non-%NULL #GDestroyNotify function it is called for the
1269 * It returns %TRUE if the pointer was removed, or %FALSE if the
1270 * pointer was not found.
1273 g_ptr_array_remove (GPtrArray *farray,
1276 GRealPtrArray* array = (GRealPtrArray*) farray;
1279 g_return_val_if_fail (array, FALSE);
1281 for (i = 0; i < array->len; i += 1)
1283 if (array->pdata[i] == data)
1285 g_ptr_array_remove_index (farray, i);
1294 * g_ptr_array_remove_fast:
1295 * @array: a #GPtrArray.
1296 * @data: the pointer to remove.
1297 * @Returns: %TRUE if the pointer was found in the array.
1299 * Removes the first occurrence of the given pointer from the pointer
1300 * array. The last element in the array is used to fill in the space,
1301 * so this function does not preserve the order of the array. But it is
1302 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1303 * #GDestroyNotify function it is called for the removed element.
1305 * It returns %TRUE if the pointer was removed, or %FALSE if the
1306 * pointer was not found.
1309 g_ptr_array_remove_fast (GPtrArray *farray,
1312 GRealPtrArray* array = (GRealPtrArray*) farray;
1315 g_return_val_if_fail (array, FALSE);
1317 for (i = 0; i < array->len; i += 1)
1319 if (array->pdata[i] == data)
1321 g_ptr_array_remove_index_fast (farray, i);
1331 * @array: a #GPtrArray.
1332 * @data: the pointer to add.
1334 * Adds a pointer to the end of the pointer array. The array will grow
1335 * in size automatically if necessary.
1338 g_ptr_array_add (GPtrArray *farray,
1341 GRealPtrArray* array = (GRealPtrArray*) farray;
1343 g_return_if_fail (array);
1345 g_ptr_array_maybe_expand (array, 1);
1347 array->pdata[array->len++] = data;
1352 * @array: a #GPtrArray.
1353 * @compare_func: comparison function.
1355 * Sorts the array, using @compare_func which should be a qsort()-style
1356 * comparison function (returns less than zero for first arg is less
1357 * than second arg, zero for equal, greater than zero if irst arg is
1358 * greater than second arg).
1360 * If two array elements compare equal, their order in the sorted array
1361 * is undefined. If you want equal elements to keep their order (i.e.
1362 * you want a stable sort) you can write a comparison function that,
1363 * if two elements would otherwise compare equal, compares them by
1366 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1367 * take the pointers from the array as arguments, it takes pointers to
1368 * the pointers in the array.</para></note>
1371 g_ptr_array_sort (GPtrArray *array,
1372 GCompareFunc compare_func)
1374 g_return_if_fail (array != NULL);
1376 qsort (array->pdata,
1383 * g_ptr_array_sort_with_data:
1384 * @array: a #GPtrArray.
1385 * @compare_func: comparison function.
1386 * @user_data: data to pass to @compare_func.
1388 * Like g_ptr_array_sort(), but the comparison function has an extra
1389 * user data argument.
1391 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1392 * doesn't take the pointers from the array as arguments, it takes
1393 * pointers to the pointers in the array.</para></note>
1396 g_ptr_array_sort_with_data (GPtrArray *array,
1397 GCompareDataFunc compare_func,
1400 g_return_if_fail (array != NULL);
1402 g_qsort_with_data (array->pdata,
1410 * g_ptr_array_foreach:
1411 * @array: a #GPtrArray
1412 * @func: the function to call for each array element
1413 * @user_data: user data to pass to the function
1415 * Calls a function for each element of a #GPtrArray.
1420 g_ptr_array_foreach (GPtrArray *array,
1426 g_return_if_fail (array);
1428 for (i = 0; i < array->len; i++)
1429 (*func) (array->pdata[i], user_data);
1433 * SECTION:arrays_byte
1434 * @title: Byte Arrays
1435 * @short_description: arrays of bytes
1437 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1438 * of bytes which grow automatically as elements are added.
1440 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1441 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1443 * To set the size of a #GByteArray, use g_byte_array_set_size().
1445 * To free a #GByteArray, use g_byte_array_free().
1448 * <title>Using a #GByteArray</title>
1450 * GByteArray *gbarray;
1453 * gbarray = g_byte_array_new (<!-- -->);
1454 * for (i = 0; i < 10000; i++)
1455 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1457 * for (i = 0; i < 10000; i++)
1459 * g_assert (gbarray->data[4*i] == 'a');
1460 * g_assert (gbarray->data[4*i+1] == 'b');
1461 * g_assert (gbarray->data[4*i+2] == 'c');
1462 * g_assert (gbarray->data[4*i+3] == 'd');
1465 * g_byte_array_free (gbarray, TRUE);
1469 * See #GBytes if you are interested in an immutable object representing a
1470 * sequence of bytes.
1475 * @data: a pointer to the element data. The data may be moved as
1476 * elements are added to the #GByteArray.
1477 * @len: the number of elements in the #GByteArray.
1479 * The <structname>GByteArray</structname> struct allows access to the
1480 * public fields of a <structname>GByteArray</structname>.
1485 * @Returns: the new #GByteArray.
1487 * Creates a new #GByteArray with a reference count of 1.
1489 GByteArray* g_byte_array_new (void)
1491 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1495 * g_byte_array_new_take:
1496 * @data: (array length=len): byte data for the array
1497 * @len: length of @data
1499 * Create byte array containing the data. The data will be owned by the array
1500 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1504 * Returns: (transfer full): a new #GByteArray
1507 g_byte_array_new_take (guint8 *data,
1513 array = g_byte_array_new ();
1514 real = (GRealArray *)array;
1515 g_assert (real->data == NULL);
1516 g_assert (real->len == 0);
1525 * g_byte_array_sized_new:
1526 * @reserved_size: number of bytes preallocated.
1527 * @Returns: the new #GByteArray.
1529 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1530 * This avoids frequent reallocation, if you are going to add many
1531 * bytes to the array. Note however that the size of the array is still
1534 GByteArray* g_byte_array_sized_new (guint reserved_size)
1536 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1540 * g_byte_array_free:
1541 * @array: a #GByteArray.
1542 * @free_segment: if %TRUE the actual byte data is freed as well.
1543 * @Returns: the element data if @free_segment is %FALSE, otherwise
1544 * %NULL. The element data should be freed using g_free().
1546 * Frees the memory allocated by the #GByteArray. If @free_segment is
1547 * %TRUE it frees the actual byte data. If the reference count of
1548 * @array is greater than one, the #GByteArray wrapper is preserved but
1549 * the size of @array will be set to zero.
1551 guint8* g_byte_array_free (GByteArray *array,
1552 gboolean free_segment)
1554 return (guint8*) g_array_free ((GArray*) array, free_segment);
1558 * g_byte_array_free_to_bytes:
1559 * @array: (transfer full): a #GByteArray
1561 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1563 * The #GByteArray is freed unless the reference count of @array is greater
1564 * than one, the #GByteArray wrapper is preserved but the size of @array
1565 * will be set to zero.
1567 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1572 * Returns: (transfer full): a new immutable #GBytes representing same byte
1573 * data that was in the array
1576 g_byte_array_free_to_bytes (GByteArray *array)
1580 g_return_val_if_fail (array != NULL, NULL);
1582 length = array->len;
1583 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1588 * @array: A #GByteArray.
1590 * Atomically increments the reference count of @array by one. This
1591 * function is MT-safe and may be called from any thread.
1593 * Returns: The passed in #GByteArray.
1598 g_byte_array_ref (GByteArray *array)
1600 return (GByteArray *) g_array_ref ((GArray *) array);
1604 * g_byte_array_unref:
1605 * @array: A #GByteArray.
1607 * Atomically decrements the reference count of @array by one. If the
1608 * reference count drops to 0, all memory allocated by the array is
1609 * released. This function is MT-safe and may be called from any
1615 g_byte_array_unref (GByteArray *array)
1617 g_array_unref ((GArray *) array);
1621 * g_byte_array_append:
1622 * @array: a #GByteArray.
1623 * @data: the byte data to be added.
1624 * @len: the number of bytes to add.
1625 * @Returns: the #GByteArray.
1627 * Adds the given bytes to the end of the #GByteArray. The array will
1628 * grow in size automatically if necessary.
1630 GByteArray* g_byte_array_append (GByteArray *array,
1634 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1640 * g_byte_array_prepend:
1641 * @array: a #GByteArray.
1642 * @data: the byte data to be added.
1643 * @len: the number of bytes to add.
1644 * @Returns: the #GByteArray.
1646 * Adds the given data to the start of the #GByteArray. The array will
1647 * grow in size automatically if necessary.
1649 GByteArray* g_byte_array_prepend (GByteArray *array,
1653 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1659 * g_byte_array_set_size:
1660 * @array: a #GByteArray.
1661 * @length: the new size of the #GByteArray.
1662 * @Returns: the #GByteArray.
1664 * Sets the size of the #GByteArray, expanding it if necessary.
1666 GByteArray* g_byte_array_set_size (GByteArray *array,
1669 g_array_set_size ((GArray*) array, length);
1675 * g_byte_array_remove_index:
1676 * @array: a #GByteArray.
1677 * @index_: the index of the byte to remove.
1678 * @Returns: the #GByteArray.
1680 * Removes the byte at the given index from a #GByteArray. The
1681 * following bytes are moved down one place.
1683 GByteArray* g_byte_array_remove_index (GByteArray *array,
1686 g_array_remove_index ((GArray*) array, index_);
1692 * g_byte_array_remove_index_fast:
1693 * @array: a #GByteArray.
1694 * @index_: the index of the byte to remove.
1695 * @Returns: the #GByteArray.
1697 * Removes the byte at the given index from a #GByteArray. The last
1698 * element in the array is used to fill in the space, so this function
1699 * does not preserve the order of the #GByteArray. But it is faster
1700 * than g_byte_array_remove_index().
1702 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1705 g_array_remove_index_fast ((GArray*) array, index_);
1711 * g_byte_array_remove_range:
1712 * @array: a @GByteArray.
1713 * @index_: the index of the first byte to remove.
1714 * @length: the number of bytes to remove.
1715 * @Returns: the #GByteArray.
1717 * Removes the given number of bytes starting at the given index from a
1718 * #GByteArray. The following elements are moved to close the gap.
1723 g_byte_array_remove_range (GByteArray *array,
1727 g_return_val_if_fail (array, NULL);
1728 g_return_val_if_fail (index_ < array->len, NULL);
1729 g_return_val_if_fail (index_ + length <= array->len, NULL);
1731 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1735 * g_byte_array_sort:
1736 * @array: a #GByteArray.
1737 * @compare_func: comparison function.
1739 * Sorts a byte array, using @compare_func which should be a
1740 * qsort()-style comparison function (returns less than zero for first
1741 * arg is less than second arg, zero for equal, greater than zero if
1742 * first arg is greater than second arg).
1744 * If two array elements compare equal, their order in the sorted array
1745 * is undefined. If you want equal elements to keep their order (i.e.
1746 * you want a stable sort) you can write a comparison function that,
1747 * if two elements would otherwise compare equal, compares them by
1751 g_byte_array_sort (GByteArray *array,
1752 GCompareFunc compare_func)
1754 g_array_sort ((GArray *) array, compare_func);
1758 * g_byte_array_sort_with_data:
1759 * @array: a #GByteArray.
1760 * @compare_func: comparison function.
1761 * @user_data: data to pass to @compare_func.
1763 * Like g_byte_array_sort(), but the comparison function takes an extra
1764 * user data argument.
1767 g_byte_array_sort_with_data (GByteArray *array,
1768 GCompareDataFunc compare_func,
1771 g_array_sort_with_data ((GArray *) array, compare_func, user_data);