1 #ifndef _LINUX_SLAB_DEF_H
2 #define _LINUX_SLAB_DEF_H
5 * Definitions unique to the original Linux SLAB allocator.
7 * What we provide here is a way to optimize the frequent kmalloc
8 * calls in the kernel by selecting the appropriate general cache
9 * if kmalloc was called with a size that can be established at
13 #include <linux/init.h>
14 #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
15 #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
16 #include <linux/compiler.h>
17 #include <linux/kmemtrace.h>
26 /* 1) per-cpu data, touched during every alloc/free */
27 struct array_cache *array[NR_CPUS];
28 /* 2) Cache tunables. Protected by cache_chain_mutex */
29 unsigned int batchcount;
33 unsigned int buffer_size;
34 u32 reciprocal_buffer_size;
35 /* 3) touched by every alloc & free from the backend */
37 unsigned int flags; /* constant flags */
38 unsigned int num; /* # of objs per slab */
40 /* 4) cache_grow/shrink */
41 /* order of pgs per slab (2^n) */
42 unsigned int gfporder;
44 /* force GFP flags, e.g. GFP_DMA */
47 size_t colour; /* cache colouring range */
48 unsigned int colour_off; /* colour offset */
49 struct kmem_cache *slabp_cache;
50 unsigned int slab_size;
51 unsigned int dflags; /* dynamic flags */
53 /* constructor func */
54 void (*ctor)(void *obj);
56 /* 5) cache creation/removal */
58 struct list_head next;
61 #ifdef CONFIG_DEBUG_SLAB
62 unsigned long num_active;
63 unsigned long num_allocations;
64 unsigned long high_mark;
68 unsigned long max_freeable;
69 unsigned long node_allocs;
70 unsigned long node_frees;
71 unsigned long node_overflow;
78 * If debugging is enabled, then the allocator can add additional
79 * fields and/or padding to every object. buffer_size contains the total
80 * object size including these internal fields, the following two
81 * variables contain the offset to the user object and its size.
85 #endif /* CONFIG_DEBUG_SLAB */
88 * We put nodelists[] at the end of kmem_cache, because we want to size
89 * this array to nr_node_ids slots instead of MAX_NUMNODES
90 * (see kmem_cache_init())
91 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
92 * is statically defined, so we reserve the max number of nodes.
94 struct kmem_list3 *nodelists[MAX_NUMNODES];
96 * Do not add fields after nodelists[]
100 /* Size description struct for general caches. */
103 struct kmem_cache *cs_cachep;
104 #ifdef CONFIG_ZONE_DMA
105 struct kmem_cache *cs_dmacachep;
108 extern struct cache_sizes malloc_sizes[];
110 void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
111 void *__kmalloc(size_t size, gfp_t flags);
113 #ifdef CONFIG_KMEMTRACE
114 extern void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags);
115 extern size_t slab_buffer_size(struct kmem_cache *cachep);
117 static __always_inline void *
118 kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
120 return kmem_cache_alloc(cachep, flags);
122 static inline size_t slab_buffer_size(struct kmem_cache *cachep)
128 static __always_inline void *kmalloc(size_t size, gfp_t flags)
130 struct kmem_cache *cachep;
133 if (__builtin_constant_p(size)) {
137 return ZERO_SIZE_PTR;
144 #include <linux/kmalloc_sizes.h>
148 #ifdef CONFIG_ZONE_DMA
150 cachep = malloc_sizes[i].cs_dmacachep;
153 cachep = malloc_sizes[i].cs_cachep;
155 ret = kmem_cache_alloc_notrace(cachep, flags);
157 trace_kmalloc(_THIS_IP_, ret,
158 size, slab_buffer_size(cachep), flags);
162 return __kmalloc(size, flags);
166 extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
167 extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
169 #ifdef CONFIG_KMEMTRACE
170 extern void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
174 static __always_inline void *
175 kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
179 return kmem_cache_alloc_node(cachep, flags, nodeid);
183 static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
185 struct kmem_cache *cachep;
188 if (__builtin_constant_p(size)) {
192 return ZERO_SIZE_PTR;
199 #include <linux/kmalloc_sizes.h>
203 #ifdef CONFIG_ZONE_DMA
205 cachep = malloc_sizes[i].cs_dmacachep;
208 cachep = malloc_sizes[i].cs_cachep;
210 ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
212 trace_kmalloc_node(_THIS_IP_, ret,
213 size, slab_buffer_size(cachep),
218 return __kmalloc_node(size, flags, node);
221 #endif /* CONFIG_NUMA */
223 #endif /* _LINUX_SLAB_DEF_H */