Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_con / ecore_con_alloc.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "Ecore.h"
6 #include "ecore_private.h"
7 #include "Ecore_Con.h"
8 #include "ecore_con_private.h"
9
10 typedef struct _Ecore_Con_Mempool Ecore_Con_Mempool;
11 struct _Ecore_Con_Mempool
12 {
13    const char *name;
14    Eina_Mempool *mp;
15    size_t size;
16 };
17
18 #define GENERIC_ALLOC_FREE(TYPE, Type)                                  \
19   Ecore_Con_Mempool Type##_mp = { #TYPE,  NULL, sizeof (TYPE) };        \
20                                                                         \
21   TYPE *                                                                \
22   Type##_alloc(void)                                                    \
23   {                                                                     \
24      return eina_mempool_malloc(Type##_mp.mp, sizeof (TYPE));           \
25   }                                                                     \
26                                                                         \
27   void                                                                  \
28   Type##_free(TYPE *e)                                                  \
29   {                                                                     \
30      eina_mempool_free(Type##_mp.mp, e);                                \
31   }
32
33 GENERIC_ALLOC_FREE(Ecore_Con_Event_Client_Add, ecore_con_event_client_add);
34 GENERIC_ALLOC_FREE(Ecore_Con_Event_Client_Del, ecore_con_event_client_del);
35 GENERIC_ALLOC_FREE(Ecore_Con_Event_Client_Write, ecore_con_event_client_write);
36 GENERIC_ALLOC_FREE(Ecore_Con_Event_Client_Data, ecore_con_event_client_data);
37 GENERIC_ALLOC_FREE(Ecore_Con_Event_Server_Error, ecore_con_event_server_error);
38 GENERIC_ALLOC_FREE(Ecore_Con_Event_Client_Error, ecore_con_event_client_error);
39 GENERIC_ALLOC_FREE(Ecore_Con_Event_Server_Add, ecore_con_event_server_add);
40 GENERIC_ALLOC_FREE(Ecore_Con_Event_Server_Del, ecore_con_event_server_del);
41 GENERIC_ALLOC_FREE(Ecore_Con_Event_Server_Write, ecore_con_event_server_write);
42 GENERIC_ALLOC_FREE(Ecore_Con_Event_Server_Data, ecore_con_event_server_data);
43 GENERIC_ALLOC_FREE(Ecore_Con_Event_Proxy_Bind, ecore_con_event_proxy_bind);
44
45 static Ecore_Con_Mempool *mempool_array[] = {
46   &ecore_con_event_client_add_mp,
47   &ecore_con_event_client_del_mp,
48   &ecore_con_event_client_write_mp,
49   &ecore_con_event_client_data_mp,
50   &ecore_con_event_server_error_mp,
51   &ecore_con_event_client_error_mp,
52   &ecore_con_event_server_add_mp,
53   &ecore_con_event_server_del_mp,
54   &ecore_con_event_server_write_mp,
55   &ecore_con_event_server_data_mp,
56   &ecore_con_event_proxy_bind_mp
57 };
58
59 void
60 ecore_con_mempool_init(void)
61 {
62    const char *choice;
63    unsigned int i;
64
65    choice = getenv("EINA_MEMPOOL");
66    if (!choice || !choice[0])
67      choice = "chained_mempool";
68
69    for (i = 0; i < sizeof (mempool_array) / sizeof (mempool_array[0]); ++i)
70      {
71      retry:
72         mempool_array[i]->mp = eina_mempool_add(choice, mempool_array[i]->name, NULL, mempool_array[i]->size, 16);
73         if (!mempool_array[i]->mp)
74           {
75              if (!(!strcmp(choice, "pass_through")))
76                {
77                   ERR("Falling back to pass through ! Previously tried '%s' mempool.", choice);
78                   choice = "pass_through";
79                   goto retry;
80                }
81              else
82                {
83                   ERR("Impossible to allocate mempool '%s' !", choice);
84                   return ;
85                }
86           }
87      }
88 }
89
90 void
91 ecore_con_mempool_shutdown(void)
92 {
93    unsigned int i;
94
95    for (i = 0; i < sizeof (mempool_array) / sizeof (mempool_array[0]); ++i)
96      {
97         eina_mempool_del(mempool_array[i]->mp);
98         mempool_array[i]->mp = NULL;
99      }
100 }
101