After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 580450 - Reference counting and boxed types for arrays
Reference counting and boxed types for arrays
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
: 508347 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2009-04-27 14:50 UTC by David Zeuthen (not reading bugmail)
Modified: 2009-04-29 15:27 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed patch (21.82 KB, patch)
2009-04-27 14:52 UTC, David Zeuthen (not reading bugmail)
none Details | Review
Updated patch (17.47 KB, patch)
2009-04-27 19:05 UTC, David Zeuthen (not reading bugmail)
accepted-commit_now Details | Review
Also use @element_free_func when removing elements (21.10 KB, patch)
2009-04-28 15:49 UTC, David Zeuthen (not reading bugmail)
none Details | Review
Add test cases (27.21 KB, patch)
2009-04-29 14:50 UTC, David Zeuthen (not reading bugmail)
none Details | Review
Include boxed types (30.11 KB, patch)
2009-04-29 15:08 UTC, David Zeuthen (not reading bugmail)
accepted-commit_now Details | Review

Description David Zeuthen (not reading bugmail) 2009-04-27 14:50:55 UTC
It would be nice if 

 1. GArray, GPtrArray and GByteArray had reference counting and boxed types

 2. it was possible to set a GDestroyNotify on pointer arrays
    so when returning a GPtrArray all the user has to do to free it
    is g_ptr_array_unref()

 3. it was possible to get the element size of a GArray

I will attach a patch for doing this.
Comment 1 David Zeuthen (not reading bugmail) 2009-04-27 14:52:23 UTC
Created attachment 133420 [details] [review]
Proposed patch

This is similar to how GHashTable works - e.g. if you call g_array_free() on an array with reference count > 1 then the GArray wrapper is preserved but the size of the array will be set to 0.
Comment 2 David Zeuthen (not reading bugmail) 2009-04-27 14:54:17 UTC
I need this for the proposed GDBus library (bug 579571) but I think this is useful on it's own too.
Comment 3 Matthias Clasen 2009-04-27 15:05:19 UTC
This all looks great to me. 

But why do you 'resurrect' the array just before freeing it here ?
Looking at the g_ptr_array_free code, it doesn't seem to make any difference,
since the behaviour of free only depends on whether the refcount is > 1 or not.

+  if (g_atomic_int_dec_and_test (&array->ref_count))
+    {
+      g_atomic_int_inc (&array->ref_count);
+      g_ptr_array_free (farray, TRUE);
+    }
Comment 4 David Zeuthen (not reading bugmail) 2009-04-27 19:05:05 UTC
(In reply to comment #3)
> This all looks great to me. 

Great, thanks for looking at it.

> But why do you 'resurrect' the array just before freeing it here ?
> Looking at the g_ptr_array_free code, it doesn't seem to make any difference,
> since the behaviour of free only depends on whether the refcount is > 1 or not.
> 
> +  if (g_atomic_int_dec_and_test (&array->ref_count))
> +    {
> +      g_atomic_int_inc (&array->ref_count);
> +      g_ptr_array_free (farray, TRUE);
> +    }

Yeah, that's not necessary (I just changed it and my tests in GDBus passed) - it was there when I had g_array_free() warn to stderr if the reference count was != 1. Will attach a patch without this.
Comment 5 David Zeuthen (not reading bugmail) 2009-04-27 19:05:40 UTC
Created attachment 133440 [details] [review]
Updated patch
Comment 6 Matthias Clasen 2009-04-28 03:30:08 UTC
Almost good to go. Please fix the parameter names to match in headers, sources and doc comments. If they don't, like below, gtk-doc will get it wrong at some point. I also think it is confusing if the doc comments has different parameter
names than the function right below it...

+/**
+ * g_array_unref:
+ * @array: A #GArray.
+ *
+ * Atomically decrements the reference count of @array by one. If the
+ * reference count drops to 0, all memory allocated by the array is
+ * released. This function is MT-safe and may be called from any
+ * thread.
+ *
+ * Since: 2.22
+ **/
+void
+g_array_unref (GArray *farray)



Comment 7 David Zeuthen (not reading bugmail) 2009-04-28 15:49:37 UTC
Created attachment 133512 [details] [review]
Also use @element_free_func when removing elements

(In reply to comment #6)
> Almost good to go. 

Thanks for the review. I've attached an updated patch that also uses @element_free_func on g_ptr_array_remove() and friends. We generally want this, yes?

> Please fix the parameter names to match in headers, sources
> and doc comments. If they don't, like below, gtk-doc will get it wrong at some
> point. I also think it is confusing if the doc comments has different parameter
> names than the function right below it...

OK, done for the added functions.
Comment 8 David Zeuthen (not reading bugmail) 2009-04-29 14:50:55 UTC
Created attachment 133565 [details] [review]
Add test cases

Here's a new patch with test cases - believe it or not, the test cases uncovered a bug which is now fixed in the updated patch...
Comment 9 Matthias Clasen 2009-04-29 14:59:35 UTC
Thanks for adding the tests.

Please add the new test to the Makefile target  when you commit, thanks.
Comment 10 David Zeuthen (not reading bugmail) 2009-04-29 15:08:05 UTC
Created attachment 133569 [details] [review]
Include boxed types

Gah, I forgot the boxed types, here we go again.

> Please add the new test to the Makefile target  when you commit, thanks.

Nothing to add, I just extended glib/tests/array-tests.c
Comment 11 Matthias Clasen 2009-04-29 15:12:21 UTC
Thanks, please commit
Comment 12 David Zeuthen (not reading bugmail) 2009-04-29 15:20:39 UTC
Committed, thanks.

http://git.gnome.org/cgit/glib/commit/?id=402847c8878a6bf839facdf7a91f096769ebc609
Comment 13 Dan Winship 2009-04-29 15:27:16 UTC
*** Bug 508347 has been marked as a duplicate of this bug. ***