Merge branch 'doc-types' into 'master'
[glib.git] / glib / garray.c
blob8f908e44385fadb71e73c350cbfdee706967f3a4
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"
44 #include "grefcount.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 gatomicrefcount 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->clear_func = NULL;
204 g_atomic_ref_count_init (&array->ref_count);
206 if (array->zero_terminated || reserved_size != 0)
208 g_array_maybe_expand (array, reserved_size);
209 g_array_zero_terminate(array);
212 return (GArray*) array;
216 * g_array_set_clear_func:
217 * @array: A #GArray
218 * @clear_func: a function to clear an element of @array
220 * Sets a function to clear an element of @array.
222 * The @clear_func will be called when an element in the array
223 * data segment is removed and when the array is freed and data
224 * segment is deallocated as well. @clear_func will be passed a
225 * pointer to the element to clear, rather than the element itself.
227 * Note that in contrast with other uses of #GDestroyNotify
228 * functions, @clear_func is expected to clear the contents of
229 * the array element it is given, but not free the element itself.
231 * Since: 2.32
233 void
234 g_array_set_clear_func (GArray *array,
235 GDestroyNotify clear_func)
237 GRealArray *rarray = (GRealArray *) array;
239 g_return_if_fail (array != NULL);
241 rarray->clear_func = clear_func;
245 * g_array_ref:
246 * @array: A #GArray
248 * Atomically increments the reference count of @array by one.
249 * This function is thread-safe and may be called from any thread.
251 * Returns: The passed in #GArray
253 * Since: 2.22
255 GArray *
256 g_array_ref (GArray *array)
258 GRealArray *rarray = (GRealArray*) array;
259 g_return_val_if_fail (array, NULL);
261 g_atomic_ref_count_inc (&rarray->ref_count);
263 return array;
266 typedef enum
268 FREE_SEGMENT = 1 << 0,
269 PRESERVE_WRAPPER = 1 << 1
270 } ArrayFreeFlags;
272 static gchar *array_free (GRealArray *, ArrayFreeFlags);
275 * g_array_unref:
276 * @array: A #GArray
278 * Atomically decrements the reference count of @array by one. If the
279 * reference count drops to 0, all memory allocated by the array is
280 * released. This function is thread-safe and may be called from any
281 * thread.
283 * Since: 2.22
285 void
286 g_array_unref (GArray *array)
288 GRealArray *rarray = (GRealArray*) array;
289 g_return_if_fail (array);
291 if (g_atomic_ref_count_dec (&rarray->ref_count))
292 array_free (rarray, FREE_SEGMENT);
296 * g_array_get_element_size:
297 * @array: A #GArray
299 * Gets the size of the elements in @array.
301 * Returns: Size of each element, in bytes
303 * Since: 2.22
305 guint
306 g_array_get_element_size (GArray *array)
308 GRealArray *rarray = (GRealArray*) array;
310 g_return_val_if_fail (array, 0);
312 return rarray->elt_size;
316 * g_array_free:
317 * @array: a #GArray
318 * @free_segment: if %TRUE the actual element data is freed as well
320 * Frees the memory allocated for the #GArray. If @free_segment is
321 * %TRUE it frees the memory block holding the elements as well and
322 * also each element if @array has a @element_free_func set. Pass
323 * %FALSE if you want to free the #GArray wrapper but preserve the
324 * underlying array for use elsewhere. If the reference count of @array
325 * is greater than one, the #GArray wrapper is preserved but the size
326 * of @array will be set to zero.
328 * If array elements contain dynamically-allocated memory, they should
329 * be freed separately.
331 * This function is not thread-safe. If using a #GArray from multiple
332 * threads, use only the atomic g_array_ref() and g_array_unref()
333 * functions.
335 * Returns: the element data if @free_segment is %FALSE, otherwise
336 * %NULL. The element data should be freed using g_free().
338 gchar*
339 g_array_free (GArray *farray,
340 gboolean free_segment)
342 GRealArray *array = (GRealArray*) farray;
343 ArrayFreeFlags flags;
345 g_return_val_if_fail (array, NULL);
347 flags = (free_segment ? FREE_SEGMENT : 0);
349 /* if others are holding a reference, preserve the wrapper but do free/return the data */
350 if (!g_atomic_ref_count_dec (&array->ref_count))
351 flags |= PRESERVE_WRAPPER;
353 return array_free (array, flags);
356 static gchar *
357 array_free (GRealArray *array,
358 ArrayFreeFlags flags)
360 gchar *segment;
362 if (flags & FREE_SEGMENT)
364 if (array->clear_func != NULL)
366 guint i;
368 for (i = 0; i < array->len; i++)
369 array->clear_func (g_array_elt_pos (array, i));
372 g_free (array->data);
373 segment = NULL;
375 else
376 segment = (gchar*) array->data;
378 if (flags & PRESERVE_WRAPPER)
380 array->data = NULL;
381 array->len = 0;
382 array->alloc = 0;
384 else
386 g_slice_free1 (sizeof (GRealArray), array);
389 return segment;
393 * g_array_append_vals:
394 * @array: a #GArray
395 * @data: (not nullable): a pointer to the elements to append to the end of the array
396 * @len: the number of elements to append
398 * Adds @len elements onto the end of the array.
400 * Returns: the #GArray
403 * g_array_append_val:
404 * @a: a #GArray
405 * @v: the value to append to the #GArray
407 * Adds the value on to the end of the array. The array will grow in
408 * size automatically if necessary.
410 * g_array_append_val() is a macro which uses a reference to the value
411 * parameter @v. This means that you cannot use it with literal values
412 * such as "27". You must use variables.
414 * Returns: the #GArray
416 GArray*
417 g_array_append_vals (GArray *farray,
418 gconstpointer data,
419 guint len)
421 GRealArray *array = (GRealArray*) farray;
423 g_return_val_if_fail (array, NULL);
425 if (len == 0)
426 return farray;
428 g_array_maybe_expand (array, len);
430 memcpy (g_array_elt_pos (array, array->len), data,
431 g_array_elt_len (array, len));
433 array->len += len;
435 g_array_zero_terminate (array);
437 return farray;
441 * g_array_prepend_vals:
442 * @array: a #GArray
443 * @data: (nullable): a pointer to the elements to prepend to the start of the array
444 * @len: the number of elements to prepend, which may be zero
446 * Adds @len elements onto the start of the array.
448 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this
449 * function is a no-op.
451 * This operation is slower than g_array_append_vals() since the
452 * existing elements in the array have to be moved to make space for
453 * the new elements.
455 * Returns: the #GArray
458 * g_array_prepend_val:
459 * @a: a #GArray
460 * @v: the value to prepend to the #GArray
462 * Adds the value on to the start of the array. The array will grow in
463 * size automatically if necessary.
465 * This operation is slower than g_array_append_val() since the
466 * existing elements in the array have to be moved to make space for
467 * the new element.
469 * g_array_prepend_val() is a macro which uses a reference to the value
470 * parameter @v. This means that you cannot use it with literal values
471 * such as "27". You must use variables.
473 * Returns: the #GArray
475 GArray*
476 g_array_prepend_vals (GArray *farray,
477 gconstpointer data,
478 guint len)
480 GRealArray *array = (GRealArray*) farray;
482 g_return_val_if_fail (array, NULL);
484 if (len == 0)
485 return farray;
487 g_array_maybe_expand (array, len);
489 memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
490 g_array_elt_len (array, array->len));
492 memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
494 array->len += len;
496 g_array_zero_terminate (array);
498 return farray;
502 * g_array_insert_vals:
503 * @array: a #GArray
504 * @index_: the index to place the elements at
505 * @data: (nullable): a pointer to the elements to insert
506 * @len: the number of elements to insert
508 * Inserts @len elements into a #GArray at the given index.
510 * If @index_ is greater than the array’s current length, the array is expanded.
511 * The elements between the old end of the array and the newly inserted elements
512 * will be initialised to zero if the array was configured to clear elements;
513 * otherwise their values will be undefined.
515 * @data may be %NULL if (and only if) @len is zero. If @len is zero, this
516 * function is a no-op.
518 * Returns: the #GArray
521 * g_array_insert_val:
522 * @a: a #GArray
523 * @i: the index to place the element at
524 * @v: the value to insert into the array
526 * Inserts an element into an array at the given index.
528 * g_array_insert_val() is a macro which uses a reference to the value
529 * parameter @v. This means that you cannot use it with literal values
530 * such as "27". You must use variables.
532 * Returns: the #GArray
534 GArray*
535 g_array_insert_vals (GArray *farray,
536 guint index_,
537 gconstpointer data,
538 guint len)
540 GRealArray *array = (GRealArray*) farray;
542 g_return_val_if_fail (array, NULL);
544 if (len == 0)
545 return farray;
547 /* Is the index off the end of the array, and hence do we need to over-allocate
548 * and clear some elements? */
549 if (index_ >= array->len)
551 g_array_maybe_expand (array, index_ - array->len + len);
552 return g_array_append_vals (g_array_set_size (farray, index_), data, len);
555 g_array_maybe_expand (array, len);
557 memmove (g_array_elt_pos (array, len + index_),
558 g_array_elt_pos (array, index_),
559 g_array_elt_len (array, array->len - index_));
561 memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
563 array->len += len;
565 g_array_zero_terminate (array);
567 return farray;
571 * g_array_set_size:
572 * @array: a #GArray
573 * @length: the new size of the #GArray
575 * Sets the size of the array, expanding it if necessary. If the array
576 * was created with @clear_ set to %TRUE, the new elements are set to 0.
578 * Returns: the #GArray
580 GArray*
581 g_array_set_size (GArray *farray,
582 guint length)
584 GRealArray *array = (GRealArray*) farray;
586 g_return_val_if_fail (array, NULL);
588 if (length > array->len)
590 g_array_maybe_expand (array, length - array->len);
592 if (array->clear)
593 g_array_elt_zero (array, array->len, length - array->len);
595 else if (length < array->len)
596 g_array_remove_range (farray, length, array->len - length);
598 array->len = length;
600 g_array_zero_terminate (array);
602 return farray;
606 * g_array_remove_index:
607 * @array: a #GArray
608 * @index_: the index of the element to remove
610 * Removes the element at the given index from a #GArray. The following
611 * elements are moved down one place.
613 * Returns: the #GArray
615 GArray*
616 g_array_remove_index (GArray *farray,
617 guint index_)
619 GRealArray* array = (GRealArray*) farray;
621 g_return_val_if_fail (array, NULL);
623 g_return_val_if_fail (index_ < array->len, NULL);
625 if (array->clear_func != NULL)
626 array->clear_func (g_array_elt_pos (array, index_));
628 if (index_ != array->len - 1)
629 memmove (g_array_elt_pos (array, index_),
630 g_array_elt_pos (array, index_ + 1),
631 g_array_elt_len (array, array->len - index_ - 1));
633 array->len -= 1;
635 if (G_UNLIKELY (g_mem_gc_friendly))
636 g_array_elt_zero (array, array->len, 1);
637 else
638 g_array_zero_terminate (array);
640 return farray;
644 * g_array_remove_index_fast:
645 * @array: a @GArray
646 * @index_: the index of the element to remove
648 * Removes the element at the given index from a #GArray. The last
649 * element in the array is used to fill in the space, so this function
650 * does not preserve the order of the #GArray. But it is faster than
651 * g_array_remove_index().
653 * Returns: the #GArray
655 GArray*
656 g_array_remove_index_fast (GArray *farray,
657 guint index_)
659 GRealArray* array = (GRealArray*) farray;
661 g_return_val_if_fail (array, NULL);
663 g_return_val_if_fail (index_ < array->len, NULL);
665 if (array->clear_func != NULL)
666 array->clear_func (g_array_elt_pos (array, index_));
668 if (index_ != array->len - 1)
669 memcpy (g_array_elt_pos (array, index_),
670 g_array_elt_pos (array, array->len - 1),
671 g_array_elt_len (array, 1));
673 array->len -= 1;
675 if (G_UNLIKELY (g_mem_gc_friendly))
676 g_array_elt_zero (array, array->len, 1);
677 else
678 g_array_zero_terminate (array);
680 return farray;
684 * g_array_remove_range:
685 * @array: a @GArray
686 * @index_: the index of the first element to remove
687 * @length: the number of elements to remove
689 * Removes the given number of elements starting at the given index
690 * from a #GArray. The following elements are moved to close the gap.
692 * Returns: the #GArray
694 * Since: 2.4
696 GArray*
697 g_array_remove_range (GArray *farray,
698 guint index_,
699 guint length)
701 GRealArray *array = (GRealArray*) farray;
703 g_return_val_if_fail (array, NULL);
704 g_return_val_if_fail (index_ <= array->len, NULL);
705 g_return_val_if_fail (index_ + length <= array->len, NULL);
707 if (array->clear_func != NULL)
709 guint i;
711 for (i = 0; i < length; i++)
712 array->clear_func (g_array_elt_pos (array, index_ + i));
715 if (index_ + length != array->len)
716 memmove (g_array_elt_pos (array, index_),
717 g_array_elt_pos (array, index_ + length),
718 (array->len - (index_ + length)) * array->elt_size);
720 array->len -= length;
721 if (G_UNLIKELY (g_mem_gc_friendly))
722 g_array_elt_zero (array, array->len, length);
723 else
724 g_array_zero_terminate (array);
726 return farray;
730 * g_array_sort:
731 * @array: a #GArray
732 * @compare_func: comparison function
734 * Sorts a #GArray using @compare_func which should be a qsort()-style
735 * comparison function (returns less than zero for first arg is less
736 * than second arg, zero for equal, greater zero if first arg is
737 * greater than second arg).
739 * This is guaranteed to be a stable sort since version 2.32.
741 void
742 g_array_sort (GArray *farray,
743 GCompareFunc compare_func)
745 GRealArray *array = (GRealArray*) farray;
747 g_return_if_fail (array != NULL);
749 /* Don't use qsort as we want a guaranteed stable sort */
750 g_qsort_with_data (array->data,
751 array->len,
752 array->elt_size,
753 (GCompareDataFunc)compare_func,
754 NULL);
758 * g_array_sort_with_data:
759 * @array: a #GArray
760 * @compare_func: comparison function
761 * @user_data: data to pass to @compare_func
763 * Like g_array_sort(), but the comparison function receives an extra
764 * user data argument.
766 * This is guaranteed to be a stable sort since version 2.32.
768 * There used to be a comment here about making the sort stable by
769 * using the addresses of the elements in the comparison function.
770 * This did not actually work, so any such code should be removed.
772 void
773 g_array_sort_with_data (GArray *farray,
774 GCompareDataFunc compare_func,
775 gpointer user_data)
777 GRealArray *array = (GRealArray*) farray;
779 g_return_if_fail (array != NULL);
781 g_qsort_with_data (array->data,
782 array->len,
783 array->elt_size,
784 compare_func,
785 user_data);
788 /* Returns the smallest power of 2 greater than n, or n if
789 * such power does not fit in a guint
791 static guint
792 g_nearest_pow (gint num)
794 guint n = 1;
796 while (n < num && n > 0)
797 n <<= 1;
799 return n ? n : num;
802 static void
803 g_array_maybe_expand (GRealArray *array,
804 gint len)
806 guint want_alloc = g_array_elt_len (array, array->len + len +
807 array->zero_terminated);
809 if (want_alloc > array->alloc)
811 want_alloc = g_nearest_pow (want_alloc);
812 want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
814 array->data = g_realloc (array->data, want_alloc);
816 if (G_UNLIKELY (g_mem_gc_friendly))
817 memset (array->data + array->alloc, 0, want_alloc - array->alloc);
819 array->alloc = want_alloc;
824 * SECTION:arrays_pointer
825 * @title: Pointer Arrays
826 * @short_description: arrays of pointers to any type of data, which
827 * grow automatically as new elements are added
829 * Pointer Arrays are similar to Arrays but are used only for storing
830 * pointers.
832 * If you remove elements from the array, elements at the end of the
833 * array are moved into the space previously occupied by the removed
834 * element. This means that you should not rely on the index of particular
835 * elements remaining the same. You should also be careful when deleting
836 * elements while iterating over the array.
838 * To create a pointer array, use g_ptr_array_new().
840 * To add elements to a pointer array, use g_ptr_array_add().
842 * To remove elements from a pointer array, use g_ptr_array_remove(),
843 * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
845 * To access an element of a pointer array, use g_ptr_array_index().
847 * To set the size of a pointer array, use g_ptr_array_set_size().
849 * To free a pointer array, use g_ptr_array_free().
851 * An example using a #GPtrArray:
852 * |[<!-- language="C" -->
853 * GPtrArray *array;
854 * gchar *string1 = "one";
855 * gchar *string2 = "two";
856 * gchar *string3 = "three";
858 * array = g_ptr_array_new ();
859 * g_ptr_array_add (array, (gpointer) string1);
860 * g_ptr_array_add (array, (gpointer) string2);
861 * g_ptr_array_add (array, (gpointer) string3);
863 * if (g_ptr_array_index (array, 0) != (gpointer) string1)
864 * g_print ("ERROR: got %p instead of %p\n",
865 * g_ptr_array_index (array, 0), string1);
867 * g_ptr_array_free (array, TRUE);
868 * ]|
871 typedef struct _GRealPtrArray GRealPtrArray;
874 * GPtrArray:
875 * @pdata: points to the array of pointers, which may be moved when the
876 * array grows
877 * @len: number of pointers in the array
879 * Contains the public fields of a pointer array.
881 struct _GRealPtrArray
883 gpointer *pdata;
884 guint len;
885 guint alloc;
886 gatomicrefcount ref_count;
887 GDestroyNotify element_free_func;
891 * g_ptr_array_index:
892 * @array: a #GPtrArray
893 * @index_: the index of the pointer to return
895 * Returns the pointer at the given index of the pointer array.
897 * This does not perform bounds checking on the given @index_,
898 * so you are responsible for checking it against the array length.
900 * Returns: the pointer at the given index
903 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
904 gint len);
907 * g_ptr_array_new:
909 * Creates a new #GPtrArray with a reference count of 1.
911 * Returns: the new #GPtrArray
913 GPtrArray*
914 g_ptr_array_new (void)
916 return g_ptr_array_sized_new (0);
920 * g_ptr_array_sized_new:
921 * @reserved_size: number of pointers preallocated
923 * Creates a new #GPtrArray with @reserved_size pointers preallocated
924 * and a reference count of 1. This avoids frequent reallocation, if
925 * you are going to add many pointers to the array. Note however that
926 * the size of the array is still 0.
928 * Returns: the new #GPtrArray
930 GPtrArray*
931 g_ptr_array_sized_new (guint reserved_size)
933 GRealPtrArray *array;
935 array = g_slice_new (GRealPtrArray);
937 array->pdata = NULL;
938 array->len = 0;
939 array->alloc = 0;
940 array->element_free_func = NULL;
942 g_atomic_ref_count_init (&array->ref_count);
944 if (reserved_size != 0)
945 g_ptr_array_maybe_expand (array, reserved_size);
947 return (GPtrArray*) array;
951 * g_ptr_array_new_with_free_func:
952 * @element_free_func: (nullable): A function to free elements with
953 * destroy @array or %NULL
955 * Creates a new #GPtrArray with a reference count of 1 and use
956 * @element_free_func for freeing each element when the array is destroyed
957 * either via g_ptr_array_unref(), when g_ptr_array_free() is called with
958 * @free_segment set to %TRUE or when removing elements.
960 * Returns: A new #GPtrArray
962 * Since: 2.22
964 GPtrArray*
965 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
967 GPtrArray *array;
969 array = g_ptr_array_new ();
970 g_ptr_array_set_free_func (array, element_free_func);
972 return array;
976 * g_ptr_array_new_full:
977 * @reserved_size: number of pointers preallocated
978 * @element_free_func: (nullable): A function to free elements with
979 * destroy @array or %NULL
981 * Creates a new #GPtrArray with @reserved_size pointers preallocated
982 * and a reference count of 1. This avoids frequent reallocation, if
983 * you are going to add many pointers to the array. Note however that
984 * the size of the array is still 0. It also set @element_free_func
985 * for freeing each element when the array is destroyed either via
986 * g_ptr_array_unref(), when g_ptr_array_free() is called with
987 * @free_segment set to %TRUE or when removing elements.
989 * Returns: A new #GPtrArray
991 * Since: 2.30
993 GPtrArray*
994 g_ptr_array_new_full (guint reserved_size,
995 GDestroyNotify element_free_func)
997 GPtrArray *array;
999 array = g_ptr_array_sized_new (reserved_size);
1000 g_ptr_array_set_free_func (array, element_free_func);
1002 return array;
1006 * g_ptr_array_set_free_func:
1007 * @array: A #GPtrArray
1008 * @element_free_func: (nullable): A function to free elements with
1009 * destroy @array or %NULL
1011 * Sets a function for freeing each element when @array is destroyed
1012 * either via g_ptr_array_unref(), when g_ptr_array_free() is called
1013 * with @free_segment set to %TRUE or when removing elements.
1015 * Since: 2.22
1017 void
1018 g_ptr_array_set_free_func (GPtrArray *array,
1019 GDestroyNotify element_free_func)
1021 GRealPtrArray *rarray = (GRealPtrArray *)array;
1023 g_return_if_fail (array);
1025 rarray->element_free_func = element_free_func;
1029 * g_ptr_array_ref:
1030 * @array: a #GPtrArray
1032 * Atomically increments the reference count of @array by one.
1033 * This function is thread-safe and may be called from any thread.
1035 * Returns: The passed in #GPtrArray
1037 * Since: 2.22
1039 GPtrArray*
1040 g_ptr_array_ref (GPtrArray *array)
1042 GRealPtrArray *rarray = (GRealPtrArray *)array;
1044 g_return_val_if_fail (array, NULL);
1046 g_atomic_ref_count_inc (&rarray->ref_count);
1048 return array;
1051 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
1054 * g_ptr_array_unref:
1055 * @array: A #GPtrArray
1057 * Atomically decrements the reference count of @array by one. If the
1058 * reference count drops to 0, the effect is the same as calling
1059 * g_ptr_array_free() with @free_segment set to %TRUE. This function
1060 * is thread-safe and may be called from any thread.
1062 * Since: 2.22
1064 void
1065 g_ptr_array_unref (GPtrArray *array)
1067 GRealPtrArray *rarray = (GRealPtrArray *)array;
1069 g_return_if_fail (array);
1071 if (g_atomic_ref_count_dec (&rarray->ref_count))
1072 ptr_array_free (array, FREE_SEGMENT);
1076 * g_ptr_array_free:
1077 * @array: a #GPtrArray
1078 * @free_seg: if %TRUE the actual pointer array is freed as well
1080 * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1081 * it frees the memory block holding the elements as well. Pass %FALSE
1082 * if you want to free the #GPtrArray wrapper but preserve the
1083 * underlying array for use elsewhere. If the reference count of @array
1084 * is greater than one, the #GPtrArray wrapper is preserved but the
1085 * size of @array will be set to zero.
1087 * If array contents point to dynamically-allocated memory, they should
1088 * be freed separately if @free_seg is %TRUE and no #GDestroyNotify
1089 * function has been set for @array.
1091 * This function is not thread-safe. If using a #GPtrArray from multiple
1092 * threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
1093 * functions.
1095 * Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1096 * The pointer array should be freed using g_free().
1098 gpointer*
1099 g_ptr_array_free (GPtrArray *array,
1100 gboolean free_segment)
1102 GRealPtrArray *rarray = (GRealPtrArray *)array;
1103 ArrayFreeFlags flags;
1105 g_return_val_if_fail (rarray, NULL);
1107 flags = (free_segment ? FREE_SEGMENT : 0);
1109 /* if others are holding a reference, preserve the wrapper but
1110 * do free/return the data
1112 if (!g_atomic_ref_count_dec (&rarray->ref_count))
1113 flags |= PRESERVE_WRAPPER;
1115 return ptr_array_free (array, flags);
1118 static gpointer *
1119 ptr_array_free (GPtrArray *array,
1120 ArrayFreeFlags flags)
1122 GRealPtrArray *rarray = (GRealPtrArray *)array;
1123 gpointer *segment;
1125 if (flags & FREE_SEGMENT)
1127 /* Data here is stolen and freed manually. It is an
1128 * error to attempt to access the array data (including
1129 * mutating the array bounds) during destruction).
1131 * https://bugzilla.gnome.org/show_bug.cgi?id=769064
1133 gpointer *stolen_pdata = g_steal_pointer (&rarray->pdata);
1134 if (rarray->element_free_func != NULL)
1136 gsize i;
1137 for (i = 0; i < rarray->len; ++i)
1138 rarray->element_free_func (stolen_pdata[i]);
1141 g_free (stolen_pdata);
1142 segment = NULL;
1144 else
1145 segment = rarray->pdata;
1147 if (flags & PRESERVE_WRAPPER)
1149 rarray->pdata = NULL;
1150 rarray->len = 0;
1151 rarray->alloc = 0;
1153 else
1155 g_slice_free1 (sizeof (GRealPtrArray), rarray);
1158 return segment;
1161 static void
1162 g_ptr_array_maybe_expand (GRealPtrArray *array,
1163 gint len)
1165 if ((array->len + len) > array->alloc)
1167 guint old_alloc = array->alloc;
1168 array->alloc = g_nearest_pow (array->len + len);
1169 array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1170 array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1171 if (G_UNLIKELY (g_mem_gc_friendly))
1172 for ( ; old_alloc < array->alloc; old_alloc++)
1173 array->pdata [old_alloc] = NULL;
1178 * g_ptr_array_set_size:
1179 * @array: a #GPtrArray
1180 * @length: the new length of the pointer array
1182 * Sets the size of the array. When making the array larger,
1183 * newly-added elements will be set to %NULL. When making it smaller,
1184 * if @array has a non-%NULL #GDestroyNotify function then it will be
1185 * called for the removed elements.
1187 void
1188 g_ptr_array_set_size (GPtrArray *array,
1189 gint length)
1191 GRealPtrArray *rarray = (GRealPtrArray *)array;
1193 g_return_if_fail (rarray);
1194 g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
1196 if (length > rarray->len)
1198 int i;
1199 g_ptr_array_maybe_expand (rarray, (length - rarray->len));
1200 /* This is not
1201 * memset (array->pdata + array->len, 0,
1202 * sizeof (gpointer) * (length - array->len));
1203 * to make it really portable. Remember (void*)NULL needn't be
1204 * bitwise zero. It of course is silly not to use memset (..,0,..).
1206 for (i = rarray->len; i < length; i++)
1207 rarray->pdata[i] = NULL;
1209 else if (length < rarray->len)
1210 g_ptr_array_remove_range (array, length, rarray->len - length);
1212 rarray->len = length;
1215 static gpointer
1216 ptr_array_remove_index (GPtrArray *array,
1217 guint index_,
1218 gboolean fast,
1219 gboolean free_element)
1221 GRealPtrArray *rarray = (GRealPtrArray *) array;
1222 gpointer result;
1224 g_return_val_if_fail (rarray, NULL);
1225 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
1227 g_return_val_if_fail (index_ < rarray->len, NULL);
1229 result = rarray->pdata[index_];
1231 if (rarray->element_free_func != NULL && free_element)
1232 rarray->element_free_func (rarray->pdata[index_]);
1234 if (index_ != rarray->len - 1 && !fast)
1235 memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
1236 sizeof (gpointer) * (rarray->len - index_ - 1));
1237 else if (index_ != rarray->len - 1)
1238 rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
1240 rarray->len -= 1;
1242 if (G_UNLIKELY (g_mem_gc_friendly))
1243 rarray->pdata[rarray->len] = NULL;
1245 return result;
1249 * g_ptr_array_remove_index:
1250 * @array: a #GPtrArray
1251 * @index_: the index of the pointer to remove
1253 * Removes the pointer at the given index from the pointer array.
1254 * The following elements are moved down one place. If @array has
1255 * a non-%NULL #GDestroyNotify function it is called for the removed
1256 * element. If so, the return value from this function will potentially point
1257 * to freed memory (depending on the #GDestroyNotify implementation).
1259 * Returns: (nullable): the pointer which was removed
1261 gpointer
1262 g_ptr_array_remove_index (GPtrArray *array,
1263 guint index_)
1265 return ptr_array_remove_index (array, index_, FALSE, TRUE);
1269 * g_ptr_array_remove_index_fast:
1270 * @array: a #GPtrArray
1271 * @index_: the index of the pointer to remove
1273 * Removes the pointer at the given index from the pointer array.
1274 * The last element in the array is used to fill in the space, so
1275 * this function does not preserve the order of the array. But it
1276 * is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
1277 * #GDestroyNotify function it is called for the removed element. If so, the
1278 * return value from this function will potentially point to freed memory
1279 * (depending on the #GDestroyNotify implementation).
1281 * Returns: (nullable): the pointer which was removed
1283 gpointer
1284 g_ptr_array_remove_index_fast (GPtrArray *array,
1285 guint index_)
1287 return ptr_array_remove_index (array, index_, TRUE, TRUE);
1291 * g_ptr_array_steal_index:
1292 * @array: a #GPtrArray
1293 * @index_: the index of the pointer to steal
1295 * Removes the pointer at the given index from the pointer array.
1296 * The following elements are moved down one place. The #GDestroyNotify for
1297 * @array is *not* called on the removed element; ownership is transferred to
1298 * the caller of this function.
1300 * Returns: (transfer full) (nullable): the pointer which was removed
1301 * Since: 2.58
1303 gpointer
1304 g_ptr_array_steal_index (GPtrArray *array,
1305 guint index_)
1307 return ptr_array_remove_index (array, index_, FALSE, FALSE);
1311 * g_ptr_array_steal_index_fast:
1312 * @array: a #GPtrArray
1313 * @index_: the index of the pointer to steal
1315 * Removes the pointer at the given index from the pointer array.
1316 * The last element in the array is used to fill in the space, so
1317 * this function does not preserve the order of the array. But it
1318 * is faster than g_ptr_array_steal_index(). The #GDestroyNotify for @array is
1319 * *not* called on the removed element; ownership is transferred to the caller
1320 * of this function.
1322 * Returns: (transfer full) (nullable): the pointer which was removed
1323 * Since: 2.58
1325 gpointer
1326 g_ptr_array_steal_index_fast (GPtrArray *array,
1327 guint index_)
1329 return ptr_array_remove_index (array, index_, TRUE, FALSE);
1333 * g_ptr_array_remove_range:
1334 * @array: a @GPtrArray
1335 * @index_: the index of the first pointer to remove
1336 * @length: the number of pointers to remove
1338 * Removes the given number of pointers starting at the given index
1339 * from a #GPtrArray. The following elements are moved to close the
1340 * gap. If @array has a non-%NULL #GDestroyNotify function it is
1341 * called for the removed elements.
1343 * Returns: the @array
1345 * Since: 2.4
1347 GPtrArray*
1348 g_ptr_array_remove_range (GPtrArray *array,
1349 guint index_,
1350 guint length)
1352 GRealPtrArray *rarray = (GRealPtrArray *)array;
1353 guint n;
1355 g_return_val_if_fail (rarray != NULL, NULL);
1356 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
1357 g_return_val_if_fail (index_ <= rarray->len, NULL);
1358 g_return_val_if_fail (index_ + length <= rarray->len, NULL);
1360 if (rarray->element_free_func != NULL)
1362 for (n = index_; n < index_ + length; n++)
1363 rarray->element_free_func (rarray->pdata[n]);
1366 if (index_ + length != rarray->len)
1368 memmove (&rarray->pdata[index_],
1369 &rarray->pdata[index_ + length],
1370 (rarray->len - (index_ + length)) * sizeof (gpointer));
1373 rarray->len -= length;
1374 if (G_UNLIKELY (g_mem_gc_friendly))
1376 guint i;
1377 for (i = 0; i < length; i++)
1378 rarray->pdata[rarray->len + i] = NULL;
1381 return array;
1385 * g_ptr_array_remove:
1386 * @array: a #GPtrArray
1387 * @data: the pointer to remove
1389 * Removes the first occurrence of the given pointer from the pointer
1390 * array. The following elements are moved down one place. If @array
1391 * has a non-%NULL #GDestroyNotify function it is called for the
1392 * removed element.
1394 * It returns %TRUE if the pointer was removed, or %FALSE if the
1395 * pointer was not found.
1397 * Returns: %TRUE if the pointer is removed, %FALSE if the pointer
1398 * is not found in the array
1400 gboolean
1401 g_ptr_array_remove (GPtrArray *array,
1402 gpointer data)
1404 guint i;
1406 g_return_val_if_fail (array, FALSE);
1407 g_return_val_if_fail (array->len == 0 || (array->len != 0 && array->pdata != NULL), FALSE);
1409 for (i = 0; i < array->len; i += 1)
1411 if (array->pdata[i] == data)
1413 g_ptr_array_remove_index (array, i);
1414 return TRUE;
1418 return FALSE;
1422 * g_ptr_array_remove_fast:
1423 * @array: a #GPtrArray
1424 * @data: the pointer to remove
1426 * Removes the first occurrence of the given pointer from the pointer
1427 * array. The last element in the array is used to fill in the space,
1428 * so this function does not preserve the order of the array. But it
1429 * is faster than g_ptr_array_remove(). If @array has a non-%NULL
1430 * #GDestroyNotify function it is called for the removed element.
1432 * It returns %TRUE if the pointer was removed, or %FALSE if the
1433 * pointer was not found.
1435 * Returns: %TRUE if the pointer was found in the array
1437 gboolean
1438 g_ptr_array_remove_fast (GPtrArray *array,
1439 gpointer data)
1441 GRealPtrArray *rarray = (GRealPtrArray *)array;
1442 guint i;
1444 g_return_val_if_fail (rarray, FALSE);
1445 g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), FALSE);
1447 for (i = 0; i < rarray->len; i += 1)
1449 if (rarray->pdata[i] == data)
1451 g_ptr_array_remove_index_fast (array, i);
1452 return TRUE;
1456 return FALSE;
1460 * g_ptr_array_add:
1461 * @array: a #GPtrArray
1462 * @data: the pointer to add
1464 * Adds a pointer to the end of the pointer array. The array will grow
1465 * in size automatically if necessary.
1467 void
1468 g_ptr_array_add (GPtrArray *array,
1469 gpointer data)
1471 GRealPtrArray *rarray = (GRealPtrArray *)array;
1473 g_return_if_fail (rarray);
1474 g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
1476 g_ptr_array_maybe_expand (rarray, 1);
1478 rarray->pdata[rarray->len++] = data;
1482 * g_ptr_array_insert:
1483 * @array: a #GPtrArray
1484 * @index_: the index to place the new element at, or -1 to append
1485 * @data: the pointer to add.
1487 * Inserts an element into the pointer array at the given index. The
1488 * array will grow in size automatically if necessary.
1490 * Since: 2.40
1492 void
1493 g_ptr_array_insert (GPtrArray *array,
1494 gint index_,
1495 gpointer data)
1497 GRealPtrArray *rarray = (GRealPtrArray *)array;
1499 g_return_if_fail (rarray);
1500 g_return_if_fail (index_ >= -1);
1501 g_return_if_fail (index_ <= (gint)rarray->len);
1503 g_ptr_array_maybe_expand (rarray, 1);
1505 if (index_ < 0)
1506 index_ = rarray->len;
1508 if (index_ < rarray->len)
1509 memmove (&(rarray->pdata[index_ + 1]),
1510 &(rarray->pdata[index_]),
1511 (rarray->len - index_) * sizeof (gpointer));
1513 rarray->len++;
1514 rarray->pdata[index_] = data;
1518 * g_ptr_array_sort:
1519 * @array: a #GPtrArray
1520 * @compare_func: comparison function
1522 * Sorts the array, using @compare_func which should be a qsort()-style
1523 * comparison function (returns less than zero for first arg is less
1524 * than second arg, zero for equal, greater than zero if irst arg is
1525 * greater than second arg).
1527 * Note that the comparison function for g_ptr_array_sort() doesn't
1528 * take the pointers from the array as arguments, it takes pointers to
1529 * the pointers in the array.
1531 * This is guaranteed to be a stable sort since version 2.32.
1533 void
1534 g_ptr_array_sort (GPtrArray *array,
1535 GCompareFunc compare_func)
1537 g_return_if_fail (array != NULL);
1539 /* Don't use qsort as we want a guaranteed stable sort */
1540 g_qsort_with_data (array->pdata,
1541 array->len,
1542 sizeof (gpointer),
1543 (GCompareDataFunc)compare_func,
1544 NULL);
1548 * g_ptr_array_sort_with_data:
1549 * @array: a #GPtrArray
1550 * @compare_func: comparison function
1551 * @user_data: data to pass to @compare_func
1553 * Like g_ptr_array_sort(), but the comparison function has an extra
1554 * user data argument.
1556 * Note that the comparison function for g_ptr_array_sort_with_data()
1557 * doesn't take the pointers from the array as arguments, it takes
1558 * pointers to the pointers in the array.
1560 * This is guaranteed to be a stable sort since version 2.32.
1562 void
1563 g_ptr_array_sort_with_data (GPtrArray *array,
1564 GCompareDataFunc compare_func,
1565 gpointer user_data)
1567 g_return_if_fail (array != NULL);
1569 g_qsort_with_data (array->pdata,
1570 array->len,
1571 sizeof (gpointer),
1572 compare_func,
1573 user_data);
1577 * g_ptr_array_foreach:
1578 * @array: a #GPtrArray
1579 * @func: the function to call for each array element
1580 * @user_data: user data to pass to the function
1582 * Calls a function for each element of a #GPtrArray. @func must not
1583 * add elements to or remove elements from the array.
1585 * Since: 2.4
1587 void
1588 g_ptr_array_foreach (GPtrArray *array,
1589 GFunc func,
1590 gpointer user_data)
1592 guint i;
1594 g_return_if_fail (array);
1596 for (i = 0; i < array->len; i++)
1597 (*func) (array->pdata[i], user_data);
1601 * g_ptr_array_find: (skip)
1602 * @haystack: pointer array to be searched
1603 * @needle: pointer to look for
1604 * @index_: (optional) (out caller-allocates): return location for the index of
1605 * the element, if found
1607 * Checks whether @needle exists in @haystack. If the element is found, %TRUE is
1608 * returned and the element’s index is returned in @index_ (if non-%NULL).
1609 * Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
1610 * multiple times in @haystack, the index of the first instance is returned.
1612 * This does pointer comparisons only. If you want to use more complex equality
1613 * checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
1615 * Returns: %TRUE if @needle is one of the elements of @haystack
1616 * Since: 2.54
1618 gboolean
1619 g_ptr_array_find (GPtrArray *haystack,
1620 gconstpointer needle,
1621 guint *index_)
1623 return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
1627 * g_ptr_array_find_with_equal_func: (skip)
1628 * @haystack: pointer array to be searched
1629 * @needle: pointer to look for
1630 * @equal_func: (nullable): the function to call for each element, which should
1631 * return %TRUE when the desired element is found; or %NULL to use pointer
1632 * equality
1633 * @index_: (optional) (out caller-allocates): return location for the index of
1634 * the element, if found
1636 * Checks whether @needle exists in @haystack, using the given @equal_func.
1637 * If the element is found, %TRUE is returned and the element’s index is
1638 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
1639 * is undefined. If @needle exists multiple times in @haystack, the index of
1640 * the first instance is returned.
1642 * @equal_func is called with the element from the array as its first parameter,
1643 * and @needle as its second parameter. If @equal_func is %NULL, pointer
1644 * equality is used.
1646 * Returns: %TRUE if @needle is one of the elements of @haystack
1647 * Since: 2.54
1649 gboolean
1650 g_ptr_array_find_with_equal_func (GPtrArray *haystack,
1651 gconstpointer needle,
1652 GEqualFunc equal_func,
1653 guint *index_)
1655 guint i;
1657 g_return_val_if_fail (haystack != NULL, FALSE);
1659 if (equal_func == NULL)
1660 equal_func = g_direct_equal;
1662 for (i = 0; i < haystack->len; i++)
1664 if (equal_func (g_ptr_array_index (haystack, i), needle))
1666 if (index_ != NULL)
1667 *index_ = i;
1668 return TRUE;
1672 return FALSE;
1676 * SECTION:arrays_byte
1677 * @title: Byte Arrays
1678 * @short_description: arrays of bytes
1680 * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1681 * of bytes which grow automatically as elements are added.
1683 * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1684 * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1686 * To set the size of a #GByteArray, use g_byte_array_set_size().
1688 * To free a #GByteArray, use g_byte_array_free().
1690 * An example for using a #GByteArray:
1691 * |[<!-- language="C" -->
1692 * GByteArray *gbarray;
1693 * gint i;
1695 * gbarray = g_byte_array_new ();
1696 * for (i = 0; i < 10000; i++)
1697 * g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1699 * for (i = 0; i < 10000; i++)
1701 * g_assert (gbarray->data[4*i] == 'a');
1702 * g_assert (gbarray->data[4*i+1] == 'b');
1703 * g_assert (gbarray->data[4*i+2] == 'c');
1704 * g_assert (gbarray->data[4*i+3] == 'd');
1707 * g_byte_array_free (gbarray, TRUE);
1708 * ]|
1710 * See #GBytes if you are interested in an immutable object representing a
1711 * sequence of bytes.
1715 * GByteArray:
1716 * @data: a pointer to the element data. The data may be moved as
1717 * elements are added to the #GByteArray
1718 * @len: the number of elements in the #GByteArray
1720 * Contains the public fields of a GByteArray.
1724 * g_byte_array_new:
1726 * Creates a new #GByteArray with a reference count of 1.
1728 * Returns: (transfer full): the new #GByteArray
1730 GByteArray*
1731 g_byte_array_new (void)
1733 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
1737 * g_byte_array_new_take:
1738 * @data: (transfer full) (array length=len): byte data for the array
1739 * @len: length of @data
1741 * Create byte array containing the data. The data will be owned by the array
1742 * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1744 * Since: 2.32
1746 * Returns: (transfer full): a new #GByteArray
1748 GByteArray*
1749 g_byte_array_new_take (guint8 *data,
1750 gsize len)
1752 GByteArray *array;
1753 GRealArray *real;
1755 array = g_byte_array_new ();
1756 real = (GRealArray *)array;
1757 g_assert (real->data == NULL);
1758 g_assert (real->len == 0);
1760 real->data = data;
1761 real->len = len;
1762 real->alloc = len;
1764 return array;
1768 * g_byte_array_sized_new:
1769 * @reserved_size: number of bytes preallocated
1771 * Creates a new #GByteArray with @reserved_size bytes preallocated.
1772 * This avoids frequent reallocation, if you are going to add many
1773 * bytes to the array. Note however that the size of the array is still
1774 * 0.
1776 * Returns: the new #GByteArray
1778 GByteArray*
1779 g_byte_array_sized_new (guint reserved_size)
1781 return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1785 * g_byte_array_free:
1786 * @array: a #GByteArray
1787 * @free_segment: if %TRUE the actual byte data is freed as well
1789 * Frees the memory allocated by the #GByteArray. If @free_segment is
1790 * %TRUE it frees the actual byte data. If the reference count of
1791 * @array is greater than one, the #GByteArray wrapper is preserved but
1792 * the size of @array will be set to zero.
1794 * Returns: the element data if @free_segment is %FALSE, otherwise
1795 * %NULL. The element data should be freed using g_free().
1797 guint8*
1798 g_byte_array_free (GByteArray *array,
1799 gboolean free_segment)
1801 return (guint8 *)g_array_free ((GArray *)array, free_segment);
1805 * g_byte_array_free_to_bytes:
1806 * @array: (transfer full): a #GByteArray
1808 * Transfers the data from the #GByteArray into a new immutable #GBytes.
1810 * The #GByteArray is freed unless the reference count of @array is greater
1811 * than one, the #GByteArray wrapper is preserved but the size of @array
1812 * will be set to zero.
1814 * This is identical to using g_bytes_new_take() and g_byte_array_free()
1815 * together.
1817 * Since: 2.32
1819 * Returns: (transfer full): a new immutable #GBytes representing same
1820 * byte data that was in the array
1822 GBytes*
1823 g_byte_array_free_to_bytes (GByteArray *array)
1825 gsize length;
1827 g_return_val_if_fail (array != NULL, NULL);
1829 length = array->len;
1830 return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1834 * g_byte_array_ref:
1835 * @array: A #GByteArray
1837 * Atomically increments the reference count of @array by one.
1838 * This function is thread-safe and may be called from any thread.
1840 * Returns: The passed in #GByteArray
1842 * Since: 2.22
1844 GByteArray*
1845 g_byte_array_ref (GByteArray *array)
1847 return (GByteArray *)g_array_ref ((GArray *)array);
1851 * g_byte_array_unref:
1852 * @array: A #GByteArray
1854 * Atomically decrements the reference count of @array by one. If the
1855 * reference count drops to 0, all memory allocated by the array is
1856 * released. This function is thread-safe and may be called from any
1857 * thread.
1859 * Since: 2.22
1861 void
1862 g_byte_array_unref (GByteArray *array)
1864 g_array_unref ((GArray *)array);
1868 * g_byte_array_append:
1869 * @array: a #GByteArray
1870 * @data: the byte data to be added
1871 * @len: the number of bytes to add
1873 * Adds the given bytes to the end of the #GByteArray.
1874 * The array will grow in size automatically if necessary.
1876 * Returns: the #GByteArray
1878 GByteArray*
1879 g_byte_array_append (GByteArray *array,
1880 const guint8 *data,
1881 guint len)
1883 g_array_append_vals ((GArray *)array, (guint8 *)data, len);
1885 return array;
1889 * g_byte_array_prepend:
1890 * @array: a #GByteArray
1891 * @data: the byte data to be added
1892 * @len: the number of bytes to add
1894 * Adds the given data to the start of the #GByteArray.
1895 * The array will grow in size automatically if necessary.
1897 * Returns: the #GByteArray
1899 GByteArray*
1900 g_byte_array_prepend (GByteArray *array,
1901 const guint8 *data,
1902 guint len)
1904 g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
1906 return array;
1910 * g_byte_array_set_size:
1911 * @array: a #GByteArray
1912 * @length: the new size of the #GByteArray
1914 * Sets the size of the #GByteArray, expanding it if necessary.
1916 * Returns: the #GByteArray
1918 GByteArray*
1919 g_byte_array_set_size (GByteArray *array,
1920 guint length)
1922 g_array_set_size ((GArray *)array, length);
1924 return array;
1928 * g_byte_array_remove_index:
1929 * @array: a #GByteArray
1930 * @index_: the index of the byte to remove
1932 * Removes the byte at the given index from a #GByteArray.
1933 * The following bytes are moved down one place.
1935 * Returns: the #GByteArray
1937 GByteArray*
1938 g_byte_array_remove_index (GByteArray *array,
1939 guint index_)
1941 g_array_remove_index ((GArray *)array, index_);
1943 return array;
1947 * g_byte_array_remove_index_fast:
1948 * @array: a #GByteArray
1949 * @index_: the index of the byte to remove
1951 * Removes the byte at the given index from a #GByteArray. The last
1952 * element in the array is used to fill in the space, so this function
1953 * does not preserve the order of the #GByteArray. But it is faster
1954 * than g_byte_array_remove_index().
1956 * Returns: the #GByteArray
1958 GByteArray*
1959 g_byte_array_remove_index_fast (GByteArray *array,
1960 guint index_)
1962 g_array_remove_index_fast ((GArray *)array, index_);
1964 return array;
1968 * g_byte_array_remove_range:
1969 * @array: a @GByteArray
1970 * @index_: the index of the first byte to remove
1971 * @length: the number of bytes to remove
1973 * Removes the given number of bytes starting at the given index from a
1974 * #GByteArray. The following elements are moved to close the gap.
1976 * Returns: the #GByteArray
1978 * Since: 2.4
1980 GByteArray*
1981 g_byte_array_remove_range (GByteArray *array,
1982 guint index_,
1983 guint length)
1985 g_return_val_if_fail (array, NULL);
1986 g_return_val_if_fail (index_ <= array->len, NULL);
1987 g_return_val_if_fail (index_ + length <= array->len, NULL);
1989 return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
1993 * g_byte_array_sort:
1994 * @array: a #GByteArray
1995 * @compare_func: comparison function
1997 * Sorts a byte array, using @compare_func which should be a
1998 * qsort()-style comparison function (returns less than zero for first
1999 * arg is less than second arg, zero for equal, greater than zero if
2000 * first arg is greater than second arg).
2002 * If two array elements compare equal, their order in the sorted array
2003 * is undefined. If you want equal elements to keep their order (i.e.
2004 * you want a stable sort) you can write a comparison function that,
2005 * if two elements would otherwise compare equal, compares them by
2006 * their addresses.
2008 void
2009 g_byte_array_sort (GByteArray *array,
2010 GCompareFunc compare_func)
2012 g_array_sort ((GArray *)array, compare_func);
2016 * g_byte_array_sort_with_data:
2017 * @array: a #GByteArray
2018 * @compare_func: comparison function
2019 * @user_data: data to pass to @compare_func
2021 * Like g_byte_array_sort(), but the comparison function takes an extra
2022 * user data argument.
2024 void
2025 g_byte_array_sort_with_data (GByteArray *array,
2026 GCompareDataFunc compare_func,
2027 gpointer user_data)
2029 g_array_sort_with_data ((GArray *)array, compare_func, user_data);