目录html
测试结果:git
$ make grade alloctest: OK (7.2s) alloctest: OK (5.8s) usertests: OK (84.3s) Score: 100/100
实验指导连接github
上来直接:bash
$ cd xv6-riscv-fall19 $ git checkout alloc
实验分为两个子任务:函数
B1_is_free XOR B2_is_free
这个占用状态,节约了 ~1M 内存。感受是重在阅读和理解 xv6 代码,这两个 lab 代码量都很小。工具
把 filealloc()
中遍历 ftable.file
的代码和 fileclose()
相应的释放代码替换为 bd_malloc()
便可。测试
个人修改code
这个 buddy allocator 中维护了两个 bitset ,一个存是否分裂 bd_sizes[k].split
,另外一个存是否已占用 bd_sizes[k].alloc
。 只须要不断查找 bit_*
这组工具函数出现的位置而后替换成相应的实现便可。htm
重难点不在于动态 malloc/free
的部分,实际上这些代码很好改。关键在于 bd_initfree()
和 bd_initfree_pair()
部分。内存
由于 buddy allocator 管理内存的同时须要在内存区域头部放一些 metadata,且内核提供内存区域的长度也极可能不是对其 2^k 次方的,故须要把一些区域 mark 为 allocated 。同时这些区域对应的 buddy 可能须要被加入 free_list (bd_initfree()/bd_initfree_pair()
用来完成此工做)
根据 bd_init()
中代码:
// done allocating; mark the memory range [base, p) as allocated, so // that buddy will not hand out that memory. int meta = bd_mark_data_structures(p); // mark the unavailable memory range [end, HEAP_SIZE) as allocated, // so that buddy will not hand out that memory. int unavailable = bd_mark_unavailable(end, p); void *bd_end = bd_base+BLK_SIZE(MAXSIZE)-unavailable; // initialize free lists for each size k int free = bd_initfree(p, bd_end, p, end);
这些不可用内存对应的内存区间为 [begin, p)
和 [end, HEAP_SIZE)
。在 bd_initfree_pair()
中特判这些内存范围,就能够把他们的 buddy 识别出来,而无需查找 bd_sizes[k].alloc
。