blob: 39c3a5eb8ebe677851fedd8c8ff6a70730d17c0b [file] [log] [blame]
Christoph Lameter2e892f42006-12-13 00:34:23 -08001#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. */
19struct cache_sizes {
20 size_t cs_size;
21 struct kmem_cache *cs_cachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -080022#ifdef CONFIG_ZONE_DMA
Christoph Lameter2e892f42006-12-13 00:34:23 -080023 struct kmem_cache *cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -080024#endif
Christoph Lameter2e892f42006-12-13 00:34:23 -080025};
26extern struct cache_sizes malloc_sizes[];
27
Paul Mundt6193a2f2007-07-15 23:38:22 -070028void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
29void *__kmalloc(size_t size, gfp_t flags);
30
Christoph Lameter2e892f42006-12-13 00:34:23 -080031static inline void *kmalloc(size_t size, gfp_t flags)
32{
33 if (__builtin_constant_p(size)) {
34 int i = 0;
Christoph Lameter6cb8f912007-07-17 04:03:22 -070035
36 if (!size)
37 return ZERO_SIZE_PTR;
38
Christoph Lameter2e892f42006-12-13 00:34:23 -080039#define CACHE(x) \
40 if (size <= x) \
41 goto found; \
42 else \
43 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -080044#include <linux/kmalloc_sizes.h>
Christoph Lameter2e892f42006-12-13 00:34:23 -080045#undef CACHE
46 {
47 extern void __you_cannot_kmalloc_that_much(void);
48 __you_cannot_kmalloc_that_much();
49 }
50found:
Christoph Lameter4b51d662007-02-10 01:43:10 -080051#ifdef CONFIG_ZONE_DMA
52 if (flags & GFP_DMA)
53 return kmem_cache_alloc(malloc_sizes[i].cs_dmacachep,
54 flags);
55#endif
56 return kmem_cache_alloc(malloc_sizes[i].cs_cachep, flags);
Christoph Lameter2e892f42006-12-13 00:34:23 -080057 }
58 return __kmalloc(size, flags);
59}
60
Christoph Lameter2e892f42006-12-13 00:34:23 -080061#ifdef CONFIG_NUMA
62extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
Paul Mundt6193a2f2007-07-15 23:38:22 -070063extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
Christoph Lameter2e892f42006-12-13 00:34:23 -080064
65static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
66{
67 if (__builtin_constant_p(size)) {
68 int i = 0;
Christoph Lameter6cb8f912007-07-17 04:03:22 -070069
70 if (!size)
71 return ZERO_SIZE_PTR;
72
Christoph Lameter2e892f42006-12-13 00:34:23 -080073#define CACHE(x) \
74 if (size <= x) \
75 goto found; \
76 else \
77 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -080078#include <linux/kmalloc_sizes.h>
Christoph Lameter2e892f42006-12-13 00:34:23 -080079#undef CACHE
80 {
81 extern void __you_cannot_kmalloc_that_much(void);
82 __you_cannot_kmalloc_that_much();
83 }
84found:
Christoph Lameter4b51d662007-02-10 01:43:10 -080085#ifdef CONFIG_ZONE_DMA
86 if (flags & GFP_DMA)
87 return kmem_cache_alloc_node(malloc_sizes[i].cs_dmacachep,
88 flags, node);
89#endif
90 return kmem_cache_alloc_node(malloc_sizes[i].cs_cachep,
91 flags, node);
Christoph Lameter2e892f42006-12-13 00:34:23 -080092 }
93 return __kmalloc_node(size, flags, node);
94}
95
96#endif /* CONFIG_NUMA */
97
Christoph Lameter2e892f42006-12-13 00:34:23 -080098#endif /* _LINUX_SLAB_DEF_H */