Commit d737c09cfb78424276e3eb45d1e7af578a451fd4
Committed by
Jean Delvare
1 parent
3d30f9e69f
Exists in
master
and in
4 other branches
hwmon: (max1111) Avoid extra memory allocations
We can allocate the tx and rx buffers as part of our data structure. Doing so is faster and spares memory. Signed-off-by: Jean Delvare <khali@linux-fr.org> Tested-by: Stanislav Brabec <utx@penguin.cz>
Showing 1 changed file with 6 additions and 21 deletions Side-by-side Diff
drivers/hwmon/max1111.c
... | ... | @@ -38,8 +38,8 @@ |
38 | 38 | struct device *hwmon_dev; |
39 | 39 | struct spi_message msg; |
40 | 40 | struct spi_transfer xfer[2]; |
41 | - uint8_t *tx_buf; | |
42 | - uint8_t *rx_buf; | |
41 | + uint8_t tx_buf[MAX1111_TX_BUF_SIZE]; | |
42 | + uint8_t rx_buf[MAX1111_RX_BUF_SIZE]; | |
43 | 43 | struct mutex drvdata_lock; |
44 | 44 | /* protect msg, xfer and buffers from multiple access */ |
45 | 45 | }; |
46 | 46 | |
47 | 47 | |
48 | 48 | |
... | ... | @@ -131,33 +131,23 @@ |
131 | 131 | .attrs = max1111_attributes, |
132 | 132 | }; |
133 | 133 | |
134 | -static int setup_transfer(struct max1111_data *data) | |
134 | +static int __devinit setup_transfer(struct max1111_data *data) | |
135 | 135 | { |
136 | 136 | struct spi_message *m; |
137 | 137 | struct spi_transfer *x; |
138 | 138 | |
139 | - data->tx_buf = kmalloc(MAX1111_TX_BUF_SIZE, GFP_KERNEL); | |
140 | - if (!data->tx_buf) | |
141 | - return -ENOMEM; | |
142 | - | |
143 | - data->rx_buf = kmalloc(MAX1111_RX_BUF_SIZE, GFP_KERNEL); | |
144 | - if (!data->rx_buf) { | |
145 | - kfree(data->tx_buf); | |
146 | - return -ENOMEM; | |
147 | - } | |
148 | - | |
149 | 139 | m = &data->msg; |
150 | 140 | x = &data->xfer[0]; |
151 | 141 | |
152 | 142 | spi_message_init(m); |
153 | 143 | |
154 | 144 | x->tx_buf = &data->tx_buf[0]; |
155 | - x->len = 1; | |
145 | + x->len = MAX1111_TX_BUF_SIZE; | |
156 | 146 | spi_message_add_tail(x, m); |
157 | 147 | |
158 | 148 | x++; |
159 | 149 | x->rx_buf = &data->rx_buf[0]; |
160 | - x->len = 2; | |
150 | + x->len = MAX1111_RX_BUF_SIZE; | |
161 | 151 | spi_message_add_tail(x, m); |
162 | 152 | |
163 | 153 | return 0; |
... | ... | @@ -192,7 +182,7 @@ |
192 | 182 | err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group); |
193 | 183 | if (err) { |
194 | 184 | dev_err(&spi->dev, "failed to create attribute group\n"); |
195 | - goto err_free_all; | |
185 | + goto err_free_data; | |
196 | 186 | } |
197 | 187 | |
198 | 188 | data->hwmon_dev = hwmon_device_register(&spi->dev); |
... | ... | @@ -209,9 +199,6 @@ |
209 | 199 | |
210 | 200 | err_remove: |
211 | 201 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
212 | -err_free_all: | |
213 | - kfree(data->rx_buf); | |
214 | - kfree(data->tx_buf); | |
215 | 202 | err_free_data: |
216 | 203 | kfree(data); |
217 | 204 | return err; |
... | ... | @@ -224,8 +211,6 @@ |
224 | 211 | hwmon_device_unregister(data->hwmon_dev); |
225 | 212 | sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group); |
226 | 213 | mutex_destroy(&data->drvdata_lock); |
227 | - kfree(data->rx_buf); | |
228 | - kfree(data->tx_buf); | |
229 | 214 | kfree(data); |
230 | 215 | return 0; |
231 | 216 | } |