* @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);
* }
*
* @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.
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);
const _Eo_Class *klass;
#ifdef EO_DEBUG
Eina_Inlist *xrefs;
+ Eina_Inlist *data_xrefs;
#endif
Eina_List *composite_objects;
Eo_Id obj_id;
int refcount;
+ int datarefcount;
Eina_Bool do_error:1;
Eina_Bool condtor_done:1;
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;
}
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;
}
}
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
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);
}
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)
}
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))
{
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);
}
#endif
- ret = _eo_data_get(obj, klass);
+ ret = _eo_data_scope_get(obj, klass);
#ifdef EO_DEBUG
if (!ret && (klass->desc->data_size == 0))
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)
{
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);
}
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);
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);
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);
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);
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));
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;
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;
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);
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();
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);
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);