0431977589ac31513f32fa27c2ec213a40e08253
[framework/uifw/evas.git] / src / lib / main.c
1 #include "evas_common.h"
2 #include "evas_private.h"
3
4 static Evas_Version _version = { VMAJ, VMIN, VMIC, VREV };
5 EAPI Evas_Version *evas_version = &_version;
6
7 int _evas_alloc_error = 0;
8 static int _evas_debug_init = 0;
9 static enum {
10      _EVAS_DEBUG_DEFAULT,
11      _EVAS_DEBUG_HIDE,
12      _EVAS_DEBUG_SHOW
13 } _evas_debug_show = _EVAS_DEBUG_DEFAULT;
14 static int _evas_debug_abort = 0;
15
16 EAPI Evas_Alloc_Error
17 evas_alloc_error(void)
18 {
19    return _evas_alloc_error;
20 }
21
22 /* free cached items only in ram for speed reasons. return 0 if can't free */
23 int
24 evas_mem_free(int mem_required __UNUSED__)
25 {
26    return 0;
27 }
28
29 /* start reducing quality of images etc. return 0 if can't free anything */
30 int
31 evas_mem_degrade(int mem_required __UNUSED__)
32 {
33    return 0;
34 }
35
36 void *
37 evas_mem_calloc(int size)
38 {
39    void *ptr;
40
41    ptr = calloc(1, size);
42    if (ptr) return ptr;
43    MERR_BAD();
44    while ((!ptr) && (evas_mem_free(size))) ptr = calloc(1, size);
45    if (ptr) return ptr;
46    while ((!ptr) && (evas_mem_degrade(size))) ptr = calloc(1, size);
47    if (ptr) return ptr;
48    MERR_FATAL();
49    return NULL;
50 }
51
52 static void
53 _evas_debug_init_from_env()
54 {
55    const char *tmp = getenv("EVAS_DEBUG_SHOW");
56    if (tmp)
57      {
58         int dbgshow = atoi(tmp);
59         _evas_debug_show = (dbgshow) ? _EVAS_DEBUG_SHOW : _EVAS_DEBUG_HIDE;
60      }
61    if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
62    _evas_debug_init = 1;
63 }
64
65 void
66 evas_debug_error(void)
67 {
68    if (!_evas_debug_init)
69      {
70         _evas_debug_init_from_env();
71      }
72    if (_evas_debug_show == _EVAS_DEBUG_SHOW)
73      CRIT("Evas Magic Check Failed!!!");
74 }
75
76 void
77 evas_debug_input_null(void)
78 {
79    if (!_evas_debug_init)
80      {
81         _evas_debug_init_from_env();
82      }
83    if (_evas_debug_show == _EVAS_DEBUG_SHOW)
84      CRIT("Input object pointer is NULL!");
85    if (_evas_debug_abort) abort();
86 }
87
88 void
89 evas_debug_magic_null(void)
90 {
91    if (!_evas_debug_init)
92      {
93         _evas_debug_init_from_env();
94      }
95    if ((_evas_debug_show == _EVAS_DEBUG_SHOW) ||
96          (_evas_debug_show == _EVAS_DEBUG_DEFAULT))
97      CRIT("Input object is zero'ed out (maybe a freed object or zero-filled RAM)!");
98    if (_evas_debug_abort) abort();
99 }
100
101 void
102 evas_debug_magic_wrong(DATA32 expected, DATA32 supplied)
103 {
104    if (!_evas_debug_init)
105      {
106         _evas_debug_init_from_env();
107      }
108    if ((_evas_debug_show == _EVAS_DEBUG_SHOW) ||
109          (_evas_debug_show == _EVAS_DEBUG_DEFAULT))
110      CRIT("Input object is wrong type\n"
111           "    Expected: %08x - %s\n"
112           "    Supplied: %08x - %s",
113           expected, evas_debug_magic_string_get(expected),
114           supplied, evas_debug_magic_string_get(supplied));
115    if (_evas_debug_abort) abort();
116 }
117
118 void
119 evas_debug_generic(const char *str)
120 {
121    if (!_evas_debug_init)
122      {
123         _evas_debug_init_from_env();
124      }
125    if ((_evas_debug_show == _EVAS_DEBUG_SHOW) ||
126          (_evas_debug_show == _EVAS_DEBUG_DEFAULT))
127      CRIT("%s", str);
128    if (_evas_debug_abort) abort();
129 }
130
131 const char *
132 evas_debug_magic_string_get(DATA32 magic)
133 {
134    switch (magic)
135      {
136       case MAGIC_EVAS:
137         return "Evas";
138       case MAGIC_OBJ:
139         return "Evas_Object";
140       case MAGIC_OBJ_RECTANGLE:
141         return "Evas_Object (Rectangle)";
142       case MAGIC_OBJ_LINE:
143         return "Evas_Object (Line)";
144       case MAGIC_OBJ_POLYGON:
145         return "Evas_Object (Polygon)";
146       case MAGIC_OBJ_IMAGE:
147         return "Evas_Object (Image)";
148       case MAGIC_OBJ_TEXT:
149         return "Evas_Object (Text)";
150       case MAGIC_OBJ_SMART:
151         return "Evas_Object (Smart)";
152       case MAGIC_EVAS_GL:
153         return "Evas_GL";
154       case MAGIC_MAP:
155         return "Evas_Map";
156       default:
157         return "<UNKNOWN>";
158      };
159    return "<UNKNOWN>";
160 }