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 // threshold to use external cake metadata
9 #define EXTERN_THRESHOLD 128
11 static char piles_names[][PILE_NAME_MAXLEN] =
13 "valloc_8", "valloc_16", "valloc_32", "valloc_64",
14 "valloc_128", "valloc_256", "valloc_512", "valloc_1k",
15 "valloc_2k", "valloc_4k", "valloc_8k"
19 #define M1K (M128 + 3)
21 static int page_counts[] =
36 static char piles_names_dma[][PILE_NAME_MAXLEN] =
38 "valloc_dma_128", "valloc_dma_256", "valloc_dma_512",
39 "valloc_dma_1k", "valloc_dma_2k", "valloc_dma_4k"
42 static struct cake_pile* piles[CLASS_LEN(piles_names)];
43 static struct cake_pile* piles_dma[CLASS_LEN(piles_names_dma)];
49 for (size_t i = 0; i < CLASS_LEN(piles_names); i++) {
50 int size = 1 << (i + 3);
51 if (size >= EXTERN_THRESHOLD) {
52 opts |= PILE_FL_EXTERN;
54 piles[i] = cake_new_pile(piles_names[i], size, page_counts[i], opts);
57 opts = PILE_ALIGN_CACHE;
59 for (size_t i = 0; i < CLASS_LEN(piles_names_dma); i++) {
60 int size = 1 << (i + 7);
61 if (size >= EXTERN_THRESHOLD) {
62 opts |= PILE_FL_EXTERN;
64 piles_dma[i] = cake_new_pile(
65 piles_names_dma[i], size, page_counts[M128 + i], opts);
70 __valloc(unsigned int size,
71 struct cake_pile** segregate_list,
75 size_t i = ilog2(size);
76 i += (size - (1 << i) != 0);
82 return cake_grab(segregate_list[i]);
86 __vfree(void* ptr, struct cake_pile** segregate_list, size_t len)
89 for (; i < len; i++) {
90 if (cake_release(segregate_list[i], ptr)) {
97 valloc(unsigned int size)
99 return __valloc(size, piles, CLASS_LEN(piles_names), 3);
103 vzalloc(unsigned int size)
105 void* ptr = __valloc(size, piles, CLASS_LEN(piles_names), 3);
106 memset(ptr, 0, size);
111 vcalloc(unsigned int size, unsigned int count)
113 unsigned int alloc_size;
114 if (umul_of(size, count, &alloc_size)) {
118 void* ptr = __valloc(alloc_size, piles, CLASS_LEN(piles_names), 3);
119 memset(ptr, 0, alloc_size);
126 __vfree(ptr, piles, CLASS_LEN(piles_names));
130 vfree_safe(void* ptr)
136 __vfree(ptr, piles, CLASS_LEN(piles_names));
140 valloc_dma(unsigned int size)
142 return __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
146 vzalloc_dma(unsigned int size)
148 void* ptr = __valloc(size, piles_dma, CLASS_LEN(piles_names_dma), 7);
149 memset(ptr, 0, size);
156 __vfree(ptr, piles_dma, CLASS_LEN(piles_names_dma));
159 inline void must_inline
160 valloc_ensure_valid(void* ptr) {
161 cake_ensure_valid(ptr);