Commit dd392710 authored by Pekka J Enberg's avatar Pekka J Enberg Committed by Linus Torvalds

[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: default avatarPekka Enberg <penberg@cs.helsinki.fi>
Signed-off-by: default avatarAdrian Bunk <bunk@stusta.de>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 640e8033
...@@ -99,7 +99,21 @@ static inline void *kmalloc(size_t size, unsigned int __nocast flags) ...@@ -99,7 +99,21 @@ static inline void *kmalloc(size_t size, unsigned int __nocast flags)
return __kmalloc(size, flags); return __kmalloc(size, flags);
} }
extern void *kcalloc(size_t, size_t, unsigned int __nocast); extern void *kzalloc(size_t, unsigned int __nocast);
/**
* kcalloc - allocate memory for an array. The memory is set to zero.
* @n: number of elements.
* @size: element size.
* @flags: the type of memory to allocate.
*/
static inline void *kcalloc(size_t n, size_t size, unsigned int __nocast flags)
{
if (n != 0 && size > INT_MAX / n)
return NULL;
return kzalloc(n * size, flags);
}
extern void kfree(const void *); extern void kfree(const void *);
extern unsigned int ksize(const void *); extern unsigned int ksize(const void *);
......
...@@ -39,7 +39,7 @@ void inter_module_register(const char *im_name, struct module *owner, const void ...@@ -39,7 +39,7 @@ void inter_module_register(const char *im_name, struct module *owner, const void
struct list_head *tmp; struct list_head *tmp;
struct inter_module_entry *ime, *ime_new; struct inter_module_entry *ime, *ime_new;
if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) { if (!(ime_new = kzalloc(sizeof(*ime), GFP_KERNEL))) {
/* Overloaded kernel, not fatal */ /* Overloaded kernel, not fatal */
printk(KERN_ERR printk(KERN_ERR
"Aiee, inter_module_register: cannot kmalloc entry for '%s'\n", "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,7 +47,6 @@ void inter_module_register(const char *im_name, struct module *owner, const void
kmalloc_failed = 1; kmalloc_failed = 1;
return; return;
} }
memset(ime_new, 0, sizeof(*ime_new));
ime_new->im_name = im_name; ime_new->im_name = im_name;
ime_new->owner = owner; ime_new->owner = owner;
ime_new->userdata = userdata; ime_new->userdata = userdata;
......
...@@ -542,8 +542,8 @@ static void __init kernel_param_sysfs_setup(const char *name, ...@@ -542,8 +542,8 @@ static void __init kernel_param_sysfs_setup(const char *name,
{ {
struct module_kobject *mk; struct module_kobject *mk;
mk = kmalloc(sizeof(struct module_kobject), GFP_KERNEL); mk = kzalloc(sizeof(struct module_kobject), GFP_KERNEL);
memset(mk, 0, sizeof(struct module_kobject)); BUG_ON(!mk);
mk->mod = THIS_MODULE; mk->mod = THIS_MODULE;
kobj_set_kset_s(mk, module_subsys); kobj_set_kset_s(mk, module_subsys);
......
...@@ -60,9 +60,8 @@ struct pm_dev *pm_register(pm_dev_t type, ...@@ -60,9 +60,8 @@ struct pm_dev *pm_register(pm_dev_t type,
unsigned long id, unsigned long id,
pm_callback callback) pm_callback callback)
{ {
struct pm_dev *dev = kmalloc(sizeof(struct pm_dev), GFP_KERNEL); struct pm_dev *dev = kzalloc(sizeof(struct pm_dev), GFP_KERNEL);
if (dev) { if (dev) {
memset(dev, 0, sizeof(*dev));
dev->type = type; dev->type = type;
dev->id = id; dev->id = id;
dev->callback = callback; dev->callback = callback;
......
...@@ -430,10 +430,9 @@ EXPORT_SYMBOL(adjust_resource); ...@@ -430,10 +430,9 @@ EXPORT_SYMBOL(adjust_resource);
*/ */
struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name) struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
{ {
struct resource *res = kmalloc(sizeof(*res), GFP_KERNEL); struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
if (res) { if (res) {
memset(res, 0, sizeof(*res));
res->name = name; res->name = name;
res->start = start; res->start = start;
res->end = start + n - 1; res->end = start + n - 1;
......
...@@ -308,10 +308,9 @@ struct workqueue_struct *__create_workqueue(const char *name, ...@@ -308,10 +308,9 @@ struct workqueue_struct *__create_workqueue(const char *name,
struct workqueue_struct *wq; struct workqueue_struct *wq;
struct task_struct *p; struct task_struct *p;
wq = kmalloc(sizeof(*wq), GFP_KERNEL); wq = kzalloc(sizeof(*wq), GFP_KERNEL);
if (!wq) if (!wq)
return NULL; return NULL;
memset(wq, 0, sizeof(*wq));
wq->name = name; wq->name = name;
/* We don't need the distraction of CPUs appearing and vanishing. */ /* 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,24 +2558,18 @@ void kmem_cache_free(kmem_cache_t *cachep, void *objp)
EXPORT_SYMBOL(kmem_cache_free); EXPORT_SYMBOL(kmem_cache_free);
/** /**
* kcalloc - allocate memory for an array. The memory is set to zero. * kzalloc - allocate memory. The memory is set to zero.
* @n: number of elements. * @size: how many bytes of memory are required.
* @size: element size.
* @flags: the type of memory to allocate. * @flags: the type of memory to allocate.
*/ */
void *kcalloc(size_t n, size_t size, unsigned int __nocast flags) void *kzalloc(size_t size, unsigned int __nocast flags)
{ {
void *ret = NULL; void *ret = kmalloc(size, flags);
if (n != 0 && size > INT_MAX / n)
return ret;
ret = kmalloc(n * size, flags);
if (ret) if (ret)
memset(ret, 0, n * size); memset(ret, 0, size);
return ret; return ret;
} }
EXPORT_SYMBOL(kcalloc); EXPORT_SYMBOL(kzalloc);
/** /**
* kfree - free previously allocated memory * kfree - free previously allocated memory
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment