garray: Add g_ptr_array_new_take() to take a C array without copies

GPtrArray is a nice interface to handle pointer arrays, however if a classic
array needs to be converted into a GPtrArray is currently needed to manually
go through all its elements and do new allocations that could be avoided.

So add g_ptr_array_new_take() which steals the data from an array of
pointers and allows to manage it using the GPtrArray API.
This commit is contained in:
Marco Trevisan (Treviño)
2022-12-07 13:35:04 +01:00
parent 4eb9b09014
commit 14ded2ef94
4 changed files with 197 additions and 0 deletions

View File

@@ -1150,6 +1150,53 @@ g_ptr_array_new (void)
return ptr_array_new (0, NULL, FALSE);
}
/**
* 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.
*
* This avoids having to copy such data manually. @data will eventually be
* freed using g_free(), so must have been allocated with a suitable allocator.
*
* 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;
}
/**
* g_ptr_array_steal:
* @array: a #GPtrArray.