4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2004, 2008 Develer S.r.l. (http://www.develer.com/)
34 * \author Giovanni Bajo <rasky@develer.com>
40 #include <cfg/macros.h>
41 #include <struct/list.h>
43 #define EXTERN_POOL(name) \
46 #define DECLARE_POOL_WITH_STORAGE(name, type, num, storage) \
47 static type name##_items[num]; \
49 INLINE void name##_init(void (*init_func)(type*)) \
53 for (i=0;i<countof(name##_items);++i) \
55 if (init_func) init_func(&name##_items[i]); \
56 ADDTAIL(&name, (Node*)&name##_items[i]); \
59 INLINE void name##_init(void (*init_func)(type*)) \
63 * \brief Helper macro to declare a Pool data type.
65 * Data type inserted into the pool must be a \code Node * \endcode
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.
72 #define DECLARE_POOL(name, type, num) \
73 DECLARE_POOL_WITH_STORAGE(name, type, num, List)
75 #define DECLARE_POOL_STATIC(name, type, num) \
76 DECLARE_POOL_WITH_STORAGE(name, type, num, static List)
78 #define pool_init(name, init_func) (*(name##_init))(init_func)
81 * \brief Allocate an element from the pool.
83 * The returned element is of type \code Node * \endcode, it's safe to
84 * cast it to the type contained in the pool.
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.
89 * \param name Pointer to pool to alloc from.
90 * \return Element of the type present in the pool.
92 #define pool_alloc(name) list_remHead(name)
95 * \brief Recycle an element into the pool
97 * \note Element fields are not reset to its original values, keep that in
98 * mind when you alloc nodes.
100 * \param name Pointer to pool where the node should be recycled.
101 * \param elem Element to be recycled.
103 #define pool_free(name, elem) ADDHEAD(name, (Node*)elem)
106 * \brief Test if the pool is empty
108 * \param name Pointer to pool.
109 * \return True if the pool is empty, false otherwise.
111 #define pool_empty(name) LIST_EMPTY(name)
113 #endif /* STRUCT_POOL_H */