diff options
author | Pekka J Enberg <penberg@cs.Helsinki.FI> | 2005-09-06 18:18:31 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2005-09-07 19:57:45 -0400 |
commit | dd3927105b6f65afb7dac17682172cdfb86d3f00 (patch) | |
tree | 5cf282aff500cad23b9d7e13dc19b2b2d31e1ce6 | |
parent | 640e803376b9c4072f69fec42e304c974a631298 (diff) |
[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>
-rw-r--r-- | include/linux/slab.h | 16 | ||||
-rw-r--r-- | kernel/intermodule.c | 3 | ||||
-rw-r--r-- | kernel/params.c | 4 | ||||
-rw-r--r-- | kernel/power/pm.c | 3 | ||||
-rw-r--r-- | kernel/resource.c | 3 | ||||
-rw-r--r-- | kernel/workqueue.c | 3 | ||||
-rw-r--r-- | mm/slab.c | 18 |
7 files changed, 27 insertions, 23 deletions
diff --git a/include/linux/slab.h b/include/linux/slab.h index 80b2dfde2e80..42a6bea58af3 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h | |||
@@ -99,7 +99,21 @@ found: | |||
99 | return __kmalloc(size, flags); | 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 | extern void kfree(const void *); | 117 | extern void kfree(const void *); |
104 | extern unsigned int ksize(const void *); | 118 | extern unsigned int ksize(const void *); |
105 | 119 | ||
diff --git a/kernel/intermodule.c b/kernel/intermodule.c index 388977f3e9b7..0cbe633420fb 100644 --- a/kernel/intermodule.c +++ b/kernel/intermodule.c | |||
@@ -39,7 +39,7 @@ void inter_module_register(const char *im_name, struct module *owner, const void | |||
39 | struct list_head *tmp; | 39 | struct list_head *tmp; |
40 | struct inter_module_entry *ime, *ime_new; | 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 | /* Overloaded kernel, not fatal */ | 43 | /* Overloaded kernel, not fatal */ |
44 | printk(KERN_ERR | 44 | printk(KERN_ERR |
45 | "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n", | 45 | "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n", |
@@ -47,7 +47,6 @@ void inter_module_register(const char *im_name, struct module *owner, const void | |||
47 | kmalloc_failed = 1; | 47 | kmalloc_failed = 1; |
48 | return; | 48 | return; |
49 | } | 49 | } |
50 | memset(ime_new, 0, sizeof(*ime_new)); | ||
51 | ime_new->im_name = im_name; | 50 | ime_new->im_name = im_name; |
52 | ime_new->owner = owner; | 51 | ime_new->owner = owner; |
53 | ime_new->userdata = userdata; | 52 | ime_new->userdata = userdata; |
diff --git a/kernel/params.c b/kernel/params.c index d586c35ef8fc..fbf173215fd2 100644 --- a/kernel/params.c +++ b/kernel/params.c | |||
@@ -542,8 +542,8 @@ static void __init kernel_param_sysfs_setup(const char *name, | |||
542 | { | 542 | { |
543 | struct module_kobject *mk; | 543 | struct module_kobject *mk; |
544 | 544 | ||
545 | mk = kmalloc(sizeof(struct module_kobject), GFP_KERNEL); | 545 | mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL); |
546 | memset(mk, 0, sizeof(struct module_kobject)); | 546 | BUG_ON(!mk); |
547 | 547 | ||
548 | mk->mod = THIS_MODULE; | 548 | mk->mod = THIS_MODULE; |
549 | kobj_set_kset_s(mk, module_subsys); | 549 | kobj_set_kset_s(mk, module_subsys); |
diff --git a/kernel/power/pm.c b/kernel/power/pm.c index 61deda04e39e..159149321b3c 100644 --- a/kernel/power/pm.c +++ b/kernel/power/pm.c | |||
@@ -60,9 +60,8 @@ struct pm_dev *pm_register(pm_dev_t type, | |||
60 | unsigned long id, | 60 | unsigned long id, |
61 | pm_callback callback) | 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 | if (dev) { | 64 | if (dev) { |
65 | memset(dev, 0, sizeof(*dev)); | ||
66 | dev->type = type; | 65 | dev->type = type; |
67 | dev->id = id; | 66 | dev->id = id; |
68 | dev->callback = callback; | 67 | dev->callback = callback; |
diff --git a/kernel/resource.c b/kernel/resource.c index 26967e042201..92285d822de6 100644 --- a/kernel/resource.c +++ b/kernel/resource.c | |||
@@ -430,10 +430,9 @@ EXPORT_SYMBOL(adjust_resource); | |||
430 | */ | 430 | */ |
431 | struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name) | 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 | if (res) { | 435 | if (res) { |
436 | memset(res, 0, sizeof(*res)); | ||
437 | res->name = name; | 436 | res->name = name; |
438 | res->start = start; | 437 | res->start = start; |
439 | res->end = start + n - 1; | 438 | res->end = start + n - 1; |
diff --git a/kernel/workqueue.c b/kernel/workqueue.c index a3de837a8ddd..91bacb13a7e2 100644 --- a/kernel/workqueue.c +++ b/kernel/workqueue.c | |||
@@ -308,10 +308,9 @@ struct workqueue_struct *__create_workqueue(const char *name, | |||
308 | struct workqueue_struct *wq; | 308 | struct workqueue_struct *wq; |
309 | struct task_struct *p; | 309 | struct task_struct *p; |
310 | 310 | ||
311 | wq = kmalloc(sizeof(*wq), GFP_KERNEL); | 311 | wq = kzalloc(sizeof(*wq), GFP_KERNEL); |
312 | if (!wq) | 312 | if (!wq) |
313 | return NULL; | 313 | return NULL; |
314 | memset(wq, 0, sizeof(*wq)); | ||
315 | 314 | ||
316 | wq->name = name; | 315 | wq->name = name; |
317 | /* We don't need the distraction of CPUs appearing and vanishing. */ | 316 | /* We don't need the distraction of CPUs appearing and vanishing. */ |
@@ -2558,24 +2558,18 @@ void kmem_cache_free(kmem_cache_t *cachep, void *objp) | |||
2558 | EXPORT_SYMBOL(kmem_cache_free); | 2558 | EXPORT_SYMBOL(kmem_cache_free); |
2559 | 2559 | ||
2560 | /** | 2560 | /** |
2561 | * kcalloc - allocate memory for an array. The memory is set to zero. | 2561 | * kzalloc - allocate memory. The memory is set to zero. |
2562 | * @n: number of elements. | 2562 | * @size: how many bytes of memory are required. |
2563 | * @size: element size. | ||
2564 | * @flags: the type of memory to allocate. | 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; | 2567 | void *ret = kmalloc(size, flags); |
2569 | |||
2570 | if (n != 0 && size > INT_MAX / n) | ||
2571 | return ret; | ||
2572 | |||
2573 | ret = kmalloc(n * size, flags); | ||
2574 | if (ret) | 2568 | if (ret) |
2575 | memset(ret, 0, n * size); | 2569 | memset(ret, 0, size); |
2576 | return ret; | 2570 | return ret; |
2577 | } | 2571 | } |
2578 | EXPORT_SYMBOL(kcalloc); | 2572 | EXPORT_SYMBOL(kzalloc); |
2579 | 2573 | ||
2580 | /** | 2574 | /** |
2581 | * kfree - free previously allocated memory | 2575 | * kfree - free previously allocated memory |