cogl-object: Adds an internal _cogl_object_set_user_data

This adds an internal alternative to cogl_object_set_user_data that also
passes an instance pointer to destroy notify callbacks.

When setting private data on a CoglObject it's often desirable to know
the instance being destroyed when we are being notified to free the
private data due to the object being freed. The typical solution to this
is to track a pointer to the instance in the private data itself so it
can be identified but that usually requires an extra micro allocation
for the private data that could have been avoided if only the callback
were given an instance pointer.

The new internal _cogl_object_set_user_data passes the instance pointer
as a second argument which means it is ABI compatible for us to layer
the public version on top of this internal function.
This commit is contained in:
Robert Bragg 2011-01-12 20:37:53 +00:00
parent 131578c13a
commit 385e0f84c6
2 changed files with 37 additions and 8 deletions

View File

@ -36,6 +36,19 @@
typedef struct _CoglObjectClass CoglHandleClass; typedef struct _CoglObjectClass CoglHandleClass;
typedef struct _CoglObject CoglHandleObject; typedef struct _CoglObject CoglHandleObject;
/* XXX: sadly we didn't fully consider when we copied the cairo API
* for _set_user_data that the callback doesn't get a pointer to the
* instance which is desired in most cases. This means you tend to end
* up creating micro allocations for the private data just so you can
* pair up the data of interest with the original instance for
* identification when it is later destroyed.
*
* Internally we use a small hack to avoid needing these micro
* allocations by actually passing the instance as a second argument
* to the callback */
typedef void (*CoglUserDataDestroyInternalCallback) (void *user_data,
void *instance);
typedef struct _CoglObjectClass typedef struct _CoglObjectClass
{ {
GQuark type; GQuark type;
@ -48,7 +61,7 @@ typedef struct
{ {
CoglUserDataKey *key; CoglUserDataKey *key;
void *user_data; void *user_data;
CoglUserDataDestroyCallback destroy; CoglUserDataDestroyInternalCallback destroy;
} CoglUserDataEntry; } CoglUserDataEntry;
/* All Cogl objects inherit from this base object by adding a member: /* All Cogl objects inherit from this base object by adding a member:
@ -253,5 +266,11 @@ _cogl_##type_name##_handle_new (CoglHandle handle) \
#define COGL_HANDLE_DEFINE(TypeName, type_name) \ #define COGL_HANDLE_DEFINE(TypeName, type_name) \
COGL_HANDLE_DEFINE_WITH_CODE (TypeName, type_name, (void) 0) COGL_HANDLE_DEFINE_WITH_CODE (TypeName, type_name, (void) 0)
void
_cogl_object_set_user_data (CoglObject *object,
CoglUserDataKey *key,
void *user_data,
CoglUserDataDestroyInternalCallback destroy);
#endif /* __COGL_OBJECT_PRIVATE_H */ #endif /* __COGL_OBJECT_PRIVATE_H */

View File

@ -72,7 +72,7 @@ cogl_object_unref (void *object)
{ {
CoglUserDataEntry *entry = &obj->user_data_entry[i]; CoglUserDataEntry *entry = &obj->user_data_entry[i];
if (entry->destroy) if (entry->destroy)
entry->destroy (entry->user_data); entry->destroy (entry->user_data, obj);
} }
if (obj->user_data_array != NULL) if (obj->user_data_array != NULL)
@ -84,7 +84,7 @@ cogl_object_unref (void *object)
CoglUserDataEntry, i); CoglUserDataEntry, i);
if (entry->destroy) if (entry->destroy)
entry->destroy (entry->user_data); entry->destroy (entry->user_data, obj);
} }
g_array_free (obj->user_data_array, TRUE); g_array_free (obj->user_data_array, TRUE);
} }
@ -157,10 +157,10 @@ _cogl_object_find_entry (CoglObject *object, CoglUserDataKey *key)
} }
void void
cogl_object_set_user_data (CoglObject *object, _cogl_object_set_user_data (CoglObject *object,
CoglUserDataKey *key, CoglUserDataKey *key,
void *user_data, void *user_data,
CoglUserDataDestroyCallback destroy) CoglUserDataDestroyInternalCallback destroy)
{ {
CoglUserDataEntry new_entry; CoglUserDataEntry new_entry;
CoglUserDataEntry *entry; CoglUserDataEntry *entry;
@ -178,7 +178,7 @@ cogl_object_set_user_data (CoglObject *object,
if (entry) if (entry)
{ {
if (G_LIKELY (entry->destroy)) if (G_LIKELY (entry->destroy))
entry->destroy (entry->user_data); entry->destroy (entry->user_data, object);
} }
else else
{ {
@ -206,6 +206,16 @@ cogl_object_set_user_data (CoglObject *object,
*entry = new_entry; *entry = new_entry;
} }
void
cogl_object_set_user_data (CoglObject *object,
CoglUserDataKey *key,
void *user_data,
CoglUserDataDestroyCallback destroy)
{
_cogl_object_set_user_data (object, key, user_data,
(CoglUserDataDestroyInternalCallback)destroy);
}
void * void *
cogl_object_get_user_data (CoglObject *object, CoglUserDataKey *key) cogl_object_get_user_data (CoglObject *object, CoglUserDataKey *key)
{ {