blob: 50e1ff10bff9d0f89b0a7b71e52ac413a3856f64 [file] [log] [blame]
Christoph Lameter039363f2012-07-06 15:25:10 -05001/*
2 * Slab allocator functions that are independent of the allocator strategy
3 *
4 * (C) 2012 Christoph Lameter <cl@linux.com>
5 */
6#include <linux/slab.h>
7
8#include <linux/mm.h>
9#include <linux/poison.h>
10#include <linux/interrupt.h>
11#include <linux/memory.h>
12#include <linux/compiler.h>
13#include <linux/module.h>
14
15#include <asm/cacheflush.h>
16#include <asm/tlbflush.h>
17#include <asm/page.h>
18
Christoph Lameter97d06602012-07-06 15:25:11 -050019#include "slab.h"
20
21enum slab_state slab_state;
Christoph Lameter18004c52012-07-06 15:25:12 -050022LIST_HEAD(slab_caches);
23DEFINE_MUTEX(slab_mutex);
Christoph Lameter97d06602012-07-06 15:25:11 -050024
Christoph Lameter039363f2012-07-06 15:25:10 -050025/*
26 * kmem_cache_create - Create a cache.
27 * @name: A string which is used in /proc/slabinfo to identify this cache.
28 * @size: The size of objects to be created in this cache.
29 * @align: The required alignment for the objects.
30 * @flags: SLAB flags
31 * @ctor: A constructor for the objects.
32 *
33 * Returns a ptr to the cache on success, NULL on failure.
34 * Cannot be called within a interrupt, but can be interrupted.
35 * The @ctor is run when new pages are allocated by the cache.
36 *
37 * The flags are
38 *
39 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
40 * to catch references to uninitialised memory.
41 *
42 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
43 * for buffer overruns.
44 *
45 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
46 * cacheline. This can be beneficial if you're counting cycles as closely
47 * as davem.
48 */
49
50struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
51 unsigned long flags, void (*ctor)(void *))
52{
53 struct kmem_cache *s = NULL;
54
55#ifdef CONFIG_DEBUG_VM
56 if (!name || in_interrupt() || size < sizeof(void *) ||
57 size > KMALLOC_MAX_SIZE) {
58 printk(KERN_ERR "kmem_cache_create(%s) integrity check"
59 " failed\n", name);
60 goto out;
61 }
62#endif
63
64 s = __kmem_cache_create(name, size, align, flags, ctor);
65
66#ifdef CONFIG_DEBUG_VM
67out:
68#endif
69 if (!s && (flags & SLAB_PANIC))
70 panic("kmem_cache_create: Failed to create slab '%s'\n", name);
71
72 return s;
73}
74EXPORT_SYMBOL(kmem_cache_create);
Christoph Lameter97d06602012-07-06 15:25:11 -050075
76int slab_is_available(void)
77{
78 return slab_state >= UP;
79}