Eo: Added a check if object is already deleted in eo_unref.
[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 %x ('%s') while expecting: %x ('%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' (%x) 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' (%x) 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 instance op '%s' (%x) 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_type != EO_OP_TYPE_INVALID ; itr++)
756           {
757              const Eo_Op_Description *op_desc = _eo_op_id_desc_get(itr->op);
758
759              if (EINA_UNLIKELY(!op_desc || (itr->op == EO_NOOP)))
760                {
761                   ERR("Setting implementation for non-existent op %x for class '%s'. Func index: %d", itr->op, klass->desc->name, itr - func_descs);
762                }
763              else if (EINA_LIKELY(itr->op_type == op_desc->op_type))
764                {
765                   _dich_func_set(klass, itr->op, itr->func);
766                }
767              else
768                {
769                   ERR("Set function's op type (%x) is different than the one in the op description (%d) for op '%s' in class '%s'. Func index: %d",
770                         itr->op_type,
771                         (op_desc) ? op_desc->op_type : EO_OP_TYPE_REGULAR,
772                         (op_desc) ? op_desc->name : NULL,
773                         klass->desc->name,
774                         itr - func_descs);
775                }
776           }
777      }
778 }
779
780 static void
781 eo_class_free(Eo_Class *klass)
782 {
783    if (klass->constructed)
784      {
785         if (klass->desc->class_destructor)
786            klass->desc->class_destructor(klass);
787
788         _dich_func_clean_all(klass);
789      }
790
791      {
792         Eina_Inlist *itrn;
793         Eo_Extension_Node *extn = NULL;
794         EINA_INLIST_FOREACH_SAFE(klass->extensions, itrn, extn)
795           {
796              free(extn);
797           }
798      }
799
800    if (klass->mro)
801       free(klass->mro);
802
803    if (klass->extn_data_off)
804       free(klass->extn_data_off);
805
806    free(klass);
807 }
808
809 /* DEVCHECK */
810 static Eina_Bool
811 _eo_class_check_op_descs(const Eo_Class *klass, Eo_Class_Id id)
812 {
813    const Eo_Class_Description *desc = klass->desc;
814    const Eo_Op_Description *itr;
815    size_t i;
816
817    if (desc->ops.count > 0)
818      {
819         if (((id == 0) || (id > EO_STATIC_IDS_LAST)) && !desc->ops.base_op_id)
820           {
821              ERR("Class '%s' has a non-zero ops count, but base_id is NULL.",
822                    desc->name);
823              return EINA_FALSE;
824           }
825
826         if (!desc->ops.descs)
827           {
828              ERR("Class '%s' has a non-zero ops count, but there are no descs.",
829                    desc->name);
830              return EINA_FALSE;
831           }
832      }
833
834    itr = desc->ops.descs;
835    for (i = 0 ; i < desc->ops.count ; i++, itr++)
836      {
837         if (itr->sub_op != i)
838           {
839              if (itr->name)
840                {
841                   ERR("Wrong order in Ops description for class '%s'. Expected %x and got %x", desc->name, i, itr->sub_op);
842                }
843              else
844                {
845                   ERR("Found too few Ops description for class '%s'. Expected %x descriptions, but found %x.", desc->name, desc->ops.count, i);
846                }
847              return EINA_FALSE;
848           }
849      }
850
851    if (itr && itr->name)
852      {
853         ERR("Found extra Ops description for class '%s'. Expected %d descriptions, but found more.", desc->name, desc->ops.count);
854         return EINA_FALSE;
855      }
856
857    return EINA_TRUE;
858 }
859
860 EAPI const Eo_Class *
861 eo_class_new(const Eo_Class_Description *desc, Eo_Class_Id id, const Eo_Class *parent, ...)
862 {
863    Eo_Class *klass;
864    va_list p_list;
865
866    if (parent && !EINA_MAGIC_CHECK(parent, EO_CLASS_EINA_MAGIC))
867      {
868         EINA_MAGIC_FAIL(parent, EO_CLASS_EINA_MAGIC);
869         return NULL;
870      }
871
872    if (id > EO_STATIC_IDS_LAST)
873      {
874         ERR("Tried creating a class with the static id %d while the maximum static id is %d. Aborting.", id, EO_STATIC_IDS_LAST);
875         return NULL;
876      }
877
878    va_start(p_list, parent);
879
880    EINA_SAFETY_ON_NULL_RETURN_VAL(desc, NULL);
881    EINA_SAFETY_ON_NULL_RETURN_VAL(desc->name, NULL);
882
883    /* Check restrictions on Interface types. */
884    if (desc->type == EO_CLASS_TYPE_INTERFACE)
885      {
886         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->constructor, NULL);
887         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->destructor, NULL);
888         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_constructor, NULL);
889         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->class_destructor, NULL);
890         EINA_SAFETY_ON_FALSE_RETURN_VAL(!desc->data_size, NULL);
891      }
892
893    klass = calloc(1, sizeof(Eo_Class));
894    klass->parent = parent;
895
896    /* Handle class extensions */
897      {
898         Eo_Class *extn = NULL;
899
900         extn = va_arg(p_list, Eo_Class *);
901         while (extn)
902           {
903              switch (extn->desc->type)
904                {
905                 case EO_CLASS_TYPE_REGULAR:
906                 case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
907                    /* Use it like an interface. */
908                 case EO_CLASS_TYPE_INTERFACE:
909                    break;
910                 case EO_CLASS_TYPE_MIXIN:
911                      {
912                         Eo_Extension_Node *node = calloc(1, sizeof(*node));
913                         node->klass = extn;
914                         klass->extensions =
915                            eina_inlist_append(klass->extensions,
916                                  EINA_INLIST_GET(node));
917                      }
918                    break;
919                }
920
921              extn = va_arg(p_list, Eo_Class *);
922           }
923      }
924
925    klass->desc = desc;
926
927    /* Handle the inheritance */
928    if (klass->parent)
929      {
930         /* Verify the inheritance is allowed. */
931         switch (klass->desc->type)
932           {
933            case EO_CLASS_TYPE_REGULAR:
934            case EO_CLASS_TYPE_REGULAR_NO_INSTANT:
935               if ((klass->parent->desc->type != EO_CLASS_TYPE_REGULAR) &&
936                     (klass->parent->desc->type != EO_CLASS_TYPE_REGULAR_NO_INSTANT))
937                 {
938                    ERR("Regular classes ('%s') aren't allowed to inherit from non-regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
939                    goto cleanup;
940                 }
941               break;
942            case EO_CLASS_TYPE_INTERFACE:
943            case EO_CLASS_TYPE_MIXIN:
944               if ((klass->parent->desc->type != EO_CLASS_TYPE_INTERFACE) &&
945                     (klass->parent->desc->type != EO_CLASS_TYPE_MIXIN))
946                 {
947                    ERR("Non-regular classes ('%s') aren't allowed to inherit from regular classes ('%s').", klass->desc->name, klass->parent->desc->name);
948                    goto cleanup;
949                 }
950               break;
951           }
952
953
954         /* Update the current offset. */
955         /* FIXME: Make sure this alignment is enough. */
956         klass->data_offset = klass->parent->data_offset +
957            EO_ALIGN_SIZE(klass->parent->desc->data_size);
958      }
959
960    if (!_eo_class_check_op_descs(klass, id))
961      {
962         goto cleanup;
963      }
964
965    if (!_eo_class_mro_init(klass))
966      {
967         goto cleanup;
968      }
969
970    /* create MIXIN offset table. */
971      {
972         const Eo_Class **mro_itr = klass->mro;
973         Eo_Extension_Data_Offset *extn_data_itr;
974         size_t extn_num = 0;
975         size_t extn_data_off = klass->data_offset +
976            EO_ALIGN_SIZE(klass->desc->data_size);
977
978         /* FIXME: Make faster... */
979         while (*mro_itr)
980           {
981              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
982                    ((*mro_itr)->desc->data_size > 0))
983                {
984                   extn_num++;
985                }
986              mro_itr++;
987           }
988
989         klass->extn_data_off = calloc(extn_num + 1,
990               sizeof(*klass->extn_data_off));
991
992         extn_data_itr = klass->extn_data_off;
993         mro_itr = klass->mro;
994         while (*mro_itr)
995           {
996              if (((*mro_itr)->desc->type == EO_CLASS_TYPE_MIXIN) &&
997                    ((*mro_itr)->desc->data_size > 0))
998                {
999                   extn_data_itr->klass = *mro_itr;
1000                   extn_data_itr->offset = extn_data_off;
1001
1002                   extn_data_off += EO_ALIGN_SIZE(extn_data_itr->klass->desc->data_size);
1003                   extn_data_itr++;
1004                }
1005              mro_itr++;
1006           }
1007
1008         klass->extn_data_size = extn_data_off;
1009      }
1010
1011    eina_lock_take(&_eo_class_creation_lock);
1012
1013    if (id == 0)
1014      {
1015         klass->class_id = ++_eo_classes_last_id;
1016      }
1017    else
1018      {
1019 #ifndef NDEBUG
1020         if (_eo_classes && _eo_classes[id - 1])
1021           {
1022              ERR("A class with id %d was already defined (%s). Aborting.", id,
1023                    _eo_classes[id - 1]->desc->name);
1024              eina_lock_release(&_eo_class_creation_lock);
1025              goto cleanup;
1026           }
1027 #endif
1028         klass->class_id = id;
1029      }
1030
1031
1032      {
1033         /* FIXME: Handle errors. */
1034         size_t arrsize = _eo_classes_last_id * sizeof(*_eo_classes);
1035         Eo_Class **tmp;
1036         tmp = realloc(_eo_classes, arrsize);
1037
1038         /* If it's the first allocation, memset. */
1039         if (!_eo_classes)
1040            memset(tmp, 0, arrsize);
1041
1042         _eo_classes = tmp;
1043         _eo_classes[klass->class_id - 1] = klass;
1044      }
1045    eina_lock_release(&_eo_class_creation_lock);
1046
1047    EINA_MAGIC_SET(klass, EO_CLASS_EINA_MAGIC);
1048
1049    /* Flatten the function array */
1050      {
1051         const Eo_Class **mro_itr = klass->mro;
1052         for (  ; *mro_itr ; mro_itr++)
1053            ;
1054
1055         /* Skip ourselves. */
1056         for ( mro_itr-- ; mro_itr > klass->mro ; mro_itr--)
1057           {
1058              _dich_copy_all(klass, *mro_itr);
1059           }
1060      }
1061
1062    _eo_class_base_op_init(klass);
1063
1064    _eo_class_constructor(klass);
1065
1066    va_end(p_list);
1067
1068    return klass;
1069
1070 cleanup:
1071    eo_class_free(klass);
1072    return NULL;
1073 }
1074
1075 EAPI Eina_Bool
1076 eo_parent_set(Eo *obj, const Eo *parent)
1077 {
1078    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, EINA_FALSE);
1079    if (parent)
1080       EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, EINA_FALSE);
1081
1082    if (obj->parent == parent)
1083       return EINA_TRUE;
1084
1085    _eo_ref(obj);
1086
1087    if (eo_composite_is(obj))
1088      {
1089         eo_composite_object_detach(obj->parent, obj);
1090      }
1091
1092    if (obj->parent)
1093      {
1094         obj->parent->children =
1095            eina_inlist_remove(obj->parent->children, EINA_INLIST_GET(obj));
1096         eo_xunref(obj, obj->parent);
1097      }
1098
1099    obj->parent = (Eo *) parent;
1100    if (obj->parent)
1101      {
1102         obj->parent->children =
1103            eina_inlist_append(obj->parent->children, EINA_INLIST_GET(obj));
1104         eo_xref(obj, obj->parent);
1105      }
1106
1107    _eo_unref(obj);
1108
1109    return EINA_TRUE;
1110 }
1111
1112 EAPI Eo *
1113 eo_add(const Eo_Class *klass, Eo *parent)
1114 {
1115    EO_MAGIC_RETURN_VAL(klass, EO_CLASS_EINA_MAGIC, NULL);
1116
1117    if (parent) EO_MAGIC_RETURN_VAL(parent, EO_EINA_MAGIC, NULL);
1118
1119    if (EINA_UNLIKELY(klass->desc->type != EO_CLASS_TYPE_REGULAR))
1120      {
1121         ERR("Class '%s' is not instantiate-able. Aborting.", klass->desc->name);
1122         return NULL;
1123      }
1124
1125    Eo *obj = calloc(1, EO_ALIGN_SIZE(sizeof(*obj)) +
1126          (klass->data_offset + EO_ALIGN_SIZE(klass->desc->data_size)) +
1127          klass->extn_data_size);
1128    EINA_MAGIC_SET(obj, EO_EINA_MAGIC);
1129    obj->refcount++;
1130    obj->klass = klass;
1131
1132    eo_parent_set(obj, parent);
1133
1134    Eo_Kls_Itr prev_state;
1135
1136    _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1137    _eo_error_unset(obj);
1138
1139    _eo_ref(obj);
1140    _eo_constructor(obj, klass);
1141
1142    if (EINA_UNLIKELY(_eo_error_get(obj)))
1143      {
1144         ERR("Type '%s' - One of the object constructors have failed.", klass->desc->name);
1145         goto fail;
1146      }
1147
1148    if (EINA_UNLIKELY(!_eo_kls_itr_reached_end(&obj->mro_itr)))
1149      {
1150         ERR("Type '%s' - Not all of the object constructors have been executed.", klass->desc->name);
1151         goto fail;
1152      }
1153    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1154    _eo_unref(obj);
1155
1156    return obj;
1157
1158 fail:
1159    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1160    /* Unref twice, once for the ref above, and once for the basic object ref. */
1161    _eo_unref(obj);
1162    _eo_unref(obj);
1163    return NULL;
1164 }
1165
1166 typedef struct
1167 {
1168    EINA_INLIST;
1169    const Eo *ref_obj;
1170    const char *file;
1171    int line;
1172 } Eo_Xref_Node;
1173
1174 EAPI Eo *
1175 eo_xref_internal(Eo *obj, const Eo *ref_obj, const char *file, int line)
1176 {
1177    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1178
1179    _eo_ref(obj);
1180
1181 #ifndef NDEBUG
1182    Eo_Xref_Node *xref = calloc(1, sizeof(*xref));
1183    xref->ref_obj = ref_obj;
1184    xref->file = file;
1185    xref->line = line;
1186
1187    obj->xrefs = eina_inlist_prepend(obj->xrefs, EINA_INLIST_GET(xref));
1188 #else
1189    (void) ref_obj;
1190    (void) file;
1191    (void) line;
1192 #endif
1193
1194    return obj;
1195 }
1196
1197 EAPI void
1198 eo_xunref(Eo *obj, const Eo *ref_obj)
1199 {
1200    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1201 #ifndef NDEBUG
1202    Eo_Xref_Node *xref = NULL;
1203    EINA_INLIST_FOREACH(obj->xrefs, xref)
1204      {
1205         if (xref->ref_obj == ref_obj)
1206            break;
1207      }
1208
1209    if (xref)
1210      {
1211         obj->xrefs = eina_inlist_remove(obj->xrefs, EINA_INLIST_GET(xref));
1212         free(xref);
1213      }
1214    else
1215      {
1216         ERR("ref_obj (%p) does not reference obj (%p). Aborting unref.", ref_obj, obj);
1217         return;
1218      }
1219 #else
1220    (void) ref_obj;
1221 #endif
1222    _eo_unref(obj);
1223 }
1224
1225 static inline Eo *
1226 _eo_ref(Eo *obj)
1227 {
1228    obj->refcount++;
1229    return obj;
1230 }
1231
1232 EAPI Eo *
1233 eo_ref(const Eo *_obj)
1234 {
1235    Eo *obj = (Eo *) _obj;
1236    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, obj);
1237
1238    return _eo_ref(obj);
1239 }
1240
1241 static inline void
1242 _eo_del_internal(Eo *obj)
1243 {
1244    /* We need that for the event callbacks that may ref/unref. */
1245    obj->refcount++;
1246
1247    eo_do(obj, eo_event_callback_call(EO_EV_DEL, NULL, NULL));
1248
1249    const Eo_Class *klass = eo_class_get(obj);
1250    Eo_Kls_Itr prev_state;
1251
1252    _eo_kls_itr_init(klass, &obj->mro_itr, EO_NOOP, &prev_state);
1253    _eo_error_unset(obj);
1254    _eo_destructor(obj, klass);
1255    if (_eo_error_get(obj))
1256      {
1257         ERR("Type '%s' - One of the object destructors have failed.", klass->desc->name);
1258      }
1259
1260    if (!_eo_kls_itr_reached_end(&obj->mro_itr))
1261      {
1262         ERR("Type '%s' - Not all of the object destructors have been executed.", klass->desc->name);
1263      }
1264    _eo_kls_itr_end(&obj->mro_itr, &prev_state);
1265    /*FIXME: add eo_class_unref(klass) ? - just to clear the caches. */
1266
1267      {
1268         Eina_List *itr, *itr_n;
1269         Eo *emb_obj;
1270         EINA_LIST_FOREACH_SAFE(obj->composite_objects, itr, itr_n, emb_obj)
1271           {
1272              eo_composite_object_detach(obj, emb_obj);
1273           }
1274      }
1275
1276    while (obj->children)
1277      {
1278         eo_parent_set(EINA_INLIST_CONTAINER_GET(obj->children, Eo), NULL);
1279      }
1280
1281    obj->del = EINA_TRUE;
1282    obj->refcount--;
1283 }
1284
1285 static inline void
1286 _eo_free(Eo *obj)
1287 {
1288    EINA_MAGIC_SET(obj, EO_FREED_EINA_MAGIC);
1289    free(obj);
1290 }
1291
1292 static inline void
1293 _eo_unref(Eo *obj)
1294 {
1295    if (--(obj->refcount) == 0)
1296      {
1297         if (obj->del)
1298           {
1299              ERR("Object %p already deleted.", obj);
1300              return;
1301           }
1302
1303         _eo_del_internal(obj);
1304
1305 #ifndef NDEBUG
1306         /* If for some reason it's not empty, clear it. */
1307         while (obj->xrefs)
1308           {
1309              WRN("obj->xrefs is not empty, possibly a bug, please report. - An error will be reported for each xref in the stack.");
1310              Eina_Inlist *nitr = obj->xrefs->next;
1311              free(EINA_INLIST_CONTAINER_GET(obj->xrefs, Eo_Xref_Node));
1312              obj->xrefs = nitr;
1313           }
1314 #endif
1315
1316         if (!obj->manual_free)
1317            _eo_free(obj);
1318         else
1319            _eo_ref(obj); /* If we manual free, we keep a phantom ref. */
1320      }
1321 }
1322
1323 EAPI void
1324 eo_unref(const Eo *_obj)
1325 {
1326    Eo *obj = (Eo *) _obj;
1327    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1328
1329    _eo_unref(obj);
1330 }
1331
1332 EAPI void
1333 eo_del(const Eo *obj)
1334 {
1335    eo_parent_set((Eo *) obj, NULL);
1336    eo_unref(obj);
1337 }
1338
1339 EAPI int
1340 eo_ref_get(const Eo *obj)
1341 {
1342    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, 0);
1343
1344    return obj->refcount;
1345 }
1346
1347 EAPI Eo *
1348 eo_parent_get(const Eo *obj)
1349 {
1350    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1351
1352    return obj->parent;
1353 }
1354
1355 EAPI void
1356 eo_error_set_internal(const Eo *obj, const char *file, int line)
1357 {
1358    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1359
1360    ERR("Error with obj '%p' at %s:%d", obj, file, line);
1361
1362    ((Eo *) obj)->construct_error = EINA_TRUE;
1363 }
1364
1365 static inline void
1366 _eo_error_unset(Eo *obj)
1367 {
1368    obj->construct_error = EINA_FALSE;
1369 }
1370
1371 /**
1372  * @internal
1373  * @brief Check if there was an error when constructing, destructing or calling a function of the object.
1374  * @param obj the object to work on.
1375  * @return @c EINA_TRUE if there was an error.
1376  */
1377 static inline Eina_Bool
1378 _eo_error_get(const Eo *obj)
1379 {
1380    return obj->construct_error;
1381 }
1382
1383 static inline void
1384 _eo_constructor_default(Eo *obj)
1385 {
1386    eo_constructor_super(obj);
1387 }
1388
1389 static inline void
1390 _eo_destructor_default(Eo *obj)
1391 {
1392    eo_destructor_super(obj);
1393 }
1394
1395 static void
1396 _eo_constructor(Eo *obj, const Eo_Class *klass)
1397 {
1398    if (!klass)
1399       return;
1400
1401    if (klass->desc->constructor)
1402       klass->desc->constructor(obj, _eo_data_get(obj, klass));
1403    else
1404       _eo_constructor_default(obj);
1405 }
1406
1407 static void
1408 _eo_destructor(Eo *obj, const Eo_Class *klass)
1409 {
1410    if (!klass)
1411       return;
1412
1413    if (klass->desc->destructor)
1414       klass->desc->destructor(obj, _eo_data_get(obj, klass));
1415    else
1416       _eo_destructor_default(obj);
1417 }
1418
1419 EAPI void
1420 eo_constructor_super(Eo *obj)
1421 {
1422    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1423
1424    _eo_constructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1425 }
1426
1427 EAPI void
1428 eo_destructor_super(Eo *obj)
1429 {
1430    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1431
1432    _eo_destructor(obj, _eo_kls_itr_next(&obj->mro_itr, EO_NOOP));
1433 }
1434
1435 static inline void *
1436 _eo_data_get(const Eo *obj, const Eo_Class *klass)
1437 {
1438    if (EINA_LIKELY(klass->desc->data_size > 0))
1439      {
1440         if (EINA_UNLIKELY(klass->desc->type == EO_CLASS_TYPE_MIXIN))
1441           {
1442              Eo_Extension_Data_Offset *doff_itr =
1443                 eo_class_get(obj)->extn_data_off;
1444
1445              if (!doff_itr)
1446                 return NULL;
1447
1448              while (doff_itr->klass)
1449                {
1450                   if (doff_itr->klass == klass)
1451                      return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1452                            doff_itr->offset;
1453                   doff_itr++;
1454                }
1455           }
1456         else
1457           {
1458           return ((char *) obj) + EO_ALIGN_SIZE(sizeof(*obj)) +
1459              klass->data_offset;
1460           }
1461      }
1462
1463    return NULL;
1464 }
1465
1466 EAPI void *
1467 eo_data_get(const Eo *obj, const Eo_Class *klass)
1468 {
1469    void *ret;
1470    EO_MAGIC_RETURN_VAL(obj, EO_EINA_MAGIC, NULL);
1471
1472 #ifndef NDEBUG
1473    if (!_eo_class_mro_has(obj->klass, klass))
1474      {
1475         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);
1476         return NULL;
1477      }
1478 #endif
1479
1480    ret = _eo_data_get(obj, klass);
1481
1482 #ifndef NDEBUG
1483    if (!ret && (klass->desc->data_size == 0))
1484      {
1485         ERR("Tried getting data of class '%s', but it has none..", klass->desc->name);
1486      }
1487 #endif
1488
1489    return ret;
1490 }
1491
1492 EAPI Eina_Bool
1493 eo_init(void)
1494 {
1495    const char *log_dom = "eo";
1496    if (_eo_init_count++ > 0)
1497       return EINA_TRUE;
1498
1499    eina_init();
1500
1501    _eo_classes = NULL;
1502    _eo_classes_last_id = EO_STATIC_IDS_LAST;
1503    _eo_log_dom = eina_log_domain_register(log_dom, EINA_COLOR_LIGHTBLUE);
1504    if (_eo_log_dom < 0)
1505      {
1506         EINA_LOG_ERR("Could not register log domain: %s", log_dom);
1507         return EINA_FALSE;
1508      }
1509
1510    if (!eina_lock_new(&_eo_class_creation_lock))
1511      {
1512         EINA_LOG_ERR("Could not init lock.");
1513         return EINA_FALSE;
1514      }
1515
1516    eina_magic_string_static_set(EO_EINA_MAGIC, EO_EINA_MAGIC_STR);
1517    eina_magic_string_static_set(EO_FREED_EINA_MAGIC,
1518          EO_FREED_EINA_MAGIC_STR);
1519    eina_magic_string_static_set(EO_CLASS_EINA_MAGIC,
1520          EO_CLASS_EINA_MAGIC_STR);
1521
1522    return EINA_TRUE;
1523 }
1524
1525 EAPI Eina_Bool
1526 eo_shutdown(void)
1527 {
1528    size_t i;
1529    Eo_Class **cls_itr = _eo_classes;
1530
1531    if (--_eo_init_count > 0)
1532       return EINA_TRUE;
1533
1534    for (i = 0 ; i < _eo_classes_last_id ; i++, cls_itr++)
1535      {
1536         if (*cls_itr)
1537            eo_class_free(*cls_itr);
1538      }
1539
1540    if (_eo_classes)
1541       free(_eo_classes);
1542
1543    eina_lock_free(&_eo_class_creation_lock);
1544
1545    eina_log_domain_unregister(_eo_log_dom);
1546    _eo_log_dom = -1;
1547
1548    eina_shutdown();
1549    return EINA_TRUE;
1550 }
1551
1552 EAPI void
1553 eo_composite_object_attach(Eo *obj, Eo *emb_obj)
1554 {
1555    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1556    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1557
1558    emb_obj->composite = EINA_TRUE;
1559    eo_parent_set(emb_obj, obj);
1560    obj->composite_objects = eina_list_prepend(obj->composite_objects, emb_obj);
1561 }
1562
1563 EAPI void
1564 eo_composite_object_detach(Eo *obj, Eo *emb_obj)
1565 {
1566    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1567    EO_MAGIC_RETURN(emb_obj, EO_EINA_MAGIC);
1568
1569    emb_obj->composite = EINA_FALSE;
1570    obj->composite_objects = eina_list_remove(obj->composite_objects, emb_obj);
1571    eo_parent_set(emb_obj, NULL);
1572 }
1573
1574 EAPI Eina_Bool
1575 eo_composite_is(const Eo *emb_obj)
1576 {
1577    if (!EINA_MAGIC_CHECK(emb_obj, EO_EINA_MAGIC))
1578      {
1579         EINA_MAGIC_FAIL(emb_obj, EO_EINA_MAGIC);
1580         return EINA_FALSE;
1581      }
1582
1583    return emb_obj->composite;
1584 }
1585
1586 EAPI void
1587 eo_manual_free_set(Eo *obj, Eina_Bool manual_free)
1588 {
1589    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1590    obj->manual_free = manual_free;
1591 }
1592
1593 EAPI void
1594 eo_manual_free(Eo *obj)
1595 {
1596    EO_MAGIC_RETURN(obj, EO_EINA_MAGIC);
1597
1598    if (EINA_FALSE == obj->manual_free)
1599      {
1600         ERR("Tried to manually free the object %p while the option has not been set; see eo_manual_free_set for more information.", obj);
1601         return;
1602      }
1603
1604    if (!obj->del)
1605      {
1606         ERR("Tried deleting the object %p while still referenced(%d).", obj, eo_ref_get(obj));
1607         return;
1608      }
1609
1610    _eo_free(obj);
1611 }
1612