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