File: eina_cow.c

package info (click to toggle)
efl 1.8.6-2.5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 122,896 kB
  • ctags: 52,058
  • sloc: ansic: 503,707; sh: 12,299; cpp: 11,154; makefile: 1,756; lisp: 433; pascal: 398; python: 233; asm: 209; objc: 101; xml: 35; sed: 16
file content (635 lines) | stat: -rw-r--r-- 14,298 bytes parent folder | download | duplicates (2)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/* Eina - EFL data type library
 * Copyright (C) 2013 Cedric Bail
 *
 * 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

#ifdef HAVE_BACKTRACE
# include <execinfo.h>
#endif

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

#include "eina_config.h"
#include "eina_private.h"
#include "eina_log.h"
#include "eina_mempool.h"
#include "eina_types.h"
#include "eina_safety_checks.h"
#include "eina_list.h"
#include "eina_hash.h"

#include "eina_cow.h"

#ifdef EINA_COW_MAGIC_ON
#define EINA_COW_MAGIC 0xDEADBEEF

# define EINA_COW_PTR_MAGIC 0xBEEFE00
#endif

typedef struct _Eina_Cow_Ptr Eina_Cow_Ptr;
typedef struct _Eina_Cow_GC Eina_Cow_GC;

#ifdef HAVE_BACKTRACE
#define EINA_DEBUG_BT_NUM 64
typedef void (*Eina_Bt_Func) ();
#endif

struct _Eina_Cow_Ptr
{
#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC;
# ifdef HAVE_BACKTRACE
   Eina_Bt_Func writer_bt[EINA_DEBUG_BT_NUM];
   int  writer_bt_num;
# endif
#endif
   int refcount;

   Eina_Bool hashed : 1;
   Eina_Bool togc : 1;
#ifdef EINA_COW_MAGIC_ON
   Eina_Bool writing : 1;
#endif
};

struct _Eina_Cow_GC
{
#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC;
#endif

   Eina_Cow_Ptr *ref;
   const void **dst;
};
struct _Eina_Cow
{
#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC;
#endif

   Eina_Hash *togc;
   Eina_Hash *match;

   Eina_Mempool *pool;
   const Eina_Cow_Data *default_value;

   unsigned int struct_size;
   unsigned int total_size;
};

typedef int (*Eina_Cow_Hash)(const void *, int);

#ifdef EINA_COW_MAGIC_ON
# define EINA_COW_MAGIC_CHECK(d)                        \
  do {                                                  \
     if (!EINA_MAGIC_CHECK((d), EINA_COW_MAGIC))        \
       EINA_MAGIC_FAIL((d), EINA_COW_MAGIC);            \
  } while (0);

# define EINA_COW_PTR_MAGIC_CHECK(d)                    \
  do {                                                  \
     if (!EINA_MAGIC_CHECK((d), EINA_COW_PTR_MAGIC))    \
       EINA_MAGIC_FAIL((d), EINA_COW_PTR_MAGIC);        \
  } while (0);
#else
# define EINA_COW_MAGIC_CHECK(d)
# define EINA_COW_PTR_MAGIC_CHECK(d)
#endif

#define EINA_COW_PTR_SIZE                       \
  eina_mempool_alignof(sizeof (Eina_Cow_Ptr))

#define EINA_COW_PTR_GET(d)				\
  (((Eina_Cow_Ptr *)d) - 1)

#define EINA_COW_DATA_GET(d)                    \
  (((Eina_Cow_Ptr *)d) + 1)

static int _eina_cow_log_dom = -1;

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_eina_cow_log_dom, __VA_ARGS__)

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

#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_eina_cow_log_dom, __VA_ARGS__)


static Eina_Mempool *gc_pool = NULL;

static inline int
_eina_cow_hash_gen(const void *key, int key_length,
                   Eina_Cow_Hash hash,
                   int size)
{
   const unsigned char *walk = key;
   int r = 0xDEADBEEF;

   while (key_length > 0)
     {
        r ^= hash(walk, size);

        walk += size;
        key_length -= size;
     }

   return r;
}

#ifdef EFL64
static int
_eina_cow_hash64(const void *key, int key_length)
{
   return _eina_cow_hash_gen(key, key_length,
                             (Eina_Cow_Hash) eina_hash_int64, sizeof (unsigned long long int));
}
#else
static int
_eina_cow_hash32(const void *key, int key_length)
{
   return _eina_cow_hash_gen(key, key_length,
                             (Eina_Cow_Hash) eina_hash_int32, sizeof (int));
}
#endif

static int current_cow_size = 0;

static unsigned int
_eina_cow_length(const void *key EINA_UNUSED)
{
   /* nasty hack, since only gc needs to access the hash, it will be in charge
      of that global. access to the hash should be considered global.
    */
   return current_cow_size;
}

static int
_eina_cow_cmp(const void *key1, int key1_length,
              const void *key2, int key2_length EINA_UNUSED)
{
   return memcmp(key1, key2, key1_length);
}

static inline void
_eina_cow_hash_del(Eina_Cow *cow,
                   const void *data,
                   Eina_Cow_Ptr *ref)
{
   /* eina_cow_gc is not supposed to be thread safe */
   if (!ref->hashed) return;

   current_cow_size = cow->struct_size;
   eina_hash_del(cow->match, data, data);
   ref->hashed = EINA_FALSE;
}

static void
_eina_cow_gc_free(void *data)
{
   eina_mempool_free(gc_pool, data);
}

static inline void
_eina_cow_togc_del(Eina_Cow *cow, Eina_Cow_Ptr *ref)
{
   /* eina_cow_gc is not supposed to be thread safe */
   if (!ref->togc) return;
   eina_hash_del(cow->togc, &ref, NULL);
   ref->togc = EINA_FALSE;
}

static void
_eina_cow_togc_add(Eina_Cow *cow,
                   Eina_Cow_Ptr *ref,
                   const Eina_Cow_Data ** dst)
{
   Eina_Cow_GC *gc;

   /* needed if we want to make cow gc safe */
   if (ref->togc) return;
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif

   gc = eina_mempool_malloc(gc_pool, sizeof (Eina_Cow_GC));
   if (!gc) return; /* That one will not get gced this time */

   gc->ref = ref;
   gc->dst = dst;
   eina_hash_direct_add(cow->togc, &gc->ref, gc);
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
   ref->togc = EINA_TRUE;
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif
}

static void
_eina_cow_gc(Eina_Cow *cow, Eina_Cow_GC *gc)
{
   Eina_Cow_Data *data;
   Eina_Cow_Data *match;

   data = EINA_COW_DATA_GET(gc->ref);

   current_cow_size = cow->struct_size;
   match = eina_hash_find(cow->match, data);
   if (match)
     {
        Eina_Cow_Ptr *ref = EINA_COW_PTR_GET(match);
#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
        ref->refcount += gc->ref->refcount;

        *gc->dst = match;
        eina_cow_free(cow, (const Eina_Cow_Data**) &data);

#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif
     }
   else
     {
        eina_hash_direct_add(cow->match, data, data);
        gc->ref->hashed = EINA_TRUE;
        gc->ref->togc = EINA_FALSE;
        eina_hash_del(cow->togc, &gc->ref, gc);
     }
}

Eina_Bool
eina_cow_init(void)
{
   const char *choice, *tmp;

   _eina_cow_log_dom = eina_log_domain_register("eina_cow", EINA_LOG_COLOR_DEFAULT);
   if (_eina_cow_log_dom < 0)
     {
        EINA_LOG_ERR("Could not register log domain: eina_cow");
        return EINA_FALSE;
     }

#ifdef EINA_DEFAULT_MEMPOOL
   choice = "pass_through";
#else
   choice = "chained_mempool";
#endif
   tmp = getenv("EINA_MEMPOOL");
   if (tmp && tmp[0])
     choice = tmp;

   gc_pool = eina_mempool_add(choice, "gc", NULL, sizeof (Eina_Cow_GC), 32);
   if (!gc_pool)
     {
        ERR("Mempool for cow gc cannot be allocated.");
        return EINA_FALSE;
     }
   return EINA_TRUE;
}

Eina_Bool
eina_cow_shutdown(void)
{
   eina_mempool_del(gc_pool);
   return EINA_TRUE;
}

EAPI Eina_Cow *
eina_cow_add(const char *name, unsigned int struct_size, unsigned int step, const void *default_value, Eina_Bool gc)
{
   const char *choice, *tmp;
   Eina_Cow *cow;
   unsigned int total_size;

   EINA_SAFETY_ON_NULL_RETURN_VAL(default_value, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(struct_size, NULL);
   EINA_SAFETY_ON_FALSE_RETURN_VAL(step, NULL);

   cow = malloc(sizeof (Eina_Cow));
   if (!cow) return NULL;

#ifdef EINA_DEFAULT_MEMPOOL
   choice = "pass_through";
#else
   choice = "chained_mempool";
#endif
   tmp = getenv("EINA_MEMPOOL");
   if (tmp && tmp[0])
     choice = tmp;

   INF("Creating Cow '%s' with mempool of type '%s'", name, choice);
   total_size = eina_mempool_alignof(struct_size + EINA_COW_PTR_SIZE);
   cow->pool = eina_mempool_add(choice, name, NULL, total_size, step);
   if (!cow->pool)
     {
        ERR("Mempool for cow '%s' cannot be allocated.", name);
        goto on_error;
     }

#ifdef EFL64
   cow->match = eina_hash_new(_eina_cow_length,
                              _eina_cow_cmp,
                              _eina_cow_hash64,
                              NULL,
                              6);
#else
   cow->match = eina_hash_new(_eina_cow_length,
                              _eina_cow_cmp,
                              _eina_cow_hash32,
                              NULL,
                              6);
#endif
   if (gc)
     cow->togc = eina_hash_pointer_new(_eina_cow_gc_free);
   else
     cow->togc = NULL;
   cow->default_value = default_value;
   cow->struct_size = struct_size;
   cow->total_size = total_size;

#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC_SET(cow, EINA_COW_MAGIC);
#endif

   return cow;

 on_error:
   free(cow);
   return NULL;
}

EAPI void
eina_cow_del(Eina_Cow *cow)
{
   if (!cow) return;

#ifdef EINA_COW_MAGIC_ON
   EINA_COW_MAGIC_CHECK(cow);
#endif

   eina_mempool_del(cow->pool);
   eina_hash_free(cow->match);
   if (cow->togc) eina_hash_free(cow->togc);
   free(cow);
}

EAPI const Eina_Cow_Data *
eina_cow_alloc(Eina_Cow *cow)
{
#ifdef EINA_COW_MAGIC_ON
   EINA_COW_MAGIC_CHECK(cow);
#endif

   return cow->default_value;
}

EAPI void
eina_cow_free(Eina_Cow *cow, const Eina_Cow_Data **data)
{
   Eina_Cow_Ptr *ref;

#ifdef EINA_COW_MAGIC_ON
   EINA_COW_MAGIC_CHECK(cow);
#endif

   if (!data || !*data) return;
   if (cow->default_value == *data) return;

   ref = EINA_COW_PTR_GET(*data);
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
   ref->refcount--;

   if (ref->refcount == 0) _eina_cow_hash_del(cow, *data, ref);
   *data = (Eina_Cow_Data*) cow->default_value;

   if (ref->refcount > 0)
     {
#ifndef NVALGRIND
       VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif
       return;
     }

#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC_SET(ref, EINA_MAGIC_NONE);
#endif
   _eina_cow_togc_del(cow, ref);
   eina_mempool_free(cow->pool, (void*) ref);
}

EAPI void *
eina_cow_write(Eina_Cow *cow,
	       const Eina_Cow_Data * const *data)
{
   Eina_Cow_Ptr *ref;
   Eina_Cow_Data *r;

#ifdef EINA_COW_MAGIC_ON
   EINA_COW_MAGIC_CHECK(cow);
#endif

   if (!*data) return NULL; /* cow pointer is always != NULL */
   if (*data == cow->default_value)
     goto allocate;

   ref = EINA_COW_PTR_GET(*data);

#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
   if (ref->refcount == 1)
     {
#ifdef EINA_COW_MAGIC_ON
        EINA_COW_PTR_MAGIC_CHECK(ref);

        if (ref->writing)
          {
             ERR("Request writing on an pointer that is already in a writing process %p\n", data);
#ifdef HAVE_BACKTRACE
             backtrace_symbols_fd((void **) ref->writer_bt,
                                  ref->writer_bt_num, 1);
#endif
             return NULL;
          }
#endif

        if (cow->togc)
          _eina_cow_hash_del(cow, *data, ref);

#ifndef NVALGRIND
        VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif
        goto end;
     }
   ref->refcount--;

 allocate:
   ref = eina_mempool_malloc(cow->pool, cow->total_size);
   ref->refcount = 1;
   ref->hashed = EINA_FALSE;
   ref->togc = EINA_FALSE;
#ifdef EINA_COW_MAGIC_ON
   EINA_MAGIC_SET(ref, EINA_COW_PTR_MAGIC);
#endif

#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif

   r = EINA_COW_DATA_GET(ref);
   memcpy(r, *data, cow->struct_size);
   *((Eina_Cow_Data**) data) = r;

 end:
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
#ifdef EINA_COW_MAGIC_ON
# ifdef HAVE_BACKTRACE
   ref->writer_bt_num = backtrace((void **)(ref->writer_bt),
                                  EINA_DEBUG_BT_NUM);
# endif
   ref->writing = EINA_TRUE;
#endif
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif

   return (void *) *data;
}

EAPI void
eina_cow_done(Eina_Cow *cow,
	      const Eina_Cow_Data * const * dst,
	      const void *data,
              Eina_Bool needed_gc)
{
   Eina_Cow_Ptr *ref;

   EINA_COW_MAGIC_CHECK(cow);

   ref = EINA_COW_PTR_GET(data);
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
   EINA_COW_PTR_MAGIC_CHECK(ref);
#ifdef EINA_COW_MAGIC_ON
   if (!ref->writing)
     ERR("Pointer %p is not in a writable state !", dst);

   ref->writing = EINA_FALSE;
#endif
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif

   if (!cow->togc || !needed_gc) return;

#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif

   _eina_cow_togc_add(cow, ref, (const Eina_Cow_Data **) dst);
}

EAPI void
eina_cow_memcpy(Eina_Cow *cow,
		const Eina_Cow_Data * const *dst,
		const Eina_Cow_Data *src)
{
   Eina_Cow_Ptr *ref;

   EINA_COW_MAGIC_CHECK(cow);

   if (*dst == src) return;

   if (src != cow->default_value)
     {
       ref = EINA_COW_PTR_GET(src);
#ifndef NVALGRIND
       VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif

       EINA_COW_PTR_MAGIC_CHECK(ref);
       ref->refcount++;

       if (cow->togc)
         _eina_cow_togc_del(cow, ref);

#ifndef NVALGRIND
       VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif
     }

   eina_cow_free(cow, (const Eina_Cow_Data**) dst);

   *((const void**)dst) = src;
}

EAPI Eina_Bool
eina_cow_gc(Eina_Cow *cow)
{
   Eina_Cow_GC *gc;
   Eina_Iterator *it;
   Eina_Bool r;
#ifndef NVALGRIND
   Eina_Cow_Ptr *ref;
#endif

   EINA_COW_MAGIC_CHECK(cow);

   if (!cow->togc || !eina_hash_population(cow->togc))
     return EINA_FALSE;

   it = eina_hash_iterator_data_new(cow->togc);
   r = eina_iterator_next(it, (void**) &gc);
   eina_iterator_free(it);

   if (!r) return EINA_FALSE; /* Something did go wrong here */

#ifndef NVALGRIND
   /* Do handle hash and all funky merge thing here */
   ref = gc->ref;

   VALGRIND_MAKE_MEM_DEFINED(ref, sizeof (*ref));
#endif
   _eina_cow_gc(cow, gc);
#ifndef NVALGRIND
   VALGRIND_MAKE_MEM_NOACCESS(ref, sizeof (*ref));
#endif

   return EINA_TRUE;
}