Cleans up smallpool, adds locking
[akaros.git] / kern / include / pool.h
1 /* See COPYRIGHT for copyright information. */
2 /* Kevin Klues <klueska@cs.berkeley.edu>        */
3
4 #ifndef ROS_INC_POOL_H
5 #define ROS_INC_POOL_H
6
7 #include <string.h>
8
9 #define POOL_TYPE_DEFINE(_type, p, sz)                                                \
10 typedef struct struct_##p {                                                             \
11         uint32_t size;                                                         \
12         uint32_t free;                                                         \
13         uint32_t index;                                                        \
14         _type* queue[(sz)];                                                       \
15         _type pool[(sz)];                                                         \
16 } p##_t;
17
18 #define POOL_INIT(p, sz)                                                       \
19 ({                                                                             \
20         (p)->size = (sz);                                                          \
21         (p)->free = (sz);                                                          \
22         (p)->index = 0;                                                            \
23         memset((p)->pool, 0, (sz) * sizeof((p)->pool[0]));                         \
24         for(int i=0; i<(p)->size; i++) {                                           \
25                 (p)->queue[i] = &((p)->pool[i]);                                       \
26         }                                                                          \
27 })
28
29 #define POOL_GET(p)                                            \
30 ({                                                             \
31         void* rval = NULL;                                         \
32         if((p)->free) {                                            \
33                 rval = (p)->queue[(p)->index];                         \
34                 (p)->queue[(p)->index] = NULL;                         \
35                 (p)->free--;                                           \
36                 (p)->index++;                                          \
37                 if((p)->index == (p)->size) {                          \
38                 (p)->index = 0;                                    \
39         }                                                      \
40         }                                                          \
41         rval;                                                      \
42 })
43
44 // emptyIndex is also the first element that has been allocated, iterate thru to index-1
45
46 #define POOL_FOR_EACH(p, func)                                                                  \
47 ({                                                                                                                              \
48         int emptyIndex = ((p)->index + (p)->free);                  \
49         if (emptyIndex >= (p)->size) {                                          \
50                 emptyIndex -= (p)->size;                                        \
51         }                                                                   \
52         for(int _i = emptyIndex;  _i < (p)->index; _i++){                       \
53                 func((p)->queue[_i]);                                                                   \
54         }                                                                                                                       \
55 })                                                                                                                              \
56
57 #define POOL_PUT(p, val)                                                       \
58 ({                                                                             \
59         int rval = -1;                                                            \
60         if((p)->free < (p)->size) {                                           \
61                 int emptyIndex = ((p)->index + (p)->free);                     \
62                 if (emptyIndex >= (p)->size) {                                 \
63                         emptyIndex -= (p)->size;                               \
64                 }                                                              \
65                 (p)->queue[emptyIndex] = val;                                  \
66                 (p)->free++;                                                   \
67                 rval = 1;                                                             \
68         }                                                                      \
69         rval;                                                                 \
70 })
71
72 #define POOL_EMPTY(p) ((p)->free == 0)
73 #define POOL_SIZE(p) ((p)->free)
74 #define POOL_MAX_SIZE(p) ((p)->size)
75
76 #endif //ROS_INC_POOL_H