Import changes from sc/firmware.
[bertos.git] / mware / pool.h
1 #ifndef MWARE_POOL_H
2 #define MWARE_POOL_H
3
4 #include <macros.h>
5 #include <mware/list.h>
6
7 #define EXTERN_POOL(name) \
8         extern List name
9
10 #define DECLARE_POOL_WITH_STORAGE(name, type, num, storage) \
11         static type name##_items[num]; \
12         storage name; \
13         INLINE void name##_init(void (*init_func)(type*)) \
14         { \
15                 int i; \
16                 INITLIST(&name); \
17                 for (i=0;i<countof(name##_items);++i) \
18                 { \
19                         if (init_func) init_func(&name##_items[i]); \
20                         ADDTAIL(&name, (Node*)&name##_items[i]); \
21                 } \
22         } \
23         INLINE void name##_init(void (*init_func)(type*)) \
24         /**/
25
26 #define DECLARE_POOL(name, type, num) \
27         DECLARE_POOL_WITH_STORAGE(name, type, num, List)
28
29 #define DECLARE_POOL_STATIC(name, type, num) \
30         DECLARE_POOL_WITH_STORAGE(name, type, num, static List)
31
32 #define pool_init(name, init_func)     (*(name##_init))(init_func)
33 #define pool_alloc(name)               REMHEAD(name)
34 #define pool_free(name, elem)          ADDHEAD(name, (Node*)elem)
35 #define pool_empty(name)               ISLISTEMPTY(name)
36
37 #endif