glib-aux: rename and change nm_g_ptr_array_copy() to nm_g_ptr_array_new_clone()

There is g_ptr_array_copy() in glib, but only since 2.68 so we cannot use it.
We had a compat implementation nm_g_ptr_array_copy(), however that one always
requires an additional parameter, the free function of the new array.

g_ptr_array_copy() always does a deep clone, and uses the source array's
free function. We don't have access to the free function (seems quite a
limitation of GPtrArray API), so our nm_g_ptr_array_copy() cannot be
exactly the same.

Previously, nm_g_ptr_array_copy() aimed to be as similar as possible to
g_ptr_array_copy(), and it would require the caller that the free
function is the same as the array's. That seems an unnecessary
limitation, and our compat implementation still looks different and has
a different name. If we were able to fully re-implement it, we would
instead add it to "nm-glib.h".

Anyway. As our implementation already differs, there is no need for the
arbitrary limitation to only perform deep copies. Instead, also allow
shallow copies. Rename the function to nm_g_ptr_array_new_clone() to
make it clearly distinct from g_ptr_array_copy().
This commit is contained in:
Thomas Haller 2023-01-19 08:48:14 +01:00
parent dabfea2fc2
commit fe99d462ec
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 19 additions and 29 deletions

View file

@ -2130,15 +2130,16 @@ nm_strv_cleanup(char **strv, gboolean strip_whitespace, gboolean skip_empty, gbo
/*****************************************************************************/
GPtrArray *
_nm_g_ptr_array_copy(GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func)
nm_g_ptr_array_new_clone(GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func)
{
GPtrArray *new_array;
guint i;
g_return_val_if_fail(array, NULL);
nm_assert((!!func) == (!!element_free_func));
new_array = g_ptr_array_new_full(array->len, element_free_func);
for (i = 0; i < array->len; i++) {

View file

@ -2068,40 +2068,29 @@ nm_g_ptr_array_pdata(const GPtrArray *arr)
return arr ? arr->pdata : NULL;
}
GPtrArray *_nm_g_ptr_array_copy(GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func);
/**
* nm_g_ptr_array_copy:
* nm_g_ptr_array_new_clone:
* @array: the #GPtrArray to clone.
* @func: the copy function.
* @user_data: the user data for the copy function
* @element_free_func: the free function of the elements. @array MUST have
* the same element_free_func. This argument is only used on older
* glib, that doesn't support g_ptr_array_copy().
* @element_free_func: the free function of the elements. This function
* must agree with the owner-ship semantics of @func.
*
* This is a replacement for g_ptr_array_copy(), which is not available
* before glib 2.62. Since GPtrArray does not allow to access the internal
* element_free_func, we cannot add a compatibility implementation of g_ptr_array_copy()
* and the user must provide a suitable destroy function.
* as the caller must provide the correct element_free_func.
*
* Note that the @element_free_func MUST correspond to free function set in @array.
* So this is not the same as g_ptr_array_copy() (hence the different name) because
* g_ptr_array_copy() uses the free func of the source array, which we cannot access.
* With g_ptr_array_copy() the copy func must agree with the array's free func.
* Here, it must agree with the provided @element_free_func. This allows for example
* to do a shallow-copy without cloning the elements (which you cannot do with g_ptr_array_copy()).
*/
#if GLIB_CHECK_VERSION(2, 62, 0)
#define nm_g_ptr_array_copy(array, func, user_data, element_free_func) \
({ \
_nm_unused GDestroyNotify const _element_free_func = (element_free_func); \
\
G_GNUC_BEGIN_IGNORE_DEPRECATIONS; \
g_ptr_array_copy((array), (func), (user_data)); \
G_GNUC_END_IGNORE_DEPRECATIONS; \
})
#else
#define nm_g_ptr_array_copy(array, func, user_data, element_free_func) \
_nm_g_ptr_array_copy((array), (func), (user_data), (element_free_func))
#endif
GPtrArray *nm_g_ptr_array_new_clone(GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func);
/*****************************************************************************/
@ -2510,7 +2499,7 @@ nm_strv_ptrarray_clone(const GPtrArray *src, gboolean null_if_empty)
{
if (!src || (null_if_empty && src->len == 0))
return NULL;
return nm_g_ptr_array_copy((GPtrArray *) src, nm_copy_func_g_strdup, NULL, g_free);
return nm_g_ptr_array_new_clone((GPtrArray *) src, nm_copy_func_g_strdup, NULL, g_free);
}
static inline void