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 return (GArray*) g_array_sized_new (zero_terminated, clear, elt_size, 0);
170 * @zero_terminated: %TRUE if the array should have an extra element at
171 * the end with all bits cleared.
172 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
174 * @element_size: size of each element in the array.
175 * @reserved_size: number of elements preallocated.
176 * @Returns: the new #GArray.
178 * Creates a new #GArray with @reserved_size elements preallocated and
179 * a reference count of 1. This avoids frequent reallocation, if you
180 * are going to add many elements to the array. Note however that the
181 * size of the array is still 0.
183 GArray* g_array_sized_new (gboolean zero_terminated,
188 GRealArray *array = g_slice_new (GRealArray);
193 array->zero_terminated = (zero_terminated ? 1 : 0);
194 array->clear = (clear ? 1 : 0);
195 array->elt_size = elt_size;
196 array->ref_count = 1;
198 if (array->zero_terminated || reserved_size != 0)
200 g_array_maybe_expand (array, reserved_size);
201 g_array_zero_terminate(array);
204 return (GArray*) array;
211 * Atomically increments the reference count of @array by one. This
212 * function is MT-safe and may be called from any thread.
214 * Returns: The passed in #GArray.
219 g_array_ref (GArray *array)
221 GRealArray *rarray = (GRealArray*) array;
222 g_return_val_if_fail (array, NULL);
224 g_atomic_int_inc (&rarray->ref_count);
233 * Atomically decrements the reference count of @array by one. If the
234 * reference count drops to 0, all memory allocated by the array is
235 * released. This function is MT-safe and may be called from any
241 g_array_unref (GArray *array)
243 GRealArray *rarray = (GRealArray*) array;
244 g_return_if_fail (array);
246 if (g_atomic_int_dec_and_test (&rarray->ref_count))
247 g_array_free (array, TRUE);
251 * g_array_get_element_size:
254 * Gets the size of the elements in @array.
256 * Returns: Size of each element, in bytes.
261 g_array_get_element_size (GArray *array)
263 GRealArray *rarray = (GRealArray*) array;
265 g_return_val_if_fail (array, 0);
267 return rarray->elt_size;
273 * @free_segment: if %TRUE the actual element data is freed as well.
274 * @Returns: the element data if @free_segment is %FALSE, otherwise
275 * %NULL. The element data should be freed using g_free().
277 * Frees the memory allocated for the #GArray. If @free_segment is
278 * %TRUE it frees the memory block holding the elements as well and
279 * also each element if @array has a @element_free_func set. Pass
280 * %FALSE if you want to free the #GArray wrapper but preserve the
281 * underlying array for use elsewhere. If the reference count of @array
282 * is greater than one, the #GArray wrapper is preserved but the size
283 * of @array will be set to zero.
285 * <note><para>If array elements contain dynamically-allocated memory,
286 * they should be freed separately.</para></note>
289 g_array_free (GArray *farray,
290 gboolean free_segment)
292 GRealArray *array = (GRealArray*) farray;
294 gboolean preserve_wrapper;
296 g_return_val_if_fail (array, NULL);
298 /* if others are holding a reference, preserve the wrapper but do free/return the data */
299 preserve_wrapper = FALSE;
300 if (g_atomic_int_get (&array->ref_count) > 1)
301 preserve_wrapper = TRUE;
305 g_free (array->data);
309 segment = (gchar*) array->data;
311 if (preserve_wrapper)
319 g_slice_free1 (sizeof (GRealArray), array);
326 * g_array_append_vals:
328 * @data: a pointer to the elements to append to the end of the array.
329 * @len: the number of elements to append.
330 * @Returns: the #GArray.
332 * Adds @len elements onto the end of the array.
335 * g_array_append_val:
337 * @v: the value to append to the #GArray.
338 * @Returns: the #GArray.
340 * Adds the value on to the end of the array. The array will grow in
341 * size automatically if necessary.
343 * <note><para>g_array_append_val() is a macro which uses a reference
344 * to the value parameter @v. This means that you cannot use it with
345 * literal values such as "27". You must use variables.</para></note>
348 g_array_append_vals (GArray *farray,
352 GRealArray *array = (GRealArray*) farray;
354 g_return_val_if_fail (array, NULL);
356 g_array_maybe_expand (array, len);
358 memcpy (g_array_elt_pos (array, array->len), data,
359 g_array_elt_len (array, len));
363 g_array_zero_terminate (array);
369 * g_array_prepend_vals:
371 * @data: a pointer to the elements to prepend to the start of the
373 * @len: the number of elements to prepend.
374 * @Returns: the #GArray.
376 * Adds @len elements onto the start of the array.
378 * This operation is slower than g_array_append_vals() since the
379 * existing elements in the array have to be moved to make space for
383 * g_array_prepend_val:
385 * @v: the value to prepend to the #GArray.
386 * @Returns: the #GArray.
388 * Adds the value on to the start of the array. The array will grow in
389 * size automatically if necessary.
391 * This operation is slower than g_array_append_val() since the
392 * existing elements in the array have to be moved to make space for
395 * <note><para>g_array_prepend_val() is a macro which uses a reference
396 * to the value parameter @v. This means that you cannot use it with
397 * literal values such as "27". You must use variables.</para></note>
400 g_array_prepend_vals (GArray *farray,
404 GRealArray *array = (GRealArray*) farray;
406 g_return_val_if_fail (array, NULL);
408 g_array_maybe_expand (array, len);
410 g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
411 g_array_elt_len (array, array->len));
413 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
417 g_array_zero_terminate (array);
423 * g_array_insert_vals:
425 * @index_: the index to place the elements at.
426 * @data: a pointer to the elements to insert.
427 * @len: the number of elements to insert.
428 * @Returns: the #GArray.
430 * Inserts @len elements into a #GArray at the given index.
433 * g_array_insert_val:
435 * @i: the index to place the element at.
436 * @v: the value to insert into the array.
437 * @Returns: the #GArray.
439 * Inserts an element into an array at the given index.
441 * <note><para>g_array_insert_val() is a macro which uses a reference
442 * to the value parameter @v. This means that you cannot use it with
443 * literal values such as "27". You must use variables.</para></note>
446 g_array_insert_vals (GArray *farray,
451 GRealArray *array = (GRealArray*) farray;
453 g_return_val_if_fail (array, NULL);
455 g_array_maybe_expand (array, len);
457 g_memmove (g_array_elt_pos (array, len + index_),
458 g_array_elt_pos (array, index_),
459 g_array_elt_len (array, array->len - index_));
461 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
465 g_array_zero_terminate (array);
473 * @length: the new size of the #GArray.
474 * @Returns: the #GArray.
476 * Sets the size of the array, expanding it if necessary. If the array
477 * was created with @clear_ set to %TRUE, the new elements are set to 0.
480 g_array_set_size (GArray *farray,
483 GRealArray *array = (GRealArray*) farray;
485 g_return_val_if_fail (array, NULL);
487 if (length > array->len)
489 g_array_maybe_expand (array, length - array->len);
492 g_array_elt_zero (array, array->len, length - array->len);
494 else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
495 g_array_elt_zero (array, length, array->len - length);
499 g_array_zero_terminate (array);
505 * g_array_remove_index:
507 * @index_: the index of the element to remove.
508 * @Returns: the #GArray.
510 * Removes the element at the given index from a #GArray. The following
511 * elements are moved down one place.
514 g_array_remove_index (GArray *farray,
517 GRealArray* array = (GRealArray*) farray;
519 g_return_val_if_fail (array, NULL);
521 g_return_val_if_fail (index_ < array->len, NULL);
523 if (index_ != array->len - 1)
524 g_memmove (g_array_elt_pos (array, index_),
525 g_array_elt_pos (array, index_ + 1),
526 g_array_elt_len (array, array->len - index_ - 1));
530 if (G_UNLIKELY (g_mem_gc_friendly))
531 g_array_elt_zero (array, array->len, 1);
533 g_array_zero_terminate (array);
539 * g_array_remove_index_fast:
541 * @index_: the index of the element to remove.
542 * @Returns: the #GArray.
544 * Removes the element at the given index from a #GArray. The last
545 * element in the array is used to fill in the space, so this function
546 * does not preserve the order of the #GArray. But it is faster than
547 * g_array_remove_index().
550 g_array_remove_index_fast (GArray *farray,
553 GRealArray* array = (GRealArray*) farray;
555 g_return_val_if_fail (array, NULL);
557 g_return_val_if_fail (index_ < array->len, NULL);
559 if (index_ != array->len - 1)
560 memcpy (g_array_elt_pos (array, index_),
561 g_array_elt_pos (array, array->len - 1),
562 g_array_elt_len (array, 1));
566 if (G_UNLIKELY (g_mem_gc_friendly))
567 g_array_elt_zero (array, array->len, 1);
569 g_array_zero_terminate (array);
575 * g_array_remove_range:
577 * @index_: the index of the first element to remove.
578 * @length: the number of elements to remove.
579 * @Returns: the #GArray.
581 * Removes the given number of elements starting at the given index
582 * from a #GArray. The following elements are moved to close the gap.
587 g_array_remove_range (GArray *farray,
591 GRealArray *array = (GRealArray*) farray;
593 g_return_val_if_fail (array, NULL);
594 g_return_val_if_fail (index_ < array->len, NULL);
595 g_return_val_if_fail (index_ + length <= array->len, NULL);
597 if (index_ + length != array->len)
598 g_memmove (g_array_elt_pos (array, index_),
599 g_array_elt_pos (array, index_ + length),
600 (array->len - (index_ + length)) * array->elt_size);
602 array->len -= length;
603 if (G_UNLIKELY (g_mem_gc_friendly))
604 g_array_elt_zero (array, array->len, length);
606 g_array_zero_terminate (array);
614 * @compare_func: comparison function.
616 * Sorts a #GArray using @compare_func which should be a qsort()-style
617 * comparison function (returns less than zero for first arg is less
618 * than second arg, zero for equal, greater zero if first arg is
619 * greater than second arg).
621 * If two array elements compare equal, their order in the sorted array
622 * is undefined. If you want equal elements to keep their order (i.e.
623 * you want a stable sort) you can write a comparison function that,
624 * if two elements would otherwise compare equal, compares them by
628 g_array_sort (GArray *farray,
629 GCompareFunc compare_func)
631 GRealArray *array = (GRealArray*) farray;
633 g_return_if_fail (array != NULL);
642 * g_array_sort_with_data:
644 * @compare_func: comparison function.
645 * @user_data: data to pass to @compare_func.
647 * Like g_array_sort(), but the comparison function receives an extra
648 * user data argument.
651 g_array_sort_with_data (GArray *farray,
652 GCompareDataFunc compare_func,
655 GRealArray *array = (GRealArray*) farray;
657 g_return_if_fail (array != NULL);
659 g_qsort_with_data (array->data,
666 /* Returns the smallest power of 2 greater than n, or n if
667 * such power does not fit in a guint
670 g_nearest_pow (gint num)
674 while (n < num && n > 0)
681 g_array_maybe_expand (GRealArray *array,
684 guint want_alloc = g_array_elt_len (array, array->len + len +
685 array->zero_terminated);
687 if (want_alloc > array->alloc)
689 want_alloc = g_nearest_pow (want_alloc);
690 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
692 array->data = g_realloc (array->data, want_alloc);
694 if (G_UNLIKELY (g_mem_gc_friendly))
695 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
697 array->alloc = want_alloc;
702 * SECTION:arrays_pointer
703 * @title: Pointer Arrays
704 * @short_description: arrays of pointers to any type of data, which
705 * grow automatically as new elements are added
707 * Pointer Arrays are similar to Arrays but are used only for storing
710 * <note><para>If you remove elements from the array, elements at the
711 * end of the array are moved into the space previously occupied by the
712 * removed element. This means that you should not rely on the index of
713 * particular elements remaining the same. You should also be careful
714 * when deleting elements while iterating over the array.</para></note>
716 * To create a pointer array, use g_ptr_array_new().
718 * To add elements to a pointer array, use g_ptr_array_add().
720 * To remove elements from a pointer array, use g_ptr_array_remove(),
721 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
723 * To access an element of a pointer array, use g_ptr_array_index().
725 * To set the size of a pointer array, use g_ptr_array_set_size().
727 * To free a pointer array, use g_ptr_array_free().
730 * <title>Using a #GPtrArray</title>
732 * GPtrArray *gparray;
733 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
735 * gparray = g_ptr_array_new (<!-- -->);
736 * g_ptr_array_add (gparray, (gpointer) string1);
737 * g_ptr_array_add (gparray, (gpointer) string2);
738 * g_ptr_array_add (gparray, (gpointer) string3);
740 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
741 * g_print ("ERROR: got %p instead of %p\n",
742 * g_ptr_array_index (gparray, 0), string1);
744 * g_ptr_array_free (gparray, TRUE);
749 typedef struct _GRealPtrArray GRealPtrArray;
753 * @pdata: points to the array of pointers, which may be moved when the
755 * @len: number of pointers in the array.
757 * Contains the public fields of a pointer array.
759 struct _GRealPtrArray
765 GDestroyNotify element_free_func;
770 * @array: a #GPtrArray.
771 * @index_: the index of the pointer to return.
772 * @Returns: the pointer at the given index.
774 * Returns the pointer at the given index of the pointer array.
777 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
782 * @Returns: the new #GPtrArray.
784 * Creates a new #GPtrArray with a reference count of 1.
787 g_ptr_array_new (void)
789 return g_ptr_array_sized_new (0);
793 * g_ptr_array_sized_new:
794 * @reserved_size: number of pointers preallocated.
795 * @Returns: the new #GPtrArray.
797 * Creates a new #GPtrArray with @reserved_size pointers preallocated
798 * and a reference count of 1. This avoids frequent reallocation, if
799 * you are going to add many pointers to the array. Note however that
800 * the size of the array is still 0.
803 g_ptr_array_sized_new (guint reserved_size)
805 GRealPtrArray *array = g_slice_new (GRealPtrArray);
810 array->ref_count = 1;
811 array->element_free_func = NULL;
813 if (reserved_size != 0)
814 g_ptr_array_maybe_expand (array, reserved_size);
816 return (GPtrArray*) array;
820 * g_ptr_array_new_with_free_func:
821 * @element_free_func: A function to free elements with destroy @array or %NULL.
823 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
824 * for freeing each element when the array is destroyed either via
825 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
826 * set to %TRUE or when removing elements.
828 * Returns: A new #GPtrArray.
833 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
837 array = g_ptr_array_new ();
838 g_ptr_array_set_free_func (array, element_free_func);
843 * g_ptr_array_new_full:
844 * @reserved_size: number of pointers preallocated.
845 * @element_free_func: A function to free elements with destroy @array or %NULL.
847 * Creates a new #GPtrArray with @reserved_size pointers preallocated
848 * and a reference count of 1. This avoids frequent reallocation, if
849 * you are going to add many pointers to the array. Note however that
850 * the size of the array is still 0. It also set @element_free_func
851 * for freeing each element when the array is destroyed either via
852 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
853 * set to %TRUE or when removing elements.
855 * Returns: A new #GPtrArray.
860 g_ptr_array_new_full (guint reserved_size,
861 GDestroyNotify element_free_func)
865 array = g_ptr_array_sized_new (reserved_size);
866 g_ptr_array_set_free_func (array, element_free_func);
871 * g_ptr_array_set_free_func:
872 * @array: A #GPtrArray.
873 * @element_free_func: A function to free elements with destroy @array or %NULL.
875 * Sets a function for freeing each element when @array is destroyed
876 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
877 * with @free_segment set to %TRUE or when removing elements.
882 g_ptr_array_set_free_func (GPtrArray *array,
883 GDestroyNotify element_free_func)
885 GRealPtrArray* rarray = (GRealPtrArray*) array;
887 g_return_if_fail (array);
889 rarray->element_free_func = element_free_func;
896 * Atomically increments the reference count of @array by one. This
897 * function is MT-safe and may be called from any thread.
899 * Returns: The passed in #GPtrArray.
904 g_ptr_array_ref (GPtrArray *array)
906 GRealPtrArray *rarray = (GRealPtrArray*) array;
908 g_return_val_if_fail (array, NULL);
910 g_atomic_int_inc (&rarray->ref_count);
917 * @array: A #GPtrArray.
919 * Atomically decrements the reference count of @array by one. If the
920 * reference count drops to 0, the effect is the same as calling
921 * g_ptr_array_free() with @free_segment set to %TRUE. This function
922 * is MT-safe and may be called from any thread.
927 g_ptr_array_unref (GPtrArray *array)
929 GRealPtrArray *rarray = (GRealPtrArray*) array;
930 g_return_if_fail (array);
932 if (g_atomic_int_dec_and_test (&rarray->ref_count))
933 g_ptr_array_free (array, TRUE);
938 * @array: a #GPtrArray.
939 * @free_seg: if %TRUE the actual pointer array is freed as well.
940 * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
941 * The pointer array should be freed using g_free().
943 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
944 * it frees the memory block holding the elements as well. Pass %FALSE
945 * if you want to free the #GPtrArray wrapper but preserve the
946 * underlying array for use elsewhere. If the reference count of @array
947 * is greater than one, the #GPtrArray wrapper is preserved but the
948 * size of @array will be set to zero.
950 * <note><para>If array contents point to dynamically-allocated
951 * memory, they should be freed separately if @free_seg is %TRUE and no
952 * #GDestroyNotify function has been set for @array.</para></note>
955 g_ptr_array_free (GPtrArray *farray,
956 gboolean free_segment)
958 GRealPtrArray *array = (GRealPtrArray*) farray;
960 gboolean preserve_wrapper;
962 g_return_val_if_fail (array, NULL);
964 /* if others are holding a reference, preserve the wrapper but do free/return the data */
965 preserve_wrapper = FALSE;
966 if (g_atomic_int_get (&array->ref_count) > 1)
967 preserve_wrapper = TRUE;
971 if (array->element_free_func != NULL)
972 g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
973 g_free (array->pdata);
977 segment = array->pdata;
979 if (preserve_wrapper)
987 g_slice_free1 (sizeof (GRealPtrArray), array);
994 g_ptr_array_maybe_expand (GRealPtrArray *array,
997 if ((array->len + len) > array->alloc)
999 guint old_alloc = array->alloc;
1000 array->alloc = g_nearest_pow (array->len + len);
1001 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1002 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1003 if (G_UNLIKELY (g_mem_gc_friendly))
1004 for ( ; old_alloc < array->alloc; old_alloc++)
1005 array->pdata [old_alloc] = NULL;
1010 * g_ptr_array_set_size:
1011 * @array: a #GPtrArray.
1012 * @length: the new length of the pointer array.
1014 * Sets the size of the array. When making the array larger,
1015 * newly-added elements will be set to %NULL. When making it smaller,
1016 * if @array has a non-%NULL #GDestroyNotify function then it will be
1017 * called for the removed elements.
1020 g_ptr_array_set_size (GPtrArray *farray,
1023 GRealPtrArray* array = (GRealPtrArray*) farray;
1025 g_return_if_fail (array);
1027 if (length > array->len)
1030 g_ptr_array_maybe_expand (array, (length - array->len));
1032 * memset (array->pdata + array->len, 0,
1033 * sizeof (gpointer) * (length - array->len));
1034 * to make it really portable. Remember (void*)NULL needn't be
1035 * bitwise zero. It of course is silly not to use memset (..,0,..).
1037 for (i = array->len; i < length; i++)
1038 array->pdata[i] = NULL;
1040 else if (length < array->len)
1041 g_ptr_array_remove_range (farray, length, array->len - length);
1043 array->len = length;
1047 * g_ptr_array_remove_index:
1048 * @array: a #GPtrArray.
1049 * @index_: the index of the pointer to remove.
1050 * @Returns: the pointer which was removed.
1052 * Removes the pointer at the given index from the pointer array. The
1053 * following elements are moved down one place. If @array has a
1054 * non-%NULL #GDestroyNotify function it is called for the removed
1058 g_ptr_array_remove_index (GPtrArray *farray,
1061 GRealPtrArray* array = (GRealPtrArray*) farray;
1064 g_return_val_if_fail (array, NULL);
1066 g_return_val_if_fail (index_ < array->len, NULL);
1068 result = array->pdata[index_];
1070 if (array->element_free_func != NULL)
1071 array->element_free_func (array->pdata[index_]);
1073 if (index_ != array->len - 1)
1074 g_memmove (array->pdata + index_, array->pdata + index_ + 1,
1075 sizeof (gpointer) * (array->len - index_ - 1));
1079 if (G_UNLIKELY (g_mem_gc_friendly))
1080 array->pdata[array->len] = NULL;
1086 * g_ptr_array_remove_index_fast:
1087 * @array: a #GPtrArray.
1088 * @index_: the index of the pointer to remove.
1089 * @Returns: the pointer which was removed.
1091 * Removes the pointer at the given index from the pointer array. The
1092 * last element in the array is used to fill in the space, so this
1093 * function does not preserve the order of the array. But it is faster
1094 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1095 * #GDestroyNotify function it is called for the removed element.
1098 g_ptr_array_remove_index_fast (GPtrArray *farray,
1101 GRealPtrArray* array = (GRealPtrArray*) farray;
1104 g_return_val_if_fail (array, NULL);
1106 g_return_val_if_fail (index_ < array->len, NULL);
1108 result = array->pdata[index_];
1110 if (array->element_free_func != NULL)
1111 array->element_free_func (array->pdata[index_]);
1113 if (index_ != array->len - 1)
1114 array->pdata[index_] = array->pdata[array->len - 1];
1118 if (G_UNLIKELY (g_mem_gc_friendly))
1119 array->pdata[array->len] = NULL;
1125 * g_ptr_array_remove_range:
1126 * @array: a @GPtrArray.
1127 * @index_: the index of the first pointer to remove.
1128 * @length: the number of pointers to remove.
1130 * Removes the given number of pointers starting at the given index
1131 * from a #GPtrArray. The following elements are moved to close the
1132 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1133 * for the removed elements.
1138 g_ptr_array_remove_range (GPtrArray *farray,
1142 GRealPtrArray* array = (GRealPtrArray*) farray;
1145 g_return_if_fail (array);
1146 g_return_if_fail (index_ < array->len);
1147 g_return_if_fail (index_ + length <= array->len);
1149 if (array->element_free_func != NULL)
1151 for (n = index_; n < index_ + length; n++)
1152 array->element_free_func (array->pdata[n]);
1155 if (index_ + length != array->len)
1157 g_memmove (&array->pdata[index_],
1158 &array->pdata[index_ + length],
1159 (array->len - (index_ + length)) * sizeof (gpointer));
1162 array->len -= length;
1163 if (G_UNLIKELY (g_mem_gc_friendly))
1166 for (i = 0; i < length; i++)
1167 array->pdata[array->len + i] = NULL;
1172 * g_ptr_array_remove:
1173 * @array: a #GPtrArray.
1174 * @data: the pointer to remove.
1175 * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1176 * not found in the array.
1178 * Removes the first occurrence of the given pointer from the pointer
1179 * array. The following elements are moved down one place. If @array
1180 * has a non-%NULL #GDestroyNotify function it is called for the
1183 * It returns %TRUE if the pointer was removed, or %FALSE if the
1184 * pointer was not found.
1187 g_ptr_array_remove (GPtrArray *farray,
1190 GRealPtrArray* array = (GRealPtrArray*) farray;
1193 g_return_val_if_fail (array, FALSE);
1195 for (i = 0; i < array->len; i += 1)
1197 if (array->pdata[i] == data)
1199 g_ptr_array_remove_index (farray, i);
1208 * g_ptr_array_remove_fast:
1209 * @array: a #GPtrArray.
1210 * @data: the pointer to remove.
1211 * @Returns: %TRUE if the pointer was found in the array.
1213 * Removes the first occurrence of the given pointer from the pointer
1214 * array. The last element in the array is used to fill in the space,
1215 * so this function does not preserve the order of the array. But it is
1216 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1217 * #GDestroyNotify function it is called for the removed element.
1219 * It returns %TRUE if the pointer was removed, or %FALSE if the
1220 * pointer was not found.
1223 g_ptr_array_remove_fast (GPtrArray *farray,
1226 GRealPtrArray* array = (GRealPtrArray*) farray;
1229 g_return_val_if_fail (array, FALSE);
1231 for (i = 0; i < array->len; i += 1)
1233 if (array->pdata[i] == data)
1235 g_ptr_array_remove_index_fast (farray, i);
1245 * @array: a #GPtrArray.
1246 * @data: the pointer to add.
1248 * Adds a pointer to the end of the pointer array. The array will grow
1249 * in size automatically if necessary.
1252 g_ptr_array_add (GPtrArray *farray,
1255 GRealPtrArray* array = (GRealPtrArray*) farray;
1257 g_return_if_fail (array);
1259 g_ptr_array_maybe_expand (array, 1);
1261 array->pdata[array->len++] = data;
1266 * @array: a #GPtrArray.
1267 * @compare_func: comparison function.
1269 * Sorts the array, using @compare_func which should be a qsort()-style
1270 * comparison function (returns less than zero for first arg is less
1271 * than second arg, zero for equal, greater than zero if irst arg is
1272 * greater than second arg).
1274 * If two array elements compare equal, their order in the sorted array
1275 * is undefined. If you want equal elements to keep their order (i.e.
1276 * you want a stable sort) you can write a comparison function that,
1277 * if two elements would otherwise compare equal, compares them by
1280 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1281 * take the pointers from the array as arguments, it takes pointers to
1282 * the pointers in the array.</para></note>
1285 g_ptr_array_sort (GPtrArray *array,
1286 GCompareFunc compare_func)
1288 g_return_if_fail (array != NULL);
1290 qsort (array->pdata,
1297 * g_ptr_array_sort_with_data:
1298 * @array: a #GPtrArray.
1299 * @compare_func: comparison function.
1300 * @user_data: data to pass to @compare_func.
1302 * Like g_ptr_array_sort(), but the comparison function has an extra
1303 * user data argument.
1305 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1306 * doesn't take the pointers from the array as arguments, it takes
1307 * pointers to the pointers in the array.</para></note>
1310 g_ptr_array_sort_with_data (GPtrArray *array,
1311 GCompareDataFunc compare_func,
1314 g_return_if_fail (array != NULL);
1316 g_qsort_with_data (array->pdata,
1324 * g_ptr_array_foreach:
1325 * @array: a #GPtrArray
1326 * @func: the function to call for each array element
1327 * @user_data: user data to pass to the function
1329 * Calls a function for each element of a #GPtrArray.
1334 g_ptr_array_foreach (GPtrArray *array,
1340 g_return_if_fail (array);
1342 for (i = 0; i < array->len; i++)
1343 (*func) (array->pdata[i], user_data);
1347 * SECTION:arrays_byte
1348 * @title: Byte Arrays
1349 * @short_description: arrays of bytes
1351 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1352 * of bytes which grow automatically as elements are added.
1354 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1355 * #GByteArray, use g_byte_array_append(), and 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);
1383 * See #GBytes if you are interested in an immutable object representing a
1384 * sequence of bytes.
1389 * @data: a pointer to the element data. The data may be moved as
1390 * elements are added to the #GByteArray.
1391 * @len: the number of elements in the #GByteArray.
1393 * The <structname>GByteArray</structname> struct allows access to the
1394 * public fields of a <structname>GByteArray</structname>.
1399 * @Returns: the new #GByteArray.
1401 * Creates a new #GByteArray with a reference count of 1.
1403 GByteArray* g_byte_array_new (void)
1405 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1409 * g_byte_array_new_take:
1410 * @data: (array length=len): byte data for the array
1411 * @len: length of @data
1413 * Create byte array containing the data. The data will be owned by the array
1414 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1418 * Returns: (transfer full): a new #GByteArray
1421 g_byte_array_new_take (guint8 *data,
1427 array = g_byte_array_new ();
1428 real = (GRealArray *)array;
1429 g_assert (real->data == NULL);
1430 g_assert (real->len == 0);
1439 * g_byte_array_sized_new:
1440 * @reserved_size: number of bytes preallocated.
1441 * @Returns: the new #GByteArray.
1443 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1444 * This avoids frequent reallocation, if you are going to add many
1445 * bytes to the array. Note however that the size of the array is still
1448 GByteArray* g_byte_array_sized_new (guint reserved_size)
1450 return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1454 * g_byte_array_free:
1455 * @array: a #GByteArray.
1456 * @free_segment: if %TRUE the actual byte data is freed as well.
1457 * @Returns: the element data if @free_segment is %FALSE, otherwise
1458 * %NULL. The element data should be freed using g_free().
1460 * Frees the memory allocated by the #GByteArray. If @free_segment is
1461 * %TRUE it frees the actual byte data. If the reference count of
1462 * @array is greater than one, the #GByteArray wrapper is preserved but
1463 * the size of @array will be set to zero.
1465 guint8* g_byte_array_free (GByteArray *array,
1466 gboolean free_segment)
1468 return (guint8*) g_array_free ((GArray*) array, free_segment);
1472 * g_byte_array_free_to_bytes:
1473 * @array: (transfer full): a #GByteArray
1475 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1477 * The #GByteArray is freed unless the reference count of @array is greater
1478 * than one, the #GByteArray wrapper is preserved but the size of @array
1479 * will be set to zero.
1481 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1486 * Returns: (transfer full): a new immutable #GBytes representing same byte
1487 * data that was in the array
1490 g_byte_array_free_to_bytes (GByteArray *array)
1494 g_return_val_if_fail (array != NULL, NULL);
1496 length = array->len;
1497 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1502 * @array: A #GByteArray.
1504 * Atomically increments the reference count of @array by one. This
1505 * function is MT-safe and may be called from any thread.
1507 * Returns: The passed in #GByteArray.
1512 g_byte_array_ref (GByteArray *array)
1514 return (GByteArray *) g_array_ref ((GArray *) array);
1518 * g_byte_array_unref:
1519 * @array: A #GByteArray.
1521 * Atomically decrements the reference count of @array by one. If the
1522 * reference count drops to 0, all memory allocated by the array is
1523 * released. This function is MT-safe and may be called from any
1529 g_byte_array_unref (GByteArray *array)
1531 g_array_unref ((GArray *) array);
1535 * g_byte_array_append:
1536 * @array: a #GByteArray.
1537 * @data: the byte data to be added.
1538 * @len: the number of bytes to add.
1539 * @Returns: the #GByteArray.
1541 * Adds the given bytes to the end of the #GByteArray. The array will
1542 * grow in size automatically if necessary.
1544 GByteArray* g_byte_array_append (GByteArray *array,
1548 g_array_append_vals ((GArray*) array, (guint8*)data, len);
1554 * g_byte_array_prepend:
1555 * @array: a #GByteArray.
1556 * @data: the byte data to be added.
1557 * @len: the number of bytes to add.
1558 * @Returns: the #GByteArray.
1560 * Adds the given data to the start of the #GByteArray. The array will
1561 * grow in size automatically if necessary.
1563 GByteArray* g_byte_array_prepend (GByteArray *array,
1567 g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1573 * g_byte_array_set_size:
1574 * @array: a #GByteArray.
1575 * @length: the new size of the #GByteArray.
1576 * @Returns: the #GByteArray.
1578 * Sets the size of the #GByteArray, expanding it if necessary.
1580 GByteArray* g_byte_array_set_size (GByteArray *array,
1583 g_array_set_size ((GArray*) array, length);
1589 * g_byte_array_remove_index:
1590 * @array: a #GByteArray.
1591 * @index_: the index of the byte to remove.
1592 * @Returns: the #GByteArray.
1594 * Removes the byte at the given index from a #GByteArray. The
1595 * following bytes are moved down one place.
1597 GByteArray* g_byte_array_remove_index (GByteArray *array,
1600 g_array_remove_index ((GArray*) array, index_);
1606 * g_byte_array_remove_index_fast:
1607 * @array: a #GByteArray.
1608 * @index_: the index of the byte to remove.
1609 * @Returns: the #GByteArray.
1611 * Removes the byte at the given index from a #GByteArray. The last
1612 * element in the array is used to fill in the space, so this function
1613 * does not preserve the order of the #GByteArray. But it is faster
1614 * than g_byte_array_remove_index().
1616 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1619 g_array_remove_index_fast ((GArray*) array, index_);
1625 * g_byte_array_remove_range:
1626 * @array: a @GByteArray.
1627 * @index_: the index of the first byte to remove.
1628 * @length: the number of bytes to remove.
1629 * @Returns: the #GByteArray.
1631 * Removes the given number of bytes starting at the given index from a
1632 * #GByteArray. The following elements are moved to close the gap.
1637 g_byte_array_remove_range (GByteArray *array,
1641 g_return_val_if_fail (array, NULL);
1642 g_return_val_if_fail (index_ < array->len, NULL);
1643 g_return_val_if_fail (index_ + length <= array->len, NULL);
1645 return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1649 * g_byte_array_sort:
1650 * @array: a #GByteArray.
1651 * @compare_func: comparison function.
1653 * Sorts a byte array, using @compare_func which should be a
1654 * qsort()-style comparison function (returns less than zero for first
1655 * arg is less than second arg, zero for equal, greater than zero if
1656 * first arg is greater than second arg).
1658 * If two array elements compare equal, their order in the sorted array
1659 * is undefined. If you want equal elements to keep their order (i.e.
1660 * you want a stable sort) you can write a comparison function that,
1661 * if two elements would otherwise compare equal, compares them by
1665 g_byte_array_sort (GByteArray *array,
1666 GCompareFunc compare_func)
1668 g_array_sort ((GArray *) array, compare_func);
1672 * g_byte_array_sort_with_data:
1673 * @array: a #GByteArray.
1674 * @compare_func: comparison function.
1675 * @user_data: data to pass to @compare_func.
1677 * Like g_byte_array_sort(), but the comparison function takes an extra
1678 * user data argument.
1681 g_byte_array_sort_with_data (GByteArray *array,
1682 GCompareDataFunc compare_func,
1685 g_array_sort_with_data ((GArray *) array, compare_func, user_data);