Commit dd3927105b6f65afb7dac17682172cdfb86d3f00
Committed by
Linus Torvalds
1 parent
640e803376
Exists in
master
and in
39 other branches
[PATCH] introduce and use kzalloc
This patch introduces a kzalloc wrapper and converts kernel/ to use it. It saves a little program text. Signed-off-by: Pekka Enberg <penberg@cs.helsinki.fi> Signed-off-by: Adrian Bunk <bunk@stusta.de> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Showing 7 changed files with 27 additions and 23 deletions Side-by-side Diff
include/linux/slab.h
... | ... | @@ -99,7 +99,21 @@ |
99 | 99 | return __kmalloc(size, flags); |
100 | 100 | } |
101 | 101 | |
102 | -extern void *kcalloc(size_t, size_t, unsigned int __nocast); | |
102 | +extern void *kzalloc(size_t, unsigned int __nocast); | |
103 | + | |
104 | +/** | |
105 | + * kcalloc - allocate memory for an array. The memory is set to zero. | |
106 | + * @n: number of elements. | |
107 | + * @size: element size. | |
108 | + * @flags: the type of memory to allocate. | |
109 | + */ | |
110 | +static inline void *kcalloc(size_t n, size_t size, unsigned int __nocast flags) | |
111 | +{ | |
112 | + if (n != 0 && size > INT_MAX / n) | |
113 | + return NULL; | |
114 | + return kzalloc(n * size, flags); | |
115 | +} | |
116 | + | |
103 | 117 | extern void kfree(const void *); |
104 | 118 | extern unsigned int ksize(const void *); |
105 | 119 |
kernel/intermodule.c
... | ... | @@ -39,7 +39,7 @@ |
39 | 39 | struct list_head *tmp; |
40 | 40 | struct inter_module_entry *ime, *ime_new; |
41 | 41 | |
42 | - if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) { | |
42 | + if (!(ime_new = kzalloc(sizeof(*ime), GFP_KERNEL))) { | |
43 | 43 | /* Overloaded kernel, not fatal */ |
44 | 44 | printk(KERN_ERR |
45 | 45 | "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n", |
... | ... | @@ -47,7 +47,6 @@ |
47 | 47 | kmalloc_failed = 1; |
48 | 48 | return; |
49 | 49 | } |
50 | - memset(ime_new, 0, sizeof(*ime_new)); | |
51 | 50 | ime_new->im_name = im_name; |
52 | 51 | ime_new->owner = owner; |
53 | 52 | ime_new->userdata = userdata; |
kernel/params.c
... | ... | @@ -542,8 +542,8 @@ |
542 | 542 | { |
543 | 543 | struct module_kobject *mk; |
544 | 544 | |
545 | - mk = kmalloc(sizeof(struct module_kobject), GFP_KERNEL); | |
546 | - memset(mk, 0, sizeof(struct module_kobject)); | |
545 | + mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); | |
546 | + BUG_ON(!mk); | |
547 | 547 | |
548 | 548 | mk->mod = THIS_MODULE; |
549 | 549 | kobj_set_kset_s(mk, module_subsys); |
kernel/power/pm.c
... | ... | @@ -60,9 +60,8 @@ |
60 | 60 | unsigned long id, |
61 | 61 | pm_callback callback) |
62 | 62 | { |
63 | - struct pm_dev *dev = kmalloc(sizeof(struct pm_dev), GFP_KERNEL); | |
63 | + struct pm_dev *dev = kzalloc(sizeof(struct pm_dev), GFP_KERNEL); | |
64 | 64 | if (dev) { |
65 | - memset(dev, 0, sizeof(*dev)); | |
66 | 65 | dev->type = type; |
67 | 66 | dev->id = id; |
68 | 67 | dev->callback = callback; |
kernel/resource.c
... | ... | @@ -430,10 +430,9 @@ |
430 | 430 | */ |
431 | 431 | struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name) |
432 | 432 | { |
433 | - struct resource *res = kmalloc(sizeof(*res), GFP_KERNEL); | |
433 | + struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); | |
434 | 434 | |
435 | 435 | if (res) { |
436 | - memset(res, 0, sizeof(*res)); | |
437 | 436 | res->name = name; |
438 | 437 | res->start = start; |
439 | 438 | res->end = start + n - 1; |
kernel/workqueue.c
... | ... | @@ -308,10 +308,9 @@ |
308 | 308 | struct workqueue_struct *wq; |
309 | 309 | struct task_struct *p; |
310 | 310 | |
311 | - wq = kmalloc(sizeof(*wq), GFP_KERNEL); | |
311 | + wq = kzalloc(sizeof(*wq), GFP_KERNEL); | |
312 | 312 | if (!wq) |
313 | 313 | return NULL; |
314 | - memset(wq, 0, sizeof(*wq)); | |
315 | 314 | |
316 | 315 | wq->name = name; |
317 | 316 | /* We don't need the distraction of CPUs appearing and vanishing. */ |
mm/slab.c
... | ... | @@ -2558,24 +2558,18 @@ |
2558 | 2558 | EXPORT_SYMBOL(kmem_cache_free); |
2559 | 2559 | |
2560 | 2560 | /** |
2561 | - * kcalloc - allocate memory for an array. The memory is set to zero. | |
2562 | - * @n: number of elements. | |
2563 | - * @size: element size. | |
2561 | + * kzalloc - allocate memory. The memory is set to zero. | |
2562 | + * @size: how many bytes of memory are required. | |
2564 | 2563 | * @flags: the type of memory to allocate. |
2565 | 2564 | */ |
2566 | -void *kcalloc(size_t n, size_t size, unsigned int __nocast flags) | |
2565 | +void *kzalloc(size_t size, unsigned int __nocast flags) | |
2567 | 2566 | { |
2568 | - void *ret = NULL; | |
2569 | - | |
2570 | - if (n != 0 && size > INT_MAX / n) | |
2571 | - return ret; | |
2572 | - | |
2573 | - ret = kmalloc(n * size, flags); | |
2567 | + void *ret = kmalloc(size, flags); | |
2574 | 2568 | if (ret) |
2575 | - memset(ret, 0, n * size); | |
2569 | + memset(ret, 0, size); | |
2576 | 2570 | return ret; |
2577 | 2571 | } |
2578 | -EXPORT_SYMBOL(kcalloc); | |
2572 | +EXPORT_SYMBOL(kzalloc); | |
2579 | 2573 | |
2580 | 2574 | /** |
2581 | 2575 | * kfree - free previously allocated memory |