From 40c07ae8daa659b8feb149c84731629386873c16 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 25 Mar 2006 03:06:43 -0800 Subject: [PATCH] [PATCH] slab: optimize constant-size kzalloc calls As suggested by Eric Dumazet, optimize kzalloc() calls that pass a compile-time constant size. Please note that the patch increases kernel text slightly (~200 bytes for defconfig on x86). Signed-off-by: Pekka Enberg Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- include/linux/slab.h | 30 +++++++++++++++++++++++++++--- mm/util.c | 6 +++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/linux/slab.h b/include/linux/slab.h index 1216b09e07b1..15e1d9736b1b 100644 --- a/include/linux/slab.h +++ b/include/linux/slab.h @@ -110,7 +110,30 @@ found: return __kmalloc(size, flags); } -extern void *kzalloc(size_t, gfp_t); +extern void *__kzalloc(size_t, gfp_t); + +static inline void *kzalloc(size_t size, gfp_t flags) +{ + if (__builtin_constant_p(size)) { + int i = 0; +#define CACHE(x) \ + if (size <= x) \ + goto found; \ + else \ + i++; +#include "kmalloc_sizes.h" +#undef CACHE + { + extern void __you_cannot_kzalloc_that_much(void); + __you_cannot_kzalloc_that_much(); + } +found: + return kmem_cache_zalloc((flags & GFP_DMA) ? + malloc_sizes[i].cs_dmacachep : + malloc_sizes[i].cs_cachep, flags); + } + return __kzalloc(size, flags); +} /** * kcalloc - allocate memory for an array. The memory is set to zero. @@ -161,14 +184,14 @@ void *kmem_cache_zalloc(struct kmem_cache *, gfp_t); void kmem_cache_free(struct kmem_cache *c, void *b); const char *kmem_cache_name(struct kmem_cache *); void *kmalloc(size_t size, gfp_t flags); -void *kzalloc(size_t size, gfp_t flags); +void *__kzalloc(size_t size, gfp_t flags); void kfree(const void *m); unsigned int ksize(const void *m); unsigned int kmem_cache_size(struct kmem_cache *c); static inline void *kcalloc(size_t n, size_t size, gfp_t flags) { - return kzalloc(n * size, flags); + return __kzalloc(n * size, flags); } #define kmem_cache_shrink(d) (0) @@ -176,6 +199,7 @@ static inline void *kcalloc(size_t n, size_t size, gfp_t flags) #define kmem_ptr_validate(a, b) (0) #define kmem_cache_alloc_node(c, f, n) kmem_cache_alloc(c, f) #define kmalloc_node(s, f, n) kmalloc(s, f) +#define kzalloc(s, f) __kzalloc(s, f) #define ____kmalloc kmalloc #endif /* CONFIG_SLOB */ diff --git a/mm/util.c b/mm/util.c index b68d3d7d0359..7368479220b3 100644 --- a/mm/util.c +++ b/mm/util.c @@ -5,18 +5,18 @@ #include /** - * kzalloc - allocate memory. The memory is set to zero. + * __kzalloc - allocate memory. The memory is set to zero. * @size: how many bytes of memory are required. * @flags: the type of memory to allocate. */ -void *kzalloc(size_t size, gfp_t flags) +void *__kzalloc(size_t size, gfp_t flags) { void *ret = ____kmalloc(size, flags); if (ret) memset(ret, 0, size); return ret; } -EXPORT_SYMBOL(kzalloc); +EXPORT_SYMBOL(__kzalloc); /* * kstrdup - allocate space for and copy an existing string -- 2.34.1