]>
Commit | Line | Data |
---|---|---|
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 | ||
14 | struct 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 | */ | |
25 | struct 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); | |
81819f0f CL |
43 | int inuse; /* Offset to metadata */ |
44 | int align; /* Alignment */ | |
45 | const char *name; /* Name (only for display!) */ | |
46 | struct list_head list; /* List of slab caches */ | |
47 | struct kobject kobj; /* For sysfs */ | |
48 | ||
49 | #ifdef CONFIG_NUMA | |
50 | int defrag_ratio; | |
51 | struct kmem_cache_node *node[MAX_NUMNODES]; | |
52 | #endif | |
53 | struct page *cpu_slab[NR_CPUS]; | |
54 | }; | |
55 | ||
56 | /* | |
57 | * Kmalloc subsystem. | |
58 | */ | |
59 | #define KMALLOC_SHIFT_LOW 3 | |
60 | ||
81819f0f CL |
61 | /* |
62 | * We keep the general caches in an array of slab caches that are used for | |
63 | * 2^x bytes of allocations. | |
64 | */ | |
65 | extern struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1]; | |
66 | ||
67 | /* | |
68 | * Sorry that the following has to be that ugly but some versions of GCC | |
69 | * have trouble with constant propagation and loops. | |
70 | */ | |
0aa817f0 | 71 | static inline int kmalloc_index(size_t size) |
81819f0f | 72 | { |
272c1d21 CL |
73 | if (!size) |
74 | return 0; | |
614410d5 | 75 | |
0aa817f0 | 76 | if (size > KMALLOC_MAX_SIZE) |
cfbf07f2 CL |
77 | return -1; |
78 | ||
81819f0f CL |
79 | if (size > 64 && size <= 96) |
80 | return 1; | |
81 | if (size > 128 && size <= 192) | |
82 | return 2; | |
83 | if (size <= 8) return 3; | |
84 | if (size <= 16) return 4; | |
85 | if (size <= 32) return 5; | |
86 | if (size <= 64) return 6; | |
87 | if (size <= 128) return 7; | |
88 | if (size <= 256) return 8; | |
89 | if (size <= 512) return 9; | |
90 | if (size <= 1024) return 10; | |
91 | if (size <= 2 * 1024) return 11; | |
92 | if (size <= 4 * 1024) return 12; | |
93 | if (size <= 8 * 1024) return 13; | |
94 | if (size <= 16 * 1024) return 14; | |
95 | if (size <= 32 * 1024) return 15; | |
96 | if (size <= 64 * 1024) return 16; | |
97 | if (size <= 128 * 1024) return 17; | |
98 | if (size <= 256 * 1024) return 18; | |
81819f0f CL |
99 | if (size <= 512 * 1024) return 19; |
100 | if (size <= 1024 * 1024) return 20; | |
81819f0f CL |
101 | if (size <= 2 * 1024 * 1024) return 21; |
102 | if (size <= 4 * 1024 * 1024) return 22; | |
103 | if (size <= 8 * 1024 * 1024) return 23; | |
104 | if (size <= 16 * 1024 * 1024) return 24; | |
105 | if (size <= 32 * 1024 * 1024) return 25; | |
81819f0f CL |
106 | return -1; |
107 | ||
108 | /* | |
109 | * What we really wanted to do and cannot do because of compiler issues is: | |
110 | * int i; | |
111 | * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) | |
112 | * if (size <= (1 << i)) | |
113 | * return i; | |
114 | */ | |
115 | } | |
116 | ||
117 | /* | |
118 | * Find the slab cache for a given combination of allocation flags and size. | |
119 | * | |
120 | * This ought to end up with a global pointer to the right cache | |
121 | * in kmalloc_caches. | |
122 | */ | |
123 | static inline struct kmem_cache *kmalloc_slab(size_t size) | |
124 | { | |
125 | int index = kmalloc_index(size); | |
126 | ||
127 | if (index == 0) | |
128 | return NULL; | |
129 | ||
ade3aff2 AM |
130 | /* |
131 | * This function only gets expanded if __builtin_constant_p(size), so | |
132 | * testing it here shouldn't be needed. But some versions of gcc need | |
133 | * help. | |
134 | */ | |
135 | if (__builtin_constant_p(size) && index < 0) { | |
81819f0f CL |
136 | /* |
137 | * Generate a link failure. Would be great if we could | |
138 | * do something to stop the compile here. | |
139 | */ | |
140 | extern void __kmalloc_size_too_large(void); | |
141 | __kmalloc_size_too_large(); | |
142 | } | |
143 | return &kmalloc_caches[index]; | |
144 | } | |
145 | ||
146 | #ifdef CONFIG_ZONE_DMA | |
147 | #define SLUB_DMA __GFP_DMA | |
148 | #else | |
149 | /* Disable DMA functionality */ | |
150 | #define SLUB_DMA 0 | |
151 | #endif | |
152 | ||
272c1d21 CL |
153 | |
154 | /* | |
155 | * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. | |
156 | * | |
157 | * Dereferencing ZERO_SIZE_PTR will lead to a distinct access fault. | |
158 | * | |
159 | * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. | |
160 | * Both make kfree a no-op. | |
161 | */ | |
162 | #define ZERO_SIZE_PTR ((void *)16) | |
163 | ||
164 | ||
81819f0f CL |
165 | static inline void *kmalloc(size_t size, gfp_t flags) |
166 | { | |
167 | if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) { | |
168 | struct kmem_cache *s = kmalloc_slab(size); | |
169 | ||
170 | if (!s) | |
272c1d21 | 171 | return ZERO_SIZE_PTR; |
81819f0f CL |
172 | |
173 | return kmem_cache_alloc(s, flags); | |
174 | } else | |
175 | return __kmalloc(size, flags); | |
176 | } | |
177 | ||
178 | static inline void *kzalloc(size_t size, gfp_t flags) | |
179 | { | |
180 | if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) { | |
181 | struct kmem_cache *s = kmalloc_slab(size); | |
182 | ||
183 | if (!s) | |
272c1d21 | 184 | return ZERO_SIZE_PTR; |
81819f0f CL |
185 | |
186 | return kmem_cache_zalloc(s, flags); | |
187 | } else | |
188 | return __kzalloc(size, flags); | |
189 | } | |
190 | ||
191 | #ifdef CONFIG_NUMA | |
192 | extern void *__kmalloc_node(size_t size, gfp_t flags, int node); | |
193 | ||
194 | static inline void *kmalloc_node(size_t size, gfp_t flags, int node) | |
195 | { | |
196 | if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) { | |
197 | struct kmem_cache *s = kmalloc_slab(size); | |
198 | ||
199 | if (!s) | |
272c1d21 | 200 | return ZERO_SIZE_PTR; |
81819f0f CL |
201 | |
202 | return kmem_cache_alloc_node(s, flags, node); | |
203 | } else | |
204 | return __kmalloc_node(size, flags, node); | |
205 | } | |
206 | #endif | |
207 | ||
208 | #endif /* _LINUX_SLUB_DEF_H */ |