[glib/th/g-ptr-array-set-null-terminated] array: add support for g_ptr_array_null_terminated()
- From: Thomas Haller <thaller src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/th/g-ptr-array-set-null-terminated] array: add support for g_ptr_array_null_terminated()
- Date: Thu, 7 May 2020 09:25:53 +0000 (UTC)
commit 66a97535373789b347c1a76ffe199584f9b4b371
Author: Thomas Haller <thaller redhat com>
Date: Wed May 6 19:35:37 2020 +0200
array: add support for g_ptr_array_null_terminated()
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 a
strv array with a GPtrArray.
As workaround for this missing features you could use a GArray instead
(ugly) or to explicitly add the NULL sentinel. 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 g_ptr_array_null_terminated(). The null-terminated flag cannot be
reverted. 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 be re-evaluated.
The API g_ptr_array_null_terminated() with the "make_null_terminated"
argument seems odd at first. It is this way because we need both a
set-null-terminated and a get-null-terminated function. The getter
is very useful, because if somebody provides you an GPtrArray of strings
that you'd like to use as a strv array, you need to be able to confirm
whether it is safe to do. Instead of adding two new symbols for a
getter and setter, only function to both enable and retrieve the flag.
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,
although we don't make the new flag a bitfield.
The biggest downside of the patch is the runtime overhead that most
operations now need to check whether %NULL termination is requested.
https://gitlab.gnome.org/GNOME/glib/-/issues/353
glib/garray.c | 146 +++++++++++++++++++++++++++++++++++++++++-------
glib/garray.h | 3 +
glib/tests/array-test.c | 122 +++++++++++++++++++++++++++-------------
3 files changed, 211 insertions(+), 60 deletions(-)
---
diff --git a/glib/garray.c b/glib/garray.c
index 5bb8b4321..730351b4d 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1052,6 +1052,7 @@ struct _GRealPtrArray
guint len;
guint alloc;
gatomicrefcount ref_count;
+ guint8 null_terminated;
GDestroyNotify element_free_func;
};
@@ -1071,6 +1072,13 @@ struct _GRealPtrArray
static void g_ptr_array_maybe_expand (GRealPtrArray *array,
guint len);
+static void
+ptr_array_null_terminate (GRealPtrArray *rarray)
+{
+ if (G_UNLIKELY (rarray->null_terminated))
+ rarray->pdata[rarray->len] = NULL;
+}
+
/**
* g_ptr_array_new:
*
@@ -1175,7 +1183,8 @@ g_ptr_array_steal (GPtrArray *array,
* pointing to) are copied to the new #GPtrArray.
*
* The copy of @array will have the same #GDestroyNotify for its elements as
- * @array.
+ * @array. The copy will also be %NULL terminated if (and only if) the source
+ * array is.
*
* Returns: (transfer full): a deep copy of the initial #GPtrArray.
*
@@ -1186,27 +1195,41 @@ g_ptr_array_copy (GPtrArray *array,
GCopyFunc func,
gpointer user_data)
{
+ GRealPtrArray *rarray = (GRealPtrArray *) array;
GPtrArray *new_array;
g_return_val_if_fail (array != NULL, NULL);
- new_array = g_ptr_array_sized_new (array->len);
- g_ptr_array_set_free_func (new_array, ((GRealPtrArray *) array)->element_free_func);
+ new_array = g_ptr_array_sized_new (0);
+ g_ptr_array_set_free_func (new_array, rarray->element_free_func);
- if (func != NULL)
+ if (rarray->null_terminated)
{
- guint i;
-
- for (i = 0; i < array->len; i++)
- new_array->pdata[i] = func (array->pdata[i], user_data);
+ /* propagate the null terminated flag. */
+ g_ptr_array_null_terminated (new_array, TRUE);
}
- else if (array->len > 0)
+
+ if (((GRealPtrArray *) array)->alloc > 0)
{
- memcpy (new_array->pdata, array->pdata,
- array->len * sizeof (*array->pdata));
- }
+ g_ptr_array_maybe_expand ((GRealPtrArray *) new_array, array->len + rarray->null_terminated);
+
+ if (func != NULL)
+ {
+ guint i;
- new_array->len = array->len;
+ 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;
+
+ ptr_array_null_terminate (rarray);
+ }
return new_array;
}
@@ -1232,6 +1255,7 @@ g_ptr_array_sized_new (guint reserved_size)
array->pdata = NULL;
array->len = 0;
array->alloc = 0;
+ array->null_terminated = FALSE;
array->element_free_func = NULL;
g_atomic_ref_count_init (&array->ref_count);
@@ -1351,6 +1375,57 @@ g_ptr_array_set_free_func (GPtrArray *array,
rarray->element_free_func = element_free_func;
}
+/**
+ * g_ptr_array_null_terminated:
+ * @array: A #GPtrArray
+ * @make_null_terminated: if %FALSE, the function only returns
+ * whether @array is already %NULL terminated. If %TRUE,
+ * this marks @array as %NULL terminated.
+ *
+ * #GPtrArray is not %NULL terminated by default. By calling
+ * this function with @make_null_terminated, the instance gets
+ * marked to be %NULL terminated. Once the instance is marked
+ * to be %NULL terminated, it cannot be reverted.
+ *
+ * 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.
+ *
+ * When calling this function on a non empty array, the initial
+ * null termination may cause a reallocation to grow the buffer.
+ *
+ * Calling this function with %make_null_terminated %FALSE, then
+ * @array is not modified.
+ *
+ * Returns: %TRUE if @array is marked to be %NULL terminated.
+ *
+ * Since: 2.66
+ */
+gboolean
+g_ptr_array_null_terminated (GPtrArray *array, gboolean make_null_terminated)
+{
+ GRealPtrArray *rarray = (GRealPtrArray *)array;
+
+ g_return_val_if_fail (array, FALSE);
+
+ if (!make_null_terminated)
+ return rarray->null_terminated;
+
+ if (!rarray->null_terminated)
+ {
+ rarray->null_terminated = TRUE;
+ if (rarray->alloc > 0)
+ {
+ g_ptr_array_maybe_expand (rarray, 1);
+ rarray->pdata[rarray->len] = NULL;
+ }
+ }
+
+ return TRUE;
+}
+
/**
* g_ptr_array_ref:
* @array: a #GPtrArray
@@ -1496,6 +1571,7 @@ g_ptr_array_maybe_expand (GRealPtrArray *array,
if ((array->len + len) > array->alloc)
{
guint old_alloc = array->alloc;
+
array->alloc = g_nearest_pow (array->len + len);
array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
@@ -1531,7 +1607,13 @@ g_ptr_array_set_size (GPtrArray *array,
if (length_unsigned > rarray->len)
{
guint i;
- g_ptr_array_maybe_expand (rarray, (length_unsigned - rarray->len));
+
+ 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
* memset (array->pdata + array->len, 0,
* sizeof (gpointer) * (length_unsigned - array->len));
@@ -1540,11 +1622,13 @@ g_ptr_array_set_size (GPtrArray *array,
*/
for (i = rarray->len; i < length_unsigned; i++)
rarray->pdata[i] = NULL;
+
+ rarray->len = length_unsigned;
+
+ ptr_array_null_terminate (rarray);
}
else if (length_unsigned < rarray->len)
g_ptr_array_remove_range (array, length_unsigned, rarray->len - length_unsigned);
-
- rarray->len = length_unsigned;
}
static gpointer
@@ -1574,7 +1658,8 @@ ptr_array_remove_index (GPtrArray *array,
rarray->len -= 1;
- if (G_UNLIKELY (g_mem_gc_friendly))
+ if ( G_UNLIKELY (g_mem_gc_friendly)
+ || rarray->null_terminated)
rarray->pdata[rarray->len] = NULL;
return result;
@@ -1690,6 +1775,10 @@ g_ptr_array_remove_range (GPtrArray *array,
g_return_val_if_fail (rarray != NULL, NULL);
g_return_val_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL), NULL);
g_return_val_if_fail (index_ <= rarray->len, NULL);
+
+ if (length == 0)
+ return array;
+
g_return_val_if_fail (index_ + length <= rarray->len, NULL);
if (rarray->element_free_func != NULL)
@@ -1711,6 +1800,8 @@ g_ptr_array_remove_range (GPtrArray *array,
for (i = 0; i < length; i++)
rarray->pdata[rarray->len + i] = NULL;
}
+ else
+ ptr_array_null_terminate (rarray);
return array;
}
@@ -1807,9 +1898,11 @@ g_ptr_array_add (GPtrArray *array,
g_return_if_fail (rarray);
g_return_if_fail (rarray->len == 0 || (rarray->len != 0 && rarray->pdata != NULL));
- g_ptr_array_maybe_expand (rarray, 1);
+ g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
rarray->pdata[rarray->len++] = data;
+
+ ptr_array_null_terminate (rarray);
}
/**
@@ -1845,7 +1938,14 @@ g_ptr_array_extend (GPtrArray *array_to_extend,
g_return_if_fail (array_to_extend != NULL);
g_return_if_fail (array != NULL);
- g_ptr_array_maybe_expand (rarray_to_extend, array->len);
+ 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);
if (func != NULL)
{
@@ -1855,13 +1955,15 @@ g_ptr_array_extend (GPtrArray *array_to_extend,
rarray_to_extend->pdata[i + rarray_to_extend->len] =
func (array->pdata[i], user_data);
}
- else if (array->len > 0)
+ else
{
memcpy (rarray_to_extend->pdata + rarray_to_extend->len, array->pdata,
array->len * sizeof (*array->pdata));
}
rarray_to_extend->len += array->len;
+
+ ptr_array_null_terminate (rarray_to_extend);
}
/**
@@ -1919,7 +2021,7 @@ g_ptr_array_insert (GPtrArray *array,
g_return_if_fail (index_ >= -1);
g_return_if_fail (index_ <= (gint)rarray->len);
- g_ptr_array_maybe_expand (rarray, 1);
+ g_ptr_array_maybe_expand (rarray, 1u + rarray->null_terminated);
if (index_ < 0)
index_ = rarray->len;
@@ -1931,6 +2033,8 @@ g_ptr_array_insert (GPtrArray *array,
rarray->len++;
rarray->pdata[index_] = data;
+
+ ptr_array_null_terminate (rarray);
}
/* Please keep this doc-comment in sync with pointer_array_sort_example()
diff --git a/glib/garray.h b/glib/garray.h
index 67131b5b3..c98403ad1 100644
--- a/glib/garray.h
+++ b/glib/garray.h
@@ -223,6 +223,9 @@ gboolean g_ptr_array_find_with_equal_func (GPtrArray *haystack,
GEqualFunc equal_func,
guint *index_);
+GLIB_AVAILABLE_IN_2_66
+gboolean g_ptr_array_null_terminated (GPtrArray *array, gboolean set_null_terminated);
+
/* Byte arrays, an array of guint8. Implemented as a GArray,
* but type-safe.
diff --git a/glib/tests/array-test.c b/glib/tests/array-test.c
index 1da514a3e..eb261cca4 100644
--- a/glib/tests/array-test.c
+++ b/glib/tests/array-test.c
@@ -837,6 +837,39 @@ test_array_copy_sized (void)
g_array_unref (array1);
}
+static GPtrArray *
+ptr_array_init (GPtrArray *array)
+{
+ gboolean rnd = ((g_random_int () % 3u) == 0u);
+
+ g_assert (array);
+
+ /* Randomly make the GPtrArray %NULL terminated.
+ * A %NULL terminated array gives stronger guarantees, so wherever
+ * the test checks something, it must also hold for a %NULL terminated
+ * array. To increase the test coverage, randomly set the flag. */
+ g_ptr_array_null_terminated (array, rnd);
+ return array;
+}
+
+#define ptr_array_assert_state(array) \
+ G_STMT_START { \
+ GPtrArray *_array = (array); \
+ \
+ if (_array) \
+ { \
+ if (!_array->pdata) \
+ { \
+ g_assert_cmpint (_array->len, ==, 0); \
+ } \
+ else \
+ { \
+ if (g_ptr_array_null_terminated (_array, FALSE)) \
+ g_assert (_array->pdata[_array->len] == NULL); \
+ } \
+ } \
+ } G_STMT_END
+
/* Check g_ptr_array_steal() function */
static void
pointer_array_steal (void)
@@ -847,7 +880,8 @@ pointer_array_steal (void)
guint i;
gsize len, past_len;
- gparray = g_ptr_array_new ();
+ gparray = ptr_array_init (g_ptr_array_new ());
+
pdata = g_ptr_array_steal (gparray, NULL);
g_assert_null (pdata);
@@ -864,6 +898,7 @@ pointer_array_steal (void)
g_assert_cmpint (past_len, ==, len);
g_ptr_array_add (gparray, GINT_TO_POINTER (10));
+ ptr_array_assert_state (gparray);
g_assert_cmpint ((gsize) pdata[0], ==, (gsize) GINT_TO_POINTER (0));
g_assert_cmpint ((gsize) g_ptr_array_index (gparray, 0), ==,
(gsize) GINT_TO_POINTER (10));
@@ -872,11 +907,16 @@ pointer_array_steal (void)
g_ptr_array_remove_index (gparray, 0);
for (i = 0; i < array_size; i++)
- g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+ {
+ g_ptr_array_add (gparray, GINT_TO_POINTER (i));
+ ptr_array_assert_state (gparray);
+ }
g_assert_cmpmem (pdata, array_size * sizeof (gpointer),
gparray->pdata, array_size * sizeof (gpointer));
g_free (pdata);
+ ptr_array_assert_state (gparray);
+
g_ptr_array_free (gparray, TRUE);
}
@@ -888,7 +928,7 @@ pointer_array_add (void)
gint sum = 0;
gpointer *segment;
- gparray = g_ptr_array_sized_new (1000);
+ gparray = ptr_array_init (g_ptr_array_sized_new (1000));
for (i = 0; i < 10000; i++)
g_ptr_array_add (gparray, GINT_TO_POINTER (i));
@@ -913,7 +953,7 @@ pointer_array_insert (void)
gint sum = 0;
gint index;
- gparray = g_ptr_array_sized_new (1000);
+ gparray = ptr_array_init (g_ptr_array_sized_new (1000));
for (i = 0; i < 10000; i++)
{
@@ -935,7 +975,7 @@ pointer_array_ref_count (void)
gint i;
gint sum = 0;
- gparray = g_ptr_array_new ();
+ gparray = ptr_array_init (g_ptr_array_new ());
for (i = 0; i < 10000; i++)
g_ptr_array_add (gparray, GINT_TO_POINTER (i));
@@ -975,16 +1015,16 @@ pointer_array_free_func (void)
gchar *s;
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_unref (gparray);
g_assert_cmpint (num_free_func_invocations, ==, 0);
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_free (gparray, TRUE);
g_assert_cmpint (num_free_func_invocations, ==, 0);
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_add (gparray, g_strdup ("foo"));
g_ptr_array_add (gparray, g_strdup ("bar"));
g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1009,7 +1049,7 @@ pointer_array_free_func (void)
g_assert_cmpint (num_free_func_invocations, ==, 5);
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_full (10, my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_full (10, my_free_func));
g_ptr_array_add (gparray, g_strdup ("foo"));
g_ptr_array_add (gparray, g_strdup ("bar"));
g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1023,7 +1063,7 @@ pointer_array_free_func (void)
g_assert_cmpint (num_free_func_invocations, ==, 0);
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_add (gparray, g_strdup ("foo"));
g_ptr_array_add (gparray, g_strdup ("bar"));
g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1032,7 +1072,7 @@ pointer_array_free_func (void)
g_assert_cmpint (num_free_func_invocations, ==, 3);
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_add (gparray, g_strdup ("foo"));
g_ptr_array_add (gparray, g_strdup ("bar"));
g_ptr_array_add (gparray, g_strdup ("baz"));
@@ -1040,7 +1080,7 @@ pointer_array_free_func (void)
g_assert_cmpint (num_free_func_invocations, ==, 3);
num_free_func_invocations = 0;
- gparray = g_ptr_array_new_with_free_func (my_free_func);
+ gparray = ptr_array_init (g_ptr_array_new_with_free_func (my_free_func));
g_ptr_array_add (gparray, "foo");
g_ptr_array_add (gparray, "bar");
g_ptr_array_add (gparray, "baz");
@@ -1085,8 +1125,8 @@ pointer_array_copy (void)
array_test[i] = i;
/* Test copy an empty array */
- ptr_array = g_ptr_array_sized_new (0);
- ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (0));
+ ptr_array2 = ptr_array_init (g_ptr_array_copy (ptr_array, NULL, NULL));
g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
@@ -1094,12 +1134,12 @@ pointer_array_copy (void)
g_ptr_array_unref (ptr_array2);
/* Test simple copy */
- ptr_array = g_ptr_array_sized_new (array_size);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size));
for (i = 0; i < array_size; i++)
g_ptr_array_add (ptr_array, &array_test[i]);
- ptr_array2 = g_ptr_array_copy (ptr_array, NULL, NULL);
+ ptr_array2 = ptr_array_init (g_ptr_array_copy (ptr_array, NULL, NULL));
g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
for (i = 0; i < array_size; i++)
@@ -1112,7 +1152,7 @@ pointer_array_copy (void)
g_ptr_array_free (ptr_array2, TRUE);
/* Test copy through GCopyFunc */
- ptr_array2 = g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL);
+ ptr_array2 = ptr_array_init (g_ptr_array_copy (ptr_array, ptr_array_copy_func, NULL));
g_ptr_array_set_free_func (ptr_array2, g_free);
g_assert_cmpuint (ptr_array2->len, ==, ptr_array->len);
@@ -1142,7 +1182,7 @@ pointer_array_extend (void)
if (g_test_undefined ())
{
/* Testing degenerated cases */
- ptr_array = g_ptr_array_sized_new (0);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (0));
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion*!= NULL*");
g_ptr_array_extend (NULL, ptr_array, NULL, NULL);
@@ -1161,8 +1201,8 @@ pointer_array_extend (void)
array_test[i] = i;
/* Testing extend with array of size zero */
- ptr_array = g_ptr_array_sized_new (0);
- ptr_array2 = g_ptr_array_sized_new (0);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (0));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (0));
g_ptr_array_extend (ptr_array, ptr_array2, NULL, NULL);
@@ -1173,8 +1213,8 @@ pointer_array_extend (void)
g_ptr_array_unref (ptr_array2);
/* Testing extend an array of size zero */
- ptr_array = g_ptr_array_sized_new (array_size);
- ptr_array2 = g_ptr_array_sized_new (0);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (0));
for (i = 0; i < array_size; i++)
{
@@ -1190,8 +1230,8 @@ pointer_array_extend (void)
g_ptr_array_unref (ptr_array2);
/* Testing extend an array of size zero */
- ptr_array = g_ptr_array_sized_new (0);
- ptr_array2 = g_ptr_array_sized_new (array_size);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (0));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (array_size));
for (i = 0; i < array_size; i++)
{
@@ -1207,8 +1247,8 @@ pointer_array_extend (void)
g_ptr_array_unref (ptr_array2);
/* Testing simple extend */
- ptr_array = g_ptr_array_sized_new (array_size / 2);
- ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
for (i = 0; i < array_size / 2; i++)
{
@@ -1225,8 +1265,8 @@ pointer_array_extend (void)
g_ptr_array_unref (ptr_array2);
/* Testing extend with GCopyFunc */
- ptr_array = g_ptr_array_sized_new (array_size / 2);
- ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
for (i = 0; i < array_size / 2; i++)
{
@@ -1262,8 +1302,8 @@ pointer_array_extend_and_steal (void)
array_test[i] = i;
/* Testing simple extend_and_steal() */
- ptr_array = g_ptr_array_sized_new (array_size / 2);
- ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
for (i = 0; i < array_size / 2; i++)
{
@@ -1271,16 +1311,20 @@ pointer_array_extend_and_steal (void)
g_ptr_array_add (ptr_array2, &array_test[i + (array_size / 2)]);
}
+ ptr_array_assert_state (ptr_array2);
+
g_ptr_array_extend_and_steal (ptr_array, ptr_array2);
+ ptr_array_assert_state (ptr_array);
+
for (i = 0; i < array_size; i++)
g_assert_cmpuint (*((gsize *) g_ptr_array_index (ptr_array, i)), ==, i);
g_ptr_array_free (ptr_array, TRUE);
/* Testing extend_and_steal() with a pending reference to stolen array */
- ptr_array = g_ptr_array_sized_new (array_size / 2);
- ptr_array2 = g_ptr_array_sized_new (array_size / 2);
+ ptr_array = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
+ ptr_array2 = ptr_array_init (g_ptr_array_sized_new (array_size / 2));
for (i = 0; i < array_size / 2; i++)
{
@@ -1333,7 +1377,7 @@ pointer_array_sort (void)
gint val;
gint prev, cur;
- gparray = g_ptr_array_new ();
+ gparray = ptr_array_init (g_ptr_array_new ());
for (i = 0; i < 10000; i++)
{
val = g_random_int_range (0, 10000);
@@ -1388,7 +1432,7 @@ pointer_array_sort_example (void)
g_test_summary ("Check that the doc-comment for g_ptr_array_sort() is correct");
- file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+ file_list = ptr_array_init (g_ptr_array_new_with_free_func (file_list_entry_free));
entry = g_new0 (FileListEntry, 1);
entry->name = g_strdup ("README");
@@ -1455,7 +1499,7 @@ pointer_array_sort_with_data_example (void)
g_test_summary ("Check that the doc-comment for g_ptr_array_sort_with_data() is correct");
- file_list = g_ptr_array_new_with_free_func (file_list_entry_free);
+ file_list = ptr_array_init (g_ptr_array_new_with_free_func (file_list_entry_free));
entry = g_new0 (FileListEntry, 1);
entry->name = g_strdup ("README");
@@ -1504,7 +1548,7 @@ pointer_array_sort_with_data (void)
gint i;
gint prev, cur;
- gparray = g_ptr_array_new ();
+ gparray = ptr_array_init (g_ptr_array_new ());
for (i = 0; i < 10000; i++)
g_ptr_array_add (gparray, GINT_TO_POINTER (g_random_int_range (0, 10000)));
@@ -1527,7 +1571,7 @@ pointer_array_find_empty (void)
GPtrArray *array;
guint idx;
- array = g_ptr_array_new ();
+ array = ptr_array_init (g_ptr_array_new ());
g_assert_false (g_ptr_array_find (array, "some-value", NULL)); /* NULL index */
g_assert_false (g_ptr_array_find (array, "some-value", &idx)); /* non-NULL index */
@@ -1544,7 +1588,7 @@ pointer_array_find_non_empty (void)
guint idx;
const gchar *str_pointer = "static-string";
- array = g_ptr_array_new ();
+ array = ptr_array_init (g_ptr_array_new ());
g_ptr_array_add (array, "some");
g_ptr_array_add (array, "random");
@@ -1588,7 +1632,7 @@ pointer_array_steal_index (void)
{
guint i1 = 0, i2 = 0, i3 = 0, i4 = 0;
gpointer out1, out2;
- GPtrArray *array = g_ptr_array_new_with_free_func (steal_destroy_notify);
+ GPtrArray *array = ptr_array_init (g_ptr_array_new_with_free_func (steal_destroy_notify));
g_ptr_array_add (array, &i1);
g_ptr_array_add (array, &i2);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]