1 #ifndef _LINUX_SLUB_DEF_H
2 #define _LINUX_SLUB_DEF_H
5 * SLUB : A Slab allocator without object queues.
7 * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
9 #include <linux/types.h>
10 #include <linux/gfp.h>
11 #include <linux/workqueue.h>
12 #include <linux/kobject.h>
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;
22 * Slab cache management.
25 /* Used for retriving partial slabs etc */
27 int size; /* The size of an object including meta data */
28 int objsize; /* The size of an object without meta data */
29 int offset; /* Free pointer offset. */
33 * Avoid an extra cache line for UP, SMP and for the node local to
36 struct kmem_cache_node local_node;
38 /* Allocation and freeing of slabs */
39 int objects; /* Number of objects in slab */
40 int refcount; /* Refcount for slab cache destroy */
41 void (*ctor)(void *, struct kmem_cache *, unsigned long);
42 void (*dtor)(void *, struct kmem_cache *, unsigned long);
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 */
51 struct kmem_cache_node *node[MAX_NUMNODES];
53 struct page *cpu_slab[NR_CPUS];
59 #define KMALLOC_SHIFT_LOW 3
61 #ifdef CONFIG_LARGE_ALLOCS
62 #define KMALLOC_SHIFT_HIGH 25
64 #if !defined(CONFIG_MMU) || NR_CPUS > 512 || MAX_NUMNODES > 256
65 #define KMALLOC_SHIFT_HIGH 20
67 #define KMALLOC_SHIFT_HIGH 18
72 * We keep the general caches in an array of slab caches that are used for
73 * 2^x bytes of allocations.
75 extern struct kmem_cache kmalloc_caches[KMALLOC_SHIFT_HIGH + 1];
78 * Sorry that the following has to be that ugly but some versions of GCC
79 * have trouble with constant propagation and loops.
81 static inline int kmalloc_index(int size)
84 * We should return 0 if size == 0 but we use the smallest object
85 * here for SLAB legacy reasons.
87 WARN_ON_ONCE(size == 0);
89 if (size > 64 && size <= 96)
91 if (size > 128 && size <= 192)
93 if (size <= 8) return 3;
94 if (size <= 16) return 4;
95 if (size <= 32) return 5;
96 if (size <= 64) return 6;
97 if (size <= 128) return 7;
98 if (size <= 256) return 8;
99 if (size <= 512) return 9;
100 if (size <= 1024) return 10;
101 if (size <= 2 * 1024) return 11;
102 if (size <= 4 * 1024) return 12;
103 if (size <= 8 * 1024) return 13;
104 if (size <= 16 * 1024) return 14;
105 if (size <= 32 * 1024) return 15;
106 if (size <= 64 * 1024) return 16;
107 if (size <= 128 * 1024) return 17;
108 if (size <= 256 * 1024) return 18;
109 #if KMALLOC_SHIFT_HIGH > 18
110 if (size <= 512 * 1024) return 19;
111 if (size <= 1024 * 1024) return 20;
113 #if KMALLOC_SHIFT_HIGH > 20
114 if (size <= 2 * 1024 * 1024) return 21;
115 if (size <= 4 * 1024 * 1024) return 22;
116 if (size <= 8 * 1024 * 1024) return 23;
117 if (size <= 16 * 1024 * 1024) return 24;
118 if (size <= 32 * 1024 * 1024) return 25;
123 * What we really wanted to do and cannot do because of compiler issues is:
125 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
126 * if (size <= (1 << i))
132 * Find the slab cache for a given combination of allocation flags and size.
134 * This ought to end up with a global pointer to the right cache
137 static inline struct kmem_cache *kmalloc_slab(size_t size)
139 int index = kmalloc_index(size);
146 * Generate a link failure. Would be great if we could
147 * do something to stop the compile here.
149 extern void __kmalloc_size_too_large(void);
150 __kmalloc_size_too_large();
152 return &kmalloc_caches[index];
155 #ifdef CONFIG_ZONE_DMA
156 #define SLUB_DMA __GFP_DMA
158 /* Disable DMA functionality */
162 static inline void *kmalloc(size_t size, gfp_t flags)
164 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
165 struct kmem_cache *s = kmalloc_slab(size);
170 return kmem_cache_alloc(s, flags);
172 return __kmalloc(size, flags);
175 static inline void *kzalloc(size_t size, gfp_t flags)
177 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
178 struct kmem_cache *s = kmalloc_slab(size);
183 return kmem_cache_zalloc(s, flags);
185 return __kzalloc(size, flags);
189 extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
191 static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
193 if (__builtin_constant_p(size) && !(flags & SLUB_DMA)) {
194 struct kmem_cache *s = kmalloc_slab(size);
199 return kmem_cache_alloc_node(s, flags, node);
201 return __kmalloc_node(size, flags, node);
205 #endif /* _LINUX_SLUB_DEF_H */