Decoupling Architectural-specific Code (#35)
[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 1
10
11 struct cake_pile;
12
13 typedef void (*pile_cb)(struct cake_pile*, void*);
14
15 struct cake_pile
16 {
17     struct llist_header piles;
18     struct llist_header full;
19     struct llist_header partial;
20     struct llist_header free;
21     u32_t offset;
22     u32_t piece_size;
23     u32_t cakes_count;
24     u32_t alloced_pieces;
25     u32_t pieces_per_cake;
26     u32_t pg_per_cake;
27     char pile_name[PILE_NAME_MAXLEN+1];
28
29     pile_cb ctor;
30 };
31
32 typedef unsigned int piece_index_t;
33
34 #define EO_FREE_PIECE ((u32_t)-1)
35
36 struct cake_s
37 {
38     struct llist_header cakes;
39     void* first_piece;
40     unsigned int used_pieces;
41     unsigned int next_free;
42     piece_index_t free_list[0];
43 };
44
45 /**
46  * @brief 创建一个蛋糕堆
47  *
48  * @param name 堆名称
49  * @param piece_size 每个蛋糕切块儿的大小
50  * @param pg_per_cake 每个蛋糕所占据的页数
51  * @return struct cake_pile*
52  */
53 struct cake_pile*
54 cake_new_pile(char* name,
55               unsigned int piece_size,
56               unsigned int pg_per_cake,
57               int options);
58
59 void
60 cake_set_constructor(struct cake_pile* pile, pile_cb ctor);
61
62 /**
63  * @brief 拿一块儿蛋糕
64  *
65  * @param pile
66  * @return void*
67  */
68 void*
69 cake_grab(struct cake_pile* pile);
70
71 /**
72  * @brief 归还一块儿蛋糕
73  *
74  * @param pile
75  * @param area
76  */
77 int
78 cake_release(struct cake_pile* pile, void* area);
79
80 void
81 cake_init();
82
83 void
84 cake_export();
85
86 /********** some handy constructor ***********/
87
88 void
89 cake_ctor_zeroing(struct cake_pile* pile, void* piece);
90
91 #define DEADCAKE_MARK 0xdeadcafeUL
92
93 static inline void
94 cake_ensure_valid(void* area) {
95     if (unlikely(*(unsigned int*)area == DEADCAKE_MARK)) {
96         fail("access to freed cake piece");
97     }
98 }
99
100 #endif /* __LUNAIX_VALLOC_H */