summaryrefslogtreecommitdiff
path: root/src/benchmarks/eina/evas_mempool.c
blob: 921b7dfafbe71608b4a9791d7be1b8b7f0942dfb (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

#include "Evas_Data.h"
#include "evas_mempool.h"

//#define NOPOOL

typedef struct _Pool Pool;

struct _Pool
{
   int usage;
   void *base;
   Pool *prev, *next;
};


Pool *
_evas_mp_pool_new(Evas_Mempool *pool)
{
#ifdef NOPOOL
   static Pool thepool;
   return &thepool;
#else
   Pool *p;
   void **ptr;
   int item_alloc, i;

   item_alloc =
      ((pool->item_size + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
   p = malloc(sizeof(Pool) + (pool->pool_size * item_alloc));
   ptr = (void **)(((unsigned char *)p) + sizeof(Pool));
   p->usage = 0;
   p->base = ptr;
   for (i = 0; i < pool->pool_size - 1; i++)
     {
        *ptr = (void **)(((unsigned char *)ptr) + item_alloc);
        ptr = *ptr;
     }
   *ptr = NULL;
   return p;
#endif
}

void
_evas_mp_pool_free(Pool *p)
{
#if !defined(NOPOOL)
   free(p);
#endif
}

void *
evas_mempool_malloc(Evas_Mempool *pool, int size EINA_UNUSED)
{
#ifdef NOPOOL
   return malloc(size);
#else
   Pool *p;
   void *mem;

   for (p = pool->first; p; p = p->next) // look 4 pool from 2nd bucket on
     {
        if (p->base) // base is not NULL - has a free slot
          {
             if (p->prev)
               {
                  if (pool->last == p)
                     pool->last = p->prev;

                  p->prev->next = p->next;
                  p->prev = NULL;
                  p->next = pool->first;
                  p->next->prev = p;
                  pool->first = p;
               }

             break;
          }
     }
   if (!p) // we have reached the end of the list - no free pools
     {
        p = _evas_mp_pool_new(pool);
        if (!p)
           return NULL;

        p->prev = NULL;
        p->next = pool->first;
        if (p->next)
           p->next->prev = p;

        if (!pool->last)
           pool->last = p;

        pool->first = p;
     }

   mem = p->base; // this points to the next free block - so take it
   p->base = *((void **)mem); // base now points to the next free block
   if (!p->base) // move to end - it just filled up
      if (p->next)
        {
           if (p->prev)
              p->prev->next = p->next;
           else
              pool->first = p->next;

           p->next->prev = p->prev;
           ((Pool *)pool->last)->next = p;
           p->prev = pool->last;
           p->next = NULL;
           pool->last = p;
        }

   p->usage++;
   pool->usage++;
   return mem;
#endif
}

void
evas_mempool_free(Evas_Mempool *pool, void *ptr)
{
#ifdef NOPOOL
   free(ptr);
#else
   Pool *p;
   void *pmem;
   int item_alloc, psize;

   item_alloc =
      ((pool->item_size + sizeof(void *) - 1) / sizeof(void *)) * sizeof(void *);
   psize = item_alloc * pool->pool_size;
   for (p = (Pool *)(pool->first); p; p = p->next) // look 4 pool
     {
        pmem = (void *)(((unsigned char *)p) + sizeof(Pool)); // pool mem base
        if ((ptr >= pmem) &&
            ((unsigned char *)ptr < (((unsigned char *)pmem) + psize))) // is it in pool mem?
          {
             *((void **)ptr) = p->base; // freed node points to prev free node
             p->base = ptr; // next free node is now the one we freed
             p->usage--;
             pool->usage--;
             if (p->usage == 0) // free bucket
               {
                  if (p->prev)
                     p->prev->next = p->next;

                  if (p->next)
                     p->next->prev = p->prev;

                  if (pool->last == p)
                     pool->last = p->prev;

                  if (pool->first == p)
                     pool->first = p->next;

                  _evas_mp_pool_free(p);
               }
             else if (p->prev) // if not the first - move to front
               {
                  p->prev->next = p->next;
                  if (p->next)
                     p->next->prev = p->prev;

                  if (pool->last == p)
                     pool->last = p->prev;

                  p->prev = NULL;
                  p->next = pool->first;
                  p->next->prev = p;
                  pool->first = p;
               }

             break;
          }
     }
#endif
}


void *
evas_mempool_calloc(Evas_Mempool *pool, int size)
{
#ifdef NOPOOL
   return calloc(1, size);
#else
   void *mem;

   mem = evas_mempool_malloc(pool, size);
   memset(mem, 0, size);
   return mem;
#endif
}