aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/mtdchar.c
diff options
context:
space:
mode:
authorThago Galesi <thiagogalesi@gmail.com>2006-04-17 12:38:15 -0400
committerDavid Woodhouse <dwmw2@infradead.org>2006-04-17 12:38:15 -0400
commitb802c0741103aa92251d536c115874d51f802ec8 (patch)
treecf2cb539e68b224ad06b4a43350454b847543381 /drivers/mtd/mtdchar.c
parentcd2866faaa0efd9af18fe4a86d129cbd99240796 (diff)
[PATCH] Remove unnecessary kmalloc/kfree calls in mtdchar
This patch removes repeated calls to kmalloc / kfree in mtd_write / mtd_read functions, replacing them by a single kmalloc / kfree pair. Signed-off-by: Thiago Galesi <thiagogalesi@gmail.com> Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Diffstat (limited to 'drivers/mtd/mtdchar.c')
-rw-r--r--drivers/mtd/mtdchar.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 6f044584bdc6..6b83aee8abb8 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -170,16 +170,22 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
170 170
171 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers 171 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
172 and pass them directly to the MTD functions */ 172 and pass them directly to the MTD functions */
173
174 if (count > MAX_KMALLOC_SIZE)
175 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
176 else
177 kbuf=kmalloc(count, GFP_KERNEL);
178
179 if (!kbuf)
180 return -ENOMEM;
181
173 while (count) { 182 while (count) {
183
174 if (count > MAX_KMALLOC_SIZE) 184 if (count > MAX_KMALLOC_SIZE)
175 len = MAX_KMALLOC_SIZE; 185 len = MAX_KMALLOC_SIZE;
176 else 186 else
177 len = count; 187 len = count;
178 188
179 kbuf=kmalloc(len,GFP_KERNEL);
180 if (!kbuf)
181 return -ENOMEM;
182
183 switch (MTD_MODE(file)) { 189 switch (MTD_MODE(file)) {
184 case MTD_MODE_OTP_FACT: 190 case MTD_MODE_OTP_FACT:
185 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf); 191 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
@@ -215,9 +221,9 @@ static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t
215 return ret; 221 return ret;
216 } 222 }
217 223
218 kfree(kbuf);
219 } 224 }
220 225
226 kfree(kbuf);
221 return total_retlen; 227 return total_retlen;
222} /* mtd_read */ 228} /* mtd_read */
223 229
@@ -241,18 +247,21 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
241 if (!count) 247 if (!count)
242 return 0; 248 return 0;
243 249
250 if (count > MAX_KMALLOC_SIZE)
251 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
252 else
253 kbuf=kmalloc(count, GFP_KERNEL);
254
255 if (!kbuf)
256 return -ENOMEM;
257
244 while (count) { 258 while (count) {
259
245 if (count > MAX_KMALLOC_SIZE) 260 if (count > MAX_KMALLOC_SIZE)
246 len = MAX_KMALLOC_SIZE; 261 len = MAX_KMALLOC_SIZE;
247 else 262 else
248 len = count; 263 len = count;
249 264
250 kbuf=kmalloc(len,GFP_KERNEL);
251 if (!kbuf) {
252 printk("kmalloc is null\n");
253 return -ENOMEM;
254 }
255
256 if (copy_from_user(kbuf, buf, len)) { 265 if (copy_from_user(kbuf, buf, len)) {
257 kfree(kbuf); 266 kfree(kbuf);
258 return -EFAULT; 267 return -EFAULT;
@@ -282,10 +291,9 @@ static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count
282 kfree(kbuf); 291 kfree(kbuf);
283 return ret; 292 return ret;
284 } 293 }
285
286 kfree(kbuf);
287 } 294 }
288 295
296 kfree(kbuf);
289 return total_retlen; 297 return total_retlen;
290} /* mtd_write */ 298} /* mtd_write */
291 299