On Jan 6, 2008 6:58 PM, Onkar <[EMAIL PROTECTED]> wrote: > Hi all, > > i am implementing scull (character device driver in LDD : chapter 3 ) , in > that I found :: > > Please refer to Linux Device Drivers Rubunu et al , 3/e.pg 57 > > ssize_t scull_read(struct file *filp, char __user *buf, size_t count,loff_t > *f_pos) > > Scull_Dev *scull_follow(Scull_Dev *dev, int n) > { > while (n--) { > if (!dev->next) { > dev->next = kmalloc(sizeof(Scull_Dev), GFP_KERNEL); > <<<============== why memory allocations in read operations?? > memset(dev->next, 0, sizeof(Scull_Dev)); > } > dev = dev->next; > continue; > } > return dev; > } > > IMHO this implementation is WRONG ?? Correct me if I am wrong. > > other implementation is : > > /* > scull_follow > Take a walk in the linked list until node 'n' has been reached. > */ > struct scull_qset* scull_follow(struct scull_dev *dev, int n){ > struct scull_qset *qs = dev->data; > > // Allocate first node if needed
Only if qs == NULL then allocate memory. (In case of write) else skip this statement block altogether. For read and if qs has some persistent data already, this statement block will be skipped. > if(!qs) { > // Reserve memory > qs = dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL); > // Failure :( > if(qs == NULL){ > return NULL; > } > // Zero out the region > memset(qs, 0, sizeof(struct scull_qset)); > } Same below. > > > while(n--){ > // If the next node does not exist, allocate it. > if(!qs->next){ > qs->next = kmalloc(sizeof(struct scull_qset), GFP_KERNEL); > if(qs->next== NULL){ > return NULL; > } > memset(qs->next, 0, sizeof(struct scull_qset)); > } > qs = qs->next; > continue; > } > return qs; > } > > But here too i am not convinced with memory allocation ?? Why memory > allocations in read when we only > want to reach the first partially filled node ?? > > Thanks.