Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_SLAB_DEF_H |
| 2 | #define _LINUX_SLAB_DEF_H |
| 3 | |
| 4 | /* |
| 5 | * Definitions unique to the original Linux SLAB allocator. |
| 6 | * |
| 7 | * What we provide here is a way to optimize the frequent kmalloc |
| 8 | * calls in the kernel by selecting the appropriate general cache |
| 9 | * if kmalloc was called with a size that can be established at |
| 10 | * compile time. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/init.h> |
| 14 | #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */ |
| 15 | #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */ |
| 16 | #include <linux/compiler.h> |
| 17 | |
| 18 | /* Size description struct for general caches. */ |
| 19 | struct cache_sizes { |
| 20 | size_t cs_size; |
| 21 | struct kmem_cache *cs_cachep; |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 22 | #ifdef CONFIG_ZONE_DMA |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 23 | struct kmem_cache *cs_dmacachep; |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 24 | #endif |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 25 | }; |
| 26 | extern struct cache_sizes malloc_sizes[]; |
| 27 | |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 28 | void *kmem_cache_alloc(struct kmem_cache *, gfp_t); |
| 29 | void *__kmalloc(size_t size, gfp_t flags); |
| 30 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 31 | static inline void *kmalloc(size_t size, gfp_t flags) |
| 32 | { |
| 33 | if (__builtin_constant_p(size)) { |
| 34 | int i = 0; |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 35 | |
| 36 | if (!size) |
| 37 | return ZERO_SIZE_PTR; |
| 38 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 39 | #define CACHE(x) \ |
| 40 | if (size <= x) \ |
| 41 | goto found; \ |
| 42 | else \ |
| 43 | i++; |
Joe Perches | 1c61fc4 | 2008-03-05 13:58:17 -0800 | [diff] [blame] | 44 | #include <linux/kmalloc_sizes.h> |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 45 | #undef CACHE |
Jeff Mahoney | 1cf3eb2 | 2009-01-27 23:48:59 +0200 | [diff] [blame] | 46 | return NULL; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 47 | found: |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 48 | #ifdef CONFIG_ZONE_DMA |
| 49 | if (flags & GFP_DMA) |
| 50 | return kmem_cache_alloc(malloc_sizes[i].cs_dmacachep, |
| 51 | flags); |
| 52 | #endif |
| 53 | return kmem_cache_alloc(malloc_sizes[i].cs_cachep, flags); |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 54 | } |
| 55 | return __kmalloc(size, flags); |
| 56 | } |
| 57 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 58 | #ifdef CONFIG_NUMA |
| 59 | extern void *__kmalloc_node(size_t size, gfp_t flags, int node); |
Paul Mundt | 6193a2f | 2007-07-15 23:38:22 -0700 | [diff] [blame] | 60 | extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node); |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 61 | |
| 62 | static inline void *kmalloc_node(size_t size, gfp_t flags, int node) |
| 63 | { |
| 64 | if (__builtin_constant_p(size)) { |
| 65 | int i = 0; |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 66 | |
| 67 | if (!size) |
| 68 | return ZERO_SIZE_PTR; |
| 69 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 70 | #define CACHE(x) \ |
| 71 | if (size <= x) \ |
| 72 | goto found; \ |
| 73 | else \ |
| 74 | i++; |
Joe Perches | 1c61fc4 | 2008-03-05 13:58:17 -0800 | [diff] [blame] | 75 | #include <linux/kmalloc_sizes.h> |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 76 | #undef CACHE |
Jeff Mahoney | 1cf3eb2 | 2009-01-27 23:48:59 +0200 | [diff] [blame] | 77 | return NULL; |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 78 | found: |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 79 | #ifdef CONFIG_ZONE_DMA |
| 80 | if (flags & GFP_DMA) |
| 81 | return kmem_cache_alloc_node(malloc_sizes[i].cs_dmacachep, |
| 82 | flags, node); |
| 83 | #endif |
| 84 | return kmem_cache_alloc_node(malloc_sizes[i].cs_cachep, |
| 85 | flags, node); |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 86 | } |
| 87 | return __kmalloc_node(size, flags, node); |
| 88 | } |
| 89 | |
| 90 | #endif /* CONFIG_NUMA */ |
| 91 | |
Christoph Lameter | 2e892f4 | 2006-12-13 00:34:23 -0800 | [diff] [blame] | 92 | #endif /* _LINUX_SLAB_DEF_H */ |