EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_inline_mempool.x
1 /* EINA - EFL data type library
2  * Copyright (C) 2008 Cedric Bail
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library;
16  * if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef EINA_INLINE_MEMPOOL_X_
20 #define EINA_INLINE_MEMPOOL_X_
21
22 #include <string.h>
23
24 /**
25  * @addtogroup Eina_Memory_Pool_Group Memory Pool
26  *
27  * @{
28  */
29
30 /* Memory Pool */
31 typedef struct _Eina_Mempool_Backend_ABI1 Eina_Mempool_Backend_ABI1;
32 typedef struct _Eina_Mempool_Backend_ABI2 Eina_Mempool_Backend_ABI2;
33
34 struct _Eina_Mempool_Backend
35 {
36    const char *name;
37    void *(*init)(const char *context, const char *options, va_list args);
38    void (*free)(void *data, void *element);
39    void *(*alloc)(void *data, unsigned int size);
40    void *(*realloc)(void *data, void *element, unsigned int size);
41    void (*garbage_collect)(void *data);
42    void (*statistics)(void *data);
43    void (*shutdown)(void *data);
44    void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
45 };
46
47 struct _Eina_Mempool_Backend_ABI1
48 {
49    const char *name;
50    void *(*init)(const char *context, const char *options, va_list args);
51    void (*free)(void *data, void *element);
52    void *(*alloc)(void *data, unsigned int size);
53    void *(*realloc)(void *data, void *element, unsigned int size);
54    void (*garbage_collect)(void *data);
55    void (*statistics)(void *data);
56    void (*shutdown)(void *data);
57 };
58
59 struct _Eina_Mempool_Backend_ABI2
60 {
61    void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
62 };
63
64 struct _Eina_Mempool
65 {
66    Eina_Mempool_Backend_ABI1 backend;
67    void *backend_data;
68    Eina_Mempool_Backend_ABI2 *backend2;
69 };
70
71 /**
72  * @brief Re-allocate an amount memory by the given mempool.
73  *
74  * @param mp The mempool.
75  * @param element The element to re-allocate.
76  * @param size The size in bytes to re-allocate.
77  * @return The newly re-allocated data.
78  *
79  * This function re-allocates and returns @p element with @p size bytes using the
80  * mempool @p mp. If not used anymore, the data must be freed with eina_mempool_free().
81  * @warning No checks are done for @p mp.
82  */
83 static inline void *
84 eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size)
85 {
86    return mp->backend.realloc(mp->backend_data, element, size);
87 }
88
89 /**
90  * @brief Allocate memory using the given mempool.
91  *
92  * @param mp The mempool.
93  * @param size The size in bytes to allocate.
94  * @return The newly allocated data.
95  *
96  * This function allocates and returns @p size bytes using the mempool @p mp.
97  * If not used anymore, the data must be freed with eina_mempool_free().
98  * @warning No checks are done for @p mp.
99  */
100 static inline void *
101 eina_mempool_malloc(Eina_Mempool *mp, unsigned int size)
102 {
103    return mp->backend.alloc(mp->backend_data, size);
104 }
105
106 /**
107  * @brief Allocate and zero memory using the given mempool.
108  *
109  * @param mp The mempool.
110  * @param size The size in bytes to allocate.
111  * @return The newly allocated data.
112  *
113  * This function allocates, zeroes, and returns @p size bytes using the mempool @p mp.
114  * If not used anymore, the data must be freed with eina_mempool_free().
115  * @warning No checks are done for @p mp.
116  * @since 1.2
117  */
118 static inline void *
119 eina_mempool_calloc(Eina_Mempool *mp, unsigned int size)
120 {
121    void *r = mp->backend.alloc(mp->backend_data, size);
122    if (!r) return NULL;
123    memset(r, 0, size);
124    return r;
125 }
126
127 /**
128  * @brief Free resources previously allocated by the given mempool.
129  *
130  * @param mp The mempool.
131  * @param element The data to free.
132  *
133  * This function frees @p element allocated by @p mp. @p element must
134  * have been obtained from eina_mempool_malloc(), eina_mempool_calloc(), or
135  * eina_mempool_realloc().
136  * @warning No checks are done for @p mp.
137  */
138 static inline void
139 eina_mempool_free(Eina_Mempool *mp, void *element)
140 {
141    if (!element) return ;
142    mp->backend.free(mp->backend_data, element);
143 }
144
145 /**
146  * @}
147  */
148
149 #endif