EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / include / eina_mempool.h
1 /* EINA - EFL data type library
2  * Copyright (C) 2007-2008 Jorge Luis Zapata Muga
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_MEMPOOL_H_
20 #define EINA_MEMPOOL_H_
21
22 #include "eina_types.h"
23 #include "eina_error.h"
24 #include "eina_module.h"
25
26
27 /**
28  * @addtogroup Eina_Memory_Pool_Group Memory Pool
29  *
30  * @brief These functions provide memory pool management.
31  *
32  * Several mempool are available:
33  *
34  * @li @c buddy: It uses the
35  * <a href="http://en.wikipedia.org/wiki/Buddy_memory_allocation">"buddy
36  * allocator" algorithm</a> but the Eina implementation differs in the
37  * sense that the chunk information is not stored on the chunk itself,
38  * but on another memory area. This is useful for cases where the
39  * memory to manage might be slower to access, or limited (like video
40  * memory).
41  * @li @c chained_pool: It is the default one. It allocates a big
42  * chunk of memory with malloc() and split the result in chunks of the
43  * requested size that are pushed inside a stack. When requested, it
44  * takes this pointer from the stack to give them to whoever wants
45  * them.
46  * @li @c ememoa_fixed and @c ememoa_unknown: experimental allocators
47  * which could be useful when a fixed amount of memory is needed.
48  * @li @c fixed_bitmap: It allocates with malloc) 32* the requested
49  * size and push the pool pointer in an rbtree. To find empty space in
50  * a pool, it will just search for the first bit set in an int (32
51  * bits). Then, when a pointer is freed, it will do a search inside
52  * the rbtree.
53  * @li @c pass_through: it just call malloc() and free(). It may be
54  * faster on some computers than using our own allocators (like having
55  * a huge L2 cache, over 4MB).
56  * @li @c one_big: It call just one time malloc for the requested number
57  * of items. Useful when you know in advance how many object of some
58  * type will live during the life of the mempool.
59  */
60
61 /**
62  * @addtogroup Eina_Tools_Group Tools
63  *
64  * @{
65  */
66
67 /**
68  * @defgroup Eina_Memory_Pool_Group Memory Pool
69  *
70  * @{
71  */
72
73 /**
74  * @typedef Eina_Mempool
75  * Mempool type.
76  */
77 typedef struct _Eina_Mempool Eina_Mempool;
78
79 /**
80  * @typedef Eina_Mempool_Backend
81  * Mempool backend type.
82  */
83 typedef struct _Eina_Mempool_Backend Eina_Mempool_Backend;
84
85
86 /**
87  * @typedef Eina_Mempool_Repack_Cb
88  * Type for a callback who need to unreference an old object from a mempool
89  * and reference the new one instead. Memcpy is taken care by the mempool.
90  */
91 typedef void (*Eina_Mempool_Repack_Cb)(void *dst, void *src, void *data);
92
93 EAPI extern Eina_Error EINA_ERROR_NOT_MEMPOOL_MODULE;
94
95 EAPI Eina_Mempool  *eina_mempool_add(const char *module, const char *context, const char *options, ...) EINA_MALLOC EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
96 EAPI void           eina_mempool_del(Eina_Mempool *mp) EINA_ARG_NONNULL(1);
97
98 static inline void *eina_mempool_realloc(Eina_Mempool *mp, void *element, unsigned int size) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
99 static inline void *eina_mempool_malloc(Eina_Mempool *mp, unsigned int size) EINA_MALLOC EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT;
100 static inline void  eina_mempool_free(Eina_Mempool *mp, void *element) EINA_ARG_NONNULL(1);
101
102 EAPI void           eina_mempool_repack(Eina_Mempool *mp,
103                                         Eina_Mempool_Repack_Cb cb,
104                                         void *data) EINA_ARG_NONNULL(1, 2);
105 EAPI void           eina_mempool_gc(Eina_Mempool *mp) EINA_ARG_NONNULL(1);
106 EAPI void           eina_mempool_statistics(Eina_Mempool *mp) EINA_ARG_NONNULL(1);
107
108 EAPI Eina_Bool      eina_mempool_register(Eina_Mempool_Backend *be) EINA_ARG_NONNULL(1);
109 EAPI void           eina_mempool_unregister(Eina_Mempool_Backend *be) EINA_ARG_NONNULL(1);
110
111 EAPI unsigned int   eina_mempool_alignof(unsigned int size);
112
113 #include "eina_inline_mempool.x"
114
115 /**
116  * @}
117  */
118
119 /**
120  * @}
121  */
122
123 #endif /* EINA_MEMPOOL_H_ */