slab_def.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. static inline void *kmalloc(size_t size, gfp_t flags)
  25. {
  26. if (__builtin_constant_p(size)) {
  27. int i = 0;
  28. #define CACHE(x) \
  29. if (size <= x) \
  30. goto found; \
  31. else \
  32. i++;
  33. #include "kmalloc_sizes.h"
  34. #undef CACHE
  35. {
  36. extern void __you_cannot_kmalloc_that_much(void);
  37. __you_cannot_kmalloc_that_much();
  38. }
  39. found:
  40. #ifdef CONFIG_ZONE_DMA
  41. if (flags & GFP_DMA)
  42. return kmem_cache_alloc(malloc_sizes[i].cs_dmacachep,
  43. flags);
  44. #endif
  45. return kmem_cache_alloc(malloc_sizes[i].cs_cachep, flags);
  46. }
  47. return __kmalloc(size, flags);
  48. }
  49. static inline void *kzalloc(size_t size, gfp_t flags)
  50. {
  51. if (__builtin_constant_p(size)) {
  52. int i = 0;
  53. #define CACHE(x) \
  54. if (size <= x) \
  55. goto found; \
  56. else \
  57. i++;
  58. #include "kmalloc_sizes.h"
  59. #undef CACHE
  60. {
  61. extern void __you_cannot_kzalloc_that_much(void);
  62. __you_cannot_kzalloc_that_much();
  63. }
  64. found:
  65. #ifdef CONFIG_ZONE_DMA
  66. if (flags & GFP_DMA)
  67. return kmem_cache_zalloc(malloc_sizes[i].cs_dmacachep,
  68. flags);
  69. #endif
  70. return kmem_cache_zalloc(malloc_sizes[i].cs_cachep, flags);
  71. }
  72. return __kzalloc(size, flags);
  73. }
  74. #ifdef CONFIG_NUMA
  75. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  76. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  77. {
  78. if (__builtin_constant_p(size)) {
  79. int i = 0;
  80. #define CACHE(x) \
  81. if (size <= x) \
  82. goto found; \
  83. else \
  84. i++;
  85. #include "kmalloc_sizes.h"
  86. #undef CACHE
  87. {
  88. extern void __you_cannot_kmalloc_that_much(void);
  89. __you_cannot_kmalloc_that_much();
  90. }
  91. found:
  92. #ifdef CONFIG_ZONE_DMA
  93. if (flags & GFP_DMA)
  94. return kmem_cache_alloc_node(malloc_sizes[i].cs_dmacachep,
  95. flags, node);
  96. #endif
  97. return kmem_cache_alloc_node(malloc_sizes[i].cs_cachep,
  98. flags, node);
  99. }
  100. return __kmalloc_node(size, flags, node);
  101. }
  102. #endif /* CONFIG_NUMA */
  103. #endif /* _LINUX_SLAB_DEF_H */