1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
41 #include "gtestutils.h"
43 #include "gmessages.h"
50 * @short_description: arrays of arbitrary elements which grow
51 * automatically as elements are added
53 * Arrays are similar to standard C arrays, except that they grow
54 * automatically as elements are added.
56 * Array elements can be of any size (though all elements of one array
57 * are the same size), and the array can be automatically cleared to
58 * '0's and zero-terminated.
60 * To create a new array use g_array_new().
62 * To add elements to an array, use g_array_append_val(),
63 * g_array_append_vals(), g_array_prepend_val(), and
64 * g_array_prepend_vals().
66 * To access an element of an array, use g_array_index().
68 * To set the size of an array, use g_array_set_size().
70 * To free an array, use g_array_free().
73 * <title>Using a #GArray to store #gint values</title>
77 * /<!-- -->* We create a new array to store gint values.
78 * We don't want it zero-terminated or cleared to 0's. *<!-- -->/
79 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
80 * for (i = 0; i < 10000; i++)
81 * g_array_append_val (garray, i);
82 * for (i = 0; i < 10000; i++)
83 * if (g_array_index (garray, gint, i) != i)
84 * g_print ("ERROR: got %d instead of %d\n",
85 * g_array_index (garray, gint, i), i);
86 * g_array_free (garray, TRUE);
91 #define MIN_ARRAY_SIZE 16
93 typedef struct _GRealArray GRealArray
;
97 * @data: a pointer to the element data. The data may be moved as
98 * elements are added to the #GArray.
99 * @len: the number of elements in the #GArray not including the
100 * possible terminating zero element.
102 * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
110 guint zero_terminated
: 1;
113 GDestroyNotify clear_func
;
119 * @t: the type of the elements.
120 * @i: the index of the element to return.
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);
135 * Returns: the element of the #GArray at the index given by @i.
138 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
139 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
140 #define g_array_elt_zero(array, pos, len) \
141 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
142 #define g_array_zero_terminate(array) G_STMT_START{ \
143 if ((array)->zero_terminated) \
144 g_array_elt_zero ((array), (array)->len, 1); \
147 static guint
g_nearest_pow (gint num
) G_GNUC_CONST
;
148 static void g_array_maybe_expand (GRealArray
*array
,
153 * @zero_terminated: %TRUE if the array should have an extra element at
154 * the end which is set to 0.
155 * @clear_: %TRUE if #GArray elements should be automatically cleared
156 * to 0 when they are allocated.
157 * @element_size: the size of each element in bytes.
159 * Creates a new #GArray with a reference count of 1.
161 * Returns: the new #GArray.
164 g_array_new (gboolean zero_terminated
,
168 g_return_val_if_fail (elt_size
> 0, NULL
);
170 return g_array_sized_new (zero_terminated
, clear
, elt_size
, 0);
175 * @zero_terminated: %TRUE if the array should have an extra element at
176 * the end with all bits cleared.
177 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
179 * @element_size: size of each element in the array.
180 * @reserved_size: number of elements preallocated.
182 * Creates a new #GArray with @reserved_size elements preallocated and
183 * a reference count of 1. This avoids frequent reallocation, if you
184 * are going to add many elements to the array. Note however that the
185 * size of the array is still 0.
187 * Returns: the new #GArray.
189 GArray
* g_array_sized_new (gboolean zero_terminated
,
196 g_return_val_if_fail (elt_size
> 0, NULL
);
198 array
= g_slice_new (GRealArray
);
203 array
->zero_terminated
= (zero_terminated
? 1 : 0);
204 array
->clear
= (clear
? 1 : 0);
205 array
->elt_size
= elt_size
;
206 array
->ref_count
= 1;
207 array
->clear_func
= NULL
;
209 if (array
->zero_terminated
|| reserved_size
!= 0)
211 g_array_maybe_expand (array
, reserved_size
);
212 g_array_zero_terminate(array
);
215 return (GArray
*) array
;
219 * g_array_set_clear_func:
221 * @clear_func: a function to clear an element of @array
223 * Sets a function to clear an element of @array.
225 * The @clear_func will be called when an element in the array
226 * data segment is removed and when the array is freed and data
227 * segment is deallocated as well.
229 * Note that in contrast with other uses of #GDestroyNotify
230 * functions, @clear_func is expected to clear the contents of
231 * the array element it is given, but not free the element itself.
236 g_array_set_clear_func (GArray
*array
,
237 GDestroyNotify clear_func
)
239 GRealArray
*rarray
= (GRealArray
*) array
;
241 g_return_if_fail (array
!= NULL
);
243 rarray
->clear_func
= clear_func
;
250 * Atomically increments the reference count of @array by one. This
251 * function is MT-safe and may be called from any thread.
253 * Returns: The passed in #GArray.
258 g_array_ref (GArray
*array
)
260 GRealArray
*rarray
= (GRealArray
*) array
;
261 g_return_val_if_fail (array
, NULL
);
263 g_atomic_int_inc (&rarray
->ref_count
);
270 FREE_SEGMENT
= 1 << 0,
271 PRESERVE_WRAPPER
= 1 << 1
274 static gchar
*array_free (GRealArray
*, ArrayFreeFlags
);
280 * Atomically decrements the reference count of @array by one. If the
281 * reference count drops to 0, all memory allocated by the array is
282 * released. This function is MT-safe and may be called from any
288 g_array_unref (GArray
*array
)
290 GRealArray
*rarray
= (GRealArray
*) array
;
291 g_return_if_fail (array
);
293 if (g_atomic_int_dec_and_test (&rarray
->ref_count
))
294 array_free (rarray
, FREE_SEGMENT
);
298 * g_array_get_element_size:
301 * Gets the size of the elements in @array.
303 * Returns: Size of each element, in bytes.
308 g_array_get_element_size (GArray
*array
)
310 GRealArray
*rarray
= (GRealArray
*) array
;
312 g_return_val_if_fail (array
, 0);
314 return rarray
->elt_size
;
320 * @free_segment: if %TRUE the actual element data is freed as well.
322 * Frees the memory allocated for the #GArray. If @free_segment is
323 * %TRUE it frees the memory block holding the elements as well and
324 * also each element if @array has a @element_free_func set. Pass
325 * %FALSE if you want to free the #GArray wrapper but preserve the
326 * underlying array for use elsewhere. If the reference count of @array
327 * is greater than one, the #GArray wrapper is preserved but the size
328 * of @array will be set to zero.
330 * <note><para>If array elements contain dynamically-allocated memory,
331 * they should be freed separately.</para></note>
333 * Returns: the element data if @free_segment is %FALSE, otherwise
334 * %NULL. The element data should be freed using g_free().
337 g_array_free (GArray
*farray
,
338 gboolean free_segment
)
340 GRealArray
*array
= (GRealArray
*) farray
;
341 ArrayFreeFlags flags
;
343 g_return_val_if_fail (array
, NULL
);
345 flags
= (free_segment
? FREE_SEGMENT
: 0);
347 /* if others are holding a reference, preserve the wrapper but do free/return the data */
348 if (!g_atomic_int_dec_and_test (&array
->ref_count
))
349 flags
|= PRESERVE_WRAPPER
;
351 return array_free (array
, flags
);
355 array_free (GRealArray
*array
,
356 ArrayFreeFlags flags
)
360 if (flags
& FREE_SEGMENT
)
362 if (array
->clear_func
!= NULL
)
366 for (i
= 0; i
< array
->len
; i
++)
367 array
->clear_func (g_array_elt_pos (array
, i
));
370 g_free (array
->data
);
374 segment
= (gchar
*) array
->data
;
376 if (flags
& PRESERVE_WRAPPER
)
384 g_slice_free1 (sizeof (GRealArray
), array
);
391 * g_array_append_vals:
393 * @data: a pointer to the elements to append to the end of the array.
394 * @len: the number of elements to append.
396 * Adds @len elements onto the end of the array.
398 * Returns: the #GArray.
401 * g_array_append_val:
403 * @v: the value to append to the #GArray.
405 * Adds the value on to the end of the array. The array will grow in
406 * size automatically if necessary.
408 * <note><para>g_array_append_val() is a macro which uses a reference
409 * to the value parameter @v. This means that you cannot use it with
410 * literal values such as "27". You must use variables.</para></note>
412 * Returns: the #GArray.
415 g_array_append_vals (GArray
*farray
,
419 GRealArray
*array
= (GRealArray
*) farray
;
421 g_return_val_if_fail (array
, NULL
);
423 g_array_maybe_expand (array
, len
);
425 memcpy (g_array_elt_pos (array
, array
->len
), data
,
426 g_array_elt_len (array
, len
));
430 g_array_zero_terminate (array
);
436 * g_array_prepend_vals:
438 * @data: a pointer to the elements to prepend to the start of the
440 * @len: the number of elements to prepend.
442 * Adds @len elements onto the start of the array.
444 * This operation is slower than g_array_append_vals() since the
445 * existing elements in the array have to be moved to make space for
448 * Returns: the #GArray.
451 * g_array_prepend_val:
453 * @v: the value to prepend to the #GArray.
455 * Adds the value on to the start of the array. The array will grow in
456 * size automatically if necessary.
458 * This operation is slower than g_array_append_val() since the
459 * existing elements in the array have to be moved to make space for
462 * <note><para>g_array_prepend_val() is a macro which uses a reference
463 * to the value parameter @v. This means that you cannot use it with
464 * literal values such as "27". You must use variables.</para></note>
466 * Returns: the #GArray.
469 g_array_prepend_vals (GArray
*farray
,
473 GRealArray
*array
= (GRealArray
*) farray
;
475 g_return_val_if_fail (array
, NULL
);
477 g_array_maybe_expand (array
, len
);
479 g_memmove (g_array_elt_pos (array
, len
), g_array_elt_pos (array
, 0),
480 g_array_elt_len (array
, array
->len
));
482 memcpy (g_array_elt_pos (array
, 0), data
, g_array_elt_len (array
, len
));
486 g_array_zero_terminate (array
);
492 * g_array_insert_vals:
494 * @index_: the index to place the elements at.
495 * @data: a pointer to the elements to insert.
496 * @len: the number of elements to insert.
498 * Inserts @len elements into a #GArray at the given index.
500 * Returns: the #GArray.
503 * g_array_insert_val:
505 * @i: the index to place the element at.
506 * @v: the value to insert into the array.
508 * Inserts an element into an array at the given index.
510 * <note><para>g_array_insert_val() is a macro which uses a reference
511 * to the value parameter @v. This means that you cannot use it with
512 * literal values such as "27". You must use variables.</para></note>
514 * Returns: the #GArray.
517 g_array_insert_vals (GArray
*farray
,
522 GRealArray
*array
= (GRealArray
*) farray
;
524 g_return_val_if_fail (array
, NULL
);
526 g_array_maybe_expand (array
, len
);
528 g_memmove (g_array_elt_pos (array
, len
+ index_
),
529 g_array_elt_pos (array
, index_
),
530 g_array_elt_len (array
, array
->len
- index_
));
532 memcpy (g_array_elt_pos (array
, index_
), data
, g_array_elt_len (array
, len
));
536 g_array_zero_terminate (array
);
544 * @length: the new size of the #GArray.
546 * Sets the size of the array, expanding it if necessary. If the array
547 * was created with @clear_ set to %TRUE, the new elements are set to 0.
549 * Returns: the #GArray.
552 g_array_set_size (GArray
*farray
,
555 GRealArray
*array
= (GRealArray
*) farray
;
557 g_return_val_if_fail (array
, NULL
);
559 if (length
> array
->len
)
561 g_array_maybe_expand (array
, length
- array
->len
);
564 g_array_elt_zero (array
, array
->len
, length
- array
->len
);
566 else if (length
< array
->len
)
567 g_array_remove_range (farray
, length
, array
->len
- length
);
571 g_array_zero_terminate (array
);
577 * g_array_remove_index:
579 * @index_: the index of the element to remove.
581 * Removes the element at the given index from a #GArray. The following
582 * elements are moved down one place.
584 * Returns: the #GArray.
587 g_array_remove_index (GArray
*farray
,
590 GRealArray
* array
= (GRealArray
*) farray
;
592 g_return_val_if_fail (array
, NULL
);
594 g_return_val_if_fail (index_
< array
->len
, NULL
);
596 if (array
->clear_func
!= NULL
)
597 array
->clear_func (g_array_elt_pos (array
, index_
));
599 if (index_
!= array
->len
- 1)
600 g_memmove (g_array_elt_pos (array
, index_
),
601 g_array_elt_pos (array
, index_
+ 1),
602 g_array_elt_len (array
, array
->len
- index_
- 1));
606 if (G_UNLIKELY (g_mem_gc_friendly
))
607 g_array_elt_zero (array
, array
->len
, 1);
609 g_array_zero_terminate (array
);
615 * g_array_remove_index_fast:
617 * @index_: the index of the element to remove.
619 * Removes the element at the given index from a #GArray. The last
620 * element in the array is used to fill in the space, so this function
621 * does not preserve the order of the #GArray. But it is faster than
622 * g_array_remove_index().
624 * Returns: the #GArray.
627 g_array_remove_index_fast (GArray
*farray
,
630 GRealArray
* array
= (GRealArray
*) farray
;
632 g_return_val_if_fail (array
, NULL
);
634 g_return_val_if_fail (index_
< array
->len
, NULL
);
636 if (array
->clear_func
!= NULL
)
637 array
->clear_func (g_array_elt_pos (array
, index_
));
639 if (index_
!= array
->len
- 1)
640 memcpy (g_array_elt_pos (array
, index_
),
641 g_array_elt_pos (array
, array
->len
- 1),
642 g_array_elt_len (array
, 1));
646 if (G_UNLIKELY (g_mem_gc_friendly
))
647 g_array_elt_zero (array
, array
->len
, 1);
649 g_array_zero_terminate (array
);
655 * g_array_remove_range:
657 * @index_: the index of the first element to remove.
658 * @length: the number of elements to remove.
660 * Removes the given number of elements starting at the given index
661 * from a #GArray. The following elements are moved to close the gap.
663 * Returns: the #GArray.
668 g_array_remove_range (GArray
*farray
,
672 GRealArray
*array
= (GRealArray
*) farray
;
674 g_return_val_if_fail (array
, NULL
);
675 g_return_val_if_fail (index_
< array
->len
, NULL
);
676 g_return_val_if_fail (index_
+ length
<= array
->len
, NULL
);
678 if (array
->clear_func
!= NULL
)
682 for (i
= 0; i
< length
; i
++)
683 array
->clear_func (g_array_elt_pos (array
, index_
+ i
));
686 if (index_
+ length
!= array
->len
)
687 g_memmove (g_array_elt_pos (array
, index_
),
688 g_array_elt_pos (array
, index_
+ length
),
689 (array
->len
- (index_
+ length
)) * array
->elt_size
);
691 array
->len
-= length
;
692 if (G_UNLIKELY (g_mem_gc_friendly
))
693 g_array_elt_zero (array
, array
->len
, length
);
695 g_array_zero_terminate (array
);
703 * @compare_func: comparison function.
705 * Sorts a #GArray using @compare_func which should be a qsort()-style
706 * comparison function (returns less than zero for first arg is less
707 * than second arg, zero for equal, greater zero if first arg is
708 * greater than second arg).
710 * This is guaranteed to be a stable sort since version 2.32.
713 g_array_sort (GArray
*farray
,
714 GCompareFunc compare_func
)
716 GRealArray
*array
= (GRealArray
*) farray
;
718 g_return_if_fail (array
!= NULL
);
720 /* Don't use qsort as we want a guaranteed stable sort */
721 g_qsort_with_data (array
->data
,
724 (GCompareDataFunc
)compare_func
,
729 * g_array_sort_with_data:
731 * @compare_func: comparison function.
732 * @user_data: data to pass to @compare_func.
734 * Like g_array_sort(), but the comparison function receives an extra
735 * user data argument.
737 * This is guaranteed to be a stable sort since version 2.32.
739 * There used to be a comment here about making the sort stable by
740 * using the addresses of the elements in the comparison function.
741 * This did not actually work, so any such code should be removed.
744 g_array_sort_with_data (GArray
*farray
,
745 GCompareDataFunc compare_func
,
748 GRealArray
*array
= (GRealArray
*) farray
;
750 g_return_if_fail (array
!= NULL
);
752 g_qsort_with_data (array
->data
,
759 /* Returns the smallest power of 2 greater than n, or n if
760 * such power does not fit in a guint
763 g_nearest_pow (gint num
)
767 while (n
< num
&& n
> 0)
774 g_array_maybe_expand (GRealArray
*array
,
777 guint want_alloc
= g_array_elt_len (array
, array
->len
+ len
+
778 array
->zero_terminated
);
780 if (want_alloc
> array
->alloc
)
782 want_alloc
= g_nearest_pow (want_alloc
);
783 want_alloc
= MAX (want_alloc
, MIN_ARRAY_SIZE
);
785 array
->data
= g_realloc (array
->data
, want_alloc
);
787 if (G_UNLIKELY (g_mem_gc_friendly
))
788 memset (array
->data
+ array
->alloc
, 0, want_alloc
- array
->alloc
);
790 array
->alloc
= want_alloc
;
795 * SECTION:arrays_pointer
796 * @title: Pointer Arrays
797 * @short_description: arrays of pointers to any type of data, which
798 * grow automatically as new elements are added
800 * Pointer Arrays are similar to Arrays but are used only for storing
803 * <note><para>If you remove elements from the array, elements at the
804 * end of the array are moved into the space previously occupied by the
805 * removed element. This means that you should not rely on the index of
806 * particular elements remaining the same. You should also be careful
807 * when deleting elements while iterating over the array.</para></note>
809 * To create a pointer array, use g_ptr_array_new().
811 * To add elements to a pointer array, use g_ptr_array_add().
813 * To remove elements from a pointer array, use g_ptr_array_remove(),
814 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
816 * To access an element of a pointer array, use g_ptr_array_index().
818 * To set the size of a pointer array, use g_ptr_array_set_size().
820 * To free a pointer array, use g_ptr_array_free().
823 * <title>Using a #GPtrArray</title>
825 * GPtrArray *gparray;
826 * gchar *string1 = "one", *string2 = "two", *string3 = "three";
828 * gparray = g_ptr_array_new (<!-- -->);
829 * g_ptr_array_add (gparray, (gpointer) string1);
830 * g_ptr_array_add (gparray, (gpointer) string2);
831 * g_ptr_array_add (gparray, (gpointer) string3);
833 * if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
834 * g_print ("ERROR: got %p instead of %p\n",
835 * g_ptr_array_index (gparray, 0), string1);
837 * g_ptr_array_free (gparray, TRUE);
842 typedef struct _GRealPtrArray GRealPtrArray
;
846 * @pdata: points to the array of pointers, which may be moved when the
848 * @len: number of pointers in the array.
850 * Contains the public fields of a pointer array.
852 struct _GRealPtrArray
858 GDestroyNotify element_free_func
;
863 * @array: a #GPtrArray.
864 * @index_: the index of the pointer to return.
866 * Returns the pointer at the given index of the pointer array.
868 * Returns: the pointer at the given index.
871 static void g_ptr_array_maybe_expand (GRealPtrArray
*array
,
877 * Creates a new #GPtrArray with a reference count of 1.
879 * Returns: the new #GPtrArray.
882 g_ptr_array_new (void)
884 return g_ptr_array_sized_new (0);
888 * g_ptr_array_sized_new:
889 * @reserved_size: number of pointers preallocated.
891 * Creates a new #GPtrArray with @reserved_size pointers preallocated
892 * and a reference count of 1. This avoids frequent reallocation, if
893 * you are going to add many pointers to the array. Note however that
894 * the size of the array is still 0.
896 * Returns: the new #GPtrArray.
899 g_ptr_array_sized_new (guint reserved_size
)
901 GRealPtrArray
*array
= g_slice_new (GRealPtrArray
);
906 array
->ref_count
= 1;
907 array
->element_free_func
= NULL
;
909 if (reserved_size
!= 0)
910 g_ptr_array_maybe_expand (array
, reserved_size
);
912 return (GPtrArray
*) array
;
916 * g_ptr_array_new_with_free_func:
917 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
919 * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
920 * for freeing each element when the array is destroyed either via
921 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
922 * set to %TRUE or when removing elements.
924 * Returns: A new #GPtrArray.
929 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func
)
933 array
= g_ptr_array_new ();
934 g_ptr_array_set_free_func (array
, element_free_func
);
939 * g_ptr_array_new_full:
940 * @reserved_size: number of pointers preallocated.
941 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
943 * Creates a new #GPtrArray with @reserved_size pointers preallocated
944 * and a reference count of 1. This avoids frequent reallocation, if
945 * you are going to add many pointers to the array. Note however that
946 * the size of the array is still 0. It also set @element_free_func
947 * for freeing each element when the array is destroyed either via
948 * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
949 * set to %TRUE or when removing elements.
951 * Returns: A new #GPtrArray.
956 g_ptr_array_new_full (guint reserved_size
,
957 GDestroyNotify element_free_func
)
961 array
= g_ptr_array_sized_new (reserved_size
);
962 g_ptr_array_set_free_func (array
, element_free_func
);
967 * g_ptr_array_set_free_func:
968 * @array: A #GPtrArray.
969 * @element_free_func: (allow-none): A function to free elements with destroy @array or %NULL.
971 * Sets a function for freeing each element when @array is destroyed
972 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
973 * with @free_segment set to %TRUE or when removing elements.
978 g_ptr_array_set_free_func (GPtrArray
*array
,
979 GDestroyNotify element_free_func
)
981 GRealPtrArray
* rarray
= (GRealPtrArray
*) array
;
983 g_return_if_fail (array
);
985 rarray
->element_free_func
= element_free_func
;
990 * @array: a #GPtrArray
992 * Atomically increments the reference count of @array by one.
993 * This function is thread-safe and may be called from any thread.
995 * Returns: The passed in #GPtrArray
1000 g_ptr_array_ref (GPtrArray
*array
)
1002 GRealPtrArray
*rarray
= (GRealPtrArray
*) array
;
1004 g_return_val_if_fail (array
, NULL
);
1006 g_atomic_int_inc (&rarray
->ref_count
);
1011 static gpointer
*ptr_array_free (GPtrArray
*, ArrayFreeFlags
);
1014 * g_ptr_array_unref:
1015 * @array: A #GPtrArray.
1017 * Atomically decrements the reference count of @array by one. If the
1018 * reference count drops to 0, the effect is the same as calling
1019 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1020 * is MT-safe and may be called from any thread.
1025 g_ptr_array_unref (GPtrArray
*array
)
1027 GRealPtrArray
*rarray
= (GRealPtrArray
*) array
;
1028 g_return_if_fail (array
);
1030 if (g_atomic_int_dec_and_test (&rarray
->ref_count
))
1031 ptr_array_free (array
, FREE_SEGMENT
);
1036 * @array: a #GPtrArray.
1037 * @free_seg: if %TRUE the actual pointer array is freed as well.
1039 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1040 * it frees the memory block holding the elements as well. Pass %FALSE
1041 * if you want to free the #GPtrArray wrapper but preserve the
1042 * underlying array for use elsewhere. If the reference count of @array
1043 * is greater than one, the #GPtrArray wrapper is preserved but the
1044 * size of @array will be set to zero.
1046 * <note><para>If array contents point to dynamically-allocated
1047 * memory, they should be freed separately if @free_seg is %TRUE and no
1048 * #GDestroyNotify function has been set for @array.</para></note>
1050 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1051 * The pointer array should be freed using g_free().
1054 g_ptr_array_free (GPtrArray
*farray
,
1055 gboolean free_segment
)
1057 GRealPtrArray
*array
= (GRealPtrArray
*) farray
;
1058 ArrayFreeFlags flags
;
1060 g_return_val_if_fail (array
, NULL
);
1062 flags
= (free_segment
? FREE_SEGMENT
: 0);
1064 /* if others are holding a reference, preserve the wrapper but do free/return the data */
1065 if (!g_atomic_int_dec_and_test (&array
->ref_count
))
1066 flags
|= PRESERVE_WRAPPER
;
1068 return ptr_array_free (farray
, flags
);
1072 ptr_array_free (GPtrArray
*farray
,
1073 ArrayFreeFlags flags
)
1075 GRealPtrArray
*array
= (GRealPtrArray
*) farray
;
1078 if (flags
& FREE_SEGMENT
)
1080 if (array
->element_free_func
!= NULL
)
1081 g_ptr_array_foreach (farray
, (GFunc
) array
->element_free_func
, NULL
);
1082 g_free (array
->pdata
);
1086 segment
= array
->pdata
;
1088 if (flags
& PRESERVE_WRAPPER
)
1090 array
->pdata
= NULL
;
1096 g_slice_free1 (sizeof (GRealPtrArray
), array
);
1103 g_ptr_array_maybe_expand (GRealPtrArray
*array
,
1106 if ((array
->len
+ len
) > array
->alloc
)
1108 guint old_alloc
= array
->alloc
;
1109 array
->alloc
= g_nearest_pow (array
->len
+ len
);
1110 array
->alloc
= MAX (array
->alloc
, MIN_ARRAY_SIZE
);
1111 array
->pdata
= g_realloc (array
->pdata
, sizeof (gpointer
) * array
->alloc
);
1112 if (G_UNLIKELY (g_mem_gc_friendly
))
1113 for ( ; old_alloc
< array
->alloc
; old_alloc
++)
1114 array
->pdata
[old_alloc
] = NULL
;
1119 * g_ptr_array_set_size:
1120 * @array: a #GPtrArray.
1121 * @length: the new length of the pointer array.
1123 * Sets the size of the array. When making the array larger,
1124 * newly-added elements will be set to %NULL. When making it smaller,
1125 * if @array has a non-%NULL #GDestroyNotify function then it will be
1126 * called for the removed elements.
1129 g_ptr_array_set_size (GPtrArray
*farray
,
1132 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1134 g_return_if_fail (array
);
1136 if (length
> array
->len
)
1139 g_ptr_array_maybe_expand (array
, (length
- array
->len
));
1141 * memset (array->pdata + array->len, 0,
1142 * sizeof (gpointer) * (length - array->len));
1143 * to make it really portable. Remember (void*)NULL needn't be
1144 * bitwise zero. It of course is silly not to use memset (..,0,..).
1146 for (i
= array
->len
; i
< length
; i
++)
1147 array
->pdata
[i
] = NULL
;
1149 else if (length
< array
->len
)
1150 g_ptr_array_remove_range (farray
, length
, array
->len
- length
);
1152 array
->len
= length
;
1156 * g_ptr_array_remove_index:
1157 * @array: a #GPtrArray.
1158 * @index_: the index of the pointer to remove.
1160 * Removes the pointer at the given index from the pointer array. The
1161 * following elements are moved down one place. If @array has a
1162 * non-%NULL #GDestroyNotify function it is called for the removed
1165 * Returns: the pointer which was removed.
1168 g_ptr_array_remove_index (GPtrArray
*farray
,
1171 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1174 g_return_val_if_fail (array
, NULL
);
1176 g_return_val_if_fail (index_
< array
->len
, NULL
);
1178 result
= array
->pdata
[index_
];
1180 if (array
->element_free_func
!= NULL
)
1181 array
->element_free_func (array
->pdata
[index_
]);
1183 if (index_
!= array
->len
- 1)
1184 g_memmove (array
->pdata
+ index_
, array
->pdata
+ index_
+ 1,
1185 sizeof (gpointer
) * (array
->len
- index_
- 1));
1189 if (G_UNLIKELY (g_mem_gc_friendly
))
1190 array
->pdata
[array
->len
] = NULL
;
1196 * g_ptr_array_remove_index_fast:
1197 * @array: a #GPtrArray.
1198 * @index_: the index of the pointer to remove.
1200 * Removes the pointer at the given index from the pointer array. The
1201 * last element in the array is used to fill in the space, so this
1202 * function does not preserve the order of the array. But it is faster
1203 * than g_ptr_array_remove_index(). If @array has a non-%NULL
1204 * #GDestroyNotify function it is called for the removed element.
1206 * Returns: the pointer which was removed.
1209 g_ptr_array_remove_index_fast (GPtrArray
*farray
,
1212 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1215 g_return_val_if_fail (array
, NULL
);
1217 g_return_val_if_fail (index_
< array
->len
, NULL
);
1219 result
= array
->pdata
[index_
];
1221 if (array
->element_free_func
!= NULL
)
1222 array
->element_free_func (array
->pdata
[index_
]);
1224 if (index_
!= array
->len
- 1)
1225 array
->pdata
[index_
] = array
->pdata
[array
->len
- 1];
1229 if (G_UNLIKELY (g_mem_gc_friendly
))
1230 array
->pdata
[array
->len
] = NULL
;
1236 * g_ptr_array_remove_range:
1237 * @array: a @GPtrArray.
1238 * @index_: the index of the first pointer to remove.
1239 * @length: the number of pointers to remove.
1241 * Removes the given number of pointers starting at the given index
1242 * from a #GPtrArray. The following elements are moved to close the
1243 * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1244 * for the removed elements.
1249 g_ptr_array_remove_range (GPtrArray
*farray
,
1253 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1256 g_return_if_fail (array
);
1257 g_return_if_fail (index_
< array
->len
);
1258 g_return_if_fail (index_
+ length
<= array
->len
);
1260 if (array
->element_free_func
!= NULL
)
1262 for (n
= index_
; n
< index_
+ length
; n
++)
1263 array
->element_free_func (array
->pdata
[n
]);
1266 if (index_
+ length
!= array
->len
)
1268 g_memmove (&array
->pdata
[index_
],
1269 &array
->pdata
[index_
+ length
],
1270 (array
->len
- (index_
+ length
)) * sizeof (gpointer
));
1273 array
->len
-= length
;
1274 if (G_UNLIKELY (g_mem_gc_friendly
))
1277 for (i
= 0; i
< length
; i
++)
1278 array
->pdata
[array
->len
+ i
] = NULL
;
1283 * g_ptr_array_remove:
1284 * @array: a #GPtrArray.
1285 * @data: the pointer to remove.
1287 * Removes the first occurrence of the given pointer from the pointer
1288 * array. The following elements are moved down one place. If @array
1289 * has a non-%NULL #GDestroyNotify function it is called for the
1292 * It returns %TRUE if the pointer was removed, or %FALSE if the
1293 * pointer was not found.
1295 * Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1296 * not found in the array.
1299 g_ptr_array_remove (GPtrArray
*farray
,
1302 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1305 g_return_val_if_fail (array
, FALSE
);
1307 for (i
= 0; i
< array
->len
; i
+= 1)
1309 if (array
->pdata
[i
] == data
)
1311 g_ptr_array_remove_index (farray
, i
);
1320 * g_ptr_array_remove_fast:
1321 * @array: a #GPtrArray.
1322 * @data: the pointer to remove.
1324 * Removes the first occurrence of the given pointer from the pointer
1325 * array. The last element in the array is used to fill in the space,
1326 * so this function does not preserve the order of the array. But it is
1327 * faster than g_ptr_array_remove(). If @array has a non-%NULL
1328 * #GDestroyNotify function it is called for the removed element.
1330 * It returns %TRUE if the pointer was removed, or %FALSE if the
1331 * pointer was not found.
1333 * Returns: %TRUE if the pointer was found in the array.
1336 g_ptr_array_remove_fast (GPtrArray
*farray
,
1339 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1342 g_return_val_if_fail (array
, FALSE
);
1344 for (i
= 0; i
< array
->len
; i
+= 1)
1346 if (array
->pdata
[i
] == data
)
1348 g_ptr_array_remove_index_fast (farray
, i
);
1358 * @array: a #GPtrArray.
1359 * @data: the pointer to add.
1361 * Adds a pointer to the end of the pointer array. The array will grow
1362 * in size automatically if necessary.
1365 g_ptr_array_add (GPtrArray
*farray
,
1368 GRealPtrArray
* array
= (GRealPtrArray
*) farray
;
1370 g_return_if_fail (array
);
1372 g_ptr_array_maybe_expand (array
, 1);
1374 array
->pdata
[array
->len
++] = data
;
1379 * @array: a #GPtrArray.
1380 * @compare_func: comparison function.
1382 * Sorts the array, using @compare_func which should be a qsort()-style
1383 * comparison function (returns less than zero for first arg is less
1384 * than second arg, zero for equal, greater than zero if irst arg is
1385 * greater than second arg).
1387 * <note><para>The comparison function for g_ptr_array_sort() doesn't
1388 * take the pointers from the array as arguments, it takes pointers to
1389 * the pointers in the array.</para></note>
1391 * This is guaranteed to be a stable sort since version 2.32.
1394 g_ptr_array_sort (GPtrArray
*array
,
1395 GCompareFunc compare_func
)
1397 g_return_if_fail (array
!= NULL
);
1399 /* Don't use qsort as we want a guaranteed stable sort */
1400 g_qsort_with_data (array
->pdata
,
1403 (GCompareDataFunc
)compare_func
,
1408 * g_ptr_array_sort_with_data:
1409 * @array: a #GPtrArray.
1410 * @compare_func: comparison function.
1411 * @user_data: data to pass to @compare_func.
1413 * Like g_ptr_array_sort(), but the comparison function has an extra
1414 * user data argument.
1416 * <note><para>The comparison function for g_ptr_array_sort_with_data()
1417 * doesn't take the pointers from the array as arguments, it takes
1418 * pointers to the pointers in the array.</para></note>
1420 * This is guaranteed to be a stable sort since version 2.32.
1423 g_ptr_array_sort_with_data (GPtrArray
*array
,
1424 GCompareDataFunc compare_func
,
1427 g_return_if_fail (array
!= NULL
);
1429 g_qsort_with_data (array
->pdata
,
1437 * g_ptr_array_foreach:
1438 * @array: a #GPtrArray
1439 * @func: the function to call for each array element
1440 * @user_data: user data to pass to the function
1442 * Calls a function for each element of a #GPtrArray.
1447 g_ptr_array_foreach (GPtrArray
*array
,
1453 g_return_if_fail (array
);
1455 for (i
= 0; i
< array
->len
; i
++)
1456 (*func
) (array
->pdata
[i
], user_data
);
1460 * SECTION:arrays_byte
1461 * @title: Byte Arrays
1462 * @short_description: arrays of bytes
1464 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1465 * of bytes which grow automatically as elements are added.
1467 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1468 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1470 * To set the size of a #GByteArray, use g_byte_array_set_size().
1472 * To free a #GByteArray, use g_byte_array_free().
1475 * <title>Using a #GByteArray</title>
1477 * GByteArray *gbarray;
1480 * gbarray = g_byte_array_new (<!-- -->);
1481 * for (i = 0; i < 10000; i++)
1482 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1484 * for (i = 0; i < 10000; i++)
1486 * g_assert (gbarray->data[4*i] == 'a');
1487 * g_assert (gbarray->data[4*i+1] == 'b');
1488 * g_assert (gbarray->data[4*i+2] == 'c');
1489 * g_assert (gbarray->data[4*i+3] == 'd');
1492 * g_byte_array_free (gbarray, TRUE);
1496 * See #GBytes if you are interested in an immutable object representing a
1497 * sequence of bytes.
1502 * @data: a pointer to the element data. The data may be moved as
1503 * elements are added to the #GByteArray.
1504 * @len: the number of elements in the #GByteArray.
1506 * The <structname>GByteArray</structname> struct allows access to the
1507 * public fields of a <structname>GByteArray</structname>.
1513 * Creates a new #GByteArray with a reference count of 1.
1515 * Returns: (transfer full): the new #GByteArray.
1517 GByteArray
* g_byte_array_new (void)
1519 return (GByteArray
*) g_array_sized_new (FALSE
, FALSE
, 1, 0);
1523 * g_byte_array_new_take:
1524 * @data: (transfer full) (array length=len): byte data for the array
1525 * @len: length of @data
1527 * Create byte array containing the data. The data will be owned by the array
1528 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1532 * Returns: (transfer full): a new #GByteArray
1535 g_byte_array_new_take (guint8
*data
,
1541 array
= g_byte_array_new ();
1542 real
= (GRealArray
*)array
;
1543 g_assert (real
->data
== NULL
);
1544 g_assert (real
->len
== 0);
1553 * g_byte_array_sized_new:
1554 * @reserved_size: number of bytes preallocated.
1556 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1557 * This avoids frequent reallocation, if you are going to add many
1558 * bytes to the array. Note however that the size of the array is still
1561 * Returns: the new #GByteArray.
1563 GByteArray
* g_byte_array_sized_new (guint reserved_size
)
1565 return (GByteArray
*) g_array_sized_new (FALSE
, FALSE
, 1, reserved_size
);
1569 * g_byte_array_free:
1570 * @array: a #GByteArray.
1571 * @free_segment: if %TRUE the actual byte data is freed as well.
1573 * Frees the memory allocated by the #GByteArray. If @free_segment is
1574 * %TRUE it frees the actual byte data. If the reference count of
1575 * @array is greater than one, the #GByteArray wrapper is preserved but
1576 * the size of @array will be set to zero.
1578 * Returns: the element data if @free_segment is %FALSE, otherwise
1579 * %NULL. The element data should be freed using g_free().
1581 guint8
* g_byte_array_free (GByteArray
*array
,
1582 gboolean free_segment
)
1584 return (guint8
*) g_array_free ((GArray
*) array
, free_segment
);
1588 * g_byte_array_free_to_bytes:
1589 * @array: (transfer full): a #GByteArray
1591 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1593 * The #GByteArray is freed unless the reference count of @array is greater
1594 * than one, the #GByteArray wrapper is preserved but the size of @array
1595 * will be set to zero.
1597 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1602 * Returns: (transfer full): a new immutable #GBytes representing same byte
1603 * data that was in the array
1606 g_byte_array_free_to_bytes (GByteArray
*array
)
1610 g_return_val_if_fail (array
!= NULL
, NULL
);
1612 length
= array
->len
;
1613 return g_bytes_new_take (g_byte_array_free (array
, FALSE
), length
);
1618 * @array: A #GByteArray.
1620 * Atomically increments the reference count of @array by one. This
1621 * function is MT-safe and may be called from any thread.
1623 * Returns: The passed in #GByteArray.
1628 g_byte_array_ref (GByteArray
*array
)
1630 return (GByteArray
*) g_array_ref ((GArray
*) array
);
1634 * g_byte_array_unref:
1635 * @array: A #GByteArray.
1637 * Atomically decrements the reference count of @array by one. If the
1638 * reference count drops to 0, all memory allocated by the array is
1639 * released. This function is MT-safe and may be called from any
1645 g_byte_array_unref (GByteArray
*array
)
1647 g_array_unref ((GArray
*) array
);
1651 * g_byte_array_append:
1652 * @array: a #GByteArray.
1653 * @data: the byte data to be added.
1654 * @len: the number of bytes to add.
1656 * Adds the given bytes to the end of the #GByteArray. The array will
1657 * grow in size automatically if necessary.
1659 * Returns: the #GByteArray.
1661 GByteArray
* g_byte_array_append (GByteArray
*array
,
1665 g_array_append_vals ((GArray
*) array
, (guint8
*)data
, len
);
1671 * g_byte_array_prepend:
1672 * @array: a #GByteArray.
1673 * @data: the byte data to be added.
1674 * @len: the number of bytes to add.
1676 * Adds the given data to the start of the #GByteArray. The array will
1677 * grow in size automatically if necessary.
1679 * Returns: the #GByteArray.
1681 GByteArray
* g_byte_array_prepend (GByteArray
*array
,
1685 g_array_prepend_vals ((GArray
*) array
, (guint8
*)data
, len
);
1691 * g_byte_array_set_size:
1692 * @array: a #GByteArray.
1693 * @length: the new size of the #GByteArray.
1695 * Sets the size of the #GByteArray, expanding it if necessary.
1697 * Returns: the #GByteArray.
1699 GByteArray
* g_byte_array_set_size (GByteArray
*array
,
1702 g_array_set_size ((GArray
*) array
, length
);
1708 * g_byte_array_remove_index:
1709 * @array: a #GByteArray.
1710 * @index_: the index of the byte to remove.
1712 * Removes the byte at the given index from a #GByteArray. The
1713 * following bytes are moved down one place.
1715 * Returns: the #GByteArray.
1717 GByteArray
* g_byte_array_remove_index (GByteArray
*array
,
1720 g_array_remove_index ((GArray
*) array
, index_
);
1726 * g_byte_array_remove_index_fast:
1727 * @array: a #GByteArray.
1728 * @index_: the index of the byte to remove.
1730 * Removes the byte at the given index from a #GByteArray. The last
1731 * element in the array is used to fill in the space, so this function
1732 * does not preserve the order of the #GByteArray. But it is faster
1733 * than g_byte_array_remove_index().
1735 * Returns: the #GByteArray.
1737 GByteArray
* g_byte_array_remove_index_fast (GByteArray
*array
,
1740 g_array_remove_index_fast ((GArray
*) array
, index_
);
1746 * g_byte_array_remove_range:
1747 * @array: a @GByteArray.
1748 * @index_: the index of the first byte to remove.
1749 * @length: the number of bytes to remove.
1751 * Removes the given number of bytes starting at the given index from a
1752 * #GByteArray. The following elements are moved to close the gap.
1754 * Returns: the #GByteArray.
1759 g_byte_array_remove_range (GByteArray
*array
,
1763 g_return_val_if_fail (array
, NULL
);
1764 g_return_val_if_fail (index_
< array
->len
, NULL
);
1765 g_return_val_if_fail (index_
+ length
<= array
->len
, NULL
);
1767 return (GByteArray
*)g_array_remove_range ((GArray
*) array
, index_
, length
);
1771 * g_byte_array_sort:
1772 * @array: a #GByteArray.
1773 * @compare_func: comparison function.
1775 * Sorts a byte array, using @compare_func which should be a
1776 * qsort()-style comparison function (returns less than zero for first
1777 * arg is less than second arg, zero for equal, greater than zero if
1778 * first arg is greater than second arg).
1780 * If two array elements compare equal, their order in the sorted array
1781 * is undefined. If you want equal elements to keep their order (i.e.
1782 * you want a stable sort) you can write a comparison function that,
1783 * if two elements would otherwise compare equal, compares them by
1787 g_byte_array_sort (GByteArray
*array
,
1788 GCompareFunc compare_func
)
1790 g_array_sort ((GArray
*) array
, compare_func
);
1794 * g_byte_array_sort_with_data:
1795 * @array: a #GByteArray.
1796 * @compare_func: comparison function.
1797 * @user_data: data to pass to @compare_func.
1799 * Like g_byte_array_sort(), but the comparison function takes an extra
1800 * user data argument.
1803 g_byte_array_sort_with_data (GByteArray
*array
,
1804 GCompareDataFunc compare_func
,
1807 g_array_sort_with_data ((GArray
*) array
, compare_func
, user_data
);