EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / modules / mp / ememoa_fixed / eina_ememoa_fixed.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric BAIL
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ememoa_mempool_fixed.h>
26
27 #include "eina_inlist.h"
28 #include "eina_error.h"
29 #include "eina_module.h"
30 #include "eina_mempool.h"
31
32 #include "eina_private.h"
33
34 typedef struct _Eina_Ememoa_Fixed_Mempool Eina_Ememoa_Fixed_Mempool;
35 struct _Eina_Ememoa_Fixed_Mempool
36 {
37    struct ememoa_mempool_desc_s *desc;
38    int pool;
39 };
40
41 static void *
42 eina_ememoa_fixed_malloc(void *data, __UNUSED__ unsigned int size)
43 {
44    Eina_Ememoa_Fixed_Mempool *efm = data;
45
46    return ememoa_mempool_fixed_pop_object(efm->pool);
47 }
48
49 static void
50 eina_ememoa_fixed_free(void *data, void *ptr)
51 {
52    Eina_Ememoa_Fixed_Mempool *efm = data;
53
54    ememoa_mempool_fixed_push_object(efm->pool, ptr);
55 }
56
57 static void *
58 eina_ememoa_fixed_realloc(__UNUSED__ void *data,
59                           __UNUSED__ void *element,
60                           __UNUSED__ unsigned int size)
61 {
62    return NULL;
63 }
64
65 static void
66 eina_ememoa_fixed_gc(void *data)
67 {
68    Eina_Ememoa_Fixed_Mempool *efm = data;
69
70    ememoa_mempool_fixed_garbage_collect(efm->pool);
71 }
72
73 static void
74 eina_ememoa_fixed_statistics(void *data)
75 {
76    Eina_Ememoa_Fixed_Mempool *efm = data;
77
78    ememoa_mempool_fixed_display_statistic(efm->pool);
79    (void)efm;
80 }
81
82 static void *
83 eina_ememoa_fixed_init(const char *context,
84                        __UNUSED__ const char *option,
85                        va_list args)
86 {
87    struct ememoa_mempool_desc_s *desc = NULL;
88    Eina_Ememoa_Fixed_Mempool *efm = NULL;
89    Eina_Bool thread_protect;
90    int context_length;
91    int item_size;
92    int pool_size;
93
94    if (context)
95      {
96         context_length = strlen(context) + 1;
97
98         desc = calloc(1, sizeof (struct ememoa_mempool_desc_s) + context_length);
99         if (!desc)
100            goto on_error;
101
102         desc->name = (char *)(desc + 1);
103         memcpy((char *)desc->name, context, context_length);
104      }
105
106    item_size = va_arg(args, int);
107    pool_size = va_arg(args, int);
108    thread_protect = va_arg(args, int);
109
110    efm = malloc(sizeof (Eina_Ememoa_Fixed_Mempool));
111    if (!efm)
112       goto on_error;
113
114    efm->desc = desc;
115    efm->pool = ememoa_mempool_fixed_init(
116          item_size,
117          pool_size,
118          thread_protect ?
119          EMEMOA_THREAD_PROTECTION : 0,
120          efm->desc);
121    if (efm->pool < 0)
122       goto on_error;
123
124    return efm;
125
126 on_error:
127    if (desc)
128       free(desc);
129
130    if (efm)
131       free(efm);
132
133    return NULL;
134 }
135
136 static void
137 eina_ememoa_fixed_shutdown(void *data)
138 {
139    Eina_Ememoa_Fixed_Mempool *efm = data;
140
141    if (efm->desc)
142       free(efm->desc);
143
144       ememoa_mempool_fixed_clean(efm->pool);
145    free(efm);
146 }
147
148 static Eina_Mempool_Backend _eina_ememoa_mp_backend = {
149    .name = "ememoa_fixed",
150    .init = &eina_ememoa_fixed_init,
151    .shutdown = &eina_ememoa_fixed_shutdown,
152    .realloc = &eina_ememoa_fixed_realloc,
153    .alloc = &eina_ememoa_fixed_malloc,
154    .free = &eina_ememoa_fixed_free,
155    .garbage_collect = &eina_ememoa_fixed_gc,
156    .statistics = &eina_ememoa_fixed_statistics,
157    .repack = NULL
158 };
159
160 Eina_Bool ememoa_fixed_init(void)
161 {
162    return eina_mempool_register(&_eina_ememoa_mp_backend);
163 }
164
165 void ememoa_fixed_shutdown(void)
166 {
167    eina_mempool_unregister(&_eina_ememoa_mp_backend);
168 }
169
170
171 #ifndef EINA_STATIC_BUILD_EMEMOA_FIXED
172
173 EINA_MODULE_INIT(ememoa_fixed_init);
174 EINA_MODULE_SHUTDOWN(ememoa_fixed_shutdown);
175
176 #endif /* ! EINA_STATIC_BUILD_EMEMOA_FIXED */