slab_common.c 1.9 KB

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