1

I am having some basic question about the usage of structure pointer in linux device drivers.

Taking an example of RTC ISL12022 driver 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)

In above example structure pointer is used to get the I2C client data instead of creating local copyinto the function. Mostly I have seen such practice in all the drivers. Hereby I am looking to know about the pros of using structure pointer instead of creating local copy?

-----------------------------------EDIT-----------------------------------------------

Nicely Explained. Thanks. When passing the structure to the function pointer needs to be used, Agree on it. But considering the above rtc-isl12022.c example taking function

isl12022_set_datetime(struct i2c_client *client, struct rtc_time *tm)

the i2c_client structure is already passed to the function as pointer. (Thats there)

Now inside the isl12022_set_datetime function *client pointer has been used directly instead of creating local copy of it and then using that copy to ensure that by mistake original structure pointed by *client is safe and unaltered.

struct isl12022 isl2022; and then copy the structure pointed as i2c_get_clientdata(client);

The above statement will create isl12022 structure pointer which points to isl12022 structure returned by i2c_get_clientdata fn. Wont be that distrub the original datastructure which is pointed by client?

  • If you copy a struct, it is generally independant from the original struct (unless it contains pointers to other data – shallow copy vs. deep copy) – knittl Apr 02 '12 at 08:31

1 Answers1

0

It saves memory. Passing an address to a struct is much more efficient than copying a struct around.

alex
  • 479,566
  • 201
  • 878
  • 984