Add support for g_auto[s]list(Type)
[glib.git] / glib / garray.c
blob5670408fdbaef38ebe74de1e641c42fb6ee0f651
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.1 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/.
25 /*
26 * MT safe
29 #include "config.h"
31 #include <string.h>
32 #include <stdlib.h>
34 #include "garray.h"
36 #include "gbytes.h"
37 #include "ghash.h"
38 #include "gslice.h"
39 #include "gmem.h"
40 #include "gtestutils.h"
41 #include "gthread.h"
42 #include "gmessages.h"
43 #include "gqsort.h"
46 /**
47 * SECTION:arrays
48 * @title: Arrays
49 * @short_description: arrays of arbitrary elements which grow
50 * automatically as elements are added
52 * Arrays are similar to standard C arrays, except that they grow
53 * automatically as elements are added.
55 * Array elements can be of any size (though all elements of one array
56 * are the same size), and the array can be automatically cleared to
57 * '0's and zero-terminated.
59 * To create a new array use g_array_new().
61 * To add elements to an array, use g_array_append_val(),
62 * g_array_append_vals(), g_array_prepend_val(), and
63 * g_array_prepend_vals().
65 * To access an element of an array, use g_array_index().
67 * To set the size of an array, use g_array_set_size().
69 * To free an array, use g_array_free().
71 * Here is an example that stores integers in a #GArray:
72 * |[<!-- language="C" -->
73 * GArray *garray;
74 * gint i;
75 * // We create a new array to store gint values.
76 * // We don't want it zero-terminated or cleared to 0's.
77 * garray = g_array_new (FALSE, FALSE, sizeof (gint));
78 * for (i = 0; i < 10000; i++)
79 * g_array_append_val (garray, i);
80 * for (i = 0; i < 10000; i++)
81 * if (g_array_index (garray, gint, i) != i)
82 * g_print ("ERROR: got %d instead of %d\n",
83 * g_array_index (garray, gint, i), i);
84 * g_array_free (garray, TRUE);
85 * ]|
88 #define MIN_ARRAY_SIZE 16
90 typedef struct _GRealArray GRealArray;
92 /**
93 * GArray:
94 * @data: a pointer to the element data. The data may be moved as
95 * elements are added to the #GArray.
96 * @len: the number of elements in the #GArray not including the
97 * possible terminating zero element.
99 * Contains the public fields of a GArray.
101 struct _GRealArray
103 guint8 *data;
104 guint len;
105 guint alloc;
106 guint elt_size;
107 guint zero_terminated : 1;
108 guint clear : 1;
109 gint ref_count;
110 GDestroyNotify clear_func;
114 * g_array_index:
115 * @a: a #GArray
116 * @t: the type of the elements
117 * @i: the index of the element to return
119 * Returns the element of a #GArray at the given index. The return
120 * value is cast to the given type.
122 * This example gets a pointer to an element in a #GArray:
123 * |[<!-- language="C" -->
124 * EDayViewEvent *event;
125 * // This gets a pointer to the 4th element in the array of
126 * // EDayViewEvent structs.
127 * event = &g_array_index (events, EDayViewEvent, 3);
128 * ]|
130 * Returns: the element of the #GArray at the index given by @i
133 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
134 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
135 #define g_array_elt_zero(array, pos, len) \
136 (memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
137 #define g_array_zero_terminate(array) G_STMT_START{ \
138 if ((array)->zero_terminated) \
139 g_array_elt_zero ((array), (array)->len, 1); \
140 }G_STMT_END
142 static guint g_nearest_pow (gint num) G_GNUC_CONST;
143 static void g_array_maybe_expand (GRealArray *array,
144 gint len);
147 * g_array_new:
148 * @zero_terminated: %TRUE if the array should have an extra element at
149 * the end which is set to 0
150 * @clear_: %TRUE if #GArray elements should be automatically cleared
151 * to 0 when they are allocated
152 * @element_size: the size of each element in bytes
154 * Creates a new #GArray with a reference count of 1.
156 * Returns: the new #GArray
158 GArray*
159 g_array_new (gboolean zero_terminated,
160 gboolean clear,
161 guint elt_size)
163 g_return_val_if_fail (elt_size > 0, NULL);
165 return g_array_sized_new (zero_terminated, clear, elt_size, 0);
169 * g_array_sized_new:
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
173 * allocation
174 * @element_size: size of each element in the array
175 * @reserved_size: number of elements preallocated
177 * Creates a new #GArray with @reserved_size elements preallocated and
178 * a reference count of 1. This avoids frequent reallocation, if you
179 * are going to add many elements to the array. Note however that the
180 * size of the array is still 0.
182 * Returns: the new #GArray
184 GArray*
185 g_array_sized_new (gboolean zero_terminated,
186 gboolean clear,
187 guint elt_size,
188 guint reserved_size)
190 GRealArray *array;
192 g_return_val_if_fail (elt_size > 0, NULL);
194 array = g_slice_new (GRealArray);
196 array->data = NULL;
197 array->len = 0;
198 array->alloc = 0;
199 array->zero_terminated = (zero_terminated ? 1 : 0);
200 array->clear = (clear ? 1 : 0);
201 array->elt_size = elt_size;
202 array->ref_count = 1;
203 array->clear_func = NULL;
205 if (array->zero_terminated || reserved_size != 0)
207 g_array_maybe_expand (array, reserved_size);
208 g_array_zero_terminate(array);
211 return (GArray*) array;
215 * g_array_set_clear_func:
216 * @array: A #GArray
217 * @clear_func: a function to clear an element of @array
219 * Sets a function to clear an element of @array.
221 * The @clear_func will be called when an element in the array
222 * data segment is removed and when the array is freed and data
223 * segment is deallocated as well.
225 * Note that in contrast with other uses of #GDestroyNotify
226 * functions, @clear_func is expected to clear the contents of
227 * the array element it is given, but not free the element itself.
229 * Since: 2.32
231 void
232 g_array_set_clear_func (GArray *array,
233 GDestroyNotify clear_func)
235 GRealArray *rarray = (GRealArray *) array;
237 g_return_if_fail (array != NULL);
239 rarray->clear_func = clear_func;
243 * g_array_ref:
244 * @array: A #GArray
246 * Atomically increments the reference count of @array by one.
247 * This function is thread-safe and may be called from any thread.
249 * Returns: The passed in #GArray
251 * Since: 2.22
253 GArray *
254 g_array_ref (GArray *array)
256 GRealArray *rarray = (GRealArray*) array;
257 g_return_val_if_fail (array, NULL);
259 g_atomic_int_inc (&rarray->ref_count);
261 return array;
264 typedef enum
266 FREE_SEGMENT = 1 << 0,
267 PRESERVE_WRAPPER = 1 << 1
268 } ArrayFreeFlags;
270 static gchar *array_free (GRealArray *, ArrayFreeFlags);
273 * g_array_unref:
274 * @array: A #GArray
276 * Atomically decrements the reference count of @array by one. If the
277 * reference count drops to 0, all memory allocated by the array is
278 * released. This function is thread-safe and may be called from any
279 * thread.
281 * Since: 2.22
283 void
284 g_array_unref (GArray *array)
286 GRealArray *rarray = (GRealArray*) array;
287 g_return_if_fail (array);
289 if (g_atomic_int_dec_and_test (&rarray->ref_count))
290 array_free (rarray, FREE_SEGMENT);
294 * g_array_get_element_size:
295 * @array: A #GArray
297 * Gets the size of the elements in @array.
299 * Returns: Size of each element, in bytes
301 * Since: 2.22
303 guint
304 g_array_get_element_size (GArray *array)
306 GRealArray *rarray = (GRealArray*) array;
308 g_return_val_if_fail (array, 0);
310 return rarray->elt_size;
314 * g_array_free:
315 * @array: a #GArray
316 * @free_segment: if %TRUE the actual element data is freed as well
318 * Frees the memory allocated for the #GArray. If @free_segment is
319 * %TRUE it frees the memory block holding the elements as well and
320 * also each element if @array has a @element_free_func set. Pass
321 * %FALSE if you want to free the #GArray wrapper but preserve the
322 * underlying array for use elsewhere. If the reference count of @array
323 * is greater than one, the #GArray wrapper is preserved but the size
324 * of @array will be set to zero.
326 * If array elements contain dynamically-allocated memory, they should
327 * be freed separately.
329 * This function is not thread-safe. If using a #GArray from multiple
330 * threads, use only the atomic g_array_ref() and g_array_unref()
331 * functions.
333 * Returns: the element data if @free_segment is %FALSE, otherwise
334 * %NULL. The element data should be freed using g_free().
336 gchar*
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);
354 static gchar *
355 array_free (GRealArray *array,
356 ArrayFreeFlags flags)
358 gchar *segment;
360 if (flags & FREE_SEGMENT)
362 if (array->clear_func != NULL)
364 guint i;
366 for (i = 0; i < array->len; i++)
367 array->clear_func (g_array_elt_pos (array, i));
370 g_free (array->data);
371 segment = NULL;
373 else
374 segment = (gchar*) array->data;
376 if (flags & PRESERVE_WRAPPER)
378 array->data = NULL;
379 array->len = 0;
380 array->alloc = 0;
382 else
384 g_slice_free1 (sizeof (GRealArray), array);
387 return segment;
391 * g_array_append_vals:
392 * @array: a #GArray
393 * @data: (not nullable): 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:
402 * @a: a #GArray
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 * g_array_append_val() is a macro which uses a reference to the value
409 * parameter @v. This means that you cannot use it with literal values
410 * such as "27". You must use variables.
412 * Returns: the #GArray
414 GArray*
415 g_array_append_vals (GArray *farray,
416 gconstpointer data,
417 guint len)
419 GRealArray *array = (GRealArray*) farray;
421 g_return_val_if_fail (array, NULL);
423 if (len == 0)
424 return farray;
426 g_array_maybe_expand (array, len);
428 memcpy (g_array_elt_pos (array, array->len), data,
429 g_array_elt_len (array, len));
431 array->len += len;
433 g_array_zero_terminate (array);
435 return farray;
439 * g_array_prepend_vals:
440 * @array: a #GArray
441 * @data: (not nullable): a pointer to the elements to prepend to the start of the array
442 * @len: the number of elements to prepend
444 * Adds @len elements onto the start of the array.
446 * This operation is slower than g_array_append_vals() since the
447 * existing elements in the array have to be moved to make space for
448 * the new elements.
450 * Returns: the #GArray
453 * g_array_prepend_val:
454 * @a: a #GArray
455 * @v: the value to prepend to the #GArray
457 * Adds the value on to the start of the array. The array will grow in
458 * size automatically if necessary.
460 * This operation is slower than g_array_append_val() since the
461 * existing elements in the array have to be moved to make space for
462 * the new element.
464 * g_array_prepend_val() is a macro which uses a reference to the value
465 * parameter @v. This means that you cannot use it with literal values
466 * such as "27". You must use variables.
468 * Returns: the #GArray
470 GArray*
471 g_array_prepend_vals (GArray *farray,
472 gconstpointer data,
473 guint len)
475 GRealArray *array = (GRealArray*) farray;
477 g_return_val_if_fail (array, NULL);
479 if (len == 0)
480 return farray;
482 g_array_maybe_expand (array, len);
484 memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
485 g_array_elt_len (array, array->len));
487 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
489 array->len += len;
491 g_array_zero_terminate (array);
493 return farray;
497 * g_array_insert_vals:
498 * @array: a #GArray
499 * @index_: the index to place the elements at
500 * @data: (not nullable): a pointer to the elements to insert
501 * @len: the number of elements to insert
503 * Inserts @len elements into a #GArray at the given index.
505 * Returns: the #GArray
508 * g_array_insert_val:
509 * @a: a #GArray
510 * @i: the index to place the element at
511 * @v: the value to insert into the array
513 * Inserts an element into an array at the given index.
515 * g_array_insert_val() is a macro which uses a reference to the value
516 * parameter @v. This means that you cannot use it with literal values
517 * such as "27". You must use variables.
519 * Returns: the #GArray
521 GArray*
522 g_array_insert_vals (GArray *farray,
523 guint index_,
524 gconstpointer data,
525 guint len)
527 GRealArray *array = (GRealArray*) farray;
529 g_return_val_if_fail (array, NULL);
531 if (len == 0)
532 return farray;
534 g_array_maybe_expand (array, len);
536 memmove (g_array_elt_pos (array, len + index_),
537 g_array_elt_pos (array, index_),
538 g_array_elt_len (array, array->len - index_));
540 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
542 array->len += len;
544 g_array_zero_terminate (array);
546 return farray;
550 * g_array_set_size:
551 * @array: a #GArray
552 * @length: the new size of the #GArray
554 * Sets the size of the array, expanding it if necessary. If the array
555 * was created with @clear_ set to %TRUE, the new elements are set to 0.
557 * Returns: the #GArray
559 GArray*
560 g_array_set_size (GArray *farray,
561 guint length)
563 GRealArray *array = (GRealArray*) farray;
565 g_return_val_if_fail (array, NULL);
567 if (length > array->len)
569 g_array_maybe_expand (array, length - array->len);
571 if (array->clear)
572 g_array_elt_zero (array, array->len, length - array->len);
574 else if (length < array->len)
575 g_array_remove_range (farray, length, array->len - length);
577 array->len = length;
579 g_array_zero_terminate (array);
581 return farray;
585 * g_array_remove_index:
586 * @array: a #GArray
587 * @index_: the index of the element to remove
589 * Removes the element at the given index from a #GArray. The following
590 * elements are moved down one place.
592 * Returns: the #GArray
594 GArray*
595 g_array_remove_index (GArray *farray,
596 guint index_)
598 GRealArray* array = (GRealArray*) farray;
600 g_return_val_if_fail (array, NULL);
602 g_return_val_if_fail (index_ < array->len, NULL);
604 if (array->clear_func != NULL)
605 array->clear_func (g_array_elt_pos (array, index_));
607 if (index_ != array->len - 1)
608 memmove (g_array_elt_pos (array, index_),
609 g_array_elt_pos (array, index_ + 1),
610 g_array_elt_len (array, array->len - index_ - 1));
612 array->len -= 1;
614 if (G_UNLIKELY (g_mem_gc_friendly))
615 g_array_elt_zero (array, array->len, 1);
616 else
617 g_array_zero_terminate (array);
619 return farray;
623 * g_array_remove_index_fast:
624 * @array: a @GArray
625 * @index_: the index of the element to remove
627 * Removes the element at the given index from a #GArray. The last
628 * element in the array is used to fill in the space, so this function
629 * does not preserve the order of the #GArray. But it is faster than
630 * g_array_remove_index().
632 * Returns: the #GArray
634 GArray*
635 g_array_remove_index_fast (GArray *farray,
636 guint index_)
638 GRealArray* array = (GRealArray*) farray;
640 g_return_val_if_fail (array, NULL);
642 g_return_val_if_fail (index_ < array->len, NULL);
644 if (array->clear_func != NULL)
645 array->clear_func (g_array_elt_pos (array, index_));
647 if (index_ != array->len - 1)
648 memcpy (g_array_elt_pos (array, index_),
649 g_array_elt_pos (array, array->len - 1),
650 g_array_elt_len (array, 1));
652 array->len -= 1;
654 if (G_UNLIKELY (g_mem_gc_friendly))
655 g_array_elt_zero (array, array->len, 1);
656 else
657 g_array_zero_terminate (array);
659 return farray;
663 * g_array_remove_range:
664 * @array: a @GArray
665 * @index_: the index of the first element to remove
666 * @length: the number of elements to remove
668 * Removes the given number of elements starting at the given index
669 * from a #GArray. The following elements are moved to close the gap.
671 * Returns: the #GArray
673 * Since: 2.4
675 GArray*
676 g_array_remove_range (GArray *farray,
677 guint index_,
678 guint length)
680 GRealArray *array = (GRealArray*) farray;
682 g_return_val_if_fail (array, NULL);
683 g_return_val_if_fail (index_ <= array->len, NULL);
684 g_return_val_if_fail (index_ + length <= array->len, NULL);
686 if (array->clear_func != NULL)
688 guint i;
690 for (i = 0; i < length; i++)
691 array->clear_func (g_array_elt_pos (array, index_ + i));
694 if (index_ + length != array->len)
695 memmove (g_array_elt_pos (array, index_),
696 g_array_elt_pos (array, index_ + length),
697 (array->len - (index_ + length)) * array->elt_size);
699 array->len -= length;
700 if (G_UNLIKELY (g_mem_gc_friendly))
701 g_array_elt_zero (array, array->len, length);
702 else
703 g_array_zero_terminate (array);
705 return farray;
709 * g_array_sort:
710 * @array: a #GArray
711 * @compare_func: comparison function
713 * Sorts a #GArray using @compare_func which should be a qsort()-style
714 * comparison function (returns less than zero for first arg is less
715 * than second arg, zero for equal, greater zero if first arg is
716 * greater than second arg).
718 * This is guaranteed to be a stable sort since version 2.32.
720 void
721 g_array_sort (GArray *farray,
722 GCompareFunc compare_func)
724 GRealArray *array = (GRealArray*) farray;
726 g_return_if_fail (array != NULL);
728 /* Don't use qsort as we want a guaranteed stable sort */
729 g_qsort_with_data (array->data,
730 array->len,
731 array->elt_size,
732 (GCompareDataFunc)compare_func,
733 NULL);
737 * g_array_sort_with_data:
738 * @array: a #GArray
739 * @compare_func: comparison function
740 * @user_data: data to pass to @compare_func
742 * Like g_array_sort(), but the comparison function receives an extra
743 * user data argument.
745 * This is guaranteed to be a stable sort since version 2.32.
747 * There used to be a comment here about making the sort stable by
748 * using the addresses of the elements in the comparison function.
749 * This did not actually work, so any such code should be removed.
751 void
752 g_array_sort_with_data (GArray *farray,
753 GCompareDataFunc compare_func,
754 gpointer user_data)
756 GRealArray *array = (GRealArray*) farray;
758 g_return_if_fail (array != NULL);
760 g_qsort_with_data (array->data,
761 array->len,
762 array->elt_size,
763 compare_func,
764 user_data);
767 /* Returns the smallest power of 2 greater than n, or n if
768 * such power does not fit in a guint
770 static guint
771 g_nearest_pow (gint num)
773 guint n = 1;
775 while (n < num && n > 0)
776 n <<= 1;
778 return n ? n : num;
781 static void
782 g_array_maybe_expand (GRealArray *array,
783 gint len)
785 guint want_alloc = g_array_elt_len (array, array->len + len +
786 array->zero_terminated);
788 if (want_alloc > array->alloc)
790 want_alloc = g_nearest_pow (want_alloc);
791 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
793 array->data = g_realloc (array->data, want_alloc);
795 if (G_UNLIKELY (g_mem_gc_friendly))
796 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
798 array->alloc = want_alloc;
803 * SECTION:arrays_pointer
804 * @title: Pointer Arrays
805 * @short_description: arrays of pointers to any type of data, which
806 * grow automatically as new elements are added
808 * Pointer Arrays are similar to Arrays but are used only for storing
809 * pointers.
811 * If you remove elements from the array, elements at the end of the
812 * array are moved into the space previously occupied by the removed
813 * element. This means that you should not rely on the index of particular
814 * elements remaining the same. You should also be careful when deleting
815 * elements while iterating over the array.
817 * To create a pointer array, use g_ptr_array_new().
819 * To add elements to a pointer array, use g_ptr_array_add().
821 * To remove elements from a pointer array, use g_ptr_array_remove(),
822 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
824 * To access an element of a pointer array, use g_ptr_array_index().
826 * To set the size of a pointer array, use g_ptr_array_set_size().
828 * To free a pointer array, use g_ptr_array_free().
830 * An example using a #GPtrArray:
831 * |[<!-- language="C" -->
832 * GPtrArray *array;
833 * gchar *string1 = "one";
834 * gchar *string2 = "two";
835 * gchar *string3 = "three";
837 * array = g_ptr_array_new ();
838 * g_ptr_array_add (array, (gpointer) string1);
839 * g_ptr_array_add (array, (gpointer) string2);
840 * g_ptr_array_add (array, (gpointer) string3);
842 * if (g_ptr_array_index (array, 0) != (gpointer) string1)
843 * g_print ("ERROR: got %p instead of %p\n",
844 * g_ptr_array_index (array, 0), string1);
846 * g_ptr_array_free (array, TRUE);
847 * ]|
850 typedef struct _GRealPtrArray GRealPtrArray;
853 * GPtrArray:
854 * @pdata: points to the array of pointers, which may be moved when the
855 * array grows
856 * @len: number of pointers in the array
858 * Contains the public fields of a pointer array.
860 struct _GRealPtrArray
862 gpointer *pdata;
863 guint len;
864 guint alloc;
865 gint ref_count;
866 GDestroyNotify element_free_func;
870 * g_ptr_array_index:
871 * @array: a #GPtrArray
872 * @index_: the index of the pointer to return
874 * Returns the pointer at the given index of the pointer array.
876 * This does not perform bounds checking on the given @index_,
877 * so you are responsible for checking it against the array length.
879 * Returns: the pointer at the given index
882 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
883 gint len);
886 * g_ptr_array_new:
888 * Creates a new #GPtrArray with a reference count of 1.
890 * Returns: the new #GPtrArray
892 GPtrArray*
893 g_ptr_array_new (void)
895 return g_ptr_array_sized_new (0);
899 * g_ptr_array_sized_new:
900 * @reserved_size: number of pointers preallocated
902 * Creates a new #GPtrArray with @reserved_size pointers preallocated
903 * and a reference count of 1. This avoids frequent reallocation, if
904 * you are going to add many pointers to the array. Note however that
905 * the size of the array is still 0.
907 * Returns: the new #GPtrArray
909 GPtrArray*
910 g_ptr_array_sized_new (guint reserved_size)
912 GRealPtrArray *array;
914 array = g_slice_new (GRealPtrArray);
916 array->pdata = NULL;
917 array->len = 0;
918 array->alloc = 0;
919 array->ref_count = 1;
920 array->element_free_func = NULL;
922 if (reserved_size != 0)
923 g_ptr_array_maybe_expand (array, reserved_size);
925 return (GPtrArray*) array;
929 * g_ptr_array_new_with_free_func:
930 * @element_free_func: (nullable): A function to free elements with
931 * destroy @array or %NULL
933 * Creates a new #GPtrArray with a reference count of 1 and use
934 * @element_free_func for freeing each element when the array is destroyed
935 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
936 * @free_segment set to %TRUE or when removing elements.
938 * Returns: A new #GPtrArray
940 * Since: 2.22
942 GPtrArray*
943 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
945 GPtrArray *array;
947 array = g_ptr_array_new ();
948 g_ptr_array_set_free_func (array, element_free_func);
950 return array;
954 * g_ptr_array_new_full:
955 * @reserved_size: number of pointers preallocated
956 * @element_free_func: (nullable): A function to free elements with
957 * destroy @array or %NULL
959 * Creates a new #GPtrArray with @reserved_size pointers preallocated
960 * and a reference count of 1. This avoids frequent reallocation, if
961 * you are going to add many pointers to the array. Note however that
962 * the size of the array is still 0. It also set @element_free_func
963 * for freeing each element when the array is destroyed either via
964 * g_ptr_array_unref(), when g_ptr_array_free() is called with
965 * @free_segment set to %TRUE or when removing elements.
967 * Returns: A new #GPtrArray
969 * Since: 2.30
971 GPtrArray*
972 g_ptr_array_new_full (guint reserved_size,
973 GDestroyNotify element_free_func)
975 GPtrArray *array;
977 array = g_ptr_array_sized_new (reserved_size);
978 g_ptr_array_set_free_func (array, element_free_func);
980 return array;
984 * g_ptr_array_set_free_func:
985 * @array: A #GPtrArray
986 * @element_free_func: (nullable): A function to free elements with
987 * destroy @array or %NULL
989 * Sets a function for freeing each element when @array is destroyed
990 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
991 * with @free_segment set to %TRUE or when removing elements.
993 * Since: 2.22
995 void
996 g_ptr_array_set_free_func (GPtrArray *array,
997 GDestroyNotify element_free_func)
999 GRealPtrArray *rarray = (GRealPtrArray *)array;
1001 g_return_if_fail (array);
1003 rarray->element_free_func = element_free_func;
1007 * g_ptr_array_ref:
1008 * @array: a #GPtrArray
1010 * Atomically increments the reference count of @array by one.
1011 * This function is thread-safe and may be called from any thread.
1013 * Returns: The passed in #GPtrArray
1015 * Since: 2.22
1017 GPtrArray*
1018 g_ptr_array_ref (GPtrArray *array)
1020 GRealPtrArray *rarray = (GRealPtrArray *)array;
1022 g_return_val_if_fail (array, NULL);
1024 g_atomic_int_inc (&rarray->ref_count);
1026 return array;
1029 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
1032 * g_ptr_array_unref:
1033 * @array: A #GPtrArray
1035 * Atomically decrements the reference count of @array by one. If the
1036 * reference count drops to 0, the effect is the same as calling
1037 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1038 * is thread-safe and may be called from any thread.
1040 * Since: 2.22
1042 void
1043 g_ptr_array_unref (GPtrArray *array)
1045 GRealPtrArray *rarray = (GRealPtrArray *)array;
1047 g_return_if_fail (array);
1049 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1050 ptr_array_free (array, FREE_SEGMENT);
1054 * g_ptr_array_free:
1055 * @array: a #GPtrArray
1056 * @free_seg: if %TRUE the actual pointer array is freed as well
1058 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1059 * it frees the memory block holding the elements as well. Pass %FALSE
1060 * if you want to free the #GPtrArray wrapper but preserve the
1061 * underlying array for use elsewhere. If the reference count of @array
1062 * is greater than one, the #GPtrArray wrapper is preserved but the
1063 * size of @array will be set to zero.
1065 * If array contents point to dynamically-allocated memory, they should
1066 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
1067 * function has been set for @array.
1069 * This function is not thread-safe. If using a #GPtrArray from multiple
1070 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
1071 * functions.
1073 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1074 * The pointer array should be freed using g_free().
1076 gpointer*
1077 g_ptr_array_free (GPtrArray *array,
1078 gboolean free_segment)
1080 GRealPtrArray *rarray = (GRealPtrArray *)array;
1081 ArrayFreeFlags flags;
1083 g_return_val_if_fail (rarray, NULL);
1085 flags = (free_segment ? FREE_SEGMENT : 0);
1087 /* if others are holding a reference, preserve the wrapper but
1088 * do free/return the data
1090 if (!g_atomic_int_dec_and_test (&rarray->ref_count))
1091 flags |= PRESERVE_WRAPPER;
1093 return ptr_array_free (array, flags);
1096 static gpointer *
1097 ptr_array_free (GPtrArray *array,
1098 ArrayFreeFlags flags)
1100 GRealPtrArray *rarray = (GRealPtrArray *)array;
1101 gpointer *segment;
1103 if (flags & FREE_SEGMENT)
1105 if (rarray->element_free_func != NULL)
1106 g_ptr_array_foreach (array, (GFunc) rarray->element_free_func, NULL);
1107 g_free (rarray->pdata);
1108 segment = NULL;
1110 else
1111 segment = rarray->pdata;
1113 if (flags & PRESERVE_WRAPPER)
1115 rarray->pdata = NULL;
1116 rarray->len = 0;
1117 rarray->alloc = 0;
1119 else
1121 g_slice_free1 (sizeof (GRealPtrArray), rarray);
1124 return segment;
1127 static void
1128 g_ptr_array_maybe_expand (GRealPtrArray *array,
1129 gint len)
1131 if ((array->len + len) > array->alloc)
1133 guint old_alloc = array->alloc;
1134 array->alloc = g_nearest_pow (array->len + len);
1135 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1136 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1137 if (G_UNLIKELY (g_mem_gc_friendly))
1138 for ( ; old_alloc < array->alloc; old_alloc++)
1139 array->pdata [old_alloc] = NULL;
1144 * g_ptr_array_set_size:
1145 * @array: a #GPtrArray
1146 * @length: the new length of the pointer array
1148 * Sets the size of the array. When making the array larger,
1149 * newly-added elements will be set to %NULL. When making it smaller,
1150 * if @array has a non-%NULL #GDestroyNotify function then it will be
1151 * called for the removed elements.
1153 void
1154 g_ptr_array_set_size (GPtrArray *array,
1155 gint length)
1157 GRealPtrArray *rarray = (GRealPtrArray *)array;
1159 g_return_if_fail (rarray);
1161 if (length > rarray->len)
1163 int i;
1164 g_ptr_array_maybe_expand (rarray, (length - rarray->len));
1165 /* This is not
1166 * memset (array->pdata + array->len, 0,
1167 * sizeof (gpointer) * (length - array->len));
1168 * to make it really portable. Remember (void*)NULL needn't be
1169 * bitwise zero. It of course is silly not to use memset (..,0,..).
1171 for (i = rarray->len; i < length; i++)
1172 rarray->pdata[i] = NULL;
1174 else if (length < rarray->len)
1175 g_ptr_array_remove_range (array, length, rarray->len - length);
1177 rarray->len = length;
1181 * g_ptr_array_remove_index:
1182 * @array: a #GPtrArray
1183 * @index_: the index of the pointer to remove
1185 * Removes the pointer at the given index from the pointer array.
1186 * The following elements are moved down one place. If @array has
1187 * a non-%NULL #GDestroyNotify function it is called for the removed
1188 * element.
1190 * Returns: the pointer which was removed
1192 gpointer
1193 g_ptr_array_remove_index (GPtrArray *array,
1194 guint index_)
1196 GRealPtrArray *rarray = (GRealPtrArray *)array;
1197 gpointer result;
1199 g_return_val_if_fail (rarray, NULL);
1201 g_return_val_if_fail (index_ < rarray->len, NULL);
1203 result = rarray->pdata[index_];
1205 if (rarray->element_free_func != NULL)
1206 rarray->element_free_func (rarray->pdata[index_]);
1208 if (index_ != rarray->len - 1)
1209 memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
1210 sizeof (gpointer) * (rarray->len - index_ - 1));
1212 rarray->len -= 1;
1214 if (G_UNLIKELY (g_mem_gc_friendly))
1215 rarray->pdata[rarray->len] = NULL;
1217 return result;
1221 * g_ptr_array_remove_index_fast:
1222 * @array: a #GPtrArray
1223 * @index_: the index of the pointer to remove
1225 * Removes the pointer at the given index from the pointer array.
1226 * The last element in the array is used to fill in the space, so
1227 * this function does not preserve the order of the array. But it
1228 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
1229 * #GDestroyNotify function it is called for the removed element.
1231 * Returns: the pointer which was removed
1233 gpointer
1234 g_ptr_array_remove_index_fast (GPtrArray *array,
1235 guint index_)
1237 GRealPtrArray *rarray = (GRealPtrArray *)array;
1238 gpointer result;
1240 g_return_val_if_fail (rarray, NULL);
1242 g_return_val_if_fail (index_ < rarray->len, NULL);
1244 result = rarray->pdata[index_];
1246 if (rarray->element_free_func != NULL)
1247 rarray->element_free_func (rarray->pdata[index_]);
1249 if (index_ != rarray->len - 1)
1250 rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
1252 rarray->len -= 1;
1254 if (G_UNLIKELY (g_mem_gc_friendly))
1255 rarray->pdata[rarray->len] = NULL;
1257 return result;
1261 * g_ptr_array_remove_range:
1262 * @array: a @GPtrArray
1263 * @index_: the index of the first pointer to remove
1264 * @length: the number of pointers to remove
1266 * Removes the given number of pointers starting at the given index
1267 * from a #GPtrArray. The following elements are moved to close the
1268 * gap. If @array has a non-%NULL #GDestroyNotify function it is
1269 * called for the removed elements.
1271 * Returns: the @array
1273 * Since: 2.4
1275 GPtrArray*
1276 g_ptr_array_remove_range (GPtrArray *array,
1277 guint index_,
1278 guint length)
1280 GRealPtrArray *rarray = (GRealPtrArray *)array;
1281 guint n;
1283 g_return_val_if_fail (rarray != NULL, NULL);
1284 g_return_val_if_fail (index_ <= rarray->len, NULL);
1285 g_return_val_if_fail (index_ + length <= rarray->len, NULL);
1287 if (rarray->element_free_func != NULL)
1289 for (n = index_; n < index_ + length; n++)
1290 rarray->element_free_func (rarray->pdata[n]);
1293 if (index_ + length != rarray->len)
1295 memmove (&rarray->pdata[index_],
1296 &rarray->pdata[index_ + length],
1297 (rarray->len - (index_ + length)) * sizeof (gpointer));
1300 rarray->len -= length;
1301 if (G_UNLIKELY (g_mem_gc_friendly))
1303 guint i;
1304 for (i = 0; i < length; i++)
1305 rarray->pdata[rarray->len + i] = NULL;
1308 return array;
1312 * g_ptr_array_remove:
1313 * @array: a #GPtrArray
1314 * @data: the pointer to remove
1316 * Removes the first occurrence of the given pointer from the pointer
1317 * array. The following elements are moved down one place. If @array
1318 * has a non-%NULL #GDestroyNotify function it is called for the
1319 * removed element.
1321 * It returns %TRUE if the pointer was removed, or %FALSE if the
1322 * pointer was not found.
1324 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
1325 * is not found in the array
1327 gboolean
1328 g_ptr_array_remove (GPtrArray *array,
1329 gpointer data)
1331 guint i;
1333 g_return_val_if_fail (array, FALSE);
1335 for (i = 0; i < array->len; i += 1)
1337 if (array->pdata[i] == data)
1339 g_ptr_array_remove_index (array, i);
1340 return TRUE;
1344 return FALSE;
1348 * g_ptr_array_remove_fast:
1349 * @array: a #GPtrArray
1350 * @data: the pointer to remove
1352 * Removes the first occurrence of the given pointer from the pointer
1353 * array. The last element in the array is used to fill in the space,
1354 * so this function does not preserve the order of the array. But it
1355 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
1356 * #GDestroyNotify function it is called for the removed element.
1358 * It returns %TRUE if the pointer was removed, or %FALSE if the
1359 * pointer was not found.
1361 * Returns: %TRUE if the pointer was found in the array
1363 gboolean
1364 g_ptr_array_remove_fast (GPtrArray *array,
1365 gpointer data)
1367 GRealPtrArray *rarray = (GRealPtrArray *)array;
1368 guint i;
1370 g_return_val_if_fail (rarray, FALSE);
1372 for (i = 0; i < rarray->len; i += 1)
1374 if (rarray->pdata[i] == data)
1376 g_ptr_array_remove_index_fast (array, i);
1377 return TRUE;
1381 return FALSE;
1385 * g_ptr_array_add:
1386 * @array: a #GPtrArray
1387 * @data: the pointer to add
1389 * Adds a pointer to the end of the pointer array. The array will grow
1390 * in size automatically if necessary.
1392 void
1393 g_ptr_array_add (GPtrArray *array,
1394 gpointer data)
1396 GRealPtrArray *rarray = (GRealPtrArray *)array;
1398 g_return_if_fail (rarray);
1400 g_ptr_array_maybe_expand (rarray, 1);
1402 rarray->pdata[rarray->len++] = data;
1406 * g_ptr_array_insert:
1407 * @array: a #GPtrArray
1408 * @index_: the index to place the new element at, or -1 to append
1409 * @data: the pointer to add.
1411 * Inserts an element into the pointer array at the given index. The
1412 * array will grow in size automatically if necessary.
1414 * Since: 2.40
1416 void
1417 g_ptr_array_insert (GPtrArray *array,
1418 gint index_,
1419 gpointer data)
1421 GRealPtrArray *rarray = (GRealPtrArray *)array;
1423 g_return_if_fail (rarray);
1424 g_return_if_fail (index_ >= -1);
1425 g_return_if_fail (index_ <= (gint)rarray->len);
1427 g_ptr_array_maybe_expand (rarray, 1);
1429 if (index_ < 0)
1430 index_ = rarray->len;
1432 if (index_ < rarray->len)
1433 memmove (&(rarray->pdata[index_ + 1]),
1434 &(rarray->pdata[index_]),
1435 (rarray->len - index_) * sizeof (gpointer));
1437 rarray->len++;
1438 rarray->pdata[index_] = data;
1442 * g_ptr_array_sort:
1443 * @array: a #GPtrArray
1444 * @compare_func: comparison function
1446 * Sorts the array, using @compare_func which should be a qsort()-style
1447 * comparison function (returns less than zero for first arg is less
1448 * than second arg, zero for equal, greater than zero if irst arg is
1449 * greater than second arg).
1451 * Note that the comparison function for g_ptr_array_sort() doesn't
1452 * take the pointers from the array as arguments, it takes pointers to
1453 * the pointers in the array.
1455 * This is guaranteed to be a stable sort since version 2.32.
1457 void
1458 g_ptr_array_sort (GPtrArray *array,
1459 GCompareFunc compare_func)
1461 g_return_if_fail (array != NULL);
1463 /* Don't use qsort as we want a guaranteed stable sort */
1464 g_qsort_with_data (array->pdata,
1465 array->len,
1466 sizeof (gpointer),
1467 (GCompareDataFunc)compare_func,
1468 NULL);
1472 * g_ptr_array_sort_with_data:
1473 * @array: a #GPtrArray
1474 * @compare_func: comparison function
1475 * @user_data: data to pass to @compare_func
1477 * Like g_ptr_array_sort(), but the comparison function has an extra
1478 * user data argument.
1480 * Note that the comparison function for g_ptr_array_sort_with_data()
1481 * doesn't take the pointers from the array as arguments, it takes
1482 * pointers to the pointers in the array.
1484 * This is guaranteed to be a stable sort since version 2.32.
1486 void
1487 g_ptr_array_sort_with_data (GPtrArray *array,
1488 GCompareDataFunc compare_func,
1489 gpointer user_data)
1491 g_return_if_fail (array != NULL);
1493 g_qsort_with_data (array->pdata,
1494 array->len,
1495 sizeof (gpointer),
1496 compare_func,
1497 user_data);
1501 * g_ptr_array_foreach:
1502 * @array: a #GPtrArray
1503 * @func: the function to call for each array element
1504 * @user_data: user data to pass to the function
1506 * Calls a function for each element of a #GPtrArray. @func must not
1507 * add elements to or remove elements from the array.
1509 * Since: 2.4
1511 void
1512 g_ptr_array_foreach (GPtrArray *array,
1513 GFunc func,
1514 gpointer user_data)
1516 guint i;
1518 g_return_if_fail (array);
1520 for (i = 0; i < array->len; i++)
1521 (*func) (array->pdata[i], user_data);
1525 * g_ptr_array_find: (skip)
1526 * @haystack: pointer array to be searched
1527 * @needle: pointer to look for
1528 * @index_: (optional) (out caller-allocates): return location for the index of
1529 * the element, if found
1531 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is
1532 * returned and the element’s index is returned in @index_ (if non-%NULL).
1533 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
1534 * multiple times in @haystack, the index of the first instance is returned.
1536 * This does pointer comparisons only. If you want to use more complex equality
1537 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
1539 * Returns: %TRUE if @needle is one of the elements of @haystack
1540 * Since: 2.54
1542 gboolean
1543 g_ptr_array_find (GPtrArray *haystack,
1544 gconstpointer needle,
1545 guint *index_)
1547 return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
1551 * g_ptr_array_find_with_equal_func: (skip)
1552 * @haystack: pointer array to be searched
1553 * @needle: pointer to look for
1554 * @equal_func: (nullable): the function to call for each element, which should
1555 * return %TRUE when the desired element is found; or %NULL to use pointer
1556 * equality
1557 * @index_: (optional) (out caller-allocates): return location for the index of
1558 * the element, if found
1560 * Checks whether @needle exists in @haystack, using the given @equal_func.
1561 * If the element is found, %TRUE is returned and the element’s index is
1562 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
1563 * is undefined. If @needle exists multiple times in @haystack, the index of
1564 * the first instance is returned.
1566 * @equal_func is called with the element from the array as its first parameter,
1567 * and @needle as its second parameter. If @equal_func is %NULL, pointer
1568 * equality is used.
1570 * Returns: %TRUE if @needle is one of the elements of @haystack
1571 * Since: 2.54
1573 gboolean
1574 g_ptr_array_find_with_equal_func (GPtrArray *haystack,
1575 gconstpointer needle,
1576 GEqualFunc equal_func,
1577 guint *index_)
1579 guint i;
1581 g_return_val_if_fail (haystack != NULL, FALSE);
1583 if (equal_func == NULL)
1584 equal_func = g_direct_equal;
1586 for (i = 0; i < haystack->len; i++)
1588 if (equal_func (g_ptr_array_index (haystack, i), needle))
1590 if (index_ != NULL)
1591 *index_ = i;
1592 return TRUE;
1596 return FALSE;
1600 * SECTION:arrays_byte
1601 * @title: Byte Arrays
1602 * @short_description: arrays of bytes
1604 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1605 * of bytes which grow automatically as elements are added.
1607 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1608 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1610 * To set the size of a #GByteArray, use g_byte_array_set_size().
1612 * To free a #GByteArray, use g_byte_array_free().
1614 * An example for using a #GByteArray:
1615 * |[<!-- language="C" -->
1616 * GByteArray *gbarray;
1617 * gint i;
1619 * gbarray = g_byte_array_new ();
1620 * for (i = 0; i < 10000; i++)
1621 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1623 * for (i = 0; i < 10000; i++)
1625 * g_assert (gbarray->data[4*i] == 'a');
1626 * g_assert (gbarray->data[4*i+1] == 'b');
1627 * g_assert (gbarray->data[4*i+2] == 'c');
1628 * g_assert (gbarray->data[4*i+3] == 'd');
1631 * g_byte_array_free (gbarray, TRUE);
1632 * ]|
1634 * See #GBytes if you are interested in an immutable object representing a
1635 * sequence of bytes.
1639 * GByteArray:
1640 * @data: a pointer to the element data. The data may be moved as
1641 * elements are added to the #GByteArray
1642 * @len: the number of elements in the #GByteArray
1644 * Contains the public fields of a GByteArray.
1648 * g_byte_array_new:
1650 * Creates a new #GByteArray with a reference count of 1.
1652 * Returns: (transfer full): the new #GByteArray
1654 GByteArray*
1655 g_byte_array_new (void)
1657 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
1661 * g_byte_array_new_take:
1662 * @data: (transfer full) (array length=len): byte data for the array
1663 * @len: length of @data
1665 * Create byte array containing the data. The data will be owned by the array
1666 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1668 * Since: 2.32
1670 * Returns: (transfer full): a new #GByteArray
1672 GByteArray*
1673 g_byte_array_new_take (guint8 *data,
1674 gsize len)
1676 GByteArray *array;
1677 GRealArray *real;
1679 array = g_byte_array_new ();
1680 real = (GRealArray *)array;
1681 g_assert (real->data == NULL);
1682 g_assert (real->len == 0);
1684 real->data = data;
1685 real->len = len;
1686 real->alloc = len;
1688 return array;
1692 * g_byte_array_sized_new:
1693 * @reserved_size: number of bytes preallocated
1695 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1696 * This avoids frequent reallocation, if you are going to add many
1697 * bytes to the array. Note however that the size of the array is still
1698 * 0.
1700 * Returns: the new #GByteArray
1702 GByteArray*
1703 g_byte_array_sized_new (guint reserved_size)
1705 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1709 * g_byte_array_free:
1710 * @array: a #GByteArray
1711 * @free_segment: if %TRUE the actual byte data is freed as well
1713 * Frees the memory allocated by the #GByteArray. If @free_segment is
1714 * %TRUE it frees the actual byte data. If the reference count of
1715 * @array is greater than one, the #GByteArray wrapper is preserved but
1716 * the size of @array will be set to zero.
1718 * Returns: the element data if @free_segment is %FALSE, otherwise
1719 * %NULL. The element data should be freed using g_free().
1721 guint8*
1722 g_byte_array_free (GByteArray *array,
1723 gboolean free_segment)
1725 return (guint8 *)g_array_free ((GArray *)array, free_segment);
1729 * g_byte_array_free_to_bytes:
1730 * @array: (transfer full): a #GByteArray
1732 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1734 * The #GByteArray is freed unless the reference count of @array is greater
1735 * than one, the #GByteArray wrapper is preserved but the size of @array
1736 * will be set to zero.
1738 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1739 * together.
1741 * Since: 2.32
1743 * Returns: (transfer full): a new immutable #GBytes representing same
1744 * byte data that was in the array
1746 GBytes*
1747 g_byte_array_free_to_bytes (GByteArray *array)
1749 gsize length;
1751 g_return_val_if_fail (array != NULL, NULL);
1753 length = array->len;
1754 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1758 * g_byte_array_ref:
1759 * @array: A #GByteArray
1761 * Atomically increments the reference count of @array by one.
1762 * This function is thread-safe and may be called from any thread.
1764 * Returns: The passed in #GByteArray
1766 * Since: 2.22
1768 GByteArray*
1769 g_byte_array_ref (GByteArray *array)
1771 return (GByteArray *)g_array_ref ((GArray *)array);
1775 * g_byte_array_unref:
1776 * @array: A #GByteArray
1778 * Atomically decrements the reference count of @array by one. If the
1779 * reference count drops to 0, all memory allocated by the array is
1780 * released. This function is thread-safe and may be called from any
1781 * thread.
1783 * Since: 2.22
1785 void
1786 g_byte_array_unref (GByteArray *array)
1788 g_array_unref ((GArray *)array);
1792 * g_byte_array_append:
1793 * @array: a #GByteArray
1794 * @data: the byte data to be added
1795 * @len: the number of bytes to add
1797 * Adds the given bytes to the end of the #GByteArray.
1798 * The array will grow in size automatically if necessary.
1800 * Returns: the #GByteArray
1802 GByteArray*
1803 g_byte_array_append (GByteArray *array,
1804 const guint8 *data,
1805 guint len)
1807 g_array_append_vals ((GArray *)array, (guint8 *)data, len);
1809 return array;
1813 * g_byte_array_prepend:
1814 * @array: a #GByteArray
1815 * @data: the byte data to be added
1816 * @len: the number of bytes to add
1818 * Adds the given data to the start of the #GByteArray.
1819 * The array will grow in size automatically if necessary.
1821 * Returns: the #GByteArray
1823 GByteArray*
1824 g_byte_array_prepend (GByteArray *array,
1825 const guint8 *data,
1826 guint len)
1828 g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
1830 return array;
1834 * g_byte_array_set_size:
1835 * @array: a #GByteArray
1836 * @length: the new size of the #GByteArray
1838 * Sets the size of the #GByteArray, expanding it if necessary.
1840 * Returns: the #GByteArray
1842 GByteArray*
1843 g_byte_array_set_size (GByteArray *array,
1844 guint length)
1846 g_array_set_size ((GArray *)array, length);
1848 return array;
1852 * g_byte_array_remove_index:
1853 * @array: a #GByteArray
1854 * @index_: the index of the byte to remove
1856 * Removes the byte at the given index from a #GByteArray.
1857 * The following bytes are moved down one place.
1859 * Returns: the #GByteArray
1861 GByteArray*
1862 g_byte_array_remove_index (GByteArray *array,
1863 guint index_)
1865 g_array_remove_index ((GArray *)array, index_);
1867 return array;
1871 * g_byte_array_remove_index_fast:
1872 * @array: a #GByteArray
1873 * @index_: the index of the byte to remove
1875 * Removes the byte at the given index from a #GByteArray. The last
1876 * element in the array is used to fill in the space, so this function
1877 * does not preserve the order of the #GByteArray. But it is faster
1878 * than g_byte_array_remove_index().
1880 * Returns: the #GByteArray
1882 GByteArray*
1883 g_byte_array_remove_index_fast (GByteArray *array,
1884 guint index_)
1886 g_array_remove_index_fast ((GArray *)array, index_);
1888 return array;
1892 * g_byte_array_remove_range:
1893 * @array: a @GByteArray
1894 * @index_: the index of the first byte to remove
1895 * @length: the number of bytes to remove
1897 * Removes the given number of bytes starting at the given index from a
1898 * #GByteArray. The following elements are moved to close the gap.
1900 * Returns: the #GByteArray
1902 * Since: 2.4
1904 GByteArray*
1905 g_byte_array_remove_range (GByteArray *array,
1906 guint index_,
1907 guint length)
1909 g_return_val_if_fail (array, NULL);
1910 g_return_val_if_fail (index_ <= array->len, NULL);
1911 g_return_val_if_fail (index_ + length <= array->len, NULL);
1913 return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
1917 * g_byte_array_sort:
1918 * @array: a #GByteArray
1919 * @compare_func: comparison function
1921 * Sorts a byte array, using @compare_func which should be a
1922 * qsort()-style comparison function (returns less than zero for first
1923 * arg is less than second arg, zero for equal, greater than zero if
1924 * first arg is greater than second arg).
1926 * If two array elements compare equal, their order in the sorted array
1927 * is undefined. If you want equal elements to keep their order (i.e.
1928 * you want a stable sort) you can write a comparison function that,
1929 * if two elements would otherwise compare equal, compares them by
1930 * their addresses.
1932 void
1933 g_byte_array_sort (GByteArray *array,
1934 GCompareFunc compare_func)
1936 g_array_sort ((GArray *)array, compare_func);
1940 * g_byte_array_sort_with_data:
1941 * @array: a #GByteArray
1942 * @compare_func: comparison function
1943 * @user_data: data to pass to @compare_func
1945 * Like g_byte_array_sort(), but the comparison function takes an extra
1946 * user data argument.
1948 void
1949 g_byte_array_sort_with_data (GByteArray *array,
1950 GCompareDataFunc compare_func,
1951 gpointer user_data)
1953 g_array_sort_with_data ((GArray *)array, compare_func, user_data);