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_cpu {
18 /* Lots of wasted space */
19 } ____cacheline_aligned_in_smp;
21 struct kmem_cache_node {
22 spinlock_t list_lock; /* Protect partial list and nr_partial */
23 unsigned long nr_partial;
24 atomic_long_t nr_slabs;
25 struct list_head partial;
26 #ifdef CONFIG_SLUB_DEBUG
27 struct list_head full;
32 * Slab cache management.
35 /* Used for retriving partial slabs etc */
37 int size; /* The size of an object including meta data */
38 int objsize; /* The size of an object without meta data */
39 int offset; /* Free pointer offset. */
43 * Avoid an extra cache line for UP, SMP and for the node local to
46 struct kmem_cache_node local_node;
48 /* Allocation and freeing of slabs */
49 int objects; /* Number of objects in slab */
50 int refcount; /* Refcount for slab cache destroy */
51 void (*ctor)(void *, struct kmem_cache *, unsigned long);
52 int inuse; /* Offset to metadata */
53 int align; /* Alignment */
54 const char *name; /* Name (only for display!) */
55 struct list_head list; /* List of slab caches */
56 #ifdef CONFIG_SLUB_DEBUG
57 struct kobject kobj; /* For sysfs */
62 struct kmem_cache_node *node[MAX_NUMNODES];
64 struct kmem_cache_cpu cpu_slab[NR_CPUS];
70 #if defined(ARCH_KMALLOC_MINALIGN) && ARCH_KMALLOC_MINALIGN > 8
71 #define KMALLOC_MIN_SIZE ARCH_KMALLOC_MINALIGN
73 #define KMALLOC_MIN_SIZE 8
76 #define KMALLOC_SHIFT_LOW ilog2(KMALLOC_MIN_SIZE)
79 * We keep the general caches in an array of slab caches that are used for
80 * 2^x bytes of allocations.
82 extern struct kmem_cache kmalloc_caches[PAGE_SHIFT];
85 * Sorry that the following has to be that ugly but some versions of GCC
86 * have trouble with constant propagation and loops.
88 static __always_inline int kmalloc_index(size_t size)
93 if (size <= KMALLOC_MIN_SIZE)
94 return KMALLOC_SHIFT_LOW;
96 if (size > 64 && size <= 96)
98 if (size > 128 && size <= 192)
100 if (size <= 8) return 3;
101 if (size <= 16) return 4;
102 if (size <= 32) return 5;
103 if (size <= 64) return 6;
104 if (size <= 128) return 7;
105 if (size <= 256) return 8;
106 if (size <= 512) return 9;
107 if (size <= 1024) return 10;
108 if (size <= 2 * 1024) return 11;
110 * The following is only needed to support architectures with a larger page
113 if (size <= 4 * 1024) return 12;
114 if (size <= 8 * 1024) return 13;
115 if (size <= 16 * 1024) return 14;
116 if (size <= 32 * 1024) return 15;
117 if (size <= 64 * 1024) return 16;
118 if (size <= 128 * 1024) return 17;
119 if (size <= 256 * 1024) return 18;
120 if (size <= 512 * 1024) return 19;
121 if (size <= 1024 * 1024) return 20;
122 if (size <= 2 * 1024 * 1024) return 21;
126 * What we really wanted to do and cannot do because of compiler issues is:
128 * for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++)
129 * if (size <= (1 << i))
135 * Find the slab cache for a given combination of allocation flags and size.
137 * This ought to end up with a global pointer to the right cache
140 static __always_inline struct kmem_cache *kmalloc_slab(size_t size)
142 int index = kmalloc_index(size);
147 return &kmalloc_caches[index];
150 #ifdef CONFIG_ZONE_DMA
151 #define SLUB_DMA __GFP_DMA
153 /* Disable DMA functionality */
154 #define SLUB_DMA (__force gfp_t)0
157 void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
158 void *__kmalloc(size_t size, gfp_t flags);
160 static __always_inline void *kmalloc(size_t size, gfp_t flags)
162 if (__builtin_constant_p(size)) {
163 if (size > PAGE_SIZE / 2)
164 return (void *)__get_free_pages(flags | __GFP_COMP,
167 if (!(flags & SLUB_DMA)) {
168 struct kmem_cache *s = kmalloc_slab(size);
171 return ZERO_SIZE_PTR;
173 return kmem_cache_alloc(s, flags);
176 return __kmalloc(size, flags);
180 void *__kmalloc_node(size_t size, gfp_t flags, int node);
181 void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
183 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
185 if (__builtin_constant_p(size) &&
186 size <= PAGE_SIZE / 2 && !(flags & SLUB_DMA)) {
187 struct kmem_cache *s = kmalloc_slab(size);
190 return ZERO_SIZE_PTR;
192 return kmem_cache_alloc_node(s, flags, node);
194 return __kmalloc_node(size, flags, node);
198 #endif /* _LINUX_SLUB_DEF_H */