Eo: Get rid of handle. Use the shared header for detection.
authorTom Hacohen <tom@stosb.com>
Fri, 27 Sep 2013 10:13:14 +0000 (11:13 +0100)
committerTom Hacohen <tom@stosb.com>
Fri, 27 Sep 2013 13:01:47 +0000 (14:01 +0100)
src/lib/eo/eo.c
src/lib/eo/eo_private.h
src/lib/eo/eo_ptr_indirection.x

index a58273e..677d964 100644 (file)
@@ -23,7 +23,6 @@ static Eo_Op _eo_ops_last_id = 0;
 
 static size_t _eo_sz = 0;
 static size_t _eo_class_sz = 0;
-static size_t _eo_handle_sz = 0;
 
 static void _eo_condtor_reset(_Eo_Object *obj);
 static inline void *_eo_data_scope_get(const _Eo_Object *obj, const _Eo_Class *klass);
@@ -138,13 +137,15 @@ static const Eo_Op_Description noop_desc =
 static inline Eina_Bool
 _eo_is_a_class(const Eo *obj_id)
 {
+   Eo_Id oid;
 #ifdef HAVE_EO_ID
-   return (((((Eo_Id) obj_id) >> REF_TAG_SHIFT) & 0x1) == 0x0);
+   oid = (Eo_Id) obj_id;
 #else
    /* fortunately EO_OBJ_POINTER_RETURN* will handle NULL obj_id */
    if (!obj_id) return EINA_FALSE;
-   return (((_Eo_Handle *) obj_id)->is_a_class == 1);
+   oid = ((Eo_Header *) obj_id)->id;
 #endif
+   return (((oid >> REF_TAG_SHIFT) & 0x1) == 0x0);
 }
 
 static inline _Eo_Class *
@@ -153,17 +154,7 @@ _eo_class_pointer_get(const Eo *klass_id)
 #ifdef HAVE_EO_ID
    return ID_CLASS_GET((Eo_Id)klass_id);
 #else
-   return (_Eo_Class *) EO_FROM_HANDLE(klass_id);
-#endif
-}
-
-static inline
-Eo * _eo_class_id_get(const _Eo_Class *klass)
-{
-#ifdef HAVE_EO_ID
-   return (Eo *) klass->header.id;
-#else
-   return (Eo *) HANDLE_FROM_EO(klass);
+   return (_Eo_Class *) klass_id;
 #endif
 }
 
@@ -306,7 +297,7 @@ _eo_op_internal(const char *file, int line, _Eo eo_ptr, const _Eo_Class *cur_kla
              if (op_type == EO_OP_TYPE_REGULAR)
                {
                   func_data = _eo_data_scope_get(eo_ptr.obj, func->src);
-                  calling_obj = (Eo *)eo_ptr.obj->header.id;
+                  calling_obj = _eo_id_get((Eo *) eo_ptr.obj);
                }
              else
                {
@@ -692,23 +683,15 @@ eo_class_free(_Eo_Class *klass)
      }
 
    EINA_TRASH_CLEAN(&klass->objects.trash, data)
-#ifdef HAVE_EO_ID
-     free(data);
-#else
-     free((void *) HANDLE_FROM_EO(data));
-#endif
+      free(data);
 
    EINA_TRASH_CLEAN(&klass->iterators.trash, data)
-     free(data);
+      free(data);
 
    eina_lock_free(&klass->objects.trash_lock);
    eina_lock_free(&klass->iterators.trash_lock);
 
-#ifdef HAVE_EO_ID
    free(klass);
-#else
-   free((void *) HANDLE_FROM_EO(klass));
-#endif
 }
 
 /* DEVCHECK */
@@ -772,7 +755,6 @@ EAPI const Eo *
 eo_class_new(const Eo_Class_Description *desc, const Eo *parent_id, ...)
 {
    _Eo_Class *klass;
-   _Eo_Handle *hndl;
    va_list p_list;
    size_t extn_sz, mro_sz, mixins_sz;
    Eina_List *extn_list, *mro, *mixins;
@@ -900,13 +882,8 @@ eo_class_new(const Eo_Class_Description *desc, const Eo *parent_id, ...)
         DBG("Finished building Mixins list for class '%s'", desc->name);
      }
 
-#ifdef HAVE_EO_ID
-   (void) hndl;
    klass = calloc(1, _eo_class_sz + extn_sz + mro_sz + mixins_sz);
-#else
-   hndl = calloc(1, _eo_handle_sz + _eo_class_sz + extn_sz + mro_sz + mixins_sz);
-   hndl->is_a_class = 1;
-   klass = (_Eo_Class *) EO_FROM_HANDLE(hndl);
+#ifndef HAVE_EO_ID
    EINA_MAGIC_SET((Eo_Header *) klass, EO_CLASS_EINA_MAGIC);
 #endif
    eina_lock_new(&klass->objects.trash_lock);
@@ -975,11 +952,7 @@ eo_class_new(const Eo_Class_Description *desc, const Eo *parent_id, ...)
         extn_data_itr->offset = 0;
      }
 
-#ifdef HAVE_EO_ID
    klass->obj_size = _eo_sz + extn_data_off;
-#else
-   klass->obj_size = _eo_handle_sz + _eo_sz + extn_data_off;
-#endif
    if (getenv("EO_DEBUG"))
      {
         fprintf(stderr, "Eo class '%s' will take %u bytes per object.\n",
@@ -1076,7 +1049,6 @@ eo_add_internal(const char *file, int line, const Eo *klass_id, Eo *parent_id, .
 {
    Eina_Bool do_err;
    _Eo_Object *obj;
-   _Eo_Handle *hndl;
    EO_CLASS_POINTER_RETURN_VAL(klass_id, klass, NULL);
 
    if (parent_id)
@@ -1099,14 +1071,7 @@ eo_add_internal(const char *file, int line, const Eo *klass_id, Eo *parent_id, .
      }
    else
      {
-#ifdef HAVE_EO_ID
-        (void) hndl;
         obj = calloc(1, klass->obj_size);
-#else
-        hndl = calloc(1, klass->obj_size);
-        hndl->is_a_class = 0;
-        obj  = EO_FROM_HANDLE(hndl);
-#endif
      }
    eina_lock_release(&klass->objects.trash_lock);
 
@@ -1149,7 +1114,7 @@ eo_add_internal(const char *file, int line, const Eo *klass_id, Eo *parent_id, .
 
    _eo_unref(obj);
 
-   return (Eo *)obj_id;
+   return _eo_id_get((Eo *) obj);
 
 fail:
    /* Unref twice, once for the ref above, and once for the basic object ref. */
@@ -1303,7 +1268,7 @@ _eo_data_xref_internal(const char *file, int line, _Eo_Object *obj, const _Eo_Cl
    (obj->datarefcount)++;
 #ifdef EO_DEBUG
    Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
-   xref->ref_obj = (Eo *)ref_obj->header.id;
+   xref->ref_obj = _eo_id_get(ref_obj);
    xref->file = file;
    xref->line = line;
 
@@ -1332,7 +1297,7 @@ _eo_data_xunref_internal(_Eo_Object *obj, void *data, const _Eo_Object *ref_obj)
 #endif
    if (obj->datarefcount == 0)
      {
-        ERR("Data for object %lx (%s) is already not referenced.", (unsigned long)obj->header.id, obj->klass->desc->name);
+        ERR("Data for object %lx (%s) is already not referenced.", (unsigned long) _eo_id_get((Eo *) obj), obj->klass->desc->name);
      }
    else
      {
@@ -1342,7 +1307,7 @@ _eo_data_xunref_internal(_Eo_Object *obj, void *data, const _Eo_Object *ref_obj)
    Eo_Xref_Node *xref = NULL;
    EINA_INLIST_FOREACH(obj->data_xrefs, xref)
      {
-        if (xref->ref_obj == (Eo *)ref_obj->header.id)
+        if (xref->ref_obj == _eo_id_get(ref_obj))
           break;
      }
 
@@ -1353,7 +1318,7 @@ _eo_data_xunref_internal(_Eo_Object *obj, void *data, const _Eo_Object *ref_obj)
      }
    else
      {
-        ERR("ref_obj (0x%lx) does not reference data (%p) of obj (0x%lx).", (unsigned long)ref_obj->header.id, data, (unsigned long)obj->header.id);
+        ERR("ref_obj (0x%lx) does not reference data (%p) of obj (0x%lx).", (unsigned long) _eo_id_get(ref_obj), data, (unsigned long)_eo_id_get(obj));
      }
 #else
    (void) ref_obj;
@@ -1445,7 +1410,6 @@ eo_init(void)
 
    _eo_sz = EO_ALIGN_SIZE(sizeof(_Eo_Object));
    _eo_class_sz = EO_ALIGN_SIZE(sizeof(_Eo_Class));
-   _eo_handle_sz = EO_ALIGN_SIZE(sizeof(_Eo_Handle));
 
    _eo_classes = NULL;
    _eo_classes_last_id = EO_CLASS_IDS_FIRST - 1;
index 2402f04..a1ac9fe 100644 (file)
@@ -58,7 +58,6 @@ typedef uintptr_t Eo_Id;
 typedef struct _Eo_Class _Eo_Class;
 typedef struct _Eo_Object _Eo_Object;
 typedef struct _Eo_Header Eo_Header;
-typedef struct _Eo_Handle _Eo_Handle;
 typedef union _Eo {
      _Eo_Object *obj;
      _Eo_Class  *klass;
@@ -78,10 +77,6 @@ static inline void _eo_free_ids_tables(void);
 
 void _eo_condtor_done(Eo *obj);
 
-struct _Eo_Handle {
-     Eina_Bool is_a_class:1;
-};
-
 struct _Eo_Header
 {
 #ifndef HAVE_EO_ID
@@ -180,6 +175,28 @@ typedef struct
    int line;
 } Eo_Xref_Node;
 
+static inline
+Eo *_eo_header_id_get(const Eo_Header *header)
+{
+#ifdef HAVE_EO_ID
+   return (Eo *) header->id;
+#else
+   return (Eo *) header;
+#endif
+}
+
+static inline
+Eo *_eo_class_id_get(const _Eo_Class *klass)
+{
+   return _eo_header_id_get((Eo_Header *) klass);
+}
+
+static inline
+Eo *_eo_id_get(const Eo *obj)
+{
+   return _eo_header_id_get((Eo_Header *) obj);
+}
+
 static inline void
 _eo_condtor_reset(_Eo_Object *obj)
 {
@@ -193,13 +210,13 @@ _eo_del_internal(const char *file, int line, _Eo_Object *obj)
    /* We need that for the event callbacks that may ref/unref. */
    obj->refcount++;
 
-   eo_do((Eo *) obj->header.id, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
+   eo_do(_eo_id_get((Eo *) obj), eo_event_callback_call(EO_EV_DEL, NULL, NULL));
 
    const _Eo_Class *klass = obj->klass;
 
    _eo_condtor_reset(obj);
 
-   do_err = eo_do((Eo *)obj->header.id, eo_destructor());
+   do_err = eo_do(_eo_id_get((Eo *) obj), eo_destructor());
    if (EINA_UNLIKELY(!do_err))
      {
         ERR("in %s:%d: Object of class '%s' - One of the object destructors have failed.",
@@ -218,7 +235,7 @@ _eo_del_internal(const char *file, int line, _Eo_Object *obj)
         Eo *emb_obj;
         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
           {
-             eo_composite_detach(emb_obj, (Eo *)obj->header.id);
+             eo_composite_detach(emb_obj, _eo_id_get((Eo *) obj));
           }
      }
 
@@ -237,7 +254,7 @@ _eo_free(_Eo_Object *obj)
         ERR("Object %p data still referenced %d time(s).", obj, obj->datarefcount);
      }
 #endif
-   _eo_id_release(obj->header.id);
+   _eo_id_release((Eo_Id) _eo_id_get((Eo *) obj));
 
    eina_lock_take(&klass->objects.trash_lock);
    if (klass->objects.trash_count <= 8)
@@ -247,11 +264,7 @@ _eo_free(_Eo_Object *obj)
      }
    else
      {
-#ifdef HAVE_EO_ID
         free(obj);
-#else
-        free(((char *) obj) - eina_mempool_alignof(sizeof (_Eo_Handle)));
-#endif
      }
    eina_lock_release(&klass->objects.trash_lock);
 }
@@ -290,7 +303,7 @@ _eo_unref(_Eo_Object *obj)
           {
              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%lx is still referenced by object %p", (unsigned long)obj->header.id, xref->ref_obj);
+             ERR("Data of object 0x%lx is still referenced by object %p", (unsigned long) _eo_id_get((Eo *) obj), xref->ref_obj);
 
              free(xref);
              obj->data_xrefs = nitr;
index 201f908..bd1dcb1 100644 (file)
@@ -189,11 +189,6 @@ _eo_id_mem_protect(void *ptr, Eina_Bool may_not_write)
 
 #define EO_ALIGN_SIZE(size) eina_mempool_alignof(size)
 
-#ifndef HAVE_EO_ID
-# define HANDLE_FROM_EO(eo) (Eo_Id)( eo ? (((char *) eo) - EO_ALIGN_SIZE(sizeof (_Eo_Handle))) : NULL )
-# define EO_FROM_HANDLE(hndl) (_Eo_Object *) ( hndl ? (((char *) hndl) + EO_ALIGN_SIZE(sizeof (_Eo_Handle))) : NULL )
-#endif
-
 /* Entry */
 typedef struct
 {
@@ -281,7 +276,7 @@ _eo_obj_pointer_get(const Eo_Id obj_id)
 
    return NULL;
 #else
-   return EO_FROM_HANDLE(obj_id);
+   return (_Eo_Object *) obj_id;
 #endif
 }
 
@@ -372,6 +367,7 @@ _search_tables(void)
    return NULL;
 }
 
+/* Gives a fake id that serves as a marker if eo id is off. */
 static inline Eo_Id
 _eo_id_allocate(const _Eo_Object *obj)
 {
@@ -401,7 +397,9 @@ _eo_id_allocate(const _Eo_Object *obj)
                               (entry - _current_table->entries),
                               entry->generation);
 #else
-   return HANDLE_FROM_EO(obj);
+   Eo_Id ret = 0x1;
+   (void) obj;
+   return ret << REF_TAG_SHIFT;
 #endif
 }