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)
{
struct scull_dev *dev = filp->private_data;
...........
...........
item = (long)*f_pos / itemsize; /* item=number of full quantum set storing quantum*quest bytes of data ; Please correct me if wrong ?? */
...........
...........
dptr = scull_follow(dev, item); <<============ implementation ????
...........
..........
...........
out:
up(&dev->sem);
return retval;
}
in this case the purpose of scull_follow , IMHO is to reach the last scull_qset node containing fully filled quantum sets (
i.e., set with PER_QUANTUM_SIZE*NO_OF_QUANTUM_SETS bytes).
Then some implementations which i found have implemented scull_follow
as bellow:
/*
* Follow the list
*/
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
if(!qs) {
// Reserve memory
qs = dev->data = "" scull_qset), GFP_KERNEL);
// Failure :(
if(qs == NULL){
return NULL;
}
// Zero out the region
memset(qs, 0, sizeof(struct scull_qset));
}
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 ??