Commit Graph

1769 Commits

Author SHA1 Message Date
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
Sebastian Dröge
e992b9978e Merge branch 'value-interned' into 'master'
GValue: Add interned string support

Closes #2109

See merge request GNOME/glib!1497
2020-05-20 08:25:40 +00:00
Edward Hervey
73d7f35ce7 gbinding: Use new g_value_set_interned_string() API for performance
The property strings are interned already, so this potentially allows for faster
comparisons. The property strings were already not copied, as they were tagged
as static.
2020-05-19 17:52:55 +02:00
Edward Hervey
1a95ce84ed GValue: Add interned string support
This adds support to be able to explicitely stored interned strings into
G_TYPE_STRING GValue.

This is useful for cases where the user:
* *knows* the string to be stored in the GValue is canonical
* Wants to know whther the string stored is canonical

This allows:
* zero-cost GValue copy (the content is guaranteed to be unique and exist
  throughout the process life)
* zero-cost string equality checks (if both string GValue are interned, you just
  need to check the pointers for equality or not, instead of doing a strcmp).

Fixes #2109
2020-05-19 17:52:55 +02:00
Edward Hervey
c964749de6 test: Add string GValue tests
Tests creation, duplication, ownership and copies
2020-05-19 17:52:55 +02:00
Edward Hervey
e3efbd30a2 gvalue: Static strings should not be copied
When doing copies of GValue backed by static strings, the contents should not be copied
2020-05-19 17:52:55 +02:00
Philip Withnall
c1d7097d0a build: Drop unused G_ATOMIC_OP_MEMORY_BARRIER_NEEDED
See the previous commit.

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

Helps: #1323
2020-05-19 16:20:31 +01:00
Aleksander Morgado
ec6056e3ab glib-mkenums: allow optional 'since' tag
The glib-mkenums program allows generating code to handle enums/flags
with very different purposes. One of its purposes could be generating
per-enum/flag methods to be exposed in a library API, and while doing
that, it would be nice to have a way to specify in which API version
the enum/flag was introduced, so that the same version could be shown
in the generated API methods.

E.g. From the following code:

    /**
     * QmiWmsMessageProtocol:
     * @QMI_WMS_MESSAGE_PROTOCOL_CDMA: CDMA.
     * @QMI_WMS_MESSAGE_PROTOCOL_WCDMA: WCDMA.
     *
     * Type of message protocol.
     *
     * Since: 1.0
     */
    typedef enum { /*< since=1.0 >*/
        QMI_WMS_MESSAGE_PROTOCOL_CDMA  = 0x00,
        QMI_WMS_MESSAGE_PROTOCOL_WCDMA = 0x01
    } QmiWmsMessageProtocol;

The template would allow us to generate a method documented like this,
including the Since tag with the value given in the mkenums 'since' tag.

    /**
     * qmi_wms_message_protocol_get_string:
     * @val: a QmiWmsMessageProtocol.
     *
     * Gets the nickname string for the #QmiWmsMessageProtocol specified at @val.
     *
     * Returns: (transfer none): a string with the nickname, or %NULL if not found. Do not free the returned value.
     * Since: 1.0
     */
    const gchar *qmi_wms_message_protocol_get_string (QmiWmsMessageProtocol val);

Signed-off-by: Aleksander Morgado <aleksander@aleksander.es>
2020-05-14 17:00:54 +02:00
Xavier Claessens
10280deebd Meson: Override every dependency glib provides
Meson 0.54.0 added a new method meson.override_dependency() that must be
used to ensure dependency consistency. This patch ensures a project that
depends on glib will never link to a mix of system and subproject
libraries. It would happen in such cases:

The system has glib 2.40 installed, and a project does:
dependency('glib-2.0', version: '>=2.60',
  fallback: ['glib', 'glib_dep'])
dependency('gobject-2.0')

The first call will configure glib subproject because the system libglib
is too old, but the 2nd call will return system libgobject.

By overriding 'gobject-2.0' dependency while configuring glib subproject
during the first call, meson knows that on the 2nd call it must return
the subproject dependency instead of system dependency.

This also has the nice side effect that with Meson >0.54.0 an
application depending on glib can declare the fallback without knowing
the dependency variable name: dependency('glib-2.0', fallback: 'glib').
2020-04-05 00:34:04 -04:00
Martin Storsjö
fdfadfc276 Add missing 'extern' to the dllexport version of GLIB_VAR/GOBJECT_VAR
The __declspec(dllexport) attribute in itself doesn't imply
'extern' - thus any intended variable declaration with
GLIB_VAR/GOBJECT_VAR would actually be a variable definition. With
C compilers defaulting to -fcommon, this isn't an issue, but
upcoming compilers (GCC 10 and Clang 11) will default to -fno-common,
ending up with duplicate definitions of these variables.
2020-03-31 23:22:26 +03:00
Philip Withnall
641bbd0848 gobject: Clarify assertion failure on ref-after-finalize
Rename the variables involved so that people get a slightly more
obvious critical warning when they try to ref an object which has
already been finalised.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-03-23 11:48:22 +00:00
Philip Withnall
d49393e94d gparam: Clarify that parameter names must be non-empty
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-03-04 14:46:28 +00:00
Jehan
13d1697b67 gobject: Add g_{param_spec,signal}_is_valid_name() functions
Making this validation code public allows projects to validate a
GParamSpec name before creating it. While hard-coded GParamSpec don't
need this, we can't afford crashing the main program for dynamically
generated GParamSpec from user-created data.

In such case, we will need to validate the param names and return errors
instead of trying to create a GParamSpec with invalid names.

Includes modifications from Philip Withnall and Emmanuele Bassi to
rearrange the new function addition and split it into one function for
GParamSpecs and one for GSignals.
2020-03-04 14:46:28 +00:00
Philip Withnall
37a3da891b tests: Add some assertions to avoid unused variable warnings on Clang
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-02-24 16:18:58 +00:00
Philip Withnall
51acb01f73 gobject: Fix strict aliasing warnings with g_set_object()
When calling `g_set_object()` for a type derived from `GObject`, GCC 9.2
was giving the following strict aliasing warning:
```
../../source/malcontent/libmalcontent-ui/user-controls.c:1001:21: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 1001 |   if (g_set_object (&self->user, user))
/opt/gnome/install/include/glib-2.0/gobject/gobject.h:744:33: note: in definition of macro ‘g_set_object’
  744 |   (g_set_object) ((GObject **) (object_ptr), (GObject *) (new_object)) \
      |                                 ^~~~~~~~~~
```

This was due to the `(GObject **)` cast.

Pass the pointer through a union to squash this warning. We already do
some size and type checks of the dereferenced type, which should catch
casual errors. The `g_object_ref()` and `g_object_unref()` calls which
subsequently happen inside the `g_set_object()` function also do some
dynamic type checks.

Add a test.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2020-02-18 12:15:52 +00:00
Philip Withnall
2474c65037 docs: Clarify handling of 64-bit integer literals with g_object_new()
As with `g_variant_new()` (or any varargs function which takes integer
literals of differing widths), callers need to be careful to ensure
their integer literals have the right width.

Tweak the documentation for `g_object_new()`, `g_object_set()` and
`g_object_get()` to clarify this. The documentation for `g_object_get()`
shows that it is not subject to the same caveats, since it operates on
pointers.

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

Closes: #833
2019-12-17 12:23:19 +00:00
Emmanuele Bassi
829ec978ed Merge branch '650-signal-lookup-warnings' into 'master'
gsignal: Drop unnecessary warnings from g_signal_lookup()

Closes #650

See merge request GNOME/glib!1247
2019-12-16 12:41:31 +00:00
Marco Trevisan (Treviño)
ab25fa42cc gtype: Define auto-cleanup functions for Module class
While we automatically define cleanup functions for the module, we don't
do it for the module class.

This will allow to manage the ownership of the class when reffing it
without having to cast it to GTypeClass.
2019-12-13 20:07:51 +01:00
Philip Withnall
a07bc20557 gsignal: Drop unnecessary warnings from g_signal_lookup()
The `G_TYPE_IS_INSTANTIATABLE()` check is redundant with a precondition
on the function.

The `g_type_class_peek()` check seems like a pointless restriction: it
should be possible to check for a signal from a class init function.

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

Fixes: #650
2019-12-12 13:00:54 +00:00
Emmanuele Bassi
ec3589b93f Merge branch 'signal-underscores' into 'master'
Signal name handling improvements

See merge request GNOME/glib!1224
2019-12-12 12:10:55 +00:00
Benjamin Otte
276e927fd4 Revert last 4 commits
This reverts commits:
  5899c61ed2
  e994d45352
  eb20dec144
  ebec0dd359
which wer accidentally pushed to master instead of a branch
2019-11-26 00:54:15 +01:00
Benjamin Otte
5899c61ed2 value: Allow automatic transforms to/from interfaces
Use the new g_type_interface_instantiable_prerequisite() to check
compatibility for transform functions.

In particular, this allows interfaces (in my case GDK_TYPE_PAINTABLE) to
be transformed to/from any GObject type (in my case G_TYPE_OBJECT) using
the transform function registered to tranform between any 2 objects
(g_value_object_transform_value() does a type check and uses NULL if the
types don't match).

And this in turn allows be to g_object_bind_property() a gobject-typed
generic property (GtkListItem::item) to a GtkImage::paintable.
2019-11-25 20:06:49 +01:00
Benjamin Otte
e994d45352 closure: Support generic marshals for interface returns
Use the newly added g_type_interface_instantiable_prerequisite() to
allow closure return values being interfaces by looking up the
instantiable type for the interface and usings its GValue accessors.
2019-11-25 20:06:39 +01:00
Benjamin Otte
eb20dec144 gtype: Add g_type_interface_instantiable_prerequisite()
There is (at most) a single GType that is instantiable and a
prerequisite for an interface. This function returns that type.

This type is necessary in particular when dealing with GValues because a
GValue contains an instance of a type.
2019-11-25 20:06:39 +01:00
Benjamin Otte
ebec0dd359 gtype: Fix typo in API comment 2019-11-25 20:06:39 +01:00
Bastien Nocera
6fd2ea1dc6 docs: Fix "occurred" typos in API documentation 2019-11-21 13:07:08 +01:00
Philip Withnall
c577bc89cd gsignal: Warn if g_signal_lookup() is called on an invalid signal name
And add a test for it.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
463a75c11e signals: Add tests for constructing signals with invalid names
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
cce274c0aa signals: Add tests for g_signal_lookup() and g_signal_parse_name()
They’ve never previously been formally tested.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
ccbe9690d3 gsignal: Fix typos in GSignalMatchType documentation
Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
5e89ba5a74 gsignal: Clarify signal detail format
The format has never previously been specified. It can be anything, but
for sanity’s sake disallow empty strings.

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

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
fb5cd1828f signals: Use g_assert_*() in signals tests rather than g_assert()
It provides more useful output on failure, and isn’t compiled out when
building with `G_DISABLE_ASSERT`.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
152547eb10 binding: Use g_assert_*() in binding tests rather than g_assert()
It provides more useful output on failure, and isn’t compiled out when
building with `G_DISABLE_ASSERT`.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
f43f5892f5 binding: Rename some test properties to include hyphens
This will allow subsequent testing of property name canonicalisation.

This test introduces no functional changes.

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

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
89f955db2d gsignal: Canonicalise signal names at installation time
Rather than adding a canonicalised and non-canonicalised version of the
signal to `g_signal_key_bsa`, just add the canonicalised version. Signal
lookups always use the canonicalised key (since the previous commit).

This saves space in `g_signal_key_bsa`, which should speed up lookups;
and it saves significant space in the global `GQuark` table (a 9.6%
reduction in entries in that table, by a rough test using
gnome-software).

We have to be a little more relaxed on the signal name validation than
we are for property name validation, as GTK installs a
`-gtk-private-changed` signal which violates the signal naming rules.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
875e2afa55 gsignal: Canonicalise signal name when looking it up in an object
Previously, we’d look up the signal name as passed to (for example)
`g_signal_lookup()`, and rely on the fact that signals are inserted
twice into `g_signal_key_bsa`; once in canonical form and once not.

In preparation for only inserting signals into `g_signal_key_bsa` once,
we now try looking up a signal with the given signal name and, if that
fails, try canonicalising the name and trying again.

This is a performance hit on lookups for non-canonical names, but
shouldn’t affect the performance of lookups for canonical names. If
people want performance, they should use canonical names.

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

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
90b51805b7 gsignal: Fold g_quark_try_string() call into signal_id_lookup()
This eliminates a call from every call site of signal_id_lookup(). It
introduces no functional changes, but allows subsequent refactoring.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
b8eb6025f1 gsignal: Tidy up signal naming documentation
Since signal names are the same as property names, reference between the
two. Improve the formatting, and make it clearer that `_` is
discouraged.

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

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
26970edc9f gbinding: Avoid a string copy of interned property names
Interned strings are never freed, so we don’t need to take a copy of
them when returning them in a #GValue. This is a minor memory allocation
improvement, with no functional changes.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
ae27f50342 gbinding: Canonicalise source and target properties
Rather than interning a property name string which isn’t canonicalised,
canonicalise it first, and enforce stricter validation on inputs.

The previous code was not incorrect (since the property machinery would
have canonicalised the property names itself, internally), but would
have resulted in non-canonical property names getting into the GQuark
table unnecessarily. With the new code, the interned property names from
property installation time should be consistently reused.

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

Helps: #358
2019-11-15 11:06:14 +00:00
Philip Withnall
30e630c9df gparam: Tighten up property name validation
Inline with the stricter version of the property naming rules from the
documentation, tighten up the validation of property names at
installation time.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-15 11:06:14 +00:00
Philip Withnall
c0e5c6c62c tests: Use g_assert_*() in param tests rather than g_assert()
It provides more useful output on failure, and isn’t compiled out when
building with `G_DISABLE_ASSERT`.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
2019-11-12 19:45:03 +00:00
Philip Withnall
0815da8674 tests: Rework GParamSpec canonicalisation test
The documentation says that parameter names must be alphanumeric (plus
`-` or `_`) and that canonicalisation turns `_` into `-`.

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

Helps: #358
2019-11-12 19:45:03 +00:00
Philip Withnall
b080b456a6 gparam: Tidy up property naming documentation
There’s no need to have the property naming documentation in two places,
with one version of it being stricter than the other. Rationalise it to
one place, link to that consistently, and settle on the stricter
version.

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

Helps: #358
2019-11-12 19:45:03 +00:00
Thomas Haller
3a9bdcf704 gparam: fix memory leak in g_param_value_defaults()
We cannot  just call

    G_PARAM_SPEC_GET_CLASS (pspec)->value_set_default (pspec, &dflt_value);

without initializing the GValue first. It would call
param_string_set_default(), which would set the pointer value
to a cloned string (which later never gets released, because
the GValue is not known to hold a string).

Fixes: 6ad799ac67
2019-11-08 21:24:38 +01:00
Philip Withnall
b43bfcaa68 Merge branch 'use-gobject-hole' into 'master'
Use the GObject hole on 64bit arches for some flags to improve performance

See merge request GNOME/glib!1083
2019-11-07 08:15:03 +00:00
Philip Withnall
1503547766 Merge branch 'param-value-default' into 'master'
Allow using an empty GValue with g_param_value_set_default()

See merge request GNOME/glib!1186
2019-10-31 10:22:57 +00:00
Philip Withnall
1a3bba4670 gparamspecs: Fix type class leaks on error handling paths
Signed-off-by: Philip Withnall <withnall@endlessm.com>

Helps: #1911
2019-10-28 14:57:33 +00:00
Emmanuele Bassi
f7824da85f Do not validate a GValue initialized with the default
There's really no point in going through validation, if we know the
value we're validating is coming straight from the GParamSpec.
2019-10-26 14:04:26 +01:00
Emmanuele Bassi
6ad799ac67 Constify g_param_value_defaults() argument
The GValue we pass in is supposed to not be modified by the GParamSpec.
2019-10-26 14:03:16 +01:00