Commit Graph

428 Commits

Author SHA1 Message Date
Marco Trevisan (Treviño)
cbb9feb6af gobject: Use some new cleaner code to set object GValues
Use some newer APIs to make the code nicer cleaner and clearer in terms of
memory ownership.
2022-12-20 17:48:21 +01:00
Marco Trevisan (Treviño)
bc59ed8fba gobject: Avoid the ref/unref dance if assigning the same to a value
g_value_set_object could lead to perform unneeded ref/unref operations in
case we were trying to set again an object to a GValue
2022-12-20 17:48:21 +01:00
Emmanuele Bassi
fde157ace4 Improve notification queue warning
Instead of a plain reference count check failure that is really hard to
understand, let's be explicit, and warn that manipulating an object's
notification queue during its finalization is not allowed.
2022-12-12 15:01:19 +00:00
Philip Withnall
bbb3453c82 Merge branch 'atomic-unref-cleanups' into 'main'
gobject: Use compare and exchange full to re-read old ref value

See merge request GNOME/glib!3098
2022-12-06 16:12:59 +00:00
Marco Trevisan (Treviño)
6cd6cc41bb gobject: Trace unref just after this happened
Trace the unref before potentially calling (user) code that may re-ref
or unref again, causing the tracing order to be messed up.
2022-12-06 04:43:02 +01:00
Marco Trevisan (Treviño)
0918ce013a gobject: Do not call toggle down notifications if current refcount is not 1
When an object is revitalized and a notify callbacks increased the reference
counter of the object, we are calling the toggle notifier twice, while it
should only happen if also the actual reference count value is 1 (after
having been decremented from 2).
2022-12-06 04:38:26 +01:00
Marco Trevisan (Treviño)
1f852863ec gobject: Check for toggle references only if the old ref is relevant
If an object gets revitalized during the dispose vfunc, we need to call
toggle refs notifiers only if we had 2 references and if the object has
the toggle references enabled.

This may change in case an object notifier handler changes this status,
so do this check only after we've called the notifiers so that in case
toggle notifications are enabled afterwards we still call the handlers.
2022-12-06 04:28:31 +01:00
Marco Trevisan (Treviño)
c0360f626c gobject: Read the toggle reference state only after we've update the references
We were reading if an object has toggle references even if this was not
really relevant for the current object state, as we only need to notify
when going from 2 to 1 references, so first ensure that this is the case
and then check if we have toggle references enabled in the object.

This is a micro-optimization, for the way flags are defined, but still
an operation we can avoid in most cases.
2022-12-06 01:22:34 +01:00
Marco Trevisan (Treviño)
30e1d69de7 gobject: Check for valid ref counting via g_return_if_fail
Even though the check is likely to be relevant if the object is finalized,
it may still give some indication if called while an instance has just lost
the last reference.

So use `g_return_if_fail` for consistency with the rest of the code.
2022-12-05 20:23:26 +01:00
Marco Trevisan (Treviño)
65303537b0 gobject: Remove initial goto to repeat unref operation 2022-12-01 16:20:46 +01:00
Marco Trevisan (Treviño)
a89048c4f1 gobject: Use a while instead of goto to repeat atomic increment
We can use a cleaner solution now that we do not require to init the
same value multiple times in the same way.
2022-11-29 21:35:04 +01:00
Marco Trevisan (Treviño)
c95bf0514c gobject: Use compare and exchange full to re-read old ref value
In case g_atomic_int_compare_and_exchange() check fails we ended up doing
another atomic get to figure out what it was the old reference count,
however, we can avoid this by using the full version of the function that
returns the value before the exchange happened as an out value.
2022-11-29 20:53:06 +01:00
Emmanuele Bassi
f1b7a52fe2 docs: Avoid unintended titles in g_object_new()
Lines should not start with `#` unless they are titles, as docblocks are
parsed as Markdown.

Fixes: #2821
2022-11-28 11:49:51 +00:00
Simon McVittie
e974c81d0a gobject: Only emit deprecation warnings for user-specified properties
If a deprecated property only gets set because it is G_PARAM_CONSTRUCT
or G_PARAM_CONSTRUCT_ONLY, then there is nothing for the library user
to fix, and we should not emit a deprecation warning.

Resolves: https://gitlab.gnome.org/GNOME/glib/-/issues/2748
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-09-21 10:27:55 +01:00
Michael Catanzaro
0ffe86a1f7 Replace most GObject warnings with criticals
All of these warnings indicate programmer error, so critical is most
appropriate here.

Exceptions: deprecation warnings are just warnings. Also, warnings that
are worded with uncertainty can remain warnings rather than criticals.
2022-08-09 13:18:47 -05:00
Peter Bloomfield
3290ebfadc gobject: Small speedup in weak-ref-set 2022-07-08 14:37:53 +00:00
Matthias Clasen
c0a618a716 gobject: Don't skip custom dispatch_properties_changed
When I optimized GObject to skip property notification
in some cases, I looked for whether the class has a
custom notify vfunc. I overlooked that that
dispatch_properties_changed can also be customized,
and if it is, we better not skip change notification.

This showed up as breakage in the adjustment tests
in the GTK testsuite.
2022-07-08 13:57:40 +01:00
Philip Withnall
ed564b71dd Merge branch '2672-dataset-tests-and-fixes' into 'main'
gdataset: Preserve destruction order

Closes #2672 and #2676

See merge request GNOME/glib!2776
2022-07-07 11:52:19 +00:00
Philip Withnall
41691cc4c8 Merge branch 'more-spdx' into 'main'
Add more SPDX license headers

See merge request GNOME/glib!2706
2022-07-05 11:06:49 +00:00
Peter Bloomfield
94ba14d542 gobject: Weaken an assertion in g_weak_ref_set()
When weak references are being cleaned up, it is possible for the `qdata` for
both `quark_weak_locations` and `quark_weak_refs` to have been deallocated,
so that `g_datalist_id_get_data()` returns `NULL` for both. This happens
when `g_object_run_dispose()` is called for the target of a `GBinding`,
and is not an error.

See https://gitlab.gnome.org/GNOME/glib/-/issues/2676
2022-07-03 14:56:44 -04:00
Simon McVittie
d4043eced5 Revert "gobject: Use g_datalist_id_remove_multiple"
This reverts commit fa8c7c0 "gobject: Use g_datalist_id_remove_multiple"
(or more specifically the parts of it that were not already reverted
in 01ac1d9 "gobject: Fix crash when disposing a GtkAccelGroup".

Workaround for https://gitlab.gnome.org/GNOME/glib/-/issues/2676

Bug: https://gitlab.gnome.org/GNOME/glib/-/issues/2676
2022-06-26 18:00:52 +01:00
Simon McVittie
7045260c22 gsignal: Add G_CONNECT_DEFAULT
This makes calls to g_signal_connect_data() and g_signal_connect_object()
with default flags more self-documenting.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-06-23 10:46:45 +01:00
Philip Withnall
01ac1d902f gobject: Fix crash when disposing a GtkAccelGroup
This is a partial revert of commit fa8c7c0da using the approach
suggested (and tested) by Kjell Ahlstedt.

It is intended to be temporary pending a proper dig into what’s causing
the regression, just so we can get the 2.73.1 release out.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2672
2022-06-20 16:32:00 +01:00
Philip Withnall
b2f4df95e8 Merge branch 'coverity-is-stupid' into 'main'
gobject: Change GObject notify semantics under static analysis

See merge request GNOME/glib!2747
2022-06-17 15:47:39 +00:00
Philip Withnall
0932f71460 gobject: Change GObject notify semantics under static analysis
Coverity notices the `g_object_unref()` call in `g_object_notify()`, but
not the paired `g_object_ref()` call. It therefore incorrectly assumes
that every call to `g_object_notify()` frees the object. This causes a
lot (hundreds) of false positive reports about double-frees or
use-after-frees.

I can’t find a way to fix this using a model file, so the other options
are:
 * Manually mark every report as a false positive and keep updating them
   as the code changes over time. This would take a lot of maintainer
   effort.
 * Comment out the `g_object_ref()`/`g_object_unref()` calls when
   running static analysis (but not in a normal production build). This
   is ugly, but cheap and shouldn’t impact maintainability much.

So this commit implements option 2.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-06-14 11:25:50 +01:00
Philip Withnall
af7ba68a00 gobject: ifdef out some inline functions if they’re not used
This prevents `-Wunused-function` warnings on platforms which don’t have
`HAVE_OPTIONAL_FLAGS` defined.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-06-14 11:10:09 +01:00
Matthias Clasen
10a1c16b59 Avoid atomics during construction
During construction, we have exclusive access to
the object, so there is no need to use atomics
for setting or reading the optional_flags member.
2022-06-14 11:09:53 +01:00
Matthias Clasen
b92f1fcbc5 Handle notify freezing with custom constructors
g_object_new_with_custom_constructor needs to handle
freezing notifications in the same way as
g_object_new_internal.

Fixing a bug pointed out by Christian Hergert.
2022-06-12 12:01:25 -04:00
Matthias Clasen
58ab960a87 Handle a corner-case of notify avoidance
The corner-case we are handling here is that
we don't freeze the notify queue in g_object_init
(because there's no custom ->notify vfunc, but
then we gain a notify handler during instance
init, and instance init also triggers a
notification. Handle this by jit freezing
notification in g_object_notify_by_spec_internal.

Note that this is bad code - instance init really
shouldn't be doing things like this.

Testcase included.

Fixes: #2665
2022-06-12 12:01:08 -04:00
Matthias Clasen
9444ace2c8 Unconditionally mark objects as in-construction
This will be used for things other than
custom constructors in the following commits.
2022-06-12 12:01:08 -04:00
Matthias Clasen
98bd4eabf2 gobject: Don't thaw excessively
We need to match the conditions in g_object_init
for when we already have a freeze. Without that,
we underflow the freeze count and trigger a
warning.

Fixes: #2666
2022-06-10 14:09:08 +01:00
Matthias Clasen
df43536a68 Consistently use param_spec_follow_override 2022-06-07 12:39:58 -04:00
Matthias Clasen
47c12389a0 gobject: Speed up property lookup
When the param specs are provided as an array
with g_object_class_install_properties, keep
a copy of that array around and use it for
looking up properties without the param spec
pool.

Note that this is an opportunistic optimization -
currently, it only works for properties of the
class itself, not for parent classes, and it
only works if the property names are identical
string literals (we're at the mercy of the linker
for that).

If we don't get lucky, we fall back to using
the pspec pool as usual.
2022-06-07 12:39:58 -04:00
Philip Withnall
12dc714437 Merge branch 'gobject-speedup11' into 'main'
Keep a separate flag for notify handlers

See merge request GNOME/glib!2725
2022-06-07 13:43:50 +00:00
Philip Withnall
3076c60e9c Merge branch 'gobject-speedup10' into 'main'
gobject: Avoid redundant atomics

See merge request GNOME/glib!2724
2022-06-07 11:46:23 +00:00
Philip Withnall
b1e371034d Merge branch 'gobject-speedup8' into 'main'
Avoid g_param_spec_get_redirect_target

See merge request GNOME/glib!2722
2022-06-07 11:45:25 +00:00
Philip Withnall
8f7df344b6 gobject: Sink floating pspecs if adding them to a class fails
This may fix Coverity assuming that pspecs are leaked, which is causing
tens and tens of false positives in the latest Coverity reports for
GLib.

Ensure that the pspecs are sunk (if floating) even if adding them to the
class fails (due to validation failure or an identically named property
already existing).

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-06-07 11:20:32 +01:00
Matthias Clasen
7d8e2fcf6d Cosmetic coding style fixes 2022-06-06 14:28:31 -04:00
Matthias Clasen
f755f445c6 Avoid property notification during g_object_set
If we have no nontrivial notify vfunc, and no signal
handlers for notify, we don't need to maintain the
notify queue. No need to notify if nobody's listening.
2022-06-06 14:26:58 -04:00
Matthias Clasen
8851112b8d Avoid property notification during object construction
Check whether an object has a nontrivial notify vfunc
and avoid creating and updating the notify queue if
it doesn't. We know that there can be no notify signal
handlers at this point. No need to notify if nobody's
listening.
2022-06-06 14:26:45 -04:00
Matthias Clasen
8b328f1d6d Keep a separate flag for notify handlers
We currently keep a flag for whether an object has
ever had any signal handlers. But even if it had signal
handlers, it may not have any notify handlers. Keep that
information separately, so we can speed up property setting.
2022-06-06 14:25:41 -04:00
Matthias Clasen
16a8b6d819 gobject: Avoid redundant atomics
According to the commit that introduced these
calls (4b334ef8f1), we are checking
the refcount here to avoid calling g_object_ref
when the refcount is 0, in the rare case that
notification would be triggered during finalize.

But we are now freezing notifications during
finalize, and after recent changes, we no longer call
g_object_ref for notification while a freeze is
in place.
2022-06-06 14:13:20 -04:00
Matthias Clasen
9d7d439607 Avoid g_param_spec_get_redirect_target
Inline the getting of the redirect target.
This is a rare thing, and the calls were
showing up in profiles.
2022-06-06 14:00:33 -04:00
Philip Withnall
2b437402e8 Merge branch 'gobject-speedup7' into 'main'
Avoid g_object_ref/unref in some cases

See merge request GNOME/glib!2721
2022-06-06 17:53:42 +00:00
Philip Withnall
f8c5d71104 Merge branch 'gobject-speedup9' into 'main'
Avoid g_type_class_peek

See merge request GNOME/glib!2723
2022-06-06 17:16:33 +00:00
Matthias Clasen
e70a4eef77 Avoid g_object_ref/unref in some cases
We only need to take a ref on the object when
we call out to external code (ie around
->dispatch_properties_changed). If we avoid
the signal emission, we can avoid the ref/unref
too. This is not currently happening, but
might in the future.
2022-06-06 12:33:34 -04:00
Matthias Clasen
19551ac983 Avoid g_type_class_peek
Most of the time, properties belong to the class
we set them on. Check that first, before going
into GType, which takes locks and whatnot.
2022-06-06 11:51:55 -04:00
Matthias Clasen
a4940943e5 Move deprecation warnings to object_set_property
A small reorg that reduces the code and matches
what we do for object_get_property.

Note that as a consequence of this change, we now
check the deprecated flag on the redirected property,
not on the original when setting properties. This
matches what we were already doing for getting
properties.
2022-06-06 11:29:16 -04:00
Matthias Clasen
d97c948567 Don't inline deprecation warnings
The code that emits property deprecation warnings
rarely runs, and doesn't need to be inlined
everywhere. It is enough to inline the check for
the deprecation flag.
2022-06-06 11:29:14 -04:00
Philip Withnall
d730cfcdf4 Merge branch 'gobject-speedup2' into 'main'
Simplify g_object_new_with_properties

See merge request GNOME/glib!2713
2022-06-06 15:18:01 +00:00
Philip Withnall
d7ff2b3ad1 Merge branch 'gobject-speedup5' into 'main'
Don't copy values in g_object_new

See merge request GNOME/glib!2716
2022-06-06 13:51:13 +00:00
Matthias Clasen
ec69e7c463 Simplify g_object_new_with_properties
This function was doing an unnecessary extra
copy of the GValues. We can avoid that.
2022-06-03 07:10:08 -04:00
Matthias Clasen
149b0f03b4 Simplify g_object_notify_by_pspec
Fold get_notify_pspec into its only caller.
2022-06-01 19:33:04 -04:00
Matthias Clasen
95caffcd26 Don't copy values during g_object_set
It is safe not to copy arguments here,
because we are not emitting any signals
before we are done setting the values
as properties.

This matches what we do for g_object_new now.
2022-06-01 18:22:10 -04:00
Matthias Clasen
d50bc3c1c3 Don't copy values in g_object_new
We can safely use the values without copying here.
This is safe because we are not emitting any
signals before we are done setting the values
as properties.
2022-06-01 18:20:41 -04:00
Philip Withnall
26409f19cd Add SPDX license headers for LGPL-2.1-or-later to various files
These have all been added manually, as I’ve finished all the files which
I can automatically detect.

All the license headers in this commit are for LGPL-2.1-or-later, and
all have been double-checked against the license paragraph in the file
header.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #1415
2022-06-01 12:44:23 +01:00
Matthias Clasen
72a682950d gobject: Drop an unused quark
quark_in_construction is only used if
we don't have per-instance flags.
2022-05-31 16:13:18 -04:00
Matthias Clasen
ecc641de71 Avoid malloc for construct params
Stack-allocate the GObjectConstructParams (except for
extreme cases), for a small speedup of object construction.
2022-05-26 09:47:23 -04:00
Matthias Clasen
f7119a7935 Keep a count of construct params
This avoids walking the construct params list
one extra time just to count when constructing
objects, for a small speedup of object construction
in the presence of construct params.
2022-05-26 09:47:23 -04:00
Matthias Clasen
c72de9dd99 gobject: Use value_is_valid
Avoid GValue transformation when we can, using
the new value_is_valid vfunc.

This is particularly useful for string properties,
where g_value_transform will make a copy of the string.
2022-05-25 14:25:18 +01:00
Matthias Clasen
b7be0f95a8 gobject: Speed up finalization
Move the warning about finalize-during-construction
to debug-only.
2022-05-23 09:20:46 -04:00
Matthias Clasen
fa8c7c0da6 gobject: Use g_datalist_id_remove_multiple
This speeds up object finalization a little.
2022-05-23 09:19:45 -04:00
Matthias Clasen
f2a085d8ea Avoid g_type_peek_value_table
In several places we do paired calls of g_value_init
and g_value_unset, both of which peek the value table.
We can avoid half of that cost by remembering the value
table, instead of looking it up again.

This uses the new G_VALUE_COLLECT_INIT2 macro.
2022-05-23 06:35:55 -04:00
Philip Withnall
3c14b6846d docs: Standardise property ID enums in examples
Drop the redundant `PROP_0` (which isn’t a real property) and initialise
the first member of the enum instead.

Add a typedef so that the enum type can be used in `switch` statements
in `get_property()` and `set_property()` vfuncs. This allows
`-Wswitch-enum` to be used to improve type safety.

The examples here don’t have `get_property()` or `set_property()`
vfuncs, but people might copy/paste the code to somewhere which does.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-05-20 12:57:36 +01:00
Philip Withnall
8f9c5090db docs: Use G_PARAM_STATIC_STRINGS in examples and explain it more
Make it a bit clearer in the documentation that using
`G_PARAM_STATIC_STRINGS` everywhere is a good thing.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2022-05-20 12:57:36 +01:00
Gabor Karsay
7e64004db0 docs: mark macros, flags, enums with percent sign 2022-03-04 16:21:55 +00:00
Emmanuel Fleury
c8274a51cd Fix defined but not used warning in gobject/gobject.c
gobject/gobject.c:1189:1: warning: ‘floating_check’ defined but not used
2021-12-17 15:30:18 +01:00
Emmanuele Bassi
1ec331266a Defer GObject::notify during object destruction
Notifying during object destruction is a dubious "feature": objects
might end up recreating a bunch of state just before clearing it;
language bindings might get spurious notifications during garbage
collection runs.

We freeze the notification queue before running the dispose() chain; if
the object was temporarily vivified during dispose, we thaw the
notification queue, otherwise we let the instance clear it when we
finalize it.

See: https://gitlab.gnome.org/GNOME/gjs/-/issues/445
2021-11-29 15:43:59 +00:00
Nishal Kulkarni
1529c2ca4d gobject: Use new g_newa0() function
Replace old `g_newa()` and `memset()` with `g_newa0()`
2021-11-26 12:24:23 +00:00
Philip Withnall
7a8756d247 gobject: Add advice on larger alignment requirements for GObject members
We now guarantee that GObjects will always be allocated at least as
aligned as the basic types. If you want to put an element in your
GObject which has higher alignment requirements, we can’t guarantee it
will be aligned*. If you need it to be aligned, you’ll need to put it on
the heap (aligned appropriately), or add appropriate padding in your
GObject struct.

*Actually, GSlice will guarantee that the whole GObject is aligned to at
least the power of 2 greater than or equal to the size of the GObject,
which means any element in the GObject struct should always be
appropriate aligned if the compiler pads it appropriately. If malloc()
is used, however, it doesn’t make that guarantee, so we can’t make that
guarantee overall.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1231
2021-11-17 11:56:20 +00:00
Philip Withnall
0749643daa gobject: Assert that GObjects are at least as aligned as basic types
See the reasoning in the patch for why we believe GObjects *are*
(already) as aligned as the basic types.

We want to make this guarantee so that it’s guaranteed to be safe for
people to ignore -Wcast-align warnings for GObjects which contain basic
types. This typically happens with gdouble on 32-bit ARM platforms.

The checks are slightly complicated by the need to support GObjects with
custom constructors. We should expect that a custom construction
function will chain up to g_object_constructor (which calls
g_type_create_instance() as normal), but it’s possible that someone has
done something crazy and uses a custom allocator which doesn’t return
with the same alignment as GSlice. Hand them a warning in that case. If
that is true, the code which uses their custom-constructed GObject can
presumably already deal with the alignment it gets given.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1231
2021-11-17 11:56:20 +00:00
Michael Catanzaro
98e0789fea Merge branch 'weak-refs-docs' into 'main'
gobject: Clarify behaviour of adding weak refs during disposal

See merge request GNOME/glib!2255
2021-10-27 23:37:22 +00:00
Philip Withnall
579ff9f6c9 Merge branch 'ebassi/floating-warning' into 'main'
Add a (diagnostic) warning for finalized floating objects

Closes #2489

See merge request GNOME/glib!2247
2021-09-21 11:09:16 +00:00
Marco Trevisan (Treviño)
468246bb3b gobject: Ensure an object has toggle references before notifying it
When an object with toggle reference is notifying a change we just
assume that this is true because of previous checks.
However, while locking, another thread may have removed the toggle
reference causing the waiting thread to abort (as no handler is set at
that point).

To avoid this, once we've got the toggle references mutex lock, check
again if the object has toggle reference, and if it's not the case
anymore just ignore the request.

Add a test that triggers this, it's not 100% happening because this is
of course timing related, but this is very close to the truth.

Fixes: #2394
2021-09-20 17:56:25 +02:00
Philip Withnall
3f1a1cdb78 Merge branch 'prop-set-speedups' into 'main'
Small optimization for g_object_set

See merge request GNOME/glib!2254
2021-09-20 12:47:00 +00:00
Philip Withnall
3b67d53227 gobject: Clarify behaviour of adding weak refs during disposal
The previous wording was not clear about what happens if a new weak ref
is taken during disposal (shortly after resurrecting the object with a
new strong ref, otherwise taking the weak ref is invalid).

See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2064/diffs#note_1270092

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #2390
2021-09-20 13:23:34 +01:00
Matthias Clasen
497986cdc1 Small optimization for g_object_setv
No need to call memset in the loop, we can just
initialize all the values in one go.

GtkBuilder is now using g_object_setv, so this
may improve application start times a bit.
2021-09-20 08:21:49 -04:00
Matthias Clasen
23e37e05d2 Small optimization for g_object_set
We've already followed the redirection, no need
to check for that again - just avoid notifying
non-readable properties.
2021-09-20 08:21:42 -04:00
Marco Trevisan (Treviño)
a7262d6357 gobject: Cleanup weak locations data as part of dispose
Weak locations were not fully cleaned on run_dispose() and after dispose
vfunc was called, so ensure that this is the case.

Fixes: #865
2021-09-17 12:28:01 +02:00
Marco Trevisan (Treviño)
e861f60dcb gobject: Cleanup weak locations when the last one has been removed
As per the previous change, an object that had weak locations set may
need to lock again the weak locations mutex during qdata cleanup, but
we can avoid this when we know we're removing the last location, by
removing the qdata entry and freeing the data.

In case a new location is needed for the same object, new data will be
added.

However, by doing this the weak locations during dispose may be
invalidated once the weak locations lock is passed, so check again if
this is the case while removing them.
2021-09-17 12:27:59 +02:00
Marco Trevisan (Treviño)
ea68b22135 gobject: Cleanup GWeakRef locations on object finalization
It can happen that a GWeakRef is added to an object while it's disposing
(or even during finalizing) and this may happen in a thread that (weak)
references an object while the disposal isn't completed yet or when
using toggle references and switching to GWeakRef on notification (as
the API suggests).

In such scenario the weak locations are not cleaned up when the object
is finalized, and will point to a free'd area.

So, during finalization and when we're sure that the object will be
destroyed for sure, check again if there are new weak locations and
unset them if any as part of the qdata destruction.
Do this adding a new utility function so that we can avoid duplicating
code to free the weak locations.

Added various tests simulating this case.

Fixes: #2390
2021-09-17 12:21:23 +02:00
Emmanuele Bassi
6fe0f98360 Add a (diagnostic) warning for finalized floating objects
GTK currently checks if a GtkWidget is finalized while still using a
floating reference—i.e. a widget was disposed without any parent
container owning it.

This warning can be useful to identify and trace ownership transfer
issues in libraries using initially unowned floating object types.

To avoid introducing constraints ex post, we can gate this check behind
both the G_ENABLE_DEBUG compile time flag for GLib, and behind the
G_ENABLE_DIAGNOSTIC environment variable run time check.

Fixes: #2489
2021-09-14 16:09:22 +01:00
Emmanuele Bassi
f62622fc7b docs: Start stanzas with a single paragraph
When rendering the contents of the GLib documentation stored inside the
introspection data, a common behaviour is to take the first paragraph as
a summary of the symbol being documented.

The documentation is assumed to be in Markdown format, which means:

 - paragraphs must be separated by newlines
 - lines that have an indentation of four or more spaces are considered
   code blocks
 - lines that start with a `#` are considered titles

This means we need to slightly tweak the documentation in our sources to
ensure that it can be rendered appropriately by tools that are not
gtk-doc.

See issue: #2365
2021-08-02 13:22:23 +01:00
Allison Ryan Lortie
3764c6730e GObject: add g_object_take_ref()
This works in the same way as g_variant_take_ref(), and for the same
reason.

Updated and Rebased by Nitin Wartkar <nitinwartkar58@gmail.com>

Closes #1112
2021-06-11 18:13:34 +05:30
Jasper St. Pierre
edb40c7171 gobject: Allow passing %NULL for @data in g_object_remove_toggle_ref
gjs has some situations where it's not always aware of the @data that
was passed into g_object_add_toggle_ref, so allow passing %NULL to
just match on @notify.

Rebased and updated by Nitin Wartkar

Closes #817
2021-05-27 21:09:45 +05:30
Emmanuele Bassi
e97ebfe628 Fix the transfer annotations for GClosure constructors
The GClosure constructors in GObject return an instance with a floating
reference.

Fixes: #2299
2021-01-19 11:36:01 +02:00
Sebastian Dröge
a2de4b2479 Clarify in g_object_weak_ref() docs that the callback is called during disposing and not finalizing
This especially has the effect that any GWeakRefs to the object will not
necessarily be set to NULL yet if called as part of
g_object_run_dispose() and not as part of g_object_unref().
2020-12-08 18:09:50 +02:00
Philip Withnall
77361ef45e Merge branch 'fix_extra_warnings' into 'master'
Fix more warnings

See merge request GNOME/glib!1765
2020-11-24 15:37:21 +00:00
Emmanuel Fleury
236d6281b8 Fix signedness warnings in gobject/gobject.c:g_object_new_internal()
gobject/gobject.c: In function ‘g_object_new_internal’:
gobject/gobject.c:1962:25: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1962 |           for (j = 0; j < n_params; j++)
      |                         ^
gobject/gobject.c:1989:21: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1989 |       for (i = 0; i < n_params; i++)
      |                     ^
2020-11-24 16:17:46 +01:00
Emmanuel Fleury
7753a0492b Fix signedness warnings in gobject/gobject.c:g_object_new_with_custom_constructor()
gobject/gobject.c: In function ‘g_object_new_with_custom_constructor’:
gobject/gobject.c:1836:21: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1836 |       for (j = 0; j < n_params; j++)
      |                     ^
gobject/gobject.c:1914:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
 1914 |   for (i = 0; i < n_params; i++)
      |                 ^
2020-11-24 16:17:46 +01:00
Emmanuel Fleury
d50d2098b5 Fix signedness warning in gobject/gobject.c:g_object_class_install_properties()
gobject/gobject.c: In function ‘g_object_class_install_properties’:
gobject/gobject.c:766:17: error: comparison of integer expressions of different signedness: ‘gint’ {aka ‘int’} and ‘guint’ {aka ‘unsigned int’}
  766 |   for (i = 1; i < n_pspecs; i++)
      |                 ^
2020-11-24 16:17:46 +01:00
Philip Withnall
9f041c9e05 Merge branch 'th/gobject-new-parameter-list' into 'master'
gobject: allocate parameter list for g_object_new_valist() entirely on stack

See merge request GNOME/glib!1385
2020-11-21 22:55:55 +00:00
Philip Withnall
08d04d0428 gobject: Drop unnecessary volatile qualifiers from internal variables
These variables were already (correctly) accessed atomically. The
`volatile` qualifier doesn’t help with that.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Helps: #600
2020-11-20 14:40:19 +00:00
Thomas Haller
fb3977e9b4 gobject: limit required stack by heap allocating parameter list in g_object_new_valist()
The previous code consumed a larger additional amount of stack space.
That is because it would allocate the temporary buffer for GValues on
the stack with "g_newa (GValue, 1)" and thus the required stack
space grew with the number of arguments. Granted, this is already
a variadic C function, so the caller already placed that many elements
on the stack. For example, on the stack there are the property names
and the pointers to the arguments, which should amount to roughly
O(n_args * 16) (on 64 bit, with pointers being 8 bytes large).

That is not bad, because it means in the previous version the stack space
would grow linear with the already used stack space. However, a GValue is
an additional 24 bytes (on 64 bit), which probably more than doubles the
required stack space. Let's avoid that, by allocating the temporary list
on the heap after a certain threshold. This probably more than doubles the
number of possible arguments before the stack overflows.

Also, previously the heap allocated "params" array only grew one element
per iteration. Of course, it is likely that libc anyway reallocates
the buffers by growing the space exponentially. So realloc(ptr, 1)
probably does not O() scale worse than doubling the buffer sizes ourselves.
However, it seems clearer to keep track of the allocated sizes ourself, and
only call realloc() when we determine that we are out of space.
Especially because we need to update the value pointers on reallocation.

Note that we now require a heap allocation both for the "params" and the
"values" list. Theoretically that could be combined by using one buffer
for both. But that would make the code more complicated.

Now we pre-allocate buffers for 16 elements on the stack. That
is (16 * (16 + 24) bytes (or 640 bytes) on the stack. I think that
is still acceptable.
2020-11-18 15:26:14 +01:00
Thomas Haller
355c99a077 object: use guint type for n_params argument of g_object_new_is_valid_property()
Two out of three callers pass the count argument from a variable
of type guint. And the third is currently an always positive gint.

We should use the correct integer type that matches the type as it
used otherwise.
2020-11-18 13:38:53 +01:00
Philip Withnall
4f2b1e250d gobject: Standardise on the term ‘instantiatable’
Rather than using a mixture of ‘instantiable’ and ‘instantiatable’
everywhere, standardise on the term which is already in the public API.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-11-02 17:22:01 +00:00
Philip Withnall
41937c3631 gobject: Document that g_object_new() zeros out private data
It’s already mentioned at
https://developer.gnome.org/gobject/stable/chapter-gobject.html#gobject-instantiation,
but is important enough that mentioning it in the documentation for
`g_object_new()` seems helpful.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Fixes: #1022
2020-07-27 01:57:57 +01:00
Philip Withnall
a63efa4291 tree: Fix various ableist language
In almost all cases, rewording the documentation/comments made things
more specific and a little clearer.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1544#note_846645
2020-06-23 10:49:44 +01:00
Philip Withnall
00bfb3ab44 tree: Fix various typos and outdated terminology
This was mostly machine generated with the following command:
```
codespell \
    --builtin clear,rare,usage \
    --skip './po/*' --skip './.git/*' --skip './NEWS*' \
    --write-changes .
```
using the latest git version of `codespell` as per [these
instructions](https://github.com/codespell-project/codespell#user-content-updating).

Then I manually checked each change using `git add -p`, made a few
manual fixups and dropped a load of incorrect changes.

There are still some outdated or loaded terms used in GLib, mostly to do
with git branch terminology. They will need to be changed later as part
of a wider migration of git terminology.

If I’ve missed anything, please file an issue!

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-06-12 15:01:08 +01:00
Edward Hervey
898baa07b1 gobject: Handle runtime checks as such
The various `g_strdup_printf()` returns values in the implementations of GValue
lcopy_func are runtime checks which could be disabled if one wants and therefore
should be handled as such with g_return_val_if_fail()
2020-05-20 17:37:46 +02:00