From 6a0591f06c9a70d08503858117288d3d3736414a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 12 Dec 2022 18:00:10 +0100 Subject: [PATCH 1/4] gobject/tests/type-flags: Add tests for type final type flags Ensure that final flag is properly checked and used. --- gobject/tests/type-flags.c | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/gobject/tests/type-flags.c b/gobject/tests/type-flags.c index e4e501850..23313c68e 100644 --- a/gobject/tests/type-flags.c +++ b/gobject/tests/type-flags.c @@ -61,6 +61,22 @@ test_type_flags_final (void) { GType final2_type; + g_assert_true (G_TYPE_IS_FINAL (TEST_TYPE_FINAL)); + g_assert_true (g_type_test_flags (TEST_TYPE_FINAL, G_TYPE_FLAG_FINAL)); + g_assert_true (G_TYPE_IS_CLASSED (TEST_TYPE_FINAL)); + g_assert_true (g_type_test_flags (TEST_TYPE_FINAL, G_TYPE_FLAG_CLASSED)); + g_assert_true (G_TYPE_IS_INSTANTIATABLE (TEST_TYPE_FINAL)); + g_assert_true (g_type_test_flags (TEST_TYPE_FINAL, G_TYPE_FLAG_INSTANTIATABLE)); + g_assert_true (g_type_test_flags (TEST_TYPE_FINAL, + G_TYPE_FLAG_FINAL | + G_TYPE_FLAG_CLASSED | + G_TYPE_FLAG_INSTANTIATABLE)); + g_assert_false (g_type_test_flags (TEST_TYPE_FINAL, + G_TYPE_FLAG_FINAL | + G_TYPE_FLAG_CLASSED | + G_TYPE_FLAG_DEPRECATED | + G_TYPE_FLAG_INSTANTIATABLE)); + /* This is the message we print out when registering the type */ g_test_expect_message ("GLib-GObject", G_LOG_LEVEL_CRITICAL, "*cannot derive*"); @@ -102,6 +118,22 @@ test_deprecated_init (TestDeprecated *self) { } +static void +test_type_flags_final_instance_check (void) +{ + TestFinal *final; + + final = g_object_new (TEST_TYPE_FINAL, NULL); + g_assert_true (g_type_check_instance_is_a ((GTypeInstance *) final, + TEST_TYPE_FINAL)); + g_assert_false (g_type_check_instance_is_a ((GTypeInstance *) final, + TEST_TYPE_DEPRECATED)); + g_assert_true (g_type_check_instance_is_a ((GTypeInstance *) final, + G_TYPE_OBJECT)); + + g_clear_object (&final); +} + static void test_type_flags_deprecated (void) { @@ -119,6 +151,21 @@ test_type_flags_deprecated (void) g_assert_false (deprecated_type == G_TYPE_INVALID); g_assert_true (G_TYPE_IS_DEPRECATED (deprecated_type)); + g_assert_true (G_TYPE_IS_FINAL (deprecated_type)); + g_assert_true (g_type_test_flags (deprecated_type, G_TYPE_FLAG_FINAL)); + + g_assert_true (g_type_test_flags (deprecated_type, + G_TYPE_FLAG_DEPRECATED | + G_TYPE_FLAG_CLASSED | + G_TYPE_FLAG_FINAL | + G_TYPE_FLAG_INSTANTIATABLE)); + g_assert_false (g_type_test_flags (deprecated_type, + G_TYPE_FLAG_DEPRECATED | + G_TYPE_FLAG_CLASSED | + G_TYPE_FLAG_FINAL | + G_TYPE_FLAG_ABSTRACT | + G_TYPE_FLAG_INSTANTIATABLE)); + /* Instantiating it should work, but emit a warning. */ deprecated_object = g_object_new (deprecated_type, NULL); g_assert_nonnull (deprecated_object); @@ -131,6 +178,13 @@ test_type_flags_deprecated (void) deprecated_object = g_object_new (deprecated_type, NULL); g_assert_nonnull (deprecated_object); + g_assert_true (g_type_check_instance_is_a ((GTypeInstance *) deprecated_object, + TEST_TYPE_DEPRECATED)); + g_assert_true (g_type_check_instance_is_a ((GTypeInstance *) deprecated_object, + G_TYPE_OBJECT)); + g_assert_false (g_type_check_instance_is_a ((GTypeInstance *) deprecated_object, + TEST_TYPE_FINAL)); + g_test_assert_expected_messages (); g_object_unref (deprecated_object); @@ -144,6 +198,7 @@ main (int argc, char *argv[]) g_setenv ("G_ENABLE_DIAGNOSTIC", "1", TRUE); g_test_add_func ("/type/flags/final", test_type_flags_final); + g_test_add_func ("/type/flags/final/instance-check", test_type_flags_final_instance_check); g_test_add_func ("/type/flags/deprecated", test_type_flags_deprecated); return g_test_run (); From a31b042dfcdc7a0251aeb170ef4e0e6c056e9205 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 6 Jun 2022 17:26:38 -0400 Subject: [PATCH 2/4] 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. --- gobject/gtype.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gobject/gtype.c b/gobject/gtype.c index 65a23f81e..8a589d215 100644 --- a/gobject/gtype.c +++ b/gobject/gtype.c @@ -233,7 +233,9 @@ struct _TypeNode guint n_prerequisites : 9; guint is_classed : 1; guint is_instantiatable : 1; + guint is_final : 1; guint mutatable_check_cache : 1; /* combines some common path checks */ + GType *children; /* writable with lock */ TypeData *data; GQuark qname; @@ -3909,6 +3911,8 @@ type_add_flags_W (TypeNode *node, dflags = GPOINTER_TO_UINT (type_get_qdata_L (node, static_quark_type_flags)); dflags |= flags; type_set_qdata_W (node, static_quark_type_flags, GUINT_TO_POINTER (dflags)); + + node->is_final = (flags & G_TYPE_FLAG_FINAL) != 0; } /** @@ -4134,9 +4138,12 @@ g_type_check_instance_is_a (GTypeInstance *type_instance, if (!type_instance || !type_instance->g_class) return FALSE; - - node = lookup_type_node_I (type_instance->g_class->g_type); + iface = lookup_type_node_I (iface_type); + if (iface->is_final) + return type_instance->g_class->g_type == iface_type; + + node = lookup_type_node_I (type_instance->g_class->g_type); check = node && node->is_instantiatable && iface && type_node_conforms_to_U (node, iface, TRUE, FALSE); return check; From 5dc8d2ca0005eca3b33a4a4e0865d48a2c279b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 12 Dec 2022 18:09:16 +0100 Subject: [PATCH 3/4] 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. --- gobject/gtype.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gobject/gtype.c b/gobject/gtype.c index 8a589d215..72ddae25e 100644 --- a/gobject/gtype.c +++ b/gobject/gtype.c @@ -146,6 +146,13 @@ G_TYPE_FLAG_DERIVABLE | \ G_TYPE_FLAG_DEEP_DERIVABLE) #define TYPE_FLAG_MASK (G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT | G_TYPE_FLAG_FINAL | G_TYPE_FLAG_DEPRECATED) + +/* List the flags that are directly accessible via the TypeNode struct flags */ +#define NODE_FLAG_MASK ( \ + G_TYPE_FLAG_CLASSED | \ + G_TYPE_FLAG_INSTANTIATABLE | \ + G_TYPE_FLAG_FINAL) + #define SIZEOF_FUNDAMENTAL_INFO ((gssize) MAX (MAX (sizeof (GTypeFundamentalInfo), \ sizeof (gpointer)), \ sizeof (glong))) @@ -3994,6 +4001,20 @@ g_type_test_flags (GType type, node = lookup_type_node_I (type); if (node) { + if ((flags & ~NODE_FLAG_MASK) == 0) + { + if (flags & G_TYPE_FLAG_CLASSED) + result |= node->is_classed; + + if (flags & G_TYPE_FLAG_INSTANTIATABLE) + result |= node->is_instantiatable; + + if (flags & G_TYPE_FLAG_FINAL) + result |= node->is_final; + + return result; + } + guint fflags = flags & TYPE_FUNDAMENTAL_FLAG_MASK; guint tflags = flags & TYPE_FLAG_MASK; From f6ac7bc907ecbd18c779e107d33bfade6bec3d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Mon, 12 Dec 2022 18:18:02 +0100 Subject: [PATCH 4/4] gtype: Use is_final node bit to check if a type can be derived --- gobject/gtype.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/gobject/gtype.c b/gobject/gtype.c index 72ddae25e..77feb7d4a 100644 --- a/gobject/gtype.c +++ b/gobject/gtype.c @@ -797,6 +797,13 @@ check_derivation_I (GType parent_type, type_descriptive_name_I (parent_type)); return FALSE; } + if (pnode->is_final) + { + g_critical ("cannot derive '%s' from final parent type '%s'", + type_name, + NODE_NAME (pnode)); + return FALSE; + } finfo = type_node_fundamental_info_I (pnode); /* ensure flat derivability */ if (!(finfo->type_flags & G_TYPE_FLAG_DERIVABLE)) @@ -815,13 +822,6 @@ check_derivation_I (GType parent_type, NODE_NAME (pnode)); return FALSE; } - if ((G_TYPE_FLAG_FINAL & GPOINTER_TO_UINT (type_get_qdata_L (pnode, static_quark_type_flags))) == G_TYPE_FLAG_FINAL) - { - g_critical ("cannot derive '%s' from final parent type '%s'", - type_name, - NODE_NAME (pnode)); - return FALSE; - } return TRUE; }