From 0b800257d98c3d5b5f3bb715e73c6ce4c75e341c Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 28 May 2009 18:21:27 +0100 Subject: [PATCH 3/3] fd.o #13908: silently initialize specialized types whenever required Colin Watson asks: "Is there a reason not to just do the initialization at the time someone calls one of the public API entry points that depends on it?". This turns out to be non-trivial because those public API entry points are themselves used inside the initialization. --- dbus/Makefile.am | 1 + dbus/dbus-gtype-specialized-priv.h | 48 +++++++++++++++++++++++++++ dbus/dbus-gtype-specialized.c | 64 ++++++++++++++++++++++++++---------- dbus/dbus-gvalue-utils.c | 11 +++--- 4 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 dbus/dbus-gtype-specialized-priv.h diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 668acd3..02ef3a3 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -25,6 +25,7 @@ CLEANFILES = $(BUILT_SOURCES) DBUS_GLIB_INTERNALS = \ dbus-gtype-specialized.c \ + dbus-gtype-specialized-priv.h \ dbus-gutils.c \ dbus-gutils.h \ dbus-gsignature.c \ diff --git a/dbus/dbus-gtype-specialized-priv.h b/dbus/dbus-gtype-specialized-priv.h new file mode 100644 index 0000000..4a52a54 --- /dev/null +++ b/dbus/dbus-gtype-specialized-priv.h @@ -0,0 +1,48 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-gtype-specialized-priv.h: internals of specialized GTypes + * + * Copyright (C) 2009 Collabora Ltd. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef DBUS_GOBJECT_TYPE_SPECIALIZED_PRIV_H +#define DBUS_GOBJECT_TYPE_SPECIALIZED_PRIV_H + +#include "dbus-gtype-specialized.h" + +G_BEGIN_DECLS + +G_GNUC_INTERNAL +void _dbus_g_type_register_collection (const char *name, + const DBusGTypeSpecializedCollectionVtable *vtable, + guint flags); + +G_GNUC_INTERNAL +void _dbus_g_type_register_map (const char *name, + const DBusGTypeSpecializedMapVtable *vtable, + guint flags); + +G_GNUC_INTERNAL +void _dbus_g_type_register_struct (const char *name, + const DBusGTypeSpecializedStructVtable *vtable, + guint flags); + +G_END_DECLS + +#endif diff --git a/dbus/dbus-gtype-specialized.c b/dbus/dbus-gtype-specialized.c index 220f603..408bd3c 100644 --- a/dbus/dbus-gtype-specialized.c +++ b/dbus/dbus-gtype-specialized.c @@ -22,7 +22,7 @@ * */ -#include "dbus-gtype-specialized.h" +#include "dbus-gtype-specialized-priv.h" #include "dbus-gvalue-utils.h" #include #include @@ -107,12 +107,6 @@ dbus_g_type_specialized_init (void) g_once (&once, specialized_init, NULL); } -static gboolean -specialized_types_is_initialized (void) -{ - return specialized_containers != NULL; -} - static DBusGTypeSpecializedData * lookup_specialization_data (GType type) { @@ -284,7 +278,16 @@ dbus_g_type_register_collection (const char *n const DBusGTypeSpecializedCollectionVtable *vtable, guint flags) { - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + + _dbus_g_type_register_collection (name, vtable, flags); +} + +void +_dbus_g_type_register_collection (const char *name, + const DBusGTypeSpecializedCollectionVtable *vtable, + guint flags) +{ register_container (name, DBUS_G_SPECTYPE_COLLECTION, (const DBusGTypeSpecializedVtable*) vtable); } @@ -301,7 +304,16 @@ dbus_g_type_register_map (const char *name, const DBusGTypeSpecializedMapVtable *vtable, guint flags) { - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + + _dbus_g_type_register_map (name, vtable, flags); +} + +void +_dbus_g_type_register_map (const char *name, + const DBusGTypeSpecializedMapVtable *vtable, + guint flags) +{ register_container (name, DBUS_G_SPECTYPE_MAP, (const DBusGTypeSpecializedVtable*) vtable); } @@ -318,7 +330,16 @@ dbus_g_type_register_struct (const char *name, const DBusGTypeSpecializedStructVtable *vtable, guint flags) { - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + + _dbus_g_type_register_struct (name, vtable, flags); +} + +void +_dbus_g_type_register_struct (const char *name, + const DBusGTypeSpecializedStructVtable *vtable, + guint flags) +{ register_container (name, DBUS_G_SPECTYPE_STRUCT, (const DBusGTypeSpecializedVtable*) vtable); } @@ -430,7 +451,7 @@ lookup_or_register_specialized (const char *container, char *name; const DBusGTypeSpecializedContainer *klass; - g_return_val_if_fail (specialized_types_is_initialized (), G_TYPE_INVALID); + dbus_g_type_specialized_init(); klass = g_hash_table_lookup (specialized_containers, container); g_return_val_if_fail (klass != NULL, G_TYPE_INVALID); @@ -705,7 +726,8 @@ gpointer dbus_g_type_specialized_construct (GType gtype) { DBusGTypeSpecializedData *data; - g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + + dbus_g_type_specialized_init(); data = lookup_specialization_data (gtype); g_return_val_if_fail (data != NULL, FALSE); @@ -729,7 +751,8 @@ dbus_g_type_collection_get_fixed (GValue *value, DBusGTypeSpecializedData *data; GType gtype; - g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + dbus_g_type_specialized_init(); + g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), FALSE); gtype = G_VALUE_TYPE (value); @@ -760,7 +783,8 @@ dbus_g_type_collection_value_iterate (const GValue *va DBusGTypeSpecializedData *data; GType gtype; - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); gtype = G_VALUE_TYPE (value); @@ -794,7 +818,8 @@ dbus_g_type_specialized_init_append (GValue *value, DBusGTypeSpecializedAppendCo GType gtype; DBusGTypeSpecializedData *specdata; - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); gtype = G_VALUE_TYPE (value); specdata = lookup_specialization_data (gtype); @@ -875,7 +900,8 @@ dbus_g_type_map_value_iterate (const GValue *value, DBusGTypeSpecializedData *data; GType gtype; - g_return_if_fail (specialized_types_is_initialized ()); + dbus_g_type_specialized_init(); + g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); gtype = G_VALUE_TYPE (value); @@ -907,7 +933,8 @@ dbus_g_type_struct_get_member (const GValue *value, DBusGTypeSpecializedData *data; GType gtype; - g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + dbus_g_type_specialized_init(); + g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), FALSE); gtype = G_VALUE_TYPE (value); @@ -938,7 +965,8 @@ dbus_g_type_struct_set_member (GValue *value, DBusGTypeSpecializedData *data; GType gtype; - g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + dbus_g_type_specialized_init(); + g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), FALSE); gtype = G_VALUE_TYPE (value); diff --git a/dbus/dbus-gvalue-utils.c b/dbus/dbus-gvalue-utils.c index 55f58b9..71365b9 100644 --- a/dbus/dbus-gvalue-utils.c +++ b/dbus/dbus-gvalue-utils.c @@ -23,6 +23,7 @@ #include #include "dbus/dbus-glib.h" +#include "dbus-gtype-specialized-priv.h" #include "dbus-gvalue-utils.h" #include "dbus-gtest.h" #include @@ -1203,11 +1204,11 @@ _dbus_g_type_specialized_builtins_init (void) valuearray_set_member }; - dbus_g_type_register_collection ("GSList", &slist_vtable, 0); - dbus_g_type_register_collection ("GArray", &array_vtable, 0); - dbus_g_type_register_collection ("GPtrArray", &ptrarray_vtable, 0); - dbus_g_type_register_map ("GHashTable", &hashtable_vtable, 0); - dbus_g_type_register_struct ("GValueArray", &valuearray_vtable, 0); + _dbus_g_type_register_collection ("GSList", &slist_vtable, 0); + _dbus_g_type_register_collection ("GArray", &array_vtable, 0); + _dbus_g_type_register_collection ("GPtrArray", &ptrarray_vtable, 0); + _dbus_g_type_register_map ("GHashTable", &hashtable_vtable, 0); + _dbus_g_type_register_struct ("GValueArray", &valuearray_vtable, 0); } #ifdef DBUS_BUILD_TESTS -- 1.6.3.1