slab_def.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #ifndef _LINUX_SLAB_DEF_H
  2. #define _LINUX_SLAB_DEF_H
  3. /*
  4. * Definitions unique to the original Linux SLAB allocator.
  5. *
  6. * What we provide here is a way to optimize the frequent kmalloc
  7. * calls in the kernel by selecting the appropriate general cache
  8. * if kmalloc was called with a size that can be established at
  9. * compile time.
  10. */
  11. #include <linux/init.h>
  12. #include <asm/page.h> /* kmalloc_sizes.h needs PAGE_SIZE */
  13. #include <asm/cache.h> /* kmalloc_sizes.h needs L1_CACHE_BYTES */
  14. #include <linux/compiler.h>
  15. /* Size description struct for general caches. */
  16. struct cache_sizes {
  17. size_t cs_size;
  18. struct kmem_cache *cs_cachep;
  19. #ifdef CONFIG_ZONE_DMA
  20. struct kmem_cache *cs_dmacachep;
  21. #endif
  22. };
  23. extern struct cache_sizes malloc_sizes[];
  24. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  25. void *__kmalloc(size_t size, gfp_t flags);
  26. static inline void *kmalloc(size_t size, gfp_t flags)
  27. {
  28. if (__builtin_constant_p(size)) {
  29. int i = 0;
  30. if (!size)
  31. return ZERO_SIZE_PTR;
  32. #define CACHE(x) \
  33. if (size <= x) \
  34. goto found; \
  35. else \
  36. i++;
  37. #include <linux/kmalloc_sizes.h>
  38. #undef CACHE
  39. {
  40. extern void __you_cannot_kmalloc_that_much(void);
  41. __you_cannot_kmalloc_that_much();
  42. }
  43. found:
  44. #ifdef CONFIG_ZONE_DMA
  45. if (flags & GFP_DMA)
  46. return kmem_cache_alloc(malloc_sizes[i].cs_dmacachep,
  47. flags);
  48. #endif
  49. return kmem_cache_alloc(malloc_sizes[i].cs_cachep, flags);
  50. }
  51. return __kmalloc(size, flags);
  52. }
  53. #ifdef CONFIG_NUMA
  54. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  55. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  56. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  57. {
  58. if (__builtin_constant_p(size)) {
  59. int i = 0;
  60. if (!size)
  61. return ZERO_SIZE_PTR;
  62. #define CACHE(x) \
  63. if (size <= x) \
  64. goto found; \
  65. else \
  66. i++;
  67. #include <linux/kmalloc_sizes.h>
  68. #undef CACHE
  69. {
  70. extern void __you_cannot_kmalloc_that_much(void);
  71. __you_cannot_kmalloc_that_much();
  72. }
  73. found:
  74. #ifdef CONFIG_ZONE_DMA
  75. if (flags & GFP_DMA)
  76. return kmem_cache_alloc_node(malloc_sizes[i].cs_dmacachep,
  77. flags, node);
  78. #endif
  79. return kmem_cache_alloc_node(malloc_sizes[i].cs_cachep,
  80. flags, node);
  81. }
  82. return __kmalloc_node(size, flags, node);
  83. }
  84. #endif /* CONFIG_NUMA */
  85. #endif /* _LINUX_SLAB_DEF_H */