summaryrefslogtreecommitdiff
path: root/src/modules/eina/mp/one_big/eina_one_big.c
blob: 50edfaab7b2173d207258d10383919145b86afa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* EINA - EFL data type library
 * Copyright (C) 2010 Cedric BAIL, Vincent Torri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include <assert.h>

#include "eina_config.h"
#include "eina_mempool.h"
#include "eina_trash.h"
#include "eina_inlist.h"
#include "eina_log.h"
#include "eina_lock.h"
#include "eina_thread.h"
#include "eina_cpu.h"

#ifndef NVALGRIND
# include <memcheck.h>
#endif

#include "eina_private.h"

#ifdef INF
#undef INF
#endif
#define INF(...) EINA_LOG_DOM_INFO(_eina_mempool_log_dom, __VA_ARGS__)

#ifdef WRN
#undef WRN
#endif
#define WRN(...) EINA_LOG_DOM_WARN(_eina_one_big_mp_log_dom, __VA_ARGS__)

#define OVER_MEM_TO_LIST(_pool, _over_mem)                      \
  ((Eina_Inlist *)(((char *)_over_mem) + (_pool)->offset_to_item_inlist))

#define OVER_MEM_FROM_LIST(_pool, _node)        \
  ((void *)(((char *)_node) - (_pool)->offset_to_item_inlist))

static int _eina_one_big_mp_log_dom = -1;

typedef struct _One_Big One_Big;
struct _One_Big
{
   const char *name;

   unsigned int item_size;
   int offset_to_item_inlist;

   int usage;
   int over;

   unsigned int served;
   unsigned int max;
   unsigned char *base;

   Eina_Trash *empty;
   Eina_Inlist *over_list;

#ifdef EINA_HAVE_DEBUG_THREADS
   Eina_Thread self;
#endif
   Eina_Lock mutex;
};

static void *
eina_one_big_malloc(void *data, EINA_UNUSED unsigned int size)
{
   One_Big *pool = data;
   unsigned char *mem = NULL;

   if (!eina_lock_take(&pool->mutex))
     {
#ifdef EINA_HAVE_DEBUG_THREADS
        assert(eina_thread_equal(pool->self, eina_thread_self()));
#endif
     }

   if (pool->empty)
     {
#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_DEFINED(pool->empty, pool->item_size);
#endif
        mem = eina_trash_pop(&pool->empty);
        pool->usage++;
        goto on_exit;
     }

   if (!pool->base)
     {
	pool->base = malloc(pool->item_size * pool->max);
	if (!pool->base) goto retry_smaller;
#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_NOACCESS(pool->base, pool->item_size * pool->max);
#endif
     }

   if (pool->served < pool->max)
     {
        mem = pool->base + (pool->served++ *pool->item_size);
        pool->usage++;
        goto on_exit;
     }

 retry_smaller:
   mem = malloc(sizeof(Eina_Inlist) + pool->offset_to_item_inlist);
   if (mem)
     {
        Eina_Inlist *node = OVER_MEM_TO_LIST(pool, mem);
        pool->over++;
        /* Only need to zero list elements and not the payload here */
        memset(node, 0, sizeof(Eina_Inlist));
        pool->over_list = eina_inlist_append(pool->over_list, node);
     }
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(mem, pool->item_size);
#endif

on_exit:
   eina_lock_release(&pool->mutex);

#ifndef NVALGRIND
   VALGRIND_MEMPOOL_ALLOC(pool, mem, pool->item_size);
#endif
   return mem;
}

static void
eina_one_big_free(void *data, void *ptr)
{
   One_Big *pool = data;

   if (!eina_lock_take(&pool->mutex))
     {
#ifdef EINA_HAVE_DEBUG_THREADS
        assert(eina_thread_equal(pool->self, eina_thread_self()));
#endif
     }

   if ((void *)pool->base <= ptr
       && ptr < (void *)(pool->base + (pool->max * pool->item_size)))
     {
        eina_trash_push(&pool->empty, ptr);
        pool->usage--;

#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_NOACCESS(ptr, pool->item_size);
#endif
     }
   else
     {
#ifndef NDEBUG
        Eina_Inlist *it;
#endif
        Eina_Inlist *il;

        il = OVER_MEM_TO_LIST(pool, ptr);

#ifndef NDEBUG
        for (it = pool->over_list; it != NULL; it = it->next)
          if (it == il) break;

        assert(it != NULL);
#endif

        pool->over_list = eina_inlist_remove(pool->over_list, il);

#ifndef NVALGRIND
        VALGRIND_MEMPOOL_FREE(pool, ptr);
#endif
        free(ptr);
        pool->over--;
     }

   eina_lock_release(&pool->mutex);
}

static Eina_Bool
eina_one_big_from(void *data, void *ptr)
{
   One_Big *pool = data;
   Eina_Bool r = EINA_FALSE;

   if (!eina_lock_take(&pool->mutex))
     {
#ifdef EINA_HAVE_DEBUG_THREADS
        assert(eina_thread_equal(pool->self, eina_thread_self()));
#endif
     }

   if ((void *)pool->base <= ptr
       && ptr < (void *)(pool->base + (pool->max * pool->item_size)))
     {
        Eina_Trash *t;
#ifndef NVALGRIND
        Eina_Trash *last = NULL;
#endif
        // Part of the bigger area

        // Check if it is a properly aligned element
        if (((unsigned char *)ptr - (unsigned char *) pool->base) % pool->item_size)
          {
#ifdef DEBUG
             ERR("%p is %lu bytes inside a pointer served by %p '%s' One_Big_Mempool (You are freeing the wrong pointer man !).",
                 ptr, ((unsigned char *)ptr - (unsigned char *) pool->base) % pool->item_size, pool, pool->name);
#endif
             goto end;
          }

        // Check if the pointer was freed
        for (t = pool->empty; t != NULL; t = t->next)
          {
#ifndef NVALGRIND
             VALGRIND_MAKE_MEM_DEFINED(t, pool->item_size);
             if (last) VALGRIND_MAKE_MEM_NOACCESS(last, pool->item_size);
             last = t;
#endif
             if (t == ptr) goto end;
          }
#ifndef NVALGRIND
        if (last) VALGRIND_MAKE_MEM_NOACCESS(last, pool->item_size);
#endif

        // Everything seems correct
        r = EINA_TRUE;
     }
   else
     {
        Eina_Inlist *it, *il;
        // Part of the smaller items inlist

        il = OVER_MEM_TO_LIST(pool, ptr);

        for (it = pool->over_list; it != NULL; it = it->next)
          if (it == il)
            {
               r = EINA_TRUE;
               break;
            }
     }

 end:
   eina_lock_release(&pool->mutex);
   return r;
}

typedef struct _Eina_Iterator_One_Big_Mempool Eina_Iterator_One_Big_Mempool;
struct _Eina_Iterator_One_Big_Mempool
{
   Eina_Iterator iterator;

   Eina_Iterator *walker;
   One_Big *pool;

   unsigned int offset;
};

static Eina_Bool
eina_mempool_iterator_next(Eina_Iterator_One_Big_Mempool *it, void **data)
{
   Eina_Inlist *il = NULL;

 retry:
   if (it->offset < (it->pool->max * it->pool->item_size))
     {
        unsigned char *ptr = (unsigned char *) (it->pool->base);

        ptr += it->offset;
        it->offset += it->pool->item_size;

        if (!eina_one_big_from(it->pool, ptr)) goto retry;

        if (data) *data = (void *) ptr;
        return EINA_TRUE;
     }

   if (!eina_iterator_next(it->walker, (void **) &il))
     return EINA_FALSE;

   if (data) *data = OVER_MEM_FROM_LIST(it->pool, il);
   return EINA_TRUE;
}

static One_Big *
eina_mempool_iterator_get_container(Eina_Iterator_One_Big_Mempool *it)
{
   return it->pool;
}

static void
eina_mempool_iterator_free(Eina_Iterator_One_Big_Mempool *it)
{
   eina_iterator_free(it->walker);
   free(it);
}

static Eina_Iterator *
eina_one_big_iterator_new(void *data)
{
   Eina_Iterator_One_Big_Mempool *it;
   One_Big *pool = data;

   it = calloc(1, sizeof (Eina_Iterator_One_Big_Mempool));
   if (!it) return NULL;

    it->walker = eina_inlist_iterator_new(pool->over_list);
    it->pool = pool;

    it->iterator.version = EINA_ITERATOR_VERSION;
    it->iterator.next = FUNC_ITERATOR_NEXT(eina_mempool_iterator_next);
    it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER(
                                                             eina_mempool_iterator_get_container);
    it->iterator.free = FUNC_ITERATOR_FREE(eina_mempool_iterator_free);

    EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR);

    return &it->iterator;
}

static void *
eina_one_big_realloc(EINA_UNUSED void *data,
                     EINA_UNUSED void *element,
                     EINA_UNUSED unsigned int size)
{
   return NULL;
}

static void *
eina_one_big_init(const char *context,
                  EINA_UNUSED const char *option,
                  va_list args)
{
   One_Big *pool;
   int item_size;
   size_t length;

   length = context ? strlen(context) + 1 : 0;

   pool = calloc(1, sizeof (One_Big) + length);
   if (!pool)
      return NULL;

   item_size = va_arg(args, int);
   if (item_size < 1) item_size = 1;

   pool->item_size = MAX(eina_mempool_alignof(item_size), sizeof(void*));
   pool->max = va_arg(args, int);
   if (pool->max < 1) pool->max = 1;

   pool->offset_to_item_inlist = pool->item_size;
   if (pool->offset_to_item_inlist % (int)sizeof(void *) != 0)
     {
        pool->offset_to_item_inlist =
          (((pool->offset_to_item_inlist / (int)sizeof(void *)) + 1) *
           (int)sizeof(void *));
     }

   if (length)
     {
        pool->name = (const char *)(pool + 1);
        memcpy((char *)pool->name, context, length);
     }

#ifdef EINA_HAVE_DEBUG_THREADS
   pool->self = eina_thread_self();
#endif
   eina_lock_new(&pool->mutex);

#ifndef NVALGRIND
   VALGRIND_CREATE_MEMPOOL(pool, 0, 1);
#endif

   return pool;
}

static void
eina_one_big_shutdown(void *data)
{
   One_Big *pool = data;

   if (!pool) return;
   if (!eina_lock_take(&pool->mutex))
     {
#ifdef EINA_HAVE_DEBUG_THREADS
        assert(eina_thread_equal(pool->self, eina_thread_self()));
#endif
     }

   if (pool->over > 0)
     {
// FIXME: should we warn here? one_big mempool exceeded its alloc and now
// mempool is cleaning up the mess created. be quiet for now as we were before
// but edje seems to be a big offender at the moment! bad cedric! :)
//        WRN(
//            "Pool [%s] over by %i. cleaning up for you", 
//            pool->name, pool->over);
        while (pool->over_list)
          {
             Eina_Inlist *il = pool->over_list;
             void *ptr = OVER_MEM_FROM_LIST(pool, il);
             pool->over_list = eina_inlist_remove(pool->over_list, il);
             free(ptr);
             pool->over--;
          }
     }
   if (pool->over > 0)
     {
        WRN(
            "Pool [%s] still over by %i\n", 
            pool->name, pool->over);
     }

#ifndef NVALGRIND
   VALGRIND_DESTROY_MEMPOOL(pool);
#endif

   if (pool->base) free(pool->base);

   eina_lock_release(&pool->mutex);
   eina_lock_free(&pool->mutex);
   free(pool);
}


static Eina_Mempool_Backend _eina_one_big_mp_backend = {
   "one_big",
   &eina_one_big_init,
   &eina_one_big_free,
   &eina_one_big_malloc,
   &eina_one_big_realloc,
   NULL,
   NULL,
   &eina_one_big_shutdown,
   NULL,
   &eina_one_big_from,
   &eina_one_big_iterator_new,
   NULL
};

Eina_Bool one_big_init(void)
{
#ifdef DEBUG
   _eina_one_big_mp_log_dom = eina_log_domain_register("eina_one_big_mempool",
                                                       EINA_LOG_COLOR_DEFAULT);
   if (_eina_one_big_mp_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: eina_one_big_mempool");
        return EINA_FALSE;
     }

#endif
   return eina_mempool_register(&_eina_one_big_mp_backend);
}

void one_big_shutdown(void)
{
   eina_mempool_unregister(&_eina_one_big_mp_backend);
#ifdef DEBUG
   eina_log_domain_unregister(_eina_one_big_mp_log_dom);
   _eina_one_big_mp_log_dom = -1;
#endif
}

#ifndef EINA_STATIC_BUILD_ONE_BIG

EINA_MODULE_INIT(one_big_init);
EINA_MODULE_SHUTDOWN(one_big_shutdown);

#endif /* ! EINA_STATIC_BUILD_ONE_BIG */