summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_inline_mempool.x
blob: b0ab81587d63dfcdbe3508600463d879d0a2427d (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
/* EINA - EFL data type library
 * Copyright (C) 2008 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/>.
 */

#ifndef EINA_INLINE_MEMPOOL_X_
#define EINA_INLINE_MEMPOOL_X_

#include <string.h>

/* Memory Pool */
typedef struct _Eina_Mempool_Backend_ABI1 Eina_Mempool_Backend_ABI1;
typedef struct _Eina_Mempool_Backend_ABI2 Eina_Mempool_Backend_ABI2;

struct _Eina_Mempool_Backend
{
   /** Name of the mempool backend */
   const char *name;
   /** Function to initialize the backend. */
   void *(*init)(const char *context, const char *options, va_list args);
   /** Function to free memory back to the mempool. */
   void (*free)(void *data, void *element);
   /** Function to allocate memory from the mempool. */
   void *(*alloc)(void *data, unsigned int size);
   /** Function to change the size of a block of memory that is currently
    * allocated. */
   void *(*realloc)(void *data, void *element, unsigned int size);
   /** Function to trigger a garbage collection; can be NULL if the feature
    * isn't available in the backend. */
   void (*garbage_collect)(void *data);
   /** Report statistics on the content of the mempool; can be NULL if the
    * feature isn't available in the backend. */
   void (*statistics)(void *data);
   /** Function to destroy the backend, freeing memory back to the operating
    * system. */
   void (*shutdown)(void *data);
   /** Function to optimize the placement of objects in the mempool (it's
    * different from garbage_collect); can be NULL if the feature isn't
    * available in the backend.
    * @see Eina_Mempool_Repack_Cb */
   void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
   /** Function to check is a valid element from a mempool.
    * @see eina_mempool_from
    */
   Eina_Bool (*from)(void *data, void *element);
   /** Function to get an Eina_Iterator that will walk every allocated element
    * in the pool.
    * @use eina_mempool_iterator_new
    */
   Eina_Iterator *(*iterator)(void *data);
   /** Function to allocate memory near already allocated memory.
    * @since 1.24
    * @use eina_mempool_malloc_near
    */
   void *(*alloc_near)(void *data, void *after, void *before, unsigned int size);
};

struct _Eina_Mempool_Backend_ABI1
{
   const char *name;
   void *(*init)(const char *context, const char *options, va_list args);
   void (*free)(void *data, void *element);
   void *(*alloc)(void *data, unsigned int size);
   void *(*realloc)(void *data, void *element, unsigned int size);
   void (*garbage_collect)(void *data);
   void (*statistics)(void *data);
   void (*shutdown)(void *data);
};

struct _Eina_Mempool_Backend_ABI2
{
   void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
   Eina_Bool (*from)(void *data, void *element);
   Eina_Iterator *(*iterator)(void *data);
   void *(*alloc_near)(void *data, void *after, void *before, unsigned int size);
};

struct _Eina_Mempool
{
   Eina_Mempool_Backend_ABI1 backend;
   void *backend_data;
   Eina_Mempool_Backend_ABI2 *backend2;
};

static inline void *
eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size)
{
   return mp->backend.realloc(mp->backend_data, element, size);
}

static inline void *
eina_mempool_malloc(Eina_Mempool *mp, unsigned int size)
{
   return mp->backend.alloc(mp->backend_data, size);
}

static inline void *
eina_mempool_malloc_near(Eina_Mempool *mp, void *after, void *before, unsigned int size)
{
   if (mp->backend2 && mp->backend2->alloc_near && (!(after == NULL && before == NULL)))
     return mp->backend2->alloc_near(mp->backend_data, after, before, size);
   return mp->backend.alloc(mp->backend_data, size);
}

static inline void *
eina_mempool_calloc(Eina_Mempool *mp, unsigned int size)
{
   void *r = mp->backend.alloc(mp->backend_data, size);
   if (r) memset(r, 0, size);
   return r;
}

static inline void
eina_mempool_free(Eina_Mempool *mp, void *element)
{
   if (element) mp->backend.free(mp->backend_data, element);
}

static inline Eina_Bool
eina_mempool_from(Eina_Mempool *mp, void *element)
{
   if (!element) return EINA_FALSE;
   return mp->backend2->from(mp->backend_data, element);
}

static inline Eina_Iterator *
eina_mempool_iterator_new(Eina_Mempool *mp)
{
   if (!mp->backend2->iterator) return NULL;
   return mp->backend2->iterator(mp->backend_data);
}

static inline unsigned int
eina_mempool_alignof(unsigned int size)
{
   unsigned int align;

#if __WORDSIZE == 32
   if (size >= 8)
     {
        align = 8;
calc:
        return ((size / align) + (size % align ? 1 : 0)) * align;
     }
#else // __WORDSIZE == 64
   if (size >= 16)
     {
        align = 16;
calc:
        return ((size / align) + (size % align ? 1 : 0)) * align;
     }
   else if (size >= 8)
     {
        align = 8;
        goto calc;
     }
#endif
   else if (size >= 4)
     {
        align = 4;
        goto calc;
     }
   align = 2;
   goto calc;
}

#endif