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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
39 #include "gtestutils.h"
41 #include "gmessages.h"
48 * @short_description: arrays of arbitrary elements which grow
49 * automatically as elements are added
51 * Arrays are similar to standard C arrays, except that they grow
52 * automatically as elements are added.
54 * Array elements can be of any size (though all elements of one array
55 * are the same size), and the array can be automatically cleared to
56 * '0's and zero-terminated.
58 * To create a new array use g_array_new().
60 * To add elements to an array, use g_array_append_val(),
61 * g_array_append_vals(), g_array_prepend_val(), and
62 * g_array_prepend_vals().
64 * To access an element of an array, use g_array_index().
66 * To set the size of an array, use g_array_set_size().
68 * To free an array, use g_array_free().
70 * Here is an example that stores integers in a #GArray:
71 * |[<!-- language="C" -->
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);
87 #define MIN_ARRAY_SIZE 16
89 typedef struct _GRealArray GRealArray
;
93 * @data: a pointer to the element data. The data may be moved as
94 * elements are added to the #GArray.
95 * @len: the number of elements in the #GArray not including the
96 * possible terminating zero element.
98 * Contains the public fields of a GArray.
106 guint zero_terminated
: 1;
109 GDestroyNotify clear_func
;
115 * @t: the type of the elements
116 * @i: the index of the element to return
118 * Returns the element of a #GArray at the given index. The return
119 * value is cast to the given type.
121 * This example gets a pointer to an element in a #GArray:
122 * |[<!-- language="C" -->
123 * EDayViewEvent *event;
124 * // This gets a pointer to the 4th element in the array of
125 * // EDayViewEvent structs.
126 * event = &g_array_index (events, EDayViewEvent, 3);
129 * Returns: the element of the #GArray at the index given by @i
132 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
133 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
134 #define g_array_elt_zero(array, pos, len) \
135 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
136 #define g_array_zero_terminate(array) G_STMT_START{ \
137 if ((array)->zero_terminated) \
138 g_array_elt_zero ((array), (array)->len, 1); \
141 static guint
g_nearest_pow (gint num
) G_GNUC_CONST
;
142 static void g_array_maybe_expand (GRealArray
*array
,
147 * @zero_terminated: %TRUE if the array should have an extra element at
148 * the end which is set to 0
149 * @clear_: %TRUE if #GArray elements should be automatically cleared
150 * to 0 when they are allocated
151 * @element_size: the size of each element in bytes
153 * Creates a new #GArray with a reference count of 1.
155 * Returns: the new #GArray
158 g_array_new (gboolean zero_terminated
,
162 g_return_val_if_fail (elt_size
> 0, NULL
);
164 return g_array_sized_new (zero_terminated
, clear
, elt_size
, 0);
169 * @zero_terminated: %TRUE if the array should have an extra element at
170 * the end with all bits cleared
171 * @clear_: %TRUE if all bits in the array should be cleared to 0 on
173 * @element_size: size of each element in the array
174 * @reserved_size: number of elements preallocated
176 * Creates a new #GArray with @reserved_size elements preallocated and
177 * a reference count of 1. This avoids frequent reallocation, if you
178 * are going to add many elements to the array. Note however that the
179 * size of the array is still 0.
181 * Returns: the new #GArray
184 g_array_sized_new (gboolean zero_terminated
,
191 g_return_val_if_fail (elt_size
> 0, NULL
);
193 array
= g_slice_new (GRealArray
);
198 array
->zero_terminated
= (zero_terminated
? 1 : 0);
199 array
->clear
= (clear
? 1 : 0);
200 array
->elt_size
= elt_size
;
201 array
->ref_count
= 1;
202 array
->clear_func
= NULL
;
204 if (array
->zero_terminated
|| reserved_size
!= 0)
206 g_array_maybe_expand (array
, reserved_size
);
207 g_array_zero_terminate(array
);
210 return (GArray
*) array
;
214 * g_array_set_clear_func:
216 * @clear_func: a function to clear an element of @array
218 * Sets a function to clear an element of @array.
220 * The @clear_func will be called when an element in the array
221 * data segment is removed and when the array is freed and data
222 * segment is deallocated as well.
224 * Note that in contrast with other uses of #GDestroyNotify
225 * functions, @clear_func is expected to clear the contents of
226 * the array element it is given, but not free the element itself.
231 g_array_set_clear_func (GArray
*array
,
232 GDestroyNotify clear_func
)
234 GRealArray
*rarray
= (GRealArray
*) array
;
236 g_return_if_fail (array
!= NULL
);
238 rarray
->clear_func
= clear_func
;
245 * Atomically increments the reference count of @array by one.
246 * This function is MT-safe and may be called from any thread.
248 * Returns: The passed in #GArray
253 g_array_ref (GArray
*array
)
255 GRealArray
*rarray
= (GRealArray
*) array
;
256 g_return_val_if_fail (array
, NULL
);
258 g_atomic_int_inc (&rarray
->ref_count
);
265 FREE_SEGMENT
= 1 << 0,
266 PRESERVE_WRAPPER
= 1 << 1
269 static gchar
*array_free (GRealArray
*, ArrayFreeFlags
);
275 * Atomically decrements the reference count of @array by one. If the
276 * reference count drops to 0, all memory allocated by the array is
277 * released. This function is MT-safe and may be called from any
283 g_array_unref (GArray
*array
)
285 GRealArray
*rarray
= (GRealArray
*) array
;
286 g_return_if_fail (array
);
288 if (g_atomic_int_dec_and_test (&rarray
->ref_count
))
289 array_free (rarray
, FREE_SEGMENT
);
293 * g_array_get_element_size:
296 * Gets the size of the elements in @array.
298 * Returns: Size of each element, in bytes
303 g_array_get_element_size (GArray
*array
)
305 GRealArray
*rarray
= (GRealArray
*) array
;
307 g_return_val_if_fail (array
, 0);
309 return rarray
->elt_size
;
315 * @free_segment: if %TRUE the actual element data is freed as well
317 * Frees the memory allocated for the #GArray. If @free_segment is
318 * %TRUE it frees the memory block holding the elements as well and
319 * also each element if @array has a @element_free_func set. Pass
320 * %FALSE if you want to free the #GArray wrapper but preserve the
321 * underlying array for use elsewhere. If the reference count of @array
322 * is greater than one, the #GArray wrapper is preserved but the size
323 * of @array will be set to zero.
325 * If array elements contain dynamically-allocated memory, they should
326 * be freed separately.
328 * Returns: the element data if @free_segment is %FALSE, otherwise
329 * %NULL. The element data should be freed using g_free().
332 g_array_free (GArray
*farray
,
333 gboolean free_segment
)
335 GRealArray
*array
= (GRealArray
*) farray
;
336 ArrayFreeFlags flags
;
338 g_return_val_if_fail (array
, NULL
);
340 flags
= (free_segment
? FREE_SEGMENT
: 0);
342 /* if others are holding a reference, preserve the wrapper but do free/return the data */
343 if (!g_atomic_int_dec_and_test (&array
->ref_count
))
344 flags
|= PRESERVE_WRAPPER
;
346 return array_free (array
, flags
);
350 array_free (GRealArray
*array
,
351 ArrayFreeFlags flags
)
355 if (flags
& FREE_SEGMENT
)
357 if (array
->clear_func
!= NULL
)
361 for (i
= 0; i
< array
->len
; i
++)
362 array
->clear_func (g_array_elt_pos (array
, i
));
365 g_free (array
->data
);
369 segment
= (gchar
*) array
->data
;
371 if (flags
& PRESERVE_WRAPPER
)
379 g_slice_free1 (sizeof (GRealArray
), array
);
386 * g_array_append_vals:
388 * @data: (not nullable): a pointer to the elements to append to the end of the array
389 * @len: the number of elements to append
391 * Adds @len elements onto the end of the array.
393 * Returns: the #GArray
396 * g_array_append_val:
398 * @v: the value to append to the #GArray
400 * Adds the value on to the end of the array. The array will grow in
401 * size automatically if necessary.
403 * g_array_append_val() is a macro which uses a reference to the value
404 * parameter @v. This means that you cannot use it with literal values
405 * such as "27". You must use variables.
407 * Returns: the #GArray
410 g_array_append_vals (GArray
*farray
,
414 GRealArray
*array
= (GRealArray
*) farray
;
416 g_return_val_if_fail (array
, NULL
);
421 g_array_maybe_expand (array
, len
);
423 memcpy (g_array_elt_pos (array
, array
->len
), data
,
424 g_array_elt_len (array
, len
));
428 g_array_zero_terminate (array
);
434 * g_array_prepend_vals:
436 * @data: (not nullable): a pointer to the elements to prepend to the start of the array
437 * @len: the number of elements to prepend
439 * Adds @len elements onto the start of the array.
441 * This operation is slower than g_array_append_vals() since the
442 * existing elements in the array have to be moved to make space for
445 * Returns: the #GArray
448 * g_array_prepend_val:
450 * @v: the value to prepend to the #GArray
452 * Adds the value on to the start of the array. The array will grow in
453 * size automatically if necessary.
455 * This operation is slower than g_array_append_val() since the
456 * existing elements in the array have to be moved to make space for
459 * g_array_prepend_val() is a macro which uses a reference to the value
460 * parameter @v. This means that you cannot use it with literal values
461 * such as "27". You must use variables.
463 * Returns: the #GArray
466 g_array_prepend_vals (GArray
*farray
,
470 GRealArray
*array
= (GRealArray
*) farray
;
472 g_return_val_if_fail (array
, NULL
);
477 g_array_maybe_expand (array
, len
);
479 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: (not nullable): 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 * g_array_insert_val() is a macro which uses a reference to the value
511 * parameter @v. This means that you cannot use it with literal values
512 * such as "27". You must use variables.
514 * Returns: the #GArray
517 g_array_insert_vals (GArray
*farray
,
522 GRealArray
*array
= (GRealArray
*) farray
;
524 g_return_val_if_fail (array
, NULL
);
529 g_array_maybe_expand (array
, len
);
531 memmove (g_array_elt_pos (array
, len
+ index_
),
532 g_array_elt_pos (array
, index_
),
533 g_array_elt_len (array
, array
->len
- index_
));
535 memcpy (g_array_elt_pos (array
, index_
), data
, g_array_elt_len (array
, len
));
539 g_array_zero_terminate (array
);
547 * @length: the new size of the #GArray
549 * Sets the size of the array, expanding it if necessary. If the array
550 * was created with @clear_ set to %TRUE, the new elements are set to 0.
552 * Returns: the #GArray
555 g_array_set_size (GArray
*farray
,
558 GRealArray
*array
= (GRealArray
*) farray
;
560 g_return_val_if_fail (array
, NULL
);
562 if (length
> array
->len
)
564 g_array_maybe_expand (array
, length
- array
->len
);
567 g_array_elt_zero (array
, array
->len
, length
- array
->len
);
569 else if (length
< array
->len
)
570 g_array_remove_range (farray
, length
, array
->len
- length
);
574 g_array_zero_terminate (array
);
580 * g_array_remove_index:
582 * @index_: the index of the element to remove
584 * Removes the element at the given index from a #GArray. The following
585 * elements are moved down one place.
587 * Returns: the #GArray
590 g_array_remove_index (GArray
*farray
,
593 GRealArray
* array
= (GRealArray
*) farray
;
595 g_return_val_if_fail (array
, NULL
);
597 g_return_val_if_fail (index_
< array
->len
, NULL
);
599 if (array
->clear_func
!= NULL
)
600 array
->clear_func (g_array_elt_pos (array
, index_
));
602 if (index_
!= array
->len
- 1)
603 memmove (g_array_elt_pos (array
, index_
),
604 g_array_elt_pos (array
, index_
+ 1),
605 g_array_elt_len (array
, array
->len
- index_
- 1));
609 if (G_UNLIKELY (g_mem_gc_friendly
))
610 g_array_elt_zero (array
, array
->len
, 1);
612 g_array_zero_terminate (array
);
618 * g_array_remove_index_fast:
620 * @index_: the index of the element to remove
622 * Removes the element at the given index from a #GArray. The last
623 * element in the array is used to fill in the space, so this function
624 * does not preserve the order of the #GArray. But it is faster than
625 * g_array_remove_index().
627 * Returns: the #GArray
630 g_array_remove_index_fast (GArray
*farray
,
633 GRealArray
* array
= (GRealArray
*) farray
;
635 g_return_val_if_fail (array
, NULL
);
637 g_return_val_if_fail (index_
< array
->len
, NULL
);
639 if (array
->clear_func
!= NULL
)
640 array
->clear_func (g_array_elt_pos (array
, index_
));
642 if (index_
!= array
->len
- 1)
643 memcpy (g_array_elt_pos (array
, index_
),
644 g_array_elt_pos (array
, array
->len
- 1),
645 g_array_elt_len (array
, 1));
649 if (G_UNLIKELY (g_mem_gc_friendly
))
650 g_array_elt_zero (array
, array
->len
, 1);
652 g_array_zero_terminate (array
);
658 * g_array_remove_range:
660 * @index_: the index of the first element to remove
661 * @length: the number of elements to remove
663 * Removes the given number of elements starting at the given index
664 * from a #GArray. The following elements are moved to close the gap.
666 * Returns: the #GArray
671 g_array_remove_range (GArray
*farray
,
675 GRealArray
*array
= (GRealArray
*) farray
;
677 g_return_val_if_fail (array
, NULL
);
678 g_return_val_if_fail (index_
<= array
->len
, NULL
);
679 g_return_val_if_fail (index_
+ length
<= array
->len
, NULL
);
681 if (array
->clear_func
!= NULL
)
685 for (i
= 0; i
< length
; i
++)
686 array
->clear_func (g_array_elt_pos (array
, index_
+ i
));
689 if (index_
+ length
!= array
->len
)
690 memmove (g_array_elt_pos (array
, index_
),
691 g_array_elt_pos (array
, index_
+ length
),
692 (array
->len
- (index_
+ length
)) * array
->elt_size
);
694 array
->len
-= length
;
695 if (G_UNLIKELY (g_mem_gc_friendly
))
696 g_array_elt_zero (array
, array
->len
, length
);
698 g_array_zero_terminate (array
);
706 * @compare_func: comparison function
708 * Sorts a #GArray using @compare_func which should be a qsort()-style
709 * comparison function (returns less than zero for first arg is less
710 * than second arg, zero for equal, greater zero if first arg is
711 * greater than second arg).
713 * This is guaranteed to be a stable sort since version 2.32.
716 g_array_sort (GArray
*farray
,
717 GCompareFunc compare_func
)
719 GRealArray
*array
= (GRealArray
*) farray
;
721 g_return_if_fail (array
!= NULL
);
723 /* Don't use qsort as we want a guaranteed stable sort */
724 g_qsort_with_data (array
->data
,
727 (GCompareDataFunc
)compare_func
,
732 * g_array_sort_with_data:
734 * @compare_func: comparison function
735 * @user_data: data to pass to @compare_func
737 * Like g_array_sort(), but the comparison function receives an extra
738 * user data argument.
740 * This is guaranteed to be a stable sort since version 2.32.
742 * There used to be a comment here about making the sort stable by
743 * using the addresses of the elements in the comparison function.
744 * This did not actually work, so any such code should be removed.
747 g_array_sort_with_data (GArray
*farray
,
748 GCompareDataFunc compare_func
,
751 GRealArray
*array
= (GRealArray
*) farray
;
753 g_return_if_fail (array
!= NULL
);
755 g_qsort_with_data (array
->data
,
762 /* Returns the smallest power of 2 greater than n, or n if
763 * such power does not fit in a guint
766 g_nearest_pow (gint num
)
770 while (n
< num
&& n
> 0)
777 g_array_maybe_expand (GRealArray
*array
,
780 guint want_alloc
= g_array_elt_len (array
, array
->len
+ len
+
781 array
->zero_terminated
);
783 if (want_alloc
> array
->alloc
)
785 want_alloc
= g_nearest_pow (want_alloc
);
786 want_alloc
= MAX (want_alloc
, MIN_ARRAY_SIZE
);
788 array
->data
= g_realloc (array
->data
, want_alloc
);
790 if (G_UNLIKELY (g_mem_gc_friendly
))
791 memset (array
->data
+ array
->alloc
, 0, want_alloc
- array
->alloc
);
793 array
->alloc
= want_alloc
;
798 * SECTION:arrays_pointer
799 * @title: Pointer Arrays
800 * @short_description: arrays of pointers to any type of data, which
801 * grow automatically as new elements are added
803 * Pointer Arrays are similar to Arrays but are used only for storing
806 * If you remove elements from the array, elements at the end of the
807 * array are moved into the space previously occupied by the removed
808 * element. This means that you should not rely on the index of particular
809 * elements remaining the same. You should also be careful when deleting
810 * elements while iterating over the array.
812 * To create a pointer array, use g_ptr_array_new().
814 * To add elements to a pointer array, use g_ptr_array_add().
816 * To remove elements from a pointer array, use g_ptr_array_remove(),
817 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
819 * To access an element of a pointer array, use g_ptr_array_index().
821 * To set the size of a pointer array, use g_ptr_array_set_size().
823 * To free a pointer array, use g_ptr_array_free().
825 * An example using a #GPtrArray:
826 * |[<!-- language="C" -->
828 * gchar *string1 = "one";
829 * gchar *string2 = "two";
830 * gchar *string3 = "three";
832 * array = g_ptr_array_new ();
833 * g_ptr_array_add (array, (gpointer) string1);
834 * g_ptr_array_add (array, (gpointer) string2);
835 * g_ptr_array_add (array, (gpointer) string3);
837 * if (g_ptr_array_index (array, 0) != (gpointer) string1)
838 * g_print ("ERROR: got %p instead of %p\n",
839 * g_ptr_array_index (array, 0), string1);
841 * g_ptr_array_free (array, TRUE);
845 typedef struct _GRealPtrArray GRealPtrArray
;
849 * @pdata: points to the array of pointers, which may be moved when the
851 * @len: number of pointers in the array
853 * Contains the public fields of a pointer array.
855 struct _GRealPtrArray
861 GDestroyNotify element_free_func
;
866 * @array: a #GPtrArray
867 * @index_: the index of the pointer to return
869 * Returns the pointer at the given index of the pointer array.
871 * This does not perform bounds checking on the given @index_,
872 * so you are responsible for checking it against the array length.
874 * Returns: the pointer at the given index
877 static void g_ptr_array_maybe_expand (GRealPtrArray
*array
,
883 * Creates a new #GPtrArray with a reference count of 1.
885 * Returns: the new #GPtrArray
888 g_ptr_array_new (void)
890 return g_ptr_array_sized_new (0);
894 * g_ptr_array_sized_new:
895 * @reserved_size: number of pointers preallocated
897 * Creates a new #GPtrArray with @reserved_size pointers preallocated
898 * and a reference count of 1. This avoids frequent reallocation, if
899 * you are going to add many pointers to the array. Note however that
900 * the size of the array is still 0.
902 * Returns: the new #GPtrArray
905 g_ptr_array_sized_new (guint reserved_size
)
907 GRealPtrArray
*array
;
909 array
= g_slice_new (GRealPtrArray
);
914 array
->ref_count
= 1;
915 array
->element_free_func
= NULL
;
917 if (reserved_size
!= 0)
918 g_ptr_array_maybe_expand (array
, reserved_size
);
920 return (GPtrArray
*) array
;
924 * g_ptr_array_new_with_free_func:
925 * @element_free_func: (nullable): A function to free elements with
926 * destroy @array or %NULL
928 * Creates a new #GPtrArray with a reference count of 1 and use
929 * @element_free_func for freeing each element when the array is destroyed
930 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
931 * @free_segment set to %TRUE or when removing elements.
933 * Returns: A new #GPtrArray
938 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func
)
942 array
= g_ptr_array_new ();
943 g_ptr_array_set_free_func (array
, element_free_func
);
949 * g_ptr_array_new_full:
950 * @reserved_size: number of pointers preallocated
951 * @element_free_func: (nullable): A function to free elements with
952 * destroy @array or %NULL
954 * Creates a new #GPtrArray with @reserved_size pointers preallocated
955 * and a reference count of 1. This avoids frequent reallocation, if
956 * you are going to add many pointers to the array. Note however that
957 * the size of the array is still 0. It also set @element_free_func
958 * for freeing each element when the array is destroyed either via
959 * g_ptr_array_unref(), when g_ptr_array_free() is called with
960 * @free_segment set to %TRUE or when removing elements.
962 * Returns: A new #GPtrArray
967 g_ptr_array_new_full (guint reserved_size
,
968 GDestroyNotify element_free_func
)
972 array
= g_ptr_array_sized_new (reserved_size
);
973 g_ptr_array_set_free_func (array
, element_free_func
);
979 * g_ptr_array_set_free_func:
980 * @array: A #GPtrArray
981 * @element_free_func: (nullable): A function to free elements with
982 * destroy @array or %NULL
984 * Sets a function for freeing each element when @array is destroyed
985 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
986 * with @free_segment set to %TRUE or when removing elements.
991 g_ptr_array_set_free_func (GPtrArray
*array
,
992 GDestroyNotify element_free_func
)
994 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
996 g_return_if_fail (array
);
998 rarray
->element_free_func
= element_free_func
;
1003 * @array: a #GPtrArray
1005 * Atomically increments the reference count of @array by one.
1006 * This function is thread-safe and may be called from any thread.
1008 * Returns: The passed in #GPtrArray
1013 g_ptr_array_ref (GPtrArray
*array
)
1015 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1017 g_return_val_if_fail (array
, NULL
);
1019 g_atomic_int_inc (&rarray
->ref_count
);
1024 static gpointer
*ptr_array_free (GPtrArray
*, ArrayFreeFlags
);
1027 * g_ptr_array_unref:
1028 * @array: A #GPtrArray
1030 * Atomically decrements the reference count of @array by one. If the
1031 * reference count drops to 0, the effect is the same as calling
1032 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1033 * is MT-safe and may be called from any thread.
1038 g_ptr_array_unref (GPtrArray
*array
)
1040 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1042 g_return_if_fail (array
);
1044 if (g_atomic_int_dec_and_test (&rarray
->ref_count
))
1045 ptr_array_free (array
, FREE_SEGMENT
);
1050 * @array: a #GPtrArray
1051 * @free_seg: if %TRUE the actual pointer array is freed as well
1053 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1054 * it frees the memory block holding the elements as well. Pass %FALSE
1055 * if you want to free the #GPtrArray wrapper but preserve the
1056 * underlying array for use elsewhere. If the reference count of @array
1057 * is greater than one, the #GPtrArray wrapper is preserved but the
1058 * size of @array will be set to zero.
1060 * If array contents point to dynamically-allocated memory, they should
1061 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
1062 * function has been set for @array.
1064 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1065 * The pointer array should be freed using g_free().
1068 g_ptr_array_free (GPtrArray
*array
,
1069 gboolean free_segment
)
1071 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1072 ArrayFreeFlags flags
;
1074 g_return_val_if_fail (rarray
, NULL
);
1076 flags
= (free_segment
? FREE_SEGMENT
: 0);
1078 /* if others are holding a reference, preserve the wrapper but
1079 * do free/return the data
1081 if (!g_atomic_int_dec_and_test (&rarray
->ref_count
))
1082 flags
|= PRESERVE_WRAPPER
;
1084 return ptr_array_free (array
, flags
);
1088 ptr_array_free (GPtrArray
*array
,
1089 ArrayFreeFlags flags
)
1091 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1094 if (flags
& FREE_SEGMENT
)
1096 if (rarray
->element_free_func
!= NULL
)
1097 g_ptr_array_foreach (array
, (GFunc
) rarray
->element_free_func
, NULL
);
1098 g_free (rarray
->pdata
);
1102 segment
= rarray
->pdata
;
1104 if (flags
& PRESERVE_WRAPPER
)
1106 rarray
->pdata
= NULL
;
1112 g_slice_free1 (sizeof (GRealPtrArray
), rarray
);
1119 g_ptr_array_maybe_expand (GRealPtrArray
*array
,
1122 if ((array
->len
+ len
) > array
->alloc
)
1124 guint old_alloc
= array
->alloc
;
1125 array
->alloc
= g_nearest_pow (array
->len
+ len
);
1126 array
->alloc
= MAX (array
->alloc
, MIN_ARRAY_SIZE
);
1127 array
->pdata
= g_realloc (array
->pdata
, sizeof (gpointer
) * array
->alloc
);
1128 if (G_UNLIKELY (g_mem_gc_friendly
))
1129 for ( ; old_alloc
< array
->alloc
; old_alloc
++)
1130 array
->pdata
[old_alloc
] = NULL
;
1135 * g_ptr_array_set_size:
1136 * @array: a #GPtrArray
1137 * @length: the new length of the pointer array
1139 * Sets the size of the array. When making the array larger,
1140 * newly-added elements will be set to %NULL. When making it smaller,
1141 * if @array has a non-%NULL #GDestroyNotify function then it will be
1142 * called for the removed elements.
1145 g_ptr_array_set_size (GPtrArray
*array
,
1148 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1150 g_return_if_fail (rarray
);
1152 if (length
> rarray
->len
)
1155 g_ptr_array_maybe_expand (rarray
, (length
- rarray
->len
));
1157 * memset (array->pdata + array->len, 0,
1158 * sizeof (gpointer) * (length - array->len));
1159 * to make it really portable. Remember (void*)NULL needn't be
1160 * bitwise zero. It of course is silly not to use memset (..,0,..).
1162 for (i
= rarray
->len
; i
< length
; i
++)
1163 rarray
->pdata
[i
] = NULL
;
1165 else if (length
< rarray
->len
)
1166 g_ptr_array_remove_range (array
, length
, rarray
->len
- length
);
1168 rarray
->len
= length
;
1172 * g_ptr_array_remove_index:
1173 * @array: a #GPtrArray
1174 * @index_: the index of the pointer to remove
1176 * Removes the pointer at the given index from the pointer array.
1177 * The following elements are moved down one place. If @array has
1178 * a non-%NULL #GDestroyNotify function it is called for the removed
1181 * Returns: the pointer which was removed
1184 g_ptr_array_remove_index (GPtrArray
*array
,
1187 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1190 g_return_val_if_fail (rarray
, NULL
);
1192 g_return_val_if_fail (index_
< rarray
->len
, NULL
);
1194 result
= rarray
->pdata
[index_
];
1196 if (rarray
->element_free_func
!= NULL
)
1197 rarray
->element_free_func (rarray
->pdata
[index_
]);
1199 if (index_
!= rarray
->len
- 1)
1200 memmove (rarray
->pdata
+ index_
, rarray
->pdata
+ index_
+ 1,
1201 sizeof (gpointer
) * (rarray
->len
- index_
- 1));
1205 if (G_UNLIKELY (g_mem_gc_friendly
))
1206 rarray
->pdata
[rarray
->len
] = NULL
;
1212 * g_ptr_array_remove_index_fast:
1213 * @array: a #GPtrArray
1214 * @index_: the index of the pointer to remove
1216 * Removes the pointer at the given index from the pointer array.
1217 * The last element in the array is used to fill in the space, so
1218 * this function does not preserve the order of the array. But it
1219 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
1220 * #GDestroyNotify function it is called for the removed element.
1222 * Returns: the pointer which was removed
1225 g_ptr_array_remove_index_fast (GPtrArray
*array
,
1228 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1231 g_return_val_if_fail (rarray
, NULL
);
1233 g_return_val_if_fail (index_
< rarray
->len
, NULL
);
1235 result
= rarray
->pdata
[index_
];
1237 if (rarray
->element_free_func
!= NULL
)
1238 rarray
->element_free_func (rarray
->pdata
[index_
]);
1240 if (index_
!= rarray
->len
- 1)
1241 rarray
->pdata
[index_
] = rarray
->pdata
[rarray
->len
- 1];
1245 if (G_UNLIKELY (g_mem_gc_friendly
))
1246 rarray
->pdata
[rarray
->len
] = NULL
;
1252 * g_ptr_array_remove_range:
1253 * @array: a @GPtrArray
1254 * @index_: the index of the first pointer to remove
1255 * @length: the number of pointers to remove
1257 * Removes the given number of pointers starting at the given index
1258 * from a #GPtrArray. The following elements are moved to close the
1259 * gap. If @array has a non-%NULL #GDestroyNotify function it is
1260 * called for the removed elements.
1262 * Returns: the @array
1267 g_ptr_array_remove_range (GPtrArray
*array
,
1271 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1274 g_return_val_if_fail (rarray
!= NULL
, NULL
);
1275 g_return_val_if_fail (index_
<= rarray
->len
, NULL
);
1276 g_return_val_if_fail (index_
+ length
<= rarray
->len
, NULL
);
1278 if (rarray
->element_free_func
!= NULL
)
1280 for (n
= index_
; n
< index_
+ length
; n
++)
1281 rarray
->element_free_func (rarray
->pdata
[n
]);
1284 if (index_
+ length
!= rarray
->len
)
1286 memmove (&rarray
->pdata
[index_
],
1287 &rarray
->pdata
[index_
+ length
],
1288 (rarray
->len
- (index_
+ length
)) * sizeof (gpointer
));
1291 rarray
->len
-= length
;
1292 if (G_UNLIKELY (g_mem_gc_friendly
))
1295 for (i
= 0; i
< length
; i
++)
1296 rarray
->pdata
[rarray
->len
+ i
] = NULL
;
1303 * g_ptr_array_remove:
1304 * @array: a #GPtrArray
1305 * @data: the pointer to remove
1307 * Removes the first occurrence of the given pointer from the pointer
1308 * array. The following elements are moved down one place. If @array
1309 * has a non-%NULL #GDestroyNotify function it is called for the
1312 * It returns %TRUE if the pointer was removed, or %FALSE if the
1313 * pointer was not found.
1315 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
1316 * is not found in the array
1319 g_ptr_array_remove (GPtrArray
*array
,
1324 g_return_val_if_fail (array
, FALSE
);
1326 for (i
= 0; i
< array
->len
; i
+= 1)
1328 if (array
->pdata
[i
] == data
)
1330 g_ptr_array_remove_index (array
, i
);
1339 * g_ptr_array_remove_fast:
1340 * @array: a #GPtrArray
1341 * @data: the pointer to remove
1343 * Removes the first occurrence of the given pointer from the pointer
1344 * array. The last element in the array is used to fill in the space,
1345 * so this function does not preserve the order of the array. But it
1346 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
1347 * #GDestroyNotify function it is called for the removed element.
1349 * It returns %TRUE if the pointer was removed, or %FALSE if the
1350 * pointer was not found.
1352 * Returns: %TRUE if the pointer was found in the array
1355 g_ptr_array_remove_fast (GPtrArray
*array
,
1358 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1361 g_return_val_if_fail (rarray
, FALSE
);
1363 for (i
= 0; i
< rarray
->len
; i
+= 1)
1365 if (rarray
->pdata
[i
] == data
)
1367 g_ptr_array_remove_index_fast (array
, i
);
1377 * @array: a #GPtrArray
1378 * @data: the pointer to add
1380 * Adds a pointer to the end of the pointer array. The array will grow
1381 * in size automatically if necessary.
1384 g_ptr_array_add (GPtrArray
*array
,
1387 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1389 g_return_if_fail (rarray
);
1391 g_ptr_array_maybe_expand (rarray
, 1);
1393 rarray
->pdata
[rarray
->len
++] = data
;
1397 * g_ptr_array_insert:
1398 * @array: a #GPtrArray
1399 * @index_: the index to place the new element at, or -1 to append
1400 * @data: the pointer to add.
1402 * Inserts an element into the pointer array at the given index. The
1403 * array will grow in size automatically if necessary.
1408 g_ptr_array_insert (GPtrArray
*array
,
1412 GRealPtrArray
*rarray
= (GRealPtrArray
*)array
;
1414 g_return_if_fail (rarray
);
1415 g_return_if_fail (index_
>= -1);
1416 g_return_if_fail (index_
<= (gint
)rarray
->len
);
1418 g_ptr_array_maybe_expand (rarray
, 1);
1421 index_
= rarray
->len
;
1423 if (index_
< rarray
->len
)
1424 memmove (&(rarray
->pdata
[index_
+ 1]),
1425 &(rarray
->pdata
[index_
]),
1426 (rarray
->len
- index_
) * sizeof (gpointer
));
1429 rarray
->pdata
[index_
] = data
;
1434 * @array: a #GPtrArray
1435 * @compare_func: comparison function
1437 * Sorts the array, using @compare_func which should be a qsort()-style
1438 * comparison function (returns less than zero for first arg is less
1439 * than second arg, zero for equal, greater than zero if irst arg is
1440 * greater than second arg).
1442 * Note that the comparison function for g_ptr_array_sort() doesn't
1443 * take the pointers from the array as arguments, it takes pointers to
1444 * the pointers in the array.
1446 * This is guaranteed to be a stable sort since version 2.32.
1449 g_ptr_array_sort (GPtrArray
*array
,
1450 GCompareFunc compare_func
)
1452 g_return_if_fail (array
!= NULL
);
1454 /* Don't use qsort as we want a guaranteed stable sort */
1455 g_qsort_with_data (array
->pdata
,
1458 (GCompareDataFunc
)compare_func
,
1463 * g_ptr_array_sort_with_data:
1464 * @array: a #GPtrArray
1465 * @compare_func: comparison function
1466 * @user_data: data to pass to @compare_func
1468 * Like g_ptr_array_sort(), but the comparison function has an extra
1469 * user data argument.
1471 * Note that the comparison function for g_ptr_array_sort_with_data()
1472 * doesn't take the pointers from the array as arguments, it takes
1473 * pointers to the pointers in the array.
1475 * This is guaranteed to be a stable sort since version 2.32.
1478 g_ptr_array_sort_with_data (GPtrArray
*array
,
1479 GCompareDataFunc compare_func
,
1482 g_return_if_fail (array
!= NULL
);
1484 g_qsort_with_data (array
->pdata
,
1492 * g_ptr_array_foreach:
1493 * @array: a #GPtrArray
1494 * @func: the function to call for each array element
1495 * @user_data: user data to pass to the function
1497 * Calls a function for each element of a #GPtrArray.
1502 g_ptr_array_foreach (GPtrArray
*array
,
1508 g_return_if_fail (array
);
1510 for (i
= 0; i
< array
->len
; i
++)
1511 (*func
) (array
->pdata
[i
], user_data
);
1515 * SECTION:arrays_byte
1516 * @title: Byte Arrays
1517 * @short_description: arrays of bytes
1519 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1520 * of bytes which grow automatically as elements are added.
1522 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1523 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1525 * To set the size of a #GByteArray, use g_byte_array_set_size().
1527 * To free a #GByteArray, use g_byte_array_free().
1529 * An example for using a #GByteArray:
1530 * |[<!-- language="C" -->
1531 * GByteArray *gbarray;
1534 * gbarray = g_byte_array_new ();
1535 * for (i = 0; i < 10000; i++)
1536 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1538 * for (i = 0; i < 10000; i++)
1540 * g_assert (gbarray->data[4*i] == 'a');
1541 * g_assert (gbarray->data[4*i+1] == 'b');
1542 * g_assert (gbarray->data[4*i+2] == 'c');
1543 * g_assert (gbarray->data[4*i+3] == 'd');
1546 * g_byte_array_free (gbarray, TRUE);
1549 * See #GBytes if you are interested in an immutable object representing a
1550 * sequence of bytes.
1555 * @data: a pointer to the element data. The data may be moved as
1556 * elements are added to the #GByteArray
1557 * @len: the number of elements in the #GByteArray
1559 * Contains the public fields of a GByteArray.
1565 * Creates a new #GByteArray with a reference count of 1.
1567 * Returns: (transfer full): the new #GByteArray
1570 g_byte_array_new (void)
1572 return (GByteArray
*)g_array_sized_new (FALSE
, FALSE
, 1, 0);
1576 * g_byte_array_new_take:
1577 * @data: (transfer full) (array length=len): byte data for the array
1578 * @len: length of @data
1580 * Create byte array containing the data. The data will be owned by the array
1581 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1585 * Returns: (transfer full): a new #GByteArray
1588 g_byte_array_new_take (guint8
*data
,
1594 array
= g_byte_array_new ();
1595 real
= (GRealArray
*)array
;
1596 g_assert (real
->data
== NULL
);
1597 g_assert (real
->len
== 0);
1607 * g_byte_array_sized_new:
1608 * @reserved_size: number of bytes preallocated
1610 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1611 * This avoids frequent reallocation, if you are going to add many
1612 * bytes to the array. Note however that the size of the array is still
1615 * Returns: the new #GByteArray
1618 g_byte_array_sized_new (guint reserved_size
)
1620 return (GByteArray
*)g_array_sized_new (FALSE
, FALSE
, 1, reserved_size
);
1624 * g_byte_array_free:
1625 * @array: a #GByteArray
1626 * @free_segment: if %TRUE the actual byte data is freed as well
1628 * Frees the memory allocated by the #GByteArray. If @free_segment is
1629 * %TRUE it frees the actual byte data. If the reference count of
1630 * @array is greater than one, the #GByteArray wrapper is preserved but
1631 * the size of @array will be set to zero.
1633 * Returns: the element data if @free_segment is %FALSE, otherwise
1634 * %NULL. The element data should be freed using g_free().
1637 g_byte_array_free (GByteArray
*array
,
1638 gboolean free_segment
)
1640 return (guint8
*)g_array_free ((GArray
*)array
, free_segment
);
1644 * g_byte_array_free_to_bytes:
1645 * @array: (transfer full): a #GByteArray
1647 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1649 * The #GByteArray is freed unless the reference count of @array is greater
1650 * than one, the #GByteArray wrapper is preserved but the size of @array
1651 * will be set to zero.
1653 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1658 * Returns: (transfer full): a new immutable #GBytes representing same
1659 * byte data that was in the array
1662 g_byte_array_free_to_bytes (GByteArray
*array
)
1666 g_return_val_if_fail (array
!= NULL
, NULL
);
1668 length
= array
->len
;
1669 return g_bytes_new_take (g_byte_array_free (array
, FALSE
), length
);
1674 * @array: A #GByteArray
1676 * Atomically increments the reference count of @array by one.
1677 * This function is thread-safe and may be called from any thread.
1679 * Returns: The passed in #GByteArray
1684 g_byte_array_ref (GByteArray
*array
)
1686 return (GByteArray
*)g_array_ref ((GArray
*)array
);
1690 * g_byte_array_unref:
1691 * @array: A #GByteArray
1693 * Atomically decrements the reference count of @array by one. If the
1694 * reference count drops to 0, all memory allocated by the array is
1695 * released. This function is thread-safe and may be called from any
1701 g_byte_array_unref (GByteArray
*array
)
1703 g_array_unref ((GArray
*)array
);
1707 * g_byte_array_append:
1708 * @array: a #GByteArray
1709 * @data: the byte data to be added
1710 * @len: the number of bytes to add
1712 * Adds the given bytes to the end of the #GByteArray.
1713 * The array will grow in size automatically if necessary.
1715 * Returns: the #GByteArray
1718 g_byte_array_append (GByteArray
*array
,
1722 g_array_append_vals ((GArray
*)array
, (guint8
*)data
, len
);
1728 * g_byte_array_prepend:
1729 * @array: a #GByteArray
1730 * @data: the byte data to be added
1731 * @len: the number of bytes to add
1733 * Adds the given data to the start of the #GByteArray.
1734 * The array will grow in size automatically if necessary.
1736 * Returns: the #GByteArray
1739 g_byte_array_prepend (GByteArray
*array
,
1743 g_array_prepend_vals ((GArray
*)array
, (guint8
*)data
, len
);
1749 * g_byte_array_set_size:
1750 * @array: a #GByteArray
1751 * @length: the new size of the #GByteArray
1753 * Sets the size of the #GByteArray, expanding it if necessary.
1755 * Returns: the #GByteArray
1758 g_byte_array_set_size (GByteArray
*array
,
1761 g_array_set_size ((GArray
*)array
, length
);
1767 * g_byte_array_remove_index:
1768 * @array: a #GByteArray
1769 * @index_: the index of the byte to remove
1771 * Removes the byte at the given index from a #GByteArray.
1772 * The following bytes are moved down one place.
1774 * Returns: the #GByteArray
1777 g_byte_array_remove_index (GByteArray
*array
,
1780 g_array_remove_index ((GArray
*)array
, index_
);
1786 * g_byte_array_remove_index_fast:
1787 * @array: a #GByteArray
1788 * @index_: the index of the byte to remove
1790 * Removes the byte at the given index from a #GByteArray. The last
1791 * element in the array is used to fill in the space, so this function
1792 * does not preserve the order of the #GByteArray. But it is faster
1793 * than g_byte_array_remove_index().
1795 * Returns: the #GByteArray
1798 g_byte_array_remove_index_fast (GByteArray
*array
,
1801 g_array_remove_index_fast ((GArray
*)array
, index_
);
1807 * g_byte_array_remove_range:
1808 * @array: a @GByteArray
1809 * @index_: the index of the first byte to remove
1810 * @length: the number of bytes to remove
1812 * Removes the given number of bytes starting at the given index from a
1813 * #GByteArray. The following elements are moved to close the gap.
1815 * Returns: the #GByteArray
1820 g_byte_array_remove_range (GByteArray
*array
,
1824 g_return_val_if_fail (array
, NULL
);
1825 g_return_val_if_fail (index_
<= array
->len
, NULL
);
1826 g_return_val_if_fail (index_
+ length
<= array
->len
, NULL
);
1828 return (GByteArray
*)g_array_remove_range ((GArray
*)array
, index_
, length
);
1832 * g_byte_array_sort:
1833 * @array: a #GByteArray
1834 * @compare_func: comparison function
1836 * Sorts a byte array, using @compare_func which should be a
1837 * qsort()-style comparison function (returns less than zero for first
1838 * arg is less than second arg, zero for equal, greater than zero if
1839 * first arg is greater than second arg).
1841 * If two array elements compare equal, their order in the sorted array
1842 * is undefined. If you want equal elements to keep their order (i.e.
1843 * you want a stable sort) you can write a comparison function that,
1844 * if two elements would otherwise compare equal, compares them by
1848 g_byte_array_sort (GByteArray
*array
,
1849 GCompareFunc compare_func
)
1851 g_array_sort ((GArray
*)array
, compare_func
);
1855 * g_byte_array_sort_with_data:
1856 * @array: a #GByteArray
1857 * @compare_func: comparison function
1858 * @user_data: data to pass to @compare_func
1860 * Like g_byte_array_sort(), but the comparison function takes an extra
1861 * user data argument.
1864 g_byte_array_sort_with_data (GByteArray
*array
,
1865 GCompareDataFunc compare_func
,
1868 g_array_sort_with_data ((GArray
*)array
, compare_func
, user_data
);