slab_def.h 2.3 KB

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