因此,我目前正在学习如何编写内核模块/设备驱动程序,并正在研究以下实现unregister_chrdev_region
:
https://elixir.bootlin.com/linux/v6.12/source/fs/char_dev.c#L311
/**
* unregister_chrdev_region() - unregister a range of device numbers
* @from: the first in the range of numbers to unregister
* @count: the number of device numbers to unregister
*
* This function will unregister a range of @count device numbers,
* starting with @from. The caller should normally be the one who
* allocated those numbers in the first place...
*/
void unregister_chrdev_region(dev_t from, unsigned count)
{
dev_t to = from + count;
dev_t n, next;
for (n = from; n < to; n = next) {
next = MKDEV(MAJOR(n)+1, 0);
if (next > to)
next = to;
kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
}
}
我不明白的是这一行的检查:
https://elixir.bootlin.com/linux/v6.12/source/fs/char_dev.c#L318
if (next > to) next = to;
据我所知,当循环变量等于上限时,循环已经中断to = from + count
。什么时候我们会遇到条件的情况if (next > to)
?这个条件检查的原因是什么?