1 /* EINA - EFL data type library
2 * Copyright (C) 2008 Cedric Bail
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.
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.
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/>.
19 #ifndef EINA_INLINE_MEMPOOL_X_
20 #define EINA_INLINE_MEMPOOL_X_
25 * @addtogroup Eina_Memory_Pool_Group Memory Pool
31 typedef struct _Eina_Mempool_Backend_ABI1 Eina_Mempool_Backend_ABI1;
32 typedef struct _Eina_Mempool_Backend_ABI2 Eina_Mempool_Backend_ABI2;
34 struct _Eina_Mempool_Backend
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);
47 struct _Eina_Mempool_Backend_ABI1
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);
59 struct _Eina_Mempool_Backend_ABI2
61 void (*repack)(void *data, Eina_Mempool_Repack_Cb cb, void *cb_data);
66 Eina_Mempool_Backend_ABI1 backend;
68 Eina_Mempool_Backend_ABI2 *backend2;
72 * @brief Re-allocate an amount memory by the given mempool.
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.
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.
84 eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size)
86 return mp->backend.realloc(mp->backend_data, element, size);
90 * @brief Allocate memory using the given mempool.
92 * @param mp The mempool.
93 * @param size The size in bytes to allocate.
94 * @return The newly allocated data.
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.
101 eina_mempool_malloc(Eina_Mempool *mp, unsigned int size)
103 return mp->backend.alloc(mp->backend_data, size);
107 * @brief Allocate and zero memory using the given mempool.
109 * @param mp The mempool.
110 * @param size The size in bytes to allocate.
111 * @return The newly allocated data.
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.
119 eina_mempool_calloc(Eina_Mempool *mp, unsigned int size)
121 void *r = mp->backend.alloc(mp->backend_data, size);
128 * @brief Free resources previously allocated by the given mempool.
130 * @param mp The mempool.
131 * @param element The data to free.
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.
139 eina_mempool_free(Eina_Mempool *mp, void *element)
141 if (!element) return ;
142 mp->backend.free(mp->backend_data, element);