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;
118 * @t: the type of the elements.
119 * @i: the index of the element to return.
120 * @Returns: the element of the #GArray at the index given by @i.
122 * Returns the element of a #GArray at the given index. The return
123 * value is cast to the given type.
126 * <title>Getting a pointer to an element in a #GArray</title>
128 * EDayViewEvent *event;
129 * /<!-- -->* This gets a pointer to the 4th element
130 * in the array of EDayViewEvent structs. *<!-- -->/
131 * event = &g_array_index (events, EDayViewEvent, 3);
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.
156 * @Returns: the new #GArray.
158 * Creates a new #GArray with a reference count of 1.
161 g_array_new (gboolean zero_terminated,
165 g_return_val_if_fail (elt_size > 0, NULL);
167 return g_array_sized_new (zero_terminated, clear, elt_size, 0);
172 * @zero_terminated: %TRUE if the array should have an extra element at
173 * the end with all bits cleared.
174 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
176 * @element_size: size of each element in the array.
177 * @reserved_size: number of elements preallocated.
178 * @Returns: the new #GArray.
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 GArray* g_array_sized_new (gboolean zero_terminated,
192 g_return_val_if_fail (elt_size > 0, NULL);
194 array = g_slice_new (GRealArray);
199 array->zero_terminated = (zero_terminated ? 1 : 0);
200 array->clear = (clear ? 1 : 0);
201 array->elt_size = elt_size;
202 array->ref_count = 1;
204 if (array->zero_terminated || reserved_size != 0)
206 g_array_maybe_expand (array, reserved_size);
207 g_array_zero_terminate(array);
210 return (GArray*) array;
217 * Atomically increments the reference count of @array by one. This
218 * function is MT-safe and may be called from any thread.
220 * Returns: The passed in #GArray.
225 g_array_ref (GArray *array)
227 GRealArray *rarray = (GRealArray*) array;
228 g_return_val_if_fail (array, NULL);
230 g_atomic_int_inc (&rarray->ref_count);
237 FREE_SEGMENT = 1 << 0,
238 PRESERVE_WRAPPER = 1 << 1
241 static gchar *array_free (GRealArray *, ArrayFreeFlags);
247 * Atomically decrements the reference count of @array by one. If the
248 * reference count drops to 0, all memory allocated by the array is
249 * released. This function is MT-safe and may be called from any
255 g_array_unref (GArray *array)
257 GRealArray *rarray = (GRealArray*) array;
258 g_return_if_fail (array);
260 if (g_atomic_int_dec_and_test (&rarray->ref_count))
261 array_free (rarray, FREE_SEGMENT);
265 * g_array_get_element_size:
268 * Gets the size of the elements in @array.
270 * Returns: Size of each element, in bytes.
275 g_array_get_element_size (GArray *array)
277 GRealArray *rarray = (GRealArray*) array;
279 g_return_val_if_fail (array, 0);
281 return rarray->elt_size;
287 * @free_segment: if %TRUE the actual element data is freed as well.
288 * @Returns: the element data if @free_segment is %FALSE, otherwise
289 * %NULL. The element data should be freed using g_free().
291 * Frees the memory allocated for the #GArray. If @free_segment is
292 * %TRUE it frees the memory block holding the elements as well and
293 * also each element if @array has a @element_free_func set. Pass
294 * %FALSE if you want to free the #GArray wrapper but preserve the
295 * underlying array for use elsewhere. If the reference count of @array
296 * is greater than one, the #GArray wrapper is preserved but the size
297 * of @array will be set to zero.
299 * <note><para>If array elements contain dynamically-allocated memory,
300 * they should be freed separately.</para></note>
303 g_array_free (GArray *farray,
304 gboolean free_segment)
306 GRealArray *array = (GRealArray*) farray;
307 ArrayFreeFlags flags;
309 g_return_val_if_fail (array, NULL);
311 flags = (free_segment ? FREE_SEGMENT : 0);
313 /* if others are holding a reference, preserve the wrapper but do free/return the data */
314 if (!g_atomic_int_dec_and_test (&array->ref_count))
315 flags |= PRESERVE_WRAPPER;
317 return array_free (array, flags);
321 array_free (GRealArray *array,
322 ArrayFreeFlags flags)
326 if (flags & FREE_SEGMENT)
328 g_free (array->data);
332 segment = (gchar*) array->data;
334 if (flags & PRESERVE_WRAPPER)
342 g_slice_free1 (sizeof (GRealArray), array);
349 * g_array_append_vals:
351 * @data: a pointer to the elements to append to the end of the array.
352 * @len: the number of elements to append.
353 * @Returns: the #GArray.
355 * Adds @len elements onto the end of the array.
358 * g_array_append_val:
360 * @v: the value to append to the #GArray.
361 * @Returns: the #GArray.
363 * Adds the value on to the end of the array. The array will grow in
364 * size automatically if necessary.
366 * <note><para>g_array_append_val() is a macro which uses a reference
367 * to the value parameter @v. This means that you cannot use it with
368 * literal values such as "27". You must use variables.</para></note>
371 g_array_append_vals (GArray *farray,
375 GRealArray *array = (GRealArray*) farray;
377 g_return_val_if_fail (array, NULL);
379 g_array_maybe_expand (array, len);
381 memcpy (g_array_elt_pos (array, array->len), data,
382 g_array_elt_len (array, len));
386 g_array_zero_terminate (array);
392 * g_array_prepend_vals:
394 * @data: a pointer to the elements to prepend to the start of the
396 * @len: the number of elements to prepend.
397 * @Returns: the #GArray.
399 * Adds @len elements onto the start of the array.
401 * This operation is slower than g_array_append_vals() since the
402 * existing elements in the array have to be moved to make space for
406 * g_array_prepend_val:
408 * @v: the value to prepend to the #GArray.
409 * @Returns: the #GArray.
411 * Adds the value on to the start of the array. The array will grow in
412 * size automatically if necessary.
414 * This operation is slower than g_array_append_val() since the
415 * existing elements in the array have to be moved to make space for
418 * <note><para>g_array_prepend_val() is a macro which uses a reference
419 * to the value parameter @v. This means that you cannot use it with
420 * literal values such as "27". You must use variables.</para></note>
423 g_array_prepend_vals (GArray *farray,
427 GRealArray *array = (GRealArray*) farray;
429 g_return_val_if_fail (array, NULL);
431 g_array_maybe_expand (array, len);
433 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
434 g_array_elt_len (array, array->len));
436 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
440 g_array_zero_terminate (array);
446 * g_array_insert_vals:
448 * @index_: the index to place the elements at.
449 * @data: a pointer to the elements to insert.
450 * @len: the number of elements to insert.
451 * @Returns: the #GArray.
453 * Inserts @len elements into a #GArray at the given index.
456 * g_array_insert_val:
458 * @i: the index to place the element at.
459 * @v: the value to insert into the array.
460 * @Returns: the #GArray.
462 * Inserts an element into an array at the given index.
464 * <note><para>g_array_insert_val() is a macro which uses a reference
465 * to the value parameter @v. This means that you cannot use it with
466 * literal values such as "27". You must use variables.</para></note>
469 g_array_insert_vals (GArray *farray,
474 GRealArray *array = (GRealArray*) farray;
476 g_return_val_if_fail (array, NULL);
478 g_array_maybe_expand (array, len);
480 g_memmove (g_array_elt_pos (array, len + index_),
481 g_array_elt_pos (array, index_),
482 g_array_elt_len (array, array->len - index_));
484 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
488 g_array_zero_terminate (array);
496 * @length: the new size of the #GArray.
497 * @Returns: the #GArray.
499 * Sets the size of the array, expanding it if necessary. If the array
500 * was created with @clear_ set to %TRUE, the new elements are set to 0.
503 g_array_set_size (GArray *farray,
506 GRealArray *array = (GRealArray*) farray;
508 g_return_val_if_fail (array, NULL);
510 if (length > array->len)
512 g_array_maybe_expand (array, length - array->len);
515 g_array_elt_zero (array, array->len, length - array->len);
517 else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
518 g_array_elt_zero (array, length, array->len - length);
522 g_array_zero_terminate (array);
528 * g_array_remove_index:
530 * @index_: the index of the element to remove.
531 * @Returns: the #GArray.
533 * Removes the element at the given index from a #GArray. The following
534 * elements are moved down one place.
537 g_array_remove_index (GArray *farray,
540 GRealArray* array = (GRealArray*) farray;
542 g_return_val_if_fail (array, NULL);
544 g_return_val_if_fail (index_ < array->len, NULL);
546 if (index_ != array->len - 1)
547 g_memmove (g_array_elt_pos (array, index_),
548 g_array_elt_pos (array, index_ + 1),
549 g_array_elt_len (array, array->len - index_ - 1));
553 if (G_UNLIKELY (g_mem_gc_friendly))
554 g_array_elt_zero (array, array->len, 1);
556 g_array_zero_terminate (array);
562 * g_array_remove_index_fast:
564 * @index_: the index of the element to remove.
565 * @Returns: the #GArray.
567 * Removes the element at the given index from a #GArray. The last
568 * element in the array is used to fill in the space, so this function
569 * does not preserve the order of the #GArray. But it is faster than
570 * g_array_remove_index().
573 g_array_remove_index_fast (GArray *farray,
576 GRealArray* array = (GRealArray*) farray;
578 g_return_val_if_fail (array, NULL);
580 g_return_val_if_fail (index_ < array->len, NULL);
582 if (index_ != array->len - 1)
583 memcpy (g_array_elt_pos (array, index_),
584 g_array_elt_pos (array, array->len - 1),
585 g_array_elt_len (array, 1));
589 if (G_UNLIKELY (g_mem_gc_friendly))
590 g_array_elt_zero (array, array->len, 1);
592 g_array_zero_terminate (array);
598 * g_array_remove_range:
600 * @index_: the index of the first element to remove.
601 * @length: the number of elements to remove.
602 * @Returns: the #GArray.
604 * Removes the given number of elements starting at the given index
605 * from a #GArray. The following elements are moved to close the gap.
610 g_array_remove_range (GArray *farray,
614 GRealArray *array = (GRealArray*) farray;
616 g_return_val_if_fail (array, NULL);
617 g_return_val_if_fail (index_ < array->len, NULL);
618 g_return_val_if_fail (index_ + length <= array->len, NULL);
620 if (index_ + length != array->len)
621 g_memmove (g_array_elt_pos (array, index_),
622 g_array_elt_pos (array, index_ + length),
623 (array->len - (index_ + length)) * array->elt_size);
625 array->len -= length;
626 if (G_UNLIKELY (g_mem_gc_friendly))
627 g_array_elt_zero (array, array->len, length);
629 g_array_zero_terminate (array);
637 * @compare_func: comparison function.
639 * Sorts a #GArray using @compare_func which should be a qsort()-style
640 * comparison function (returns less than zero for first arg is less
641 * than second arg, zero for equal, greater zero if first arg is
642 * greater than second arg).
644 * If two array elements compare equal, their order in the sorted array
645 * is undefined. If you want equal elements to keep their order (i.e.
646 * you want a stable sort) you can write a comparison function that,
647 * if two elements would otherwise compare equal, compares them by
651 g_array_sort (GArray *farray,
652 GCompareFunc compare_func)
654 GRealArray *array = (GRealArray*) farray;
656 g_return_if_fail (array != NULL);
665 * g_array_sort_with_data:
667 * @compare_func: comparison function.
668 * @user_data: data to pass to @compare_func.
670 * Like g_array_sort(), but the comparison function receives an extra
671 * user data argument.
674 g_array_sort_with_data (GArray *farray,
675 GCompareDataFunc compare_func,
678 GRealArray *array = (GRealArray*) farray;
680 g_return_if_fail (array != NULL);
682 g_qsort_with_data (array->data,
689 /* Returns the smallest power of 2 greater than n, or n if
690 * such power does not fit in a guint
693 g_nearest_pow (gint num)
697 while (n < num && n > 0)
704 g_array_maybe_expand (GRealArray *array,
707 guint want_alloc = g_array_elt_len (array, array->len + len +
708 array->zero_terminated);
710 if (want_alloc > array->alloc)
712 want_alloc = g_nearest_pow (want_alloc);
713 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
715 array->data = g_realloc (array->data, want_alloc);
717 if (G_UNLIKELY (g_mem_gc_friendly))
718 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
720 array->alloc = want_alloc;
725 * SECTION:arrays_pointer
726 * @title: Pointer Arrays
727 * @short_description: arrays of pointers to any type of data, which
728 * grow automatically as new elements are added
730 * Pointer Arrays are similar to Arrays but are used only for storing
733 * <note><para>If you remove elements from the array, elements at the
734 * end of the array are moved into the space previously occupied by the
735 * removed element. This means that you should not rely on the index of
736 * particular elements remaining the same. You should also be careful
737 * when deleting elements while iterating over the array.</para></note>
739 * To create a pointer array, use g_ptr_array_new().
741 * To add elements to a pointer array, use g_ptr_array_add().
743 * To remove elements from a pointer array, use g_ptr_array_remove(),
744 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
746 * To access an element of a pointer array, use g_ptr_array_index().
748 * To set the size of a pointer array, use g_ptr_array_set_size().
750 * To free a pointer array, use g_ptr_array_free().
753 * <title>Using a #GPtrArray</title>
755 * GPtrArray *gparray;
756 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
758 * gparray = g_ptr_array_new (<!-- -->);
759 * g_ptr_array_add (gparray, (gpointer) string1);
760 * g_ptr_array_add (gparray, (gpointer) string2);
761 * g_ptr_array_add (gparray, (gpointer) string3);
763 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
764 * g_print ("ERROR: got %p instead of %p\n",
765 * g_ptr_array_index (gparray, 0), string1);
767 * g_ptr_array_free (gparray, TRUE);
772 typedef struct _GRealPtrArray GRealPtrArray;
776 * @pdata: points to the array of pointers, which may be moved when the
778 * @len: number of pointers in the array.
780 * Contains the public fields of a pointer array.
782 struct _GRealPtrArray
788 GDestroyNotify element_free_func;
793 * @array: a #GPtrArray.
794 * @index_: the index of the pointer to return.
795 * @Returns: the pointer at the given index.
797 * Returns the pointer at the given index of the pointer array.
800 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
805 * @Returns: the new #GPtrArray.
807 * Creates a new #GPtrArray with a reference count of 1.
810 g_ptr_array_new (void)
812 return g_ptr_array_sized_new (0);
816 * g_ptr_array_sized_new:
817 * @reserved_size: number of pointers preallocated.
818 * @Returns: the new #GPtrArray.
820 * Creates a new #GPtrArray with @reserved_size pointers preallocated
821 * and a reference count of 1. This avoids frequent reallocation, if
822 * you are going to add many pointers to the array. Note however that
823 * the size of the array is still 0.
826 g_ptr_array_sized_new (guint reserved_size)
828 GRealPtrArray *array = g_slice_new (GRealPtrArray);
833 array->ref_count = 1;
834 array->element_free_func = NULL;
836 if (reserved_size != 0)
837 g_ptr_array_maybe_expand (array, reserved_size);
839 return (GPtrArray*) array;
843 * g_ptr_array_new_with_free_func:
844 * @element_free_func: A function to free elements with destroy @array or %NULL.
846 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
847 * for freeing each element when the array is destroyed either via
848 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
849 * set to %TRUE or when removing elements.
851 * Returns: A new #GPtrArray.
856 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
860 array = g_ptr_array_new ();
861 g_ptr_array_set_free_func (array, element_free_func);
866 * g_ptr_array_new_full:
867 * @reserved_size: number of pointers preallocated.
868 * @element_free_func: A function to free elements with destroy @array or %NULL.
870 * Creates a new #GPtrArray with @reserved_size pointers preallocated
871 * and a reference count of 1. This avoids frequent reallocation, if
872 * you are going to add many pointers to the array. Note however that
873 * the size of the array is still 0. It also set @element_free_func
874 * for freeing each element when the array is destroyed either via
875 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
876 * set to %TRUE or when removing elements.
878 * Returns: A new #GPtrArray.
883 g_ptr_array_new_full (guint reserved_size,
884 GDestroyNotify element_free_func)
888 array = g_ptr_array_sized_new (reserved_size);
889 g_ptr_array_set_free_func (array, element_free_func);
894 * g_ptr_array_set_free_func:
895 * @array: A #GPtrArray.
896 * @element_free_func: A function to free elements with destroy @array or %NULL.
898 * Sets a function for freeing each element when @array is destroyed
899 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
900 * with @free_segment set to %TRUE or when removing elements.
905 g_ptr_array_set_free_func (GPtrArray *array,
906 GDestroyNotify element_free_func)
908 GRealPtrArray* rarray = (GRealPtrArray*) array;
910 g_return_if_fail (array);
912 rarray->element_free_func = element_free_func;
919 * Atomically increments the reference count of @array by one. This
920 * function is MT-safe and may be called from any thread.
922 * Returns: The passed in #GPtrArray.
927 g_ptr_array_ref (GPtrArray *array)
929 GRealPtrArray *rarray = (GRealPtrArray*) array;
931 g_return_val_if_fail (array, NULL);
933 g_atomic_int_inc (&rarray->ref_count);
938 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
942 * @array: A #GPtrArray.
944 * Atomically decrements the reference count of @array by one. If the
945 * reference count drops to 0, the effect is the same as calling
946 * g_ptr_array_free() with @free_segment set to %TRUE. This function
947 * is MT-safe and may be called from any thread.
952 g_ptr_array_unref (GPtrArray *array)
954 GRealPtrArray *rarray = (GRealPtrArray*) array;
955 g_return_if_fail (array);
957 if (g_atomic_int_dec_and_test (&rarray->ref_count))
958 ptr_array_free (array, FREE_SEGMENT);
963 * @array: a #GPtrArray.
964 * @free_seg: if %TRUE the actual pointer array is freed as well.
965 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
966 * The pointer array should be freed using g_free().
968 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
969 * it frees the memory block holding the elements as well. Pass %FALSE
970 * if you want to free the #GPtrArray wrapper but preserve the
971 * underlying array for use elsewhere. If the reference count of @array
972 * is greater than one, the #GPtrArray wrapper is preserved but the
973 * size of @array will be set to zero.
975 * <note><para>If array contents point to dynamically-allocated
976 * memory, they should be freed separately if @free_seg is %TRUE and no
977 * #GDestroyNotify function has been set for @array.</para></note>
980 g_ptr_array_free (GPtrArray *farray,
981 gboolean free_segment)
983 GRealPtrArray *array = (GRealPtrArray*) farray;
984 ArrayFreeFlags flags;
986 g_return_val_if_fail (array, NULL);
988 flags = (free_segment ? FREE_SEGMENT : 0);
990 /* if others are holding a reference, preserve the wrapper but do free/return the data */
991 if (!g_atomic_int_dec_and_test (&array->ref_count))
992 flags |= PRESERVE_WRAPPER;
994 return ptr_array_free (farray, flags);
998 ptr_array_free (GPtrArray *farray,
999 ArrayFreeFlags flags)
1001 GRealPtrArray *array = (GRealPtrArray*) farray;
1004 if (flags & FREE_SEGMENT)
1006 if (array->element_free_func != NULL)
1007 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1008 g_free (array->pdata);
1012 segment = array->pdata;
1014 if (flags & PRESERVE_WRAPPER)
1016 array->pdata = NULL;
1022 g_slice_free1 (sizeof (GRealPtrArray), array);
1029 g_ptr_array_maybe_expand (GRealPtrArray *array,
1032 if ((array->len + len) > array->alloc)
1034 guint old_alloc = array->alloc;
1035 array->alloc = g_nearest_pow (array->len + len);
1036 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1037 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1038 if (G_UNLIKELY (g_mem_gc_friendly))
1039 for ( ; old_alloc < array->alloc; old_alloc++)
1040 array->pdata [old_alloc] = NULL;
1045 * g_ptr_array_set_size:
1046 * @array: a #GPtrArray.
1047 * @length: the new length of the pointer array.
1049 * Sets the size of the array. When making the array larger,
1050 * newly-added elements will be set to %NULL. When making it smaller,
1051 * if @array has a non-%NULL #GDestroyNotify function then it will be
1052 * called for the removed elements.
1055 g_ptr_array_set_size (GPtrArray *farray,
1058 GRealPtrArray* array = (GRealPtrArray*) farray;
1060 g_return_if_fail (array);
1062 if (length > array->len)
1065 g_ptr_array_maybe_expand (array, (length - array->len));
1067 * memset (array->pdata + array->len, 0,
1068 * sizeof (gpointer) * (length - array->len));
1069 * to make it really portable. Remember (void*)NULL needn't be
1070 * bitwise zero. It of course is silly not to use memset (..,0,..).
1072 for (i = array->len; i < length; i++)
1073 array->pdata[i] = NULL;
1075 else if (length < array->len)
1076 g_ptr_array_remove_range (farray, length, array->len - length);
1078 array->len = length;
1082 * g_ptr_array_remove_index:
1083 * @array: a #GPtrArray.
1084 * @index_: the index of the pointer to remove.
1085 * @Returns: the pointer which was removed.
1087 * Removes the pointer at the given index from the pointer array. The
1088 * following elements are moved down one place. If @array has a
1089 * non-%NULL #GDestroyNotify function it is called for the removed
1093 g_ptr_array_remove_index (GPtrArray *farray,
1096 GRealPtrArray* array = (GRealPtrArray*) farray;
1099 g_return_val_if_fail (array, NULL);
1101 g_return_val_if_fail (index_ < array->len, NULL);
1103 result = array->pdata[index_];
1105 if (array->element_free_func != NULL)
1106 array->element_free_func (array->pdata[index_]);
1108 if (index_ != array->len - 1)
1109 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1110 sizeof (gpointer) * (array->len - index_ - 1));
1114 if (G_UNLIKELY (g_mem_gc_friendly))
1115 array->pdata[array->len] = NULL;
1121 * g_ptr_array_remove_index_fast:
1122 * @array: a #GPtrArray.
1123 * @index_: the index of the pointer to remove.
1124 * @Returns: the pointer which was removed.
1126 * Removes the pointer at the given index from the pointer array. The
1127 * last element in the array is used to fill in the space, so this
1128 * function does not preserve the order of the array. But it is faster
1129 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1130 * #GDestroyNotify function it is called for the removed element.
1133 g_ptr_array_remove_index_fast (GPtrArray *farray,
1136 GRealPtrArray* array = (GRealPtrArray*) farray;
1139 g_return_val_if_fail (array, NULL);
1141 g_return_val_if_fail (index_ < array->len, NULL);
1143 result = array->pdata[index_];
1145 if (array->element_free_func != NULL)
1146 array->element_free_func (array->pdata[index_]);
1148 if (index_ != array->len - 1)
1149 array->pdata[index_] = array->pdata[array->len - 1];
1153 if (G_UNLIKELY (g_mem_gc_friendly))
1154 array->pdata[array->len] = NULL;
1160 * g_ptr_array_remove_range:
1161 * @array: a @GPtrArray.
1162 * @index_: the index of the first pointer to remove.
1163 * @length: the number of pointers to remove.
1165 * Removes the given number of pointers starting at the given index
1166 * from a #GPtrArray. The following elements are moved to close the
1167 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1168 * for the removed elements.
1173 g_ptr_array_remove_range (GPtrArray *farray,
1177 GRealPtrArray* array = (GRealPtrArray*) farray;
1180 g_return_if_fail (array);
1181 g_return_if_fail (index_ < array->len);
1182 g_return_if_fail (index_ + length <= array->len);
1184 if (array->element_free_func != NULL)
1186 for (n = index_; n < index_ + length; n++)
1187 array->element_free_func (array->pdata[n]);
1190 if (index_ + length != array->len)
1192 g_memmove (&array->pdata[index_],
1193 &array->pdata[index_ + length],
1194 (array->len - (index_ + length)) * sizeof (gpointer));
1197 array->len -= length;
1198 if (G_UNLIKELY (g_mem_gc_friendly))
1201 for (i = 0; i < length; i++)
1202 array->pdata[array->len + i] = NULL;
1207 * g_ptr_array_remove:
1208 * @array: a #GPtrArray.
1209 * @data: the pointer to remove.
1210 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1211 * not found in the array.
1213 * Removes the first occurrence of the given pointer from the pointer
1214 * array. The following elements are moved down one place. If @array
1215 * has a non-%NULL #GDestroyNotify function it is called for the
1218 * It returns %TRUE if the pointer was removed, or %FALSE if the
1219 * pointer was not found.
1222 g_ptr_array_remove (GPtrArray *farray,
1225 GRealPtrArray* array = (GRealPtrArray*) farray;
1228 g_return_val_if_fail (array, FALSE);
1230 for (i = 0; i < array->len; i += 1)
1232 if (array->pdata[i] == data)
1234 g_ptr_array_remove_index (farray, i);
1243 * g_ptr_array_remove_fast:
1244 * @array: a #GPtrArray.
1245 * @data: the pointer to remove.
1246 * @Returns: %TRUE if the pointer was found in the array.
1248 * Removes the first occurrence of the given pointer from the pointer
1249 * array. The last element in the array is used to fill in the space,
1250 * so this function does not preserve the order of the array. But it is
1251 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1252 * #GDestroyNotify function it is called for the removed element.
1254 * It returns %TRUE if the pointer was removed, or %FALSE if the
1255 * pointer was not found.
1258 g_ptr_array_remove_fast (GPtrArray *farray,
1261 GRealPtrArray* array = (GRealPtrArray*) farray;
1264 g_return_val_if_fail (array, FALSE);
1266 for (i = 0; i < array->len; i += 1)
1268 if (array->pdata[i] == data)
1270 g_ptr_array_remove_index_fast (farray, i);
1280 * @array: a #GPtrArray.
1281 * @data: the pointer to add.
1283 * Adds a pointer to the end of the pointer array. The array will grow
1284 * in size automatically if necessary.
1287 g_ptr_array_add (GPtrArray *farray,
1290 GRealPtrArray* array = (GRealPtrArray*) farray;
1292 g_return_if_fail (array);
1294 g_ptr_array_maybe_expand (array, 1);
1296 array->pdata[array->len++] = data;
1301 * @array: a #GPtrArray.
1302 * @compare_func: comparison function.
1304 * Sorts the array, using @compare_func which should be a qsort()-style
1305 * comparison function (returns less than zero for first arg is less
1306 * than second arg, zero for equal, greater than zero if irst arg is
1307 * greater than second arg).
1309 * If two array elements compare equal, their order in the sorted array
1310 * is undefined. If you want equal elements to keep their order (i.e.
1311 * you want a stable sort) you can write a comparison function that,
1312 * if two elements would otherwise compare equal, compares them by
1315 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1316 * take the pointers from the array as arguments, it takes pointers to
1317 * the pointers in the array.</para></note>
1320 g_ptr_array_sort (GPtrArray *array,
1321 GCompareFunc compare_func)
1323 g_return_if_fail (array != NULL);
1325 qsort (array->pdata,
1332 * g_ptr_array_sort_with_data:
1333 * @array: a #GPtrArray.
1334 * @compare_func: comparison function.
1335 * @user_data: data to pass to @compare_func.
1337 * Like g_ptr_array_sort(), but the comparison function has an extra
1338 * user data argument.
1340 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1341 * doesn't take the pointers from the array as arguments, it takes
1342 * pointers to the pointers in the array.</para></note>
1345 g_ptr_array_sort_with_data (GPtrArray *array,
1346 GCompareDataFunc compare_func,
1349 g_return_if_fail (array != NULL);
1351 g_qsort_with_data (array->pdata,
1359 * g_ptr_array_foreach:
1360 * @array: a #GPtrArray
1361 * @func: the function to call for each array element
1362 * @user_data: user data to pass to the function
1364 * Calls a function for each element of a #GPtrArray.
1369 g_ptr_array_foreach (GPtrArray *array,
1375 g_return_if_fail (array);
1377 for (i = 0; i < array->len; i++)
1378 (*func) (array->pdata[i], user_data);
1382 * SECTION:arrays_byte
1383 * @title: Byte Arrays
1384 * @short_description: arrays of bytes
1386 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1387 * of bytes which grow automatically as elements are added.
1389 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1390 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1392 * To set the size of a #GByteArray, use g_byte_array_set_size().
1394 * To free a #GByteArray, use g_byte_array_free().
1397 * <title>Using a #GByteArray</title>
1399 * GByteArray *gbarray;
1402 * gbarray = g_byte_array_new (<!-- -->);
1403 * for (i = 0; i < 10000; i++)
1404 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1406 * for (i = 0; i < 10000; i++)
1408 * g_assert (gbarray->data[4*i] == 'a');
1409 * g_assert (gbarray->data[4*i+1] == 'b');
1410 * g_assert (gbarray->data[4*i+2] == 'c');
1411 * g_assert (gbarray->data[4*i+3] == 'd');
1414 * g_byte_array_free (gbarray, TRUE);
1418 * See #GBytes if you are interested in an immutable object representing a
1419 * sequence of bytes.
1424 * @data: a pointer to the element data. The data may be moved as
1425 * elements are added to the #GByteArray.
1426 * @len: the number of elements in the #GByteArray.
1428 * The <structname>GByteArray</structname> struct allows access to the
1429 * public fields of a <structname>GByteArray</structname>.
1434 * @Returns: the new #GByteArray.
1436 * Creates a new #GByteArray with a reference count of 1.
1438 GByteArray* g_byte_array_new (void)
1440 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1444 * g_byte_array_new_take:
1445 * @data: (array length=len): byte data for the array
1446 * @len: length of @data
1448 * Create byte array containing the data. The data will be owned by the array
1449 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1453 * Returns: (transfer full): a new #GByteArray
1456 g_byte_array_new_take (guint8 *data,
1462 array = g_byte_array_new ();
1463 real = (GRealArray *)array;
1464 g_assert (real->data == NULL);
1465 g_assert (real->len == 0);
1474 * g_byte_array_sized_new:
1475 * @reserved_size: number of bytes preallocated.
1476 * @Returns: the new #GByteArray.
1478 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1479 * This avoids frequent reallocation, if you are going to add many
1480 * bytes to the array. Note however that the size of the array is still
1483 GByteArray* g_byte_array_sized_new (guint reserved_size)
1485 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1489 * g_byte_array_free:
1490 * @array: a #GByteArray.
1491 * @free_segment: if %TRUE the actual byte data is freed as well.
1492 * @Returns: the element data if @free_segment is %FALSE, otherwise
1493 * %NULL. The element data should be freed using g_free().
1495 * Frees the memory allocated by the #GByteArray. If @free_segment is
1496 * %TRUE it frees the actual byte data. If the reference count of
1497 * @array is greater than one, the #GByteArray wrapper is preserved but
1498 * the size of @array will be set to zero.
1500 guint8* g_byte_array_free (GByteArray *array,
1501 gboolean free_segment)
1503 return (guint8*) g_array_free ((GArray*) array, free_segment);
1507 * g_byte_array_free_to_bytes:
1508 * @array: (transfer full): a #GByteArray
1510 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1512 * The #GByteArray is freed unless the reference count of @array is greater
1513 * than one, the #GByteArray wrapper is preserved but the size of @array
1514 * will be set to zero.
1516 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1521 * Returns: (transfer full): a new immutable #GBytes representing same byte
1522 * data that was in the array
1525 g_byte_array_free_to_bytes (GByteArray *array)
1529 g_return_val_if_fail (array != NULL, NULL);
1531 length = array->len;
1532 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1537 * @array: A #GByteArray.
1539 * Atomically increments the reference count of @array by one. This
1540 * function is MT-safe and may be called from any thread.
1542 * Returns: The passed in #GByteArray.
1547 g_byte_array_ref (GByteArray *array)
1549 return (GByteArray *) g_array_ref ((GArray *) array);
1553 * g_byte_array_unref:
1554 * @array: A #GByteArray.
1556 * Atomically decrements the reference count of @array by one. If the
1557 * reference count drops to 0, all memory allocated by the array is
1558 * released. This function is MT-safe and may be called from any
1564 g_byte_array_unref (GByteArray *array)
1566 g_array_unref ((GArray *) array);
1570 * g_byte_array_append:
1571 * @array: a #GByteArray.
1572 * @data: the byte data to be added.
1573 * @len: the number of bytes to add.
1574 * @Returns: the #GByteArray.
1576 * Adds the given bytes to the end of the #GByteArray. The array will
1577 * grow in size automatically if necessary.
1579 GByteArray* g_byte_array_append (GByteArray *array,
1583 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1589 * g_byte_array_prepend:
1590 * @array: a #GByteArray.
1591 * @data: the byte data to be added.
1592 * @len: the number of bytes to add.
1593 * @Returns: the #GByteArray.
1595 * Adds the given data to the start of the #GByteArray. The array will
1596 * grow in size automatically if necessary.
1598 GByteArray* g_byte_array_prepend (GByteArray *array,
1602 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1608 * g_byte_array_set_size:
1609 * @array: a #GByteArray.
1610 * @length: the new size of the #GByteArray.
1611 * @Returns: the #GByteArray.
1613 * Sets the size of the #GByteArray, expanding it if necessary.
1615 GByteArray* g_byte_array_set_size (GByteArray *array,
1618 g_array_set_size ((GArray*) array, length);
1624 * g_byte_array_remove_index:
1625 * @array: a #GByteArray.
1626 * @index_: the index of the byte to remove.
1627 * @Returns: the #GByteArray.
1629 * Removes the byte at the given index from a #GByteArray. The
1630 * following bytes are moved down one place.
1632 GByteArray* g_byte_array_remove_index (GByteArray *array,
1635 g_array_remove_index ((GArray*) array, index_);
1641 * g_byte_array_remove_index_fast:
1642 * @array: a #GByteArray.
1643 * @index_: the index of the byte to remove.
1644 * @Returns: the #GByteArray.
1646 * Removes the byte at the given index from a #GByteArray. The last
1647 * element in the array is used to fill in the space, so this function
1648 * does not preserve the order of the #GByteArray. But it is faster
1649 * than g_byte_array_remove_index().
1651 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1654 g_array_remove_index_fast ((GArray*) array, index_);
1660 * g_byte_array_remove_range:
1661 * @array: a @GByteArray.
1662 * @index_: the index of the first byte to remove.
1663 * @length: the number of bytes to remove.
1664 * @Returns: the #GByteArray.
1666 * Removes the given number of bytes starting at the given index from a
1667 * #GByteArray. The following elements are moved to close the gap.
1672 g_byte_array_remove_range (GByteArray *array,
1676 g_return_val_if_fail (array, NULL);
1677 g_return_val_if_fail (index_ < array->len, NULL);
1678 g_return_val_if_fail (index_ + length <= array->len, NULL);
1680 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1684 * g_byte_array_sort:
1685 * @array: a #GByteArray.
1686 * @compare_func: comparison function.
1688 * Sorts a byte array, using @compare_func which should be a
1689 * qsort()-style comparison function (returns less than zero for first
1690 * arg is less than second arg, zero for equal, greater than zero if
1691 * first arg is greater than second arg).
1693 * If two array elements compare equal, their order in the sorted array
1694 * is undefined. If you want equal elements to keep their order (i.e.
1695 * you want a stable sort) you can write a comparison function that,
1696 * if two elements would otherwise compare equal, compares them by
1700 g_byte_array_sort (GByteArray *array,
1701 GCompareFunc compare_func)
1703 g_array_sort ((GArray *) array, compare_func);
1707 * g_byte_array_sort_with_data:
1708 * @array: a #GByteArray.
1709 * @compare_func: comparison function.
1710 * @user_data: data to pass to @compare_func.
1712 * Like g_byte_array_sort(), but the comparison function takes an extra
1713 * user data argument.
1716 g_byte_array_sort_with_data (GByteArray *array,
1717 GCompareDataFunc compare_func,
1720 g_array_sort_with_data ((GArray *) array, compare_func, user_data);