blob: a9fb92862aaa360a4b8ad3f4112ed271e076ec74 [file] [log] [blame]
Christoph Lameter81819f02007-05-06 14:49:36 -07001#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;
Christoph Lameter643b1132007-05-06 14:49:42 -070019 struct list_head full;
Christoph Lameter81819f02007-05-06 14:49:36 -070020};
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);
Christoph Lameter81819f02007-05-06 14:49:36 -070043 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
61#ifdef CONFIG_LARGE_ALLOCS
Christoph Lametercfbf07f2007-05-15 01:42:06 -070062#define KMALLOC_SHIFT_HIGH ((MAX_ORDER + PAGE_SHIFT) =< 25 ? \
63 (MAX_ORDER + PAGE_SHIFT - 1) : 25)
Christoph Lameter81819f02007-05-06 14:49:36 -070064#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{
Christoph Lameter614410d2007-05-06 14:49:38 -070084 /*
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
Christoph Lameter1abd7272007-05-15 23:57:03 -070090 if (size > (1 << KMALLOC_SHIFT_HIGH))
Christoph Lametercfbf07f2007-05-15 01:42:06 -070091 return -1;
92
Christoph Lameter81819f02007-05-06 14:49:36 -070093 if (size > 64 && size <= 96)
94 return 1;
95 if (size > 128 && size <= 192)
96 return 2;
97 if (size <= 8) return 3;
98 if (size <= 16) return 4;
99 if (size <= 32) return 5;
100 if (size <= 64) return 6;
101 if (size <= 128) return 7;
102 if (size <= 256) return 8;
103 if (size <= 512) return 9;
104 if (size <= 1024) return 10;
105 if (size <= 2 * 1024) return 11;
106 if (size <= 4 * 1024) return 12;
107 if (size <= 8 * 1024) return 13;
108 if (size <= 16 * 1024) return 14;
109 if (size <= 32 * 1024) return 15;
110 if (size <= 64 * 1024) return 16;
111 if (size <= 128 * 1024) return 17;
112 if (size <= 256 * 1024) return 18;
113#if KMALLOC_SHIFT_HIGH > 18
114 if (size <= 512 * 1024) return 19;
115 if (size <= 1024 * 1024) return 20;
116#endif
117#if KMALLOC_SHIFT_HIGH > 20
118 if (size <= 2 * 1024 * 1024) return 21;
119 if (size <= 4 * 1024 * 1024) return 22;
120 if (size <= 8 * 1024 * 1024) return 23;
121 if (size <= 16 * 1024 * 1024) return 24;
122 if (size <= 32 * 1024 * 1024) return 25;
123#endif
124 return -1;
125
126/*
127 * What we really wanted to do and cannot do because of compiler issues is:
128 * int i;
129 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
130 * if (size <= (1 << i))
131 * return i;
132 */
133}
134
135/*
136 * Find the slab cache for a given combination of allocation flags and size.
137 *
138 * This ought to end up with a global pointer to the right cache
139 * in kmalloc_caches.
140 */
141static inline struct kmem_cache *kmalloc_slab(size_t size)
142{
143 int index = kmalloc_index(size);
144
145 if (index == 0)
146 return NULL;
147
Andrew Mortonade3aff2007-05-16 22:10:54 -0700148 /*
149 * This function only gets expanded if __builtin_constant_p(size), so
150 * testing it here shouldn't be needed. But some versions of gcc need
151 * help.
152 */
153 if (__builtin_constant_p(size) && index < 0) {
Christoph Lameter81819f02007-05-06 14:49:36 -0700154 /*
155 * Generate a link failure. Would be great if we could
156 * do something to stop the compile here.
157 */
158 extern void __kmalloc_size_too_large(void);
159 __kmalloc_size_too_large();
160 }
161 return &kmalloc_caches[index];
162}
163
164#ifdef CONFIG_ZONE_DMA
165#define SLUB_DMA __GFP_DMA
166#else
167/* Disable DMA functionality */
168#define SLUB_DMA 0
169#endif
170
171static inline void *kmalloc(size_t size, gfp_t flags)
172{
173 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
174 struct kmem_cache *s = kmalloc_slab(size);
175
176 if (!s)
177 return NULL;
178
179 return kmem_cache_alloc(s, flags);
180 } else
181 return __kmalloc(size, flags);
182}
183
184static inline void *kzalloc(size_t size, gfp_t flags)
185{
186 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
187 struct kmem_cache *s = kmalloc_slab(size);
188
189 if (!s)
190 return NULL;
191
192 return kmem_cache_zalloc(s, flags);
193 } else
194 return __kzalloc(size, flags);
195}
196
197#ifdef CONFIG_NUMA
198extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
199
200static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
201{
202 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
203 struct kmem_cache *s = kmalloc_slab(size);
204
205 if (!s)
206 return NULL;
207
208 return kmem_cache_alloc_node(s, flags, node);
209 } else
210 return __kmalloc_node(size, flags, node);
211}
212#endif
213
214#endif /* _LINUX_SLUB_DEF_H */