Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[deliverable/linux.git] / include / linux / slub_def.h
CommitLineData
81819f0f
CL
1#ifndef _LINUX_SLUB_DEF_H
2#define _LINUX_SLUB_DEF_H
3
4/*
5 * SLUB : A Slab allocator without object queues.
6 *
7 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
8 */
9#include <linux/types.h>
10#include <linux/gfp.h>
11#include <linux/workqueue.h>
12#include <linux/kobject.h>
13
14struct kmem_cache_node {
15 spinlock_t list_lock; /* Protect partial list and nr_partial */
16 unsigned long nr_partial;
17 atomic_long_t nr_slabs;
18 struct list_head partial;
643b1138 19 struct list_head full;
81819f0f
CL
20};
21
22/*
23 * Slab cache management.
24 */
25struct kmem_cache {
26 /* Used for retriving partial slabs etc */
27 unsigned long flags;
28 int size; /* The size of an object including meta data */
29 int objsize; /* The size of an object without meta data */
30 int offset; /* Free pointer offset. */
31 unsigned int order;
32
33 /*
34 * Avoid an extra cache line for UP, SMP and for the node local to
35 * struct kmem_cache.
36 */
37 struct kmem_cache_node local_node;
38
39 /* Allocation and freeing of slabs */
40 int objects; /* Number of objects in slab */
41 int refcount; /* Refcount for slab cache destroy */
42 void (*ctor)(void *, struct kmem_cache *, unsigned long);
43 void (*dtor)(void *, struct kmem_cache *, unsigned long);
44 int inuse; /* Offset to metadata */
45 int align; /* Alignment */
46 const char *name; /* Name (only for display!) */
47 struct list_head list; /* List of slab caches */
48 struct kobject kobj; /* For sysfs */
49
50#ifdef CONFIG_NUMA
51 int defrag_ratio;
52 struct kmem_cache_node *node[MAX_NUMNODES];
53#endif
54 struct page *cpu_slab[NR_CPUS];
55};
56
57/*
58 * Kmalloc subsystem.
59 */
60#define KMALLOC_SHIFT_LOW 3
61
62#ifdef CONFIG_LARGE_ALLOCS
63#define KMALLOC_SHIFT_HIGH 25
64#else
65#if !defined(CONFIG_MMU) || NR_CPUS > 512 || MAX_NUMNODES > 256
66#define KMALLOC_SHIFT_HIGH 20
67#else
68#define KMALLOC_SHIFT_HIGH 18
69#endif
70#endif
71
72/*
73 * We keep the general caches in an array of slab caches that are used for
74 * 2^x bytes of allocations.
75 */
76extern struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
77
78/*
79 * Sorry that the following has to be that ugly but some versions of GCC
80 * have trouble with constant propagation and loops.
81 */
82static inline int kmalloc_index(int size)
83{
614410d5
CL
84 /*
85 * We should return 0 if size == 0 but we use the smallest object
86 * here for SLAB legacy reasons.
87 */
88 WARN_ON_ONCE(size == 0);
89
81819f0f
CL
90 if (size > 64 && size <= 96)
91 return 1;
92 if (size > 128 && size <= 192)
93 return 2;
94 if (size <= 8) return 3;
95 if (size <= 16) return 4;
96 if (size <= 32) return 5;
97 if (size <= 64) return 6;
98 if (size <= 128) return 7;
99 if (size <= 256) return 8;
100 if (size <= 512) return 9;
101 if (size <= 1024) return 10;
102 if (size <= 2 * 1024) return 11;
103 if (size <= 4 * 1024) return 12;
104 if (size <= 8 * 1024) return 13;
105 if (size <= 16 * 1024) return 14;
106 if (size <= 32 * 1024) return 15;
107 if (size <= 64 * 1024) return 16;
108 if (size <= 128 * 1024) return 17;
109 if (size <= 256 * 1024) return 18;
110#if KMALLOC_SHIFT_HIGH > 18
111 if (size <= 512 * 1024) return 19;
112 if (size <= 1024 * 1024) return 20;
113#endif
114#if KMALLOC_SHIFT_HIGH > 20
115 if (size <= 2 * 1024 * 1024) return 21;
116 if (size <= 4 * 1024 * 1024) return 22;
117 if (size <= 8 * 1024 * 1024) return 23;
118 if (size <= 16 * 1024 * 1024) return 24;
119 if (size <= 32 * 1024 * 1024) return 25;
120#endif
121 return -1;
122
123/*
124 * What we really wanted to do and cannot do because of compiler issues is:
125 * int i;
126 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
127 * if (size <= (1 << i))
128 * return i;
129 */
130}
131
132/*
133 * Find the slab cache for a given combination of allocation flags and size.
134 *
135 * This ought to end up with a global pointer to the right cache
136 * in kmalloc_caches.
137 */
138static inline struct kmem_cache *kmalloc_slab(size_t size)
139{
140 int index = kmalloc_index(size);
141
142 if (index == 0)
143 return NULL;
144
145 if (index < 0) {
146 /*
147 * Generate a link failure. Would be great if we could
148 * do something to stop the compile here.
149 */
150 extern void __kmalloc_size_too_large(void);
151 __kmalloc_size_too_large();
152 }
153 return &kmalloc_caches[index];
154}
155
156#ifdef CONFIG_ZONE_DMA
157#define SLUB_DMA __GFP_DMA
158#else
159/* Disable DMA functionality */
160#define SLUB_DMA 0
161#endif
162
163static inline void *kmalloc(size_t size, gfp_t flags)
164{
165 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
166 struct kmem_cache *s = kmalloc_slab(size);
167
168 if (!s)
169 return NULL;
170
171 return kmem_cache_alloc(s, flags);
172 } else
173 return __kmalloc(size, flags);
174}
175
176static inline void *kzalloc(size_t size, gfp_t flags)
177{
178 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
179 struct kmem_cache *s = kmalloc_slab(size);
180
181 if (!s)
182 return NULL;
183
184 return kmem_cache_zalloc(s, flags);
185 } else
186 return __kzalloc(size, flags);
187}
188
189#ifdef CONFIG_NUMA
190extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
191
192static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
193{
194 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
195 struct kmem_cache *s = kmalloc_slab(size);
196
197 if (!s)
198 return NULL;
199
200 return kmem_cache_alloc_node(s, flags, node);
201 } else
202 return __kmalloc_node(size, flags, node);
203}
204#endif
205
206#endif /* _LINUX_SLUB_DEF_H */
This page took 0.13721 seconds and 5 git commands to generate.