1998-06-10 23:21:14 +00:00
|
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
|
*
|
2022-05-18 09:15:38 +01:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
1998-06-10 23:21:14 +00:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 11:02:02 +00:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-06-10 23:21:14 +00:00
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
1998-06-10 23:21:14 +00:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 11:02:02 +00:00
|
|
|
|
* Lesser General Public License for more details.
|
1998-06-10 23:21:14 +00:00
|
|
|
|
*
|
2000-07-26 11:02:02 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
1998-06-10 23:21:14 +00:00
|
|
|
|
*/
|
1998-12-15 05:28:02 +00:00
|
|
|
|
|
1999-02-24 06:14:27 +00:00
|
|
|
|
/*
|
2000-07-26 11:02:02 +00:00
|
|
|
|
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
1999-02-24 06:14:27 +00:00
|
|
|
|
* file for a list of people on the GLib Team. See the ChangeLog
|
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
|
* GLib at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
|
*/
|
|
|
|
|
|
1998-12-15 05:28:02 +00:00
|
|
|
|
/*
|
|
|
|
|
* MT safe
|
|
|
|
|
*/
|
|
|
|
|
|
2002-12-04 01:27:44 +00:00
|
|
|
|
#include "config.h"
|
Ok, I'm a moron. When I originally implemented ENABLE_GC_FRIENDLY, I
2000-12-19 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* gslist.c, glist.c: Ok, I'm a moron. When I originally
implemented ENABLE_GC_FRIENDLY, I forgot to include config.h into
the affected files. Now that Alex did that for those two,
inevitable typos surfaced, which are now fixed.
* garray.c, ghash.c, gqueue.c, gtree.c: Include config.h as well,
as ENABLE_GC_FRIENDLY should be known.
2000-12-19 15:40:30 +00:00
|
|
|
|
|
1998-06-10 23:21:14 +00:00
|
|
|
|
#include <string.h>
|
2000-11-20 23:59:32 +00:00
|
|
|
|
#include <stdlib.h>
|
2005-03-14 04:26:57 +00:00
|
|
|
|
|
|
|
|
|
#include "garray.h"
|
|
|
|
|
|
2022-12-14 00:58:13 +01:00
|
|
|
|
#include "galloca.h"
|
2011-11-11 17:08:26 +01:00
|
|
|
|
#include "gbytes.h"
|
2013-04-15 15:04:34 +02:00
|
|
|
|
#include "ghash.h"
|
2011-09-18 18:59:20 -04:00
|
|
|
|
#include "gslice.h"
|
2005-03-14 04:26:57 +00:00
|
|
|
|
#include "gmem.h"
|
2011-11-11 17:08:26 +01:00
|
|
|
|
#include "gtestutils.h"
|
2005-03-14 04:26:57 +00:00
|
|
|
|
#include "gthread.h"
|
|
|
|
|
#include "gmessages.h"
|
|
|
|
|
#include "gqsort.h"
|
2018-01-17 17:01:23 +00:00
|
|
|
|
#include "grefcount.h"
|
2021-11-25 14:05:42 +02:00
|
|
|
|
#include "gutilsprivate.h"
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
|
|
|
|
#define MIN_ARRAY_SIZE 16
|
|
|
|
|
|
|
|
|
|
typedef struct _GRealArray GRealArray;
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* GArray:
|
|
|
|
|
* @data: a pointer to the element data. The data may be moved as
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* elements are added to the #GArray.
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* @len: the number of elements in the #GArray not including the
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* possible terminating zero element.
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Contains the public fields of a GArray.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
1998-06-10 23:21:14 +00:00
|
|
|
|
struct _GRealArray
|
|
|
|
|
{
|
|
|
|
|
guint8 *data;
|
|
|
|
|
guint len;
|
2021-10-19 15:38:13 +00:00
|
|
|
|
guint elt_capacity;
|
1998-09-02 07:44:02 +00:00
|
|
|
|
guint elt_size;
|
|
|
|
|
guint zero_terminated : 1;
|
|
|
|
|
guint clear : 1;
|
2018-01-17 17:01:23 +00:00
|
|
|
|
gatomicrefcount ref_count;
|
2012-01-04 07:41:47 +00:00
|
|
|
|
GDestroyNotify clear_func;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
};
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_index:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @a: a #GArray
|
|
|
|
|
* @t: the type of the elements
|
|
|
|
|
* @i: the index of the element to return
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Returns the element of a #GArray at the given index. The return
|
2020-03-03 14:32:13 +00:00
|
|
|
|
* value is cast to the given type. This is the main way to read or write an
|
|
|
|
|
* element in a #GArray.
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2020-03-03 14:32:13 +00:00
|
|
|
|
* Writing an element is typically done by reference, as in the following
|
|
|
|
|
* example. This example gets a pointer to an element in a #GArray, and then
|
|
|
|
|
* writes to a field in it:
|
2014-02-01 15:11:49 -05:00
|
|
|
|
* |[<!-- language="C" -->
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* EDayViewEvent *event;
|
2014-02-14 21:33:36 -05:00
|
|
|
|
* // This gets a pointer to the 4th element in the array of
|
|
|
|
|
* // EDayViewEvent structs.
|
2014-02-01 11:57:13 -05:00
|
|
|
|
* event = &g_array_index (events, EDayViewEvent, 3);
|
2020-03-03 14:32:13 +00:00
|
|
|
|
* event->start_time = g_get_current_time ();
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
|
|
|
|
* This example reads from and writes to an array of integers:
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* g_autoptr(GArray) int_array = g_array_new (FALSE, FALSE, sizeof (guint));
|
|
|
|
|
* for (guint i = 0; i < 10; i++)
|
|
|
|
|
* g_array_append_val (int_array, i);
|
|
|
|
|
*
|
|
|
|
|
* guint *my_int = &g_array_index (int_array, guint, 1);
|
|
|
|
|
* g_print ("Int at index 1 is %u; decrementing it\n", *my_int);
|
|
|
|
|
* *my_int = *my_int - 1;
|
2014-01-31 21:56:33 -05:00
|
|
|
|
* ]|
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the element of the #GArray at the index given by @i
|
|
|
|
|
*/
|
2010-01-31 14:30:08 -05:00
|
|
|
|
|
2021-10-19 15:38:13 +00:00
|
|
|
|
#define g_array_elt_len(array,i) ((gsize)(array)->elt_size * (i))
|
2000-03-24 15:36:03 +00:00
|
|
|
|
#define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
|
2014-01-31 14:56:10 -05:00
|
|
|
|
#define g_array_elt_zero(array, pos, len) \
|
2000-03-24 15:36:03 +00:00
|
|
|
|
(memset (g_array_elt_pos ((array), pos), 0, g_array_elt_len ((array), len)))
|
2014-01-31 14:56:10 -05:00
|
|
|
|
#define g_array_zero_terminate(array) G_STMT_START{ \
|
|
|
|
|
if ((array)->zero_terminated) \
|
|
|
|
|
g_array_elt_zero ((array), (array)->len, 1); \
|
2000-03-24 15:36:03 +00:00
|
|
|
|
}G_STMT_END
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2010-01-13 14:47:58 +01:00
|
|
|
|
static void g_array_maybe_expand (GRealArray *array,
|
2018-07-10 14:15:42 +02:00
|
|
|
|
guint len);
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_new:
|
|
|
|
|
* @zero_terminated: %TRUE if the array should have an extra element at
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* the end which is set to 0
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* @clear_: %TRUE if #GArray elements should be automatically cleared
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* to 0 when they are allocated
|
|
|
|
|
* @element_size: the size of each element in bytes
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GArray with a reference count of 1.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the new #GArray
|
|
|
|
|
*/
|
1998-06-10 23:21:14 +00:00
|
|
|
|
GArray*
|
1998-09-02 07:44:02 +00:00
|
|
|
|
g_array_new (gboolean zero_terminated,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gboolean clear,
|
|
|
|
|
guint elt_size)
|
2000-04-17 10:59:46 +00:00
|
|
|
|
{
|
2012-01-14 01:13:42 +01:00
|
|
|
|
g_return_val_if_fail (elt_size > 0, NULL);
|
2021-10-25 17:11:59 +02:00
|
|
|
|
#if (UINT_WIDTH / 8) >= GLIB_SIZEOF_SIZE_T
|
2021-10-19 15:38:13 +00:00
|
|
|
|
g_return_val_if_fail (elt_size <= G_MAXSIZE / 2 - 1, NULL);
|
2021-10-25 17:11:59 +02:00
|
|
|
|
#endif
|
2012-01-14 01:13:42 +01:00
|
|
|
|
|
|
|
|
|
return g_array_sized_new (zero_terminated, clear, elt_size, 0);
|
2000-04-17 10:59:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-14 00:58:13 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_array_new_take: (skip)
|
|
|
|
|
* @data: (array length=len) (transfer full) (nullable): an array of
|
|
|
|
|
* elements of @element_size, or %NULL for an empty array
|
|
|
|
|
* @len: the number of elements in @data
|
|
|
|
|
* @clear: %TRUE if #GArray elements should be automatically cleared
|
|
|
|
|
* to 0 when they are allocated
|
|
|
|
|
* @element_size: the size of each element in bytes
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GArray with @data as array data, @len as length and a
|
|
|
|
|
* reference count of 1.
|
|
|
|
|
*
|
|
|
|
|
* This avoids having to copy the data manually, when it can just be
|
2023-05-08 01:24:25 +02:00
|
|
|
|
* inherited.
|
|
|
|
|
* After this call, @data belongs to the #GArray and may no longer be
|
|
|
|
|
* modified by the caller. The memory of @data has to be dynamically
|
|
|
|
|
* allocated and will eventually be freed with g_free().
|
2022-12-14 00:58:13 +01:00
|
|
|
|
*
|
|
|
|
|
* In case the elements need to be cleared when the array is freed, use
|
|
|
|
|
* g_array_set_clear_func() to set a #GDestroyNotify function to perform
|
|
|
|
|
* such task.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if @len or @element_size are greater than %G_MAXUINT.
|
|
|
|
|
* #GArray stores the length of its data in #guint, which may be shorter
|
|
|
|
|
* than #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GArray *
|
|
|
|
|
g_array_new_take (gpointer data,
|
|
|
|
|
gsize len,
|
|
|
|
|
gboolean clear,
|
|
|
|
|
gsize element_size)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray;
|
|
|
|
|
GArray *array;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (data != NULL || len == 0, NULL);
|
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
|
|
|
|
g_return_val_if_fail (element_size <= G_MAXUINT, NULL);
|
|
|
|
|
|
|
|
|
|
array = g_array_sized_new (FALSE, clear, element_size, 0);
|
|
|
|
|
rarray = (GRealArray *) array;
|
|
|
|
|
rarray->data = (guint8 *) g_steal_pointer (&data);
|
|
|
|
|
rarray->len = len;
|
|
|
|
|
rarray->elt_capacity = len;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_array_new_take_zero_terminated: (skip)
|
|
|
|
|
* @data: (array zero-terminated=1): an array of elements of @element_size
|
|
|
|
|
* @clear: %TRUE if #GArray elements should be automatically cleared
|
|
|
|
|
* to 0 when they are allocated
|
|
|
|
|
* @element_size: the size of each element in bytes
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GArray with @data as array data, computing the length of it
|
|
|
|
|
* and setting the reference count to 1.
|
|
|
|
|
*
|
|
|
|
|
* This avoids having to copy the data manually, when it can just be
|
2023-05-08 01:24:25 +02:00
|
|
|
|
* inherited.
|
|
|
|
|
* After this call, @data belongs to the #GArray and may no longer be
|
|
|
|
|
* modified by the caller. The memory of @data has to be dynamically
|
|
|
|
|
* allocated and will eventually be freed with g_free().
|
2022-12-14 00:58:13 +01:00
|
|
|
|
*
|
|
|
|
|
* The length is calculated by iterating through @data until the first %NULL
|
|
|
|
|
* element is found.
|
|
|
|
|
*
|
|
|
|
|
* In case the elements need to be cleared when the array is freed, use
|
|
|
|
|
* g_array_set_clear_func() to set a #GDestroyNotify function to perform
|
|
|
|
|
* such task.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if @data length or @element_size are greater than %G_MAXUINT.
|
|
|
|
|
* #GArray stores the length of its data in #guint, which may be shorter
|
|
|
|
|
* than #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GArray *
|
|
|
|
|
g_array_new_take_zero_terminated (gpointer data,
|
|
|
|
|
gboolean clear,
|
|
|
|
|
gsize element_size)
|
|
|
|
|
{
|
|
|
|
|
GArray *array;
|
|
|
|
|
gsize len = 0;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (element_size <= G_MAXUINT, NULL);
|
|
|
|
|
|
|
|
|
|
if (data != NULL)
|
|
|
|
|
{
|
|
|
|
|
guint8 *array_data = data;
|
|
|
|
|
|
|
|
|
|
for (gsize i = 0; ; ++i)
|
|
|
|
|
{
|
|
|
|
|
const guint8 *element_start = array_data + (i * element_size);
|
|
|
|
|
|
|
|
|
|
if (*element_start == 0 &&
|
|
|
|
|
memcmp (element_start, element_start + 1, element_size - 1) == 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
len += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
|
|
|
|
|
|
|
|
|
array = g_array_new_take (data, len, clear, element_size);
|
|
|
|
|
((GRealArray *)array)->zero_terminated = TRUE;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 19:22:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_array_steal:
|
|
|
|
|
* @array: a #GArray.
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @len: (optional) (out): pointer to retrieve the number of
|
2019-07-30 19:22:05 +02:00
|
|
|
|
* elements of the original array
|
|
|
|
|
*
|
|
|
|
|
* Frees the data in the array and resets the size to zero, while
|
|
|
|
|
* the underlying array is preserved for use elsewhere and returned
|
|
|
|
|
* to the caller.
|
|
|
|
|
*
|
|
|
|
|
* If the array was created with the @zero_terminate property
|
|
|
|
|
* set to %TRUE, the returned data is zero terminated too.
|
|
|
|
|
*
|
|
|
|
|
* If array elements contain dynamically-allocated memory,
|
|
|
|
|
* the array elements should also be freed by the caller.
|
|
|
|
|
*
|
|
|
|
|
* A short example of use:
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* ...
|
|
|
|
|
* gpointer data;
|
|
|
|
|
* gsize data_len;
|
|
|
|
|
* data = g_array_steal (some_array, &data_len);
|
|
|
|
|
* ...
|
|
|
|
|
* ]|
|
|
|
|
|
|
|
|
|
|
* Returns: (transfer full): the element data, which should be
|
|
|
|
|
* freed using g_free().
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.64
|
|
|
|
|
*/
|
|
|
|
|
gpointer
|
|
|
|
|
g_array_steal (GArray *array,
|
|
|
|
|
gsize *len)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray;
|
|
|
|
|
gpointer segment;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
rarray = (GRealArray *) array;
|
|
|
|
|
segment = (gpointer) rarray->data;
|
|
|
|
|
|
|
|
|
|
if (len != NULL)
|
|
|
|
|
*len = rarray->len;
|
|
|
|
|
|
|
|
|
|
rarray->data = NULL;
|
|
|
|
|
rarray->len = 0;
|
2021-10-19 15:38:13 +00:00
|
|
|
|
rarray->elt_capacity = 0;
|
2019-07-30 19:22:05 +02:00
|
|
|
|
return segment;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_sized_new:
|
|
|
|
|
* @zero_terminated: %TRUE if the array should have an extra element at
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* the end with all bits cleared
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* @clear_: %TRUE if all bits in the array should be cleared to 0 on
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* allocation
|
|
|
|
|
* @element_size: size of each element in the array
|
|
|
|
|
* @reserved_size: number of elements preallocated
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GArray with @reserved_size elements preallocated and
|
|
|
|
|
* a reference count of 1. This avoids frequent reallocation, if you
|
|
|
|
|
* are going to add many elements to the array. Note however that the
|
|
|
|
|
* size of the array is still 0.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the new #GArray
|
|
|
|
|
*/
|
|
|
|
|
GArray*
|
|
|
|
|
g_array_sized_new (gboolean zero_terminated,
|
|
|
|
|
gboolean clear,
|
|
|
|
|
guint elt_size,
|
|
|
|
|
guint reserved_size)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
2012-01-14 01:13:42 +01:00
|
|
|
|
GRealArray *array;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (elt_size > 0, NULL);
|
2021-10-25 17:11:59 +02:00
|
|
|
|
#if (UINT_WIDTH / 8) >= GLIB_SIZEOF_SIZE_T
|
|
|
|
|
g_return_val_if_fail (elt_size <= G_MAXSIZE / 2 - 1, NULL);
|
|
|
|
|
#endif
|
2012-01-14 01:13:42 +01:00
|
|
|
|
|
|
|
|
|
array = g_slice_new (GRealArray);
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
array->data = NULL;
|
|
|
|
|
array->len = 0;
|
2021-10-19 15:38:13 +00:00
|
|
|
|
array->elt_capacity = 0;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
array->zero_terminated = (zero_terminated ? 1 : 0);
|
1998-09-02 07:44:02 +00:00
|
|
|
|
array->clear = (clear ? 1 : 0);
|
|
|
|
|
array->elt_size = elt_size;
|
2012-01-25 12:03:14 +01:00
|
|
|
|
array->clear_func = NULL;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2018-01-17 17:01:23 +00:00
|
|
|
|
g_atomic_ref_count_init (&array->ref_count);
|
|
|
|
|
|
2000-04-17 10:59:46 +00:00
|
|
|
|
if (array->zero_terminated || reserved_size != 0)
|
2000-03-24 15:36:03 +00:00
|
|
|
|
{
|
2000-04-17 10:59:46 +00:00
|
|
|
|
g_array_maybe_expand (array, reserved_size);
|
2022-11-29 12:16:11 +00:00
|
|
|
|
g_assert (array->data != NULL);
|
|
|
|
|
g_array_zero_terminate (array);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-10 23:21:14 +00:00
|
|
|
|
return (GArray*) array;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-04 07:41:47 +00:00
|
|
|
|
/**
|
|
|
|
|
* g_array_set_clear_func:
|
|
|
|
|
* @array: A #GArray
|
|
|
|
|
* @clear_func: a function to clear an element of @array
|
|
|
|
|
*
|
|
|
|
|
* Sets a function to clear an element of @array.
|
|
|
|
|
*
|
|
|
|
|
* The @clear_func will be called when an element in the array
|
|
|
|
|
* data segment is removed and when the array is freed and data
|
2018-02-23 19:23:27 +00:00
|
|
|
|
* segment is deallocated as well. @clear_func will be passed a
|
|
|
|
|
* pointer to the element to clear, rather than the element itself.
|
2012-01-04 07:41:47 +00:00
|
|
|
|
*
|
|
|
|
|
* Note that in contrast with other uses of #GDestroyNotify
|
|
|
|
|
* functions, @clear_func is expected to clear the contents of
|
|
|
|
|
* the array element it is given, but not free the element itself.
|
|
|
|
|
*
|
2021-06-07 15:52:43 +00:00
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* typedef struct
|
|
|
|
|
* {
|
|
|
|
|
* gchar *str;
|
|
|
|
|
* GObject *obj;
|
|
|
|
|
* } ArrayElement;
|
|
|
|
|
*
|
|
|
|
|
* static void
|
|
|
|
|
* array_element_clear (ArrayElement *element)
|
|
|
|
|
* {
|
|
|
|
|
* g_clear_pointer (&element->str, g_free);
|
|
|
|
|
* g_clear_object (&element->obj);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* // main code
|
|
|
|
|
* GArray *garray = g_array_new (FALSE, FALSE, sizeof (ArrayElement));
|
|
|
|
|
* g_array_set_clear_func (garray, (GDestroyNotify) array_element_clear);
|
|
|
|
|
* // assign data to the structure
|
|
|
|
|
* g_array_free (garray, TRUE);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2012-01-04 07:41:47 +00:00
|
|
|
|
* Since: 2.32
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_array_set_clear_func (GArray *array,
|
|
|
|
|
GDestroyNotify clear_func)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray = (GRealArray *) array;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
|
|
|
|
rarray->clear_func = clear_func;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_array_ref:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Atomically increments the reference count of @array by one.
|
2017-08-22 15:58:18 +01:00
|
|
|
|
* This function is thread-safe and may be called from any thread.
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: The passed in #GArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
GArray *
|
|
|
|
|
g_array_ref (GArray *array)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray = (GRealArray*) array;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_val_if_fail (array, NULL);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2018-01-17 17:01:23 +00:00
|
|
|
|
g_atomic_ref_count_inc (&rarray->ref_count);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
FREE_SEGMENT = 1 << 0,
|
|
|
|
|
PRESERVE_WRAPPER = 1 << 1
|
|
|
|
|
} ArrayFreeFlags;
|
|
|
|
|
|
|
|
|
|
static gchar *array_free (GRealArray *, ArrayFreeFlags);
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_array_unref:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Atomically decrements the reference count of @array by one. If the
|
|
|
|
|
* reference count drops to 0, all memory allocated by the array is
|
2017-08-22 15:58:18 +01:00
|
|
|
|
* released. This function is thread-safe and may be called from any
|
2009-04-29 11:15:20 -04:00
|
|
|
|
* thread.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
void
|
|
|
|
|
g_array_unref (GArray *array)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray = (GRealArray*) array;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_if_fail (array);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2018-01-17 17:01:23 +00:00
|
|
|
|
if (g_atomic_ref_count_dec (&rarray->ref_count))
|
2011-12-14 16:53:24 +00:00
|
|
|
|
array_free (rarray, FREE_SEGMENT);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_array_get_element_size:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Gets the size of the elements in @array.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: Size of each element, in bytes
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
guint
|
|
|
|
|
g_array_get_element_size (GArray *array)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray = (GRealArray*) array;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, 0);
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
return rarray->elt_size;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_free:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @free_segment: if %TRUE the actual element data is freed as well
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Frees the memory allocated for the #GArray. If @free_segment is
|
2018-09-24 16:03:46 +02:00
|
|
|
|
* %TRUE it frees the memory block holding the elements as well. Pass
|
|
|
|
|
* %FALSE if you want to free the #GArray wrapper but preserve the
|
|
|
|
|
* underlying array for use elsewhere. If the reference count of
|
|
|
|
|
* @array is greater than one, the #GArray wrapper is preserved but
|
|
|
|
|
* the size of @array will be set to zero.
|
|
|
|
|
*
|
|
|
|
|
* If array contents point to dynamically-allocated memory, they should
|
|
|
|
|
* be freed separately if @free_seg is %TRUE and no @clear_func
|
|
|
|
|
* function has been set for @array.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2017-08-22 15:58:18 +01:00
|
|
|
|
* This function is not thread-safe. If using a #GArray from multiple
|
|
|
|
|
* threads, use only the atomic g_array_ref() and g_array_unref()
|
|
|
|
|
* functions.
|
|
|
|
|
*
|
2012-10-31 14:56:00 +13:00
|
|
|
|
* Returns: the element data if @free_segment is %FALSE, otherwise
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* %NULL. The element data should be freed using g_free().
|
|
|
|
|
*/
|
2000-08-17 21:37:18 +00:00
|
|
|
|
gchar*
|
2009-04-29 11:15:20 -04:00
|
|
|
|
g_array_free (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gboolean free_segment)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
2009-04-29 11:15:20 -04:00
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
2011-12-14 16:53:24 +00:00
|
|
|
|
ArrayFreeFlags flags;
|
2000-08-17 21:37:18 +00:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
flags = (free_segment ? FREE_SEGMENT : 0);
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/* if others are holding a reference, preserve the wrapper but do free/return the data */
|
2018-01-17 17:01:23 +00:00
|
|
|
|
if (!g_atomic_ref_count_dec (&array->ref_count))
|
2011-12-14 16:53:24 +00:00
|
|
|
|
flags |= PRESERVE_WRAPPER;
|
|
|
|
|
|
|
|
|
|
return array_free (array, flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
|
array_free (GRealArray *array,
|
|
|
|
|
ArrayFreeFlags flags)
|
|
|
|
|
{
|
|
|
|
|
gchar *segment;
|
2009-04-29 11:15:20 -04:00
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
if (flags & FREE_SEGMENT)
|
2000-08-17 21:37:18 +00:00
|
|
|
|
{
|
2012-01-04 07:41:47 +00:00
|
|
|
|
if (array->clear_func != NULL)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < array->len; i++)
|
|
|
|
|
array->clear_func (g_array_elt_pos (array, i));
|
|
|
|
|
}
|
|
|
|
|
|
2000-08-17 21:37:18 +00:00
|
|
|
|
g_free (array->data);
|
|
|
|
|
segment = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
2009-05-01 10:08:52 -04:00
|
|
|
|
segment = (gchar*) array->data;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
if (flags & PRESERVE_WRAPPER)
|
2009-04-29 11:15:20 -04:00
|
|
|
|
{
|
|
|
|
|
array->data = NULL;
|
|
|
|
|
array->len = 0;
|
2021-10-19 15:38:13 +00:00
|
|
|
|
array->elt_capacity = 0;
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g_slice_free1 (sizeof (GRealArray), array);
|
|
|
|
|
}
|
2000-08-17 21:37:18 +00:00
|
|
|
|
|
|
|
|
|
return segment;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_append_vals:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
2013-12-06 12:23:09 +00:00
|
|
|
|
* @data: (not nullable): a pointer to the elements to append to the end of the array
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @len: the number of elements to append
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Adds @len elements onto the end of the array.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_append_val:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @a: a #GArray
|
|
|
|
|
* @v: the value to append to the #GArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Adds the value on to the end of the array. The array will grow in
|
|
|
|
|
* size automatically if necessary.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* g_array_append_val() is a macro which uses a reference to the value
|
|
|
|
|
* parameter @v. This means that you cannot use it with literal values
|
|
|
|
|
* such as "27". You must use variables.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-06-10 23:21:14 +00:00
|
|
|
|
GArray*
|
1998-09-02 23:11:04 +00:00
|
|
|
|
g_array_append_vals (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gconstpointer data,
|
|
|
|
|
guint len)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
1998-09-02 07:44:02 +00:00
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2016-12-02 10:03:16 +00:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return farray;
|
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
g_array_maybe_expand (array, len);
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2000-03-24 15:36:03 +00:00
|
|
|
|
memcpy (g_array_elt_pos (array, array->len), data,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
g_array_elt_len (array, len));
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
array->len += len;
|
|
|
|
|
|
2000-03-24 15:36:03 +00:00
|
|
|
|
g_array_zero_terminate (array);
|
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
return farray;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_prepend_vals:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
2018-05-09 15:34:39 +01:00
|
|
|
|
* @data: (nullable): a pointer to the elements to prepend to the start of the array
|
|
|
|
|
* @len: the number of elements to prepend, which may be zero
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Adds @len elements onto the start of the array.
|
|
|
|
|
*
|
2018-05-09 15:34:39 +01:00
|
|
|
|
* @data may be %NULL if (and only if) @len is zero. If @len is zero, this
|
|
|
|
|
* function is a no-op.
|
|
|
|
|
*
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* This operation is slower than g_array_append_vals() since the
|
|
|
|
|
* existing elements in the array have to be moved to make space for
|
|
|
|
|
* the new elements.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_prepend_val:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @a: a #GArray
|
|
|
|
|
* @v: the value to prepend to the #GArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Adds the value on to the start of the array. The array will grow in
|
|
|
|
|
* size automatically if necessary.
|
|
|
|
|
*
|
|
|
|
|
* This operation is slower than g_array_append_val() since the
|
|
|
|
|
* existing elements in the array have to be moved to make space for
|
|
|
|
|
* the new element.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* g_array_prepend_val() is a macro which uses a reference to the value
|
|
|
|
|
* parameter @v. This means that you cannot use it with literal values
|
|
|
|
|
* such as "27". You must use variables.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-06-10 23:21:14 +00:00
|
|
|
|
GArray*
|
1998-09-02 23:11:04 +00:00
|
|
|
|
g_array_prepend_vals (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gconstpointer data,
|
|
|
|
|
guint len)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
1998-09-02 07:44:02 +00:00
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2016-12-02 10:03:16 +00:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return farray;
|
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
g_array_maybe_expand (array, len);
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
Require C90 compliance
Assume all supported platforms implement C90, and therefore they
(correctly) implement atexit(), memmove(), setlocale(), strerror(),
and vprintf(), and have <float.h> and <limits.h>.
(Also remove the configure check testing that "do ... while (0)" works
correctly; the non-do/while-based version of G_STMT_START and
G_STMT_END was removed years ago, but the check remained. Also, remove
some checks that configure.ac claimed were needed for libcharset, but
aren't actually used.)
Note that removing the g_memmove() function is not an ABI break even
on systems where g_memmove() was previously not a macro, because it
was never marked GLIB_AVAILABLE_IN_ALL or listed in glib.symbols, so
it would have been glib-internal since 2004.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 13:03:58 -04:00
|
|
|
|
memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0),
|
|
|
|
|
g_array_elt_len (array, array->len));
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2000-03-24 15:36:03 +00:00
|
|
|
|
memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
|
1998-09-02 07:44:02 +00:00
|
|
|
|
|
|
|
|
|
array->len += len;
|
|
|
|
|
|
2000-03-24 15:36:03 +00:00
|
|
|
|
g_array_zero_terminate (array);
|
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
return farray;
|
1998-06-10 23:21:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_insert_vals:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @index_: the index to place the elements at
|
2018-05-09 15:34:39 +01:00
|
|
|
|
* @data: (nullable): a pointer to the elements to insert
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @len: the number of elements to insert
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Inserts @len elements into a #GArray at the given index.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2018-05-09 15:35:23 +01:00
|
|
|
|
* If @index_ is greater than the array’s current length, the array is expanded.
|
|
|
|
|
* The elements between the old end of the array and the newly inserted elements
|
|
|
|
|
* will be initialised to zero if the array was configured to clear elements;
|
|
|
|
|
* otherwise their values will be undefined.
|
|
|
|
|
*
|
2020-03-03 14:32:13 +00:00
|
|
|
|
* If @index_ is less than the array’s current length, new entries will be
|
|
|
|
|
* inserted into the array, and the existing entries above @index_ will be moved
|
|
|
|
|
* upwards.
|
|
|
|
|
*
|
2018-05-09 15:34:39 +01:00
|
|
|
|
* @data may be %NULL if (and only if) @len is zero. If @len is zero, this
|
|
|
|
|
* function is a no-op.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_insert_val:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @a: a #GArray
|
|
|
|
|
* @i: the index to place the element at
|
|
|
|
|
* @v: the value to insert into the array
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Inserts an element into an array at the given index.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* g_array_insert_val() is a macro which uses a reference to the value
|
|
|
|
|
* parameter @v. This means that you cannot use it with literal values
|
|
|
|
|
* such as "27". You must use variables.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-11-23 15:02:44 +00:00
|
|
|
|
GArray*
|
|
|
|
|
g_array_insert_vals (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
guint index_,
|
|
|
|
|
gconstpointer data,
|
|
|
|
|
guint len)
|
1998-11-23 15:02:44 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
|
|
|
|
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2016-12-02 10:03:16 +00:00
|
|
|
|
if (len == 0)
|
|
|
|
|
return farray;
|
|
|
|
|
|
2018-05-09 15:35:23 +01:00
|
|
|
|
/* Is the index off the end of the array, and hence do we need to over-allocate
|
|
|
|
|
* and clear some elements? */
|
|
|
|
|
if (index_ >= array->len)
|
2018-06-04 11:42:24 +01:00
|
|
|
|
{
|
|
|
|
|
g_array_maybe_expand (array, index_ - array->len + len);
|
2018-05-09 15:35:23 +01:00
|
|
|
|
return g_array_append_vals (g_array_set_size (farray, index_), data, len);
|
2018-06-04 11:42:24 +01:00
|
|
|
|
}
|
2018-05-09 15:35:23 +01:00
|
|
|
|
|
1998-11-23 15:02:44 +00:00
|
|
|
|
g_array_maybe_expand (array, len);
|
|
|
|
|
|
Require C90 compliance
Assume all supported platforms implement C90, and therefore they
(correctly) implement atexit(), memmove(), setlocale(), strerror(),
and vprintf(), and have <float.h> and <limits.h>.
(Also remove the configure check testing that "do ... while (0)" works
correctly; the non-do/while-based version of G_STMT_START and
G_STMT_END was removed years ago, but the check remained. Also, remove
some checks that configure.ac claimed were needed for libcharset, but
aren't actually used.)
Note that removing the g_memmove() function is not an ABI break even
on systems where g_memmove() was previously not a macro, because it
was never marked GLIB_AVAILABLE_IN_ALL or listed in glib.symbols, so
it would have been glib-internal since 2004.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 13:03:58 -04:00
|
|
|
|
memmove (g_array_elt_pos (array, len + index_),
|
|
|
|
|
g_array_elt_pos (array, index_),
|
|
|
|
|
g_array_elt_len (array, array->len - index_));
|
1998-11-23 15:02:44 +00:00
|
|
|
|
|
2008-12-31 05:51:47 +00:00
|
|
|
|
memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
|
1998-11-23 15:02:44 +00:00
|
|
|
|
|
|
|
|
|
array->len += len;
|
|
|
|
|
|
2000-03-24 15:36:03 +00:00
|
|
|
|
g_array_zero_terminate (array);
|
|
|
|
|
|
1998-11-23 15:02:44 +00:00
|
|
|
|
return farray;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_set_size:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @length: the new size of the #GArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sets the size of the array, expanding it if necessary. If the array
|
|
|
|
|
* was created with @clear_ set to %TRUE, the new elements are set to 0.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-06-10 23:21:14 +00:00
|
|
|
|
GArray*
|
1998-09-02 07:44:02 +00:00
|
|
|
|
g_array_set_size (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
guint length)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
1998-09-02 07:44:02 +00:00
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
if (length > array->len)
|
2000-03-24 15:36:03 +00:00
|
|
|
|
{
|
|
|
|
|
g_array_maybe_expand (array, length - array->len);
|
|
|
|
|
|
|
|
|
|
if (array->clear)
|
2014-01-31 14:56:10 -05:00
|
|
|
|
g_array_elt_zero (array, array->len, length - array->len);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
}
|
2012-01-04 07:41:47 +00:00
|
|
|
|
else if (length < array->len)
|
|
|
|
|
g_array_remove_range (farray, length, array->len - length);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
array->len = length;
|
2000-03-24 15:36:03 +00:00
|
|
|
|
|
|
|
|
|
g_array_zero_terminate (array);
|
|
|
|
|
|
1998-09-02 07:44:02 +00:00
|
|
|
|
return farray;
|
|
|
|
|
}
|
1998-06-10 23:21:14 +00:00
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_remove_index:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @index_: the index of the element to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the element at the given index from a #GArray. The following
|
|
|
|
|
* elements are moved down one place.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-11-03 14:52:25 +00:00
|
|
|
|
GArray*
|
2008-12-31 05:51:47 +00:00
|
|
|
|
g_array_remove_index (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
guint index_)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray* array = (GRealArray*) farray;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2008-12-31 05:51:47 +00:00
|
|
|
|
g_return_val_if_fail (index_ < array->len, NULL);
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
2012-01-04 07:41:47 +00:00
|
|
|
|
if (array->clear_func != NULL)
|
|
|
|
|
array->clear_func (g_array_elt_pos (array, index_));
|
|
|
|
|
|
2008-12-31 05:51:47 +00:00
|
|
|
|
if (index_ != array->len - 1)
|
Require C90 compliance
Assume all supported platforms implement C90, and therefore they
(correctly) implement atexit(), memmove(), setlocale(), strerror(),
and vprintf(), and have <float.h> and <limits.h>.
(Also remove the configure check testing that "do ... while (0)" works
correctly; the non-do/while-based version of G_STMT_START and
G_STMT_END was removed years ago, but the check remained. Also, remove
some checks that configure.ac claimed were needed for libcharset, but
aren't actually used.)
Note that removing the g_memmove() function is not an ABI break even
on systems where g_memmove() was previously not a macro, because it
was never marked GLIB_AVAILABLE_IN_ALL or listed in glib.symbols, so
it would have been glib-internal since 2004.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 13:03:58 -04:00
|
|
|
|
memmove (g_array_elt_pos (array, index_),
|
|
|
|
|
g_array_elt_pos (array, index_ + 1),
|
|
|
|
|
g_array_elt_len (array, array->len - index_ - 1));
|
2012-01-04 07:41:47 +00:00
|
|
|
|
|
1998-11-03 14:52:25 +00:00
|
|
|
|
array->len -= 1;
|
|
|
|
|
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
|
|
|
|
g_array_elt_zero (array, array->len, 1);
|
|
|
|
|
else
|
|
|
|
|
g_array_zero_terminate (array);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
|
1998-11-03 14:52:25 +00:00
|
|
|
|
return farray;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_remove_index_fast:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a @GArray
|
|
|
|
|
* @index_: the index of the element to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the element at the given index from a #GArray. The last
|
|
|
|
|
* element in the array is used to fill in the space, so this function
|
|
|
|
|
* does not preserve the order of the #GArray. But it is faster than
|
|
|
|
|
* g_array_remove_index().
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
|
|
|
|
*/
|
1998-11-03 14:52:25 +00:00
|
|
|
|
GArray*
|
2008-12-31 05:51:47 +00:00
|
|
|
|
g_array_remove_index_fast (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
guint index_)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray* array = (GRealArray*) farray;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
|
|
|
|
|
2008-12-31 05:51:47 +00:00
|
|
|
|
g_return_val_if_fail (index_ < array->len, NULL);
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
2012-01-04 07:41:47 +00:00
|
|
|
|
if (array->clear_func != NULL)
|
|
|
|
|
array->clear_func (g_array_elt_pos (array, index_));
|
|
|
|
|
|
2008-12-31 05:51:47 +00:00
|
|
|
|
if (index_ != array->len - 1)
|
2012-01-04 07:41:47 +00:00
|
|
|
|
memcpy (g_array_elt_pos (array, index_),
|
|
|
|
|
g_array_elt_pos (array, array->len - 1),
|
|
|
|
|
g_array_elt_len (array, 1));
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
|
|
|
|
array->len -= 1;
|
|
|
|
|
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
|
|
|
|
g_array_elt_zero (array, array->len, 1);
|
|
|
|
|
else
|
|
|
|
|
g_array_zero_terminate (array);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
|
1998-11-03 14:52:25 +00:00
|
|
|
|
return farray;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_remove_range:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a @GArray
|
|
|
|
|
* @index_: the index of the first element to remove
|
|
|
|
|
* @length: the number of elements to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the given number of elements starting at the given index
|
|
|
|
|
* from a #GArray. The following elements are moved to close the gap.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the #GArray
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* Since: 2.4
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2003-07-26 08:03:16 +00:00
|
|
|
|
GArray*
|
2008-12-31 05:51:47 +00:00
|
|
|
|
g_array_remove_range (GArray *farray,
|
|
|
|
|
guint index_,
|
|
|
|
|
guint length)
|
2003-07-26 08:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
2016-03-08 18:59:34 +00:00
|
|
|
|
g_return_val_if_fail (index_ <= array->len, NULL);
|
2024-02-02 20:33:51 +01:00
|
|
|
|
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
|
2003-07-26 08:03:16 +00:00
|
|
|
|
g_return_val_if_fail (index_ + length <= array->len, NULL);
|
|
|
|
|
|
2012-01-04 07:41:47 +00:00
|
|
|
|
if (array->clear_func != NULL)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
|
array->clear_func (g_array_elt_pos (array, index_ + i));
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-26 08:03:16 +00:00
|
|
|
|
if (index_ + length != array->len)
|
Require C90 compliance
Assume all supported platforms implement C90, and therefore they
(correctly) implement atexit(), memmove(), setlocale(), strerror(),
and vprintf(), and have <float.h> and <limits.h>.
(Also remove the configure check testing that "do ... while (0)" works
correctly; the non-do/while-based version of G_STMT_START and
G_STMT_END was removed years ago, but the check remained. Also, remove
some checks that configure.ac claimed were needed for libcharset, but
aren't actually used.)
Note that removing the g_memmove() function is not an ABI break even
on systems where g_memmove() was previously not a macro, because it
was never marked GLIB_AVAILABLE_IN_ALL or listed in glib.symbols, so
it would have been glib-internal since 2004.
https://bugzilla.gnome.org/show_bug.cgi?id=710519
2013-10-19 13:03:58 -04:00
|
|
|
|
memmove (g_array_elt_pos (array, index_),
|
|
|
|
|
g_array_elt_pos (array, index_ + length),
|
|
|
|
|
(array->len - (index_ + length)) * array->elt_size);
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
|
|
|
|
array->len -= length;
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
|
|
|
|
g_array_elt_zero (array, array->len, length);
|
|
|
|
|
else
|
|
|
|
|
g_array_zero_terminate (array);
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
|
|
|
|
return farray;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_sort:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @compare_func: comparison function
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sorts a #GArray using @compare_func which should be a qsort()-style
|
|
|
|
|
* comparison function (returns less than zero for first arg is less
|
|
|
|
|
* than second arg, zero for equal, greater zero if first arg is
|
|
|
|
|
* greater than second arg).
|
|
|
|
|
*
|
2012-03-14 21:17:47 +01:00
|
|
|
|
* This is guaranteed to be a stable sort since version 2.32.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_array_sort (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareFunc compare_func)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
2012-03-14 21:17:47 +01:00
|
|
|
|
/* Don't use qsort as we want a guaranteed stable sort */
|
2020-12-09 13:57:24 +01:00
|
|
|
|
if (array->len > 0)
|
2024-06-28 15:22:33 +01:00
|
|
|
|
g_sort_array (array->data,
|
|
|
|
|
array->len,
|
|
|
|
|
array->elt_size,
|
|
|
|
|
(GCompareDataFunc) compare_func,
|
|
|
|
|
NULL);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_array_sort_with_data:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GArray
|
|
|
|
|
* @compare_func: comparison function
|
|
|
|
|
* @user_data: data to pass to @compare_func
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Like g_array_sort(), but the comparison function receives an extra
|
|
|
|
|
* user data argument.
|
2012-03-14 21:17:47 +01:00
|
|
|
|
*
|
|
|
|
|
* This is guaranteed to be a stable sort since version 2.32.
|
|
|
|
|
*
|
|
|
|
|
* There used to be a comment here about making the sort stable by
|
|
|
|
|
* using the addresses of the elements in the comparison function.
|
|
|
|
|
* This did not actually work, so any such code should be removed.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_array_sort_with_data (GArray *farray,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareDataFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
|
|
|
|
GRealArray *array = (GRealArray*) farray;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
2020-12-09 13:57:24 +01:00
|
|
|
|
if (array->len > 0)
|
2024-06-28 15:22:33 +01:00
|
|
|
|
g_sort_array (array->data,
|
|
|
|
|
array->len,
|
|
|
|
|
array->elt_size,
|
|
|
|
|
compare_func,
|
|
|
|
|
user_data);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 14:34:58 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_array_binary_search:
|
|
|
|
|
* @array: a #GArray.
|
|
|
|
|
* @target: a pointer to the item to look up.
|
|
|
|
|
* @compare_func: A #GCompareFunc used to locate @target.
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @out_match_index: (optional) (out): return location
|
2019-05-16 14:34:58 +02:00
|
|
|
|
* for the index of the element, if found.
|
|
|
|
|
*
|
|
|
|
|
* Checks whether @target exists in @array by performing a binary
|
|
|
|
|
* search based on the given comparison function @compare_func which
|
|
|
|
|
* get pointers to items as arguments. If the element is found, %TRUE
|
|
|
|
|
* is returned and the element’s index is returned in @out_match_index
|
|
|
|
|
* (if non-%NULL). Otherwise, %FALSE is returned and @out_match_index
|
|
|
|
|
* is undefined. If @target exists multiple times in @array, the index
|
|
|
|
|
* of the first instance is returned. This search is using a binary
|
|
|
|
|
* search, so the @array must absolutely be sorted to return a correct
|
|
|
|
|
* result (if not, the function may produce false-negative).
|
|
|
|
|
*
|
|
|
|
|
* This example defines a comparison function and search an element in a #GArray:
|
|
|
|
|
* |[<!-- language="C" -->
|
2023-01-10 11:09:59 +01:00
|
|
|
|
* static gint
|
2019-05-16 14:34:58 +02:00
|
|
|
|
* cmpint (gconstpointer a, gconstpointer b)
|
|
|
|
|
* {
|
|
|
|
|
* const gint *_a = a;
|
|
|
|
|
* const gint *_b = b;
|
|
|
|
|
*
|
|
|
|
|
* return *_a - *_b;
|
|
|
|
|
* }
|
|
|
|
|
* ...
|
|
|
|
|
* gint i = 424242;
|
|
|
|
|
* guint matched_index;
|
|
|
|
|
* gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index);
|
|
|
|
|
* ...
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @target is one of the elements of @array, %FALSE otherwise.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.62
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_array_binary_search (GArray *array,
|
|
|
|
|
gconstpointer target,
|
|
|
|
|
GCompareFunc compare_func,
|
|
|
|
|
guint *out_match_index)
|
|
|
|
|
{
|
|
|
|
|
gboolean result = FALSE;
|
|
|
|
|
GRealArray *_array = (GRealArray *) array;
|
2022-01-19 18:59:14 +01:00
|
|
|
|
guint left, middle = 0, right;
|
2019-05-16 14:34:58 +02:00
|
|
|
|
gint val;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (_array != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (compare_func != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
if (G_LIKELY(_array->len))
|
|
|
|
|
{
|
|
|
|
|
left = 0;
|
|
|
|
|
right = _array->len - 1;
|
|
|
|
|
|
|
|
|
|
while (left <= right)
|
|
|
|
|
{
|
2019-07-16 11:35:18 +01:00
|
|
|
|
middle = left + (right - left) / 2;
|
2019-05-16 14:34:58 +02:00
|
|
|
|
|
|
|
|
|
val = compare_func (_array->data + (_array->elt_size * middle), target);
|
2019-07-16 11:36:28 +01:00
|
|
|
|
if (val == 0)
|
2019-05-16 14:34:58 +02:00
|
|
|
|
{
|
|
|
|
|
result = TRUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-07-16 11:36:28 +01:00
|
|
|
|
else if (val < 0)
|
|
|
|
|
left = middle + 1;
|
|
|
|
|
else if (/* val > 0 && */ middle > 0)
|
|
|
|
|
right = middle - 1;
|
|
|
|
|
else
|
|
|
|
|
break; /* element not found */
|
2019-05-16 14:34:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result && out_match_index != NULL)
|
|
|
|
|
*out_match_index = middle;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
1998-06-10 23:21:14 +00:00
|
|
|
|
static void
|
|
|
|
|
g_array_maybe_expand (GRealArray *array,
|
2018-07-10 14:15:42 +02:00
|
|
|
|
guint len)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
2021-10-19 15:38:13 +00:00
|
|
|
|
guint max_len, want_len;
|
|
|
|
|
|
|
|
|
|
/* The maximum array length is derived from following constraints:
|
|
|
|
|
* - The number of bytes must fit into a gsize / 2.
|
|
|
|
|
* - The number of elements must fit into guint.
|
|
|
|
|
* - zero terminated arrays must leave space for the terminating element
|
|
|
|
|
*/
|
|
|
|
|
max_len = MIN (G_MAXSIZE / 2 / array->elt_size, G_MAXUINT) - array->zero_terminated;
|
2018-07-23 21:05:11 -07:00
|
|
|
|
|
|
|
|
|
/* Detect potential overflow */
|
2021-10-19 15:38:13 +00:00
|
|
|
|
if G_UNLIKELY ((max_len - array->len) < len)
|
2018-07-23 21:05:11 -07:00
|
|
|
|
g_error ("adding %u to array would overflow", len);
|
|
|
|
|
|
2021-10-19 15:38:13 +00:00
|
|
|
|
want_len = array->len + len + array->zero_terminated;
|
|
|
|
|
if (want_len > array->elt_capacity)
|
1998-06-10 23:21:14 +00:00
|
|
|
|
{
|
2021-10-19 15:38:13 +00:00
|
|
|
|
gsize want_alloc = g_nearest_pow (g_array_elt_len (array, want_len));
|
2023-04-12 21:10:21 +02:00
|
|
|
|
g_assert (want_alloc >= g_array_elt_len (array, want_len));
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
|
1998-09-02 07:44:02 +00:00
|
|
|
|
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
array->data = g_realloc (array->data, want_alloc);
|
|
|
|
|
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
2021-10-19 15:38:13 +00:00
|
|
|
|
memset (g_array_elt_pos (array, array->elt_capacity), 0,
|
|
|
|
|
g_array_elt_len (array, want_len - array->elt_capacity));
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
|
2022-01-18 13:45:13 +00:00
|
|
|
|
array->elt_capacity = MIN (want_alloc / array->elt_size, G_MAXUINT);
|
1998-06-10 23:21:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
typedef struct _GRealPtrArray GRealPtrArray;
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* GPtrArray:
|
|
|
|
|
* @pdata: points to the array of pointers, which may be moved when the
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* array grows
|
|
|
|
|
* @len: number of pointers in the array
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Contains the public fields of a pointer array.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
struct _GRealPtrArray
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
gpointer *pdata;
|
|
|
|
|
guint len;
|
|
|
|
|
guint alloc;
|
2018-01-17 17:01:23 +00:00
|
|
|
|
gatomicrefcount ref_count;
|
2022-12-07 13:39:15 +01:00
|
|
|
|
guint8 null_terminated : 1; /* always either 0 or 1, so it can be added to array lengths */
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GDestroyNotify element_free_func;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_index:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index of the pointer to return
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Returns the pointer at the given index of the pointer array.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* This does not perform bounds checking on the given @index_,
|
|
|
|
|
* so you are responsible for checking it against the array length.
|
2013-11-05 16:43:26 +00:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the pointer at the given index
|
|
|
|
|
*/
|
2010-01-31 14:30:08 -05:00
|
|
|
|
|
1998-06-12 09:38:32 +00:00
|
|
|
|
static void g_ptr_array_maybe_expand (GRealPtrArray *array,
|
2019-01-26 12:21:23 +01:00
|
|
|
|
guint len);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
static void
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (GRealPtrArray *rarray)
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
{
|
|
|
|
|
if (G_UNLIKELY (rarray->null_terminated))
|
|
|
|
|
rarray->pdata[rarray->len] = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 17:36:43 +02:00
|
|
|
|
static GPtrArray *
|
|
|
|
|
ptr_array_new (guint reserved_size,
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
GDestroyNotify element_free_func,
|
|
|
|
|
gboolean null_terminated)
|
2020-05-15 17:36:43 +02:00
|
|
|
|
{
|
|
|
|
|
GRealPtrArray *array;
|
|
|
|
|
|
|
|
|
|
array = g_slice_new (GRealPtrArray);
|
|
|
|
|
|
|
|
|
|
array->pdata = NULL;
|
|
|
|
|
array->len = 0;
|
|
|
|
|
array->alloc = 0;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
array->null_terminated = null_terminated ? 1 : 0;
|
2020-05-15 17:36:43 +02:00
|
|
|
|
array->element_free_func = element_free_func;
|
|
|
|
|
|
|
|
|
|
g_atomic_ref_count_init (&array->ref_count);
|
|
|
|
|
|
|
|
|
|
if (reserved_size != 0)
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
{
|
|
|
|
|
if (G_LIKELY (reserved_size < G_MAXUINT) &&
|
|
|
|
|
null_terminated)
|
|
|
|
|
reserved_size++;
|
2022-11-29 12:16:11 +00:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
g_ptr_array_maybe_expand (array, reserved_size);
|
2022-11-29 12:16:11 +00:00
|
|
|
|
g_assert (array->pdata != NULL);
|
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
if (null_terminated)
|
|
|
|
|
{
|
2022-06-07 13:31:52 +01:00
|
|
|
|
/* don't use ptr_array_maybe_null_terminate(). It helps the compiler
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
* to see when @null_terminated is false and thereby inline
|
|
|
|
|
* ptr_array_new() and possibly remove the code entirely. */
|
|
|
|
|
array->pdata[0] = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-15 17:36:43 +02:00
|
|
|
|
|
|
|
|
|
return (GPtrArray *) array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new:
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray with a reference count of 1.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the new #GPtrArray
|
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
GPtrArray*
|
1998-09-07 09:43:54 +00:00
|
|
|
|
g_ptr_array_new (void)
|
2000-04-17 10:59:46 +00:00
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
return ptr_array_new (0, NULL, FALSE);
|
2000-04-17 10:59:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 13:35:04 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_take: (skip)
|
|
|
|
|
* @data: (array length=len) (transfer full) (nullable): an array of pointers,
|
|
|
|
|
* or %NULL for an empty array
|
|
|
|
|
* @len: the number of pointers in @data
|
|
|
|
|
* @element_free_func: (nullable): A function to free elements on @array
|
|
|
|
|
* destruction or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray with @data as pointers, @len as length and a
|
|
|
|
|
* reference count of 1.
|
|
|
|
|
*
|
2023-05-08 01:24:25 +02:00
|
|
|
|
* This avoids having to copy such data manually.
|
|
|
|
|
* After this call, @data belongs to the #GPtrArray and may no longer be
|
|
|
|
|
* modified by the caller. The memory of @data has to be dynamically
|
|
|
|
|
* allocated and will eventually be freed with g_free().
|
2022-12-07 13:35:04 +01:00
|
|
|
|
*
|
|
|
|
|
* It also sets @element_free_func for freeing each element when the array is
|
|
|
|
|
* destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called
|
|
|
|
|
* with @free_segment set to %TRUE or when removing elements.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if @len is greater than %G_MAXUINT. #GPtrArray
|
|
|
|
|
* stores the length of its data in #guint, which may be shorter than
|
|
|
|
|
* #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_new_take (gpointer *data,
|
|
|
|
|
gsize len,
|
|
|
|
|
GDestroyNotify element_free_func)
|
|
|
|
|
{
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
GRealPtrArray *rarray;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (data != NULL || len == 0, NULL);
|
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
|
|
|
|
|
|
|
|
|
array = ptr_array_new (0, element_free_func, FALSE);
|
|
|
|
|
rarray = (GRealPtrArray *)array;
|
|
|
|
|
|
|
|
|
|
rarray->pdata = g_steal_pointer (&data);
|
|
|
|
|
rarray->len = len;
|
|
|
|
|
rarray->alloc = len;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 19:27:34 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_take_null_terminated: (skip)
|
|
|
|
|
* @data: (array zero-terminated=1) (transfer full) (nullable): an array
|
|
|
|
|
* of pointers, %NULL terminated, or %NULL for an empty array
|
|
|
|
|
* @element_free_func: (nullable): a function to free elements on @array
|
|
|
|
|
* destruction or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray with @data as pointers, computing the length of it
|
|
|
|
|
* and setting the reference count to 1.
|
|
|
|
|
*
|
2023-05-08 01:24:25 +02:00
|
|
|
|
* This avoids having to copy such data manually.
|
|
|
|
|
* After this call, @data belongs to the #GPtrArray and may no longer be
|
|
|
|
|
* modified by the caller. The memory of @data has to be dynamically
|
|
|
|
|
* allocated and will eventually be freed with g_free().
|
2022-12-13 19:27:34 +01:00
|
|
|
|
*
|
|
|
|
|
* The length is calculated by iterating through @data until the first %NULL
|
|
|
|
|
* element is found.
|
|
|
|
|
*
|
|
|
|
|
* It also sets @element_free_func for freeing each element when the array is
|
|
|
|
|
* destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called
|
|
|
|
|
* with @free_segment set to %TRUE or when removing elements.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if the @data length is greater than %G_MAXUINT. #GPtrArray
|
|
|
|
|
* stores the length of its data in #guint, which may be shorter than
|
|
|
|
|
* #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_new_take_null_terminated (gpointer *data,
|
|
|
|
|
GDestroyNotify element_free_func)
|
|
|
|
|
{
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
gsize len = 0;
|
|
|
|
|
|
|
|
|
|
if (data != NULL)
|
|
|
|
|
{
|
|
|
|
|
for (gsize i = 0; data[i] != NULL; ++i)
|
|
|
|
|
len += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
|
|
|
|
|
2022-12-13 19:27:34 +01:00
|
|
|
|
array = g_ptr_array_new_take (g_steal_pointer (&data), len, element_free_func);
|
|
|
|
|
((GRealPtrArray *)array)->null_terminated = TRUE;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
static GPtrArray *
|
|
|
|
|
ptr_array_new_from_array (gpointer *data,
|
|
|
|
|
gsize len,
|
|
|
|
|
GCopyFunc copy_func,
|
|
|
|
|
gpointer copy_func_user_data,
|
|
|
|
|
GDestroyNotify element_free_func,
|
|
|
|
|
gboolean null_terminated)
|
|
|
|
|
{
|
|
|
|
|
GPtrArray *array;
|
|
|
|
|
GRealPtrArray *rarray;
|
|
|
|
|
|
2022-12-29 16:07:33 +00:00
|
|
|
|
g_assert (data != NULL || len == 0);
|
2022-12-13 20:58:47 +01:00
|
|
|
|
g_assert (len <= G_MAXUINT);
|
|
|
|
|
|
|
|
|
|
array = ptr_array_new (len, element_free_func, null_terminated);
|
|
|
|
|
rarray = (GRealPtrArray *)array;
|
|
|
|
|
|
|
|
|
|
if (copy_func != NULL)
|
|
|
|
|
{
|
|
|
|
|
for (gsize i = 0; i < len; i++)
|
|
|
|
|
rarray->pdata[i] = copy_func (data[i], copy_func_user_data);
|
|
|
|
|
}
|
2023-01-10 13:40:24 +00:00
|
|
|
|
else if (len != 0)
|
2022-12-13 20:58:47 +01:00
|
|
|
|
{
|
|
|
|
|
memcpy (rarray->pdata, data, len * sizeof (gpointer));
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 12:42:23 +00:00
|
|
|
|
if (null_terminated && rarray->pdata != NULL)
|
|
|
|
|
rarray->pdata[len] = NULL;
|
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
rarray->len = len;
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-13 18:33:52 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_from_array: (skip)
|
|
|
|
|
* @data: (array length=len) (transfer none) (nullable): an array of pointers,
|
|
|
|
|
* or %NULL for an empty array
|
|
|
|
|
* @len: the number of pointers in @data
|
|
|
|
|
* @copy_func: (nullable): a copy function used to copy every element in the
|
|
|
|
|
* array or %NULL.
|
|
|
|
|
* @copy_func_user_data: user data passed to @copy_func, or %NULL
|
|
|
|
|
* @element_free_func: (nullable): a function to free elements on @array
|
|
|
|
|
* destruction or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray, copying @len pointers from @data, and setting
|
|
|
|
|
* the array’s reference count to 1.
|
|
|
|
|
*
|
|
|
|
|
* This avoids having to manually add each element one by one.
|
|
|
|
|
*
|
|
|
|
|
* If @copy_func is provided, then it is used to copy each element before
|
|
|
|
|
* adding them to the new array. If it is %NULL then the pointers are copied
|
|
|
|
|
* directly.
|
|
|
|
|
*
|
|
|
|
|
* It also sets @element_free_func for freeing each element when the array is
|
|
|
|
|
* destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called
|
|
|
|
|
* with @free_segment set to %TRUE or when removing elements.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if @len is greater than %G_MAXUINT. #GPtrArray
|
|
|
|
|
* stores the length of its data in #guint, which may be shorter than
|
|
|
|
|
* #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_new_from_array (gpointer *data,
|
|
|
|
|
gsize len,
|
|
|
|
|
GCopyFunc copy_func,
|
|
|
|
|
gpointer copy_func_user_data,
|
|
|
|
|
GDestroyNotify element_free_func)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (data != NULL || len == 0, NULL);
|
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
return ptr_array_new_from_array (
|
|
|
|
|
data, len, copy_func, copy_func_user_data, element_free_func, FALSE);
|
|
|
|
|
}
|
2022-12-13 18:33:52 +01:00
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_from_null_terminated_array: (skip)
|
|
|
|
|
* @data: (array zero-terminated=1) (transfer none) (nullable): an array of
|
|
|
|
|
* pointers, %NULL terminated; or %NULL for an empty array
|
|
|
|
|
* @copy_func: (nullable): a copy function used to copy every element in the
|
|
|
|
|
* array or %NULL.
|
|
|
|
|
* @copy_func_user_data: user data passed to @copy_func, or %NULL
|
|
|
|
|
* @element_free_func: (nullable): a function to free elements on @array
|
|
|
|
|
* destruction or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray copying the pointers from @data after having
|
|
|
|
|
* computed the length of it and with a reference count of 1.
|
|
|
|
|
* This avoids having to manually add each element one by one.
|
|
|
|
|
* If @copy_func is provided, then it is used to copy the data in the new
|
|
|
|
|
* array.
|
|
|
|
|
* It also set @element_free_func for freeing each element when the array is
|
|
|
|
|
* destroyed either via g_ptr_array_unref(), when g_ptr_array_free() is called
|
|
|
|
|
* with @free_segment set to %TRUE or when removing elements.
|
|
|
|
|
*
|
|
|
|
|
* Do not use it if the @data has more than %G_MAXUINT elements. #GPtrArray
|
|
|
|
|
* stores the length of its data in #guint, which may be shorter than
|
|
|
|
|
* #gsize.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_new_from_null_terminated_array (gpointer *data,
|
|
|
|
|
GCopyFunc copy_func,
|
|
|
|
|
gpointer copy_func_user_data,
|
|
|
|
|
GDestroyNotify element_free_func)
|
|
|
|
|
{
|
|
|
|
|
gsize len = 0;
|
|
|
|
|
|
|
|
|
|
if (data != NULL)
|
2022-12-13 18:33:52 +01:00
|
|
|
|
{
|
2022-12-13 20:58:47 +01:00
|
|
|
|
for (gsize i = 0; data[i] != NULL; ++i)
|
|
|
|
|
len += 1;
|
2022-12-13 18:33:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 16:07:33 +00:00
|
|
|
|
g_assert (data != NULL || len == 0);
|
2022-12-13 20:58:47 +01:00
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
2022-12-13 18:33:52 +01:00
|
|
|
|
|
2022-12-13 20:58:47 +01:00
|
|
|
|
return ptr_array_new_from_array (
|
|
|
|
|
data, len, copy_func, copy_func_user_data, element_free_func, TRUE);
|
2022-12-13 18:33:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 19:22:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_steal:
|
|
|
|
|
* @array: a #GPtrArray.
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @len: (optional) (out): pointer to retrieve the number of
|
2019-07-30 19:22:05 +02:00
|
|
|
|
* elements of the original array
|
|
|
|
|
*
|
|
|
|
|
* Frees the data in the array and resets the size to zero, while
|
|
|
|
|
* the underlying array is preserved for use elsewhere and returned
|
|
|
|
|
* to the caller.
|
|
|
|
|
*
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
* Note that if the array is %NULL terminated this may still return
|
|
|
|
|
* %NULL if the length of the array was zero and pdata was not yet
|
|
|
|
|
* allocated.
|
|
|
|
|
*
|
2019-07-30 19:22:05 +02:00
|
|
|
|
* Even if set, the #GDestroyNotify function will never be called
|
|
|
|
|
* on the current contents of the array and the caller is
|
|
|
|
|
* responsible for freeing the array elements.
|
|
|
|
|
*
|
2019-10-18 11:45:30 +01:00
|
|
|
|
* An example of use:
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* g_autoptr(GPtrArray) chunk_buffer = g_ptr_array_new_with_free_func (g_bytes_unref);
|
|
|
|
|
*
|
|
|
|
|
* // Some part of your application appends a number of chunks to the pointer array.
|
|
|
|
|
* g_ptr_array_add (chunk_buffer, g_bytes_new_static ("hello", 5));
|
|
|
|
|
* g_ptr_array_add (chunk_buffer, g_bytes_new_static ("world", 5));
|
|
|
|
|
*
|
|
|
|
|
* …
|
|
|
|
|
*
|
|
|
|
|
* // Periodically, the chunks need to be sent as an array-and-length to some
|
|
|
|
|
* // other part of the program.
|
|
|
|
|
* GBytes **chunks;
|
|
|
|
|
* gsize n_chunks;
|
|
|
|
|
*
|
|
|
|
|
* chunks = g_ptr_array_steal (chunk_buffer, &n_chunks);
|
|
|
|
|
* for (gsize i = 0; i < n_chunks; i++)
|
|
|
|
|
* {
|
|
|
|
|
* // Do something with each chunk here, and then free them, since
|
|
|
|
|
* // g_ptr_array_steal() transfers ownership of all the elements and the
|
|
|
|
|
* // array to the caller.
|
|
|
|
|
* …
|
|
|
|
|
*
|
|
|
|
|
* g_bytes_unref (chunks[i]);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* g_free (chunks);
|
|
|
|
|
*
|
|
|
|
|
* // After calling g_ptr_array_steal(), the pointer array can be reused for the
|
|
|
|
|
* // next set of chunks.
|
|
|
|
|
* g_assert (chunk_buffer->len == 0);
|
|
|
|
|
* ]|
|
|
|
|
|
*
|
2022-05-27 14:27:12 +01:00
|
|
|
|
* Returns: (transfer full) (nullable): the element data, which should be
|
|
|
|
|
* freed using g_free(). This may be %NULL if the array doesn’t have any
|
|
|
|
|
* elements (i.e. if `*len` is zero).
|
2019-07-30 19:22:05 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.64
|
|
|
|
|
*/
|
|
|
|
|
gpointer *
|
|
|
|
|
g_ptr_array_steal (GPtrArray *array,
|
|
|
|
|
gsize *len)
|
|
|
|
|
{
|
|
|
|
|
GRealPtrArray *rarray;
|
|
|
|
|
gpointer *segment;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
rarray = (GRealPtrArray *) array;
|
|
|
|
|
segment = (gpointer *) rarray->pdata;
|
|
|
|
|
|
|
|
|
|
if (len != NULL)
|
|
|
|
|
*len = rarray->len;
|
|
|
|
|
|
|
|
|
|
rarray->pdata = NULL;
|
|
|
|
|
rarray->len = 0;
|
|
|
|
|
rarray->alloc = 0;
|
|
|
|
|
return segment;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 12:36:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_copy:
|
|
|
|
|
* @array: #GPtrArray to duplicate
|
|
|
|
|
* @func: (nullable): a copy function used to copy every element in the array
|
|
|
|
|
* @user_data: user data passed to the copy function @func, or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Makes a full (deep) copy of a #GPtrArray.
|
|
|
|
|
*
|
|
|
|
|
* @func, as a #GCopyFunc, takes two arguments, the data to be copied
|
|
|
|
|
* and a @user_data pointer. On common processor architectures, it's safe to
|
|
|
|
|
* pass %NULL as @user_data if the copy function takes only one argument. You
|
|
|
|
|
* may get compiler warnings from this though if compiling with GCC’s
|
|
|
|
|
* `-Wcast-function-type` warning.
|
|
|
|
|
*
|
|
|
|
|
* If @func is %NULL, then only the pointers (and not what they are
|
|
|
|
|
* pointing to) are copied to the new #GPtrArray.
|
|
|
|
|
*
|
2019-07-16 10:15:09 +01:00
|
|
|
|
* The copy of @array will have the same #GDestroyNotify for its elements as
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
* @array. The copy will also be %NULL terminated if (and only if) the source
|
|
|
|
|
* array is.
|
2019-07-16 10:15:09 +01:00
|
|
|
|
*
|
2019-06-19 12:36:06 +02:00
|
|
|
|
* Returns: (transfer full): a deep copy of the initial #GPtrArray.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.62
|
|
|
|
|
**/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_copy (GPtrArray *array,
|
|
|
|
|
GCopyFunc func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *) array;
|
2019-06-19 12:36:06 +02:00
|
|
|
|
GPtrArray *new_array;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array != NULL, NULL);
|
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
new_array = ptr_array_new (0,
|
|
|
|
|
rarray->element_free_func,
|
|
|
|
|
rarray->null_terminated);
|
2019-07-16 10:15:09 +01:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
if (rarray->alloc > 0)
|
2019-06-19 12:36:06 +02:00
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
g_ptr_array_maybe_expand ((GRealPtrArray *) new_array, array->len + rarray->null_terminated);
|
2020-05-07 08:15:44 +02:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
if (array->len > 0)
|
|
|
|
|
{
|
|
|
|
|
if (func != NULL)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < array->len; i++)
|
|
|
|
|
new_array->pdata[i] = func (array->pdata[i], user_data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy (new_array->pdata, array->pdata,
|
|
|
|
|
array->len * sizeof (*array->pdata));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_array->len = array->len;
|
|
|
|
|
}
|
2019-06-19 12:36:06 +02:00
|
|
|
|
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate ((GRealPtrArray *) new_array);
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
}
|
2019-07-16 10:15:57 +01:00
|
|
|
|
|
2019-06-19 12:36:06 +02:00
|
|
|
|
return new_array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_sized_new:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @reserved_size: number of pointers preallocated
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray with @reserved_size pointers preallocated
|
|
|
|
|
* and a reference count of 1. This avoids frequent reallocation, if
|
|
|
|
|
* you are going to add many pointers to the array. Note however that
|
|
|
|
|
* the size of the array is still 0.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: the new #GPtrArray
|
|
|
|
|
*/
|
2020-05-15 17:36:43 +02:00
|
|
|
|
GPtrArray*
|
2000-04-17 10:59:46 +00:00
|
|
|
|
g_ptr_array_sized_new (guint reserved_size)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
return ptr_array_new (reserved_size, NULL, FALSE);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 09:46:20 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_array_copy:
|
|
|
|
|
* @array: A #GArray.
|
|
|
|
|
*
|
|
|
|
|
* Create a shallow copy of a #GArray. If the array elements consist of
|
|
|
|
|
* pointers to data, the pointers are copied but the actual data is not.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer container): A copy of @array.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.62
|
|
|
|
|
**/
|
|
|
|
|
GArray *
|
|
|
|
|
g_array_copy (GArray *array)
|
|
|
|
|
{
|
|
|
|
|
GRealArray *rarray = (GRealArray *) array;
|
|
|
|
|
GRealArray *new_rarray;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (rarray != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
new_rarray =
|
|
|
|
|
(GRealArray *) g_array_sized_new (rarray->zero_terminated, rarray->clear,
|
2021-10-19 15:38:13 +00:00
|
|
|
|
rarray->elt_size, rarray->elt_capacity);
|
2019-05-28 09:46:20 +02:00
|
|
|
|
new_rarray->len = rarray->len;
|
2020-02-27 11:20:26 +00:00
|
|
|
|
if (rarray->len > 0)
|
|
|
|
|
memcpy (new_rarray->data, rarray->data, rarray->len * rarray->elt_size);
|
|
|
|
|
|
|
|
|
|
g_array_zero_terminate (new_rarray);
|
2019-05-28 09:46:20 +02:00
|
|
|
|
|
|
|
|
|
return (GArray *) new_rarray;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_with_free_func:
|
2016-10-28 18:29:02 -07:00
|
|
|
|
* @element_free_func: (nullable): A function to free elements with
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* destroy @array or %NULL
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Creates a new #GPtrArray with a reference count of 1 and use
|
|
|
|
|
* @element_free_func for freeing each element when the array is destroyed
|
|
|
|
|
* either via g_ptr_array_unref(), when g_ptr_array_free() is called with
|
|
|
|
|
* @free_segment set to %TRUE or when removing elements.
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2022-05-27 14:27:12 +01:00
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GPtrArray*
|
2009-04-29 11:15:20 -04:00
|
|
|
|
g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
|
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
return ptr_array_new (0, element_free_func, FALSE);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2011-07-14 10:51:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_full:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @reserved_size: number of pointers preallocated
|
2016-10-28 18:29:02 -07:00
|
|
|
|
* @element_free_func: (nullable): A function to free elements with
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* destroy @array or %NULL
|
2011-07-14 10:51:06 +02:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GPtrArray with @reserved_size pointers preallocated
|
|
|
|
|
* and a reference count of 1. This avoids frequent reallocation, if
|
|
|
|
|
* you are going to add many pointers to the array. Note however that
|
|
|
|
|
* the size of the array is still 0. It also set @element_free_func
|
|
|
|
|
* for freeing each element when the array is destroyed either via
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* g_ptr_array_unref(), when g_ptr_array_free() is called with
|
|
|
|
|
* @free_segment set to %TRUE or when removing elements.
|
2011-07-14 10:51:06 +02:00
|
|
|
|
*
|
2022-05-27 14:27:12 +01:00
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
2011-07-14 10:51:06 +02:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.30
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GPtrArray*
|
2011-07-14 10:51:06 +02:00
|
|
|
|
g_ptr_array_new_full (guint reserved_size,
|
|
|
|
|
GDestroyNotify element_free_func)
|
|
|
|
|
{
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
return ptr_array_new (reserved_size, element_free_func, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_new_null_terminated:
|
|
|
|
|
* @reserved_size: number of pointers preallocated.
|
|
|
|
|
* If @null_terminated is %TRUE, the actually allocated
|
|
|
|
|
* buffer size is @reserved_size plus 1, unless @reserved_size
|
|
|
|
|
* is zero, in which case no initial buffer gets allocated.
|
|
|
|
|
* @element_free_func: (nullable): A function to free elements with
|
|
|
|
|
* destroy @array or %NULL
|
|
|
|
|
* @null_terminated: whether to make the array as %NULL terminated.
|
|
|
|
|
*
|
|
|
|
|
* Like g_ptr_array_new_full() but also allows to set the array to
|
|
|
|
|
* be %NULL terminated. A %NULL terminated pointer array has an
|
|
|
|
|
* additional %NULL pointer after the last element, beyond the
|
|
|
|
|
* current length.
|
|
|
|
|
*
|
|
|
|
|
* #GPtrArray created by other constructors are not automatically %NULL
|
|
|
|
|
* terminated.
|
|
|
|
|
*
|
|
|
|
|
* Note that if the @array's length is zero and currently no
|
|
|
|
|
* data array is allocated, then pdata will still be %NULL.
|
|
|
|
|
* %GPtrArray will only %NULL terminate pdata, if an actual
|
|
|
|
|
* array is allocated. It does not guarantee that an array
|
|
|
|
|
* is always allocated. In other words, if the length is zero,
|
|
|
|
|
* then pdata may either point to a %NULL terminated array of length
|
|
|
|
|
* zero or be %NULL.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): A new #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.74
|
|
|
|
|
*/
|
|
|
|
|
GPtrArray *
|
|
|
|
|
g_ptr_array_new_null_terminated (guint reserved_size,
|
|
|
|
|
GDestroyNotify element_free_func,
|
|
|
|
|
gboolean null_terminated)
|
|
|
|
|
{
|
|
|
|
|
return ptr_array_new (reserved_size, element_free_func, null_terminated);
|
2011-07-14 10:51:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_set_free_func:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GPtrArray
|
2016-10-28 18:29:02 -07:00
|
|
|
|
* @element_free_func: (nullable): A function to free elements with
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* destroy @array or %NULL
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Sets a function for freeing each element when @array is destroyed
|
|
|
|
|
* either via g_ptr_array_unref(), when g_ptr_array_free() is called
|
|
|
|
|
* with @free_segment set to %TRUE or when removing elements.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
void
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_set_free_func (GPtrArray *array,
|
|
|
|
|
GDestroyNotify element_free_func)
|
2009-04-29 11:15:20 -04:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (array);
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
rarray->element_free_func = element_free_func;
|
|
|
|
|
}
|
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_is_null_terminated:
|
|
|
|
|
* @array: the #GPtrArray
|
|
|
|
|
*
|
|
|
|
|
* Gets whether the @array was constructed as %NULL-terminated.
|
|
|
|
|
*
|
|
|
|
|
* This will only return %TRUE for arrays constructed by passing %TRUE to the
|
|
|
|
|
* `null_terminated` argument of g_ptr_array_new_null_terminated(). It will not
|
|
|
|
|
* return %TRUE for normal arrays which have had a %NULL element appended to
|
|
|
|
|
* them.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if the array is made to be %NULL terminated.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.74
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_ptr_array_is_null_terminated (GPtrArray *array)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (array, FALSE);
|
|
|
|
|
|
|
|
|
|
return ((GRealPtrArray *) array)->null_terminated;
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_ref:
|
2012-09-23 11:16:26 -04:00
|
|
|
|
* @array: a #GPtrArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2012-09-23 11:16:26 -04:00
|
|
|
|
* Atomically increments the reference count of @array by one.
|
|
|
|
|
* This function is thread-safe and may be called from any thread.
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2012-09-23 11:16:26 -04:00
|
|
|
|
* Returns: The passed in #GPtrArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2012-09-23 11:16:26 -04:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GPtrArray*
|
2009-04-29 11:15:20 -04:00
|
|
|
|
g_ptr_array_ref (GPtrArray *array)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2010-01-13 18:40:17 +01:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, NULL);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2018-01-17 17:01:23 +00:00
|
|
|
|
g_atomic_ref_count_inc (&rarray->ref_count);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_unref:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GPtrArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Atomically decrements the reference count of @array by one. If the
|
|
|
|
|
* reference count drops to 0, the effect is the same as calling
|
|
|
|
|
* g_ptr_array_free() with @free_segment set to %TRUE. This function
|
2017-08-22 15:58:18 +01:00
|
|
|
|
* is thread-safe and may be called from any thread.
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
void
|
|
|
|
|
g_ptr_array_unref (GPtrArray *array)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
|
|
|
|
|
2010-01-13 18:40:17 +01:00
|
|
|
|
g_return_if_fail (array);
|
2011-05-28 21:41:08 -04:00
|
|
|
|
|
2018-01-17 17:01:23 +00:00
|
|
|
|
if (g_atomic_ref_count_dec (&rarray->ref_count))
|
2011-12-14 16:53:24 +00:00
|
|
|
|
ptr_array_free (array, FREE_SEGMENT);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_free:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @free_seg: if %TRUE the actual pointer array is freed as well
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
|
|
|
|
|
* it frees the memory block holding the elements as well. Pass %FALSE
|
|
|
|
|
* if you want to free the #GPtrArray wrapper but preserve the
|
|
|
|
|
* underlying array for use elsewhere. If the reference count of @array
|
|
|
|
|
* is greater than one, the #GPtrArray wrapper is preserved but the
|
|
|
|
|
* size of @array will be set to zero.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* If array contents point to dynamically-allocated memory, they should
|
|
|
|
|
* be freed separately if @free_seg is %TRUE and no #GDestroyNotify
|
|
|
|
|
* function has been set for @array.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
* Note that if the array is %NULL terminated and @free_seg is %FALSE
|
|
|
|
|
* then this will always return an allocated %NULL terminated buffer.
|
|
|
|
|
* If pdata is previously %NULL, a new buffer will be allocated.
|
|
|
|
|
*
|
2017-08-22 15:58:18 +01:00
|
|
|
|
* This function is not thread-safe. If using a #GPtrArray from multiple
|
|
|
|
|
* threads, use only the atomic g_ptr_array_ref() and g_ptr_array_unref()
|
|
|
|
|
* functions.
|
|
|
|
|
*
|
2020-12-11 23:19:18 +00:00
|
|
|
|
* Returns: (transfer full) (nullable): the pointer array if @free_seg is
|
|
|
|
|
* %FALSE, otherwise %NULL. The pointer array should be freed using g_free().
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-08-17 21:37:18 +00:00
|
|
|
|
gpointer*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_free (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gboolean free_segment)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2011-12-14 16:53:24 +00:00
|
|
|
|
ArrayFreeFlags flags;
|
2000-08-17 21:37:18 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_val_if_fail (rarray, NULL);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
flags = (free_segment ? FREE_SEGMENT : 0);
|
|
|
|
|
|
2014-01-31 14:56:10 -05:00
|
|
|
|
/* if others are holding a reference, preserve the wrapper but
|
|
|
|
|
* do free/return the data
|
2022-06-07 11:05:26 +01:00
|
|
|
|
*
|
|
|
|
|
* Coverity doesn’t understand this and assumes it’s a leak, so comment this
|
|
|
|
|
* out.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2022-06-07 11:05:26 +01:00
|
|
|
|
#ifndef __COVERITY__
|
2018-01-17 17:01:23 +00:00
|
|
|
|
if (!g_atomic_ref_count_dec (&rarray->ref_count))
|
2011-12-14 16:53:24 +00:00
|
|
|
|
flags |= PRESERVE_WRAPPER;
|
2022-06-07 11:05:26 +01:00
|
|
|
|
#endif
|
2011-12-14 16:53:24 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return ptr_array_free (array, flags);
|
2011-12-14 16:53:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gpointer *
|
2014-02-01 20:45:25 -05:00
|
|
|
|
ptr_array_free (GPtrArray *array,
|
2011-12-14 16:53:24 +00:00
|
|
|
|
ArrayFreeFlags flags)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2011-12-14 16:53:24 +00:00
|
|
|
|
gpointer *segment;
|
2009-04-29 11:15:20 -04:00
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
if (flags & FREE_SEGMENT)
|
2000-08-17 21:37:18 +00:00
|
|
|
|
{
|
2018-04-21 05:07:49 +07:00
|
|
|
|
/* Data here is stolen and freed manually. It is an
|
|
|
|
|
* error to attempt to access the array data (including
|
|
|
|
|
* mutating the array bounds) during destruction).
|
|
|
|
|
*
|
|
|
|
|
* https://bugzilla.gnome.org/show_bug.cgi?id=769064
|
|
|
|
|
*/
|
|
|
|
|
gpointer *stolen_pdata = g_steal_pointer (&rarray->pdata);
|
2014-02-01 20:45:25 -05:00
|
|
|
|
if (rarray->element_free_func != NULL)
|
2018-04-21 05:07:49 +07:00
|
|
|
|
{
|
2020-05-07 08:15:44 +02:00
|
|
|
|
guint i;
|
|
|
|
|
|
2018-04-21 05:07:49 +07:00
|
|
|
|
for (i = 0; i < rarray->len; ++i)
|
|
|
|
|
rarray->element_free_func (stolen_pdata[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_free (stolen_pdata);
|
2000-08-17 21:37:18 +00:00
|
|
|
|
segment = NULL;
|
|
|
|
|
}
|
|
|
|
|
else
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
{
|
|
|
|
|
segment = rarray->pdata;
|
|
|
|
|
if (!segment && rarray->null_terminated)
|
|
|
|
|
segment = (gpointer *) g_new0 (char *, 1);
|
|
|
|
|
}
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2011-12-14 16:53:24 +00:00
|
|
|
|
if (flags & PRESERVE_WRAPPER)
|
2009-04-29 11:15:20 -04:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->pdata = NULL;
|
|
|
|
|
rarray->len = 0;
|
|
|
|
|
rarray->alloc = 0;
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_slice_free1 (sizeof (GRealPtrArray), rarray);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
2000-08-17 21:37:18 +00:00
|
|
|
|
|
|
|
|
|
return segment;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_ptr_array_maybe_expand (GRealPtrArray *array,
|
2019-01-26 12:21:23 +01:00
|
|
|
|
guint len)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2021-11-25 14:11:29 +02:00
|
|
|
|
guint max_len;
|
|
|
|
|
|
|
|
|
|
/* The maximum array length is derived from following constraints:
|
|
|
|
|
* - The number of bytes must fit into a gsize / 2.
|
|
|
|
|
* - The number of elements must fit into guint.
|
|
|
|
|
*/
|
|
|
|
|
max_len = MIN (G_MAXSIZE / 2 / sizeof (gpointer), G_MAXUINT);
|
|
|
|
|
|
2018-07-23 21:05:11 -07:00
|
|
|
|
/* Detect potential overflow */
|
2021-11-25 14:11:29 +02:00
|
|
|
|
if G_UNLIKELY ((max_len - array->len) < len)
|
2018-07-23 21:05:11 -07:00
|
|
|
|
g_error ("adding %u to array would overflow", len);
|
|
|
|
|
|
1998-06-12 09:38:32 +00:00
|
|
|
|
if ((array->len + len) > array->alloc)
|
|
|
|
|
{
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
guint old_alloc = array->alloc;
|
2022-01-18 13:45:13 +00:00
|
|
|
|
gsize want_alloc = g_nearest_pow (sizeof (gpointer) * (array->len + len));
|
|
|
|
|
want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
|
|
|
|
|
array->alloc = MIN (want_alloc / sizeof (gpointer), G_MAXUINT);
|
|
|
|
|
array->pdata = g_realloc (array->pdata, want_alloc);
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
|
|
|
|
for ( ; old_alloc < array->alloc; old_alloc++)
|
|
|
|
|
array->pdata [old_alloc] = NULL;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_set_size:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @length: the new length of the pointer array
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sets the size of the array. When making the array larger,
|
|
|
|
|
* newly-added elements will be set to %NULL. When making it smaller,
|
|
|
|
|
* if @array has a non-%NULL #GDestroyNotify function then it will be
|
|
|
|
|
* called for the removed elements.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
void
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_set_size (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gint length)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2018-07-10 14:15:42 +02:00
|
|
|
|
guint length_unsigned;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_if_fail (rarray);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
|
2018-07-10 14:15:42 +02:00
|
|
|
|
g_return_if_fail (length >= 0);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2018-07-10 14:15:42 +02:00
|
|
|
|
length_unsigned = (guint) length;
|
|
|
|
|
|
|
|
|
|
if (length_unsigned > rarray->len)
|
2000-03-24 15:36:03 +00:00
|
|
|
|
{
|
2018-07-10 14:15:42 +02:00
|
|
|
|
guint i;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (rarray->null_terminated) &&
|
|
|
|
|
length_unsigned - rarray->len > G_MAXUINT - 1)
|
|
|
|
|
g_error ("array would overflow");
|
|
|
|
|
|
|
|
|
|
g_ptr_array_maybe_expand (rarray, (length_unsigned - rarray->len) + rarray->null_terminated);
|
|
|
|
|
|
|
|
|
|
/* This is not
|
2000-03-24 15:36:03 +00:00
|
|
|
|
* memset (array->pdata + array->len, 0,
|
2018-07-10 14:15:42 +02:00
|
|
|
|
* sizeof (gpointer) * (length_unsigned - array->len));
|
2000-03-24 15:36:03 +00:00
|
|
|
|
* to make it really portable. Remember (void*)NULL needn't be
|
|
|
|
|
* bitwise zero. It of course is silly not to use memset (..,0,..).
|
|
|
|
|
*/
|
2018-07-10 14:15:42 +02:00
|
|
|
|
for (i = rarray->len; i < length_unsigned; i++)
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->pdata[i] = NULL;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
|
|
|
|
|
rarray->len = length_unsigned;
|
|
|
|
|
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (rarray);
|
2000-03-24 15:36:03 +00:00
|
|
|
|
}
|
2018-07-10 14:15:42 +02:00
|
|
|
|
else if (length_unsigned < rarray->len)
|
|
|
|
|
g_ptr_array_remove_range (array, length_unsigned, rarray->len - length_unsigned);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:46:05 +01:00
|
|
|
|
static gpointer
|
|
|
|
|
ptr_array_remove_index (GPtrArray *array,
|
|
|
|
|
guint index_,
|
2018-04-19 14:54:41 +01:00
|
|
|
|
gboolean fast,
|
|
|
|
|
gboolean free_element)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2018-04-19 14:46:05 +01:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *) array;
|
1998-09-02 07:44:02 +00:00
|
|
|
|
gpointer result;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_val_if_fail (rarray, NULL);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_val_if_fail (index_ < rarray->len, NULL);
|
1998-09-02 07:44:02 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
result = rarray->pdata[index_];
|
2018-04-19 14:46:05 +01:00
|
|
|
|
|
2018-04-19 14:54:41 +01:00
|
|
|
|
if (rarray->element_free_func != NULL && free_element)
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->element_free_func (rarray->pdata[index_]);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
|
2018-04-19 14:46:05 +01:00
|
|
|
|
if (index_ != rarray->len - 1 && !fast)
|
2014-02-01 20:45:25 -05:00
|
|
|
|
memmove (rarray->pdata + index_, rarray->pdata + index_ + 1,
|
|
|
|
|
sizeof (gpointer) * (rarray->len - index_ - 1));
|
2018-04-19 14:46:05 +01:00
|
|
|
|
else if (index_ != rarray->len - 1)
|
|
|
|
|
rarray->pdata[index_] = rarray->pdata[rarray->len - 1];
|
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->len -= 1;
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
if (rarray->null_terminated || G_UNLIKELY (g_mem_gc_friendly))
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->pdata[rarray->len] = NULL;
|
Add configure test for garbage collector friendliness for GLib. If
2000-04-17 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* configure.in, acconfig.h: Add configure test for garbage
collector friendliness for GLib. If enabled, ENABLE_GC_FRIENDLY
will be defined.
* garray.c, ghash.c, glist.c, gmain.c, gmem.c, gnode.c, gqueue.c,
gslist.c, gtree.c: If ENABLE_GC_FRIENDLY is defined, NULLify all
memory released by the user, but cached by GLib. This lets a
garbage collector have a more correct view of the actually used
memory.
2000-04-17 13:23:27 +00:00
|
|
|
|
|
1998-11-03 14:52:25 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:46:05 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_remove_index:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index of the pointer to remove
|
|
|
|
|
*
|
|
|
|
|
* Removes the pointer at the given index from the pointer array.
|
|
|
|
|
* The following elements are moved down one place. If @array has
|
|
|
|
|
* a non-%NULL #GDestroyNotify function it is called for the removed
|
2018-04-19 14:50:35 +01:00
|
|
|
|
* element. If so, the return value from this function will potentially point
|
|
|
|
|
* to freed memory (depending on the #GDestroyNotify implementation).
|
2018-04-19 14:46:05 +01:00
|
|
|
|
*
|
2018-04-19 14:50:35 +01:00
|
|
|
|
* Returns: (nullable): the pointer which was removed
|
2018-04-19 14:46:05 +01:00
|
|
|
|
*/
|
|
|
|
|
gpointer
|
|
|
|
|
g_ptr_array_remove_index (GPtrArray *array,
|
|
|
|
|
guint index_)
|
|
|
|
|
{
|
2018-04-19 14:54:41 +01:00
|
|
|
|
return ptr_array_remove_index (array, index_, FALSE, TRUE);
|
2018-04-19 14:46:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_remove_index_fast:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index of the pointer to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Removes the pointer at the given index from the pointer array.
|
|
|
|
|
* The last element in the array is used to fill in the space, so
|
|
|
|
|
* this function does not preserve the order of the array. But it
|
|
|
|
|
* is faster than g_ptr_array_remove_index(). If @array has a non-%NULL
|
2018-04-19 14:50:35 +01:00
|
|
|
|
* #GDestroyNotify function it is called for the removed element. If so, the
|
|
|
|
|
* return value from this function will potentially point to freed memory
|
|
|
|
|
* (depending on the #GDestroyNotify implementation).
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2018-04-19 14:50:35 +01:00
|
|
|
|
* Returns: (nullable): the pointer which was removed
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
1998-11-03 14:52:25 +00:00
|
|
|
|
gpointer
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_remove_index_fast (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
guint index_)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
2018-04-19 14:54:41 +01:00
|
|
|
|
return ptr_array_remove_index (array, index_, TRUE, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_steal_index:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index of the pointer to steal
|
|
|
|
|
*
|
|
|
|
|
* Removes the pointer at the given index from the pointer array.
|
|
|
|
|
* The following elements are moved down one place. The #GDestroyNotify for
|
|
|
|
|
* @array is *not* called on the removed element; ownership is transferred to
|
|
|
|
|
* the caller of this function.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full) (nullable): the pointer which was removed
|
|
|
|
|
* Since: 2.58
|
|
|
|
|
*/
|
|
|
|
|
gpointer
|
|
|
|
|
g_ptr_array_steal_index (GPtrArray *array,
|
|
|
|
|
guint index_)
|
|
|
|
|
{
|
|
|
|
|
return ptr_array_remove_index (array, index_, FALSE, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_steal_index_fast:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index of the pointer to steal
|
|
|
|
|
*
|
|
|
|
|
* Removes the pointer at the given index from the pointer array.
|
|
|
|
|
* The last element in the array is used to fill in the space, so
|
|
|
|
|
* this function does not preserve the order of the array. But it
|
|
|
|
|
* is faster than g_ptr_array_steal_index(). The #GDestroyNotify for @array is
|
|
|
|
|
* *not* called on the removed element; ownership is transferred to the caller
|
|
|
|
|
* of this function.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full) (nullable): the pointer which was removed
|
|
|
|
|
* Since: 2.58
|
|
|
|
|
*/
|
|
|
|
|
gpointer
|
|
|
|
|
g_ptr_array_steal_index_fast (GPtrArray *array,
|
|
|
|
|
guint index_)
|
|
|
|
|
{
|
|
|
|
|
return ptr_array_remove_index (array, index_, TRUE, FALSE);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_remove_range:
|
2013-11-23 21:10:06 -05:00
|
|
|
|
* @array: a @GPtrArray
|
|
|
|
|
* @index_: the index of the first pointer to remove
|
|
|
|
|
* @length: the number of pointers to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the given number of pointers starting at the given index
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* from a #GPtrArray. The following elements are moved to close the
|
|
|
|
|
* gap. If @array has a non-%NULL #GDestroyNotify function it is
|
|
|
|
|
* called for the removed elements.
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2013-11-23 21:10:06 -05:00
|
|
|
|
* Returns: the @array
|
|
|
|
|
*
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* Since: 2.4
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GPtrArray*
|
|
|
|
|
g_ptr_array_remove_range (GPtrArray *array,
|
2003-07-26 08:03:16 +00:00
|
|
|
|
guint index_,
|
|
|
|
|
guint length)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2020-05-07 08:33:57 +02:00
|
|
|
|
guint i;
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_val_if_fail (rarray != NULL, NULL);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
|
2016-03-08 18:59:34 +00:00
|
|
|
|
g_return_val_if_fail (index_ <= rarray->len, NULL);
|
2024-02-02 20:33:51 +01:00
|
|
|
|
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
g_return_val_if_fail (length == 0 || index_ + length <= rarray->len, NULL);
|
|
|
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
|
return array;
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
if (rarray->element_free_func != NULL)
|
2009-04-29 11:15:20 -04:00
|
|
|
|
{
|
2020-05-07 08:33:57 +02:00
|
|
|
|
for (i = index_; i < index_ + length; i++)
|
|
|
|
|
rarray->element_free_func (rarray->pdata[i]);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
if (index_ + length != rarray->len)
|
2009-04-29 11:15:20 -04:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
memmove (&rarray->pdata[index_],
|
|
|
|
|
&rarray->pdata[index_ + length],
|
|
|
|
|
(rarray->len - (index_ + length)) * sizeof (gpointer));
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->len -= length;
|
2006-01-25 15:51:43 +00:00
|
|
|
|
if (G_UNLIKELY (g_mem_gc_friendly))
|
|
|
|
|
{
|
|
|
|
|
for (i = 0; i < length; i++)
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->pdata[rarray->len + i] = NULL;
|
2006-01-25 15:51:43 +00:00
|
|
|
|
}
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
else
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (rarray);
|
2013-11-23 21:10:06 -05:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return array;
|
2003-07-26 08:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_remove:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @data: the pointer to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the first occurrence of the given pointer from the pointer
|
|
|
|
|
* array. The following elements are moved down one place. If @array
|
|
|
|
|
* has a non-%NULL #GDestroyNotify function it is called for the
|
|
|
|
|
* removed element.
|
|
|
|
|
*
|
|
|
|
|
* It returns %TRUE if the pointer was removed, or %FALSE if the
|
|
|
|
|
* pointer was not found.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: %TRUE if the pointer is removed, %FALSE if the pointer
|
|
|
|
|
* is not found in the array
|
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
gboolean
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_remove (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gpointer data)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
|
guint i;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array, FALSE);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_val_if_fail (array->len == 0 || (array->len != 0 && array->pdata != NULL), FALSE);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < array->len; i += 1)
|
|
|
|
|
{
|
|
|
|
|
if (array->pdata[i] == data)
|
2014-01-31 14:56:10 -05:00
|
|
|
|
{
|
2014-02-02 09:28:31 +00:00
|
|
|
|
g_ptr_array_remove_index (array, i);
|
2014-01-31 14:56:10 -05:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_remove_fast:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @data: the pointer to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the first occurrence of the given pointer from the pointer
|
|
|
|
|
* array. The last element in the array is used to fill in the space,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* so this function does not preserve the order of the array. But it
|
|
|
|
|
* is faster than g_ptr_array_remove(). If @array has a non-%NULL
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* #GDestroyNotify function it is called for the removed element.
|
|
|
|
|
*
|
|
|
|
|
* It returns %TRUE if the pointer was removed, or %FALSE if the
|
|
|
|
|
* pointer was not found.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: %TRUE if the pointer was found in the array
|
|
|
|
|
*/
|
1998-11-03 14:52:25 +00:00
|
|
|
|
gboolean
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_remove_fast (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gpointer data)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
Changes for 64-bit cleanliness, loosely based on patch from Mark Murnane.
Wed Jun 20 12:00:54 2001 Owen Taylor <otaylor@redhat.com>
Changes for 64-bit cleanliness, loosely based on patch
from Mark Murnane.
* gconvert.c (g_convert/g_convert_with_fallback): Remove
workarounds for since-fixed GNU libc bugs. Minor
doc fix.
* gconvert.[ch]: Change gint to gsize/gssize as
appropriate.
* gconvert.c (g_locale/filename_to/from_utf8): Fix incorrect
computation of bytes_read / bytes_written.
* gfileutils.[ch] (g_file_get_contents): Make length
out parameter 'gsize *len'.
* ghook.c (g_hook_compare_ids): Don't compare a
and b as 'a - b'.
* gmacros.h (GSIZE_TO_POINTER): Add GPOINTER_TO_SIZE,
GSIZE_TO_POINTER.
* gmain.c (g_timeout_prepare): Rewrite to avoid
overflows. (Fixes bug when system clock skews
backwards more than 24 days.)
* gmarkup.[ch]: Make lengths passed to callbacks
gsize, length for g_markup_parse-context_parse(),
g_markup_escape_text() gssize.
* gmessages.[ch] (g_printf_string_upper_bound): Change
return value to gsize.
* gmessages.c (printf_string_upper_bound): Remove
a ridiculous use of 'inline' on a 300 line function.
* gstring.[ch]: Represent size of string as a gsize,
not gint. Make parameters to functions take gsize,
or gssize where -1 is allowed.
* gstring.c (g_string_erase): Make
g_string_erase (string, pos, -1) a synonym for
g_string_truncate for consistency with other G*
APIs.
* gstrfuncs.[ch]: Make all functions taking a string
length, take a gsize, or gssize if -1 is allowed.
(g_strstr_len, g_strrstr_len). Also fix some boundary
conditions in g_str[r]str[_len].
* gutf8.c tests/unicode-encoding.c: Make parameters that
are byte lengths gsize, gssize as appropriate. Make
character offsets, other counts, glong.
* gasyncqueue.c gcompletion.c
timeloop.c timeloop-basic.c gutils.c gspawn.c.
Small 64 bit cleanliness fixups.
* glist.c (g_list_sort2, g_list_sort_real): Fix functions
that should have been static.
* gdate.c (g_date_fill_parse_tokens): Fix extra
declaration that was shadowing another.
* tests/module-test.c: Include string.h
Mon Jun 18 15:43:29 2001 Owen Taylor <otaylor@redhat.com>
* gutf8.c (g_get_charset): Make argument
G_CONST_RETURN char **.
2001-06-23 13:55:09 +00:00
|
|
|
|
guint i;
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_val_if_fail (rarray, FALSE);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), FALSE);
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
for (i = 0; i < rarray->len; i += 1)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
if (rarray->pdata[i] == data)
|
2014-01-31 14:56:10 -05:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_remove_index_fast (array, i);
|
2014-01-31 14:56:10 -05:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
1998-11-03 14:52:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_add:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @data: the pointer to add
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Adds a pointer to the end of the pointer array. The array will grow
|
|
|
|
|
* in size automatically if necessary.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
void
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_add (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
gpointer data)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_if_fail (rarray);
|
2018-04-21 05:07:49 +07:00
|
|
|
|
g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->pdata[rarray->len++] = data;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (rarray);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 12:40:27 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_extend:
|
|
|
|
|
* @array_to_extend: a #GPtrArray.
|
|
|
|
|
* @array: (transfer none): a #GPtrArray to add to the end of @array_to_extend.
|
|
|
|
|
* @func: (nullable): a copy function used to copy every element in the array
|
|
|
|
|
* @user_data: user data passed to the copy function @func, or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Adds all pointers of @array to the end of the array @array_to_extend.
|
|
|
|
|
* The array will grow in size automatically if needed. @array_to_extend is
|
|
|
|
|
* modified in-place.
|
|
|
|
|
*
|
|
|
|
|
* @func, as a #GCopyFunc, takes two arguments, the data to be copied
|
|
|
|
|
* and a @user_data pointer. On common processor architectures, it's safe to
|
|
|
|
|
* pass %NULL as @user_data if the copy function takes only one argument. You
|
|
|
|
|
* may get compiler warnings from this though if compiling with GCC’s
|
|
|
|
|
* `-Wcast-function-type` warning.
|
|
|
|
|
*
|
|
|
|
|
* If @func is %NULL, then only the pointers (and not what they are
|
|
|
|
|
* pointing to) are copied to the new #GPtrArray.
|
|
|
|
|
*
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
* Whether @array_to_extend is %NULL terminated stays unchanged by this function.
|
|
|
|
|
*
|
2019-06-19 12:40:27 +02:00
|
|
|
|
* Since: 2.62
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_ptr_array_extend (GPtrArray *array_to_extend,
|
|
|
|
|
GPtrArray *array,
|
|
|
|
|
GCopyFunc func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
GRealPtrArray *rarray_to_extend = (GRealPtrArray *) array_to_extend;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (array_to_extend != NULL);
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
if (array->len == 0u)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (G_UNLIKELY (array->len == G_MAXUINT) &&
|
|
|
|
|
rarray_to_extend->null_terminated)
|
|
|
|
|
g_error ("adding %u to array would overflow", array->len);
|
|
|
|
|
|
|
|
|
|
g_ptr_array_maybe_expand (rarray_to_extend, array->len + rarray_to_extend->null_terminated);
|
2019-06-19 12:40:27 +02:00
|
|
|
|
|
|
|
|
|
if (func != NULL)
|
|
|
|
|
{
|
2020-05-07 08:15:44 +02:00
|
|
|
|
guint i;
|
|
|
|
|
|
2019-06-19 12:40:27 +02:00
|
|
|
|
for (i = 0; i < array->len; i++)
|
|
|
|
|
rarray_to_extend->pdata[i + rarray_to_extend->len] =
|
|
|
|
|
func (array->pdata[i], user_data);
|
|
|
|
|
}
|
2019-10-24 11:53:23 +01:00
|
|
|
|
else if (array->len > 0)
|
2019-06-19 12:40:27 +02:00
|
|
|
|
{
|
|
|
|
|
memcpy (rarray_to_extend->pdata + rarray_to_extend->len, array->pdata,
|
|
|
|
|
array->len * sizeof (*array->pdata));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rarray_to_extend->len += array->len;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (rarray_to_extend);
|
2019-06-19 12:40:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-19 12:42:08 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_extend_and_steal:
|
|
|
|
|
* @array_to_extend: (transfer none): a #GPtrArray.
|
|
|
|
|
* @array: (transfer container): a #GPtrArray to add to the end of
|
|
|
|
|
* @array_to_extend.
|
|
|
|
|
*
|
|
|
|
|
* Adds all the pointers in @array to the end of @array_to_extend, transferring
|
|
|
|
|
* ownership of each element from @array to @array_to_extend and modifying
|
|
|
|
|
* @array_to_extend in-place. @array is then freed.
|
|
|
|
|
*
|
|
|
|
|
* As with g_ptr_array_free(), @array will be destroyed if its reference count
|
|
|
|
|
* is 1. If its reference count is higher, it will be decremented and the
|
|
|
|
|
* length of @array set to zero.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.62
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
g_ptr_array_extend_and_steal (GPtrArray *array_to_extend,
|
|
|
|
|
GPtrArray *array)
|
|
|
|
|
{
|
|
|
|
|
gpointer *pdata;
|
|
|
|
|
|
|
|
|
|
g_ptr_array_extend (array_to_extend, array, NULL, NULL);
|
|
|
|
|
|
|
|
|
|
/* Get rid of @array without triggering the GDestroyNotify attached
|
|
|
|
|
* to the elements moved from @array to @array_to_extend. */
|
|
|
|
|
pdata = g_steal_pointer (&array->pdata);
|
|
|
|
|
array->len = 0;
|
2020-05-07 08:43:14 +02:00
|
|
|
|
((GRealPtrArray *) array)->alloc = 0;
|
2019-06-19 12:42:08 +02:00
|
|
|
|
g_ptr_array_unref (array);
|
|
|
|
|
g_free (pdata);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 14:34:20 +09:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_insert:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @index_: the index to place the new element at, or -1 to append
|
2013-11-27 14:34:20 +09:00
|
|
|
|
* @data: the pointer to add.
|
|
|
|
|
*
|
|
|
|
|
* Inserts an element into the pointer array at the given index. The
|
|
|
|
|
* array will grow in size automatically if necessary.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.40
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2013-11-27 14:34:20 +09:00
|
|
|
|
void
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_ptr_array_insert (GPtrArray *array,
|
2013-11-27 14:34:20 +09:00
|
|
|
|
gint index_,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GRealPtrArray *rarray = (GRealPtrArray *)array;
|
2024-06-28 15:25:30 +01:00
|
|
|
|
guint real_index;
|
2013-11-27 14:34:20 +09:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_return_if_fail (rarray);
|
2013-11-27 14:34:20 +09:00
|
|
|
|
g_return_if_fail (index_ >= -1);
|
2024-06-28 15:25:30 +01:00
|
|
|
|
g_return_if_fail (index_ < 0 || (guint) index_ <= rarray->len);
|
2013-11-27 14:34:20 +09:00
|
|
|
|
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
|
2013-11-27 14:34:20 +09:00
|
|
|
|
|
2024-06-28 15:25:30 +01:00
|
|
|
|
real_index = (index_ >= 0) ? (guint) index_ : rarray->len;
|
2013-11-27 14:34:20 +09:00
|
|
|
|
|
2024-06-28 15:25:30 +01:00
|
|
|
|
if (real_index < rarray->len)
|
|
|
|
|
memmove (&(rarray->pdata[real_index + 1]),
|
|
|
|
|
&(rarray->pdata[real_index]),
|
|
|
|
|
(rarray->len - real_index) * sizeof (gpointer));
|
2013-11-27 14:34:20 +09:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
rarray->len++;
|
2024-06-28 15:25:30 +01:00
|
|
|
|
rarray->pdata[real_index] = data;
|
array: add support for %NULL termination in GPtrArray
GArray supports a "zero_terminated" flag, but GPtrArray doesn't.
This is odd, because especially for a pointer array it makes sense
to have a %NULL sentinel. This would be for example useful to track
or construct a strv array with a GPtrArray.
As workaround for this missing feature you could use a GArray instead
(ugly) or to explicitly add the %NULL element. However the latter increases
the "len" of the array, which can be problematic if you want to still use
the GPtrArray for other purposes.
Add API for marking a GPtrArray as %NULL terminated. In that case, the
API will ensure that there is always a valid %NULL sentinel after the
array. Note that the API does not enforce that a %NULL terminated API
actually has any data allocated. That means, even with a %NULL terminated
array, pdata can still be %NULL (only if len is zero).
Add g_ptr_array_new_null_terminated() constructor. The null-terminated flag
cannot be cleared. Once the GPtrArray is flagged to be %NULL terminated, it
sticks. The purpose is that once a user checks whether a GPtrArray instance
is safe to be treated as a %NULL terminated array, the decision does
not need to be re-evaluated.
Also add a g_ptr_array_is_null_terminated(). That is useful because it
allows you to check whether a GPtrArray created by somebody else is safe
to use as a %NULL terminated array. Since there is no API to make an
array not %NULL terminated anymore, this is not error prone.
The new flag is tracked as a guint8 in GRealPtrArray. On common 64 bit
architectures this does not increase the size of the struct as it fits
in an existing hole. Note that this is not a bitfield because it's
probably more efficient to access the entire guint8. However, there is
still a 3 bytes hole (on common 32 and 64 architectures), so if we need
to add more flags in the future, we still have space for 24 bits,
despite the new flag not being a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
Includes some tweaks and additional tests by Philip Withnall.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
2020-05-06 19:35:37 +02:00
|
|
|
|
|
2022-06-07 13:31:52 +01:00
|
|
|
|
ptr_array_maybe_null_terminate (rarray);
|
2013-11-27 14:34:20 +09:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 11:40:25 +01:00
|
|
|
|
/* Please keep this doc-comment in sync with pointer_array_sort_example()
|
|
|
|
|
* in glib/tests/array-test.c */
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_sort:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @compare_func: comparison function
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sorts the array, using @compare_func which should be a qsort()-style
|
|
|
|
|
* comparison function (returns less than zero for first arg is less
|
2022-12-19 23:26:58 +01:00
|
|
|
|
* than second arg, zero for equal, greater than zero if first arg is
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* greater than second arg).
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Note that the comparison function for g_ptr_array_sort() doesn't
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* take the pointers from the array as arguments, it takes pointers to
|
2022-12-20 00:14:18 +01:00
|
|
|
|
* the pointers in the array.
|
|
|
|
|
*
|
2023-07-11 22:42:33 +08:00
|
|
|
|
* Use g_ptr_array_sort_values() if you want to use normal
|
2022-12-20 00:14:18 +01:00
|
|
|
|
* #GCompareFuncs, otherwise here is a full example of use:
|
2019-08-01 10:30:01 +02:00
|
|
|
|
*
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* typedef struct
|
|
|
|
|
* {
|
|
|
|
|
* gchar *name;
|
|
|
|
|
* gint size;
|
|
|
|
|
* } FileListEntry;
|
|
|
|
|
*
|
|
|
|
|
* static gint
|
|
|
|
|
* sort_filelist (gconstpointer a, gconstpointer b)
|
|
|
|
|
* {
|
|
|
|
|
* const FileListEntry *entry1 = *((FileListEntry **) a);
|
|
|
|
|
* const FileListEntry *entry2 = *((FileListEntry **) b);
|
|
|
|
|
*
|
|
|
|
|
* return g_ascii_strcasecmp (entry1->name, entry2->name);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* …
|
|
|
|
|
* g_autoptr (GPtrArray) file_list = NULL;
|
|
|
|
|
*
|
|
|
|
|
* // initialize file_list array and load with many FileListEntry entries
|
|
|
|
|
* ...
|
|
|
|
|
* // now sort it with
|
2019-10-24 11:39:12 +01:00
|
|
|
|
* g_ptr_array_sort (file_list, sort_filelist);
|
2019-08-01 10:30:01 +02:00
|
|
|
|
* ]|
|
2012-03-14 21:17:47 +01:00
|
|
|
|
*
|
|
|
|
|
* This is guaranteed to be a stable sort since version 2.32.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_ptr_array_sort (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareFunc compare_func)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
2012-03-14 21:17:47 +01:00
|
|
|
|
/* Don't use qsort as we want a guaranteed stable sort */
|
2020-12-09 13:57:24 +01:00
|
|
|
|
if (array->len > 0)
|
2024-06-28 15:22:33 +01:00
|
|
|
|
g_sort_array (array->pdata,
|
|
|
|
|
array->len,
|
|
|
|
|
sizeof (gpointer),
|
|
|
|
|
(GCompareDataFunc) compare_func,
|
|
|
|
|
NULL);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 11:40:25 +01:00
|
|
|
|
/* Please keep this doc-comment in sync with
|
|
|
|
|
* pointer_array_sort_with_data_example() in glib/tests/array-test.c */
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_sort_with_data:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @compare_func: comparison function
|
|
|
|
|
* @user_data: data to pass to @compare_func
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Like g_ptr_array_sort(), but the comparison function has an extra
|
|
|
|
|
* user data argument.
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Note that the comparison function for g_ptr_array_sort_with_data()
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* doesn't take the pointers from the array as arguments, it takes
|
2022-12-20 00:14:18 +01:00
|
|
|
|
* pointers to the pointers in the array.
|
|
|
|
|
*
|
2023-07-11 22:42:33 +08:00
|
|
|
|
* Use g_ptr_array_sort_values_with_data() if you want to use normal
|
2022-12-20 00:14:18 +01:00
|
|
|
|
* #GCompareDataFuncs, otherwise here is a full example of use:
|
2019-08-01 10:30:01 +02:00
|
|
|
|
*
|
|
|
|
|
* |[<!-- language="C" -->
|
|
|
|
|
* typedef enum { SORT_NAME, SORT_SIZE } SortMode;
|
|
|
|
|
*
|
|
|
|
|
* typedef struct
|
|
|
|
|
* {
|
|
|
|
|
* gchar *name;
|
|
|
|
|
* gint size;
|
|
|
|
|
* } FileListEntry;
|
|
|
|
|
*
|
|
|
|
|
* static gint
|
|
|
|
|
* sort_filelist (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
|
* {
|
|
|
|
|
* gint order;
|
2019-10-24 11:40:00 +01:00
|
|
|
|
* const SortMode sort_mode = GPOINTER_TO_INT (user_data);
|
2019-08-01 10:30:01 +02:00
|
|
|
|
* const FileListEntry *entry1 = *((FileListEntry **) a);
|
|
|
|
|
* const FileListEntry *entry2 = *((FileListEntry **) b);
|
|
|
|
|
*
|
2019-10-24 11:40:00 +01:00
|
|
|
|
* switch (sort_mode)
|
2019-08-01 10:30:01 +02:00
|
|
|
|
* {
|
|
|
|
|
* case SORT_NAME:
|
|
|
|
|
* order = g_ascii_strcasecmp (entry1->name, entry2->name);
|
|
|
|
|
* break;
|
|
|
|
|
* case SORT_SIZE:
|
|
|
|
|
* order = entry1->size - entry2->size;
|
|
|
|
|
* break;
|
|
|
|
|
* default:
|
|
|
|
|
* order = 0;
|
|
|
|
|
* break;
|
|
|
|
|
* }
|
|
|
|
|
* return order;
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* ...
|
|
|
|
|
* g_autoptr (GPtrArray) file_list = NULL;
|
|
|
|
|
* SortMode sort_mode;
|
|
|
|
|
*
|
|
|
|
|
* // initialize file_list array and load with many FileListEntry entries
|
|
|
|
|
* ...
|
|
|
|
|
* // now sort it with
|
|
|
|
|
* sort_mode = SORT_NAME;
|
|
|
|
|
* g_ptr_array_sort_with_data (file_list,
|
2019-10-24 11:39:12 +01:00
|
|
|
|
* sort_filelist,
|
2019-08-01 10:30:01 +02:00
|
|
|
|
* GINT_TO_POINTER (sort_mode));
|
|
|
|
|
* ]|
|
2012-03-14 21:17:47 +01:00
|
|
|
|
*
|
|
|
|
|
* This is guaranteed to be a stable sort since version 2.32.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_ptr_array_sort_with_data (GPtrArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareDataFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (array != NULL);
|
|
|
|
|
|
2020-12-09 13:57:24 +01:00
|
|
|
|
if (array->len > 0)
|
2024-06-28 15:22:33 +01:00
|
|
|
|
g_sort_array (array->pdata,
|
|
|
|
|
array->len,
|
|
|
|
|
sizeof (gpointer),
|
|
|
|
|
compare_func,
|
|
|
|
|
user_data);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-20 00:14:18 +01:00
|
|
|
|
static inline gint
|
|
|
|
|
compare_ptr_array_values (gconstpointer a, gconstpointer b, gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
gconstpointer aa = *((gconstpointer *) a);
|
|
|
|
|
gconstpointer bb = *((gconstpointer *) b);
|
|
|
|
|
GCompareFunc compare_func = user_data;
|
|
|
|
|
|
|
|
|
|
return compare_func (aa, bb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_sort_values:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @compare_func: a #GCompareFunc comparison function
|
|
|
|
|
*
|
|
|
|
|
* Sorts the array, using @compare_func which should be a qsort()-style
|
|
|
|
|
* comparison function (returns less than zero for first arg is less
|
|
|
|
|
* than second arg, zero for equal, greater than zero if first arg is
|
|
|
|
|
* greater than second arg).
|
|
|
|
|
*
|
|
|
|
|
* This is guaranteed to be a stable sort.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_ptr_array_sort_values (GPtrArray *array,
|
|
|
|
|
GCompareFunc compare_func)
|
|
|
|
|
{
|
|
|
|
|
g_ptr_array_sort_with_data (array, compare_ptr_array_values, compare_func);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
GCompareDataFunc compare_func;
|
|
|
|
|
gpointer user_data;
|
|
|
|
|
} GPtrArraySortValuesData;
|
|
|
|
|
|
|
|
|
|
static inline gint
|
|
|
|
|
compare_ptr_array_values_with_data (gconstpointer a,
|
|
|
|
|
gconstpointer b,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
gconstpointer aa = *((gconstpointer *) a);
|
|
|
|
|
gconstpointer bb = *((gconstpointer *) b);
|
|
|
|
|
GPtrArraySortValuesData *data = user_data;
|
|
|
|
|
|
|
|
|
|
return data->compare_func (aa, bb, data->user_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_sort_values_with_data:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @compare_func: a #GCompareDataFunc comparison function
|
|
|
|
|
* @user_data: data to pass to @compare_func
|
|
|
|
|
*
|
|
|
|
|
* Like g_ptr_array_sort_values(), but the comparison function has an extra
|
|
|
|
|
* user data argument.
|
|
|
|
|
*
|
|
|
|
|
* This is guaranteed to be a stable sort.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.76
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
g_ptr_array_sort_values_with_data (GPtrArray *array,
|
|
|
|
|
GCompareDataFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
g_ptr_array_sort_with_data (array, compare_ptr_array_values_with_data,
|
|
|
|
|
&(GPtrArraySortValuesData){
|
|
|
|
|
.compare_func = compare_func,
|
|
|
|
|
.user_data = user_data,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2003-12-26 01:04:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_foreach:
|
|
|
|
|
* @array: a #GPtrArray
|
|
|
|
|
* @func: the function to call for each array element
|
|
|
|
|
* @user_data: user data to pass to the function
|
|
|
|
|
*
|
2014-02-14 16:03:49 -05:00
|
|
|
|
* Calls a function for each element of a #GPtrArray. @func must not
|
|
|
|
|
* add elements to or remove elements from the array.
|
2003-12-26 01:04:12 +00:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.4
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2003-12-26 01:04:12 +00:00
|
|
|
|
void
|
|
|
|
|
g_ptr_array_foreach (GPtrArray *array,
|
|
|
|
|
GFunc func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (array);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < array->len; i++)
|
|
|
|
|
(*func) (array->pdata[i], user_data);
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-15 15:04:34 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_find: (skip)
|
|
|
|
|
* @haystack: pointer array to be searched
|
|
|
|
|
* @needle: pointer to look for
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @index_: (optional) (out): return location for the index of
|
2013-04-15 15:04:34 +02:00
|
|
|
|
* the element, if found
|
|
|
|
|
*
|
|
|
|
|
* Checks whether @needle exists in @haystack. If the element is found, %TRUE is
|
|
|
|
|
* returned and the element’s index is returned in @index_ (if non-%NULL).
|
|
|
|
|
* Otherwise, %FALSE is returned and @index_ is undefined. If @needle exists
|
|
|
|
|
* multiple times in @haystack, the index of the first instance is returned.
|
|
|
|
|
*
|
|
|
|
|
* This does pointer comparisons only. If you want to use more complex equality
|
|
|
|
|
* checks, such as string comparisons, use g_ptr_array_find_with_equal_func().
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @needle is one of the elements of @haystack
|
|
|
|
|
* Since: 2.54
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_ptr_array_find (GPtrArray *haystack,
|
|
|
|
|
gconstpointer needle,
|
|
|
|
|
guint *index_)
|
|
|
|
|
{
|
|
|
|
|
return g_ptr_array_find_with_equal_func (haystack, needle, NULL, index_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_ptr_array_find_with_equal_func: (skip)
|
|
|
|
|
* @haystack: pointer array to be searched
|
|
|
|
|
* @needle: pointer to look for
|
|
|
|
|
* @equal_func: (nullable): the function to call for each element, which should
|
|
|
|
|
* return %TRUE when the desired element is found; or %NULL to use pointer
|
|
|
|
|
* equality
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @index_: (optional) (out): return location for the index of
|
2013-04-15 15:04:34 +02:00
|
|
|
|
* the element, if found
|
|
|
|
|
*
|
|
|
|
|
* Checks whether @needle exists in @haystack, using the given @equal_func.
|
|
|
|
|
* If the element is found, %TRUE is returned and the element’s index is
|
|
|
|
|
* returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
|
|
|
|
|
* is undefined. If @needle exists multiple times in @haystack, the index of
|
|
|
|
|
* the first instance is returned.
|
|
|
|
|
*
|
|
|
|
|
* @equal_func is called with the element from the array as its first parameter,
|
|
|
|
|
* and @needle as its second parameter. If @equal_func is %NULL, pointer
|
|
|
|
|
* equality is used.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if @needle is one of the elements of @haystack
|
|
|
|
|
* Since: 2.54
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_ptr_array_find_with_equal_func (GPtrArray *haystack,
|
|
|
|
|
gconstpointer needle,
|
|
|
|
|
GEqualFunc equal_func,
|
|
|
|
|
guint *index_)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (haystack != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
if (equal_func == NULL)
|
|
|
|
|
equal_func = g_direct_equal;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < haystack->len; i++)
|
|
|
|
|
{
|
|
|
|
|
if (equal_func (g_ptr_array_index (haystack, i), needle))
|
|
|
|
|
{
|
|
|
|
|
if (index_ != NULL)
|
|
|
|
|
*index_ = i;
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* GByteArray:
|
|
|
|
|
* @data: a pointer to the element data. The data may be moved as
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* elements are added to the #GByteArray
|
|
|
|
|
* @len: the number of elements in the #GByteArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Contains the public fields of a GByteArray.
|
2014-01-31 00:29:14 -05:00
|
|
|
|
*/
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_new:
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GByteArray with a reference count of 1.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: (transfer full): the new #GByteArray
|
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_new (void)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, 0);
|
2000-04-17 10:59:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 19:22:05 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_steal:
|
|
|
|
|
* @array: a #GByteArray.
|
2021-03-20 11:14:15 -07:00
|
|
|
|
* @len: (optional) (out): pointer to retrieve the number of
|
2019-07-30 19:22:05 +02:00
|
|
|
|
* elements of the original array
|
|
|
|
|
*
|
|
|
|
|
* Frees the data in the array and resets the size to zero, while
|
|
|
|
|
* the underlying array is preserved for use elsewhere and returned
|
|
|
|
|
* to the caller.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): the element data, which should be
|
|
|
|
|
* freed using g_free().
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.64
|
|
|
|
|
*/
|
|
|
|
|
guint8 *
|
|
|
|
|
g_byte_array_steal (GByteArray *array,
|
|
|
|
|
gsize *len)
|
|
|
|
|
{
|
|
|
|
|
return (guint8 *) g_array_steal ((GArray *) array, len);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-11 17:08:26 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_new_take:
|
2012-11-09 09:32:03 +01:00
|
|
|
|
* @data: (transfer full) (array length=len): byte data for the array
|
2011-11-11 17:08:26 +01:00
|
|
|
|
* @len: length of @data
|
|
|
|
|
*
|
2023-05-08 01:24:25 +02:00
|
|
|
|
* Creates a byte array containing the @data.
|
|
|
|
|
* After this call, @data belongs to the #GByteArray and may no longer be
|
|
|
|
|
* modified by the caller. The memory of @data has to be dynamically
|
|
|
|
|
* allocated and will eventually be freed with g_free().
|
2011-11-11 17:08:26 +01:00
|
|
|
|
*
|
2021-02-10 23:51:07 +01:00
|
|
|
|
* Do not use it if @len is greater than %G_MAXUINT. #GByteArray
|
|
|
|
|
* stores the length of its data in #guint, which may be shorter than
|
|
|
|
|
* #gsize.
|
|
|
|
|
*
|
2011-11-11 17:08:26 +01:00
|
|
|
|
* Since: 2.32
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): a new #GByteArray
|
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GByteArray*
|
2011-11-11 17:08:26 +01:00
|
|
|
|
g_byte_array_new_take (guint8 *data,
|
|
|
|
|
gsize len)
|
|
|
|
|
{
|
|
|
|
|
GByteArray *array;
|
|
|
|
|
GRealArray *real;
|
|
|
|
|
|
2021-02-10 23:51:07 +01:00
|
|
|
|
g_return_val_if_fail (len <= G_MAXUINT, NULL);
|
2011-11-11 17:08:26 +01:00
|
|
|
|
array = g_byte_array_new ();
|
|
|
|
|
real = (GRealArray *)array;
|
|
|
|
|
g_assert (real->data == NULL);
|
|
|
|
|
g_assert (real->len == 0);
|
|
|
|
|
|
|
|
|
|
real->data = data;
|
|
|
|
|
real->len = len;
|
2021-10-19 15:38:13 +00:00
|
|
|
|
real->elt_capacity = len;
|
2011-11-11 17:08:26 +01:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_sized_new:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @reserved_size: number of bytes preallocated
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GByteArray with @reserved_size bytes preallocated.
|
|
|
|
|
* This avoids frequent reallocation, if you are going to add many
|
|
|
|
|
* bytes to the array. Note however that the size of the array is still
|
|
|
|
|
* 0.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer full): the new #GByteArray
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_sized_new (guint reserved_size)
|
2000-04-17 10:59:46 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return (GByteArray *)g_array_sized_new (FALSE, FALSE, 1, reserved_size);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_free:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @free_segment: if %TRUE the actual byte data is freed as well
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Frees the memory allocated by the #GByteArray. If @free_segment is
|
|
|
|
|
* %TRUE it frees the actual byte data. If the reference count of
|
|
|
|
|
* @array is greater than one, the #GByteArray wrapper is preserved but
|
|
|
|
|
* the size of @array will be set to zero.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
|
|
|
|
* Returns: the element data if @free_segment is %FALSE, otherwise
|
|
|
|
|
* %NULL. The element data should be freed using g_free().
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
guint8*
|
|
|
|
|
g_byte_array_free (GByteArray *array,
|
|
|
|
|
gboolean free_segment)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return (guint8 *)g_array_free ((GArray *)array, free_segment);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-11 17:08:26 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_free_to_bytes:
|
|
|
|
|
* @array: (transfer full): a #GByteArray
|
|
|
|
|
*
|
|
|
|
|
* Transfers the data from the #GByteArray into a new immutable #GBytes.
|
|
|
|
|
*
|
|
|
|
|
* The #GByteArray is freed unless the reference count of @array is greater
|
|
|
|
|
* than one, the #GByteArray wrapper is preserved but the size of @array
|
|
|
|
|
* will be set to zero.
|
|
|
|
|
*
|
|
|
|
|
* This is identical to using g_bytes_new_take() and g_byte_array_free()
|
|
|
|
|
* together.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.32
|
|
|
|
|
*
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* Returns: (transfer full): a new immutable #GBytes representing same
|
|
|
|
|
* byte data that was in the array
|
2011-11-11 17:08:26 +01:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GBytes*
|
2011-11-11 17:08:26 +01:00
|
|
|
|
g_byte_array_free_to_bytes (GByteArray *array)
|
|
|
|
|
{
|
|
|
|
|
gsize length;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (array != NULL, NULL);
|
|
|
|
|
|
|
|
|
|
length = array->len;
|
|
|
|
|
return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-29 11:15:20 -04:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_ref:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GByteArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Atomically increments the reference count of @array by one.
|
|
|
|
|
* This function is thread-safe and may be called from any thread.
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer full): The passed in #GByteArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2014-02-01 20:45:25 -05:00
|
|
|
|
GByteArray*
|
2009-04-29 11:15:20 -04:00
|
|
|
|
g_byte_array_ref (GByteArray *array)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return (GByteArray *)g_array_ref ((GArray *)array);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_unref:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: A #GByteArray
|
2009-04-29 11:15:20 -04:00
|
|
|
|
*
|
|
|
|
|
* Atomically decrements the reference count of @array by one. If the
|
|
|
|
|
* reference count drops to 0, all memory allocated by the array is
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* released. This function is thread-safe and may be called from any
|
2009-04-29 11:15:20 -04:00
|
|
|
|
* thread.
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.22
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2009-04-29 11:15:20 -04:00
|
|
|
|
void
|
|
|
|
|
g_byte_array_unref (GByteArray *array)
|
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_unref ((GArray *)array);
|
2009-04-29 11:15:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_append:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @data: the byte data to be added
|
|
|
|
|
* @len: the number of bytes to add
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Adds the given bytes to the end of the #GByteArray.
|
|
|
|
|
* The array will grow in size automatically if necessary.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_append (GByteArray *array,
|
|
|
|
|
const guint8 *data,
|
|
|
|
|
guint len)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_append_vals ((GArray *)array, (guint8 *)data, len);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_prepend:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @data: the byte data to be added
|
|
|
|
|
* @len: the number of bytes to add
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Adds the given data to the start of the #GByteArray.
|
|
|
|
|
* The array will grow in size automatically if necessary.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_prepend (GByteArray *array,
|
|
|
|
|
const guint8 *data,
|
|
|
|
|
guint len)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_prepend_vals ((GArray *)array, (guint8 *)data, len);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_set_size:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @length: the new size of the #GByteArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sets the size of the #GByteArray, expanding it if necessary.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_set_size (GByteArray *array,
|
|
|
|
|
guint length)
|
1998-06-12 09:38:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_set_size ((GArray *)array, length);
|
1998-06-12 09:38:32 +00:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_remove_index:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @index_: the index of the byte to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
2014-02-01 20:45:25 -05:00
|
|
|
|
* Removes the byte at the given index from a #GByteArray.
|
|
|
|
|
* The following bytes are moved down one place.
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2010-01-31 14:30:08 -05:00
|
|
|
|
**/
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_remove_index (GByteArray *array,
|
|
|
|
|
guint index_)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_remove_index ((GArray *)array, index_);
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_remove_index_fast:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
|
|
|
|
* @index_: the index of the byte to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the byte at the given index from a #GByteArray. The last
|
|
|
|
|
* element in the array is used to fill in the space, so this function
|
|
|
|
|
* does not preserve the order of the #GByteArray. But it is faster
|
|
|
|
|
* than g_byte_array_remove_index().
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_remove_index_fast (GByteArray *array,
|
|
|
|
|
guint index_)
|
1998-11-03 14:52:25 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_remove_index_fast ((GArray *)array, index_);
|
1998-11-03 14:52:25 +00:00
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|
2000-11-20 23:59:32 +00:00
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_remove_range:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a @GByteArray
|
|
|
|
|
* @index_: the index of the first byte to remove
|
|
|
|
|
* @length: the number of bytes to remove
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Removes the given number of bytes starting at the given index from a
|
|
|
|
|
* #GByteArray. The following elements are moved to close the gap.
|
|
|
|
|
*
|
2023-11-29 12:01:12 +00:00
|
|
|
|
* Returns: (transfer none): the #GByteArray
|
2012-10-31 14:56:00 +13:00
|
|
|
|
*
|
2010-01-31 14:30:08 -05:00
|
|
|
|
* Since: 2.4
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2003-07-26 08:03:16 +00:00
|
|
|
|
GByteArray*
|
|
|
|
|
g_byte_array_remove_range (GByteArray *array,
|
2008-12-31 05:51:47 +00:00
|
|
|
|
guint index_,
|
|
|
|
|
guint length)
|
2003-07-26 08:03:16 +00:00
|
|
|
|
{
|
2004-01-31 02:12:06 +00:00
|
|
|
|
g_return_val_if_fail (array, NULL);
|
2016-03-08 18:59:34 +00:00
|
|
|
|
g_return_val_if_fail (index_ <= array->len, NULL);
|
2024-02-02 20:33:51 +01:00
|
|
|
|
g_return_val_if_fail (index_ <= G_MAXUINT - length, NULL);
|
2004-01-31 02:12:06 +00:00
|
|
|
|
g_return_val_if_fail (index_ + length <= array->len, NULL);
|
2003-07-26 08:03:16 +00:00
|
|
|
|
|
2014-02-01 20:45:25 -05:00
|
|
|
|
return (GByteArray *)g_array_remove_range ((GArray *)array, index_, length);
|
2003-07-26 08:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_sort:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
2023-11-29 11:59:47 +00:00
|
|
|
|
* @compare_func: (scope call): comparison function
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Sorts a byte array, using @compare_func which should be a
|
|
|
|
|
* qsort()-style comparison function (returns less than zero for first
|
|
|
|
|
* arg is less than second arg, zero for equal, greater than zero if
|
|
|
|
|
* first arg is greater than second arg).
|
|
|
|
|
*
|
|
|
|
|
* If two array elements compare equal, their order in the sorted array
|
2011-12-13 23:00:16 -05:00
|
|
|
|
* is undefined. If you want equal elements to keep their order (i.e.
|
|
|
|
|
* you want a stable sort) you can write a comparison function that,
|
2011-05-15 15:31:16 +01:00
|
|
|
|
* if two elements would otherwise compare equal, compares them by
|
2011-05-15 16:11:51 +02:00
|
|
|
|
* their addresses.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_byte_array_sort (GByteArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareFunc compare_func)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_sort ((GArray *)array, compare_func);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 14:30:08 -05:00
|
|
|
|
/**
|
|
|
|
|
* g_byte_array_sort_with_data:
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @array: a #GByteArray
|
2023-11-29 11:59:47 +00:00
|
|
|
|
* @compare_func: (scope call): comparison function
|
2014-01-31 14:56:10 -05:00
|
|
|
|
* @user_data: data to pass to @compare_func
|
2010-01-31 14:30:08 -05:00
|
|
|
|
*
|
|
|
|
|
* Like g_byte_array_sort(), but the comparison function takes an extra
|
|
|
|
|
* user data argument.
|
2014-01-31 14:56:10 -05:00
|
|
|
|
*/
|
2000-11-20 23:59:32 +00:00
|
|
|
|
void
|
|
|
|
|
g_byte_array_sort_with_data (GByteArray *array,
|
2014-01-31 14:56:10 -05:00
|
|
|
|
GCompareDataFunc compare_func,
|
|
|
|
|
gpointer user_data)
|
2000-11-20 23:59:32 +00:00
|
|
|
|
{
|
2014-02-01 20:45:25 -05:00
|
|
|
|
g_array_sort_with_data ((GArray *)array, compare_func, user_data);
|
2000-11-20 23:59:32 +00:00
|
|
|
|
}
|