LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   How does the cdev_alloc memory get freed up? (https://www.linuxquestions.org/questions/linux-kernel-70/how-does-the-cdev_alloc-memory-get-freed-up-629661/)

nabramovitz 03-21-2008 10:40 AM

How does the cdev_alloc memory get freed up?
 
The cdev_alloc function uses kmalloc to allocate the cdev structure. Looking at the st and sg drivers, the cdev_del function does not seem to free the allocated memory with kmfree. I would not expect the cdev_del to free the memory since the cdev_del can also be used with cdev_init which uses preallocated memory.

So, if you use cdev_alloc, should kmfree be called to free the memory? If not, how does this allocated memory gets freed up when cdev_del is called?

ashok449 03-25-2008 03:03 AM

cdev_alloc — allocate a cdev structure

look at below example

Code:

static struct cdev *a_cdev;
a_cdev = cdev_alloc();

this has nothing to do with cdev_del()


cdev_add() and cdev_del() are like kmalloc(), kfree()

cdev_add()
Code:

cdev_add — add a char device to the system
int cdev_add ( struct cdev *  p, 
  dev_t  dev, 
  unsigned  count);

cdev_del()
Code:

cdev_del — remove a cdev from the system

void cdev_del ( struct cdev *  p);


Hope this helps you...

nabramovitz 03-28-2008 07:04 PM

cdev_alloc
 
Unfortunately, it does not because to create a cdev structure you are suppose to use either cdev_alloc or cdev_init. The cdev_alloc allocates the storage dynamically while the cdev_init is meant for statically defined structures. The cdev_add makes the character device available to the kernel and to users. The cdev_del call makes the character device unavailable for future use. If you use the cdev_alloc does cdev_del somehow know that the dynamic memory needs to be freed? I do not see how that would happen looking at the cdev_del code. What am I missing here? There does not seem be a cdev_free call which is what I would have expected.


All times are GMT -5. The time now is 09:59 AM.