Commit Graph

274 Commits

Author SHA1 Message Date
Philip Withnall
70a49e35cc
gtype: Move an assertion to help out the static analyser
scan-build is worried that `node->data->common.value_table->value_init`
will be a `NULL` pointer dereference in the assignment to
`node->mutatable_check_cache`.

There’s already an assertion immediately below to check against this, so
let’s move it up a line to help the static analyser out.

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

Helps: #1767
2024-04-25 23:16:22 +01:00
Philip Withnall
e9655c597a
gobject: Fix various implicit conversions from size_t to smaller types
Basically various trivial instances of the following MSVC compiler
warning:
```
../gio/gio-tool-set.c(50): warning C4267: '=': conversion from 'size_t' to 'int', possible loss of data
```

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
2024-04-25 12:39:46 +01:00
Alex Richardson
a121b1bf71 Change GType print placeholders to G_GUINTPTR_FORMAT
GType is not guaranteed to be the same as gsize, but it is representable
as guintptr.
2023-10-25 12:01:02 +01:00
Alex Richardson
d0e03f0930 GType: introduce GTYPE_TO_POINTER/GPOINTER_TO_TYPE
On CHERI-enabled systems we use uintptr_t as the underlying storage for
GType and therefore casting to gsize strips the upper bits from a pointer.
Fix this by casting via uintptr_t instead and introduce a new set of
macros to convert between GType and pointers.
2023-10-25 12:01:02 +01:00
Matthias Clasen
f3eac349f9 docs: Move the GType SECTION
Move the contents to the new types.md files.

Helps: #3037
2023-10-16 23:34:29 +01:00
Alex Richardson
a1dfecf11f Use g_once_init_{enter,leave}_pointer where appropriate
This should not result in any functional changes, but will eventually
allow glib to be functional on CHERI-enabled systems such as Morello.

Helps: https://gitlab.gnome.org/GNOME/glib/-/issues/2842
2023-10-04 13:57:16 +01:00
Christian Hergert
ab2fe494a4 gobject: inline G_TYPE_IS() macros within gtype.c
If you take a release build (--buildtype=release) previously of GLib,
functions such as g_type_create_instance() would call out to
g_type_test_flags() which you can see by disassembling and looking for the
call instruction to <g_type_test_flags> via the library ABI.

Now that the previous commit allows checking abstract and deprecated flags
it makes sense to let the compiler optimize those checks into a single
pass. This is only possible if the functions themselves are inlined.

Additionally, any time we have the same TypeNode we would want to be able
to reuse that node rather than re-locate it.
2023-10-03 09:44:32 +01:00
Christian Hergert
5de7dd5a4b gobject: cache flags needed for g_type_create_instance()
Every call to g_type_create_instance() currently will incur a RWLock at
least once, but usually twice to test for both G_TYPE_FLAG_ABSTRACT and
G_TYPE_FLAG_DEPRECATED.

Additionally, each call to g_type_instance_free() also checks for these.
That results in a synchronization of GTypeInstance creation across all
threads as well as being a huge amount of overhead when creating instances
like GskRenderNode.

With this patch in place, the next two biggest issues are
g_type_class_ref() and g_type_test_flags() not getting inlined within
gtype.c in release builds. We can address that separately though.

Sysprof shows that the RWLock, with this patch in place, falls off the
profiles.
2023-10-03 09:44:32 +01:00
Philip Withnall
d9b23c5466 gtype: Allow g_type_query() to be called on dynamic types
There’s no reason that anyone can think of that this should be
disallowed. It’s useful for language runtimes like GJS to be able to
find out the allocation size of dynamic GObjects.

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

Fixes: #623
2023-07-30 11:25:42 +03:00
Philip Withnall
dc95b911f3 docs: Drop outdated docs/debugging.txt
Most of the document was about traps, which have not existed since
commit 58cdf0b474, 10 years ago.

The rest of the document was about `GOBJECT_DEBUG`, and that information
would be more easily findable in the full GObject documentation — so
move it there and update it a bit.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2023-04-27 23:56:59 +01:00
badcel
993f6d6698
g_type_name: Fix return annotation
The function can return null so it should be annotated as nullable.
2023-02-21 22:19:56 +01:00
Philip Withnall
bd8b756853 Merge branch 'gtype-use-sized-free' into 'main'
Revert "gtype: Use the system allocator on UNIX" and use g_free_sized instead

See merge request GNOME/glib!3251
2023-02-06 14:00:38 +00:00
Emmanuele Bassi
8f92f742b9 Remove n_preallocs field from InstanceData
The GTypeInfo.n_preallocs field has been ignored since GLib 2.10, but it
hasn't been removed from the internal instance data representation.
2023-02-04 16:55:30 +01:00
Marco Trevisan (Treviño)
69e9ba80e2 gtype: Use g_malloc0 and g_free_sized to allocate and free memory
We've a sized free now, so we can just use it!
2023-02-03 11:38:00 +01:00
Marco Trevisan (Treviño)
ce1cba0884 Revert "gtype: Use the system allocator on UNIX"
Since commit 45b5a6c0 GSlice is just a wrapper to g_malloc0/g_free, so
there's no point to use a different implementation for UNIXes vs
windows.

This reverts commit 3b7af4dd5d.
2023-02-03 11:38:00 +01:00
Philip Withnall
251bab3e71 gobject: Add a missing NULL check for the return from lookup_type_node_I()
This can cause a `NULL` dereference on the next line if there is no
`TypeNode` for `iface_type`, for example if `iface_type ==
G_TYPE_INVALID`.

Unlikely, but possible since this API is public.

Spotted by Coverity.

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

Coverity CID: #1501602
2022-12-21 19:16:10 +00:00
Marco Trevisan (Treviño)
f6ac7bc907 gtype: Use is_final node bit to check if a type can be derived 2022-12-12 19:40:58 +01:00
Marco Trevisan (Treviño)
5dc8d2ca00 gtype: Simplify g_type_test_flags() for node flags checks only
And this will affect G_TYPE_IS_FINAL, G_TYPE_IS_CLASSED,
G_TYPE_IS_INSTANTIATABLE and mixes of them.
2022-12-12 19:03:48 +01:00
Matthias Clasen
a31b042dfc gtype: Speed up type checking for final types
Store the final flag directly in the TypeNode,
so we can get it cheaply, and use it to speed
up g_type_instance_is_a for final types.
2022-12-12 19:03:48 +01:00
Matthias Clasen
fd0dd9e93c gobject: Add G_TYPE_FLAG_DEPRECATED
This can be used to mark entire types as deprecated,
and trigger a warning when they are instantiated
and `G_ENABLE_DIAGNOSTIC=1` is set in the environment.

There's currently no convenient macros for defining
types with the new flag, but you can do:

```c
_G_DEFINE_TYPE_EXTENDED_BEGIN (GtkAppChooserWidget,
                               gtk_app_chooser_widget,
                               GTK_TYPE_WIDGET,
                               G_TYPE_FLAG_DEPRECATED)
...
_G_DEFINE_TYPE_EXTENDED_END ()
```

Includes a unit test by Philip Withnall.
2022-11-09 12:07:31 +00:00
sunce
ea1b2cba0c Fix function name typo in comment of gtype.c
The function adjusting private struct size to private struct offset
should be `g_type_class_adjust_private_offset`, instead of the
previously misspelled `g_type_class_add_instance_private` in comment.

Fixes #2791
2022-10-26 10:17:39 +08:00
Emmanuele Bassi
3b7af4dd5d gtype: Use the system allocator on UNIX
Instead of replacing the slice allocator wholesale, we can start phasing
it out by having GTypeInstance use the system allocator on operating
systems where we can assume good performance profiles.

We cannot commit to fully gutting GSlice in the cases where we might
still need it, like the G(S)List allocator and small, similarly-sized
data structures.

The main user of GSlice is still GTypeInstance/GObject, and those have
moved out of the sweet spot of GSlice's performance envelove over the
years, with larger instance sizes and private data.

See: #1079
2022-10-07 15:54:11 +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
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
22f51b87a2 gtype: Speed up g_type_is_a
As noticed by Christian Hergert: We can reduce
some overhead by checking for exact type
equality first. According to Christian, around
3% of g_type_is_a calls are exact equalities.
2022-05-20 09:08:09 -04:00
Matthias Clasen
c492f4aec2 gtype: small optimization
These warnings are never seen at runtime, so they
are evidently very unlikely. Tell the compiler that.
2022-05-20 07:20:19 -04:00
Gabor Karsay
7e64004db0 docs: mark macros, flags, enums with percent sign 2022-03-04 16:21:55 +00:00
Loic Le Page
84c5675cef Fix redefinition of local variable in gobject/gtype.c 2022-02-18 10:49:00 +01:00
Loic Le Page
42c77c7ac7 Enable full-static build on Windows
Glib cannot be built statically on Windows because glib, gobject and gio
modules need to perform specific initialization when DLL are loaded and
cleanup when unloaded. Those initializations and cleanups are performed
using the DllMain function which is not called with static builds.

Issue is known for a while and solutions were already proposed but never
merged (see: https://gitlab.gnome.org/GNOME/glib/-/issues/692). Last
patch is from version 2.36.x and since then the
"constructor/destructor" mechanism has been implemented and used in
other part of the system.

This patch takes back the old idea and updates it to the last version of
glib to allow static compilation on Windows.

WARNING: because DllMain doesn't exist anymore in static compilation
mode, there is no easy way of knowing when a Windows thread finishes.
This patch implements a workaround for glib threads created by calling
g_thread_new(), so all glib threads created through glib API will behave
exactly the same way in static and dynamic compilation modes.
Unfortunately, Windows threads created by using CreateThread() or
_beginthread/ex() will not work with glib TLS functions. If users need
absolutely to use a thread NOT created with glib API under Windows and
in static compilation mode, they should not use glib functions within
their thread or they may encounter memory leaks when the thread finishes.

This should not be an issue as users should use exclusively the glib API
to manipulate threads in order to be cross-platform compatible and this
would be very unlikely and cumbersome that they may mix up Windows native
threads API with glib one.

Closes #692
2022-01-26 10:14:02 +01:00
Egor Bychin
5419228f63 gtype: Fix pointer being dereferenced despite NULL check 2021-10-15 14:15:43 +03: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
Emmanuele Bassi
c07dd416cf gtype: Add a "final" flag
We want to have the ability to mark types that should not be derivable
even if they are in a deeply derivable type hierarchy; in other words,
leaf nodes in the types tree.
2021-07-26 20:13:32 +01:00
Emmanuel Fleury
bee07cebcd Fix missing initializer warning in gobject/gtype.c
gobject/gtype.c:1103:55: warning: missing field 'value_free' initializer
          static const GTypeValueTable zero_vtable = { NULL, };
                                                             ^
2021-05-27 10:16:52 +02:00
Philip Withnall
f10101b909 gobject: Use g_memdup2() instead of g_memdup() in obvious places
Convert all the call sites which use `g_memdup()`’s length argument
trivially (for example, by passing a `sizeof()`), so that they use
`g_memdup2()` instead.

In almost all of these cases the use of `g_memdup()` would not have
caused problems, but it will soon be deprecated, so best port away from
it.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Helps: #2319
2021-02-04 14:13:21 +00:00
Emmanuel Fleury
cc7540909f Fix signedness warning in gobject/gtype.c:iface_node_has_available_offset_L()
gobject/gtype.c: In function ‘iface_node_has_available_offset_L’:
gobject/gtype.c:1288:42: error: comparison of integer expressions of different signedness: ‘gsize’ {aka ‘long unsigned int’} and ‘int’
 1288 |   if (G_ATOMIC_ARRAY_DATA_SIZE (offsets) <= offset)
      |                                          ^~
2020-12-16 10:56:24 +01: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
e28d9defb1 Fix signedness warning in gobject/gtype.c:g_type_interface_add_prerequisite()
gobject/gtype.c: In function ‘g_type_interface_add_prerequisite’:
gobject/gtype.c:1607:21: error: comparison of integer expressions of different signedness: ‘guint’ {aka ‘unsigned int’} and ‘int’
 1607 |       for (i = 0; i < prerequisite_node->n_supers + 1; i++)
      |                     ^
2020-11-24 16:17:37 +01:00
Michael Catanzaro
fb6e10c959 gtype: suppress valgrind memory leak warnings
The problem occurs because we keep a pointer inside the allocated block,
instead of a pointer to the start of the block. This memory exists for
the lifetime of the application, so let's silence it.

This is probably abuse of VALGRIND_MALLOCLIKE_BLOCK(), which is really
intended for use in memory allocators, but gtype.c already uses it in
two other places, and it's a practical solution. I wrote another larger
fix for this issue that involves keeping an array of extra pointers when
running under valgrind. This is simpler.

Fix suggested by Philip Withnall

```
==180238== 16 bytes in 1 blocks are possibly lost in loss record 3,078 of 16,075
==180238==    at 0x483BB1A: calloc (vg_replace_malloc.c:762)
==180238==    by 0x5489495: g_malloc0 (gmem.c:132)
==180238==    by 0x5489754: g_malloc0_n (gmem.c:364)
==180238==    by 0x53FDBEE: type_set_qdata_W (gtype.c:3722)
==180238==    by 0x53FDEE8: type_add_flags_W (gtype.c:3787)
==180238==    by 0x53FC348: g_type_register_fundamental (gtype.c:2662)
==180238==    by 0x53D969B: _g_enum_types_init (genums.c:124)
==180238==    by 0x53FF058: gobject_init (gtype.c:4432)
==180238==    by 0x53FF082: gobject_init_ctor (gtype.c:4493)
==180238==    by 0x4010F29: call_init.part.0 (dl-init.c:72)
==180238==    by 0x4011030: call_init (dl-init.c:30)
==180238==    by 0x4011030: _dl_init (dl-init.c:119)
==180238==    by 0x4002149: ??? (in /usr/lib64/ld-2.30.so)
```

Fixes #2076
2020-11-24 08:10:58 -06:00
Emmanuel Fleury
6b0552bb89 Fix signedness warning in gobject/gtype.c:type_node_add_iface_entry_W()
gobject/gtype.c: In function ‘type_node_add_iface_entry_W’:
gobject/gtype.c:1379:21: error: comparison of integer expressions of different signedness: ‘guint’ {aka ‘unsigned int’} and ‘int’
 1379 |       for (i = 0; i < num_entries; i++)
      |                     ^
2020-11-22 17:10:53 +01:00
Emmanuel Fleury
f6e234fdb6 Fix signedness warning in gobject/gtype.c:lookup_iface_entry_I()
gobject/gtype.c: In function ‘lookup_iface_entry_I’:
gobject/gtype.c:599:17: error: comparison of integer expressions of different signedness: ‘int’ and ‘long unsigned int’
  599 |       if (index < IFACE_ENTRIES_N_ENTRIES (entries))
      |                 ^
gobject/gatomicarray.h:51:8: note: in definition of macro ‘G_ATOMIC_ARRAY_DO_TRANSACTION’
   51 |       {_C_;}         \
      |        ^~~
2020-11-22 17:10:53 +01:00
Philip Withnall
47da8ec5d9 gtype: Add some missing atomic accesses to init_state
Half of the references to `init_state` in `gtype.c` already correctly
accessed it atomically, but a couple didn’t. Drop the `volatile`
qualifier from its declaration, as that’s not necessary for atomic
access.

Note that this is the `init_state` in `TypeData`, *not* the `init_state`
in `IFaceEntry`.

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

Helps: #600
2020-11-20 14:41:07 +00:00
Philip Withnall
041dd8b70b gtype: Fix some typos in comments
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
2020-11-20 14:41:07 +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
Sebastian Dröge
ddef9ba2f8 Merge branch 'instantiatable' into 'master'
gobject: Standardise on the term ‘instantiatable’

See merge request GNOME/glib!1735
2020-11-13 15:29:07 +00:00
Benjamin Otte
98f0a5a7da gtype: Add g_type_interface_instantiatable_prerequisite()
This function returns the most specific instantiatable type
that is a prerequisite for a given interface.

This type is necessary in particular when dealing with GValues
because a GValue contains an instance of a type.

This commit includes tests for the new API.
2020-11-02 17:36:18 +00:00
Benjamin Otte
e1b5afb7c0 gtype: Fix typo in API comment 2020-11-02 17:36:18 +00: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
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
Simon McVittie
44c004c84e Normalize C source files to end with exactly one newline
Some editors automatically remove trailing blank lines, or
automatically add a trailing newline to avoid having a trailing
non-blank line that is not terminated by a newline. To avoid unrelated
whitespace changes when users of such editors contribute to GLib,
let's pre-emptively normalize all files.

Unlike more intrusive whitespace normalization like removing trailing
whitespace from each line, this seems unlikely to cause significant
issues with cherry-picking changes to stable branches.

Implemented by:

    find . -name '*.[ch]' -print0 | \
    xargs -0 perl -0777 -p -i -e 's/\n+\z//g; s/\z/\n/g'

Signed-off-by: Simon McVittie <smcv@collabora.com>
2020-06-10 09:48:02 +01: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