3 * @author Lunaixsky (zelong56@gmail.com)
4 * @brief A simplified cake(slab) allocator.
5 * P.s. I call it cake as slab sounds more 'ridge' to me. :)
9 * @copyright Copyright (c) 2022
13 #include <klibc/string.h>
14 #include <lunaix/mm/cake.h>
15 #include <lunaix/mm/pmm.h>
16 #include <lunaix/mm/vmm.h>
17 #include <lunaix/spike.h>
18 #include <lunaix/syslog.h>
22 #define CACHE_LINE_SIZE 128
24 struct cake_pile master_pile;
26 struct llist_header piles = { .next = &piles, .prev = &piles };
29 __alloc_cake(unsigned int cake_pg)
31 uintptr_t pa = pmm_alloc_cpage(KERNEL_PID, cake_pg, 0);
35 return vmm_vmap(pa, cake_pg * PG_SIZE, PG_PREM_RW);
39 __new_cake(struct cake_pile* pile)
41 struct cake_s* cake = __alloc_cake(pile->pg_per_cake);
47 int max_piece = pile->pieces_per_cake;
49 cake->first_piece = (void*)((uintptr_t)cake + pile->offset);
52 piece_index_t* free_list = cake->free_list;
53 for (size_t i = 0; i < max_piece - 1; i++) {
56 free_list[max_piece - 1] = EO_FREE_PIECE;
58 llist_append(&pile->free, &cake->cakes);
64 __init_pile(struct cake_pile* pile,
66 unsigned int piece_size,
67 unsigned int pg_per_cake,
74 unsigned int offset = sizeof(long);
77 if ((options & PILE_CACHELINE)) {
78 // 对齐到128字节缓存行大小,主要用于DMA
79 offset = CACHE_LINE_SIZE;
82 piece_size = ROUNDUP(piece_size, offset);
83 *pile = (struct cake_pile){ .piece_size = piece_size,
86 (pg_per_cake * PG_SIZE) /
87 (piece_size + sizeof(piece_index_t)),
88 .pg_per_cake = pg_per_cake };
90 unsigned int free_list_size = pile->pieces_per_cake * sizeof(piece_index_t);
92 pile->offset = ROUNDUP(sizeof(struct cake_s) + free_list_size, offset);
93 pile->pieces_per_cake -= ICEIL((pile->offset - free_list_size), piece_size);
95 strncpy(pile->pile_name, name, PILE_NAME_MAXLEN);
97 llist_init_head(&pile->free);
98 llist_init_head(&pile->full);
99 llist_init_head(&pile->partial);
100 llist_append(&piles, &pile->piles);
106 __init_pile(&master_pile, "pinkamina", sizeof(master_pile), 1, 0);
110 cake_new_pile(char* name,
111 unsigned int piece_size,
112 unsigned int pg_per_cake,
115 struct cake_pile* pile = (struct cake_pile*)cake_grab(&master_pile);
117 __init_pile(pile, name, piece_size, pg_per_cake, options);
123 cake_grab(struct cake_pile* pile)
125 struct cake_s *pos, *n;
126 if (!llist_empty(&pile->partial)) {
127 pos = list_entry(pile->partial.next, typeof(*pos), cakes);
128 } else if (llist_empty(&pile->free)) {
129 pos = __new_cake(pile);
131 pos = list_entry(pile->free.next, typeof(*pos), cakes);
137 piece_index_t found_index = pos->next_free;
138 pos->next_free = pos->free_list[found_index];
140 pile->alloced_pieces++;
142 llist_delete(&pos->cakes);
143 if (pos->next_free == EO_FREE_PIECE) {
144 llist_append(&pile->full, &pos->cakes);
146 llist_append(&pile->partial, &pos->cakes);
149 return (void*)((uintptr_t)pos->first_piece +
150 found_index * pile->piece_size);
154 cake_release(struct cake_pile* pile, void* area)
156 piece_index_t piece_index;
157 struct cake_s *pos, *n;
158 struct llist_header* hdrs[2] = { &pile->full, &pile->partial };
160 for (size_t i = 0; i < 2; i++) {
161 llist_for_each(pos, n, hdrs[i], cakes)
163 if (pos->first_piece > area) {
167 (uintptr_t)(area - pos->first_piece) / pile->piece_size;
168 if (piece_index < pile->pieces_per_cake) {
177 pos->free_list[piece_index] = pos->next_free;
178 pos->next_free = piece_index;
180 pile->alloced_pieces--;
182 llist_delete(&pos->cakes);
183 if (!pos->used_pieces) {
184 llist_append(&pile->free, &pos->cakes);
186 llist_append(&pile->partial, &pos->cakes);
195 kprintf(KDEBUG "<name> <cake> <pg/c> <p/c> <alloced>\n");
197 struct cake_pile *pos, *n;
198 llist_for_each(pos, n, &piles, piles)
200 kprintf("%s %d %d %d %d\n",
204 pos->pieces_per_cake,
205 pos->alloced_pieces);