日期:2014-05-16  浏览次数:20708 次

LDD3的scull代码问题
struct   scull_qset   *scull_follow(struct   scull_dev   *dev,   int   n)
...{
        struct   scull_qset   *qs   =   dev-> data;

                /**//*   Allocate   first   qset   explicitly   if   need   be   */
        if   (!   qs)   ...{
                qs   =   dev-> data   =   kmalloc(sizeof(struct   scull_qset),   GFP_KERNEL);
                if   (qs   ==   NULL)
                        return   NULL;     /**//*   Never   mind   */
                memset(qs,   0,   sizeof(struct   scull_qset));
        }

        /**//*   Then   follow   the   list   */
        while   (n--)   ...{
                if   (!qs-> next)   ...{
                        qs-> next   =   kmalloc(sizeof(struct   scull_qset),   GFP_KERNEL);
                        if   (qs-> next   ==   NULL)
                                return   NULL;     /**//*   Never   mind   */
                        memset(qs-> next,   0,   sizeof(struct   scull_qset));
                }
                qs   =   qs-> next;
                continue;
        }
        return   qs;
}

为什么qs分配了内存,没有看到有释放?

------解决方案--------------------
这个driver就是一个虚拟内存的driver。其实是先有设备接点,然后当调用write的时候如果没有内存就分配他,相应的在read/write中有。而当cleanup是会完全卸载设备接点。具体你把整个代码看完应该能看到free的地方。