Second Extended Filesystem (ext2) and other improvements (#33)
[lunaix-os.git] / lunaix-os / kernel / mm / valloc.c
1 #include <klibc/string.h>
2 #include <lunaix/mm/cake.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
5
6 #define CLASS_LEN(class) (sizeof(class) / sizeof(class[0]))
7
8 static char piles_names[][PILE_NAME_MAXLEN] = 
9 {
10     "valloc_8",   "valloc_16",  "valloc_32",  "valloc_64",
11     "valloc_128", "valloc_256", "valloc_512", "valloc_1k",
12     "valloc_2k",  "valloc_4k",  "valloc_8k"  
13 };
14
15 static char piles_names_dma[][PILE_NAME_MAXLEN] = 
16 {
17     "valloc_dma_128", "valloc_dma_256", "valloc_dma_512",
18     "valloc_dma_1k",  "valloc_dma_2k",  "valloc_dma_4k"
19 };
20
21 static struct cake_pile* piles[CLASS_LEN(piles_names)];
22 static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
23
24 void
25 valloc_init()
26 {
27     for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
28         int size = 1 << (i + 3);
29         piles[i] = cake_new_pile(piles_names[i], size, size > 1024 ? 8 : 1, 0);
30     }
31
32     // DMA 内存保证128字节对齐
33     for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
34         int size = 1 << (i + 7);
35         piles_dma[i] = cake_new_pile(
36             piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_ALIGN_CACHE);
37     }
38 }
39
40 void*
41 __valloc(unsigned int size,
42          struct cake_pile** segregate_list,
43          size_t len,
44          size_t boffset)
45 {
46     size_t i = ilog2(size);
47     i += (size - (1 << i) != 0);
48     i -= boffset;
49
50     if (i >= len)
51         i = 0;
52
53     return cake_grab(segregate_list[i]);
54 }
55
56 void
57 __vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
58 {
59     size_t i = 0;
60     for (; i < len; i++) {
61         if (cake_release(segregate_list[i], ptr)) {
62             return;
63         }
64     }
65 }
66
67 void*
68 valloc(unsigned int size)
69 {
70     return __valloc(size, piles, CLASS_LEN(piles_names), 3);
71 }
72
73 void*
74 vzalloc(unsigned int size)
75 {
76     void* ptr = __valloc(size, piles, CLASS_LEN(piles_names), 3);
77     memset(ptr, 0, size);
78     return ptr;
79 }
80
81 void*
82 vcalloc(unsigned int size, unsigned int count)
83 {
84     unsigned int alloc_size;
85     if (umul_of(size, count, &alloc_size)) {
86         return 0;
87     }
88
89     void* ptr = __valloc(alloc_size, piles, CLASS_LEN(piles_names), 3);
90     memset(ptr, 0, alloc_size);
91     return ptr;
92 }
93
94 void
95 vfree(void* ptr)
96 {
97     __vfree(ptr, piles, CLASS_LEN(piles_names));
98 }
99
100 void
101 vfree_safe(void* ptr)
102 {
103     if (!ptr) {
104         return;
105     }
106
107     __vfree(ptr, piles, CLASS_LEN(piles_names));
108 }
109
110 void*
111 valloc_dma(unsigned int size)
112 {
113     return __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
114 }
115
116 void*
117 vzalloc_dma(unsigned int size)
118 {
119     void* ptr = __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
120     memset(ptr, 0, size);
121     return ptr;
122 }
123
124 void
125 vfree_dma(void* ptr)
126 {
127     __vfree(ptr, piles_dma, CLASS_LEN(piles_names_dma));
128 }
129
130 inline void must_inline
131 valloc_ensure_valid(void* ptr) {
132     cake_ensure_valid(ptr);
133 }