Eobj: Added mixin data support.
[profile/ivi/eobj.git] / lib / eobj.c
index e449575..2eccaa7 100644 (file)
@@ -26,6 +26,10 @@ struct _Eobj {
      const Eobj_Class *klass;
      void *data_blob;
      int refcount;
+#ifndef NDEBUG
+     Eina_Inlist *xrefs;
+#endif
+
      Eina_List *composite_objects;
 
      Eina_Inlist *callbacks;
@@ -59,6 +63,9 @@ struct _Eobj {
       })
 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
 
+#define EOBJ_ALIGN_SIZE(size) \
+        ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
+
 /* Structure of Eobj_Op is:
  * 16bit: class
  * 16bit: op.
@@ -87,6 +94,12 @@ typedef struct
      const Eobj_Class *klass;
 } Eobj_Extension_Node;
 
+typedef struct
+{
+     const Eobj_Class *klass;
+     size_t offset;
+} Eobj_Extension_Data_Offset;
+
 struct _Eobj_Class
 {
    Eobj_Class_Id class_id;
@@ -95,6 +108,9 @@ struct _Eobj_Class
    Dich_Chain1 chain[DICH_CHAIN1_SIZE];
    Eina_Inlist *extensions;
 
+   Eobj_Extension_Data_Offset *extn_data_off;
+   size_t extn_data_size;
+
    const Eobj_Class **mro;
 
    size_t data_offset; /* < Offset of the data within object data. */
@@ -186,6 +202,31 @@ dich_func_clean_all(Eobj_Class *klass)
 
 /* END OF DICH */
 
+static const Eobj_Op_Description noop_desc =
+        EOBJ_OP_DESCRIPTION(EOBJ_NOOP, "", "No operation.");
+
+static const Eobj_Op_Description *
+_eobj_op_id_desc_get(Eobj_Op op)
+{
+   const Eobj_Class *klass = OP_CLASS_GET(op);
+   Eobj_Op sub_id = OP_SUB_ID_GET(op);
+
+   if (op == EOBJ_NOOP)
+      return &noop_desc;
+
+   if (klass && (sub_id < klass->desc->ops.count))
+      return klass->desc->ops.descs + sub_id;
+
+   return NULL;
+}
+
+static const char *
+_eobj_op_id_name_get(Eobj_Op op)
+{
+   const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
+   return (desc) ? desc->name : NULL;
+}
+
 typedef struct
 {
    EINA_INLIST;
@@ -241,10 +282,20 @@ _eobj_kls_itr_get(Eobj *obj)
 }
 
 static inline const Eobj_Class *
-_eobj_kls_itr_next(Eobj *obj)
+_eobj_kls_itr_next(Eobj *obj, Eobj_Op op)
 {
    Eobj_Kls_Itr_Node *node =
       EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
+
+   if (!node || (node->op != op))
+     {
+        Eobj_Op node_op = (node) ? node->op : EOBJ_NOOP;
+        ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eobj_*_super functions from a wrong place.",
+              op, _eobj_op_id_name_get(op),
+              node_op, _eobj_op_id_name_get(node_op));
+        return NULL;
+     }
+
    const Eobj_Class **kls_itr = node->kls_itr;
    if (*kls_itr)
      {
@@ -267,18 +318,6 @@ _eobj_kls_itr_reached_end(const Eobj *obj)
    return !(*kls_itr && *(kls_itr + 1));
 }
 
-static const Eobj_Op_Description *
-_eobj_op_id_desc_get(Eobj_Op op)
-{
-   const Eobj_Class *klass = OP_CLASS_GET(op);
-   Eobj_Op sub_id = OP_SUB_ID_GET(op);
-
-   if (klass && (sub_id < klass->desc->ops.count))
-      return klass->desc->ops.descs + sub_id;
-
-   return NULL;
-}
-
 static Eina_Bool
 _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
 {
@@ -299,7 +338,7 @@ _eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
              goto end;
           }
 
-        klass = _eobj_kls_itr_next(obj);
+        klass = _eobj_kls_itr_next(obj, op);
      }
 
    /* Try composite objects */
@@ -333,12 +372,10 @@ _eobj_ops_internal(Eobj *obj, va_list *p_list)
      {
         if (!_eobj_op_internal(obj, op, p_list))
           {
-             const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
-             const char *_id_name = (desc) ? desc->name : NULL;
              const Eobj_Class *op_klass = OP_CLASS_GET(op);
              const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
              ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
-                   op, _id_name, _dom_name,
+                   op, _eobj_op_id_name_get(op), _dom_name,
                    obj->klass->desc->name);
              ret = EINA_FALSE;
              break;
@@ -369,18 +406,21 @@ eobj_do_super(Eobj *obj, Eobj_Op op, ...)
    Eina_Bool ret = EINA_TRUE;
    va_list p_list;
 
-   va_start(p_list, op);
-
    /* Advance the kls itr. */
-   obj_klass = _eobj_kls_itr_next(obj);
+   obj_klass = _eobj_kls_itr_next(obj, op);
+
+   if (!obj_klass)
+     {
+        return EINA_FALSE;
+     }
+
+   va_start(p_list, op);
    if (!_eobj_op_internal(obj, op, &p_list))
      {
-        const Eobj_Op_Description *desc = _eobj_op_id_desc_get(op);
-        const char *_id_name = (desc) ? desc->name : NULL;
         const Eobj_Class *op_klass = OP_CLASS_GET(op);
         const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL;
         ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.",
-              op, _id_name, _dom_name,
+              op, _eobj_op_id_name_get(op), _dom_name,
               (obj_klass) ? obj_klass->desc->name : NULL);
         ret = EINA_FALSE;
      }
@@ -415,25 +455,60 @@ _eobj_class_base_op_init(Eobj_Class *klass)
 static Eina_List *
 _eobj_class_mro_add(Eina_List *mro, const Eobj_Class *klass)
 {
+   Eina_List *extn_pos = NULL;
+   Eina_Bool check_consistency = !mro;
    if (!klass)
       return mro;
 
    mro = eina_list_append(mro, klass);
 
+   /* Recursively add extenions. */
      {
         Eobj_Extension_Node *extn;
         EINA_INLIST_FOREACH(klass->extensions, extn)
           {
              mro = _eobj_class_mro_add(mro, extn->klass);
+             if (!mro)
+                return NULL;
+
+             if (check_consistency)
+               {
+                  extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
+               }
           }
      }
 
+   /* Check if we can create a consistent mro. We only do it for the class
+    * we are working on (i.e no parents). */
+   if (check_consistency)
+     {
+        Eobj_Extension_Node *extn;
+
+        Eina_List *itr = extn_pos;
+        EINA_INLIST_FOREACH(klass->extensions, extn)
+          {
+             /* Get the first one after the extension. */
+             Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
+
+             /* If we found the extension again. */
+             if (eina_list_data_find_list(extn_list, extn->klass))
+               {
+                  eina_list_free(mro);
+                  ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
+                  return NULL;
+               }
+
+             itr = eina_list_next(itr);
+          }
+     }
+
+
    mro = _eobj_class_mro_add(mro, klass->parent);
 
    return mro;
 }
 
-static void
+static Eina_Bool
 _eobj_class_mro_init(Eobj_Class *klass)
 {
    Eina_List *mro = NULL;
@@ -441,6 +516,10 @@ _eobj_class_mro_init(Eobj_Class *klass)
    DBG("Started creating MRO for class '%s'", klass->desc->name);
    mro = _eobj_class_mro_add(mro, klass);
 
+   if (!mro)
+      return EINA_FALSE;
+
+   /* Remove duplicates and make them the right order. */
      {
         Eina_List *itr1, *itr2, *itr2n;
 
@@ -483,6 +562,8 @@ _eobj_class_mro_init(Eobj_Class *klass)
      }
 
    DBG("Finished creating MRO for class '%s'", klass->desc->name);
+
+   return EINA_TRUE;
 }
 
 static void
@@ -495,8 +576,6 @@ _eobj_class_constructor(Eobj_Class *klass)
 
    if (klass->desc->class_constructor)
       klass->desc->class_constructor(klass);
-
-   _eobj_class_mro_init(klass);
 }
 
 EAPI void
@@ -533,7 +612,11 @@ eobj_class_free(Eobj_Class *klass)
           }
      }
 
-   free(klass->mro);
+   if (klass->mro)
+      free(klass->mro);
+
+   if (klass->extn_data_off)
+      free(klass->extn_data_off);
 
    free(klass);
 }
@@ -675,9 +758,7 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
         /* Update the current offset. */
         /* FIXME: Make sure this alignment is enough. */
         klass->data_offset = klass->parent->data_offset +
-           klass->parent->desc->data_size +
-           (sizeof(void *) -
-                  (klass->parent->desc->data_size % sizeof(void *)));
+           EOBJ_ALIGN_SIZE(klass->parent->desc->data_size);
      }
 
    if (!_eobj_class_check_op_descs(klass))
@@ -685,6 +766,52 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
         goto cleanup;
      }
 
+   if (!_eobj_class_mro_init(klass))
+     {
+        goto cleanup;
+     }
+
+   /* create MIXIN offset table. */
+     {
+        const Eobj_Class **mro_itr = klass->mro;
+        Eobj_Extension_Data_Offset *extn_data_itr;
+        size_t extn_num = 0;
+        size_t extn_data_off = klass->data_offset +
+           EOBJ_ALIGN_SIZE(klass->desc->data_size);
+
+        /* FIXME: Make faster... */
+        while (*mro_itr)
+          {
+             if (((*mro_itr)->desc->type == EOBJ_CLASS_TYPE_MIXIN) &&
+                   ((*mro_itr)->desc->data_size > 0))
+               {
+                  extn_num++;
+               }
+             mro_itr++;
+          }
+
+        klass->extn_data_off = calloc(extn_num + 1,
+              sizeof(*klass->extn_data_off));
+
+        extn_data_itr = klass->extn_data_off;
+        mro_itr = klass->mro;
+        while (*mro_itr)
+          {
+             if (((*mro_itr)->desc->type == EOBJ_CLASS_TYPE_MIXIN) &&
+                   ((*mro_itr)->desc->data_size > 0))
+               {
+                  extn_data_itr->klass = *mro_itr;
+                  extn_data_itr->offset = extn_data_off;
+
+                  extn_data_off += EOBJ_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
+                  extn_data_itr++;
+               }
+             mro_itr++;
+          }
+
+        klass->extn_data_size = extn_data_off;
+     }
+
    klass->class_id = ++_eobj_classes_last_id;
      {
         /* FIXME: Handle errors. */
@@ -724,7 +851,8 @@ eobj_add(const Eobj_Class *klass, Eobj *parent)
 
    obj->refcount++;
 
-   obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size);
+   obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size +
+         klass->extn_data_size);
 
    _eobj_kls_itr_init(obj, EOBJ_NOOP);
    eobj_constructor_error_unset(obj);
@@ -755,6 +883,63 @@ fail:
    return NULL;
 }
 
+typedef struct
+{
+   EINA_INLIST;
+   const Eobj *ref_obj;
+   const char *file;
+   int line;
+} Eobj_Xref_Node;
+
+EAPI Eobj *
+eobj_xref_internal(Eobj *obj, const Eobj *ref_obj, const char *file, int line)
+{
+   eobj_ref(obj);
+
+#ifndef NDEBUG
+   Eobj_Xref_Node *xref = calloc(1, sizeof(*xref));
+   xref->ref_obj = ref_obj;
+   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;
+   (void) file;
+   (void) line;
+#endif
+
+   return obj;
+}
+
+EAPI void
+eobj_xunref(Eobj *obj, const Eobj *ref_obj)
+{
+#ifndef NDEBUG
+   Eobj_Xref_Node *xref = NULL;
+   EINA_INLIST_FOREACH(obj->xrefs, xref)
+     {
+        if (xref->ref_obj == ref_obj)
+           break;
+     }
+
+   if (xref)
+     {
+        obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
+        free(xref);
+     }
+   else
+     {
+        ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
+        return;
+     }
+#else
+   (void) ref_obj;
+#endif
+   eobj_unref(obj);
+}
+
 EAPI Eobj *
 eobj_ref(Eobj *obj)
 {
@@ -804,14 +989,22 @@ eobj_unref(Eobj *obj)
              obj->kls_itr = nitr;
           }
 
+#ifndef NDEBUG
+        /* If for some reason it's not empty, clear it. */
+        while (obj->xrefs)
+          {
+             WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
+             Eina_Inlist *nitr = nitr->next;
+             free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eobj_Kls_Itr_Node));
+             obj->xrefs = nitr;
+          }
+#endif
+
         Eina_List *itr, *itr_n;
         Eobj *emb_obj;
         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
           {
-             /* FIXME: Should probably be unref. */
-             eobj_del(emb_obj);
-             obj->composite_objects =
-                eina_list_remove_list(obj->composite_objects, itr);
+             eobj_composite_object_detach(obj, emb_obj);
           }
 
         _eobj_callback_remove_all(obj);
@@ -937,13 +1130,13 @@ eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
 EAPI void
 eobj_constructor_super(Eobj *obj)
 {
-   eobj_class_constructor(obj, _eobj_kls_itr_next(obj));
+   eobj_class_constructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
 }
 
 EAPI void
 eobj_destructor_super(Eobj *obj)
 {
-   eobj_class_destructor(obj, _eobj_kls_itr_next(obj));
+   eobj_class_destructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
 }
 
 EAPI void *
@@ -952,9 +1145,29 @@ eobj_data_get(Eobj *obj, const Eobj_Class *klass)
    /* FIXME: Add a check that this is of the right klass and we don't seg.
     * Probably just return NULL. */
    if (klass->desc->data_size > 0)
-      return ((char *) obj->data_blob) + klass->data_offset;
-   else
-      return NULL;
+     {
+        if (klass->desc->type == EOBJ_CLASS_TYPE_MIXIN)
+          {
+             Eobj_Extension_Data_Offset *doff_itr =
+                eobj_class_get(obj)->extn_data_off;
+
+             if (!doff_itr)
+                return NULL;
+
+             while (doff_itr->klass)
+               {
+                  if (doff_itr->klass == klass)
+                     return ((char *) obj->data_blob) + doff_itr->offset;
+                  doff_itr++;
+               }
+          }
+        else
+          {
+             return ((char *) obj->data_blob) + klass->data_offset;
+          }
+     }
+
+   return NULL;
 }
 
 EAPI Eina_Bool
@@ -1006,7 +1219,7 @@ eobj_shutdown(void)
 EAPI void
 eobj_composite_object_attach(Eobj *obj, Eobj *emb_obj)
 {
-   eobj_ref(emb_obj);
+   eobj_xref(emb_obj, obj);
    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
 }
 
@@ -1014,7 +1227,7 @@ EAPI void
 eobj_composite_object_detach(Eobj *obj, Eobj *emb_obj)
 {
    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
-   eobj_unref(emb_obj);
+   eobj_xunref(emb_obj, obj);
 }
 
 EAPI Eina_Bool
@@ -1184,7 +1397,7 @@ eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
 
    EINA_INLIST_FOREACH(obj->callbacks, cb)
      {
-        if (!cb->delete_me  && (cb->event == desc))
+        if (!cb->delete_me && (cb->event == desc))
           {
              /* Abort callback calling if the func says so. */
              if (!cb->func((void *) cb->func_data, obj, desc,