slab_common.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  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. #include <linux/mm.h>
  8. #include <linux/poison.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/memory.h>
  11. #include <linux/compiler.h>
  12. #include <linux/module.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/page.h>
  16. #include "slab.h"
  17. enum slab_state slab_state;
  18. /*
  19. * kmem_cache_create - Create a cache.
  20. * @name: A string which is used in /proc/slabinfo to identify this cache.
  21. * @size: The size of objects to be created in this cache.
  22. * @align: The required alignment for the objects.
  23. * @flags: SLAB flags
  24. * @ctor: A constructor for the objects.
  25. *
  26. * Returns a ptr to the cache on success, NULL on failure.
  27. * Cannot be called within a interrupt, but can be interrupted.
  28. * The @ctor is run when new pages are allocated by the cache.
  29. *
  30. * The flags are
  31. *
  32. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  33. * to catch references to uninitialised memory.
  34. *
  35. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  36. * for buffer overruns.
  37. *
  38. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  39. * cacheline. This can be beneficial if you're counting cycles as closely
  40. * as davem.
  41. */
  42. struct kmem_cache *kmem_cache_create(const char *name, size_t size, size_t align,
  43. unsigned long flags, void (*ctor)(void *))
  44. {
  45. struct kmem_cache *s = NULL;
  46. #ifdef CONFIG_DEBUG_VM
  47. if (!name || in_interrupt() || size < sizeof(void *) ||
  48. size > KMALLOC_MAX_SIZE) {
  49. printk(KERN_ERR "kmem_cache_create(%s) integrity check"
  50. " failed\n", name);
  51. goto out;
  52. }
  53. #endif
  54. s = __kmem_cache_create(name, size, align, flags, ctor);
  55. #ifdef CONFIG_DEBUG_VM
  56. out:
  57. #endif
  58. if (!s && (flags & SLAB_PANIC))
  59. panic("kmem_cache_create: Failed to create slab '%s'\n", name);
  60. return s;
  61. }
  62. EXPORT_SYMBOL(kmem_cache_create);
  63. int slab_is_available(void)
  64. {
  65. return slab_state >= UP;
  66. }