summaryrefslogtreecommitdiff
path: root/eina_trash.c
blob: bf11589fc097764fb931442c5ed6ec1621f58d7c (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

#if EINA_VERSION_MINOR < 8
static Eina_Lock           _trash_lock;
# define LOCK_NEW(_l)      eina_lock_new(_l)
# define LOCK_FREE(_l)     eina_lock_free(_l)
# define LOCK_TAKE(_l)     eina_lock_take(_l)
# define LOCK_RELEASE(_l)  eina_lock_release(_l)
#else
static Eina_Spinlock       _trash_lock;
# define LOCK_NEW(_l)      eina_spinlock_new(_l)
# define LOCK_FREE(_l)     eina_spinlock_free(_l)
# define LOCK_TAKE(_l)     eina_spinlock_take(_l)
# define LOCK_RELEASE(_l)  eina_spinlock_release(_l)
#endif

static Eina_Trash       *_objects_trash = NULL;

static void
_trash_init(void)
{
   LOCK_NEW(&_trash_lock);
}

static void
_trash_shutdown(void)
{
   void *data;

   /* lock release/take should not be necessary */
   LOCK_TAKE(&_trash_lock);
   EINA_TRASH_CLEAN(&_objects_trash, data)
      free(data);
   LOCK_RELEASE(&_trash_lock);
   LOCK_FREE(&_trash_lock);
}

static OBJ *
_trash_object_lease(void)
{
   OBJ *obj = NULL;

   LOCK_TAKE(&_trash_lock);
   obj = (OBJ *) eina_trash_pop(&_objects_trash);
   LOCK_RELEASE(&_trash_lock);

   if (obj)
     {
        memset(obj, 0, sizeof(OBJ));
        return obj;
     }
   else
     obj = calloc(1, sizeof(OBJ));

   EINA_LOG_DBG("%p", obj);
   return obj;
}

static void *
_trash_object_release(OBJ *obj)
{
   EINA_LOG_DBG("%p", obj);
   LOCK_TAKE(&_trash_lock);
   eina_trash_push(&_objects_trash, obj);
   LOCK_RELEASE(&_trash_lock);
}