Eo: Add reference functions for objects data
authorDaniel Zaoui <daniel.zaoui@samsung.com>
Wed, 20 Mar 2013 06:56:15 +0000 (08:56 +0200)
committerDaniel Zaoui <daniel.zaoui@samsung.com>
Wed, 1 May 2013 07:37:08 +0000 (10:37 +0300)
We want to introduce a new mechanism concerning the data of the Eo
objects.
The goal is to improve the memory management by defragmenting the memory
banks used by the Eo objects. The first phase has been done by raster
and consists in allocating the objects into a separate memory region
that the one used by malloc. So now, we know where our objects are
located.
Now, moving objects means moving data of objects. The issue we have here
is that a lot of data pointers are stored into data of other objects,
e.g Evas Object data into lists for rendering...
We need a way to reference the data and eo_data_get doesn't provide us
that. So we need to improve the API for data extraction by requesting
from the developer if the data will be stored or not. Five functions are
supplied:
- eo_data_scope_get: no referencing, the data pointer is no more used after
exiting the function.
- eo_data_ref: reference the data of the object. It means that while the
data is referenced, the object cannot be moved.
- eo_data_xref: reference the data of the object but for debug purpose,
we associate the objects that references. Same behavior as eo_data_ref
for non-debug.
- eo_data_unref: unreference the data of an object.
- eo_data_xunref: unreference the data of an object previously
referenced by another object.

I deprecated the eo_data_get function. Most of the time,
eo_data_scope_get needs to be used.

In the next patches, I changed the eo_data_get to the corresponding
functions, according to the usage of the data pointer.

The next step is to find all the places in the code where the data is
stored but not yet referenced. This will be done by:
- requesting from every object to unreference all data to other objects.
- moving all the objects from one region to another
- requesting from every object to rerefenrence the data.
- debugging by hunting the segmentation faults and other weird
creatures.

doc/eo_tutorial.dox
src/lib/eo/Eo.h
src/lib/eo/eo.c
src/tests/eo/access/access_inherit.c
src/tests/eo/access/access_main.c
src/tests/eo/function_overrides/function_overrides_main.c
src/tests/eo/mixin/mixin_main.c
src/tests/eo/signals/signals_main.c
src/tests/eo/signals/signals_simple.c
src/tests/eo/suite/eo_test_general.c

index 364ff54..333bdee 100644 (file)
  * @endcode
  * 
  *   - Eo way:
+ *     Two functions can be used to extract object data. The use depends if you want to store the data or not. If you just need to access data in the function (most of the time), just use eo_data_scope_get. If you need to store the data (for example in a list of objects data), you have to use eo_data_ref. This function references the data. If you don't need the referenced data anymore, call eo_data_unref.
+ *     This reference mechanism will be used in the future to detect bad usage of objects, defragment the memory space used by the objects...
  * @code
- *    Evas_Object_Line *o = eo_data_get(obj, EVAS_OBJ_LINE_CLASS);
+ *    Evas_Object_Line *o = eo_data_scope_get(obj, EVAS_OBJ_LINE_CLASS);
  *    if (!o) return;
  * @endcode
+ *    or
+ * @code
+ *    Evas_Object_Line *o = eo_data_ref(obj, EVAS_OBJ_LINE_CLASS);
+ *    if (!o) return;
+ *    ...
+ *    eo_data_unref(obj, o);
+ * @endcode
  * 
  * - Call function of parent
  *   - Old way:
  * @section Important to know
  * - eo_do() is the function used to invoke functions of a specific class on an object.
  * 
- * - eo_data_get() receives an object and a class and returns the data of the given class for the object. The class must belong to the object class hierarchy.
- * 
+ * - eo_data_scope_get() and eo_data_ref() receives an object and a class and returns the data of the given class for the object. The class must belong to the object class hierarchy.
+ *
+ * - eo_data_unref() receives an object and the data to unreference. The data MUST belong to this object.
+ *
  * - eo_isa() indicates if a given object is of a given type.
- * 
+ *
  * - eo_do_super() is in charge to invoke a function in the next parents that implement it. It is recommended to use eo_do_super() only from a function with the same op id.\n
  * In addition, there is no way to jump over classes who implement the function. If A inherits from B, B from C and A, B and C implement a virtual function defined in C, the function calls order will be A, then B and finally C. It is impossible to pass over B.
  * 
  *    Evas_Coord *x2 = va_arg(*list, Evas_Coord *);
  *    Evas_Coord *y2 = va_arg(*list, Evas_Coord *);
  * 
- *    Evas_Object_Protected_Data *obj = eo_data_get(eo_obj, EVAS_OBJ_CLASS);
+ *    Evas_Object_Protected_Data *obj = eo_data_scope_get(eo_obj, EVAS_OBJ_CLASS);
  *    if (x1) *x1 = obj->cur.geometry.x + o->cur.x1;
  *    if (y1) *y1 = obj->cur.geometry.y + o->cur.y1;
  *    if (x2) *x2 = obj->cur.geometry.x + o->cur.x2;
  * {
  *    eo_do_super(eo_obj, eo_constructor());
  * 
- *    Evas_Object_Protected_Data *obj = eo_data_get(eo_obj, EVAS_OBJ_CLASS);
+ *    Evas_Object_Protected_Data *obj = eo_data_scope_get(eo_obj, EVAS_OBJ_CLASS);
  *    evas_object_line_init(eo_obj);
  * }
  * 
index fcacfec..7e1d652 100644 (file)
@@ -775,8 +775,77 @@ EAPI Eina_Bool eo_parent_set(Eo *obj, const Eo *parent);
  * @param obj the object to work on.
  * @param klass the klass associated with the data.
  * @return a pointer to the data.
+ * @deprecated use eo_data_scope_get or eo_data_ref instead.
  */
-EAPI void *eo_data_get(const Eo *obj, const Eo_Class *klass);
+EAPI void *eo_data_get(const Eo *obj, const Eo_Class *klass) EINA_DEPRECATED;
+
+/**
+ * @brief Get a pointer to the data of an object for a specific class.
+ * The data reference count is not incremented. The pointer must be used only
+ * in the scope of the function and its callees.
+ * @param obj the object to work on.
+ * @param klass the klass associated with the data.
+ * @return a pointer to the data.
+ *
+ * @see eo_data_ref()
+ * @see eo_data_unref()
+ */
+EAPI void *eo_data_scope_get(const Eo *obj, const Eo_Class *klass);
+
+/**
+ * @def eo_data_xref(obj, klass, ref_obj)
+ * Use this macro if you want to associate a referencer object.
+ * Convenience macro around eo_data_xref_internal()
+ */
+#define eo_data_xref(obj, klass, ref_obj) eo_data_xref_internal(__FILE__, __LINE__, obj, klass, ref_obj)
+
+/**
+ * @def eo_data_ref(obj, klass)
+ * Use this macro if you don't want to associate a referencer object.
+ * Convenience macro around eo_data_xref_internal()
+ */
+#define eo_data_ref(obj, klass) eo_data_xref_internal(__FILE__, __LINE__, obj, klass, (const Eo *)obj)
+
+/**
+ * @brief Get a pointer to the data of an object for a specific class and
+ * increment the data reference count.
+ * @param obj the object to work on.
+ * @param klass the klass associated with the data.
+ * @param ref_obj the object that references the data.
+ * @param file the call's filename.
+ * @param line the call's line number.
+ * @return a pointer to the data.
+ *
+ * @see eo_data_xunref_internal()
+ */
+EAPI void *eo_data_xref_internal(const char *file, int line, const Eo *obj, const Eo_Class *klass, const Eo *ref_obj);
+
+/**
+ * @def eo_data_xunref(obj, data, ref_obj)
+ * Use this function if you used eo_data_xref to reference the data.
+ * Convenience macro around eo_data_xunref_internal()
+ * @see eo_data_xref()
+ */
+#define eo_data_xunref(obj, data, ref_obj) eo_data_xunref_internal(obj, data, ref_obj)
+
+/**
+ * @def eo_data_unref(obj, data)
+ * Use this function if you used eo_data_ref to reference the data.
+ * Convenience macro around eo_data_unref_internal()
+ * @see eo_data_ref()
+ */
+#define eo_data_unref(obj, data) eo_data_xunref_internal(obj, data, obj)
+
+/**
+ * @brief Decrement the object data reference count by 1.
+ * @param obj the object to work on.
+ * @param data a pointer to the data to unreference
+ * @param file the call's filename.
+ * @param line the call's line number.
+ *
+ * @see eo_data_xref_internal()
+ */
+EAPI void eo_data_xunref_internal(const Eo *obj, void *data, const Eo *ref_obj);
 
 /**
  * @brief Increment the object's reference count by 1.
index 723d2e7..611969f 100644 (file)
@@ -33,7 +33,9 @@ static Eo_Op _eo_ops_last_id = 0;
 static size_t _eo_sz = 0;
 
 static void _eo_condtor_reset(_Eo *obj);
-static inline void *_eo_data_get(const _Eo *obj, const _Eo_Class *klass);
+static inline void *_eo_data_scope_get(const _Eo *obj, const _Eo_Class *klass);
+static inline void *_eo_data_xref_internal(const char *file, int line, _Eo *obj, const _Eo_Class *klass, const _Eo *ref_obj);
+static inline void _eo_data_xunref_internal(_Eo *obj, void *data, const _Eo *ref_obj);
 static inline _Eo *_eo_ref(_Eo *obj);
 static inline void _eo_unref(_Eo *obj);
 static const _Eo_Class *_eo_op_class_get(Eo_Op op);
@@ -48,6 +50,7 @@ struct _Eo_Internal {
      const _Eo_Class *klass;
 #ifdef EO_DEBUG
      Eina_Inlist *xrefs;
+     Eina_Inlist *data_xrefs;
 #endif
 
      Eina_List *composite_objects;
@@ -55,6 +58,7 @@ struct _Eo_Internal {
      Eo_Id obj_id;
 
      int refcount;
+     int datarefcount;
 
      Eina_Bool do_error:1;
      Eina_Bool condtor_done:1;
@@ -357,7 +361,7 @@ _eo_op_internal(const char *file, int line, _Eo *obj, const _Eo_Class *cur_klass
         const op_type_funcs *func = _eo_kls_itr_func_get(cur_klass, op);
         if (EINA_LIKELY(func != NULL))
           {
-             void *func_data =_eo_data_get(obj, func->src);
+             void *func_data = _eo_data_scope_get(obj, func->src);
              func->func((Eo *)obj->obj_id, func_data, p_list);
              return EINA_TRUE;
           }
@@ -1240,7 +1244,7 @@ eo_xunref(Eo *obj_id, const Eo *ref_obj_id)
    Eo_Xref_Node *xref = NULL;
    EINA_INLIST_FOREACH(obj->xrefs, xref)
      {
-        if (xref->ref_obj == ref_obj)
+        if (xref->ref_obj == ref_obj_id)
           break;
      }
 
@@ -1251,7 +1255,7 @@ eo_xunref(Eo *obj_id, const Eo *ref_obj_id)
      }
    else
      {
-        ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
+        ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj_id, obj_id);
         return;
      }
 #else
@@ -1324,6 +1328,12 @@ _eo_del_internal(const char *file, int line, _Eo *obj)
 static inline void
 _eo_free(_Eo *obj)
 {
+#ifdef EO_DEBUG
+   if (obj->datarefcount)
+     {
+        ERR("Object %p data still referenced %d time(s).", obj, obj->datarefcount);
+     }
+#endif
    _eo_id_release(obj->obj_id);
    free(obj);
 }
@@ -1351,6 +1361,15 @@ _eo_unref(_Eo *obj)
              free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
              obj->xrefs = nitr;
           }
+        while (obj->data_xrefs)
+          {
+             Eina_Inlist *nitr = obj->data_xrefs->next;
+             Eo_Xref_Node *xref = EINA_INLIST_CONTAINER_GET(obj->data_xrefs, Eo_Xref_Node);
+             ERR("Data of object 0x%p is still referenced by object 0x%X", obj->obj_id, xref->ref_obj);
+
+             free(xref);
+             obj->data_xrefs = nitr;
+          }
 #endif
 
         if (!obj->manual_free)
@@ -1426,7 +1445,7 @@ _eo_condtor_reset(_Eo *obj)
 }
 
 static inline void *
-_eo_data_get(const _Eo *obj, const _Eo_Class *klass)
+_eo_data_scope_get(const _Eo *obj, const _Eo_Class *klass)
 {
    if (EINA_LIKELY(klass->desc->data_size > 0))
      {
@@ -1453,9 +1472,86 @@ _eo_data_get(const _Eo *obj, const _Eo_Class *klass)
    return NULL;
 }
 
+static inline void *
+_eo_data_xref_internal(const char *file, int line, _Eo *obj, const _Eo_Class *klass, const _Eo *ref_obj)
+{
+   void *data;
+   if (klass != NULL)
+     {
+        data = _eo_data_scope_get(obj, klass);
+        if (data == NULL) return NULL;
+     }
+   (obj->datarefcount)++;
+#ifdef EO_DEBUG
+   Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
+   xref->ref_obj = (Eo *)ref_obj->obj_id;
+   xref->file = file;
+   xref->line = line;
+
+   obj->data_xrefs = eina_inlist_prepend(obj->data_xrefs, EINA_INLIST_GET(xref));
+#else
+   (void) ref_obj;
+   (void) file;
+   (void) line;
+#endif
+   return data;
+}
+
+static inline void
+_eo_data_xunref_internal(_Eo *obj, void *data, const _Eo *ref_obj)
+{
+#ifdef EO_DEBUG
+   const _Eo_Class *klass = obj->klass;
+   char *data_base = ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj));
+   Eina_Bool in_range = ((char *)data >= data_base &&
+         (char *)data < (data_base + (klass->data_offset +
+               EO_ALIGN_SIZE(klass->desc->data_size) + klass->extn_data_size)));
+   if (!in_range)
+     {
+        ERR("Data %p is not in the data range of the object 0x%X (%s).", data, obj->obj_id, obj->klass->desc->name);
+     }
+#else
+   (void) data;
+#endif
+   if (obj->datarefcount == 0)
+     {
+        ERR("Data for object 0x%X (%s) is already not referenced.", obj->obj_id, obj->klass->desc->name);
+     }
+   else
+     {
+        (obj->datarefcount)--;
+     }
+#ifdef EO_DEBUG
+   Eo_Xref_Node *xref = NULL;
+   EINA_INLIST_FOREACH(obj->data_xrefs, xref)
+     {
+        if (xref->ref_obj == (Eo *)ref_obj->obj_id)
+          break;
+     }
+
+   if (xref)
+     {
+        obj->data_xrefs = eina_inlist_remove(obj->data_xrefs, EINA_INLIST_GET(xref));
+        free(xref);
+     }
+   else
+     {
+        ERR("ref_obj (0x%X) does not reference data (%p) of obj (0x%X).", ref_obj->obj_id, data, obj->obj_id);
+     }
+#else
+   (void) ref_obj;
+#endif
+}
+
 EAPI void *
 eo_data_get(const Eo *obj_id, const Eo_Class *klass_id)
 {
+   return eo_data_scope_get(obj_id, klass_id);
+}
+
+EAPI void *
+eo_data_scope_get(const Eo *obj_id, const Eo_Class *klass_id)
+{
    void *ret;
    EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, NULL);
    _Eo_Class *klass = _eo_class_pointer_get(klass_id);
@@ -1469,7 +1565,7 @@ eo_data_get(const Eo *obj_id, const Eo_Class *klass_id)
      }
 #endif
 
-   ret = _eo_data_get(obj, klass);
+   ret = _eo_data_scope_get(obj, klass);
 
 #ifdef EO_DEBUG
    if (!ret && (klass->desc->data_size == 0))
@@ -1481,6 +1577,47 @@ eo_data_get(const Eo *obj_id, const Eo_Class *klass_id)
    return ret;
 }
 
+EAPI void *
+eo_data_xref_internal(const char *file, int line, const Eo *obj_id, const Eo_Class *klass_id, const Eo *ref_obj_id)
+{
+   void *ret;
+   EO_OBJ_POINTER_RETURN_VAL(obj_id, obj, NULL);
+   EO_OBJ_POINTER_RETURN_VAL(ref_obj_id, ref_obj, NULL);
+   _Eo_Class *klass = NULL;
+   if (klass_id)
+     {
+        klass = _eo_class_pointer_get(klass_id);
+        EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
+
+#ifdef EO_DEBUG
+        if (!_eo_class_mro_has(obj->klass, klass))
+          {
+             ERR("Tried getting data of class '%s' from object of class '%s', but the former is not a direct inheritance of the latter.", klass->desc->name, obj->klass->desc->name);
+             return NULL;
+          }
+#endif
+     }
+
+   ret = _eo_data_xref_internal(file, line, obj, klass, ref_obj);
+
+#ifdef EO_DEBUG
+   if (klass && !ret && (klass->desc->data_size == 0))
+     {
+        ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
+     }
+#endif
+
+   return ret;
+}
+
+EAPI void
+eo_data_xunref_internal(const Eo *obj_id, void *data, const Eo *ref_obj_id)
+{
+   EO_OBJ_POINTER_RETURN(obj_id, obj);
+   EO_OBJ_POINTER_RETURN(ref_obj_id, ref_obj);
+   _eo_data_xunref_internal(obj, data, ref_obj);
+}
+
 EAPI Eina_Bool
 eo_init(void)
 {
index 4d1c81e..b42a4a6 100644 (file)
@@ -14,7 +14,7 @@ EAPI Eo_Op INHERIT_BASE_ID = 0;
 static void
 _prot_print(Eo *obj, void *class_data EINA_UNUSED, va_list *list)
 {
-   Simple_Protected_Data *pd = eo_data_get(obj, SIMPLE_CLASS);
+   Simple_Protected_Data *pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    (void) list;
    printf("%s %d\n", __func__, pd->protected_x1);
 }
index 474f3b9..61cd381 100644 (file)
@@ -17,7 +17,7 @@ main(int argc, char *argv[])
 
    eo_do(obj, simple_a_set(1), inherit_prot_print());
 
-   Simple_Public_Data *pd = eo_data_get(obj, SIMPLE_CLASS);
+   Simple_Public_Data *pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    printf("Pub: %d\n", pd->public_x2);
 
    eo_unref(obj);
index 48ab4bd..c93e313 100644 (file)
@@ -20,7 +20,7 @@ main(int argc, char *argv[])
    Eo *obj = eo_add(INHERIT2_CLASS, NULL);
 
    eo_do(obj, simple_a_set(1));
-   Simple_Public_Data *pd = eo_data_get(obj, SIMPLE_CLASS);
+   Simple_Public_Data *pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    fail_if(pd->a != 2);
 
    eo_unref(obj);
@@ -28,7 +28,7 @@ main(int argc, char *argv[])
    obj = eo_add(INHERIT3_CLASS, NULL);
 
    eo_do(obj, simple_a_set(1));
-   pd = eo_data_get(obj, SIMPLE_CLASS);
+   pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    fail_if(pd->a != 3);
 
    eo_unref(obj);
index bb19e9c..cde8ef9 100644 (file)
@@ -28,10 +28,10 @@ main(int argc, char *argv[])
 
    eo_do(obj, mixin_ab_sum_get(&sum), mixin_ab_sum_get(&sum));
 
-   Mixin2_Public_Data *pd2 = eo_data_get(obj, MIXIN2_CLASS);
+   Mixin2_Public_Data *pd2 = eo_data_scope_get(obj, MIXIN2_CLASS);
    fail_if(pd2->count != 6);
 
-   Mixin3_Public_Data *pd3 = eo_data_get(obj, MIXIN3_CLASS);
+   Mixin3_Public_Data *pd3 = eo_data_scope_get(obj, MIXIN3_CLASS);
    fail_if(pd3->count != 9);
 
    eo_unref(obj);
index 0fcd15a..4ed09c9 100644 (file)
@@ -44,7 +44,7 @@ main(int argc, char *argv[])
    eo_init();
 
    Eo *obj = eo_add(SIMPLE_CLASS, NULL);
-   Simple_Public_Data *pd = eo_data_get(obj, SIMPLE_CLASS);
+   Simple_Public_Data *pd = eo_data_scope_get(obj, SIMPLE_CLASS);
 
    /* The order of these two is undetermined. */
    eo_do(obj, eo_event_callback_priority_add(EV_A_CHANGED, EO_CALLBACK_PRIORITY_BEFORE, _a_changed_cb, (void *) 2));
index 37a38a9..1417988 100644 (file)
@@ -33,7 +33,7 @@ _a_set(Eo *obj, void *class_data, va_list *list)
 Eina_Bool
 _cb_added(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
 {
-   Simple_Public_Data *pd = eo_data_get(obj, MY_CLASS);
+   Simple_Public_Data *pd = eo_data_scope_get(obj, MY_CLASS);
    const Eo_Callback_Array_Item *callback_array = event_info;
    (void) data;
    (void) desc;
@@ -50,7 +50,7 @@ _cb_added(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_inf
 Eina_Bool
 _cb_deled(void *data, Eo *obj, const Eo_Event_Description *desc, void *event_info)
 {
-   Simple_Public_Data *pd = eo_data_get(obj, MY_CLASS);
+   Simple_Public_Data *pd = eo_data_scope_get(obj, MY_CLASS);
    const Eo_Callback_Array_Item *callback_array = event_info;
    (void) data;
    (void) desc;
index 866feb1..b6c10e6 100644 (file)
@@ -127,7 +127,7 @@ START_TEST(eo_data_fetch)
    Eo *obj = eo_add(klass, NULL);
    fail_if(!obj);
 #ifdef EO_DEBUG
-   fail_if(eo_data_get(obj, SIMPLE_CLASS));
+   fail_if(eo_data_scope_get(obj, SIMPLE_CLASS));
 #endif
    eo_unref(obj);
 
@@ -137,7 +137,7 @@ START_TEST(eo_data_fetch)
 
    obj = eo_add(klass, NULL);
    fail_if(!obj);
-   fail_if(eo_data_get(obj, klass));
+   fail_if(eo_data_scope_get(obj, klass));
    eo_unref(obj);
 
    eo_shutdown();
@@ -695,7 +695,7 @@ START_TEST(eo_magic_checks)
 
         eo_error_set((Eo *) buf);
 
-        fail_if(eo_data_get((Eo *) buf, SIMPLE_CLASS));
+        fail_if(eo_data_scope_get((Eo *) buf, SIMPLE_CLASS));
 
         eo_composite_attach((Eo *) buf, obj);
         eo_composite_attach(obj, (Eo *) buf);
@@ -808,13 +808,13 @@ START_TEST(eo_add_do_and_custom)
 
    obj = eo_add(SIMPLE_CLASS, NULL, simple_a_set(7));
    fail_if(!obj);
-   pd = eo_data_get(obj, SIMPLE_CLASS);
+   pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    fail_if(pd->a != 7);
    eo_unref(obj);
 
    obj = eo_add_custom(SIMPLE_CLASS, NULL, eo_constructor(), simple_a_set(7));
    fail_if(!obj);
-   pd = eo_data_get(obj, SIMPLE_CLASS);
+   pd = eo_data_scope_get(obj, SIMPLE_CLASS);
    fail_if(pd->a != 7);
    eo_unref(obj);