Eo: Added a check if object is already deleted in eo_unref.
[profile/ivi/eobj.git] / lib / eo.c
index 68c06e0..823f344 100644 (file)
--- a/lib/eo.c
+++ b/lib/eo.c
@@ -32,7 +32,9 @@ typedef struct
 
 struct _Eo {
      EINA_MAGIC
+     EINA_INLIST;
      Eo *parent;
+     Eina_Inlist *children;
      const Eo_Class *klass;
      int refcount;
 #ifndef NDEBUG
@@ -43,6 +45,7 @@ struct _Eo {
 
      Eo_Kls_Itr mro_itr;
 
+     Eina_Bool composite:1;
      Eina_Bool del:1;
      Eina_Bool construct_error:1;
      Eina_Bool manual_free:1;
@@ -305,7 +308,7 @@ _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Op op)
    if (cur->op != op)
      {
         Eo_Op node_op = cur->op;
-        ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
+        ERR("Called with op %x ('%s') while expecting: %x ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
               op, _eo_op_id_name_get(op),
               node_op, _eo_op_id_name_get(node_op));
         return NULL;
@@ -376,22 +379,24 @@ _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
 {
    Eina_Bool ret = EINA_FALSE;
 
+#ifndef NDEBUG
    const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
 
    if (op_desc)
      {
         if (op_desc->op_type == EO_OP_TYPE_CLASS)
           {
-             ERR("Tried calling a class op '%s' (%d) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
+             ERR("Tried calling a class op '%s' (%x) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
              return EINA_FALSE;
           }
         else if ((op_type == EO_OP_TYPE_CONST) &&
               (op_desc->op_type != EO_OP_TYPE_CONST))
           {
-             ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
+             ERR("Tried calling non-const or non-existant op '%s' (%x) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
              return EINA_FALSE;
           }
      }
+#endif
 
    Eo_Kls_Itr prev_state;
 
@@ -486,16 +491,18 @@ _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
 {
    Eina_Bool ret = EINA_FALSE;
 
+#ifndef NDEBUG
    const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
 
    if (op_desc)
      {
         if (op_desc->op_type != EO_OP_TYPE_CLASS)
           {
-             ERR("Tried calling an instant op '%s' (%d) from a class context.", (op_desc) ? op_desc->name : NULL, op);
+             ERR("Tried calling an instance op '%s' (%x) from a class context.", (op_desc) ? op_desc->name : NULL, op);
              return EINA_FALSE;
           }
      }
+#endif
 
    Eo_Kls_Itr prev_state;
 
@@ -745,17 +752,26 @@ eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
    itr = func_descs;
    if (itr)
      {
-        for ( ; itr->op != 0 ; itr++)
+        for ( ; itr->op_type != EO_OP_TYPE_INVALID ; itr++)
           {
              const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
 
-             if (EINA_LIKELY(!op_desc || (itr->op_type == op_desc->op_type)))
+             if (EINA_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
+               {
+                  ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
+               }
+             else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
                {
                   _dich_func_set(klass, itr->op, itr->func);
                }
              else
                {
-                  ERR("Set function's op type (%d) is different than the one in the op description (%d) for op '%s' in class '%s'.", itr->op_type, op_desc->op_type, op_desc->name, klass->desc->name);
+                  ERR("Set function's op type (%x) is different than the one in the op description (%d) for op '%s' in class '%s'. Func index: %d",
+                        itr->op_type,
+                        (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
+                        (op_desc) ? op_desc->name : NULL,
+                        klass->desc->name,
+                        itr - func_descs);
                }
           }
      }
@@ -822,11 +838,11 @@ _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
           {
              if (itr->name)
                {
-                  ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
+                  ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
                }
              else
                {
-                  ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
+                  ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
                }
              return EINA_FALSE;
           }
@@ -1056,6 +1072,43 @@ cleanup:
    return NULL;
 }
 
+EAPI Eina_Bool
+eo_parent_set(Eo *obj, const Eo *parent)
+{
+   EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
+   if (parent)
+      EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
+
+   if (obj->parent == parent)
+      return EINA_TRUE;
+
+   _eo_ref(obj);
+
+   if (eo_composite_is(obj))
+     {
+        eo_composite_object_detach(obj->parent, obj);
+     }
+
+   if (obj->parent)
+     {
+        obj->parent->children =
+           eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
+        eo_xunref(obj, obj->parent);
+     }
+
+   obj->parent = (Eo *) parent;
+   if (obj->parent)
+     {
+        obj->parent->children =
+           eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
+        eo_xref(obj, obj->parent);
+     }
+
+   _eo_unref(obj);
+
+   return EINA_TRUE;
+}
+
 EAPI Eo *
 eo_add(const Eo_Class *klass, Eo *parent)
 {
@@ -1072,17 +1125,17 @@ eo_add(const Eo_Class *klass, Eo *parent)
    Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
          (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
          klass->extn_data_size);
+   EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
+   obj->refcount++;
    obj->klass = klass;
-   obj->parent = parent;
 
-   obj->refcount++;
+   eo_parent_set(obj, parent);
 
    Eo_Kls_Itr prev_state;
 
    _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
    _eo_error_unset(obj);
 
-   EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
    _eo_ref(obj);
    _eo_constructor(obj, klass);
 
@@ -1131,7 +1184,6 @@ eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
    xref->file = file;
    xref->line = line;
 
-   /* FIXME: Make it sorted. */
    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
 #else
    (void) ref_obj;
@@ -1189,13 +1241,10 @@ eo_ref(const Eo *_obj)
 static inline void
 _eo_del_internal(Eo *obj)
 {
-   if (obj->del)
-      return;
    /* We need that for the event callbacks that may ref/unref. */
    obj->refcount++;
 
    eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
-   obj->del = EINA_TRUE;
 
    const Eo_Class *klass = eo_class_get(obj);
    Eo_Kls_Itr prev_state;
@@ -1215,20 +1264,28 @@ _eo_del_internal(Eo *obj)
    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
    /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
 
-   Eina_List *itr, *itr_n;
-   Eo *emb_obj;
-   EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
      {
-        eo_composite_object_detach(obj, emb_obj);
+        Eina_List *itr, *itr_n;
+        Eo *emb_obj;
+        EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
+          {
+             eo_composite_object_detach(obj, emb_obj);
+          }
+     }
+
+   while (obj->children)
+     {
+        eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
      }
 
+   obj->del = EINA_TRUE;
    obj->refcount--;
 }
 
 static inline void
 _eo_free(Eo *obj)
 {
-   EINA_MAGIC_SET(obj, EO_DELETED_EINA_MAGIC);
+   EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
    free(obj);
 }
 
@@ -1237,6 +1294,12 @@ _eo_unref(Eo *obj)
 {
    if (--(obj->refcount) == 0)
      {
+        if (obj->del)
+          {
+             ERR("Object %p already deleted.", obj);
+             return;
+          }
+
         _eo_del_internal(obj);
 
 #ifndef NDEBUG
@@ -1250,7 +1313,10 @@ _eo_unref(Eo *obj)
           }
 #endif
 
-        if (!obj->manual_free) _eo_free(obj);
+        if (!obj->manual_free)
+           _eo_free(obj);
+        else
+           _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
      }
 }
 
@@ -1263,6 +1329,13 @@ eo_unref(const Eo *_obj)
    _eo_unref(obj);
 }
 
+EAPI void
+eo_del(const Eo *obj)
+{
+   eo_parent_set((Eo *) obj, NULL);
+   eo_unref(obj);
+}
+
 EAPI int
 eo_ref_get(const Eo *obj)
 {
@@ -1441,8 +1514,8 @@ eo_init(void)
      }
 
    eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
-   eina_magic_string_static_set(EO_DELETED_EINA_MAGIC,
-         EO_DELETED_EINA_MAGIC_STR);
+   eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
+         EO_FREED_EINA_MAGIC_STR);
    eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
          EO_CLASS_EINA_MAGIC_STR);
 
@@ -1482,7 +1555,8 @@ eo_composite_object_attach(Eo *obj, Eo *emb_obj)
    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
 
-   eo_xref(emb_obj, obj);
+   emb_obj->composite = EINA_TRUE;
+   eo_parent_set(emb_obj, obj);
    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
 }
 
@@ -1492,8 +1566,9 @@ eo_composite_object_detach(Eo *obj, Eo *emb_obj)
    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
 
+   emb_obj->composite = EINA_FALSE;
    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
-   eo_xunref(emb_obj, obj);
+   eo_parent_set(emb_obj, NULL);
 }
 
 EAPI Eina_Bool
@@ -1505,20 +1580,7 @@ eo_composite_is(const Eo *emb_obj)
         return EINA_FALSE;
      }
 
-   Eo *obj = eo_parent_get(emb_obj);
-   Eina_List *itr;
-   Eo *tmp;
-
-   if (!obj)
-      return EINA_FALSE;
-
-   EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
-     {
-        if (tmp == emb_obj)
-           return EINA_TRUE;
-     }
-
-   return EINA_FALSE;
+   return emb_obj->composite;
 }
 
 EAPI void
@@ -1535,11 +1597,11 @@ eo_manual_free(Eo *obj)
 
    if (EINA_FALSE == obj->manual_free)
      {
-        ERR("Tried to free manually the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
+        ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
         return;
      }
 
-   if (0 != obj->refcount)
+   if (!obj->del)
      {
         ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));
         return;