Chinaunix

标题: 请问struct idr有何作用? [打印本页]

作者: fineamy    时间: 2006-11-28 12:54
标题: 请问struct idr有何作用?
/**
* idr_remove - remove the given id and free it's slot
* idp: idr handle
* id: uniqueue key
*/
void idr_remove(struct idr *idp, int id)
{
        struct idr_layer *p;

        /* Mask off upper bits we don't use for the search. */
        id &= MAX_ID_MASK;

        sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
        if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
            idp->top->ary[0]) {  // We can drop a layer

                p = idp->top->ary[0];
                idp->top->bitmap = idp->top->count = 0;
                free_layer(idp, idp->top);
                idp->top = p;
                --idp->layers;
        }
        while (idp->id_free_cnt >= IDR_FREE_MAX) {
                p = alloc_layer(idp);
                kmem_cache_free(idr_layer_cache, p);
                return;
        }
}

注,idr被做成一个根树.
下面是相关注释

* Small id to pointer translation service.
*
* It uses a radix tree like structure as a sparse array indexed
* by the id to obtain the pointer.  The bitmap makes allocating
* a new id quick.
*
* You call it to allocate an id (an int) an associate with that id a
* pointer or what ever, we treat it as a (void *).  You can pass this
* id to a user for him to pass back at a later time.  You then pass
* that id to this code and it returns your pointer.

* You can release ids at any time. When all ids are released, most of
* the memory is returned (we keep IDR_FREE_MAX) in a local pool so we
* don't need to go to the memory "store" during an id allocate, just
* so you don't need to be too concerned about locking and conflicts
* with the slab allocator.
*/


我的疑问是关于struct idr结构的一些基本功能是什么,为何引入struct idr.了解的DX能不能简单解释一下.谢谢!
作者: fineamy    时间: 2006-11-28 12:58
标题: 赫赫,写完好象有点明白了,
这里idr是否是ID RADIX缩写,就是说将ID组织到了一棵RADIX树中.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2