Eobj: Added mixin data support.
[profile/ivi/eobj.git] / lib / eobj.c
index 6b39f6f..2eccaa7 100644 (file)
@@ -1,64 +1,44 @@
 #include <Eina.h>
 
 #include "Eobj.h"
+#include "eobj_private.h"
 
-static int _eobj_log_dom = -1;
+#include "config.h"
+
+typedef int Eobj_Class_Id;
+
+int _eobj_log_dom = -1;
 
 static Eobj_Class **_eobj_classes;
 static Eobj_Class_Id _eobj_classes_last_id;
 static Eina_Bool _eobj_init_count = 0;
 
-#define CONSTRUCT_ERROR_KEY "__construct_error"
-
 static void _eobj_callback_remove_all(Eobj *obj);
-static void _eobj_generic_data_del_all(Eobj *obj);
 static void eobj_class_constructor(Eobj *obj, const Eobj_Class *klass);
 static void eobj_class_destructor(Eobj *obj, const Eobj_Class *klass);
-
-#ifdef CRITICAL
-#undef CRITICAL
-#endif
-#define CRITICAL(...) EINA_LOG_DOM_CRIT(_eobj_log_dom, __VA_ARGS__)
-
-#ifdef ERR
-#undef ERR
-#endif
-#define ERR(...) EINA_LOG_DOM_ERR(_eobj_log_dom, __VA_ARGS__)
-
-#ifdef WRN
-#undef WRN
-#endif
-#define WRN(...) EINA_LOG_DOM_WARN(_eobj_log_dom, __VA_ARGS__)
-
-#ifdef INF
-#undef INF
-#endif
-#define INF(...) EINA_LOG_DOM_INFO(_eobj_log_dom, __VA_ARGS__)
-
-#ifdef DBG
-#undef DBG
-#endif
-#define DBG(...) EINA_LOG_DOM_DBG(_eobj_log_dom, __VA_ARGS__)
+static void eobj_constructor_error_unset(Eobj *obj);
 
 typedef struct _Eobj_Callback_Description Eobj_Callback_Description;
 
 struct _Eobj {
+     EINA_MAGIC
      Eobj *parent;
      const Eobj_Class *klass;
      void *data_blob;
-     void **datas;
      int refcount;
+#ifndef NDEBUG
+     Eina_Inlist *xrefs;
+#endif
+
      Eina_List *composite_objects;
 
      Eina_Inlist *callbacks;
      int walking_list;
 
-     Eina_Inlist *generic_data;
-
-     const Eobj_Class *kls_itr;
+     Eina_Inlist *kls_itr;
 
      Eina_Bool delete:1;
-     EINA_MAGIC
+     Eina_Bool construct_error:1;
 };
 
 /* Start of Dich */
@@ -81,6 +61,10 @@ struct _Eobj {
       (Eobj_Class *) ((tmp <= _eobj_classes_last_id) && (tmp > 0)) ? \
       (_eobj_classes[tmp - 1]) : NULL; \
       })
+#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
@@ -108,9 +92,14 @@ typedef struct
 {
      EINA_INLIST;
      const Eobj_Class *klass;
-     Eina_Bool exists : 1; /* < True if already exists in class (incl parents). */
 } Eobj_Extension_Node;
 
+typedef struct
+{
+     const Eobj_Class *klass;
+     size_t offset;
+} Eobj_Extension_Data_Offset;
+
 struct _Eobj_Class
 {
    Eobj_Class_Id class_id;
@@ -118,6 +107,14 @@ struct _Eobj_Class
    const Eobj_Class_Description *desc;
    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. */
+
    Eina_Bool constructed : 1;
 };
 
@@ -205,97 +202,167 @@ dich_func_clean_all(Eobj_Class *klass)
 
 /* END OF DICH */
 
-/* FIXME: Decide if it should be fast, and if so, add a mapping.
- * Otherwise, this is very slow. But since it's only for debugging... */
+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)
 {
-   int i;
-   Eobj_Class **cls_itr = _eobj_classes;
+   const Eobj_Class *klass = OP_CLASS_GET(op);
+   Eobj_Op sub_id = OP_SUB_ID_GET(op);
 
-   for (i = 0 ; i < _eobj_classes_last_id ; i++, cls_itr++)
+   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;
+   Eobj_Op op;
+   const Eobj_Class **kls_itr;
+} Eobj_Kls_Itr_Node;
+
+static inline Eina_Bool
+_eobj_kls_itr_init(Eobj *obj, Eobj_Op op)
+{
+   if (obj->kls_itr)
      {
-        if (*cls_itr)
+        Eobj_Kls_Itr_Node *node =
+           EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
+        if (node->op == op)
           {
-             const Eobj_Op_Description *desc = (*cls_itr)->desc->ops.descs;
-             if (!desc)
-                continue;
-
-             Eobj_Op base_op_id = *(*cls_itr)->desc->ops.base_op_id;
-             while (desc->sub_op)
-               {
-                  if ((base_op_id + desc->sub_op) == op)
-                     return desc;
-                  desc++;
-               }
+             return EINA_FALSE;
           }
      }
 
-   return NULL;
+
+     {
+        Eobj_Kls_Itr_Node *node = calloc(1, sizeof(*node));
+        node->op = op;
+        node->kls_itr = obj->klass->mro;
+        obj->kls_itr = eina_inlist_prepend(obj->kls_itr,
+              EINA_INLIST_GET(node));
+
+        return EINA_TRUE;
+     }
 }
 
-static Eina_Bool
-_eobj_op_internal(Eobj *obj, const Eobj_Class *obj_klass, Eobj_Op op, va_list *p_list)
+static inline void
+_eobj_kls_itr_end(Eobj *obj, Eobj_Op op)
 {
-   const Eobj_Class *klass = obj_klass;
-   eobj_op_func_type func;
+   Eobj_Kls_Itr_Node *node =
+      EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
 
-   if (!obj_klass)
-      return EINA_FALSE;
+   if (node->op != op)
+      return;
+
+   obj->kls_itr = eina_inlist_remove(obj->kls_itr, obj->kls_itr);
+   free(node);
+}
+
+static inline const Eobj_Class *
+_eobj_kls_itr_get(Eobj *obj)
+{
+   Eobj_Kls_Itr_Node *node =
+      EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
+
+   return (node) ? *(node->kls_itr) : NULL;
+}
+
+static inline const Eobj_Class *
+_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)
+     {
+        kls_itr++;
+        node->kls_itr = kls_itr;
+        return *kls_itr;
+     }
+   else
+     {
+        return NULL;
+     }
+}
 
+static inline Eina_Bool
+_eobj_kls_itr_reached_end(const Eobj *obj)
+{
+   Eobj_Kls_Itr_Node *node =
+      EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node);
+   const Eobj_Class **kls_itr = node->kls_itr;
+   return !(*kls_itr && *(kls_itr + 1));
+}
+
+static Eina_Bool
+_eobj_op_internal(Eobj *obj, Eobj_Op op, va_list *p_list)
+{
+   const Eobj_Class *klass;
+   Eina_Bool ret = EINA_FALSE;
+   Eina_Bool _itr_init;
+
+   _itr_init = _eobj_kls_itr_init(obj, op);
+   klass = _eobj_kls_itr_get(obj);
    while (klass)
      {
-        func = dich_func_get(klass, op);
+        eobj_op_func_type func = dich_func_get(klass, op);
 
         if (func)
           {
-             func(obj, op, p_list);
-             return EINA_TRUE;
-          }
-        else
-          {
-             klass = klass->parent;
+             func(obj, eobj_data_get(obj, klass), p_list);
+             ret = EINA_TRUE;
+             goto end;
           }
+
+        klass = _eobj_kls_itr_next(obj, op);
      }
 
-   if (!klass)
+   /* Try composite objects */
      {
-        klass = obj_klass;
-        /* FIXME: Should probably flatten everything. to be faster. */
-          {
-             /* Try MIXINS */
-             Eobj_Extension_Node *itr;
-             EINA_INLIST_FOREACH(klass->extensions, itr)
-               {
-                  if (_eobj_op_internal(obj, itr->klass, op, p_list))
-                    {
-                       return EINA_TRUE;
-                    }
-               }
-          }
-
-        /* Try composite objects */
+        Eina_List *itr;
+        Eobj *emb_obj;
+        EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
           {
-             Eina_List *itr;
-             Eobj *emb_obj;
-             EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
+             if (_eobj_op_internal(emb_obj, op, p_list))
                {
-                  if (_eobj_op_internal(emb_obj, eobj_class_get(emb_obj), op, p_list))
-                    {
-                       return EINA_TRUE;
-                    }
+                  ret = EINA_TRUE;
+                  goto end;
                }
           }
-
-        /* If haven't found anything, return FALSE */
-        return EINA_FALSE;
      }
 
-   return EINA_TRUE;
+end:
+
+   if (_itr_init) _eobj_kls_itr_end(obj, op);
+   return ret;
 }
 
 static inline Eina_Bool
-_eobj_ops_internal(Eobj *obj, const Eobj_Class *obj_klass, va_list *p_list)
+_eobj_ops_internal(Eobj *obj, va_list *p_list)
 {
    Eina_Bool ret = EINA_TRUE;
    Eobj_Op op = 0;
@@ -303,15 +370,13 @@ _eobj_ops_internal(Eobj *obj, const Eobj_Class *obj_klass, va_list *p_list)
    op = va_arg(*p_list, Eobj_Op);
    while (op)
      {
-        if (!_eobj_op_internal(obj, obj_klass, op, 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,
-                   obj_klass->desc->name);
+                   op, _eobj_op_id_name_get(op), _dom_name,
+                   obj->klass->desc->name);
              ret = EINA_FALSE;
              break;
           }
@@ -326,35 +391,50 @@ eobj_do_internal(Eobj *obj, ...)
 {
    Eina_Bool ret;
    va_list p_list;
+   eobj_ref(obj);
    va_start(p_list, obj);
-   ret = _eobj_ops_internal(obj, eobj_class_get(obj), &p_list);
+   ret = _eobj_ops_internal(obj, &p_list);
    va_end(p_list);
+   eobj_unref(obj);
    return ret;
 }
 
 EAPI Eina_Bool
-eobj_class_do_internal(Eobj *obj, const Eobj_Class *klass, ...)
+eobj_do_super(Eobj *obj, Eobj_Op op, ...)
 {
-   Eina_Bool ret;
+   const Eobj_Class *obj_klass;
+   Eina_Bool ret = EINA_TRUE;
    va_list p_list;
-   va_start(p_list, klass);
-   ret = _eobj_ops_internal(obj, klass, &p_list);
+
+   /* Advance the kls itr. */
+   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_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, _eobj_op_id_name_get(op), _dom_name,
+              (obj_klass) ? obj_klass->desc->name : NULL);
+        ret = EINA_FALSE;
+     }
    va_end(p_list);
+
    return ret;
 }
 
 EAPI const Eobj_Class *
-eobj_class_get(Eobj *obj)
+eobj_class_get(const Eobj *obj)
 {
    return obj->klass;
 }
 
-EAPI const Eobj_Class *
-eobj_class_parent_get(const Eobj_Class *klass)
-{
-   return klass->parent;
-}
-
 EAPI const char *
 eobj_class_name_get(const Eobj_Class *klass)
 {
@@ -372,6 +452,120 @@ _eobj_class_base_op_init(Eobj_Class *klass)
    *(desc->ops.base_op_id) = klass->class_id << OP_CLASS_OFFSET;
 }
 
+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 Eina_Bool
+_eobj_class_mro_init(Eobj_Class *klass)
+{
+   Eina_List *mro = NULL;
+
+   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;
+
+        itr1 = eina_list_last(mro);
+        while (itr1)
+          {
+             itr2 = eina_list_prev(itr1);
+
+             while (itr2)
+               {
+                  itr2n = eina_list_prev(itr2);
+
+                  if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
+                    {
+                       mro = eina_list_remove_list(mro, itr2);
+                    }
+
+                  itr2 = itr2n;
+               }
+
+             itr1 = eina_list_prev(itr1);
+          }
+     }
+
+   /* Copy the mro and free the list. */
+     {
+        const Eobj_Class *kls_itr;
+        const Eobj_Class **mro_itr;
+        klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
+
+        mro_itr = klass->mro;
+
+        EINA_LIST_FREE(mro, kls_itr)
+          {
+             *(mro_itr++) = kls_itr;
+
+             DBG("Added '%s' to MRO", kls_itr->desc->name);
+          }
+        *(mro_itr) = NULL;
+     }
+
+   DBG("Finished creating MRO for class '%s'", klass->desc->name);
+
+   return EINA_TRUE;
+}
+
 static void
 _eobj_class_constructor(Eobj_Class *klass)
 {
@@ -380,10 +574,8 @@ _eobj_class_constructor(Eobj_Class *klass)
 
    klass->constructed = EINA_TRUE;
 
-   if (!klass->desc->class_constructor)
-      return;
-
-   klass->desc->class_constructor(klass);
+   if (klass->desc->class_constructor)
+      klass->desc->class_constructor(klass);
 }
 
 EAPI void
@@ -400,25 +592,87 @@ eobj_class_funcs_set(Eobj_Class *klass, const Eobj_Op_Func_Description *func_des
      }
 }
 
-static Eina_Bool
-_eobj_class_extn_exists(const Eobj_Class *klass, const Eobj_Class *extn_cls)
+void
+eobj_class_free(Eobj_Class *klass)
 {
-   while (klass)
+   if (klass->constructed)
      {
+        if (klass->desc->class_destructor)
+           klass->desc->class_destructor(klass);
+
+        dich_func_clean_all(klass);
+     }
+
+     {
+        Eina_Inlist *itrn;
         Eobj_Extension_Node *extn;
-        EINA_INLIST_FOREACH(klass->extensions, extn)
+        EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
+          {
+             free(extn);
+          }
+     }
+
+   if (klass->mro)
+      free(klass->mro);
+
+   if (klass->extn_data_off)
+      free(klass->extn_data_off);
+
+   free(klass);
+}
+
+/* DEVCHECK */
+static Eina_Bool
+_eobj_class_check_op_descs(const Eobj_Class *klass)
+{
+   const Eobj_Class_Description *desc = klass->desc;
+   const Eobj_Op_Description *itr;
+   size_t i;
+
+   if (desc->ops.count > 0)
+     {
+        if (!desc->ops.base_op_id)
           {
-             if (extn->klass == extn_cls)
-                return EINA_TRUE;
+             ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
+                   desc->name);
+             return EINA_FALSE;
           }
 
-        klass = klass->parent;
+        if (!desc->ops.descs)
+          {
+             ERR("Class '%s' has a non-zero ops count, but there are no descs.",
+                   desc->name);
+             return EINA_FALSE;
+          }
      }
 
-   return EINA_FALSE;
+   itr = desc->ops.descs;
+   for (i = 0 ; i < desc->ops.count ; i++, itr++)
+     {
+        if (itr->sub_op != i)
+          {
+             if (itr->name)
+               {
+                  ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", 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);
+               }
+             return EINA_FALSE;
+          }
+     }
+
+   if (itr && itr->name)
+     {
+        ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
+        return EINA_FALSE;
+     }
+
+   return EINA_TRUE;
 }
 
-EAPI Eobj_Class *
+EAPI const Eobj_Class *
 eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...)
 {
    Eobj_Class *klass;
@@ -431,7 +685,7 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
      { \
         if (!x) \
           { \
-             ERR("%s can't be NULL! Aborting.", #x); \
+             ERR("%s must not be NULL! Aborting.", #x); \
              return NULL; \
           } \
      } \
@@ -442,14 +696,6 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
 
    klass = calloc(1, sizeof(Eobj_Class));
    klass->parent = parent;
-   klass->class_id = ++_eobj_classes_last_id;
-     {
-        /* FIXME: Handle errors. */
-        Eobj_Class **tmp;
-        tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
-        _eobj_classes = tmp;
-        _eobj_classes[klass->class_id - 1] = klass;
-     }
 
    /* Handle class extensions */
      {
@@ -469,7 +715,6 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
                      {
                         Eobj_Extension_Node *node = calloc(1, sizeof(*node));
                         node->klass = extn;
-                        node->exists = _eobj_class_extn_exists(klass, extn);
                         klass->extensions =
                            eina_inlist_append(klass->extensions,
                                  EINA_INLIST_GET(node));
@@ -482,50 +727,114 @@ eobj_class_new(const Eobj_Class_Description *desc, const Eobj_Class *parent, ...
      }
 
    klass->desc = desc;
-   _eobj_class_base_op_init(klass);
 
-   /* FIXME: Shouldn't be called here - should be called from eobj_add. */
-   _eobj_class_constructor(klass);
+   /* Handle the inheritance */
+   if (klass->parent)
+     {
+        /* Verify the inheritance is allowed. */
+        switch (klass->desc->type)
+          {
+           case EOBJ_CLASS_TYPE_REGULAR:
+           case EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT:
+              if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR) &&
+                    (klass->parent->desc->type != EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT))
+                {
+                   ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
+                   goto cleanup;
+                }
+              break;
+           case EOBJ_CLASS_TYPE_INTERFACE:
+           case EOBJ_CLASS_TYPE_MIXIN:
+              if ((klass->parent->desc->type != EOBJ_CLASS_TYPE_INTERFACE) &&
+                    (klass->parent->desc->type != EOBJ_CLASS_TYPE_MIXIN))
+                {
+                   ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
+                   goto cleanup;
+                }
+              break;
+          }
 
-   va_end(p_list);
 
-   return klass;
-}
-#undef _CLS_NEW_CHECK
+        /* Update the current offset. */
+        /* FIXME: Make sure this alignment is enough. */
+        klass->data_offset = klass->parent->data_offset +
+           EOBJ_ALIGN_SIZE(klass->parent->desc->data_size);
+     }
 
-EAPI void
-eobj_class_free(Eobj_Class *klass)
-{
-   if (klass->constructed)
+   if (!_eobj_class_check_op_descs(klass))
      {
-        if (klass->desc->class_destructor)
-           klass->desc->class_destructor(klass);
+        goto cleanup;
+     }
 
-        dich_func_clean_all(klass);
+   if (!_eobj_class_mro_init(klass))
+     {
+        goto cleanup;
      }
 
+   /* create MIXIN offset table. */
      {
-        Eina_Inlist *itrn;
-        Eobj_Extension_Node *extn;
-        EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
+        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)
           {
-             free(extn);
+             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;
      }
 
-   free(klass);
-}
+   klass->class_id = ++_eobj_classes_last_id;
+     {
+        /* FIXME: Handle errors. */
+        Eobj_Class **tmp;
+        tmp = realloc(_eobj_classes, _eobj_classes_last_id * sizeof(*_eobj_classes));
+        _eobj_classes = tmp;
+        _eobj_classes[klass->class_id - 1] = klass;
+     }
 
-static int
-_eobj_class_count_parents(const Eobj_Class *klass)
-{
-   int count = 0;
+   _eobj_class_base_op_init(klass);
+
+   /* FIXME: Shouldn't be called here - should be called from eobj_add. */
+   _eobj_class_constructor(klass);
+
+   va_end(p_list);
 
-   for (count = 0 ; klass->parent ; klass = klass->parent)
-      count++;
+   return klass;
 
-   return count;
+cleanup:
+   eobj_class_free(klass);
+   return NULL;
 }
+#undef _CLS_NEW_CHECK
 
 EAPI Eobj *
 eobj_add(const Eobj_Class *klass, Eobj *parent)
@@ -541,61 +850,96 @@ eobj_add(const Eobj_Class *klass, Eobj *parent)
    obj->parent = parent;
 
    obj->refcount++;
-     {
-        size_t datas_count = 0;
-        intptr_t offset = 0;
-        size_t i;
-        const Eobj_Class *kls_itr;
-        void **pvt_itr;
-        datas_count = _eobj_class_count_parents(klass) + 1;
-
-        obj->datas = calloc(datas_count, sizeof(*(obj->datas)));
 
-        /* Calculate all the offsets and set in the datas array. */
-        pvt_itr = obj->datas + datas_count - 1;
-        for (kls_itr = klass ; kls_itr->parent ; kls_itr = kls_itr->parent)
-           {
-              *pvt_itr = (void *) offset;
+   obj->data_blob = calloc(1, klass->data_offset + klass->desc->data_size +
+         klass->extn_data_size);
 
-              /* FIXME: Make sure this alignment is enough. */
-              offset += kls_itr->desc->private_size +
-                 (sizeof(void *) -
-                  (kls_itr->desc->private_size % sizeof(void *)));
-              pvt_itr--;
-           }
-
-        /* Allocate the datas blob and update the offsets. */
-        obj->data_blob = calloc(1, offset);
-
-        pvt_itr = obj->datas;
-        for (i = 0 ; i < datas_count ; i++)
-          {
-             *pvt_itr = ((char *) obj->data_blob) + (intptr_t) *pvt_itr;
-
-              pvt_itr++;
-          }
-     }
+   _eobj_kls_itr_init(obj, EOBJ_NOOP);
+   eobj_constructor_error_unset(obj);
 
+   eobj_ref(obj);
    eobj_class_constructor(obj, klass);
-   if (obj->kls_itr && obj->kls_itr->parent)
+
+   if (eobj_constructor_error_get(obj))
      {
-        ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
+        ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
         goto fail;
      }
 
-   if (eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY))
+   if (!_eobj_kls_itr_reached_end(obj))
      {
-        ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
+        ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
         goto fail;
      }
+   _eobj_kls_itr_end(obj, EOBJ_NOOP);
+   eobj_unref(obj);
 
    return obj;
 
 fail:
+   /* Unref twice, once for the ref above, and once for the basic object ref. */
+   eobj_unref(obj);
    eobj_unref(obj);
    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)
 {
@@ -608,35 +952,118 @@ eobj_unref(Eobj *obj)
 {
    if (--(obj->refcount) == 0)
      {
+        /* We need that for the event callbacks that may ref/unref. */
+        obj->refcount++;
+
+        if (!obj->delete)
+          {
+             eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
+             obj->delete = EINA_TRUE;
+          }
+        eobj_event_callback_call(obj, EOBJ_EV_FREE, NULL);
+
+        obj->refcount--;
+
         const Eobj_Class *klass = eobj_class_get(obj);
+        _eobj_kls_itr_init(obj, EOBJ_NOOP);
+        eobj_constructor_error_unset(obj);
         eobj_class_destructor(obj, klass);
+        if (eobj_constructor_error_get(obj))
+          {
+             ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
+          }
+
+        if (!_eobj_kls_itr_reached_end(obj))
+          {
+             ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
+          }
+        _eobj_kls_itr_end(obj, EOBJ_NOOP);
         /*FIXME: add eobj_class_unref(klass) ? - just to clear the caches. */
 
+        /* If for some reason it's not empty, clear it. */
+        while (obj->kls_itr)
+          {
+             WRN("Kls_Itr is not empty, possibly a bug, please report. - An error will be reported for each kls_itr in the stack.");
+             Eina_Inlist *nitr = nitr->next;
+             free(EINA_INLIST_CONTAINER_GET(obj->kls_itr, Eobj_Kls_Itr_Node));
+             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)
           {
-             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);
 
         if (obj->data_blob)
            free(obj->data_blob);
-        free(obj->datas);
-
-        _eobj_generic_data_del_all(obj);
 
         free(obj);
      }
 }
 
+EAPI int
+eobj_ref_get(const Eobj *obj)
+{
+   return obj->refcount;
+}
+
+/* Weak reference. */
+Eina_Bool
+_eobj_weak_ref_cb(void *data, Eobj *obj __UNUSED__, const Eobj_Event_Description *desc __UNUSED__, void *event_info __UNUSED__)
+{
+   Eobj_Weak_Ref *wref = data;
+   wref->obj = NULL;
+
+   return EINA_TRUE;
+}
+
+EAPI Eobj_Weak_Ref *
+eobj_weak_ref_new(const Eobj *_obj)
+{
+   Eobj *obj = (Eobj *) _obj;
+   Eobj_Weak_Ref *wref = calloc(1, sizeof(*wref));
+   wref->obj = obj;
+   eobj_event_callback_add(obj, EOBJ_EV_DEL, _eobj_weak_ref_cb, wref);
+
+   return wref;
+}
+
+EAPI void
+eobj_weak_ref_free(Eobj_Weak_Ref *wref)
+{
+   if (wref->obj)
+     {
+        eobj_event_callback_del_full(wref->obj, EOBJ_EV_DEL, _eobj_weak_ref_cb,
+              wref);
+     }
+   free(wref);
+}
+
+/* EOF Weak reference. */
+
 EAPI void
 eobj_del(Eobj *obj)
 {
-   obj->delete = EINA_TRUE;
+   if (!obj->delete)
+     {
+        eobj_event_callback_call(obj, EOBJ_EV_DEL, NULL);
+        obj->delete = EINA_TRUE;
+     }
    eobj_unref(obj);
 }
 
@@ -649,13 +1076,19 @@ eobj_parent_get(Eobj *obj)
 EAPI void
 eobj_constructor_error_set(Eobj *obj)
 {
-   eobj_generic_data_set(obj, CONSTRUCT_ERROR_KEY, (void *) EINA_TRUE);
+   obj->construct_error = EINA_TRUE;
+}
+
+static void
+eobj_constructor_error_unset(Eobj *obj)
+{
+   obj->construct_error = EINA_FALSE;
 }
 
 EAPI Eina_Bool
 eobj_constructor_error_get(const Eobj *obj)
 {
-   return (intptr_t) eobj_generic_data_get(obj, CONSTRUCT_ERROR_KEY);
+   return obj->construct_error;
 }
 
 static inline void
@@ -673,22 +1106,11 @@ _eobj_destructor_default(Eobj *obj)
 static void
 eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
 {
-   const Eobj_Extension_Node *extn;
-
-   obj->kls_itr = klass;
-
    if (!klass)
       return;
 
-   EINA_INLIST_FOREACH(klass->extensions, extn)
-     {
-        /* Only call if it's the first one in the class. */
-        if (!extn->exists && extn->klass->desc->constructor)
-           extn->klass->desc->constructor(obj);
-     }
-
    if (klass->desc->constructor)
-      klass->desc->constructor(obj);
+      klass->desc->constructor(obj, eobj_data_get(obj, klass));
    else
       _eobj_constructor_default(obj);
 }
@@ -696,39 +1118,25 @@ eobj_class_constructor(Eobj *obj, const Eobj_Class *klass)
 static void
 eobj_class_destructor(Eobj *obj, const Eobj_Class *klass)
 {
-   const Eobj_Extension_Node *extn;
-
-   obj->kls_itr = klass;
-
    if (!klass)
       return;
 
    if (klass->desc->destructor)
-      klass->desc->destructor(obj);
+      klass->desc->destructor(obj, eobj_data_get(obj, klass));
    else
       _eobj_destructor_default(obj);
-
-   EINA_INLIST_REVERSE_FOREACH(klass->extensions, extn)
-     {
-        /* Only call if it's the first one in the class. */
-        if (!extn->exists && extn->klass->desc->destructor)
-           extn->klass->desc->destructor(obj);
-     }
-
 }
 
 EAPI void
 eobj_constructor_super(Eobj *obj)
 {
-   if (obj->kls_itr->parent)
-      eobj_class_constructor(obj, obj->kls_itr->parent);
+   eobj_class_constructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
 }
 
 EAPI void
 eobj_destructor_super(Eobj *obj)
 {
-   if (obj->kls_itr->parent)
-      eobj_class_destructor(obj, obj->kls_itr->parent);
+   eobj_class_destructor(obj, _eobj_kls_itr_next(obj, EOBJ_NOOP));
 }
 
 EAPI void *
@@ -736,103 +1144,36 @@ 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. */
-   return obj->datas[_eobj_class_count_parents(klass)];
-}
-
-typedef struct
-{
-   EINA_INLIST;
-   Eina_Stringshare *key;
-   void *data;
-} Eobj_Generic_Data_Node;
-
-static void
-_eobj_generic_data_node_free(Eobj_Generic_Data_Node *node)
-{
-   eina_stringshare_del(node->key);
-   free(node);
-}
-
-static void
-_eobj_generic_data_del_all(Eobj *obj)
-{
-   Eina_Inlist *nnode;
-   Eobj_Generic_Data_Node *node;
-
-   EINA_INLIST_FOREACH_SAFE(obj->generic_data, nnode, node)
+   if (klass->desc->data_size > 0)
      {
-        obj->generic_data = eina_inlist_remove(obj->generic_data,
-              EINA_INLIST_GET(node));
-
-        _eobj_generic_data_node_free(node);
-     }
-}
-
-EAPI void *
-eobj_generic_data_set(Eobj *obj, const char *key, const void *data)
-{
-   void *prev_data;
-   Eobj_Generic_Data_Node *node;
-
-   if (!key) return NULL;
-   if (!data) return NULL;
-
-   prev_data = eobj_generic_data_del(obj, key);
-
-   node = malloc(sizeof(Eobj_Generic_Data_Node));
-   node->key = eina_stringshare_add(key);
-   node->data = (void *) data;
-   obj->generic_data = eina_inlist_prepend(obj->generic_data,
-         EINA_INLIST_GET(node));
-
-   return prev_data;
-}
-
-EAPI void *
-eobj_generic_data_get(const Eobj *obj, const char *key)
-{
-   Eobj_Generic_Data_Node *node;
-
-   if (!key) return NULL;
-
-   EINA_INLIST_FOREACH(obj->generic_data, node)
-     {
-        if (!strcmp(node->key, key))
+        if (klass->desc->type == EOBJ_CLASS_TYPE_MIXIN)
           {
-             ((Eobj *) obj)->generic_data =
-                eina_inlist_promote(obj->generic_data, EINA_INLIST_GET(node));
-             return node->data;
-          }
-     }
-   return NULL;
-}
-
-EAPI void *
-eobj_generic_data_del(Eobj *obj, const char *key)
-{
-   Eobj_Generic_Data_Node *node;
+             Eobj_Extension_Data_Offset *doff_itr =
+                eobj_class_get(obj)->extn_data_off;
 
-   if (!key) return NULL;
+             if (!doff_itr)
+                return NULL;
 
-   EINA_INLIST_FOREACH(obj->generic_data, node)
-     {
-        if (!strcmp(node->key, key))
+             while (doff_itr->klass)
+               {
+                  if (doff_itr->klass == klass)
+                     return ((char *) obj->data_blob) + doff_itr->offset;
+                  doff_itr++;
+               }
+          }
+        else
           {
-             void *data;
-
-             data = node->data;
-             obj->generic_data = eina_inlist_remove(obj->generic_data,
-                   EINA_INLIST_GET(node));
-             _eobj_generic_data_node_free(node);
-             return data;
+             return ((char *) obj->data_blob) + klass->data_offset;
           }
      }
+
    return NULL;
 }
 
 EAPI Eina_Bool
 eobj_init(void)
 {
+   const char *log_dom = "eobj";
    if (_eobj_init_count++ > 0)
       return EINA_TRUE;
 
@@ -840,10 +1181,10 @@ eobj_init(void)
 
    _eobj_classes = NULL;
    _eobj_classes_last_id = 0;
-   _eobj_log_dom = eina_log_domain_register("eobj", EINA_COLOR_LIGHTBLUE);
+   _eobj_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
    if (_eobj_log_dom < 0)
      {
-        EINA_LOG_ERR("Could not register log domain: eobj");
+        EINA_LOG_ERR("Could not register log domain: %s", log_dom);
         return EINA_FALSE;
      }
 
@@ -878,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);
 }
 
@@ -886,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
@@ -895,6 +1236,10 @@ eobj_composite_is(Eobj *emb_obj)
    Eobj *obj = eobj_parent_get(emb_obj);
    Eina_List *itr;
    Eobj *tmp;
+
+   if (!obj)
+      return EINA_FALSE;
+
    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
      {
         if (tmp == emb_obj)
@@ -968,16 +1313,6 @@ _callback_priority_cmp(const void *_a, const void *_b)
 }
 
 EAPI Eina_Bool
-eobj_event_callback_add(Eobj *obj,
-      const Eobj_Event_Description *desc,
-      Eobj_Event_Cb cb,
-      const void *data)
-{
-   return eobj_event_callback_priority_add(obj, desc,
-         EOBJ_CALLBACK_PRIORITY_DEFAULT, cb, data);
-}
-
-EAPI Eina_Bool
 eobj_event_callback_priority_add(Eobj *obj,
       const Eobj_Event_Description *desc,
       Eobj_Callback_Priority priority,
@@ -992,7 +1327,7 @@ eobj_event_callback_priority_add(Eobj *obj,
    obj->callbacks = eina_inlist_sorted_insert(obj->callbacks,
          EINA_INLIST_GET(cb), _callback_priority_cmp);
 
-   eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_ADD, desc);
+   eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_ADD, desc);
 
    return EINA_TRUE;
 }
@@ -1012,12 +1347,14 @@ eobj_event_callback_del(Eobj *obj, const Eobj_Event_Description *desc, Eobj_Even
              cb->delete_me = EINA_TRUE;
              _eobj_callbacks_clear(obj);
              ret = data;
-             goto end;
+             goto found;
           }
      }
 
-end:
-   eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
+   return NULL;
+
+found:
+   eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
    return ret;
 }
 
@@ -1037,12 +1374,14 @@ eobj_event_callback_del_full(Eobj *obj, const Eobj_Event_Description *desc, Eobj
              cb->delete_me = EINA_TRUE;
              _eobj_callbacks_clear(obj);
              ret = data;
-             goto end;
+             goto found;
           }
      }
 
-end:
-   eobj_event_callback_call(obj, EOBJ_SIG_CALLBACK_DEL, desc);
+   return NULL;
+
+found:
+   eobj_event_callback_call(obj, EOBJ_EV_CALLBACK_DEL, desc);
    return ret;
 }
 
@@ -1050,18 +1389,21 @@ EAPI Eina_Bool
 eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
       const void *event_info)
 {
+   Eina_Bool ret = EINA_TRUE;
    Eobj_Callback_Description *cb;
 
+   eobj_ref(obj);
    obj->walking_list++;
 
    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,
                       (void *) event_info))
                {
+                  ret = EINA_FALSE;
                   break;
                }
           }
@@ -1070,8 +1412,9 @@ eobj_event_callback_call(Eobj *obj, const Eobj_Event_Description *desc,
      }
    obj->walking_list--;
    _eobj_callbacks_clear(obj);
+   eobj_unref(obj);
 
-   return EINA_TRUE;
+   return ret;
 }
 
 static Eina_Bool
@@ -1096,44 +1439,3 @@ eobj_event_callback_forwarder_del(Eobj *obj, const Eobj_Event_Description *desc,
    return EINA_TRUE;
 }
 
-/* EOBJ_CLASS_BASE stuff */
-static Eobj_Class *_my_class = NULL;
-
-/* FIXME: Set proper type descriptions. */
-EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_ADD =
-   EOBJ_EVENT_DESCRIPTION("callback,add", "?", "Called when a callback was added.");
-EAPI const Eobj_Event_Description _EOBJ_SIG_CALLBACK_DEL =
-   EOBJ_EVENT_DESCRIPTION("callback,del", "?", "Called when a callback was deleted.");
-
-static void
-_constructor(Eobj *obj)
-{
-   DBG("%p - %s.", obj, _my_class->desc->name);
-}
-
-static void
-_destructor(Eobj *obj)
-{
-   DBG("%p - %s.", obj, _my_class->desc->name);
-}
-
-EAPI const Eobj_Class *
-eobj_base_class_get(void)
-{
-   if (_my_class) return _my_class;
-
-   static const Eobj_Class_Description class_desc = {
-        "Eobj Base",
-        EOBJ_CLASS_TYPE_REGULAR_NO_INSTANT,
-        EOBJ_CLASS_DESCRIPTION_OPS(NULL, NULL, 0),
-        NULL,
-        0,
-        _constructor,
-        _destructor,
-        NULL,
-        NULL
-   };
-
-   return _my_class = eobj_class_new(&class_desc, NULL, NULL);
-}
-