a52d357111276fdf5281266454b9f0c966dd0140
[profile/ivi/eina.git] / src / modules / mp / chained_pool / eina_chained_mempool.c
1 /* EINA - EFL data type library
2  * Copyright (C) 2008-2010 Cedric BAIL, Vincent Torri
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
26 #ifdef EFL_HAVE_POSIX_THREADS
27 #include <pthread.h>
28 #endif
29
30 #ifdef EFL_HAVE_WIN32_THREADS
31 # define WIN32_LEAN_AND_MEAN
32 # include <windows.h>
33 # undef WIN32_LEAN_AND_MEAN
34 #endif
35
36 #include "eina_inlist.h"
37 #include "eina_error.h"
38 #include "eina_module.h"
39 #include "eina_mempool.h"
40 #include "eina_trash.h"
41
42 #include "eina_private.h"
43
44 #ifdef DEBUG
45 #include "eina_log.h"
46
47 static int _eina_mempool_log_dom = -1;
48
49 #ifdef INF
50 #undef INF
51 #endif
52 #define INF(...) EINA_LOG_DOM_INFO(_eina_mempool_log_dom, __VA_ARGS__)
53 #endif
54
55 typedef struct _Chained_Mempool Chained_Mempool;
56 struct _Chained_Mempool
57 {
58    Eina_Inlist *first;
59    const char *name;
60    int item_alloc;
61    int pool_size;
62    int alloc_size;
63    int group_size;
64    int usage;
65 #ifdef EFL_HAVE_THREADS
66 # ifdef EFL_HAVE_POSIX_THREADS
67    pthread_mutex_t mutex;
68 # else
69    HANDLE mutex;
70 # endif
71 #endif
72 };
73
74 typedef struct _Chained_Pool Chained_Pool;
75 struct _Chained_Pool
76 {
77    EINA_INLIST;
78    Eina_Trash *base;
79    int usage;
80
81    unsigned char *last;
82    unsigned char *limit;
83 };
84
85 static inline Chained_Pool *
86 _eina_chained_mp_pool_new(Chained_Mempool *pool)
87 {
88    Chained_Pool *p;
89    unsigned char *ptr;
90
91    eina_error_set(0);
92    p = malloc(pool->alloc_size);
93    if (!p)
94      {
95         eina_error_set(EINA_ERROR_OUT_OF_MEMORY);
96         return NULL;
97      }
98
99    ptr = (unsigned char *)p + eina_mempool_alignof(sizeof(Chained_Pool));
100    p->usage = 0;
101    p->base = NULL;
102
103    p->last = ptr;
104    p->limit = ptr + pool->item_alloc * pool->pool_size;
105    return p;
106 }
107
108 static inline void
109 _eina_chained_mp_pool_free(Chained_Pool *p)
110 {
111    free(p);
112 }
113
114 static void *
115 eina_chained_mempool_malloc(void *data, __UNUSED__ unsigned int size)
116 {
117    Chained_Mempool *pool = data;
118    Chained_Pool *p = NULL;
119    void *mem;
120
121 #ifdef EFL_HAVE_THREADS
122    if (_threads_activated)
123      {
124 # ifdef EFL_HAVE_POSIX_THREADS
125         pthread_mutex_lock(&pool->mutex);
126 # else
127         WaitForSingleObject(pool->mutex, INFINITE);
128 # endif
129      }
130 #endif
131
132    // look 4 pool from 2nd bucket on
133    EINA_INLIST_FOREACH(pool->first, p)
134    {
135       // base is not NULL - has a free slot
136       if (p->base || p->last)
137         {
138            pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
139            break;
140         }
141    }
142
143    // we have reached the end of the list - no free pools
144    if (!p)
145      {
146         p = _eina_chained_mp_pool_new(pool);
147         if (!p)
148           {
149 #ifdef EFL_HAVE_PTHREAD
150              if (_threads_activated)
151                {
152 # ifdef EFL_HAVE_POSIX_THREADS
153                   pthread_mutex_unlock(&pool->mutex);
154 # else
155                   ReleaseMutex(pool->mutex);
156 # endif
157                }
158 #endif
159              return NULL;
160           }
161
162         pool->first = eina_inlist_prepend(pool->first, EINA_INLIST_GET(p));
163      }
164
165    if (p->last)
166      {
167         mem = p->last;
168         p->last += pool->item_alloc;
169         if (p->last >= p->limit)
170           p->last = NULL;
171      }
172    else
173      // Request a free pointer
174      mem = eina_trash_pop(&p->base);
175
176    // move to end - it just filled up
177    if (!p->base && !p->last)
178       pool->first = eina_inlist_demote(pool->first, EINA_INLIST_GET(p));
179
180    p->usage++;
181    pool->usage++;
182
183 #ifdef EFL_HAVE_THREADS
184    if (_threads_activated)
185      {
186 # ifdef EFL_HAVE_POSIX_THREADS
187         pthread_mutex_unlock(&pool->mutex);
188 # else
189         ReleaseMutex(pool->mutex);
190 # endif
191      }
192 #endif
193
194    return mem;
195 }
196
197 static void
198 eina_chained_mempool_free(void *data, void *ptr)
199 {
200    Chained_Mempool *pool = data;
201    Chained_Pool *p;
202    void *pmem;
203    int psize;
204
205    psize = pool->group_size;
206    // look 4 pool
207
208 #ifdef EFL_HAVE_THREADS
209    if (_threads_activated)
210      {
211 # ifdef EFL_HAVE_POSIX_THREADS
212         pthread_mutex_lock(&pool->mutex);
213 # else
214         WaitForSingleObject(pool->mutex, INFINITE);
215 # endif
216      }
217 #endif
218
219    EINA_INLIST_FOREACH(pool->first, p)
220    {
221       // Could the pointer be inside that pool
222       if (ptr < p->limit)
223         {
224            // pool mem base
225            pmem = (void *)(((unsigned char *)p) + sizeof(Chained_Pool));
226            // is it in pool mem?
227            if (ptr >= pmem)
228              {
229                 // freed node points to prev free node
230                 eina_trash_push(&p->base, ptr);
231                 // next free node is now the one we freed
232                 p->usage--;
233                 pool->usage--;
234                 if (p->usage == 0)
235                   {
236                      // free bucket
237                      pool->first = eina_inlist_remove(pool->first, EINA_INLIST_GET(p));
238                      _eina_chained_mp_pool_free(p);
239                   }
240                 else
241                   // move to front
242                   pool->first = eina_inlist_promote(pool->first, EINA_INLIST_GET(p));
243
244                 break;
245              }
246         }
247    }
248
249 #ifdef EFL_HAVE_THREADS
250    if (_threads_activated)
251      {
252 # ifdef EFL_HAVE_POSIX_THREADS
253         pthread_mutex_unlock(&pool->mutex);
254 # else
255         ReleaseMutex(pool->mutex);
256 # endif
257      }
258 #endif
259 }
260
261 static void *
262 eina_chained_mempool_realloc(__UNUSED__ void *data,
263                              __UNUSED__ void *element,
264                              __UNUSED__ unsigned int size)
265 {
266    return NULL;
267 }
268
269 static void *
270 eina_chained_mempool_init(const char *context,
271                           __UNUSED__ const char *option,
272                           va_list args)
273 {
274    Chained_Mempool *mp;
275    int item_size;
276    size_t length;
277
278    length = context ? strlen(context) + 1 : 0;
279
280    mp = calloc(1, sizeof(Chained_Mempool) + length);
281    if (!mp)
282       return NULL;
283
284    item_size = va_arg(args, int);
285    mp->pool_size = va_arg(args, int);
286
287    if (length)
288      {
289         mp->name = (const char *)(mp + 1);
290         memcpy((char *)mp->name, context, length);
291      }
292
293    mp->item_alloc = eina_mempool_alignof(item_size);
294    mp->group_size = mp->item_alloc * mp->pool_size;
295    mp->alloc_size = mp->group_size + eina_mempool_alignof(sizeof(Chained_Pool));
296
297 #ifdef EFL_HAVE_THREADS
298 # ifdef EFL_HAVE_POSIX_THREADS
299    pthread_mutex_init(&mp->mutex, NULL);
300 # else
301    mp->mutex = CreateMutex(NULL, FALSE, NULL);
302 # endif
303 #endif
304
305    return mp;
306 }
307
308 static void
309 eina_chained_mempool_shutdown(void *data)
310 {
311    Chained_Mempool *mp;
312
313    mp = (Chained_Mempool *)data;
314
315    while (mp->first)
316      {
317         Chained_Pool *p = (Chained_Pool *)mp->first;
318
319 #ifdef DEBUG
320         if (p->usage > 0)
321            INF("Bad news we are destroying not an empty mempool [%s]\n",
322                mp->name);
323
324 #endif
325
326         mp->first = eina_inlist_remove(mp->first, mp->first);
327         _eina_chained_mp_pool_free(p);
328      }
329
330 #ifdef EFL_HAVE_THREADS
331 # ifdef EFL_HAVE_POSIX_THREADS
332    pthread_mutex_destroy(&mp->mutex);
333 # else
334    CloseHandle(mp->mutex);
335 # endif
336 #endif
337
338    free(mp);
339 }
340
341 static Eina_Mempool_Backend _eina_chained_mp_backend = {
342    "chained_mempool",
343    &eina_chained_mempool_init,
344    &eina_chained_mempool_free,
345    &eina_chained_mempool_malloc,
346    &eina_chained_mempool_realloc,
347    NULL,
348    NULL,
349    &eina_chained_mempool_shutdown
350 };
351
352 Eina_Bool chained_init(void)
353 {
354 #ifdef DEBUG
355    _eina_mempool_log_dom = eina_log_domain_register("eina_mempool",
356                                                     EINA_LOG_COLOR_DEFAULT);
357    if (_eina_mempool_log_dom < 0)
358      {
359         EINA_LOG_ERR("Could not register log domain: eina_mempool");
360         return EINA_FALSE;
361      }
362
363 #endif
364    return eina_mempool_register(&_eina_chained_mp_backend);
365 }
366
367 void chained_shutdown(void)
368 {
369    eina_mempool_unregister(&_eina_chained_mp_backend);
370 #ifdef DEBUG
371    eina_log_domain_unregister(_eina_mempool_log_dom);
372    _eina_mempool_log_dom = -1;
373 #endif
374 }
375
376 #ifndef EINA_STATIC_BUILD_CHAINED_POOL
377
378 EINA_MODULE_INIT(chained_init);
379 EINA_MODULE_SHUTDOWN(chained_shutdown);
380
381 #endif /* ! EINA_STATIC_BUILD_CHAINED_POOL */