1 #include <klibc/string.h>
2 #include <lunaix/mm/cake.h>
3 #include <lunaix/mm/valloc.h>
4 #include <lunaix/spike.h>
6 #define CLASS_LEN(class) (sizeof(class) / sizeof(class[0]))
8 static char piles_names[][PILE_NAME_MAXLEN] = {
9 "valloc_8", "valloc_16", "valloc_32", "valloc_64",
10 "valloc_128", "valloc_256", "valloc_512", "valloc_1k",
11 "valloc_2k", "valloc_4k", "valloc_8k"
14 static char piles_names_dma[][PILE_NAME_MAXLEN] = {
15 "valloc_dma_128", "valloc_dma_256", "valloc_dma_512",
16 "valloc_dma_1k", "valloc_dma_2k", "valloc_dma_4k"
19 static struct cake_pile* piles[CLASS_LEN(piles_names)];
20 static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
25 for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
26 int size = 1 << (i + 3);
27 piles[i] = cake_new_pile(&piles_names[i], size, size > 1024 ? 8 : 1, 0);
31 for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
32 int size = 1 << (i + 7);
33 piles_dma[i] = cake_new_pile(
34 &piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_CACHELINE);
39 __valloc(unsigned int size,
40 struct cake_pile** segregate_list,
44 size_t i = ILOG2(size);
45 i += (size - (1 << i) != 0);
51 return cake_grab(segregate_list[i]);
55 __vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
58 for (; i < len; i++) {
59 if (cake_release(segregate_list[i], ptr)) {
66 valloc(unsigned int size)
68 return __valloc(size, &piles, CLASS_LEN(piles_names), 3);
72 vzalloc(unsigned int size)
74 void* ptr = __valloc(size, &piles, CLASS_LEN(piles_names), 3);
80 vcalloc(unsigned int size, unsigned int count)
82 unsigned int alloc_size;
83 if (__builtin_umul_overflow(size, count, &alloc_size)) {
87 void* ptr = __valloc(alloc_size, &piles, CLASS_LEN(piles_names), 3);
88 memset(ptr, 0, alloc_size);
95 __vfree(ptr, &piles, CLASS_LEN(piles_names));
99 valloc_dma(unsigned int size)
101 return __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma), 7);
105 vzalloc_dma(unsigned int size)
107 void* ptr = __valloc(size, &piles_dma, CLASS_LEN(piles_names_dma), 7);
108 memset(ptr, 0, size);
115 __vfree(ptr, &piles_dma, CLASS_LEN(piles_names_dma));