move around - flatter.
[profile/ivi/evas.git] / src / lib / main.c
1 #include "evas_common.h"
2 #include "evas_private.h"
3
4 int _evas_alloc_error = 0;
5 static int _evas_debug_init = 0;
6 static int _evas_debug_show = 0;
7 static int _evas_debug_abort = 0;
8
9 /**
10  * Return if any allocation errors have occured during the prior function
11  * @return The allocation error flag
12  *
13  * This function will return if any memory allocation errors occured during,
14  * and what kind they were. The return value will be one of
15  * EVAS_ALLOC_ERROR_NONE, EVAS_ALLOC_ERROR_FATAL or EVAS_ALLOC_ERROR_RECOVERED
16  * with each meaning something different.
17  *
18  * EVAS_ALLOC_ERROR_NONE means that no errors occured at all and the function
19  * worked as expected.
20  *
21  * EVAS_ALLOC_ERROR_FATAL means the function was completely unable to perform
22  * its job and will  have  exited as cleanly as possible. The programmer
23  * should consider this as a sign of very low memory and should try and safely
24  * recover from the prior functions failure (or try free up memory elsewhere
25  * and try again after more memory is freed).
26  *
27  * EVAS_ALLOC_ERROR_RECOVERED means that an allocation error occured, but was
28  * recovered from by evas finding memory of its own it has allocated and
29  * freeing what it sees as not really usefully allocated memory. What is freed
30  * may vary. Evas may reduce the resolution of images, free cached images or
31  * fonts, trhow out pre-rendered data, reduce the complexity of change lists
32  * etc. Evas and the program will function as per normal after this, but this
33  * is a sign of low memory, and it is suggested that the program try and
34  * identify memory it doesn't need, and free it.
35  *
36  * Example:
37  * @code
38  * extern Evas_Object *object;
39  * void callback (void *data, Evas *e, Evas_Object *obj, void *event_info);
40  *
41  * evas_object_event_callback_add(object, EVAS_CALLBACK_MOUSE_DOWN, callback, NULL);
42  * if (evas_alloc_error() == EVAS_ALLOC_ERROR_FATAL)
43  *   {
44  *     fprintf(stderr, "ERROR: Completely unable to attach callabck. Must\n");
45  *     fprintf(stderr, "       destroy object now as it cannot be used.\n");
46  *     evas_object_del(object);
47  *     object = NULL;
48  *     fprintf(stderr, "WARNING: Memory is really low. Cleaning out RAM.\n");
49  *     my_memory_cleanup();
50  *   }
51  * if (evas_alloc_error() == EVAS_ALLOC_ERROR_RECOVERED)
52  *   {
53  *     fprintf(stderr, "WARNING: Memory is really low. Cleaning out RAM.\n");
54  *     my_memory_cleanup();
55  *   }
56  * @endcode
57  */
58 EAPI int
59 evas_alloc_error(void)
60 {
61    return _evas_alloc_error;
62 }
63
64 /* free cached items only in ram for speed reasons. return 0 if cant free */
65 int
66 evas_mem_free(int mem_required)
67 {
68    return 0;
69 }
70
71 /* start reducing quality of images etc. return 0 if cant free anything */
72 int
73 evas_mem_degrade(int mem_required)
74 {
75    return 0;
76 }
77
78 void *
79 evas_mem_calloc(int size)
80 {
81    void *ptr;
82
83    ptr = calloc(1, size);
84    if (ptr) return ptr;
85    MERR_BAD();
86    while ((!ptr) && (evas_mem_free(size))) ptr = calloc(1, size);
87    if (ptr) return ptr;
88    while ((!ptr) && (evas_mem_degrade(size))) ptr = calloc(1, size);
89    if (ptr) return ptr;
90    MERR_FATAL();
91    return NULL;
92 }
93
94 void
95 evas_debug_error(void)
96 {
97    if (!_evas_debug_init)
98      {
99         if (getenv("EVAS_DEBUG_SHOW")) _evas_debug_show = 1;
100         if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
101         _evas_debug_init = 1;
102      }
103    if (_evas_debug_show)
104      fprintf(stderr,
105              "*** EVAS ERROR: Evas Magic Check Failed!!!\n");
106 }
107
108 void
109 evas_debug_input_null(void)
110 {
111    if (!_evas_debug_init)
112      {
113         if (getenv("EVAS_DEBUG_SHOW")) _evas_debug_show = 1;
114         if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
115         _evas_debug_init = 1;
116      }
117    if (_evas_debug_show)
118      fprintf(stderr,
119              "  Input object pointer is NULL!\n");
120    if (_evas_debug_abort) abort();
121 }
122
123 void
124 evas_debug_magic_null(void)
125 {
126    if (!_evas_debug_init)
127      {
128         if (getenv("EVAS_DEBUG_SHOW")) _evas_debug_show = 1;
129         if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
130         _evas_debug_init = 1;
131      }
132    if (_evas_debug_show)
133      fprintf(stderr,
134              "  Input object is zero'ed out (maybe a freed object or zero-filled RAM)!\n");
135    if (_evas_debug_abort) abort();
136 }
137
138 void
139 evas_debug_magic_wrong(DATA32 expected, DATA32 supplied)
140 {
141    if (!_evas_debug_init)
142      {
143         if (getenv("EVAS_DEBUG_SHOW")) _evas_debug_show = 1;
144         if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
145         _evas_debug_init = 1;
146      }
147    if (_evas_debug_show)
148      fprintf(stderr,
149              "  Input object is wrong type\n"
150              "    Expected: %08x - %s\n"
151              "    Supplied: %08x - %s\n",
152              expected, evas_debug_magic_string_get(expected),
153              supplied, evas_debug_magic_string_get(supplied));
154    if (_evas_debug_abort) abort();
155 }
156
157 void
158 evas_debug_generic(const char *str)
159 {
160    if (!_evas_debug_init)
161      {
162         if (getenv("EVAS_DEBUG_SHOW")) _evas_debug_show = 1;
163         if (getenv("EVAS_DEBUG_ABORT")) _evas_debug_abort = 1;
164         _evas_debug_init = 1;
165      }
166    if (_evas_debug_show)
167      fprintf(stderr,
168              "*** EVAS ERROR:\n"
169              "%s", (char *)str);
170    if (_evas_debug_abort) abort();
171 }
172
173 const char *
174 evas_debug_magic_string_get(DATA32 magic)
175 {
176    switch (magic)
177      {
178       case MAGIC_EVAS:
179         return "Evas";
180         break;
181       case MAGIC_OBJ:
182         return "Evas_Object";
183         break;
184       case MAGIC_OBJ_RECTANGLE:
185         return "Evas_Object (Rectangle)";
186         break;
187       case MAGIC_OBJ_LINE:
188         return "Evas_Object (Line)";
189         break;
190       case MAGIC_OBJ_GRADIENT:
191         return "Evas_Object (Gradient)";
192         break;
193       case MAGIC_OBJ_POLYGON:
194         return "Evas_Object (Polygon)";
195         break;
196       case MAGIC_OBJ_IMAGE:
197         return "Evas_Object (Image)";
198         break;
199       case MAGIC_OBJ_TEXT:
200         return "Evas_Object (Text)";
201         break;
202       case MAGIC_OBJ_SMART:
203         return "Evas_Object (Smart)";
204         break;
205       default:
206         return "<UNKNOWN>";
207      };
208    return "<UNKNOWN>";
209 }