object: Remove the type member of CoglObjectClass

Unlike in GObject the type number for a CoglObject is entirely an
internal implementation detail so there is no need to make a GQuark to
make it safe to export out of the library. Instead we can just
directly use a fixed pointer address as the identifier for the type.
This patch makes it use the address of the class struct of the
identifier. This should make it faster to do type checks because it
does not need to call a function every time it wants to get the type
number.

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Neil Roberts 2012-01-24 16:24:26 +00:00
parent c39333a2c6
commit 139421de19
15 changed files with 37 additions and 86 deletions

View File

@ -55,9 +55,6 @@ struct _CoglAtlasTexture
CoglHandle sub_texture;
};
GQuark
_cogl_handle_atlas_texture_get_type (void);
CoglHandle
_cogl_atlas_texture_new_from_bitmap (CoglBitmap *bmp,
CoglTextureFlags flags,

View File

@ -102,13 +102,12 @@ struct _CoglBuffer
/* This is used to register a type to the list of handle types that
will be considered a texture in cogl_is_texture() */
void
_cogl_buffer_register_buffer_type (GQuark type);
_cogl_buffer_register_buffer_type (const CoglObjectClass *klass);
#define COGL_BUFFER_DEFINE(TypeName, type_name) \
COGL_OBJECT_DEFINE_WITH_CODE \
(TypeName, type_name, \
_cogl_buffer_register_buffer_type (_cogl_object_ \
## type_name ## _get_type ()))
_cogl_buffer_register_buffer_type (&_cogl_##type_name##_class))
void
_cogl_buffer_initialize (CoglBuffer *buffer,

View File

@ -78,12 +78,11 @@
*/
void
_cogl_buffer_register_buffer_type (GQuark type)
_cogl_buffer_register_buffer_type (const CoglObjectClass *klass)
{
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
ctx->buffer_types = g_slist_prepend (ctx->buffer_types,
GINT_TO_POINTER (type));
ctx->buffer_types = g_slist_prepend (ctx->buffer_types, (void *) klass);
}
gboolean
@ -98,7 +97,7 @@ cogl_is_buffer (const void *object)
return FALSE;
for (l = ctx->buffer_types; l; l = l->next)
if (GPOINTER_TO_INT (l->data) == obj->klass->type)
if (l->data == obj->klass)
return TRUE;
return FALSE;

View File

@ -109,7 +109,7 @@ typedef struct _CoglFramebufferStackEntry
CoglFramebuffer *read_buffer;
} CoglFramebufferStackEntry;
extern GQuark _cogl_object_onscreen_get_type (void);
extern CoglObjectClass _cogl_onscreen_class;
static void _cogl_offscreen_free (CoglOffscreen *offscreen);
@ -136,8 +136,8 @@ _cogl_is_framebuffer (void *object)
if (obj == NULL)
return FALSE;
return obj->klass->type == _cogl_object_onscreen_get_type ()
|| obj->klass->type == _cogl_object_offscreen_get_type ();
return (obj->klass == &_cogl_onscreen_class ||
obj->klass == &_cogl_offscreen_class);
}
void

View File

@ -51,8 +51,8 @@ typedef void (*CoglUserDataDestroyInternalCallback) (void *user_data,
typedef struct _CoglObjectClass
{
GQuark type;
void *virt_free;
const char *name;
void *virt_free;
} CoglObjectClass;
#define COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES 2
@ -95,18 +95,18 @@ struct _CoglObject
#define _COGL_OBJECT_DEBUG_REF(type_name, object) G_STMT_START { \
CoglObject *__obj = (CoglObject *)object; \
COGL_NOTE (HANDLE, "COGL %s REF %p %i", \
g_quark_to_string ((__obj)->klass->type), \
(__obj)->klass->name, \
(__obj), (__obj)->ref_count); } G_STMT_END
#define _COGL_OBJECT_DEBUG_UNREF(type_name, object) G_STMT_START { \
CoglObject *__obj = (CoglObject *)object; \
COGL_NOTE (HANDLE, "COGL %s UNREF %p %i", \
g_quark_to_string ((__obj)->klass->type), \
(__obj)->klass->name, \
(__obj), (__obj)->ref_count - 1); } G_STMT_END
#define COGL_OBJECT_DEBUG_FREE(obj) \
COGL_NOTE (HANDLE, "COGL %s FREE %p", \
g_quark_to_string ((obj)->klass->type), (obj))
(obj)->klass->name, (obj))
#else /* !COGL_OBJECT_DEBUG */
@ -125,7 +125,7 @@ struct _CoglObject
#define COGL_OBJECT_COMMON_DEFINE_WITH_CODE(TypeName, type_name, code) \
\
static CoglObjectClass _cogl_##type_name##_class; \
CoglObjectClass _cogl_##type_name##_class; \
static unsigned long _cogl_object_##type_name##_count; \
\
static inline void \
@ -147,34 +147,6 @@ _cogl_object_##type_name##_indirect_free (CoglObject *obj) \
_cogl_object_##type_name##_dec (); \
} \
\
GQuark \
_cogl_object_##type_name##_get_type (void) \
{ \
static GQuark type = 0; \
if (!type) \
{ \
type = g_quark_from_static_string ("Cogl"#TypeName); \
_cogl_object_##type_name##_count = 0; \
\
if (_cogl_debug_instances == NULL) \
_cogl_debug_instances = \
g_hash_table_new (g_str_hash, g_str_equal); \
\
g_hash_table_insert (_cogl_debug_instances, \
"Cogl"#TypeName, \
&_cogl_object_##type_name##_count); \
\
{ code; } \
} \
return type; \
} \
\
GQuark \
_cogl_handle_##type_name##_get_type (void) \
{ \
return _cogl_object_##type_name##_get_type (); \
} \
\
static Cogl##TypeName * \
_cogl_##type_name##_object_new (Cogl##TypeName *new_obj) \
{ \
@ -185,11 +157,23 @@ _cogl_##type_name##_object_new (Cogl##TypeName *new_obj) \
obj->user_data_array = NULL; \
\
obj->klass = &_cogl_##type_name##_class; \
if (!obj->klass->type) \
if (!obj->klass->virt_free) \
{ \
obj->klass->type = _cogl_object_##type_name##_get_type (); \
_cogl_object_##type_name##_count = 0; \
\
if (_cogl_debug_instances == NULL) \
_cogl_debug_instances = \
g_hash_table_new (g_str_hash, g_str_equal); \
\
obj->klass->virt_free = \
_cogl_object_##type_name##_indirect_free; \
obj->klass->name = "Cogl"#TypeName, \
\
g_hash_table_insert (_cogl_debug_instances, \
(void *) obj->klass->name, \
&_cogl_object_##type_name##_count); \
\
{ code; } \
} \
\
_cogl_object_##type_name##_inc (); \
@ -215,8 +199,7 @@ cogl_is_##type_name (void *object) \
if (object == NULL) \
return FALSE; \
\
return (obj->klass->type == \
_cogl_object_##type_name##_get_type ()); \
return obj->klass == &_cogl_##type_name##_class; \
}
#define COGL_OBJECT_INTERNAL_DEFINE_WITH_CODE(TypeName, type_name, code) \
@ -231,8 +214,7 @@ _cogl_is_##type_name (void *object) \
if (object == NULL) \
return FALSE; \
\
return (obj->klass->type == \
_cogl_object_##type_name##_get_type ()); \
return obj->klass == &_cogl_##type_name##_class; \
}
#define COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING(type_name) \

View File

@ -50,6 +50,7 @@ struct _CoglOnscreen
void *winsys;
};
CoglOnscreen *
_cogl_onscreen_new (void);
@ -57,7 +58,4 @@ void
_cogl_framebuffer_winsys_update_size (CoglFramebuffer *framebuffer,
int width, int height);
GQuark
_cogl_object_onscreen_get_type (void);
#endif /* __COGL_ONSCREEN_PRIVATE_H */

View File

@ -47,9 +47,6 @@ struct _CoglPixelBuffer
unsigned int stride;
};
GQuark
_cogl_handle_pixel_buffer_get_type (void);
G_END_DECLS
#endif /* __COGL_PIXEL_BUFFER_PRIVATE_H__ */

View File

@ -54,7 +54,4 @@ struct _CoglSubTexture
int sub_height;
};
GQuark
_cogl_handle_sub_texture_get_type (void);
#endif /* __COGL_SUB_TEXTURE_PRIVATE_H */

View File

@ -52,9 +52,6 @@ struct _CoglTexture2D
CoglTexturePixel first_pixel;
};
GQuark
_cogl_handle_texture_2d_get_type (void);
CoglHandle
_cogl_texture_2d_new_from_bitmap (CoglBitmap *bmp,
CoglTextureFlags flags,

View File

@ -43,9 +43,6 @@ struct _CoglTexture2DSliced
int height;
};
GQuark
_cogl_handle_texture_2d_sliced_get_type (void);
CoglTexture2DSliced *
_cogl_texture_2d_sliced_new_from_foreign (GLuint gl_handle,
GLenum gl_target,

View File

@ -58,9 +58,6 @@ struct _CoglTexture3D
CoglTexturePixel first_pixel;
};
GQuark
_cogl_handle_texture_3d_get_type (void);
/*
* cogl_texture_3d_new_from_bitmap:
* @bmp_handle: A #CoglHandle to a bitmap.

View File

@ -168,19 +168,17 @@ _cogl_texture_free (CoglTexture *texture);
/* This is used to register a type to the list of handle types that
will be considered a texture in cogl_is_texture() */
void
_cogl_texture_register_texture_type (GQuark type);
_cogl_texture_register_texture_type (const CoglObjectClass *klass);
#define COGL_TEXTURE_DEFINE(TypeName, type_name) \
COGL_HANDLE_DEFINE_WITH_CODE \
(TypeName, type_name, \
_cogl_texture_register_texture_type (_cogl_handle_ \
## type_name ## _get_type ()))
_cogl_texture_register_texture_type (&_cogl_##type_name##_class))
#define COGL_TEXTURE_INTERNAL_DEFINE(TypeName, type_name) \
COGL_HANDLE_INTERNAL_DEFINE_WITH_CODE \
(TypeName, type_name, \
_cogl_texture_register_texture_type (_cogl_handle_ \
## type_name ## _get_type ()))
_cogl_texture_register_texture_type (&_cogl_##type_name##_class))
gboolean
_cogl_texture_can_hardware_repeat (CoglTexture *texture);

View File

@ -47,9 +47,6 @@ struct _CoglTextureRectangle
gboolean is_foreign;
};
GQuark
_cogl_handle_texture_rectangle_get_type (void);
CoglTextureRectangle *
_cogl_texture_rectangle_new_from_bitmap (CoglBitmap *bmp,
CoglTextureFlags flags,

View File

@ -70,12 +70,11 @@ cogl_texture_error_quark (void)
*/
void
_cogl_texture_register_texture_type (GQuark type)
_cogl_texture_register_texture_type (const CoglObjectClass *klass)
{
_COGL_GET_CONTEXT (ctxt, NO_RETVAL);
ctxt->texture_types = g_slist_prepend (ctxt->texture_types,
GINT_TO_POINTER (type));
ctxt->texture_types = g_slist_prepend (ctxt->texture_types, (void *) klass);
}
gboolean
@ -90,7 +89,7 @@ cogl_is_texture (void *object)
return FALSE;
for (l = ctxt->texture_types; l; l = l->next)
if (GPOINTER_TO_INT (l->data) == obj->klass->type)
if (l->data == obj->klass)
return TRUE;
return FALSE;

View File

@ -81,7 +81,4 @@ struct _CoglTexturePixmapX11
gboolean use_winsys_texture;
};
GQuark
_cogl_handle_texture_pixmap_x11_get_type (void);
#endif /* __COGL_TEXTURE_PIXMAP_X11_PRIVATE_H */