make rule for user header file install
[lunaix-os.git] / lunaix-os / includes / lunaix / mm / cake.h
1 #ifndef __LUNAIX_CAKE_H
2 #define __LUNAIX_CAKE_H
3
4 #include <lunaix/ds/llist.h>
5 #include <lunaix/spike.h>
6
7 #define PILE_NAME_MAXLEN 20
8
9 #define PILE_ALIGN_CACHE 0b0001
10 #define PILE_FL_EXTERN   0b0010
11
12 struct cake_pile;
13
14 typedef void (*pile_cb)(struct cake_pile*, void*);
15
16 struct cake_pile
17 {
18     struct llist_header piles;
19     struct llist_header full;
20     struct llist_header partial;
21     struct llist_header free;
22     u32_t offset;
23     u32_t piece_size;
24     u32_t cakes_count;
25     u32_t alloced_pieces;
26     u32_t pieces_per_cake;
27     u32_t pg_per_cake;
28     u32_t options;
29     char pile_name[PILE_NAME_MAXLEN+1];
30
31     pile_cb ctor;
32 };
33
34 typedef unsigned short piece_t;
35
36 #define EO_FREE_PIECE ((piece_t)-1)
37
38 #define CAKE_FL_SIZE        128 
39 #define CAKE_FL_MAXLEN      \
40     ((unsigned int)((CAKE_FL_SIZE - sizeof(ptr_t)) / sizeof(piece_t)))
41 struct cake_fl
42 {
43     piece_t indices[CAKE_FL_MAXLEN];
44     struct cake_fl* next;
45 } align(CAKE_FL_SIZE);
46
47 struct cake_s
48 {
49     struct llist_header cakes;
50     struct cake_pile* owner;
51     void* first_piece;
52     unsigned int used_pieces;
53     unsigned int next_free;
54     union {
55         struct cake_fl* fl;
56         struct {
57             void* rsvd;
58             piece_t free_list[0];
59         };
60     };
61 };
62
63 /**
64  * @brief 创建一个蛋糕堆
65  *
66  * @param name 堆名称
67  * @param piece_size 每个蛋糕切块儿的大小
68  * @param pg_per_cake 每个蛋糕所占据的页数
69  * @return struct cake_pile*
70  */
71 struct cake_pile*
72 cake_new_pile(char* name,
73               unsigned int piece_size,
74               unsigned int pg_per_cake,
75               int options);
76
77 void
78 cake_set_constructor(struct cake_pile* pile, pile_cb ctor);
79
80 /**
81  * @brief 拿一块儿蛋糕
82  *
83  * @param pile
84  * @return void*
85  */
86 void*
87 cake_grab(struct cake_pile* pile);
88
89 /**
90  * @brief 归还一块儿蛋糕
91  *
92  * @param pile
93  * @param area
94  */
95 int
96 cake_release(struct cake_pile* pile, void* area);
97
98 void
99 cake_init();
100
101 void
102 cake_export();
103
104 void
105 cake_reclaim_freed();
106
107 /********** some handy constructor ***********/
108
109 void
110 cake_ctor_zeroing(struct cake_pile* pile, void* piece);
111
112 #define DEADCAKE_MARK 0xdeadcafeUL
113
114 static inline void
115 cake_ensure_valid(void* area) {
116     if (unlikely(*(unsigned int*)area == DEADCAKE_MARK)) {
117         fail("access to freed cake piece");
118     }
119 }
120
121 #endif /* __LUNAIX_VALLOC_H */