1 #include <klibc/string.h>
2 #include <lunaix/mm/cake.h>
3 #include <lunaix/mm/valloc.h>
5 #define CLASS_LEN(class) (sizeof(class) / sizeof(class[0]))
7 static char piles_names[][PILE_NAME_MAXLEN] = {
8 "valloc_8", "valloc_16", "valloc_32", "valloc_64", "valloc_128",
9 "valloc_256", "valloc_512", "valloc_1k", "valloc_2k", "valloc_4k"
12 static char piles_names_dma[][PILE_NAME_MAXLEN] = {
13 "valloc_dma_128", "valloc_dma_256", "valloc_dma_512",
14 "valloc_dma_1k", "valloc_dma_2k", "valloc_dma_4k"
17 static struct cake_pile* piles[CLASS_LEN(piles_names)];
18 static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
23 for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
24 int size = 1 << (i + 3);
25 piles[i] = cake_new_pile(&piles_names[i], size, size > 1024 ? 4 : 1, 0);
29 for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
30 int size = 1 << (i + 7);
31 piles_dma[i] = cake_new_pile(
32 &piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_CACHELINE);
37 __valloc(unsigned int size, struct cake_pile** segregate_list, size_t len)
40 for (; i < len; i++) {
41 if (segregate_list[i]->piece_size >= size) {
49 return cake_grab(segregate_list[i]);
53 __vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
56 for (; i < len; i++) {
57 if (cake_release(segregate_list[i], ptr)) {
64 valloc(unsigned int size)
66 return __valloc(size, &piles, CLASS_LEN(piles_names));
70 vzalloc(unsigned int size)
72 void* ptr = __valloc(size, &piles, CLASS_LEN(piles_names));
78 vcalloc(unsigned int size, unsigned int count)
80 unsigned int alloc_size;
81 if (__builtin_umul_overflow(size, count, &alloc_size)) {
85 void* ptr = __valloc(alloc_size, &piles, CLASS_LEN(piles_names));
86 memset(ptr, 0, alloc_size);
93 __vfree(ptr, &piles, CLASS_LEN(piles_names));
97 valloc_dma(unsigned int size)
99 return __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma));
103 vzalloc_dma(unsigned int size)
105 void* ptr = __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma));
106 memset(ptr, 0, size);
113 __vfree(ptr, &piles_dma, CLASS_LEN(piles_names_dma));