void mem_buddy_test() { as_mem_buddy_t *b = buddy_new(4); unsigned x = buddy_alloc(b, 4); buddy_alloc(b, 8); buddy_alloc(b, 2); buddy_free(b, x); buddy_print(b); buddy_destroy(b); }
int main() { struct Node * tempNode1, *tempNode2, *tempNode3; int size = 10; struct HeadNode * tempHead = buddy_new(size); if (tempHead!=NULL) { //注意申请的空间最大不是2^k, 而是2^k - 1 tempNode1 = buddy_alloc(tempHead,size,511); buddy_print(tempHead, size); tempNode2 = buddy_alloc(tempHead,size,10); buddy_print(tempHead, size); tempNode3 = buddy_alloc(tempHead,size,10); buddy_print(tempHead, size); buddy_combine(tempHead, size, tempNode3); buddy_print(tempHead, size); buddy_combine(tempHead, size, tempNode2); buddy_print(tempHead, size); buddy_combine(tempHead, size, tempNode1); buddy_print(tempHead, size); } buddy_free(tempHead); getchar(); return 0; }
void init_palloc() { kprintf("Initializing page frame allocator\n"); // Initialize bucket list int j, k; for (j = 0; j < PALLOC_BUCKET_COUNT; j++) { buckets[j].bucket_tag = j; buckets[j].total_pages = 0; buckets[j].avail_pages = 0; for (k = 0; k < PALLOC_ORDER_COUNT; k++) { buckets[j].buddies[k].value = 0; } spin_init(&buckets[j].lock); } // Calculate total number of nodes - 1 page needs 1 node ulong node_count = hal->paddr_space_end / PAGE_SIZE; ulong node_size = node_count * sizeof(struct palloc_node); kprintf("\tPalloc node size: %d KB, pages: %d\n", node_size / 1024, node_size / PAGE_SIZE); // Allocate and reserve memory nodes = (struct palloc_node *)hal->free_mem_start_addr; reserve_pfndb_mem(hal->free_mem_start_addr, node_size); hal->free_mem_start_addr += node_size; // Initialize all nodes ulong i; for (i = 0; i < node_count; i++) { nodes[i].avail = 0; nodes[i].tag = PALLOC_DUMMY_BUCKET; } // Go through PFN database to construct tags array struct pfndb_entry *entry = get_pfn_entry_by_paddr(0); int cur_tag = entry->tag; int recording = 0; ulong cur_bucket_start = 0; for (i = 0; i < hal->paddr_space_end; i += PAGE_SIZE) { entry = get_pfn_entry_by_paddr(i); // End of a bucket if ( recording && (!entry->usable || entry->inuse || cur_tag != entry->tag) ) { init_bucket(cur_bucket_start, i - cur_bucket_start, cur_tag); recording = 0; } // Start of a bucket else if ( !recording && (entry->usable && !entry->inuse && entry->tag != PALLOC_DUMMY_BUCKET) ) { cur_bucket_start = i; cur_tag = entry->tag; recording = 1; } } // Take care of the last bucket if (recording) { init_bucket(cur_bucket_start, hal->paddr_space_end - cur_bucket_start, cur_tag); } // Print the buddy buddy_print(); }