2011-06-23 69 views
3

我在linux内核(2.4)的一些代码上工作,出于某种原因,kmalloc返回相同的地址(我相信它只发生在测试的中间) 。我检查了在调用kmalloc之间没有调用kfree(即内存仍在使用中)。kmalloc一遍又一遍地返回相同的地址[Linux 2.4]

也许我内存不足? (kmalloc没有返回NULL ...)

关于如何发生这种事情的任何想法?

在此先感谢您的帮助!

代码:

typedef struct 
{ 
    char* buffer; 
    int read_count; 
    int write_count; 
    struct semaphore read_sm; 
    struct semaphore write_sm; 
    int reader_ready; 
    int writer_ready; 
    int createTimeStamp; 
} data_buffer_t ; 

typedef struct vsf_t vsf_t; 

struct vsf_t 
{ 
    int minor; 
    int type; 
    int open_count; 
    int waiting_pid; 
    data_buffer_t* data; 
    list_t proc_list; 
    vsf_t* otherSide_vsf; 
    int real_create_time_stamp; 
}; 

int create_vsf(struct inode *inode, struct file *filp, struct vsf_command_parameters* parms) 
{ 
... 
    buff_data = allocate_buffer(); 
    if (buff_data == NULL) 
    { 
     kfree(this_vsfRead); 
     kfree(this_vsfWrite); 
     return -ENOMEM; 
    } 
... 
} 

data_buffer_t* allocate_buffer() 
{ 
... 
    data_buffer_t* this_buff = (data_buffer_t*)kmalloc(sizeof(data_buffer_t), GFP_KERNEL); 
    if (this_buff == NULL) 
    { 
     printk(KERN_WARNING "failure at allocating memory\n"); 
     return NULL; 
    } 
... 
return this_buff; 
} 

*我每次打印的kmalloc和kfree后,我绝对相信没有kfree被称为用kmalloc的(即返回同一个地址)之间

回答

3

我不知道kmalloc的数据结构是什么样子,但是如果先前的double free在链接的缓冲区列表中导致循环,您可以想象会发生这种情况。更多的释放可能仍然链接在额外的不同缓冲器上(可以重新分配),但一旦这些缓冲器用尽了,最后的缓冲器将无限期地返回。

+0

发现它,双倍免费! – Belgi

相关问题