aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/eet/src/lib/eet_alloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/eet/src/lib/eet_alloc.c')
-rw-r--r--libraries/eet/src/lib/eet_alloc.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/libraries/eet/src/lib/eet_alloc.c b/libraries/eet/src/lib/eet_alloc.c
new file mode 100644
index 0000000..85351ad
--- /dev/null
+++ b/libraries/eet/src/lib/eet_alloc.c
@@ -0,0 +1,93 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <Eina.h>
6#include "Eet.h"
7#include "Eet_private.h"
8
9typedef struct _Eet_Mempool Eet_Mempool;
10struct _Eet_Mempool
11{
12 const char *name;
13 Eina_Mempool *mp;
14 size_t size;
15};
16
17#define GENERIC_ALLOC_FREE(TYPE, Type) \
18 Eet_Mempool Type##_mp = { #TYPE, NULL, sizeof (TYPE) }; \
19 \
20 TYPE * \
21 Type##_malloc(unsigned int num) \
22 { \
23 return eina_mempool_malloc(Type##_mp.mp, num * sizeof (TYPE)); \
24 } \
25 TYPE * \
26 Type##_calloc(unsigned int num) \
27 { \
28 return eina_mempool_calloc(Type##_mp.mp, num * sizeof (TYPE)); \
29 } \
30 void \
31 Type##_mp_free(TYPE *e) \
32 { \
33 eina_mempool_free(Type##_mp.mp, e); \
34 }
35
36GENERIC_ALLOC_FREE(Eet_File_Directory, eet_file_directory);
37GENERIC_ALLOC_FREE(Eet_File_Node, eet_file_node);
38GENERIC_ALLOC_FREE(Eet_File_Header, eet_file_header);
39GENERIC_ALLOC_FREE(Eet_Dictionary, eet_dictionary);
40GENERIC_ALLOC_FREE(Eet_File, eet_file);
41
42static Eet_Mempool *mempool_array[] = {
43 &eet_file_directory_mp,
44 &eet_file_node_mp,
45 &eet_file_header_mp,
46 &eet_dictionary_mp,
47 &eet_file_mp,
48};
49
50Eina_Bool
51eet_mempool_init(void)
52{
53 const char *choice;
54 unsigned int i;
55
56 choice = getenv("EINA_MEMPOOL");
57 if ((!choice) || (!choice[0]))
58 choice = "chained_mempool";
59
60 for (i = 0; i < sizeof (mempool_array) / sizeof (mempool_array[0]); ++i)
61 {
62 retry:
63 mempool_array[i]->mp = eina_mempool_add(choice, mempool_array[i]->name, NULL, mempool_array[i]->size, 64);
64 if (!mempool_array[i]->mp)
65 {
66 if (!strcmp(choice, "pass_through"))
67 {
68 ERR("Falling back to pass through ! Previously tried '%s' mempool.", choice);
69 choice = "pass_through";
70 goto retry;
71 }
72 else
73 {
74 ERR("Impossible to allocate mempool '%s' !", choice);
75 return EINA_FALSE;
76 }
77 }
78 }
79 return EINA_TRUE;
80}
81
82void
83eet_mempool_shutdown(void)
84{
85 unsigned int i;
86
87 for (i = 0; i < sizeof (mempool_array) / sizeof (mempool_array[0]); ++i)
88 {
89 eina_mempool_del(mempool_array[i]->mp);
90 mempool_array[i]->mp = NULL;
91 }
92}
93