summaryrefslogtreecommitdiff
path: root/glib/garray.c
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-05-15 17:36:43 +0200
committerThomas Haller <thaller@redhat.com>2020-05-15 17:47:11 +0200
commit1efa966b6f5d20d9bf10434b1d14b8b224fb60a9 (patch)
tree534390dcdf83cb9abe042107a2a8026decde376a /glib/garray.c
parentc0146be3a4e0cda7a23d7fd54cc60a0bc7ba7f7a (diff)
downloadglib-1efa966b6f5d20d9bf10434b1d14b8b224fb60a9.tar.gz
array: add internal ptr_array_new() helper for creating GPtrArray
Unify the creation of GPtrArray. Maybe we will add yet another constructor for creating %NULL terminated arrays. Unify the constructors by adding an internal helper method. The alternative instead of adding a ptr_array_new() helper, would be to let everybody call g_ptr_array_full(). For no strong reasons, choose this approach because the compiler is more eager to inline the static helper as it would inlining g_ptr_array_full().
Diffstat (limited to 'glib/garray.c')
-rw-r--r--glib/garray.c59
1 files changed, 28 insertions, 31 deletions
diff --git a/glib/garray.c b/glib/garray.c
index 5bb8b4321..4d29bc068 100644
--- a/glib/garray.c
+++ b/glib/garray.c
@@ -1071,6 +1071,27 @@ struct _GRealPtrArray
static void g_ptr_array_maybe_expand (GRealPtrArray *array,
guint len);
+static GPtrArray *
+ptr_array_new (guint reserved_size,
+ GDestroyNotify element_free_func)
+{
+ GRealPtrArray *array;
+
+ array = g_slice_new (GRealPtrArray);
+
+ array->pdata = NULL;
+ array->len = 0;
+ array->alloc = 0;
+ array->element_free_func = element_free_func;
+
+ g_atomic_ref_count_init (&array->ref_count);
+
+ if (reserved_size != 0)
+ g_ptr_array_maybe_expand (array, reserved_size);
+
+ return (GPtrArray *) array;
+}
+
/**
* g_ptr_array_new:
*
@@ -1081,7 +1102,7 @@ static void g_ptr_array_maybe_expand (GRealPtrArray *array,
GPtrArray*
g_ptr_array_new (void)
{
- return g_ptr_array_sized_new (0);
+ return ptr_array_new (0, NULL);
}
/**
@@ -1190,8 +1211,8 @@ g_ptr_array_copy (GPtrArray *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 = ptr_array_new (array->len,
+ ((GRealPtrArray *) array)->element_free_func);
if (func != NULL)
{
@@ -1222,24 +1243,10 @@ g_ptr_array_copy (GPtrArray *array,
*
* Returns: the new #GPtrArray
*/
-GPtrArray*
+GPtrArray*
g_ptr_array_sized_new (guint reserved_size)
{
- GRealPtrArray *array;
-
- array = g_slice_new (GRealPtrArray);
-
- array->pdata = NULL;
- array->len = 0;
- array->alloc = 0;
- array->element_free_func = NULL;
-
- g_atomic_ref_count_init (&array->ref_count);
-
- if (reserved_size != 0)
- g_ptr_array_maybe_expand (array, reserved_size);
-
- return (GPtrArray*) array;
+ return ptr_array_new (reserved_size, NULL);
}
/**
@@ -1290,12 +1297,7 @@ g_array_copy (GArray *array)
GPtrArray*
g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
{
- GPtrArray *array;
-
- array = g_ptr_array_new ();
- g_ptr_array_set_free_func (array, element_free_func);
-
- return array;
+ return ptr_array_new (0, element_free_func);
}
/**
@@ -1320,12 +1322,7 @@ GPtrArray*
g_ptr_array_new_full (guint reserved_size,
GDestroyNotify element_free_func)
{
- GPtrArray *array;
-
- array = g_ptr_array_sized_new (reserved_size);
- g_ptr_array_set_free_func (array, element_free_func);
-
- return array;
+ return ptr_array_new (reserved_size, element_free_func);
}
/**