我有任务,我已尽我所能,但无论我尝试什么,我都无法获得最合适的方案。以下是代码。为了实现最佳拟合,我对功能进行了更改slob_page_alloc
。代码如下:
static void *slob_page_alloc(struct page *sp, size_t size, int align)
{
slob_t *prev, *cur, *aligned = NULL, *best_fit=NULL;
/* See SLOB_UNITS defination for meaning of macro. units is required
* number OF units.*/
int delta = 0, units = SLOB_UNITS(size);
unsigned long frag_size = -1UL;
/*Iterate throught the whole page to find best fit*/
//printk("Before the for loop\n");
printk("Starting slob_page_alloc execution\t");
for(prev=NULL, cur=sp->freelist; ; prev=cur, cur=slob_next(cur)) {
slobidx_t avail = slob_units(cur);
if(align) {
aligned = (slob_t *)ALIGN((unsigned long)cur, align);
delta = aligned - cur;
}
if(avail >= delta+units) {
if( frag_size > avail-units ) {
frag_size = avail-units;
best_fit = cur;
}
}
if(slob_last(cur))
break;
}
//printk("after the for loop.\n");
if(best_fit) {
slobidx_t avail = slob_units(best_fit);
//printk("best fit found\n");
if (align) {
aligned = (slob_t *)ALIGN((unsigned long)best_fit, align);
delta = aligned - best_fit;
}
if (avail >= units + delta) { /* room enough? */
slob_t *next;
if (delta) { /* need to fragment head to align? */
next = slob_next(best_fit);
/*Update the newly fragmented slob*/
set_slob(aligned, avail - delta, next);
/* Update the lod slob about reduced size
* and new next slob*/
set_slob(best_fit, delta, aligned);
prev = best_fit;
best_fit = aligned;
avail = slob_units(best_fit);
}
next = slob_next(best_fit);
if (avail == units) { /* exact fit? unlink. */
if (prev)
set_slob(prev, slob_units(prev), next);
else
sp->freelist = next;
} else { /* fragment */
if (prev)
set_slob(prev, slob_units(prev), best_fit + units);
else
sp->freelist = best_fit + units;
set_slob(best_fit + units, avail - units, next);
}
sp->units -= units;
if (!sp->units)
clear_slob_page_free(sp);
printk("Returned from slob_page_alloc\t");
return best_fit;
}
}
printk("Returned from slob_page_alloc\t");
return NULL;
}
当我用这个方案配置内核时,它只是挂在某个地方。
调试:
我已经在此功能的某个地方以及功能中完成了打印slob_alloc
。虽然这对我没有任何意义,但我的函数被多次调用并成功返回,然后再次被调用并返回等等。但在某一时刻,它被调用会打印此函数内的语句,但不会打印其被调用者的语句,而是无限期地挂在那里。
任何帮助表示赞赏!谢谢。