Add reference counting types
[glib.git] / glib / garray.c
blob39c87ca885db6a322ce6430f22baa105f0fcfb92
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. @clear_func will be passed a
224 * pointer to the element to clear, rather than the element itself.
226 * Note that in contrast with other uses of #GDestroyNotify
227 * functions, @clear_func is expected to clear the contents of
228 * the array element it is given, but not free the element itself.
230 * Since: 2.32
232 void
233 g_array_set_clear_func (GArray *array,
234 GDestroyNotify clear_func)
236 GRealArray *rarray = (GRealArray *) array;
238 g_return_if_fail (array != NULL);
240 rarray->clear_func = clear_func;
244 * g_array_ref:
245 * @array: A #GArray
247 * Atomically increments the reference count of @array by one.
248 * This function is thread-safe and may be called from any thread.
250 * Returns: The passed in #GArray
252 * Since: 2.22
254 GArray *
255 g_array_ref (GArray *array)
257 GRealArray *rarray = (GRealArray*) array;
258 g_return_val_if_fail (array, NULL);
260 g_atomic_int_inc (&rarray->ref_count);
262 return array;
265 typedef enum
267 FREE_SEGMENT = 1 << 0,
268 PRESERVE_WRAPPER = 1 << 1
269 } ArrayFreeFlags;
271 static gchar *array_free (GRealArray *, ArrayFreeFlags);
274 * g_array_unref:
275 * @array: A #GArray
277 * Atomically decrements the reference count of @array by one. If the
278 * reference count drops to 0, all memory allocated by the array is
279 * released. This function is thread-safe and may be called from any
280 * thread.
282 * Since: 2.22
284 void
285 g_array_unref (GArray *array)
287 GRealArray *rarray = (GRealArray*) array;
288 g_return_if_fail (array);
290 if (g_atomic_int_dec_and_test (&rarray->ref_count))
291 array_free (rarray, FREE_SEGMENT);
295 * g_array_get_element_size:
296 * @array: A #GArray
298 * Gets the size of the elements in @array.
300 * Returns: Size of each element, in bytes
302 * Since: 2.22
304 guint
305 g_array_get_element_size (GArray *array)
307 GRealArray *rarray = (GRealArray*) array;
309 g_return_val_if_fail (array, 0);
311 return rarray->elt_size;
315 * g_array_free:
316 * @array: a #GArray
317 * @free_segment: if %TRUE the actual element data is freed as well
319 * Frees the memory allocated for the #GArray. If @free_segment is
320 * %TRUE it frees the memory block holding the elements as well and
321 * also each element if @array has a @element_free_func set. Pass
322 * %FALSE if you want to free the #GArray wrapper but preserve the
323 * underlying array for use elsewhere. If the reference count of @array
324 * is greater than one, the #GArray wrapper is preserved but the size
325 * of @array will be set to zero.
327 * If array elements contain dynamically-allocated memory, they should
328 * be freed separately.
330 * This function is not thread-safe. If using a #GArray from multiple
331 * threads, use only the atomic g_array_ref() and g_array_unref()
332 * functions.
334 * Returns: the element data if @free_segment is %FALSE, otherwise
335 * %NULL. The element data should be freed using g_free().
337 gchar*
338 g_array_free (GArray *farray,
339 gboolean free_segment)
341 GRealArray *array = (GRealArray*) farray;
342 ArrayFreeFlags flags;
344 g_return_val_if_fail (array, NULL);
346 flags = (free_segment ? FREE_SEGMENT : 0);
348 /* if others are holding a reference, preserve the wrapper but do free/return the data */
349 if (!g_atomic_int_dec_and_test (&array->ref_count))
350 flags |= PRESERVE_WRAPPER;
352 return array_free (array, flags);
355 static gchar *
356 array_free (GRealArray *array,
357 ArrayFreeFlags flags)
359 gchar *segment;
361 if (flags & FREE_SEGMENT)
363 if (array->clear_func != NULL)
365 guint i;
367 for (i = 0; i < array->len; i++)
368 array->clear_func (g_array_elt_pos (array, i));
371 g_free (array->data);
372 segment = NULL;
374 else
375 segment = (gchar*) array->data;
377 if (flags & PRESERVE_WRAPPER)
379 array->data = NULL;
380 array->len = 0;
381 array->alloc = 0;
383 else
385 g_slice_free1 (sizeof (GRealArray), array);
388 return segment;
392 * g_array_append_vals:
393 * @array: a #GArray
394 * @data: (not nullable): a pointer to the elements to append to the end of the array
395 * @len: the number of elements to append
397 * Adds @len elements onto the end of the array.
399 * Returns: the #GArray
402 * g_array_append_val:
403 * @a: a #GArray
404 * @v: the value to append to the #GArray
406 * Adds the value on to the end of the array. The array will grow in
407 * size automatically if necessary.
409 * g_array_append_val() is a macro which uses a reference to the value
410 * parameter @v. This means that you cannot use it with literal values
411 * such as "27". You must use variables.
413 * Returns: the #GArray
415 GArray*
416 g_array_append_vals (GArray *farray,
417 gconstpointer data,
418 guint len)
420 GRealArray *array = (GRealArray*) farray;
422 g_return_val_if_fail (array, NULL);
424 if (len == 0)
425 return farray;
427 g_array_maybe_expand (array, len);
429 memcpy (g_array_elt_pos (array, array->len), data,
430 g_array_elt_len (array, len));
432 array->len += len;
434 g_array_zero_terminate (array);
436 return farray;
440 * g_array_prepend_vals:
441 * @array: a #GArray
442 * @data: (nullable): a pointer to the elements to prepend to the start of the array
443 * @len: the number of elements to prepend, which may be zero
445 * Adds @len elements onto the start of the array.
447 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this
448 * function is a no-op.
450 * This operation is slower than g_array_append_vals() since the
451 * existing elements in the array have to be moved to make space for
452 * the new elements.
454 * Returns: the #GArray
457 * g_array_prepend_val:
458 * @a: a #GArray
459 * @v: the value to prepend to the #GArray
461 * Adds the value on to the start of the array. The array will grow in
462 * size automatically if necessary.
464 * This operation is slower than g_array_append_val() since the
465 * existing elements in the array have to be moved to make space for
466 * the new element.
468 * g_array_prepend_val() is a macro which uses a reference to the value
469 * parameter @v. This means that you cannot use it with literal values
470 * such as "27". You must use variables.
472 * Returns: the #GArray
474 GArray*
475 g_array_prepend_vals (GArray *farray,
476 gconstpointer data,
477 guint len)
479 GRealArray *array = (GRealArray*) farray;
481 g_return_val_if_fail (array, NULL);
483 if (len == 0)
484 return farray;
486 g_array_maybe_expand (array, len);
488 memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
489 g_array_elt_len (array, array->len));
491 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
493 array->len += len;
495 g_array_zero_terminate (array);
497 return farray;
501 * g_array_insert_vals:
502 * @array: a #GArray
503 * @index_: the index to place the elements at
504 * @data: (nullable): a pointer to the elements to insert
505 * @len: the number of elements to insert
507 * Inserts @len elements into a #GArray at the given index.
509 * If @index_ is greater than the array’s current length, the array is expanded.
510 * The elements between the old end of the array and the newly inserted elements
511 * will be initialised to zero if the array was configured to clear elements;
512 * otherwise their values will be undefined.
514 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this
515 * function is a no-op.
517 * Returns: the #GArray
520 * g_array_insert_val:
521 * @a: a #GArray
522 * @i: the index to place the element at
523 * @v: the value to insert into the array
525 * Inserts an element into an array at the given index.
527 * g_array_insert_val() is a macro which uses a reference to the value
528 * parameter @v. This means that you cannot use it with literal values
529 * such as "27". You must use variables.
531 * Returns: the #GArray
533 GArray*
534 g_array_insert_vals (GArray *farray,
535 guint index_,
536 gconstpointer data,
537 guint len)
539 GRealArray *array = (GRealArray*) farray;
541 g_return_val_if_fail (array, NULL);
543 if (len == 0)
544 return farray;
546 /* Is the index off the end of the array, and hence do we need to over-allocate
547 * and clear some elements? */
548 if (index_ >= array->len)
550 g_array_maybe_expand (array, index_ - array->len + len);
551 return g_array_append_vals (g_array_set_size (farray, index_), data, len);
554 g_array_maybe_expand (array, len);
556 memmove (g_array_elt_pos (array, len + index_),
557 g_array_elt_pos (array, index_),
558 g_array_elt_len (array, array->len - index_));
560 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
562 array->len += len;
564 g_array_zero_terminate (array);
566 return farray;
570 * g_array_set_size:
571 * @array: a #GArray
572 * @length: the new size of the #GArray
574 * Sets the size of the array, expanding it if necessary. If the array
575 * was created with @clear_ set to %TRUE, the new elements are set to 0.
577 * Returns: the #GArray
579 GArray*
580 g_array_set_size (GArray *farray,
581 guint length)
583 GRealArray *array = (GRealArray*) farray;
585 g_return_val_if_fail (array, NULL);
587 if (length > array->len)
589 g_array_maybe_expand (array, length - array->len);
591 if (array->clear)
592 g_array_elt_zero (array, array->len, length - array->len);
594 else if (length < array->len)
595 g_array_remove_range (farray, length, array->len - length);
597 array->len = length;
599 g_array_zero_terminate (array);
601 return farray;
605 * g_array_remove_index:
606 * @array: a #GArray
607 * @index_: the index of the element to remove
609 * Removes the element at the given index from a #GArray. The following
610 * elements are moved down one place.
612 * Returns: the #GArray
614 GArray*
615 g_array_remove_index (GArray *farray,
616 guint index_)
618 GRealArray* array = (GRealArray*) farray;
620 g_return_val_if_fail (array, NULL);
622 g_return_val_if_fail (index_ < array->len, NULL);
624 if (array->clear_func != NULL)
625 array->clear_func (g_array_elt_pos (array, index_));
627 if (index_ != array->len - 1)
628 memmove (g_array_elt_pos (array, index_),
629 g_array_elt_pos (array, index_ + 1),
630 g_array_elt_len (array, array->len - index_ - 1));
632 array->len -= 1;
634 if (G_UNLIKELY (g_mem_gc_friendly))
635 g_array_elt_zero (array, array->len, 1);
636 else
637 g_array_zero_terminate (array);
639 return farray;
643 * g_array_remove_index_fast:
644 * @array: a @GArray
645 * @index_: the index of the element to remove
647 * Removes the element at the given index from a #GArray. The last
648 * element in the array is used to fill in the space, so this function
649 * does not preserve the order of the #GArray. But it is faster than
650 * g_array_remove_index().
652 * Returns: the #GArray
654 GArray*
655 g_array_remove_index_fast (GArray *farray,
656 guint index_)
658 GRealArray* array = (GRealArray*) farray;
660 g_return_val_if_fail (array, NULL);
662 g_return_val_if_fail (index_ < array->len, NULL);
664 if (array->clear_func != NULL)
665 array->clear_func (g_array_elt_pos (array, index_));
667 if (index_ != array->len - 1)
668 memcpy (g_array_elt_pos (array, index_),
669 g_array_elt_pos (array, array->len - 1),
670 g_array_elt_len (array, 1));
672 array->len -= 1;
674 if (G_UNLIKELY (g_mem_gc_friendly))
675 g_array_elt_zero (array, array->len, 1);
676 else
677 g_array_zero_terminate (array);
679 return farray;
683 * g_array_remove_range:
684 * @array: a @GArray
685 * @index_: the index of the first element to remove
686 * @length: the number of elements to remove
688 * Removes the given number of elements starting at the given index
689 * from a #GArray. The following elements are moved to close the gap.
691 * Returns: the #GArray
693 * Since: 2.4
695 GArray*
696 g_array_remove_range (GArray *farray,
697 guint index_,
698 guint length)
700 GRealArray *array = (GRealArray*) farray;
702 g_return_val_if_fail (array, NULL);
703 g_return_val_if_fail (index_ <= array->len, NULL);
704 g_return_val_if_fail (index_ + length <= array->len, NULL);
706 if (array->clear_func != NULL)
708 guint i;
710 for (i = 0; i < length; i++)
711 array->clear_func (g_array_elt_pos (array, index_ + i));
714 if (index_ + length != array->len)
715 memmove (g_array_elt_pos (array, index_),
716 g_array_elt_pos (array, index_ + length),
717 (array->len - (index_ + length)) * array->elt_size);
719 array->len -= length;
720 if (G_UNLIKELY (g_mem_gc_friendly))
721 g_array_elt_zero (array, array->len, length);
722 else
723 g_array_zero_terminate (array);
725 return farray;
729 * g_array_sort:
730 * @array: a #GArray
731 * @compare_func: comparison function
733 * Sorts a #GArray using @compare_func which should be a qsort()-style
734 * comparison function (returns less than zero for first arg is less
735 * than second arg, zero for equal, greater zero if first arg is
736 * greater than second arg).
738 * This is guaranteed to be a stable sort since version 2.32.
740 void
741 g_array_sort (GArray *farray,
742 GCompareFunc compare_func)
744 GRealArray *array = (GRealArray*) farray;
746 g_return_if_fail (array != NULL);
748 /* Don't use qsort as we want a guaranteed stable sort */
749 g_qsort_with_data (array->data,
750 array->len,
751 array->elt_size,
752 (GCompareDataFunc)compare_func,
753 NULL);
757 * g_array_sort_with_data:
758 * @array: a #GArray
759 * @compare_func: comparison function
760 * @user_data: data to pass to @compare_func
762 * Like g_array_sort(), but the comparison function receives an extra
763 * user data argument.
765 * This is guaranteed to be a stable sort since version 2.32.
767 * There used to be a comment here about making the sort stable by
768 * using the addresses of the elements in the comparison function.
769 * This did not actually work, so any such code should be removed.
771 void
772 g_array_sort_with_data (GArray *farray,
773 GCompareDataFunc compare_func,
774 gpointer user_data)
776 GRealArray *array = (GRealArray*) farray;
778 g_return_if_fail (array != NULL);
780 g_qsort_with_data (array->data,
781 array->len,
782 array->elt_size,
783 compare_func,
784 user_data);
787 /* Returns the smallest power of 2 greater than n, or n if
788 * such power does not fit in a guint
790 static guint
791 g_nearest_pow (gint num)
793 guint n = 1;
795 while (n < num && n > 0)
796 n <<= 1;
798 return n ? n : num;
801 static void
802 g_array_maybe_expand (GRealArray *array,
803 gint len)
805 guint want_alloc = g_array_elt_len (array, array->len + len +
806 array->zero_terminated);
808 if (want_alloc > array->alloc)
810 want_alloc = g_nearest_pow (want_alloc);
811 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
813 array->data = g_realloc (array->data, want_alloc);
815 if (G_UNLIKELY (g_mem_gc_friendly))
816 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
818 array->alloc = want_alloc;
823 * SECTION:arrays_pointer
824 * @title: Pointer Arrays
825 * @short_description: arrays of pointers to any type of data, which
826 * grow automatically as new elements are added
828 * Pointer Arrays are similar to Arrays but are used only for storing
829 * pointers.
831 * If you remove elements from the array, elements at the end of the
832 * array are moved into the space previously occupied by the removed
833 * element. This means that you should not rely on the index of particular
834 * elements remaining the same. You should also be careful when deleting
835 * elements while iterating over the array.
837 * To create a pointer array, use g_ptr_array_new().
839 * To add elements to a pointer array, use g_ptr_array_add().
841 * To remove elements from a pointer array, use g_ptr_array_remove(),
842 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
844 * To access an element of a pointer array, use g_ptr_array_index().
846 * To set the size of a pointer array, use g_ptr_array_set_size().
848 * To free a pointer array, use g_ptr_array_free().
850 * An example using a #GPtrArray:
851 * |[<!-- language="C" -->
852 * GPtrArray *array;
853 * gchar *string1 = "one";
854 * gchar *string2 = "two";
855 * gchar *string3 = "three";
857 * array = g_ptr_array_new ();
858 * g_ptr_array_add (array, (gpointer) string1);
859 * g_ptr_array_add (array, (gpointer) string2);
860 * g_ptr_array_add (array, (gpointer) string3);
862 * if (g_ptr_array_index (array, 0) != (gpointer) string1)
863 * g_print ("ERROR: got %p instead of %p\n",
864 * g_ptr_array_index (array, 0), string1);
866 * g_ptr_array_free (array, TRUE);
867 * ]|
870 typedef struct _GRealPtrArray GRealPtrArray;
873 * GPtrArray:
874 * @pdata: points to the array of pointers, which may be moved when the
875 * array grows
876 * @len: number of pointers in the array
878 * Contains the public fields of a pointer array.
880 struct _GRealPtrArray
882 gpointer *pdata;
883 guint len;
884 guint alloc;
885 gint ref_count;
886 GDestroyNotify element_free_func;
890 * g_ptr_array_index:
891 * @array: a #GPtrArray
892 * @index_: the index of the pointer to return
894 * Returns the pointer at the given index of the pointer array.
896 * This does not perform bounds checking on the given @index_,
897 * so you are responsible for checking it against the array length.
899 * Returns: the pointer at the given index
902 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
903 gint len);
906 * g_ptr_array_new:
908 * Creates a new #GPtrArray with a reference count of 1.
910 * Returns: the new #GPtrArray
912 GPtrArray*
913 g_ptr_array_new (void)
915 return g_ptr_array_sized_new (0);
919 * g_ptr_array_sized_new:
920 * @reserved_size: number of pointers preallocated
922 * Creates a new #GPtrArray with @reserved_size pointers preallocated
923 * and a reference count of 1. This avoids frequent reallocation, if
924 * you are going to add many pointers to the array. Note however that
925 * the size of the array is still 0.
927 * Returns: the new #GPtrArray
929 GPtrArray*
930 g_ptr_array_sized_new (guint reserved_size)
932 GRealPtrArray *array;
934 array = g_slice_new (GRealPtrArray);
936 array->pdata = NULL;
937 array->len = 0;
938 array->alloc = 0;
939 array->ref_count = 1;
940 array->element_free_func = NULL;
942 if (reserved_size != 0)
943 g_ptr_array_maybe_expand (array, reserved_size);
945 return (GPtrArray*) array;
949 * g_ptr_array_new_with_free_func:
950 * @element_free_func: (nullable): A function to free elements with
951 * destroy @array or %NULL
953 * Creates a new #GPtrArray with a reference count of 1 and use
954 * @element_free_func for freeing each element when the array is destroyed
955 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
956 * @free_segment set to %TRUE or when removing elements.
958 * Returns: A new #GPtrArray
960 * Since: 2.22
962 GPtrArray*
963 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
965 GPtrArray *array;
967 array = g_ptr_array_new ();
968 g_ptr_array_set_free_func (array, element_free_func);
970 return array;
974 * g_ptr_array_new_full:
975 * @reserved_size: number of pointers preallocated
976 * @element_free_func: (nullable): A function to free elements with
977 * destroy @array or %NULL
979 * Creates a new #GPtrArray with @reserved_size pointers preallocated
980 * and a reference count of 1. This avoids frequent reallocation, if
981 * you are going to add many pointers to the array. Note however that
982 * the size of the array is still 0. It also set @element_free_func
983 * for freeing each element when the array is destroyed either via
984 * g_ptr_array_unref(), when g_ptr_array_free() is called with
985 * @free_segment set to %TRUE or when removing elements.
987 * Returns: A new #GPtrArray
989 * Since: 2.30
991 GPtrArray*
992 g_ptr_array_new_full (guint reserved_size,
993 GDestroyNotify element_free_func)
995 GPtrArray *array;
997 array = g_ptr_array_sized_new (reserved_size);
998 g_ptr_array_set_free_func (array, element_free_func);
1000 return array;
1004 * g_ptr_array_set_free_func:
1005 * @array: A #GPtrArray
1006 * @element_free_func: (nullable): A function to free elements with
1007 * destroy @array or %NULL
1009 * Sets a function for freeing each element when @array is destroyed
1010 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
1011 * with @free_segment set to %TRUE or when removing elements.
1013 * Since: 2.22
1015 void
1016 g_ptr_array_set_free_func (GPtrArray *array,
1017 GDestroyNotify element_free_func)
1019 GRealPtrArray *rarray = (GRealPtrArray *)array;
1021 g_return_if_fail (array);
1023 rarray->element_free_func = element_free_func;
1027 * g_ptr_array_ref:
1028 * @array: a #GPtrArray
1030 * Atomically increments the reference count of @array by one.
1031 * This function is thread-safe and may be called from any thread.
1033 * Returns: The passed in #GPtrArray
1035 * Since: 2.22
1037 GPtrArray*
1038 g_ptr_array_ref (GPtrArray *array)
1040 GRealPtrArray *rarray = (GRealPtrArray *)array;
1042 g_return_val_if_fail (array, NULL);
1044 g_atomic_int_inc (&rarray->ref_count);
1046 return array;
1049 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
1052 * g_ptr_array_unref:
1053 * @array: A #GPtrArray
1055 * Atomically decrements the reference count of @array by one. If the
1056 * reference count drops to 0, the effect is the same as calling
1057 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1058 * is thread-safe and may be called from any thread.
1060 * Since: 2.22
1062 void
1063 g_ptr_array_unref (GPtrArray *array)
1065 GRealPtrArray *rarray = (GRealPtrArray *)array;
1067 g_return_if_fail (array);
1069 if (g_atomic_int_dec_and_test (&rarray->ref_count))
1070 ptr_array_free (array, FREE_SEGMENT);
1074 * g_ptr_array_free:
1075 * @array: a #GPtrArray
1076 * @free_seg: if %TRUE the actual pointer array is freed as well
1078 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1079 * it frees the memory block holding the elements as well. Pass %FALSE
1080 * if you want to free the #GPtrArray wrapper but preserve the
1081 * underlying array for use elsewhere. If the reference count of @array
1082 * is greater than one, the #GPtrArray wrapper is preserved but the
1083 * size of @array will be set to zero.
1085 * If array contents point to dynamically-allocated memory, they should
1086 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
1087 * function has been set for @array.
1089 * This function is not thread-safe. If using a #GPtrArray from multiple
1090 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
1091 * functions.
1093 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1094 * The pointer array should be freed using g_free().
1096 gpointer*
1097 g_ptr_array_free (GPtrArray *array,
1098 gboolean free_segment)
1100 GRealPtrArray *rarray = (GRealPtrArray *)array;
1101 ArrayFreeFlags flags;
1103 g_return_val_if_fail (rarray, NULL);
1105 flags = (free_segment ? FREE_SEGMENT : 0);
1107 /* if others are holding a reference, preserve the wrapper but
1108 * do free/return the data
1110 if (!g_atomic_int_dec_and_test (&rarray->ref_count))
1111 flags |= PRESERVE_WRAPPER;
1113 return ptr_array_free (array, flags);
1116 static gpointer *
1117 ptr_array_free (GPtrArray *array,
1118 ArrayFreeFlags flags)
1120 GRealPtrArray *rarray = (GRealPtrArray *)array;
1121 gpointer *segment;
1123 if (flags & FREE_SEGMENT)
1125 /* Data here is stolen and freed manually. It is an
1126 * error to attempt to access the array data (including
1127 * mutating the array bounds) during destruction).
1129 * https://bugzilla.gnome.org/show_bug.cgi?id=769064
1131 gpointer *stolen_pdata = g_steal_pointer (&rarray->pdata);
1132 if (rarray->element_free_func != NULL)
1134 gsize i;
1135 for (i = 0; i < rarray->len; ++i)
1136 rarray->element_free_func (stolen_pdata[i]);
1139 g_free (stolen_pdata);
1140 segment = NULL;
1142 else
1143 segment = rarray->pdata;
1145 if (flags & PRESERVE_WRAPPER)
1147 rarray->pdata = NULL;
1148 rarray->len = 0;
1149 rarray->alloc = 0;
1151 else
1153 g_slice_free1 (sizeof (GRealPtrArray), rarray);
1156 return segment;
1159 static void
1160 g_ptr_array_maybe_expand (GRealPtrArray *array,
1161 gint len)
1163 if ((array->len + len) > array->alloc)
1165 guint old_alloc = array->alloc;
1166 array->alloc = g_nearest_pow (array->len + len);
1167 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1168 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1169 if (G_UNLIKELY (g_mem_gc_friendly))
1170 for ( ; old_alloc < array->alloc; old_alloc++)
1171 array->pdata [old_alloc] = NULL;
1176 * g_ptr_array_set_size:
1177 * @array: a #GPtrArray
1178 * @length: the new length of the pointer array
1180 * Sets the size of the array. When making the array larger,
1181 * newly-added elements will be set to %NULL. When making it smaller,
1182 * if @array has a non-%NULL #GDestroyNotify function then it will be
1183 * called for the removed elements.
1185 void
1186 g_ptr_array_set_size (GPtrArray *array,
1187 gint length)
1189 GRealPtrArray *rarray = (GRealPtrArray *)array;
1191 g_return_if_fail (rarray);
1192 g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
1194 if (length > rarray->len)
1196 int i;
1197 g_ptr_array_maybe_expand (rarray, (length - rarray->len));
1198 /* This is not
1199 * memset (array->pdata + array->len, 0,
1200 * sizeof (gpointer) * (length - array->len));
1201 * to make it really portable. Remember (void*)NULL needn't be
1202 * bitwise zero. It of course is silly not to use memset (..,0,..).
1204 for (i = rarray->len; i < length; i++)
1205 rarray->pdata[i] = NULL;
1207 else if (length < rarray->len)
1208 g_ptr_array_remove_range (array, length, rarray->len - length);
1210 rarray->len = length;
1213 static gpointer
1214 ptr_array_remove_index (GPtrArray *array,
1215 guint index_,
1216 gboolean fast,
1217 gboolean free_element)
1219 GRealPtrArray *rarray = (GRealPtrArray *) array;
1220 gpointer result;
1222 g_return_val_if_fail (rarray, NULL);
1223 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
1225 g_return_val_if_fail (index_ < rarray->len, NULL);
1227 result = rarray->pdata[index_];
1229 if (rarray->element_free_func != NULL && free_element)
1230 rarray->element_free_func (rarray->pdata[index_]);
1232 if (index_ != rarray->len - 1 && !fast)
1233 memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
1234 sizeof (gpointer) * (rarray->len - index_ - 1));
1235 else if (index_ != rarray->len - 1)
1236 rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
1238 rarray->len -= 1;
1240 if (G_UNLIKELY (g_mem_gc_friendly))
1241 rarray->pdata[rarray->len] = NULL;
1243 return result;
1247 * g_ptr_array_remove_index:
1248 * @array: a #GPtrArray
1249 * @index_: the index of the pointer to remove
1251 * Removes the pointer at the given index from the pointer array.
1252 * The following elements are moved down one place. If @array has
1253 * a non-%NULL #GDestroyNotify function it is called for the removed
1254 * element. If so, the return value from this function will potentially point
1255 * to freed memory (depending on the #GDestroyNotify implementation).
1257 * Returns: (nullable): the pointer which was removed
1259 gpointer
1260 g_ptr_array_remove_index (GPtrArray *array,
1261 guint index_)
1263 return ptr_array_remove_index (array, index_, FALSE, TRUE);
1267 * g_ptr_array_remove_index_fast:
1268 * @array: a #GPtrArray
1269 * @index_: the index of the pointer to remove
1271 * Removes the pointer at the given index from the pointer array.
1272 * The last element in the array is used to fill in the space, so
1273 * this function does not preserve the order of the array. But it
1274 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
1275 * #GDestroyNotify function it is called for the removed element. If so, the
1276 * return value from this function will potentially point to freed memory
1277 * (depending on the #GDestroyNotify implementation).
1279 * Returns: (nullable): the pointer which was removed
1281 gpointer
1282 g_ptr_array_remove_index_fast (GPtrArray *array,
1283 guint index_)
1285 return ptr_array_remove_index (array, index_, TRUE, TRUE);
1289 * g_ptr_array_steal_index:
1290 * @array: a #GPtrArray
1291 * @index_: the index of the pointer to steal
1293 * Removes the pointer at the given index from the pointer array.
1294 * The following elements are moved down one place. The #GDestroyNotify for
1295 * @array is *not* called on the removed element; ownership is transferred to
1296 * the caller of this function.
1298 * Returns: (transfer full) (nullable): the pointer which was removed
1299 * Since: 2.58
1301 gpointer
1302 g_ptr_array_steal_index (GPtrArray *array,
1303 guint index_)
1305 return ptr_array_remove_index (array, index_, FALSE, FALSE);
1309 * g_ptr_array_steal_index_fast:
1310 * @array: a #GPtrArray
1311 * @index_: the index of the pointer to steal
1313 * Removes the pointer at the given index from the pointer array.
1314 * The last element in the array is used to fill in the space, so
1315 * this function does not preserve the order of the array. But it
1316 * is faster than g_ptr_array_steal_index(). The #GDestroyNotify for @array is
1317 * *not* called on the removed element; ownership is transferred to the caller
1318 * of this function.
1320 * Returns: (transfer full) (nullable): the pointer which was removed
1321 * Since: 2.58
1323 gpointer
1324 g_ptr_array_steal_index_fast (GPtrArray *array,
1325 guint index_)
1327 return ptr_array_remove_index (array, index_, TRUE, FALSE);
1331 * g_ptr_array_remove_range:
1332 * @array: a @GPtrArray
1333 * @index_: the index of the first pointer to remove
1334 * @length: the number of pointers to remove
1336 * Removes the given number of pointers starting at the given index
1337 * from a #GPtrArray. The following elements are moved to close the
1338 * gap. If @array has a non-%NULL #GDestroyNotify function it is
1339 * called for the removed elements.
1341 * Returns: the @array
1343 * Since: 2.4
1345 GPtrArray*
1346 g_ptr_array_remove_range (GPtrArray *array,
1347 guint index_,
1348 guint length)
1350 GRealPtrArray *rarray = (GRealPtrArray *)array;
1351 guint n;
1353 g_return_val_if_fail (rarray != NULL, NULL);
1354 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
1355 g_return_val_if_fail (index_ <= rarray->len, NULL);
1356 g_return_val_if_fail (index_ + length <= rarray->len, NULL);
1358 if (rarray->element_free_func != NULL)
1360 for (n = index_; n < index_ + length; n++)
1361 rarray->element_free_func (rarray->pdata[n]);
1364 if (index_ + length != rarray->len)
1366 memmove (&rarray->pdata[index_],
1367 &rarray->pdata[index_ + length],
1368 (rarray->len - (index_ + length)) * sizeof (gpointer));
1371 rarray->len -= length;
1372 if (G_UNLIKELY (g_mem_gc_friendly))
1374 guint i;
1375 for (i = 0; i < length; i++)
1376 rarray->pdata[rarray->len + i] = NULL;
1379 return array;
1383 * g_ptr_array_remove:
1384 * @array: a #GPtrArray
1385 * @data: the pointer to remove
1387 * Removes the first occurrence of the given pointer from the pointer
1388 * array. The following elements are moved down one place. If @array
1389 * has a non-%NULL #GDestroyNotify function it is called for the
1390 * removed element.
1392 * It returns %TRUE if the pointer was removed, or %FALSE if the
1393 * pointer was not found.
1395 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
1396 * is not found in the array
1398 gboolean
1399 g_ptr_array_remove (GPtrArray *array,
1400 gpointer data)
1402 guint i;
1404 g_return_val_if_fail (array, FALSE);
1405 g_return_val_if_fail (array->len == 0 || (array->len != 0 && array->pdata != NULL), FALSE);
1407 for (i = 0; i < array->len; i += 1)
1409 if (array->pdata[i] == data)
1411 g_ptr_array_remove_index (array, i);
1412 return TRUE;
1416 return FALSE;
1420 * g_ptr_array_remove_fast:
1421 * @array: a #GPtrArray
1422 * @data: the pointer to remove
1424 * Removes the first occurrence of the given pointer from the pointer
1425 * array. The last element in the array is used to fill in the space,
1426 * so this function does not preserve the order of the array. But it
1427 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
1428 * #GDestroyNotify function it is called for the removed element.
1430 * It returns %TRUE if the pointer was removed, or %FALSE if the
1431 * pointer was not found.
1433 * Returns: %TRUE if the pointer was found in the array
1435 gboolean
1436 g_ptr_array_remove_fast (GPtrArray *array,
1437 gpointer data)
1439 GRealPtrArray *rarray = (GRealPtrArray *)array;
1440 guint i;
1442 g_return_val_if_fail (rarray, FALSE);
1443 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), FALSE);
1445 for (i = 0; i < rarray->len; i += 1)
1447 if (rarray->pdata[i] == data)
1449 g_ptr_array_remove_index_fast (array, i);
1450 return TRUE;
1454 return FALSE;
1458 * g_ptr_array_add:
1459 * @array: a #GPtrArray
1460 * @data: the pointer to add
1462 * Adds a pointer to the end of the pointer array. The array will grow
1463 * in size automatically if necessary.
1465 void
1466 g_ptr_array_add (GPtrArray *array,
1467 gpointer data)
1469 GRealPtrArray *rarray = (GRealPtrArray *)array;
1471 g_return_if_fail (rarray);
1472 g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
1474 g_ptr_array_maybe_expand (rarray, 1);
1476 rarray->pdata[rarray->len++] = data;
1480 * g_ptr_array_insert:
1481 * @array: a #GPtrArray
1482 * @index_: the index to place the new element at, or -1 to append
1483 * @data: the pointer to add.
1485 * Inserts an element into the pointer array at the given index. The
1486 * array will grow in size automatically if necessary.
1488 * Since: 2.40
1490 void
1491 g_ptr_array_insert (GPtrArray *array,
1492 gint index_,
1493 gpointer data)
1495 GRealPtrArray *rarray = (GRealPtrArray *)array;
1497 g_return_if_fail (rarray);
1498 g_return_if_fail (index_ >= -1);
1499 g_return_if_fail (index_ <= (gint)rarray->len);
1501 g_ptr_array_maybe_expand (rarray, 1);
1503 if (index_ < 0)
1504 index_ = rarray->len;
1506 if (index_ < rarray->len)
1507 memmove (&(rarray->pdata[index_ + 1]),
1508 &(rarray->pdata[index_]),
1509 (rarray->len - index_) * sizeof (gpointer));
1511 rarray->len++;
1512 rarray->pdata[index_] = data;
1516 * g_ptr_array_sort:
1517 * @array: a #GPtrArray
1518 * @compare_func: comparison function
1520 * Sorts the array, using @compare_func which should be a qsort()-style
1521 * comparison function (returns less than zero for first arg is less
1522 * than second arg, zero for equal, greater than zero if irst arg is
1523 * greater than second arg).
1525 * Note that the comparison function for g_ptr_array_sort() doesn't
1526 * take the pointers from the array as arguments, it takes pointers to
1527 * the pointers in the array.
1529 * This is guaranteed to be a stable sort since version 2.32.
1531 void
1532 g_ptr_array_sort (GPtrArray *array,
1533 GCompareFunc compare_func)
1535 g_return_if_fail (array != NULL);
1537 /* Don't use qsort as we want a guaranteed stable sort */
1538 g_qsort_with_data (array->pdata,
1539 array->len,
1540 sizeof (gpointer),
1541 (GCompareDataFunc)compare_func,
1542 NULL);
1546 * g_ptr_array_sort_with_data:
1547 * @array: a #GPtrArray
1548 * @compare_func: comparison function
1549 * @user_data: data to pass to @compare_func
1551 * Like g_ptr_array_sort(), but the comparison function has an extra
1552 * user data argument.
1554 * Note that the comparison function for g_ptr_array_sort_with_data()
1555 * doesn't take the pointers from the array as arguments, it takes
1556 * pointers to the pointers in the array.
1558 * This is guaranteed to be a stable sort since version 2.32.
1560 void
1561 g_ptr_array_sort_with_data (GPtrArray *array,
1562 GCompareDataFunc compare_func,
1563 gpointer user_data)
1565 g_return_if_fail (array != NULL);
1567 g_qsort_with_data (array->pdata,
1568 array->len,
1569 sizeof (gpointer),
1570 compare_func,
1571 user_data);
1575 * g_ptr_array_foreach:
1576 * @array: a #GPtrArray
1577 * @func: the function to call for each array element
1578 * @user_data: user data to pass to the function
1580 * Calls a function for each element of a #GPtrArray. @func must not
1581 * add elements to or remove elements from the array.
1583 * Since: 2.4
1585 void
1586 g_ptr_array_foreach (GPtrArray *array,
1587 GFunc func,
1588 gpointer user_data)
1590 guint i;
1592 g_return_if_fail (array);
1594 for (i = 0; i < array->len; i++)
1595 (*func) (array->pdata[i], user_data);
1599 * g_ptr_array_find: (skip)
1600 * @haystack: pointer array to be searched
1601 * @needle: pointer to look for
1602 * @index_: (optional) (out caller-allocates): return location for the index of
1603 * the element, if found
1605 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is
1606 * returned and the element’s index is returned in @index_ (if non-%NULL).
1607 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
1608 * multiple times in @haystack, the index of the first instance is returned.
1610 * This does pointer comparisons only. If you want to use more complex equality
1611 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
1613 * Returns: %TRUE if @needle is one of the elements of @haystack
1614 * Since: 2.54
1616 gboolean
1617 g_ptr_array_find (GPtrArray *haystack,
1618 gconstpointer needle,
1619 guint *index_)
1621 return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
1625 * g_ptr_array_find_with_equal_func: (skip)
1626 * @haystack: pointer array to be searched
1627 * @needle: pointer to look for
1628 * @equal_func: (nullable): the function to call for each element, which should
1629 * return %TRUE when the desired element is found; or %NULL to use pointer
1630 * equality
1631 * @index_: (optional) (out caller-allocates): return location for the index of
1632 * the element, if found
1634 * Checks whether @needle exists in @haystack, using the given @equal_func.
1635 * If the element is found, %TRUE is returned and the element’s index is
1636 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
1637 * is undefined. If @needle exists multiple times in @haystack, the index of
1638 * the first instance is returned.
1640 * @equal_func is called with the element from the array as its first parameter,
1641 * and @needle as its second parameter. If @equal_func is %NULL, pointer
1642 * equality is used.
1644 * Returns: %TRUE if @needle is one of the elements of @haystack
1645 * Since: 2.54
1647 gboolean
1648 g_ptr_array_find_with_equal_func (GPtrArray *haystack,
1649 gconstpointer needle,
1650 GEqualFunc equal_func,
1651 guint *index_)
1653 guint i;
1655 g_return_val_if_fail (haystack != NULL, FALSE);
1657 if (equal_func == NULL)
1658 equal_func = g_direct_equal;
1660 for (i = 0; i < haystack->len; i++)
1662 if (equal_func (g_ptr_array_index (haystack, i), needle))
1664 if (index_ != NULL)
1665 *index_ = i;
1666 return TRUE;
1670 return FALSE;
1674 * SECTION:arrays_byte
1675 * @title: Byte Arrays
1676 * @short_description: arrays of bytes
1678 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1679 * of bytes which grow automatically as elements are added.
1681 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1682 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1684 * To set the size of a #GByteArray, use g_byte_array_set_size().
1686 * To free a #GByteArray, use g_byte_array_free().
1688 * An example for using a #GByteArray:
1689 * |[<!-- language="C" -->
1690 * GByteArray *gbarray;
1691 * gint i;
1693 * gbarray = g_byte_array_new ();
1694 * for (i = 0; i < 10000; i++)
1695 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1697 * for (i = 0; i < 10000; i++)
1699 * g_assert (gbarray->data[4*i] == 'a');
1700 * g_assert (gbarray->data[4*i+1] == 'b');
1701 * g_assert (gbarray->data[4*i+2] == 'c');
1702 * g_assert (gbarray->data[4*i+3] == 'd');
1705 * g_byte_array_free (gbarray, TRUE);
1706 * ]|
1708 * See #GBytes if you are interested in an immutable object representing a
1709 * sequence of bytes.
1713 * GByteArray:
1714 * @data: a pointer to the element data. The data may be moved as
1715 * elements are added to the #GByteArray
1716 * @len: the number of elements in the #GByteArray
1718 * Contains the public fields of a GByteArray.
1722 * g_byte_array_new:
1724 * Creates a new #GByteArray with a reference count of 1.
1726 * Returns: (transfer full): the new #GByteArray
1728 GByteArray*
1729 g_byte_array_new (void)
1731 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
1735 * g_byte_array_new_take:
1736 * @data: (transfer full) (array length=len): byte data for the array
1737 * @len: length of @data
1739 * Create byte array containing the data. The data will be owned by the array
1740 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1742 * Since: 2.32
1744 * Returns: (transfer full): a new #GByteArray
1746 GByteArray*
1747 g_byte_array_new_take (guint8 *data,
1748 gsize len)
1750 GByteArray *array;
1751 GRealArray *real;
1753 array = g_byte_array_new ();
1754 real = (GRealArray *)array;
1755 g_assert (real->data == NULL);
1756 g_assert (real->len == 0);
1758 real->data = data;
1759 real->len = len;
1760 real->alloc = len;
1762 return array;
1766 * g_byte_array_sized_new:
1767 * @reserved_size: number of bytes preallocated
1769 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1770 * This avoids frequent reallocation, if you are going to add many
1771 * bytes to the array. Note however that the size of the array is still
1772 * 0.
1774 * Returns: the new #GByteArray
1776 GByteArray*
1777 g_byte_array_sized_new (guint reserved_size)
1779 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1783 * g_byte_array_free:
1784 * @array: a #GByteArray
1785 * @free_segment: if %TRUE the actual byte data is freed as well
1787 * Frees the memory allocated by the #GByteArray. If @free_segment is
1788 * %TRUE it frees the actual byte data. If the reference count of
1789 * @array is greater than one, the #GByteArray wrapper is preserved but
1790 * the size of @array will be set to zero.
1792 * Returns: the element data if @free_segment is %FALSE, otherwise
1793 * %NULL. The element data should be freed using g_free().
1795 guint8*
1796 g_byte_array_free (GByteArray *array,
1797 gboolean free_segment)
1799 return (guint8 *)g_array_free ((GArray *)array, free_segment);
1803 * g_byte_array_free_to_bytes:
1804 * @array: (transfer full): a #GByteArray
1806 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1808 * The #GByteArray is freed unless the reference count of @array is greater
1809 * than one, the #GByteArray wrapper is preserved but the size of @array
1810 * will be set to zero.
1812 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1813 * together.
1815 * Since: 2.32
1817 * Returns: (transfer full): a new immutable #GBytes representing same
1818 * byte data that was in the array
1820 GBytes*
1821 g_byte_array_free_to_bytes (GByteArray *array)
1823 gsize length;
1825 g_return_val_if_fail (array != NULL, NULL);
1827 length = array->len;
1828 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1832 * g_byte_array_ref:
1833 * @array: A #GByteArray
1835 * Atomically increments the reference count of @array by one.
1836 * This function is thread-safe and may be called from any thread.
1838 * Returns: The passed in #GByteArray
1840 * Since: 2.22
1842 GByteArray*
1843 g_byte_array_ref (GByteArray *array)
1845 return (GByteArray *)g_array_ref ((GArray *)array);
1849 * g_byte_array_unref:
1850 * @array: A #GByteArray
1852 * Atomically decrements the reference count of @array by one. If the
1853 * reference count drops to 0, all memory allocated by the array is
1854 * released. This function is thread-safe and may be called from any
1855 * thread.
1857 * Since: 2.22
1859 void
1860 g_byte_array_unref (GByteArray *array)
1862 g_array_unref ((GArray *)array);
1866 * g_byte_array_append:
1867 * @array: a #GByteArray
1868 * @data: the byte data to be added
1869 * @len: the number of bytes to add
1871 * Adds the given bytes to the end of the #GByteArray.
1872 * The array will grow in size automatically if necessary.
1874 * Returns: the #GByteArray
1876 GByteArray*
1877 g_byte_array_append (GByteArray *array,
1878 const guint8 *data,
1879 guint len)
1881 g_array_append_vals ((GArray *)array, (guint8 *)data, len);
1883 return array;
1887 * g_byte_array_prepend:
1888 * @array: a #GByteArray
1889 * @data: the byte data to be added
1890 * @len: the number of bytes to add
1892 * Adds the given data to the start of the #GByteArray.
1893 * The array will grow in size automatically if necessary.
1895 * Returns: the #GByteArray
1897 GByteArray*
1898 g_byte_array_prepend (GByteArray *array,
1899 const guint8 *data,
1900 guint len)
1902 g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
1904 return array;
1908 * g_byte_array_set_size:
1909 * @array: a #GByteArray
1910 * @length: the new size of the #GByteArray
1912 * Sets the size of the #GByteArray, expanding it if necessary.
1914 * Returns: the #GByteArray
1916 GByteArray*
1917 g_byte_array_set_size (GByteArray *array,
1918 guint length)
1920 g_array_set_size ((GArray *)array, length);
1922 return array;
1926 * g_byte_array_remove_index:
1927 * @array: a #GByteArray
1928 * @index_: the index of the byte to remove
1930 * Removes the byte at the given index from a #GByteArray.
1931 * The following bytes are moved down one place.
1933 * Returns: the #GByteArray
1935 GByteArray*
1936 g_byte_array_remove_index (GByteArray *array,
1937 guint index_)
1939 g_array_remove_index ((GArray *)array, index_);
1941 return array;
1945 * g_byte_array_remove_index_fast:
1946 * @array: a #GByteArray
1947 * @index_: the index of the byte to remove
1949 * Removes the byte at the given index from a #GByteArray. The last
1950 * element in the array is used to fill in the space, so this function
1951 * does not preserve the order of the #GByteArray. But it is faster
1952 * than g_byte_array_remove_index().
1954 * Returns: the #GByteArray
1956 GByteArray*
1957 g_byte_array_remove_index_fast (GByteArray *array,
1958 guint index_)
1960 g_array_remove_index_fast ((GArray *)array, index_);
1962 return array;
1966 * g_byte_array_remove_range:
1967 * @array: a @GByteArray
1968 * @index_: the index of the first byte to remove
1969 * @length: the number of bytes to remove
1971 * Removes the given number of bytes starting at the given index from a
1972 * #GByteArray. The following elements are moved to close the gap.
1974 * Returns: the #GByteArray
1976 * Since: 2.4
1978 GByteArray*
1979 g_byte_array_remove_range (GByteArray *array,
1980 guint index_,
1981 guint length)
1983 g_return_val_if_fail (array, NULL);
1984 g_return_val_if_fail (index_ <= array->len, NULL);
1985 g_return_val_if_fail (index_ + length <= array->len, NULL);
1987 return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
1991 * g_byte_array_sort:
1992 * @array: a #GByteArray
1993 * @compare_func: comparison function
1995 * Sorts a byte array, using @compare_func which should be a
1996 * qsort()-style comparison function (returns less than zero for first
1997 * arg is less than second arg, zero for equal, greater than zero if
1998 * first arg is greater than second arg).
2000 * If two array elements compare equal, their order in the sorted array
2001 * is undefined. If you want equal elements to keep their order (i.e.
2002 * you want a stable sort) you can write a comparison function that,
2003 * if two elements would otherwise compare equal, compares them by
2004 * their addresses.
2006 void
2007 g_byte_array_sort (GByteArray *array,
2008 GCompareFunc compare_func)
2010 g_array_sort ((GArray *)array, compare_func);
2014 * g_byte_array_sort_with_data:
2015 * @array: a #GByteArray
2016 * @compare_func: comparison function
2017 * @user_data: data to pass to @compare_func
2019 * Like g_byte_array_sort(), but the comparison function takes an extra
2020 * user data argument.
2022 void
2023 g_byte_array_sort_with_data (GByteArray *array,
2024 GCompareDataFunc compare_func,
2025 gpointer user_data)
2027 g_array_sort_with_data ((GArray *)array, compare_func, user_data);