Eo: Moved more saftey checks to release mode only.
[profile/ivi/eobj.git] / lib / eo.c
1 #include <Eina.h>
2
3 #include "Eo.h"
4 #include "eo_private.h"
5
6 #include "config.h"
7
8 /* The last id that should be reserved for statically allocated classes. */
9 #define EO_STATIC_IDS_LAST 10
10
11 /* Used inside the class_get functions of classes, see #EO_DEFINE_CLASS */
12 EAPI Eina_Lock _eo_class_creation_lock;
13 int _eo_log_dom = -1;
14
15 static Eo_Class **_eo_classes;
16 static Eo_Class_Id _eo_classes_last_id;
17 static Eina_Bool _eo_init_count = 0;
18
19 static void _eo_constructor(Eo *obj, const Eo_Class *klass);
20 static void _eo_destructor(Eo *obj, const Eo_Class *klass);
21 static inline Eina_Bool _eo_error_get(const Eo *obj);
22 static inline void _eo_error_unset(Eo *obj);
23 static inline void *_eo_data_get(const Eo *obj, const Eo_Class *klass);
24 static inline Eo *_eo_ref(Eo *obj);
25 static inline void _eo_unref(Eo *obj);
26
27 typedef struct
28 {
29    Eo_Op op;
30    const Eo_Class **kls_itr;
31 } Eo_Kls_Itr;
32
33 struct _Eo {
34      EINA_MAGIC
35      Eo *parent;
36      const Eo_Class *klass;
37      int refcount;
38 #ifndef NDEBUG
39      Eina_Inlist *xrefs;
40 #endif
41
42      Eina_List *composite_objects;
43
44      Eo_Kls_Itr mro_itr;
45
46      Eina_Bool del:1;
47      Eina_Bool construct_error:1;
48      Eina_Bool manual_free:1;
49 };
50
51 /* Start of Dich */
52 /* Dich search, split to 0xff 0xff 0xffff */
53
54 #define DICH_CHAIN1_MASK (0xffff)
55 #define DICH_CHAIN_LAST_MASK (0xffff)
56 #define DICH_CHAIN1(x) (((x) >> 16) & DICH_CHAIN1_MASK)
57 #define DICH_CHAIN_LAST(x) ((x) & DICH_CHAIN_LAST_MASK)
58
59 #define OP_CLASS_OFFSET_GET(x) (((x) >> EO_OP_CLASS_OFFSET) & 0xffff)
60 #define OP_CLASS_GET(op) ({ \
61       Eo_Class_Id tmp = OP_CLASS_OFFSET_GET(op); \
62       ID_CLASS_GET(tmp); \
63       })
64 #define OP_SUB_ID_GET(op) ((op) & 0xffff)
65
66 #define ID_CLASS_GET(id) ({ \
67       (Eo_Class *) ((id <= _eo_classes_last_id) && (id > 0)) ? \
68       (_eo_classes[id - 1]) : NULL; \
69       })
70
71 #define EO_ALIGN_SIZE(size) \
72         ((size) + (sizeof(void *) - ((size) % sizeof(void *))))
73
74 /* Structure of Eo_Op is:
75  * 16bit: class
76  * 16bit: op.
77  */
78
79 typedef struct _Dich_Chain1 Dich_Chain1;
80
81 typedef struct
82 {
83    eo_op_func_type func;
84    const Eo_Class *src;
85 } op_type_funcs;
86
87 struct _Dich_Chain1
88 {
89    op_type_funcs *funcs;
90 };
91
92 typedef struct
93 {
94      EINA_INLIST;
95      const Eo_Class *klass;
96 } Eo_Extension_Node;
97
98 typedef struct
99 {
100      const Eo_Class *klass;
101      size_t offset;
102 } Eo_Extension_Data_Offset;
103
104 struct _Eo_Class
105 {
106    EINA_MAGIC
107    Eo_Class_Id class_id;
108    const Eo_Class *parent;
109    const Eo_Class_Description *desc;
110    Dich_Chain1 *chain; /**< The size is class_id */
111    Eina_Inlist *extensions;
112
113    Eo_Extension_Data_Offset *extn_data_off;
114    size_t extn_data_size;
115
116    const Eo_Class **mro;
117    Eo_Kls_Itr mro_itr;
118
119    size_t data_offset; /* < Offset of the data within object data. */
120
121    Eina_Bool constructed : 1;
122 };
123
124 static inline void
125 _dich_chain_alloc(Dich_Chain1 *chain1, size_t num_ops)
126 {
127    if (!chain1->funcs)
128      {
129         chain1->funcs = calloc(num_ops, sizeof(*(chain1->funcs)));
130      }
131 }
132
133 static inline void
134 _dich_copy_all(Eo_Class *dst, const Eo_Class *src)
135 {
136    if (!src->chain) return;
137
138    if (!dst->chain)
139      {
140         dst->chain = calloc(dst->class_id, sizeof(*dst->chain));
141      }
142
143    Eo_Class_Id i;
144    const Dich_Chain1 *sc1 = src->chain;
145    Dich_Chain1 *dc1 = dst->chain;
146    for (i = 0 ; i < src->class_id ; i++, sc1++, dc1++)
147      {
148         if (sc1->funcs)
149           {
150              size_t j;
151              const Eo_Class *op_klass = ID_CLASS_GET(i + 1);
152              /* Can be NULL because of future static classes. */
153              if (!op_klass)
154                 continue;
155
156              size_t num_ops = op_klass->desc->ops.count;
157              _dich_chain_alloc(dc1, num_ops);
158
159              const op_type_funcs *sf = sc1->funcs;
160              op_type_funcs *df = dc1->funcs;
161              for (j = 0 ; j < num_ops ; j++, df++, sf++)
162                {
163                   if (sf->func)
164                     {
165                        memcpy(df, sf, sizeof(*df));
166                     }
167                }
168           }
169      }
170 }
171
172 static inline const op_type_funcs *
173 _dich_func_get(const Eo_Class *klass, Eo_Op op)
174 {
175    if (!klass->chain) return NULL;
176
177    size_t idx1 = DICH_CHAIN1(op) - 1;
178    if (idx1 >= klass->class_id) return NULL;
179    const Dich_Chain1 *chain1 = &klass->chain[idx1];
180    if (!chain1->funcs) return NULL;
181
182    size_t idxl = DICH_CHAIN_LAST(op);
183    /* num_ops is calculated from the class. */
184    const Eo_Class *op_klass = ID_CLASS_GET(idx1 + 1);
185    if (!op_klass || (idxl >= op_klass->desc->ops.count))
186       return NULL;
187
188    return &chain1->funcs[idxl];
189 }
190
191 static inline void
192 _dich_func_set(Eo_Class *klass, Eo_Op op, eo_op_func_type func)
193 {
194    const Eo_Class *op_klass = OP_CLASS_GET(op);
195    size_t num_ops;
196
197    /* Verify op is valid. */
198    if (op_klass)
199      {
200         /* num_ops is calculated from the class. */
201         num_ops = op_klass->desc->ops.count;
202         if (DICH_CHAIN_LAST(op) >= num_ops)
203           {
204              ERR("OP %x is too big for the domain '%s', expected value < %x.",
205                    op, op_klass->desc->name, op_klass->desc->ops.count);
206              return;
207           }
208      }
209    else
210      {
211         ERR("OP %x is from an illegal class.", op);
212         return;
213      }
214
215    if (!klass->chain)
216      {
217         klass->chain = calloc(klass->class_id, sizeof(*klass->chain));
218      }
219
220    size_t idx1 = DICH_CHAIN1(op) - 1;
221    Dich_Chain1 *chain1 = &klass->chain[idx1];
222    _dich_chain_alloc(chain1, num_ops);
223    chain1->funcs[DICH_CHAIN_LAST(op)].func = func;
224    chain1->funcs[DICH_CHAIN_LAST(op)].src = klass;
225 }
226
227 static inline void
228 _dich_func_clean_all(Eo_Class *klass)
229 {
230    size_t i;
231    Dich_Chain1 *chain1 = klass->chain;
232
233    if (!chain1)
234       return;
235
236    for (i = 0 ; i < klass->class_id ; i++, chain1++)
237      {
238         if (chain1->funcs)
239            free(chain1->funcs);
240      }
241    free(klass->chain);
242    klass->chain = NULL;
243 }
244
245 /* END OF DICH */
246
247 static const Eo_Op_Description noop_desc =
248         EO_OP_DESCRIPTION(EO_NOOP, "", "No operation.");
249
250 static const Eo_Op_Description *
251 _eo_op_id_desc_get(Eo_Op op)
252 {
253    const Eo_Class *klass = OP_CLASS_GET(op);
254    Eo_Op sub_id = OP_SUB_ID_GET(op);
255
256    if (op == EO_NOOP)
257       return &noop_desc;
258
259    if (klass && (sub_id < klass->desc->ops.count))
260       return klass->desc->ops.descs + sub_id;
261
262    return NULL;
263 }
264
265 static const char *
266 _eo_op_id_name_get(Eo_Op op)
267 {
268    const Eo_Op_Description *desc = _eo_op_id_desc_get(op);
269    return (desc) ? desc->name : NULL;
270 }
271
272 static inline void
273 _eo_kls_itr_init(const Eo_Class *obj_klass, Eo_Kls_Itr *cur, Eo_Op op, Eo_Kls_Itr *prev_state)
274 {
275    prev_state->op = cur->op;
276    prev_state->kls_itr = cur->kls_itr;
277
278    /* If we are in a constructor/destructor or we changed an op - init. */
279    if ((op == EO_NOOP) || (cur->op != op))
280      {
281         cur->op = op;
282         cur->kls_itr = obj_klass->mro;
283      }
284 }
285
286 static inline void
287 _eo_kls_itr_end(Eo_Kls_Itr *cur, Eo_Kls_Itr *prev_state)
288 {
289    if (cur->op != prev_state->op)
290      {
291         cur->op = prev_state->op;
292         cur->kls_itr = prev_state->kls_itr;
293      }
294 }
295
296 static inline const Eo_Class *
297 _eo_kls_itr_get(Eo_Kls_Itr *cur)
298 {
299    return (cur->kls_itr) ? *(cur->kls_itr) : NULL;
300 }
301
302 static inline const Eo_Class *
303 _eo_kls_itr_next(Eo_Kls_Itr *cur, Eo_Op op)
304 {
305    if (cur->op != op)
306      {
307         Eo_Op node_op = cur->op;
308         ERR("Called with op %d ('%s') while expecting: %d ('%s'). This probaly means you called eo_*_super functions from a wrong place.",
309               op, _eo_op_id_name_get(op),
310               node_op, _eo_op_id_name_get(node_op));
311         return NULL;
312      }
313
314    const Eo_Class **kls_itr = cur->kls_itr;
315    if (*kls_itr)
316      {
317         if (op != EO_NOOP)
318           {
319              const op_type_funcs *fsrc = _dich_func_get(*kls_itr, op);
320
321              while (*kls_itr && (*(kls_itr++) != fsrc->src))
322                 ;
323           }
324         else
325           {
326              kls_itr++;
327           }
328
329         cur->kls_itr = kls_itr;
330         return *kls_itr;
331      }
332    else
333      {
334         return NULL;
335      }
336 }
337
338 static inline Eina_Bool
339 _eo_kls_itr_reached_end(const Eo_Kls_Itr *cur)
340 {
341    const Eo_Class **kls_itr = cur->kls_itr;
342    return !(*kls_itr && *(kls_itr + 1));
343 }
344
345 static inline const op_type_funcs *
346 _eo_kls_itr_func_get(const Eo_Class *klass, Eo_Kls_Itr *mro_itr, Eo_Op op, Eo_Kls_Itr *prev_state)
347 {
348    _eo_kls_itr_init(klass, mro_itr, op, prev_state);
349    klass = _eo_kls_itr_get(mro_itr);
350    if (klass)
351      {
352         const op_type_funcs *func = _dich_func_get(klass, op);
353
354         if (func && func->func)
355           {
356              return func;
357           }
358      }
359
360    return NULL;
361 }
362
363 #define _EO_OP_ERR_NO_OP_PRINT(op, klass) \
364    do \
365       { \
366          const Eo_Class *op_klass = OP_CLASS_GET(op); \
367          const char *_dom_name = (op_klass) ? op_klass->desc->name : NULL; \
368          ERR("Can't find func for op %x ('%s' of domain '%s') for class '%s'. Aborting.", \
369                op, _eo_op_id_name_get(op), _dom_name, \
370                (klass) ? klass->desc->name : NULL); \
371       } \
372    while (0)
373
374 static Eina_Bool
375 _eo_op_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, va_list *p_list)
376 {
377    Eina_Bool ret = EINA_FALSE;
378
379 #ifndef NDEBUG
380    const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
381
382    if (op_desc)
383      {
384         if (op_desc->op_type == EO_OP_TYPE_CLASS)
385           {
386              ERR("Tried calling a class op '%s' (%d) from a non-class context.", (op_desc) ? op_desc->name : NULL, op);
387              return EINA_FALSE;
388           }
389         else if ((op_type == EO_OP_TYPE_CONST) &&
390               (op_desc->op_type != EO_OP_TYPE_CONST))
391           {
392              ERR("Tried calling non-const or non-existant op '%s' (%d) from a const (query) function.", (op_desc) ? op_desc->name : NULL, op);
393              return EINA_FALSE;
394           }
395      }
396 #endif
397
398    Eo_Kls_Itr prev_state;
399
400      {
401         const op_type_funcs *func =
402            _eo_kls_itr_func_get(obj->klass, &obj->mro_itr, op, &prev_state);
403         if (func)
404           {
405              func->func(obj, _eo_data_get(obj, func->src), p_list);
406              ret = EINA_TRUE;
407              goto end;
408           }
409      }
410
411    /* Try composite objects */
412      {
413         Eina_List *itr;
414         Eo *emb_obj;
415         EINA_LIST_FOREACH(obj->composite_objects, itr, emb_obj)
416           {
417              if (_eo_op_internal(emb_obj, op_type, op, p_list))
418                {
419                   ret = EINA_TRUE;
420                   goto end;
421                }
422           }
423      }
424
425 end:
426    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
427    return ret;
428 }
429
430 EAPI Eina_Bool
431 eo_do_internal(Eo *obj, Eo_Op_Type op_type, ...)
432 {
433    Eina_Bool ret = EINA_TRUE;
434    Eo_Op op = EO_NOOP;
435    va_list p_list;
436
437    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
438
439    _eo_ref(obj);
440
441    va_start(p_list, op_type);
442
443    op = va_arg(p_list, Eo_Op);
444    while (op)
445      {
446         if (!_eo_op_internal(obj, op_type, op, &p_list))
447           {
448              _EO_OP_ERR_NO_OP_PRINT(op, obj->klass);
449              ret = EINA_FALSE;
450              break;
451           }
452         op = va_arg(p_list, Eo_Op);
453      }
454
455    va_end(p_list);
456
457    _eo_unref(obj);
458    return ret;
459 }
460
461 EAPI Eina_Bool
462 eo_do_super_internal(Eo *obj, Eo_Op_Type op_type, Eo_Op op, ...)
463 {
464    const Eo_Class *nklass;
465    Eina_Bool ret = EINA_TRUE;
466    va_list p_list;
467    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
468
469    /* Advance the kls itr. */
470    nklass = _eo_kls_itr_next(&obj->mro_itr, op);
471
472    if (obj->mro_itr.op != op)
473       return EINA_FALSE;
474
475    va_start(p_list, op);
476    if (!_eo_op_internal(obj, op_type, op, &p_list))
477      {
478         _EO_OP_ERR_NO_OP_PRINT(op, nklass);
479         ret = EINA_FALSE;
480      }
481    va_end(p_list);
482
483    return ret;
484 }
485
486 static Eina_Bool
487 _eo_class_op_internal(Eo_Class *klass, Eo_Op op, va_list *p_list)
488 {
489    Eina_Bool ret = EINA_FALSE;
490
491 #ifndef NDEBUG
492    const Eo_Op_Description *op_desc = _eo_op_id_desc_get(op);
493
494    if (op_desc)
495      {
496         if (op_desc->op_type != EO_OP_TYPE_CLASS)
497           {
498              ERR("Tried calling an instant op '%s' (%d) from a class context.", (op_desc) ? op_desc->name : NULL, op);
499              return EINA_FALSE;
500           }
501      }
502 #endif
503
504    Eo_Kls_Itr prev_state;
505
506      {
507         const op_type_funcs *func =
508            _eo_kls_itr_func_get(klass, &klass->mro_itr, op, &prev_state);
509         if (func)
510           {
511              ((eo_op_func_type_class) func->func)(klass, p_list);
512              ret = EINA_TRUE;
513              goto end;
514           }
515      }
516
517 end:
518    _eo_kls_itr_end(&klass->mro_itr, &prev_state);
519    return ret;
520 }
521
522 EAPI Eina_Bool
523 eo_class_do_internal(const Eo_Class *klass, ...)
524 {
525    Eina_Bool ret = EINA_TRUE;
526    Eo_Op op = EO_NOOP;
527    va_list p_list;
528
529    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
530
531    va_start(p_list, klass);
532
533    op = va_arg(p_list, Eo_Op);
534    while (op)
535      {
536         if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
537           {
538              _EO_OP_ERR_NO_OP_PRINT(op, klass);
539              ret = EINA_FALSE;
540              break;
541           }
542         op = va_arg(p_list, Eo_Op);
543      }
544
545    va_end(p_list);
546
547    return ret;
548 }
549
550 EAPI Eina_Bool
551 eo_class_do_super_internal(const Eo_Class *klass, Eo_Op op, ...)
552 {
553    const Eo_Class *nklass;
554    Eina_Bool ret = EINA_TRUE;
555    va_list p_list;
556    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, EINA_FALSE);
557
558    /* Advance the kls itr. */
559    nklass = _eo_kls_itr_next(&((Eo_Class *) klass)->mro_itr, op);
560
561    if (klass->mro_itr.op != op)
562       return EINA_FALSE;
563
564    va_start(p_list, op);
565    if (!_eo_class_op_internal((Eo_Class *) klass, op, &p_list))
566      {
567         _EO_OP_ERR_NO_OP_PRINT(op, nklass);
568         ret = EINA_FALSE;
569      }
570    va_end(p_list);
571
572    return ret;
573 }
574
575 EAPI const Eo_Class *
576 eo_class_get(const Eo *obj)
577 {
578    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
579
580    return obj->klass;
581 }
582
583 EAPI const char *
584 eo_class_name_get(const Eo_Class *klass)
585 {
586    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
587
588    return klass->desc->name;
589 }
590
591 static void
592 _eo_class_base_op_init(Eo_Class *klass)
593 {
594    const Eo_Class_Description *desc = klass->desc;
595    if (!desc || !desc->ops.base_op_id)
596       return;
597
598    *(desc->ops.base_op_id) = EO_CLASS_ID_TO_BASE_ID(klass->class_id);
599 }
600
601 #ifndef NDEBUG
602 static Eina_Bool
603 _eo_class_mro_has(const Eo_Class *klass, const Eo_Class *find)
604 {
605    const Eo_Class **itr;
606    for (itr = klass->mro ; *itr ; itr++)
607      {
608         if (*itr == find)
609           {
610              return EINA_TRUE;
611           }
612      }
613
614    return EINA_FALSE;
615 }
616 #endif
617
618 static Eina_List *
619 _eo_class_mro_add(Eina_List *mro, const Eo_Class *klass)
620 {
621    Eina_List *extn_pos = NULL;
622    Eina_Bool check_consistency = !mro;
623    if (!klass)
624       return mro;
625
626    mro = eina_list_append(mro, klass);
627
628    /* Recursively add extenions. */
629      {
630         Eo_Extension_Node *extn;
631         EINA_INLIST_FOREACH(klass->extensions, extn)
632           {
633              mro = _eo_class_mro_add(mro, extn->klass);
634              /* Not possible: if (!mro) return NULL; */
635
636              if (check_consistency)
637                {
638                   extn_pos = eina_list_append(extn_pos, eina_list_last(mro));
639                }
640           }
641      }
642
643    /* Check if we can create a consistent mro. We only do it for the class
644     * we are working on (i.e no parents). */
645    if (check_consistency)
646      {
647         Eo_Extension_Node *extn;
648
649         Eina_List *itr = extn_pos;
650         EINA_INLIST_FOREACH(klass->extensions, extn)
651           {
652              /* Get the first one after the extension. */
653              Eina_List *extn_list = eina_list_next(eina_list_data_get(itr));
654
655              /* If we found the extension again. */
656              if (eina_list_data_find_list(extn_list, extn->klass))
657                {
658                   eina_list_free(mro);
659                   ERR("Cannot create a consistent method resolution order for class '%s' because of '%s'.", klass->desc->name, extn->klass->desc->name);
660                   return NULL;
661                }
662
663              itr = eina_list_next(itr);
664           }
665      }
666
667
668    mro = _eo_class_mro_add(mro, klass->parent);
669
670    return mro;
671 }
672
673 static Eina_Bool
674 _eo_class_mro_init(Eo_Class *klass)
675 {
676    Eina_List *mro = NULL;
677
678    DBG("Started creating MRO for class '%s'", klass->desc->name);
679    mro = _eo_class_mro_add(mro, klass);
680
681    if (!mro)
682       return EINA_FALSE;
683
684    /* Remove duplicates and make them the right order. */
685      {
686         Eina_List *itr1, *itr2, *itr2n;
687
688         itr1 = eina_list_last(mro);
689         while (itr1)
690           {
691              itr2 = eina_list_prev(itr1);
692
693              while (itr2)
694                {
695                   itr2n = eina_list_prev(itr2);
696
697                   if (eina_list_data_get(itr1) == eina_list_data_get(itr2))
698                     {
699                        mro = eina_list_remove_list(mro, itr2);
700                     }
701
702                   itr2 = itr2n;
703                }
704
705              itr1 = eina_list_prev(itr1);
706           }
707      }
708
709    /* Copy the mro and free the list. */
710      {
711         const Eo_Class *kls_itr;
712         const Eo_Class **mro_itr;
713         klass->mro = calloc(sizeof(*klass->mro), eina_list_count(mro) + 1);
714
715         mro_itr = klass->mro;
716
717         EINA_LIST_FREE(mro, kls_itr)
718           {
719              *(mro_itr++) = kls_itr;
720
721              DBG("Added '%s' to MRO", kls_itr->desc->name);
722           }
723         *(mro_itr) = NULL;
724      }
725
726    DBG("Finished creating MRO for class '%s'", klass->desc->name);
727
728    return EINA_TRUE;
729 }
730
731 static void
732 _eo_class_constructor(Eo_Class *klass)
733 {
734    if (klass->constructed)
735       return;
736
737    klass->constructed = EINA_TRUE;
738
739    if (klass->desc->class_constructor)
740       klass->desc->class_constructor(klass);
741 }
742
743 EAPI void
744 eo_class_funcs_set(Eo_Class *klass, const Eo_Op_Func_Description *func_descs)
745 {
746    EO_MAGIC_RETURN(klass, EO_CLASS_EINA_MAGIC);
747
748    const Eo_Op_Func_Description *itr;
749    itr = func_descs;
750    if (itr)
751      {
752         for ( ; itr->op != 0 ; itr++)
753           {
754              const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
755
756              if (EINA_LIKELY(!op_desc || (itr->op_type == op_desc->op_type)))
757                {
758                   _dich_func_set(klass, itr->op, itr->func);
759                }
760              else
761                {
762                   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);
763                }
764           }
765      }
766 }
767
768 static void
769 eo_class_free(Eo_Class *klass)
770 {
771    if (klass->constructed)
772      {
773         if (klass->desc->class_destructor)
774            klass->desc->class_destructor(klass);
775
776         _dich_func_clean_all(klass);
777      }
778
779      {
780         Eina_Inlist *itrn;
781         Eo_Extension_Node *extn = NULL;
782         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
783           {
784              free(extn);
785           }
786      }
787
788    if (klass->mro)
789       free(klass->mro);
790
791    if (klass->extn_data_off)
792       free(klass->extn_data_off);
793
794    free(klass);
795 }
796
797 /* DEVCHECK */
798 static Eina_Bool
799 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
800 {
801    const Eo_Class_Description *desc = klass->desc;
802    const Eo_Op_Description *itr;
803    size_t i;
804
805    if (desc->ops.count > 0)
806      {
807         if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
808           {
809              ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
810                    desc->name);
811              return EINA_FALSE;
812           }
813
814         if (!desc->ops.descs)
815           {
816              ERR("Class '%s' has a non-zero ops count, but there are no descs.",
817                    desc->name);
818              return EINA_FALSE;
819           }
820      }
821
822    itr = desc->ops.descs;
823    for (i = 0 ; i < desc->ops.count ; i++, itr++)
824      {
825         if (itr->sub_op != i)
826           {
827              if (itr->name)
828                {
829                   ERR("Wrong order in Ops description for class '%s'. Expected %d and got %d", desc->name, i, itr->sub_op);
830                }
831              else
832                {
833                   ERR("Found too few Ops description for class '%s'. Expected %d descriptions, but found %d.", desc->name, desc->ops.count, i);
834                }
835              return EINA_FALSE;
836           }
837      }
838
839    if (itr && itr->name)
840      {
841         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
842         return EINA_FALSE;
843      }
844
845    return EINA_TRUE;
846 }
847
848 EAPI const Eo_Class *
849 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
850 {
851    Eo_Class *klass;
852    va_list p_list;
853
854    if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
855      {
856         EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
857         return NULL;
858      }
859
860    if (id > EO_STATIC_IDS_LAST)
861      {
862         ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
863         return NULL;
864      }
865
866    va_start(p_list, parent);
867
868    EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
869    EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
870
871    /* Check restrictions on Interface types. */
872    if (desc->type == EO_CLASS_TYPE_INTERFACE)
873      {
874         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
875         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
876         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
877         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
878         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
879      }
880
881    klass = calloc(1, sizeof(Eo_Class));
882    klass->parent = parent;
883
884    /* Handle class extensions */
885      {
886         Eo_Class *extn = NULL;
887
888         extn = va_arg(p_list, Eo_Class *);
889         while (extn)
890           {
891              switch (extn->desc->type)
892                {
893                 case EO_CLASS_TYPE_REGULAR:
894                 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
895                    /* Use it like an interface. */
896                 case EO_CLASS_TYPE_INTERFACE:
897                    break;
898                 case EO_CLASS_TYPE_MIXIN:
899                      {
900                         Eo_Extension_Node *node = calloc(1, sizeof(*node));
901                         node->klass = extn;
902                         klass->extensions =
903                            eina_inlist_append(klass->extensions,
904                                  EINA_INLIST_GET(node));
905                      }
906                    break;
907                }
908
909              extn = va_arg(p_list, Eo_Class *);
910           }
911      }
912
913    klass->desc = desc;
914
915    /* Handle the inheritance */
916    if (klass->parent)
917      {
918         /* Verify the inheritance is allowed. */
919         switch (klass->desc->type)
920           {
921            case EO_CLASS_TYPE_REGULAR:
922            case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
923               if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
924                     (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
925                 {
926                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
927                    goto cleanup;
928                 }
929               break;
930            case EO_CLASS_TYPE_INTERFACE:
931            case EO_CLASS_TYPE_MIXIN:
932               if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
933                     (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
934                 {
935                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
936                    goto cleanup;
937                 }
938               break;
939           }
940
941
942         /* Update the current offset. */
943         /* FIXME: Make sure this alignment is enough. */
944         klass->data_offset = klass->parent->data_offset +
945            EO_ALIGN_SIZE(klass->parent->desc->data_size);
946      }
947
948    if (!_eo_class_check_op_descs(klass, id))
949      {
950         goto cleanup;
951      }
952
953    if (!_eo_class_mro_init(klass))
954      {
955         goto cleanup;
956      }
957
958    /* create MIXIN offset table. */
959      {
960         const Eo_Class **mro_itr = klass->mro;
961         Eo_Extension_Data_Offset *extn_data_itr;
962         size_t extn_num = 0;
963         size_t extn_data_off = klass->data_offset +
964            EO_ALIGN_SIZE(klass->desc->data_size);
965
966         /* FIXME: Make faster... */
967         while (*mro_itr)
968           {
969              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
970                    ((*mro_itr)->desc->data_size > 0))
971                {
972                   extn_num++;
973                }
974              mro_itr++;
975           }
976
977         klass->extn_data_off = calloc(extn_num + 1,
978               sizeof(*klass->extn_data_off));
979
980         extn_data_itr = klass->extn_data_off;
981         mro_itr = klass->mro;
982         while (*mro_itr)
983           {
984              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
985                    ((*mro_itr)->desc->data_size > 0))
986                {
987                   extn_data_itr->klass = *mro_itr;
988                   extn_data_itr->offset = extn_data_off;
989
990                   extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
991                   extn_data_itr++;
992                }
993              mro_itr++;
994           }
995
996         klass->extn_data_size = extn_data_off;
997      }
998
999    eina_lock_take(&_eo_class_creation_lock);
1000
1001    if (id == 0)
1002      {
1003         klass->class_id = ++_eo_classes_last_id;
1004      }
1005    else
1006      {
1007 #ifndef NDEBUG
1008         if (_eo_classes && _eo_classes[id - 1])
1009           {
1010              ERR("A class with id %d was already defined (%s). Aborting.", id,
1011                    _eo_classes[id - 1]->desc->name);
1012              eina_lock_release(&_eo_class_creation_lock);
1013              goto cleanup;
1014           }
1015 #endif
1016         klass->class_id = id;
1017      }
1018
1019
1020      {
1021         /* FIXME: Handle errors. */
1022         size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1023         Eo_Class **tmp;
1024         tmp = realloc(_eo_classes, arrsize);
1025
1026         /* If it's the first allocation, memset. */
1027         if (!_eo_classes)
1028            memset(tmp, 0, arrsize);
1029
1030         _eo_classes = tmp;
1031         _eo_classes[klass->class_id - 1] = klass;
1032      }
1033    eina_lock_release(&_eo_class_creation_lock);
1034
1035    EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1036
1037    /* Flatten the function array */
1038      {
1039         const Eo_Class **mro_itr = klass->mro;
1040         for (  ; *mro_itr ; mro_itr++)
1041            ;
1042
1043         /* Skip ourselves. */
1044         for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1045           {
1046              _dich_copy_all(klass, *mro_itr);
1047           }
1048      }
1049
1050    _eo_class_base_op_init(klass);
1051
1052    _eo_class_constructor(klass);
1053
1054    va_end(p_list);
1055
1056    return klass;
1057
1058 cleanup:
1059    eo_class_free(klass);
1060    return NULL;
1061 }
1062
1063 EAPI Eo *
1064 eo_add(const Eo_Class *klass, Eo *parent)
1065 {
1066    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1067
1068    if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1069
1070    if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1071      {
1072         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1073         return NULL;
1074      }
1075
1076    Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1077          (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1078          klass->extn_data_size);
1079    obj->klass = klass;
1080    obj->parent = parent;
1081
1082    obj->refcount++;
1083
1084    Eo_Kls_Itr prev_state;
1085
1086    _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1087    _eo_error_unset(obj);
1088
1089    EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
1090    _eo_ref(obj);
1091    _eo_constructor(obj, klass);
1092
1093    if (EINA_UNLIKELY(_eo_error_get(obj)))
1094      {
1095         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
1096         goto fail;
1097      }
1098
1099    if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(&obj->mro_itr)))
1100      {
1101         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1102         goto fail;
1103      }
1104    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1105    _eo_unref(obj);
1106
1107    return obj;
1108
1109 fail:
1110    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1111    /* Unref twice, once for the ref above, and once for the basic object ref. */
1112    _eo_unref(obj);
1113    _eo_unref(obj);
1114    return NULL;
1115 }
1116
1117 typedef struct
1118 {
1119    EINA_INLIST;
1120    const Eo *ref_obj;
1121    const char *file;
1122    int line;
1123 } Eo_Xref_Node;
1124
1125 EAPI Eo *
1126 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1127 {
1128    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1129
1130    _eo_ref(obj);
1131
1132 #ifndef NDEBUG
1133    Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1134    xref->ref_obj = ref_obj;
1135    xref->file = file;
1136    xref->line = line;
1137
1138    /* FIXME: Make it sorted. */
1139    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1140 #else
1141    (void) ref_obj;
1142    (void) file;
1143    (void) line;
1144 #endif
1145
1146    return obj;
1147 }
1148
1149 EAPI void
1150 eo_xunref(Eo *obj, const Eo *ref_obj)
1151 {
1152    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1153 #ifndef NDEBUG
1154    Eo_Xref_Node *xref = NULL;
1155    EINA_INLIST_FOREACH(obj->xrefs, xref)
1156      {
1157         if (xref->ref_obj == ref_obj)
1158            break;
1159      }
1160
1161    if (xref)
1162      {
1163         obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1164         free(xref);
1165      }
1166    else
1167      {
1168         ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1169         return;
1170      }
1171 #else
1172    (void) ref_obj;
1173 #endif
1174    _eo_unref(obj);
1175 }
1176
1177 static inline Eo *
1178 _eo_ref(Eo *obj)
1179 {
1180    obj->refcount++;
1181    return obj;
1182 }
1183
1184 EAPI Eo *
1185 eo_ref(const Eo *_obj)
1186 {
1187    Eo *obj = (Eo *) _obj;
1188    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1189
1190    return _eo_ref(obj);
1191 }
1192
1193 static inline void
1194 _eo_del_internal(Eo *obj)
1195 {
1196    /* We need that for the event callbacks that may ref/unref. */
1197    obj->refcount++;
1198
1199    eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1200
1201    const Eo_Class *klass = eo_class_get(obj);
1202    Eo_Kls_Itr prev_state;
1203
1204    _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1205    _eo_error_unset(obj);
1206    _eo_destructor(obj, klass);
1207    if (_eo_error_get(obj))
1208      {
1209         ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1210      }
1211
1212    if (!_eo_kls_itr_reached_end(&obj->mro_itr))
1213      {
1214         ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1215      }
1216    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1217    /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1218
1219    Eina_List *itr, *itr_n;
1220    Eo *emb_obj;
1221    EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1222      {
1223         eo_composite_object_detach(obj, emb_obj);
1224      }
1225
1226    obj->del = EINA_TRUE;
1227    obj->refcount--;
1228 }
1229
1230 static inline void
1231 _eo_free(Eo *obj)
1232 {
1233    EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1234    free(obj);
1235 }
1236
1237 static inline void
1238 _eo_unref(Eo *obj)
1239 {
1240    if (--(obj->refcount) == 0)
1241      {
1242         _eo_del_internal(obj);
1243
1244 #ifndef NDEBUG
1245         /* If for some reason it's not empty, clear it. */
1246         while (obj->xrefs)
1247           {
1248              WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1249              Eina_Inlist *nitr = obj->xrefs->next;
1250              free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1251              obj->xrefs = nitr;
1252           }
1253 #endif
1254
1255         if (!obj->manual_free) _eo_free(obj);
1256      }
1257 }
1258
1259 EAPI void
1260 eo_unref(const Eo *_obj)
1261 {
1262    Eo *obj = (Eo *) _obj;
1263    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1264
1265    _eo_unref(obj);
1266 }
1267
1268 EAPI int
1269 eo_ref_get(const Eo *obj)
1270 {
1271    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1272
1273    return obj->refcount;
1274 }
1275
1276 EAPI Eo *
1277 eo_parent_get(const Eo *obj)
1278 {
1279    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1280
1281    return obj->parent;
1282 }
1283
1284 EAPI void
1285 eo_error_set_internal(const Eo *obj, const char *file, int line)
1286 {
1287    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1288
1289    ERR("Error with obj '%p' at %s:%d", obj, file, line);
1290
1291    ((Eo *) obj)->construct_error = EINA_TRUE;
1292 }
1293
1294 static inline void
1295 _eo_error_unset(Eo *obj)
1296 {
1297    obj->construct_error = EINA_FALSE;
1298 }
1299
1300 /**
1301  * @internal
1302  * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1303  * @param obj the object to work on.
1304  * @return @c EINA_TRUE if there was an error.
1305  */
1306 static inline Eina_Bool
1307 _eo_error_get(const Eo *obj)
1308 {
1309    return obj->construct_error;
1310 }
1311
1312 static inline void
1313 _eo_constructor_default(Eo *obj)
1314 {
1315    eo_constructor_super(obj);
1316 }
1317
1318 static inline void
1319 _eo_destructor_default(Eo *obj)
1320 {
1321    eo_destructor_super(obj);
1322 }
1323
1324 static void
1325 _eo_constructor(Eo *obj, const Eo_Class *klass)
1326 {
1327    if (!klass)
1328       return;
1329
1330    if (klass->desc->constructor)
1331       klass->desc->constructor(obj, _eo_data_get(obj, klass));
1332    else
1333       _eo_constructor_default(obj);
1334 }
1335
1336 static void
1337 _eo_destructor(Eo *obj, const Eo_Class *klass)
1338 {
1339    if (!klass)
1340       return;
1341
1342    if (klass->desc->destructor)
1343       klass->desc->destructor(obj, _eo_data_get(obj, klass));
1344    else
1345       _eo_destructor_default(obj);
1346 }
1347
1348 EAPI void
1349 eo_constructor_super(Eo *obj)
1350 {
1351    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1352
1353    _eo_constructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1354 }
1355
1356 EAPI void
1357 eo_destructor_super(Eo *obj)
1358 {
1359    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1360
1361    _eo_destructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1362 }
1363
1364 static inline void *
1365 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1366 {
1367    if (EINA_LIKELY(klass->desc->data_size > 0))
1368      {
1369         if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1370           {
1371              Eo_Extension_Data_Offset *doff_itr =
1372                 eo_class_get(obj)->extn_data_off;
1373
1374              if (!doff_itr)
1375                 return NULL;
1376
1377              while (doff_itr->klass)
1378                {
1379                   if (doff_itr->klass == klass)
1380                      return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1381                            doff_itr->offset;
1382                   doff_itr++;
1383                }
1384           }
1385         else
1386           {
1387           return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1388              klass->data_offset;
1389           }
1390      }
1391
1392    return NULL;
1393 }
1394
1395 EAPI void *
1396 eo_data_get(const Eo *obj, const Eo_Class *klass)
1397 {
1398    void *ret;
1399    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1400
1401 #ifndef NDEBUG
1402    if (!_eo_class_mro_has(obj->klass, klass))
1403      {
1404         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);
1405         return NULL;
1406      }
1407 #endif
1408
1409    ret = _eo_data_get(obj, klass);
1410
1411 #ifndef NDEBUG
1412    if (!ret && (klass->desc->data_size == 0))
1413      {
1414         ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1415      }
1416 #endif
1417
1418    return ret;
1419 }
1420
1421 EAPI Eina_Bool
1422 eo_init(void)
1423 {
1424    const char *log_dom = "eo";
1425    if (_eo_init_count++ > 0)
1426       return EINA_TRUE;
1427
1428    eina_init();
1429
1430    _eo_classes = NULL;
1431    _eo_classes_last_id = EO_STATIC_IDS_LAST;
1432    _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1433    if (_eo_log_dom < 0)
1434      {
1435         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1436         return EINA_FALSE;
1437      }
1438
1439    if (!eina_lock_new(&_eo_class_creation_lock))
1440      {
1441         EINA_LOG_ERR("Could not init lock.");
1442         return EINA_FALSE;
1443      }
1444
1445    eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1446    eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1447          EO_FREED_EINA_MAGIC_STR);
1448    eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1449          EO_CLASS_EINA_MAGIC_STR);
1450
1451    return EINA_TRUE;
1452 }
1453
1454 EAPI Eina_Bool
1455 eo_shutdown(void)
1456 {
1457    size_t i;
1458    Eo_Class **cls_itr = _eo_classes;
1459
1460    if (--_eo_init_count > 0)
1461       return EINA_TRUE;
1462
1463    for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1464      {
1465         if (*cls_itr)
1466            eo_class_free(*cls_itr);
1467      }
1468
1469    if (_eo_classes)
1470       free(_eo_classes);
1471
1472    eina_lock_free(&_eo_class_creation_lock);
1473
1474    eina_log_domain_unregister(_eo_log_dom);
1475    _eo_log_dom = -1;
1476
1477    eina_shutdown();
1478    return EINA_TRUE;
1479 }
1480
1481 EAPI void
1482 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1483 {
1484    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1485    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1486
1487    eo_xref(emb_obj, obj);
1488    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1489 }
1490
1491 EAPI void
1492 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1493 {
1494    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1495    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1496
1497    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1498    eo_xunref(emb_obj, obj);
1499 }
1500
1501 EAPI Eina_Bool
1502 eo_composite_is(const Eo *emb_obj)
1503 {
1504    if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1505      {
1506         EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1507         return EINA_FALSE;
1508      }
1509
1510    Eo *obj = eo_parent_get(emb_obj);
1511    Eina_List *itr;
1512    Eo *tmp;
1513
1514    if (!obj)
1515       return EINA_FALSE;
1516
1517    EINA_LIST_FOREACH(obj->composite_objects, itr, tmp)
1518      {
1519         if (tmp == emb_obj)
1520            return EINA_TRUE;
1521      }
1522
1523    return EINA_FALSE;
1524 }
1525
1526 EAPI void
1527 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1528 {
1529    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1530    obj->manual_free = manual_free;
1531 }
1532
1533 EAPI void
1534 eo_manual_free(Eo *obj)
1535 {
1536    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1537
1538    if (EINA_FALSE == obj->manual_free)
1539      {
1540         ERR("Tried manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1541         return;
1542      }
1543
1544    if (0 != obj->refcount)
1545      {
1546         ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));
1547         return;
1548      }
1549
1550    _eo_free(obj);
1551 }
1552