X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/c3f8598f7b2e540e4040955f313a7d05de715c99..a5338b60e111972364a8bc6f07011c6defd213d2:/lunaix-os/kernel/mm/cake.c diff --git a/lunaix-os/kernel/mm/cake.c b/lunaix-os/kernel/mm/cake.c index 2117286..a64b3ea 100644 --- a/lunaix-os/kernel/mm/cake.c +++ b/lunaix-os/kernel/mm/cake.c @@ -1,5 +1,5 @@ /** - * @file valloc.c + * @file cake.c * @author Lunaixsky (zelong56@gmail.com) * @brief A simplified cake(slab) allocator. * P.s. I call it cake as slab sounds more 'ridge' to me. :) @@ -19,7 +19,7 @@ LOG_MODULE("CAKE") -#define SLAB_SIZE PG_SIZE +#define CACHE_LINE_SIZE 128 struct cake_pile master_pile; @@ -39,8 +39,7 @@ __new_cake(struct cake_pile* pile) int max_piece = pile->pieces_per_cake; - cake->first_piece = - (void*)((uintptr_t)(cake + 1) + max_piece * sizeof(piece_index_t)); + cake->first_piece = (void*)((uintptr_t)cake + pile->offset); cake->next_free = 0; piece_index_t* free_list = &cake->free_list; @@ -58,8 +57,18 @@ void __init_pile(struct cake_pile* pile, char* name, unsigned int piece_size, - unsigned int pg_per_cake) + unsigned int pg_per_cake, + int options) { + unsigned int offset = sizeof(long); + + // 默认每块儿蛋糕对齐到地址总线宽度 + if ((options & PILE_CACHELINE)) { + // 对齐到128字节缓存行大小,主要用于DMA + offset = CACHE_LINE_SIZE; + } + + piece_size = ROUNDUP(piece_size, offset); *pile = (struct cake_pile){ .piece_size = piece_size, .cakes_count = 1, .pieces_per_cake = @@ -67,6 +76,11 @@ __init_pile(struct cake_pile* pile, (piece_size + sizeof(piece_index_t)), .pg_per_cake = pg_per_cake }; + unsigned int free_list_size = pile->pieces_per_cake * sizeof(piece_index_t); + + pile->offset = ROUNDUP(sizeof(struct cake_s) + free_list_size, offset); + pile->pieces_per_cake -= ICEIL((pile->offset - free_list_size), piece_size); + strncpy(&pile->pile_name, name, PILE_NAME_MAXLEN); llist_init_head(&pile->free); @@ -78,15 +92,18 @@ __init_pile(struct cake_pile* pile, void cake_init() { - __init_pile(&master_pile, "master", sizeof(master_pile), 1); + __init_pile(&master_pile, "pinkamina", sizeof(master_pile), 1, 0); } struct cake_pile* -cake_new_pile(char* name, unsigned int piece_size, unsigned int pg_per_cake) +cake_new_pile(char* name, + unsigned int piece_size, + unsigned int pg_per_cake, + int options) { struct cake_pile* pile = (struct cake_pile*)cake_grab(&master_pile); - __init_pile(pile, name, piece_size, pg_per_cake); + __init_pile(pile, name, piece_size, pg_per_cake, options); return pile; } @@ -95,20 +112,14 @@ void* cake_grab(struct cake_pile* pile) { struct cake_s *pos, *n; - llist_for_each(pos, n, &pile->partial, cakes) - { - if (pos->next_free != EO_FREE_PIECE) { - goto found; - } - } - - if (llist_empty(&pile->free)) { + if (!llist_empty(&pile->partial)) { + pos = list_entry(pile->partial.next, typeof(*pos), cakes); + } else if (llist_empty(&pile->free)) { pos = __new_cake(pile); } else { pos = list_entry(pile->free.next, typeof(*pos), cakes); } -found: piece_index_t found_index = pos->next_free; pos->next_free = pos->free_list[found_index]; pos->used_pieces++; @@ -128,7 +139,7 @@ found: int cake_release(struct cake_pile* pile, void* area) { - piece_index_t area_index; + piece_index_t piece_index; struct cake_s *pos, *n; struct llist_header* hdrs[2] = { &pile->full, &pile->partial }; @@ -138,9 +149,9 @@ cake_release(struct cake_pile* pile, void* area) if (pos->first_piece > area) { continue; } - area_index = + piece_index = (uintptr_t)(area - pos->first_piece) / pile->piece_size; - if (area_index < pile->pieces_per_cake) { + if (piece_index < pile->pieces_per_cake) { goto found; } } @@ -149,8 +160,8 @@ cake_release(struct cake_pile* pile, void* area) return 0; found: - pos->free_list[area_index] = pos->next_free; - pos->next_free = area_index; + pos->free_list[piece_index] = pos->next_free; + pos->next_free = piece_index; pos->used_pieces--; pile->alloced_pieces--;