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] = {"valloc_8",
20 static char piles_names_dma[][PILE_NAME_MAXLEN] = {"valloc_dma_128",
27 static struct cake_pile* piles[CLASS_LEN(piles_names)];
28 static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
33 for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
34 int size = 1 << (i + 3);
35 piles[i] = cake_new_pile(piles_names[i], size, size > 1024 ? 8 : 1, 0);
39 for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
40 int size = 1 << (i + 7);
41 piles_dma[i] = cake_new_pile(
42 piles_names_dma[i], size, size > 1024 ? 4 : 1, PILE_ALIGN_CACHE);
47 __valloc(unsigned int size,
48 struct cake_pile** segregate_list,
52 size_t i = ILOG2(size);
53 i += (size - (1 << i) != 0);
59 return cake_grab(segregate_list[i]);
63 __vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
66 for (; i < len; i++) {
67 if (cake_release(segregate_list[i], ptr)) {
74 valloc(unsigned int size)
76 return __valloc(size, piles, CLASS_LEN(piles_names), 3);
80 vzalloc(unsigned int size)
82 void* ptr = __valloc(size, piles, CLASS_LEN(piles_names), 3);
88 vcalloc(unsigned int size, unsigned int count)
90 unsigned int alloc_size;
91 if (umul_overflow(size, count, &alloc_size)) {
95 void* ptr = __valloc(alloc_size, piles, CLASS_LEN(piles_names), 3);
96 memset(ptr, 0, alloc_size);
103 __vfree(ptr, piles, CLASS_LEN(piles_names));
107 vfree_safe(void* ptr)
113 __vfree(ptr, piles, CLASS_LEN(piles_names));
117 valloc_dma(unsigned int size)
119 return __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
123 vzalloc_dma(unsigned int size)
125 void* ptr = __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
126 memset(ptr, 0, size);
133 __vfree(ptr, piles_dma, CLASS_LEN(piles_names_dma));