我对linux设备驱动程序中结构指针的使用有一些基本的问题。
以RTC ISL12022驱动程序linux / drivers / rtc / rtc-isl12022.c为例
struct isl12022 *isl12022 = i2c_get_clientdata(client); //From Function static int
isl12022_set_datetime(struct i2c_client *client, struct rtc_time *tm)
在上面的示例中,结构指针用于获取I2C客户端数据,而不是在函数中创建本地copy。 大多数情况下,我在所有车手中都看到了这种做法。因此,我想知道使用结构指针而不是创建本地副本的优点吗?
----------------------------------- EDIT ----------- ------------------------------------
很好地解释。谢谢。当将结构传递给函数指针需要使用时,同意它。 但考虑到上面的rtc-isl12022.c示例采取功能
isl12022_set_datetime(struct i2c_client *client, struct rtc_time *tm)
i2c_client结构已作为指针传递给函数。 (那就是那里)
现在在isl12022_set_datetime函数内部*客户端指针已被直接使用,而不是创建它的本地副本,然后使用该副本确保错误地* client指向的原始结构是安全且未改变的。
struct isl12022 isl2022; and then copy the structure pointed as i2c_get_clientdata(client);
上面的语句将创建isl12022结构指针,该指针指向i2c_get_clientdata fn返回的isl12022结构。不会发布客户指向的原始数据结构吗?
答案 0 :(得分:0)
节省内存。将地址传递给struct
比复制struct
更有效。