mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-09-27 17:52:58 +02:00
Add simple instance count facility
Add GOBJECT_DEBUG=instance-count which enables internal accounting of the number of instances of each GType, and g_type_get_instance_count() to retrieve the result. https://bugzilla.gnome.org/show_bug.cgi?id=354457
This commit is contained in:
committed by
Matthias Clasen
parent
aa401aef87
commit
011bf876e7
@@ -33,6 +33,9 @@
|
|||||||
|
|
||||||
#include "gconstructor.h"
|
#include "gconstructor.h"
|
||||||
|
|
||||||
|
#ifdef G_ENABLE_DEBUG
|
||||||
|
#define IF_DEBUG(debug_type) if (_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type)
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SECTION:gtype
|
* SECTION:gtype
|
||||||
@@ -223,6 +226,9 @@ typedef enum
|
|||||||
struct _TypeNode
|
struct _TypeNode
|
||||||
{
|
{
|
||||||
guint volatile ref_count;
|
guint volatile ref_count;
|
||||||
|
#ifdef G_ENABLE_DEBUG
|
||||||
|
guint volatile instance_count;
|
||||||
|
#endif
|
||||||
GTypePlugin *plugin;
|
GTypePlugin *plugin;
|
||||||
guint n_children; /* writable with lock */
|
guint n_children; /* writable with lock */
|
||||||
guint n_supers : 8;
|
guint n_supers : 8;
|
||||||
@@ -506,7 +512,6 @@ type_node_any_new_W (TypeNode *pnode,
|
|||||||
node->data = NULL;
|
node->data = NULL;
|
||||||
node->qname = g_quark_from_string (name);
|
node->qname = g_quark_from_string (name);
|
||||||
node->global_gdata = NULL;
|
node->global_gdata = NULL;
|
||||||
|
|
||||||
g_hash_table_insert (static_type_nodes_ht,
|
g_hash_table_insert (static_type_nodes_ht,
|
||||||
(gpointer) g_quark_to_string (node->qname),
|
(gpointer) g_quark_to_string (node->qname),
|
||||||
(gpointer) type);
|
(gpointer) type);
|
||||||
@@ -1864,6 +1869,13 @@ g_type_create_instance (GType type)
|
|||||||
if (node->data->instance.instance_init)
|
if (node->data->instance.instance_init)
|
||||||
node->data->instance.instance_init (instance, class);
|
node->data->instance.instance_init (instance, class);
|
||||||
|
|
||||||
|
#ifdef G_ENABLE_DEBUG
|
||||||
|
IF_DEBUG (INSTANCE_COUNT)
|
||||||
|
{
|
||||||
|
g_atomic_int_inc ((int *) &node->instance_count);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TRACE(GOBJECT_OBJECT_NEW(instance, type));
|
TRACE(GOBJECT_OBJECT_NEW(instance, type));
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
@@ -1934,6 +1946,13 @@ g_type_free_instance (GTypeInstance *instance)
|
|||||||
else
|
else
|
||||||
g_slice_free1 (private_size + ivar_size, allocated);
|
g_slice_free1 (private_size + ivar_size, allocated);
|
||||||
|
|
||||||
|
#ifdef G_ENABLE_DEBUG
|
||||||
|
IF_DEBUG (INSTANCE_COUNT)
|
||||||
|
{
|
||||||
|
g_atomic_int_add ((int *) &node->instance_count, -1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
g_type_class_unref (class);
|
g_type_class_unref (class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3807,6 +3826,34 @@ g_type_query (GType type,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_type_get_instance_count:
|
||||||
|
* @type: a #GType
|
||||||
|
*
|
||||||
|
* Returns the number of instances allocated of the particular type;
|
||||||
|
* this is only available if GLib is built with debugging support and
|
||||||
|
* the instance_count debug flag is set (by setting the GOBJECT_DEBUG
|
||||||
|
* variable to include instance-count).
|
||||||
|
*
|
||||||
|
* Returns: the number of instances allocated of the given type;
|
||||||
|
* if instance counts are not available, returns 0.
|
||||||
|
*
|
||||||
|
* Since: 2.44
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
g_type_get_instance_count (GType type)
|
||||||
|
{
|
||||||
|
#ifdef G_ENABLE_DEBUG
|
||||||
|
TypeNode *node;
|
||||||
|
|
||||||
|
node = lookup_type_node_I (type);
|
||||||
|
g_return_if_fail (node != NULL);
|
||||||
|
|
||||||
|
return g_atomic_int_get (&node->instance_count);
|
||||||
|
#else
|
||||||
|
return 0;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/* --- implementation details --- */
|
/* --- implementation details --- */
|
||||||
gboolean
|
gboolean
|
||||||
@@ -4337,6 +4384,7 @@ gobject_init_ctor (void)
|
|||||||
{
|
{
|
||||||
GDebugKey debug_keys[] = {
|
GDebugKey debug_keys[] = {
|
||||||
{ "objects", G_TYPE_DEBUG_OBJECTS },
|
{ "objects", G_TYPE_DEBUG_OBJECTS },
|
||||||
|
{ "instance-count", G_TYPE_DEBUG_INSTANCE_COUNT },
|
||||||
{ "signals", G_TYPE_DEBUG_SIGNALS },
|
{ "signals", G_TYPE_DEBUG_SIGNALS },
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -4817,3 +4865,4 @@ g_type_ensure (GType type)
|
|||||||
if (G_UNLIKELY (type == (GType)-1))
|
if (G_UNLIKELY (type == (GType)-1))
|
||||||
g_error ("can't happen");
|
g_error ("can't happen");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -656,6 +656,7 @@ struct _GTypeQuery
|
|||||||
* @G_TYPE_DEBUG_OBJECTS: Print messages about object bookkeeping
|
* @G_TYPE_DEBUG_OBJECTS: Print messages about object bookkeeping
|
||||||
* @G_TYPE_DEBUG_SIGNALS: Print messages about signal emissions
|
* @G_TYPE_DEBUG_SIGNALS: Print messages about signal emissions
|
||||||
* @G_TYPE_DEBUG_MASK: Mask covering all debug flags
|
* @G_TYPE_DEBUG_MASK: Mask covering all debug flags
|
||||||
|
* @G_TYPE_DEBUG_INSTANCE_COUNT: Keep a count of instances of each type
|
||||||
*
|
*
|
||||||
* These flags used to be passed to g_type_init_with_debug_flags() which
|
* These flags used to be passed to g_type_init_with_debug_flags() which
|
||||||
* is now deprecated.
|
* is now deprecated.
|
||||||
@@ -670,7 +671,8 @@ typedef enum /*< skip >*/
|
|||||||
G_TYPE_DEBUG_NONE = 0,
|
G_TYPE_DEBUG_NONE = 0,
|
||||||
G_TYPE_DEBUG_OBJECTS = 1 << 0,
|
G_TYPE_DEBUG_OBJECTS = 1 << 0,
|
||||||
G_TYPE_DEBUG_SIGNALS = 1 << 1,
|
G_TYPE_DEBUG_SIGNALS = 1 << 1,
|
||||||
G_TYPE_DEBUG_MASK = 0x03
|
G_TYPE_DEBUG_INSTANCE_COUNT = 1 << 2,
|
||||||
|
G_TYPE_DEBUG_MASK = 0x07
|
||||||
} GTypeDebugFlags;
|
} GTypeDebugFlags;
|
||||||
|
|
||||||
|
|
||||||
@@ -738,6 +740,8 @@ GLIB_AVAILABLE_IN_ALL
|
|||||||
void g_type_query (GType type,
|
void g_type_query (GType type,
|
||||||
GTypeQuery *query);
|
GTypeQuery *query);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_44
|
||||||
|
int g_type_get_instance_count (GType type);
|
||||||
|
|
||||||
/* --- type registration --- */
|
/* --- type registration --- */
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user