X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/45e1f8b055043e54be35462852ab6649d634da7c..965940833071025bf0d386f4a9c70a5258453dbd:/lunaix-os/kernel/mm/cake.c diff --git a/lunaix-os/kernel/mm/cake.c b/lunaix-os/kernel/mm/cake.c index 6434303..9503260 100644 --- a/lunaix-os/kernel/mm/cake.c +++ b/lunaix-os/kernel/mm/cake.c @@ -28,11 +28,11 @@ struct llist_header piles = { .next = &piles, .prev = &piles }; void* __alloc_cake(unsigned int cake_pg) { - ptr_t pa = (ptr_t)pmm_alloc_cpage(KERNEL_PID, cake_pg, 0); + ptr_t pa = (ptr_t)pmm_alloc_cpage(cake_pg, 0); if (!pa) { return NULL; } - return vmm_vmap(pa, cake_pg * PG_SIZE, PG_PREM_RW); + return vmap(pa, cake_pg * PG_SIZE, PG_PREM_RW, 0); } struct cake_s* @@ -77,14 +77,14 @@ __init_pile(struct cake_pile* pile, unsigned int offset = sizeof(long); // 默认每块儿蛋糕对齐到地址总线宽度 - if ((options & PILE_CACHELINE)) { + if ((options & PILE_ALIGN_CACHE)) { // 对齐到128字节缓存行大小,主要用于DMA offset = CACHE_LINE_SIZE; } piece_size = ROUNDUP(piece_size, offset); *pile = (struct cake_pile){ .piece_size = piece_size, - .cakes_count = 1, + .cakes_count = 0, .pieces_per_cake = (pg_per_cake * PG_SIZE) / (piece_size + sizeof(piece_index_t)), @@ -149,7 +149,7 @@ cake_grab(struct cake_pile* pile) pile->alloced_pieces++; llist_delete(&pos->cakes); - if (pos->next_free == EO_FREE_PIECE) { + if (pos->free_list[pos->next_free] == EO_FREE_PIECE) { llist_append(&pile->full, &pos->cakes); } else { llist_append(&pile->partial, &pos->cakes); @@ -169,6 +169,7 @@ int cake_release(struct cake_pile* pile, void* area) { piece_index_t piece_index; + size_t dsize = 0; struct cake_s *pos, *n; struct llist_header* hdrs[2] = { &pile->full, &pile->partial }; @@ -178,7 +179,8 @@ cake_release(struct cake_pile* pile, void* area) if (pos->first_piece > area) { continue; } - piece_index = (ptr_t)(area - pos->first_piece) / pile->piece_size; + dsize = (ptr_t)(area - pos->first_piece); + piece_index = dsize / pile->piece_size; if (piece_index < pile->pieces_per_cake) { goto found; } @@ -188,8 +190,12 @@ cake_release(struct cake_pile* pile, void* area) return 0; found: + assert(!(dsize % pile->piece_size)); pos->free_list[piece_index] = pos->next_free; pos->next_free = piece_index; + + assert_msg(pos->free_list[piece_index] != pos->next_free, "double free"); + pos->used_pieces--; pile->alloced_pieces--; @@ -200,6 +206,8 @@ found: llist_append(&pile->partial, &pos->cakes); } + *((unsigned int*)area) = DEADCAKE_MARK; + return 1; }