doc: Add pool module documentation.
[bertos.git] / bertos / struct / pool.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Pool macros.
33  *
34  * \author Giovanni Bajo <rasky@develer.com>
35  */
36
37 #ifndef STRUCT_POOL_H
38 #define STRUCT_POOL_H
39
40 #include <cfg/macros.h>
41 #include <struct/list.h>
42
43 #define EXTERN_POOL(name) \
44         extern List name
45
46 #define DECLARE_POOL_WITH_STORAGE(name, type, num, storage) \
47         static type name##_items[num]; \
48         storage name; \
49         INLINE void name##_init(void (*init_func)(type*)) \
50         { \
51                 size_t i; \
52                 LIST_INIT(&name); \
53                 for (i=0;i<countof(name##_items);++i) \
54                 { \
55                         if (init_func) init_func(&name##_items[i]); \
56                         ADDTAIL(&name, (Node*)&name##_items[i]); \
57                 } \
58         } \
59         INLINE void name##_init(void (*init_func)(type*)) \
60         /**/
61
62 /**
63  * \brief Helper macro to declare a Pool data type.
64  *
65  * Data type inserted into the pool must be a \code Node * \endcode
66  * type.
67  *
68  * \param name Variable name of the pool.
69  * \param type Data type held by the pool.
70  * \param num Number of elements in pool.
71  */
72 #define DECLARE_POOL(name, type, num) \
73         DECLARE_POOL_WITH_STORAGE(name, type, num, List)
74
75 #define DECLARE_POOL_STATIC(name, type, num) \
76         DECLARE_POOL_WITH_STORAGE(name, type, num, static List)
77
78 #define pool_init(name, init_func)     (*(name##_init))(init_func)
79
80 /**
81  * \brief Allocate an element from the pool.
82  *
83  * The returned element is of type \code Node * \endcode, it's safe to
84  * cast it to the type contained in the pool.
85  *
86  * \note If the element was recycled with pool_free(), it will not be reset,
87  * so don't assume that the element has specific values.
88  *
89  * \param name Pointer to pool to alloc from.
90  * \return Element of the type present in the pool.
91  */
92 #define pool_alloc(name)               list_remHead(name)
93
94 /**
95  * \brief Recycle an element into the pool
96  *
97  * \note Element fields are not reset to its original values, keep that in
98  * mind when you alloc nodes.
99  *
100  * \param name Pointer to pool where the node should be recycled.
101  * \param elem Element to be recycled.
102  */
103 #define pool_free(name, elem)          ADDHEAD(name, (Node*)elem)
104
105 /**
106  * \brief Test if the pool is empty
107  *
108  * \param name Pointer to pool.
109  * \return True if the pool is empty, false otherwise.
110  */
111 #define pool_empty(name)               LIST_EMPTY(name)
112
113 #endif /* STRUCT_POOL_H */