driverdevicememsetkmalloc

Understanding the use of memset in this example


I'm studying an example from the Linux Device Driver book(http://lwn.net/Kernel/LDD3/), and I don't understand the use and usefullness of the function memset in this context and I hoped that someone could explain it to me. I understand that we allocate memory for our device structure using kmalloc and with memset we put 0's in front of the memory address? Here is the example nonortheless:

int scull_p_init(dev_t firstdev)
{
int i, result;

result = register_chrdev_region(firstdev, scull_p_nr_devs, "scullp");
if (result < 0) {
    printk(KERN_NOTICE "Unable to get scullp region, error %d\n", result);
    return 0;
}
scull_p_devno = firstdev;
scull_p_devices = kmalloc(scull_p_nr_devs * sizeof(struct scull_pipe), GFP_KERNEL);
if (scull_p_devices == NULL) {
    unregister_chrdev_region(firstdev, scull_p_nr_devs);
    return 0;
}
memset(scull_p_devices, 0, scull_p_nr_devs * sizeof(struct scull_pipe));
for (i = 0; i < scull_p_nr_devs; i++) {
    init_waitqueue_head(&(scull_p_devices[i].inq));
    init_waitqueue_head(&(scull_p_devices[i].outq));
    init_MUTEX(&scull_p_devices[i].sem);
    scull_p_setup_cdev(scull_p_devices + i, i);
}

Solution

  • The memset is not putting 0 in front of scull_p_devices. It is overwriting the memory from the address in scull_p_devices up to the size of the allocated region with zeros.