feat: cake (slab) allocator, for dynamic continuous physical page allocation
[lunaix-os.git] / lunaix-os / includes / lunaix / mm / cake.h
1 #ifndef __LUNAIX_SLAB_H
2 #define __LUNAIX_SLAB_H
3
4 #include <lunaix/ds/llist.h>
5
6 #define PILE_NAME_MAXLEN 20
7
8 struct cake_pile
9 {
10     struct llist_header piles;
11     struct llist_header full;
12     struct llist_header partial;
13     struct llist_header free;
14     unsigned int piece_size;
15     unsigned int cakes_count;
16     unsigned int alloced_pieces;
17     unsigned int pieces_per_cake;
18     unsigned int pg_per_cake;
19     char pile_name[PILE_NAME_MAXLEN];
20 };
21
22 typedef unsigned int piece_index_t;
23
24 #define EO_FREE_PIECE (-1)
25
26 struct cake_s
27 {
28     struct llist_header cakes;
29     void* first_piece;
30     unsigned int used_pieces;
31     unsigned int next_free;
32     piece_index_t free_list[0];
33 };
34
35 /**
36  * @brief 创建一个堆
37  *
38  * @param name 堆名称
39  * @param piece_size 每个蛋糕上可以被切分的大小
40  * @param pg_per_cake 每个蛋糕所占据的页数
41  * @return struct cake_pile*
42  */
43 struct cake_pile*
44 cake_new_pile(char* name, unsigned int piece_size, unsigned int pg_per_cake);
45
46 /**
47  * @brief 拿一块儿蛋糕
48  *
49  * @param pile
50  * @return void*
51  */
52 void*
53 cake_grab(struct cake_pile* pile);
54
55 /**
56  * @brief 归还一块儿蛋糕
57  *
58  * @param pile
59  * @param area
60  */
61 int
62 cake_release(struct cake_pile* pile, void* area);
63
64 void
65 cake_init();
66
67 /**
68  * @brief 统计蛋糕数量 - 问问Pinkie :D
69  *
70  */
71 void
72 cake_stats();
73
74 #endif /* __LUNAIX_VALLOC_H */