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/.
40 #include "gmessages.h"
47 * @short_description: arrays of arbitrary elements which grow
48 * automatically as elements are added
50 * Arrays are similar to standard C arrays, except that they grow
51 * automatically as elements are added.
53 * Array elements can be of any size (though all elements of one array
54 * are the same size), and the array can be automatically cleared to
55 * '0's and zero-terminated.
57 * To create a new array use g_array_new().
59 * To add elements to an array, use g_array_append_val(),
60 * g_array_append_vals(), g_array_prepend_val(), and
61 * g_array_prepend_vals().
63 * To access an element of an array, use g_array_index().
65 * To set the size of an array, use g_array_set_size().
67 * To free an array, use g_array_free().
70 * <title>Using a #GArray to store #gint values</title>
74 * /<!-- -->* We create a new array to store gint values.
75 * We don't want it zero-terminated or cleared to 0's. *<!-- -->/
76 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
77 * for (i = 0; i < 10000; i++)
78 * g_array_append_val (garray, i);
79 * for (i = 0; i < 10000; i++)
80 * if (g_array_index (garray, gint, i) != i)
81 * g_print ("ERROR: got %d instead of %d\n",
82 * g_array_index (garray, gint, i), i);
83 * g_array_free (garray, TRUE);
88 #define MIN_ARRAY_SIZE 16
90 typedef struct _GRealArray GRealArray;
94 * @data: a pointer to the element data. The data may be moved as
95 * elements are added to the #GArray.
96 * @len: the number of elements in the #GArray not including the
97 * possible terminating zero element.
99 * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
107 guint zero_terminated : 1;
115 * @t: the type of the elements.
116 * @i: the index of the element to return.
117 * @Returns: the element of the #GArray at the index given by @i.
119 * Returns the element of a #GArray at the given index. The return
120 * value is cast to the given type.
123 * <title>Getting a pointer to an element in a #GArray</title>
125 * EDayViewEvent *event;
126 * /<!-- -->* This gets a pointer to the 4th element
127 * in the array of EDayViewEvent structs. *<!-- -->/
128 * event = &g_array_index (events, EDayViewEvent, 3);
133 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
134 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
135 #define g_array_elt_zero(array, pos, len) \
136 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
137 #define g_array_zero_terminate(array) G_STMT_START{ \
138 if ((array)->zero_terminated) \
139 g_array_elt_zero ((array), (array)->len, 1); \
142 static guint g_nearest_pow (gint num) G_GNUC_CONST;
143 static void g_array_maybe_expand (GRealArray *array,
148 * @zero_terminated: %TRUE if the array should have an extra element at
149 * the end which is set to 0.
150 * @clear_: %TRUE if #GArray elements should be automatically cleared
151 * to 0 when they are allocated.
152 * @element_size: the size of each element in bytes.
153 * @Returns: the new #GArray.
155 * Creates a new #GArray with a reference count of 1.
158 g_array_new (gboolean zero_terminated,
162 return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
167 * @zero_terminated: %TRUE if the array should have an extra element at
168 * the end with all bits cleared.
169 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
171 * @element_size: size of each element in the array.
172 * @reserved_size: number of elements preallocated.
173 * @Returns: the new #GArray.
175 * Creates a new #GArray with @reserved_size elements preallocated and
176 * a reference count of 1. This avoids frequent reallocation, if you
177 * are going to add many elements to the array. Note however that the
178 * size of the array is still 0.
180 GArray* g_array_sized_new (gboolean zero_terminated,
185 GRealArray *array = g_slice_new (GRealArray);
190 array->zero_terminated = (zero_terminated ? 1 : 0);
191 array->clear = (clear ? 1 : 0);
192 array->elt_size = elt_size;
193 array->ref_count = 1;
195 if (array->zero_terminated || reserved_size != 0)
197 g_array_maybe_expand (array, reserved_size);
198 g_array_zero_terminate(array);
201 return (GArray*) array;
208 * Atomically increments the reference count of @array by one. This
209 * function is MT-safe and may be called from any thread.
211 * Returns: The passed in #GArray.
216 g_array_ref (GArray *array)
218 GRealArray *rarray = (GRealArray*) array;
219 g_return_val_if_fail (array, NULL);
221 g_atomic_int_inc (&rarray->ref_count);
230 * Atomically decrements the reference count of @array by one. If the
231 * reference count drops to 0, all memory allocated by the array is
232 * released. This function is MT-safe and may be called from any
238 g_array_unref (GArray *array)
240 GRealArray *rarray = (GRealArray*) array;
241 g_return_if_fail (array);
243 if (g_atomic_int_dec_and_test (&rarray->ref_count))
244 g_array_free (array, TRUE);
248 * g_array_get_element_size:
251 * Gets the size of the elements in @array.
253 * Returns: Size of each element, in bytes.
258 g_array_get_element_size (GArray *array)
260 GRealArray *rarray = (GRealArray*) array;
262 g_return_val_if_fail (array, 0);
264 return rarray->elt_size;
270 * @free_segment: if %TRUE the actual element data is freed as well.
271 * @Returns: the element data if @free_segment is %FALSE, otherwise
272 * %NULL. The element data should be freed using g_free().
274 * Frees the memory allocated for the #GArray. If @free_segment is
275 * %TRUE it frees the memory block holding the elements as well and
276 * also each element if @array has a @element_free_func set. Pass
277 * %FALSE if you want to free the #GArray wrapper but preserve the
278 * underlying array for use elsewhere. If the reference count of @array
279 * is greater than one, the #GArray wrapper is preserved but the size
280 * of @array will be set to zero.
282 * <note><para>If array elements contain dynamically-allocated memory,
283 * they should be freed separately.</para></note>
286 g_array_free (GArray *farray,
287 gboolean free_segment)
289 GRealArray *array = (GRealArray*) farray;
291 gboolean preserve_wrapper;
293 g_return_val_if_fail (array, NULL);
295 /* if others are holding a reference, preserve the wrapper but do free/return the data */
296 preserve_wrapper = FALSE;
297 if (g_atomic_int_get (&array->ref_count) > 1)
298 preserve_wrapper = TRUE;
302 g_free (array->data);
306 segment = (gchar*) array->data;
308 if (preserve_wrapper)
316 g_slice_free1 (sizeof (GRealArray), array);
323 * g_array_append_vals:
325 * @data: a pointer to the elements to append to the end of the array.
326 * @len: the number of elements to append.
327 * @Returns: the #GArray.
329 * Adds @len elements onto the end of the array.
332 * g_array_append_val:
334 * @v: the value to append to the #GArray.
335 * @Returns: the #GArray.
337 * Adds the value on to the end of the array. The array will grow in
338 * size automatically if necessary.
340 * <note><para>g_array_append_val() is a macro which uses a reference
341 * to the value parameter @v. This means that you cannot use it with
342 * literal values such as "27". You must use variables.</para></note>
345 g_array_append_vals (GArray *farray,
349 GRealArray *array = (GRealArray*) farray;
351 g_return_val_if_fail (array, NULL);
353 g_array_maybe_expand (array, len);
355 memcpy (g_array_elt_pos (array, array->len), data,
356 g_array_elt_len (array, len));
360 g_array_zero_terminate (array);
366 * g_array_prepend_vals:
368 * @data: a pointer to the elements to prepend to the start of the
370 * @len: the number of elements to prepend.
371 * @Returns: the #GArray.
373 * Adds @len elements onto the start of the array.
375 * This operation is slower than g_array_append_vals() since the
376 * existing elements in the array have to be moved to make space for
380 * g_array_prepend_val:
382 * @v: the value to prepend to the #GArray.
383 * @Returns: the #GArray.
385 * Adds the value on to the start of the array. The array will grow in
386 * size automatically if necessary.
388 * This operation is slower than g_array_append_val() since the
389 * existing elements in the array have to be moved to make space for
392 * <note><para>g_array_prepend_val() is a macro which uses a reference
393 * to the value parameter @v. This means that you cannot use it with
394 * literal values such as "27". You must use variables.</para></note>
397 g_array_prepend_vals (GArray *farray,
401 GRealArray *array = (GRealArray*) farray;
403 g_return_val_if_fail (array, NULL);
405 g_array_maybe_expand (array, len);
407 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
408 g_array_elt_len (array, array->len));
410 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
414 g_array_zero_terminate (array);
420 * g_array_insert_vals:
422 * @index_: the index to place the elements at.
423 * @data: a pointer to the elements to insert.
424 * @len: the number of elements to insert.
425 * @Returns: the #GArray.
427 * Inserts @len elements into a #GArray at the given index.
430 * g_array_insert_val:
432 * @i: the index to place the element at.
433 * @v: the value to insert into the array.
434 * @Returns: the #GArray.
436 * Inserts an element into an array at the given index.
438 * <note><para>g_array_insert_val() is a macro which uses a reference
439 * to the value parameter @v. This means that you cannot use it with
440 * literal values such as "27". You must use variables.</para></note>
443 g_array_insert_vals (GArray *farray,
448 GRealArray *array = (GRealArray*) farray;
450 g_return_val_if_fail (array, NULL);
452 g_array_maybe_expand (array, len);
454 g_memmove (g_array_elt_pos (array, len + index_),
455 g_array_elt_pos (array, index_),
456 g_array_elt_len (array, array->len - index_));
458 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
462 g_array_zero_terminate (array);
470 * @length: the new size of the #GArray.
471 * @Returns: the #GArray.
473 * Sets the size of the array, expanding it if necessary. If the array
474 * was created with @clear_ set to %TRUE, the new elements are set to 0.
477 g_array_set_size (GArray *farray,
480 GRealArray *array = (GRealArray*) farray;
482 g_return_val_if_fail (array, NULL);
484 if (length > array->len)
486 g_array_maybe_expand (array, length - array->len);
489 g_array_elt_zero (array, array->len, length - array->len);
491 else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
492 g_array_elt_zero (array, length, array->len - length);
496 g_array_zero_terminate (array);
502 * g_array_remove_index:
504 * @index_: the index of the element to remove.
505 * @Returns: the #GArray.
507 * Removes the element at the given index from a #GArray. The following
508 * elements are moved down one place.
511 g_array_remove_index (GArray *farray,
514 GRealArray* array = (GRealArray*) farray;
516 g_return_val_if_fail (array, NULL);
518 g_return_val_if_fail (index_ < array->len, NULL);
520 if (index_ != array->len - 1)
521 g_memmove (g_array_elt_pos (array, index_),
522 g_array_elt_pos (array, index_ + 1),
523 g_array_elt_len (array, array->len - index_ - 1));
527 if (G_UNLIKELY (g_mem_gc_friendly))
528 g_array_elt_zero (array, array->len, 1);
530 g_array_zero_terminate (array);
536 * g_array_remove_index_fast:
538 * @index_: the index of the element to remove.
539 * @Returns: the #GArray.
541 * Removes the element at the given index from a #GArray. The last
542 * element in the array is used to fill in the space, so this function
543 * does not preserve the order of the #GArray. But it is faster than
544 * g_array_remove_index().
547 g_array_remove_index_fast (GArray *farray,
550 GRealArray* array = (GRealArray*) farray;
552 g_return_val_if_fail (array, NULL);
554 g_return_val_if_fail (index_ < array->len, NULL);
556 if (index_ != array->len - 1)
557 memcpy (g_array_elt_pos (array, index_),
558 g_array_elt_pos (array, array->len - 1),
559 g_array_elt_len (array, 1));
563 if (G_UNLIKELY (g_mem_gc_friendly))
564 g_array_elt_zero (array, array->len, 1);
566 g_array_zero_terminate (array);
572 * g_array_remove_range:
574 * @index_: the index of the first element to remove.
575 * @length: the number of elements to remove.
576 * @Returns: the #GArray.
578 * Removes the given number of elements starting at the given index
579 * from a #GArray. The following elements are moved to close the gap.
584 g_array_remove_range (GArray *farray,
588 GRealArray *array = (GRealArray*) farray;
590 g_return_val_if_fail (array, NULL);
591 g_return_val_if_fail (index_ < array->len, NULL);
592 g_return_val_if_fail (index_ + length <= array->len, NULL);
594 if (index_ + length != array->len)
595 g_memmove (g_array_elt_pos (array, index_),
596 g_array_elt_pos (array, index_ + length),
597 (array->len - (index_ + length)) * array->elt_size);
599 array->len -= length;
600 if (G_UNLIKELY (g_mem_gc_friendly))
601 g_array_elt_zero (array, array->len, length);
603 g_array_zero_terminate (array);
611 * @compare_func: comparison function.
613 * Sorts a #GArray using @compare_func which should be a qsort()-style
614 * comparison function (returns less than zero for first arg is less
615 * than second arg, zero for equal, greater zero if first arg is
616 * greater than second arg).
618 * If two array elements compare equal, their order in the sorted array
619 * is undefined. If you want equal elements to keep their order – i.e.
620 * you want a stable sort – you can write a comparison function that,
621 * if two elements would otherwise compare equal, compares them by
625 g_array_sort (GArray *farray,
626 GCompareFunc compare_func)
628 GRealArray *array = (GRealArray*) farray;
630 g_return_if_fail (array != NULL);
639 * g_array_sort_with_data:
641 * @compare_func: comparison function.
642 * @user_data: data to pass to @compare_func.
644 * Like g_array_sort(), but the comparison function receives an extra
645 * user data argument.
648 g_array_sort_with_data (GArray *farray,
649 GCompareDataFunc compare_func,
652 GRealArray *array = (GRealArray*) farray;
654 g_return_if_fail (array != NULL);
656 g_qsort_with_data (array->data,
663 /* Returns the smallest power of 2 greater than n, or n if
664 * such power does not fit in a guint
667 g_nearest_pow (gint num)
671 while (n < num && n > 0)
678 g_array_maybe_expand (GRealArray *array,
681 guint want_alloc = g_array_elt_len (array, array->len + len +
682 array->zero_terminated);
684 if (want_alloc > array->alloc)
686 want_alloc = g_nearest_pow (want_alloc);
687 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
689 array->data = g_realloc (array->data, want_alloc);
691 if (G_UNLIKELY (g_mem_gc_friendly))
692 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
694 array->alloc = want_alloc;
699 * SECTION:arrays_pointer
700 * @title: Pointer Arrays
701 * @short_description: arrays of pointers to any type of data, which
702 * grow automatically as new elements are added
704 * Pointer Arrays are similar to Arrays but are used only for storing
707 * <note><para>If you remove elements from the array, elements at the
708 * end of the array are moved into the space previously occupied by the
709 * removed element. This means that you should not rely on the index of
710 * particular elements remaining the same. You should also be careful
711 * when deleting elements while iterating over the array.</para></note>
713 * To create a pointer array, use g_ptr_array_new().
715 * To add elements to a pointer array, use g_ptr_array_add().
717 * To remove elements from a pointer array, use g_ptr_array_remove(),
718 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
720 * To access an element of a pointer array, use g_ptr_array_index().
722 * To set the size of a pointer array, use g_ptr_array_set_size().
724 * To free a pointer array, use g_ptr_array_free().
727 * <title>Using a #GPtrArray</title>
729 * GPtrArray *gparray;
730 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
732 * gparray = g_ptr_array_new (<!-- -->);
733 * g_ptr_array_add (gparray, (gpointer) string1);
734 * g_ptr_array_add (gparray, (gpointer) string2);
735 * g_ptr_array_add (gparray, (gpointer) string3);
737 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
738 * g_print ("ERROR: got %p instead of %p\n",
739 * g_ptr_array_index (gparray, 0), string1);
741 * g_ptr_array_free (gparray, TRUE);
746 typedef struct _GRealPtrArray GRealPtrArray;
750 * @pdata: points to the array of pointers, which may be moved when the
752 * @len: number of pointers in the array.
754 * Contains the public fields of a pointer array.
756 struct _GRealPtrArray
762 GDestroyNotify element_free_func;
767 * @array: a #GPtrArray.
768 * @index_: the index of the pointer to return.
769 * @Returns: the pointer at the given index.
771 * Returns the pointer at the given index of the pointer array.
774 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
779 * @Returns: the new #GPtrArray.
781 * Creates a new #GPtrArray with a reference count of 1.
784 g_ptr_array_new (void)
786 return g_ptr_array_sized_new (0);
790 * g_ptr_array_sized_new:
791 * @reserved_size: number of pointers preallocated.
792 * @Returns: the new #GPtrArray.
794 * Creates a new #GPtrArray with @reserved_size pointers preallocated
795 * and a reference count of 1. This avoids frequent reallocation, if
796 * you are going to add many pointers to the array. Note however that
797 * the size of the array is still 0.
800 g_ptr_array_sized_new (guint reserved_size)
802 GRealPtrArray *array = g_slice_new (GRealPtrArray);
807 array->ref_count = 1;
808 array->element_free_func = NULL;
810 if (reserved_size != 0)
811 g_ptr_array_maybe_expand (array, reserved_size);
813 return (GPtrArray*) array;
817 * g_ptr_array_new_with_free_func:
818 * @element_free_func: A function to free elements with destroy @array or %NULL.
820 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
821 * for freeing each element when the array is destroyed either via
822 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
823 * set to %TRUE or when removing elements.
825 * Returns: A new #GPtrArray.
830 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
834 array = g_ptr_array_new ();
835 g_ptr_array_set_free_func (array, element_free_func);
840 * g_ptr_array_new_full:
841 * @reserved_size: number of pointers preallocated.
842 * @element_free_func: A function to free elements with destroy @array or %NULL.
844 * Creates a new #GPtrArray with @reserved_size pointers preallocated
845 * and a reference count of 1. This avoids frequent reallocation, if
846 * you are going to add many pointers to the array. Note however that
847 * the size of the array is still 0. It also set @element_free_func
848 * for freeing each element when the array is destroyed either via
849 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
850 * set to %TRUE or when removing elements.
852 * Returns: A new #GPtrArray.
857 g_ptr_array_new_full (guint reserved_size,
858 GDestroyNotify element_free_func)
862 array = g_ptr_array_sized_new (reserved_size);
863 g_ptr_array_set_free_func (array, element_free_func);
868 * g_ptr_array_set_free_func:
869 * @array: A #GPtrArray.
870 * @element_free_func: A function to free elements with destroy @array or %NULL.
872 * Sets a function for freeing each element when @array is destroyed
873 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
874 * with @free_segment set to %TRUE or when removing elements.
879 g_ptr_array_set_free_func (GPtrArray *array,
880 GDestroyNotify element_free_func)
882 GRealPtrArray* rarray = (GRealPtrArray*) array;
884 g_return_if_fail (array);
886 rarray->element_free_func = element_free_func;
893 * Atomically increments the reference count of @array by one. This
894 * function is MT-safe and may be called from any thread.
896 * Returns: The passed in #GPtrArray.
901 g_ptr_array_ref (GPtrArray *array)
903 GRealPtrArray *rarray = (GRealPtrArray*) array;
905 g_return_val_if_fail (array, NULL);
907 g_atomic_int_inc (&rarray->ref_count);
914 * @array: A #GPtrArray.
916 * Atomically decrements the reference count of @array by one. If the
917 * reference count drops to 0, the effect is the same as calling
918 * g_ptr_array_free() with @free_segment set to %TRUE. This function
919 * is MT-safe and may be called from any thread.
924 g_ptr_array_unref (GPtrArray *array)
926 GRealPtrArray *rarray = (GRealPtrArray*) array;
927 g_return_if_fail (array);
929 if (g_atomic_int_dec_and_test (&rarray->ref_count))
930 g_ptr_array_free (array, TRUE);
935 * @array: a #GPtrArray.
936 * @free_seg: if %TRUE the actual pointer array is freed as well.
937 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
938 * The pointer array should be freed using g_free().
940 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
941 * it frees the memory block holding the elements as well. Pass %FALSE
942 * if you want to free the #GPtrArray wrapper but preserve the
943 * underlying array for use elsewhere. If the reference count of @array
944 * is greater than one, the #GPtrArray wrapper is preserved but the
945 * size of @array will be set to zero.
947 * <note><para>If array contents point to dynamically-allocated
948 * memory, they should be freed separately if @free_seg is %TRUE and no
949 * #GDestroyNotify function has been set for @array.</para></note>
952 g_ptr_array_free (GPtrArray *farray,
953 gboolean free_segment)
955 GRealPtrArray *array = (GRealPtrArray*) farray;
957 gboolean preserve_wrapper;
959 g_return_val_if_fail (array, NULL);
961 /* if others are holding a reference, preserve the wrapper but do free/return the data */
962 preserve_wrapper = FALSE;
963 if (g_atomic_int_get (&array->ref_count) > 1)
964 preserve_wrapper = TRUE;
968 if (array->element_free_func != NULL)
969 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
970 g_free (array->pdata);
974 segment = array->pdata;
976 if (preserve_wrapper)
984 g_slice_free1 (sizeof (GRealPtrArray), array);
991 g_ptr_array_maybe_expand (GRealPtrArray *array,
994 if ((array->len + len) > array->alloc)
996 guint old_alloc = array->alloc;
997 array->alloc = g_nearest_pow (array->len + len);
998 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
999 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1000 if (G_UNLIKELY (g_mem_gc_friendly))
1001 for ( ; old_alloc < array->alloc; old_alloc++)
1002 array->pdata [old_alloc] = NULL;
1007 * g_ptr_array_set_size:
1008 * @array: a #GPtrArray.
1009 * @length: the new length of the pointer array.
1011 * Sets the size of the array. When making the array larger,
1012 * newly-added elements will be set to %NULL. When making it smaller,
1013 * if @array has a non-%NULL #GDestroyNotify function then it will be
1014 * called for the removed elements.
1017 g_ptr_array_set_size (GPtrArray *farray,
1020 GRealPtrArray* array = (GRealPtrArray*) farray;
1022 g_return_if_fail (array);
1024 if (length > array->len)
1027 g_ptr_array_maybe_expand (array, (length - array->len));
1029 * memset (array->pdata + array->len, 0,
1030 * sizeof (gpointer) * (length - array->len));
1031 * to make it really portable. Remember (void*)NULL needn't be
1032 * bitwise zero. It of course is silly not to use memset (..,0,..).
1034 for (i = array->len; i < length; i++)
1035 array->pdata[i] = NULL;
1037 else if (length < array->len)
1038 g_ptr_array_remove_range (farray, length, array->len - length);
1040 array->len = length;
1044 * g_ptr_array_remove_index:
1045 * @array: a #GPtrArray.
1046 * @index_: the index of the pointer to remove.
1047 * @Returns: the pointer which was removed.
1049 * Removes the pointer at the given index from the pointer array. The
1050 * following elements are moved down one place. If @array has a
1051 * non-%NULL #GDestroyNotify function it is called for the removed
1055 g_ptr_array_remove_index (GPtrArray *farray,
1058 GRealPtrArray* array = (GRealPtrArray*) farray;
1061 g_return_val_if_fail (array, NULL);
1063 g_return_val_if_fail (index_ < array->len, NULL);
1065 result = array->pdata[index_];
1067 if (array->element_free_func != NULL)
1068 array->element_free_func (array->pdata[index_]);
1070 if (index_ != array->len - 1)
1071 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1072 sizeof (gpointer) * (array->len - index_ - 1));
1076 if (G_UNLIKELY (g_mem_gc_friendly))
1077 array->pdata[array->len] = NULL;
1083 * g_ptr_array_remove_index_fast:
1084 * @array: a #GPtrArray.
1085 * @index_: the index of the pointer to remove.
1086 * @Returns: the pointer which was removed.
1088 * Removes the pointer at the given index from the pointer array. The
1089 * last element in the array is used to fill in the space, so this
1090 * function does not preserve the order of the array. But it is faster
1091 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1092 * #GDestroyNotify function it is called for the removed element.
1095 g_ptr_array_remove_index_fast (GPtrArray *farray,
1098 GRealPtrArray* array = (GRealPtrArray*) farray;
1101 g_return_val_if_fail (array, NULL);
1103 g_return_val_if_fail (index_ < array->len, NULL);
1105 result = array->pdata[index_];
1107 if (array->element_free_func != NULL)
1108 array->element_free_func (array->pdata[index_]);
1110 if (index_ != array->len - 1)
1111 array->pdata[index_] = array->pdata[array->len - 1];
1115 if (G_UNLIKELY (g_mem_gc_friendly))
1116 array->pdata[array->len] = NULL;
1122 * g_ptr_array_remove_range:
1123 * @array: a @GPtrArray.
1124 * @index_: the index of the first pointer to remove.
1125 * @length: the number of pointers to remove.
1127 * Removes the given number of pointers starting at the given index
1128 * from a #GPtrArray. The following elements are moved to close the
1129 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1130 * for the removed elements.
1135 g_ptr_array_remove_range (GPtrArray *farray,
1139 GRealPtrArray* array = (GRealPtrArray*) farray;
1142 g_return_if_fail (array);
1143 g_return_if_fail (index_ < array->len);
1144 g_return_if_fail (index_ + length <= array->len);
1146 if (array->element_free_func != NULL)
1148 for (n = index_; n < index_ + length; n++)
1149 array->element_free_func (array->pdata[n]);
1152 if (index_ + length != array->len)
1154 g_memmove (&array->pdata[index_],
1155 &array->pdata[index_ + length],
1156 (array->len - (index_ + length)) * sizeof (gpointer));
1159 array->len -= length;
1160 if (G_UNLIKELY (g_mem_gc_friendly))
1163 for (i = 0; i < length; i++)
1164 array->pdata[array->len + i] = NULL;
1169 * g_ptr_array_remove:
1170 * @array: a #GPtrArray.
1171 * @data: the pointer to remove.
1172 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1173 * not found in the array.
1175 * Removes the first occurrence of the given pointer from the pointer
1176 * array. The following elements are moved down one place. If @array
1177 * has a non-%NULL #GDestroyNotify function it is called for the
1180 * It returns %TRUE if the pointer was removed, or %FALSE if the
1181 * pointer was not found.
1184 g_ptr_array_remove (GPtrArray *farray,
1187 GRealPtrArray* array = (GRealPtrArray*) farray;
1190 g_return_val_if_fail (array, FALSE);
1192 for (i = 0; i < array->len; i += 1)
1194 if (array->pdata[i] == data)
1196 g_ptr_array_remove_index (farray, i);
1205 * g_ptr_array_remove_fast:
1206 * @array: a #GPtrArray.
1207 * @data: the pointer to remove.
1208 * @Returns: %TRUE if the pointer was found in the array.
1210 * Removes the first occurrence of the given pointer from the pointer
1211 * array. The last element in the array is used to fill in the space,
1212 * so this function does not preserve the order of the array. But it is
1213 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1214 * #GDestroyNotify function it is called for the removed element.
1216 * It returns %TRUE if the pointer was removed, or %FALSE if the
1217 * pointer was not found.
1220 g_ptr_array_remove_fast (GPtrArray *farray,
1223 GRealPtrArray* array = (GRealPtrArray*) farray;
1226 g_return_val_if_fail (array, FALSE);
1228 for (i = 0; i < array->len; i += 1)
1230 if (array->pdata[i] == data)
1232 g_ptr_array_remove_index_fast (farray, i);
1242 * @array: a #GPtrArray.
1243 * @data: the pointer to add.
1245 * Adds a pointer to the end of the pointer array. The array will grow
1246 * in size automatically if necessary.
1249 g_ptr_array_add (GPtrArray *farray,
1252 GRealPtrArray* array = (GRealPtrArray*) farray;
1254 g_return_if_fail (array);
1256 g_ptr_array_maybe_expand (array, 1);
1258 array->pdata[array->len++] = data;
1263 * @array: a #GPtrArray.
1264 * @compare_func: comparison function.
1266 * Sorts the array, using @compare_func which should be a qsort()-style
1267 * comparison function (returns less than zero for first arg is less
1268 * than second arg, zero for equal, greater than zero if irst arg is
1269 * greater than second arg).
1271 * If two array elements compare equal, their order in the sorted array
1272 * is undefined. If you want equal elements to keep their order – i.e.
1273 * you want a stable sort – you can write a comparison function that,
1274 * if two elements would otherwise compare equal, compares them by
1277 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1278 * take the pointers from the array as arguments, it takes pointers to
1279 * the pointers in the array.</para></note>
1282 g_ptr_array_sort (GPtrArray *array,
1283 GCompareFunc compare_func)
1285 g_return_if_fail (array != NULL);
1287 qsort (array->pdata,
1294 * g_ptr_array_sort_with_data:
1295 * @array: a #GPtrArray.
1296 * @compare_func: comparison function.
1297 * @user_data: data to pass to @compare_func.
1299 * Like g_ptr_array_sort(), but the comparison function has an extra
1300 * user data argument.
1302 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1303 * doesn't take the pointers from the array as arguments, it takes
1304 * pointers to the pointers in the array.</para></note>
1307 g_ptr_array_sort_with_data (GPtrArray *array,
1308 GCompareDataFunc compare_func,
1311 g_return_if_fail (array != NULL);
1313 g_qsort_with_data (array->pdata,
1321 * g_ptr_array_foreach:
1322 * @array: a #GPtrArray
1323 * @func: the function to call for each array element
1324 * @user_data: user data to pass to the function
1326 * Calls a function for each element of a #GPtrArray.
1331 g_ptr_array_foreach (GPtrArray *array,
1337 g_return_if_fail (array);
1339 for (i = 0; i < array->len; i++)
1340 (*func) (array->pdata[i], user_data);
1344 * SECTION:arrays_byte
1345 * @title: Byte Arrays
1346 * @short_description: arrays of bytes, which grow automatically as
1347 * elements are added
1349 * #GByteArray is based on #GArray, to provide arrays of bytes which
1350 * grow automatically as elements are added.
1352 * To create a new #GByteArray use g_byte_array_new().
1354 * To add elements to a #GByteArray, use g_byte_array_append(), and
1355 * g_byte_array_prepend().
1357 * To set the size of a #GByteArray, use g_byte_array_set_size().
1359 * To free a #GByteArray, use g_byte_array_free().
1362 * <title>Using a #GByteArray</title>
1364 * GByteArray *gbarray;
1367 * gbarray = g_byte_array_new (<!-- -->);
1368 * for (i = 0; i < 10000; i++)
1369 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1371 * for (i = 0; i < 10000; i++)
1373 * g_assert (gbarray->data[4*i] == 'a');
1374 * g_assert (gbarray->data[4*i+1] == 'b');
1375 * g_assert (gbarray->data[4*i+2] == 'c');
1376 * g_assert (gbarray->data[4*i+3] == 'd');
1379 * g_byte_array_free (gbarray, TRUE);
1386 * @data: a pointer to the element data. The data may be moved as
1387 * elements are added to the #GByteArray.
1388 * @len: the number of elements in the #GByteArray.
1390 * The <structname>GByteArray</structname> struct allows access to the
1391 * public fields of a <structname>GByteArray</structname>.
1396 * @Returns: the new #GByteArray.
1398 * Creates a new #GByteArray with a reference count of 1.
1400 GByteArray* g_byte_array_new (void)
1402 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1406 * g_byte_array_sized_new:
1407 * @reserved_size: number of bytes preallocated.
1408 * @Returns: the new #GByteArray.
1410 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1411 * This avoids frequent reallocation, if you are going to add many
1412 * bytes to the array. Note however that the size of the array is still
1415 GByteArray* g_byte_array_sized_new (guint reserved_size)
1417 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1421 * g_byte_array_free:
1422 * @array: a #GByteArray.
1423 * @free_segment: if %TRUE the actual byte data is freed as well.
1424 * @Returns: the element data if @free_segment is %FALSE, otherwise
1425 * %NULL. The element data should be freed using g_free().
1427 * Frees the memory allocated by the #GByteArray. If @free_segment is
1428 * %TRUE it frees the actual byte data. If the reference count of
1429 * @array is greater than one, the #GByteArray wrapper is preserved but
1430 * the size of @array will be set to zero.
1432 guint8* g_byte_array_free (GByteArray *array,
1433 gboolean free_segment)
1435 return (guint8*) g_array_free ((GArray*) array, free_segment);
1440 * @array: A #GByteArray.
1442 * Atomically increments the reference count of @array by one. This
1443 * function is MT-safe and may be called from any thread.
1445 * Returns: The passed in #GByteArray.
1450 g_byte_array_ref (GByteArray *array)
1452 return (GByteArray *) g_array_ref ((GArray *) array);
1456 * g_byte_array_unref:
1457 * @array: A #GByteArray.
1459 * Atomically decrements the reference count of @array by one. If the
1460 * reference count drops to 0, all memory allocated by the array is
1461 * released. This function is MT-safe and may be called from any
1467 g_byte_array_unref (GByteArray *array)
1469 g_array_unref ((GArray *) array);
1473 * g_byte_array_append:
1474 * @array: a #GByteArray.
1475 * @data: the byte data to be added.
1476 * @len: the number of bytes to add.
1477 * @Returns: the #GByteArray.
1479 * Adds the given bytes to the end of the #GByteArray. The array will
1480 * grow in size automatically if necessary.
1482 GByteArray* g_byte_array_append (GByteArray *array,
1486 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1492 * g_byte_array_prepend:
1493 * @array: a #GByteArray.
1494 * @data: the byte data to be added.
1495 * @len: the number of bytes to add.
1496 * @Returns: the #GByteArray.
1498 * Adds the given data to the start of the #GByteArray. The array will
1499 * grow in size automatically if necessary.
1501 GByteArray* g_byte_array_prepend (GByteArray *array,
1505 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1511 * g_byte_array_set_size:
1512 * @array: a #GByteArray.
1513 * @length: the new size of the #GByteArray.
1514 * @Returns: the #GByteArray.
1516 * Sets the size of the #GByteArray, expanding it if necessary.
1518 GByteArray* g_byte_array_set_size (GByteArray *array,
1521 g_array_set_size ((GArray*) array, length);
1527 * g_byte_array_remove_index:
1528 * @array: a #GByteArray.
1529 * @index_: the index of the byte to remove.
1530 * @Returns: the #GByteArray.
1532 * Removes the byte at the given index from a #GByteArray. The
1533 * following bytes are moved down one place.
1535 GByteArray* g_byte_array_remove_index (GByteArray *array,
1538 g_array_remove_index ((GArray*) array, index_);
1544 * g_byte_array_remove_index_fast:
1545 * @array: a #GByteArray.
1546 * @index_: the index of the byte to remove.
1547 * @Returns: the #GByteArray.
1549 * Removes the byte at the given index from a #GByteArray. The last
1550 * element in the array is used to fill in the space, so this function
1551 * does not preserve the order of the #GByteArray. But it is faster
1552 * than g_byte_array_remove_index().
1554 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1557 g_array_remove_index_fast ((GArray*) array, index_);
1563 * g_byte_array_remove_range:
1564 * @array: a @GByteArray.
1565 * @index_: the index of the first byte to remove.
1566 * @length: the number of bytes to remove.
1567 * @Returns: the #GByteArray.
1569 * Removes the given number of bytes starting at the given index from a
1570 * #GByteArray. The following elements are moved to close the gap.
1575 g_byte_array_remove_range (GByteArray *array,
1579 g_return_val_if_fail (array, NULL);
1580 g_return_val_if_fail (index_ < array->len, NULL);
1581 g_return_val_if_fail (index_ + length <= array->len, NULL);
1583 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1587 * g_byte_array_sort:
1588 * @array: a #GByteArray.
1589 * @compare_func: comparison function.
1591 * Sorts a byte array, using @compare_func which should be a
1592 * qsort()-style comparison function (returns less than zero for first
1593 * arg is less than second arg, zero for equal, greater than zero if
1594 * first arg is greater than second arg).
1596 * If two array elements compare equal, their order in the sorted array
1597 * is undefined. If you want equal elements to keep their order – i.e.
1598 * you want a stable sort – you can write a comparison function that,
1599 * if two elements would otherwise compare equal, compares them by
1603 g_byte_array_sort (GByteArray *array,
1604 GCompareFunc compare_func)
1606 g_array_sort ((GArray *) array, compare_func);
1610 * g_byte_array_sort_with_data:
1611 * @array: a #GByteArray.
1612 * @compare_func: comparison function.
1613 * @user_data: data to pass to @compare_func.
1615 * Like g_byte_array_sort(), but the comparison function takes an extra
1616 * user data argument.
1619 g_byte_array_sort_with_data (GByteArray *array,
1620 GCompareDataFunc compare_func,
1623 g_array_sort_with_data ((GArray *) array, compare_func, user_data);