GNOME Bugzilla – Bug 795376
Add g_ptr_array_steal()
Last modified: 2018-05-09 12:54:24 UTC
At the moment, if you create a GPtrArray with a free function, it’s really quite hard to remove an entry from the array without its free function being called. If the free function ignores NULLs, you can do g_steal_pointer (&my_array->pdata[i]), but if the free function doesn’t, you’re a bit stuck. It would be useful to add gpointer g_ptr_array_steal (GPtrArray *array, guint index);
Created attachment 371132 [details] [review] garray: Factor out implementation of g_ptr_array_remove_index*() They were almost identically the same. This introduces no functional changes, but will help with upcoming additions to GPtrArray. Signed-off-by: Philip Withnall <withnall@endlessm.com>
Created attachment 371133 [details] [review] garray: Document that return value of g_ptr_array_remove() may be junk If using g_ptr_array_remove*() with a non-NULL GDestroyNotify function, the value returned will probably be freed memory (depending on what the GDestroyNotify) function actually does. Warn about that in the documentation. We can’t just unconditionally return NULL in these cases, though, since the user might have set the GDestroyNotify to a nifty function which doesn’t actually free the element; so returning it might still be valid and useful. Also add missing (nullable) annotations to that documentation. Signed-off-by: Philip Withnall <withnall@endlessm.com>
Created attachment 371134 [details] [review] garray: Add g_ptr_array_steal_index*() functions These make it easy to steal elements from pointer arrays without having the array’s GDestroyNotify called on them, similarly to what’s possible with g_hash_table_steal(). Signed-off-by: Philip Withnall <withnall@endlessm.com>
I do wonder if we want to have the function under the `g_steal_` namespace instead of using the `GPtrArray` one.
(In reply to Emmanuele Bassi (:ebassi) from comment #4) > I do wonder if we want to have the function under the `g_steal_` namespace > instead of using the `GPtrArray` one. I think it makes more sense to have under the GPtrArray namespace. It’s definitely a method of the GPtrArray class (I realise it’s not a class in the GObject sense, but it is still one in the general OOP sense).
Fair enough; then the whole series is ACK-by: me.
Trivial rebase and pushed to master, ta. Attachment 371132 [details] pushed as 2c9a84e - garray: Factor out implementation of g_ptr_array_remove_index*() Attachment 371133 [details] pushed as e6200ea - garray: Document that return value of g_ptr_array_remove() may be junk Attachment 371134 [details] pushed as d0a48f2 - garray: Add g_ptr_array_steal_index*() functions