cache_estimate

来源:互联网 发布:主流编程语言对比 编辑:程序博客网 时间:2024/06/06 15:13

/* Cal the num objs, wastage, and bytes left over for a given slab size. */
在页的阶为 gfporder,每个对象大小为size的条件下:
本函数负责计算对象的数目,浪费的空间,以及在所给的slab中剩余的空间。
计算出来后,外层函数评估此时对象个数,浪费的大小是否能够接受,如果不能接受,则
需要调整gfporder,重新调用此函数进行计算。

static void kmem_cache_estimate (unsigned long gfporder, size_t size,int flags, size_t *left_over, unsigned int *num)
{
    int i;
    size_t wastage = PAGE_SIZE<<gfporder;
    size_t extra = 0;
    size_t base = 0;

    if(!(flags & CFLGS_OFF_SLAB)) {
        base = sizeof(slab_t);
        extra = sizeof(kmem_bufctl_t);
    }

    i = 0;

    /*-- L1_CACHE_ALIGN 是计算L1 CACHE对齐的
         L1_CACHE 有许多线( cache line )每一个线可以同时传输多个字节,比如16,32
         如果进行L1 CACHE对齐可以使得对象不占用过多的线,不知道是否可以加快速度??
    */

    while(i*size + L1_CACHE_ALIGN(base+i*extra) <= wastage)
        i++;

    if(i > 0)
        i--;

    if(i > SLAB_LIMIT)
        i = SLAB_LIMIT;

    *num = i;
    wastage -= i*size;
    wastage -= L1_CACHE_ALIGN(base+i*extra);
    *left_over = wastage;计算出来的浪费的空间
}