cogl-object: Adds an internal _cogl_object_set_user_data
authorRobert Bragg <robert@linux.intel.com>
Wed, 12 Jan 2011 20:37:53 +0000 (20:37 +0000)
committerRobert Bragg <robert@linux.intel.com>
Fri, 21 Jan 2011 16:18:10 +0000 (16:18 +0000)
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.

clutter/cogl/cogl/cogl-object-private.h
clutter/cogl/cogl/cogl-object.c

index 2e90f05..e8b4ed0 100644 (file)
 typedef struct _CoglObjectClass CoglHandleClass;
 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
 {
   GQuark   type;
@@ -48,7 +61,7 @@ typedef struct
 {
   CoglUserDataKey *key;
   void *user_data;
-  CoglUserDataDestroyCallback destroy;
+  CoglUserDataDestroyInternalCallback destroy;
 } CoglUserDataEntry;
 
 /* 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)                 \
   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 */
 
index f80a0ea..90109eb 100644 (file)
@@ -72,7 +72,7 @@ cogl_object_unref (void *object)
             {
               CoglUserDataEntry *entry = &obj->user_data_entry[i];
               if (entry->destroy)
-                entry->destroy (entry->user_data);
+                entry->destroy (entry->user_data, obj);
             }
 
           if (obj->user_data_array != NULL)
@@ -84,7 +84,7 @@ cogl_object_unref (void *object)
                                     CoglUserDataEntry, i);
 
                   if (entry->destroy)
-                    entry->destroy (entry->user_data);
+                    entry->destroy (entry->user_data, obj);
                 }
               g_array_free (obj->user_data_array, TRUE);
             }
@@ -157,10 +157,10 @@ _cogl_object_find_entry (CoglObject *object, CoglUserDataKey *key)
 }
 
 void
-cogl_object_set_user_data (CoglObject *object,
-                           CoglUserDataKey *key,
-                           void *user_data,
-                           CoglUserDataDestroyCallback destroy)
+_cogl_object_set_user_data (CoglObject *object,
+                            CoglUserDataKey *key,
+                            void *user_data,
+                            CoglUserDataDestroyInternalCallback destroy)
 {
   CoglUserDataEntry new_entry;
   CoglUserDataEntry *entry;
@@ -178,7 +178,7 @@ cogl_object_set_user_data (CoglObject *object,
   if (entry)
     {
       if (G_LIKELY (entry->destroy))
-        entry->destroy (entry->user_data);
+        entry->destroy (entry->user_data, object);
     }
   else
     {
@@ -206,6 +206,16 @@ cogl_object_set_user_data (CoglObject *object,
   *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 *
 cogl_object_get_user_data (CoglObject *object, CoglUserDataKey *key)
 {