slab_def.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <linux/kmemtrace.h>
  16. /* Size description struct for general caches. */
  17. struct cache_sizes {
  18. size_t cs_size;
  19. struct kmem_cache *cs_cachep;
  20. #ifdef CONFIG_ZONE_DMA
  21. struct kmem_cache *cs_dmacachep;
  22. #endif
  23. };
  24. extern struct cache_sizes malloc_sizes[];
  25. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  26. void *__kmalloc(size_t size, gfp_t flags);
  27. #ifdef CONFIG_KMEMTRACE
  28. extern void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags);
  29. extern size_t slab_buffer_size(struct kmem_cache *cachep);
  30. #else
  31. static __always_inline void *
  32. kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
  33. {
  34. return kmem_cache_alloc(cachep, flags);
  35. }
  36. static inline size_t slab_buffer_size(struct kmem_cache *cachep)
  37. {
  38. return 0;
  39. }
  40. #endif
  41. static __always_inline void *kmalloc(size_t size, gfp_t flags)
  42. {
  43. struct kmem_cache *cachep;
  44. void *ret;
  45. if (__builtin_constant_p(size)) {
  46. int i = 0;
  47. if (!size)
  48. return ZERO_SIZE_PTR;
  49. #define CACHE(x) \
  50. if (size <= x) \
  51. goto found; \
  52. else \
  53. i++;
  54. #include <linux/kmalloc_sizes.h>
  55. #undef CACHE
  56. {
  57. extern void __you_cannot_kmalloc_that_much(void);
  58. __you_cannot_kmalloc_that_much();
  59. }
  60. found:
  61. #ifdef CONFIG_ZONE_DMA
  62. if (flags & GFP_DMA)
  63. cachep = malloc_sizes[i].cs_dmacachep;
  64. else
  65. #endif
  66. cachep = malloc_sizes[i].cs_cachep;
  67. ret = kmem_cache_alloc_notrace(cachep, flags);
  68. kmemtrace_mark_alloc(KMEMTRACE_TYPE_KMALLOC, _THIS_IP_, ret,
  69. size, slab_buffer_size(cachep), flags);
  70. return ret;
  71. }
  72. return __kmalloc(size, flags);
  73. }
  74. #ifdef CONFIG_NUMA
  75. extern void *__kmalloc_node(size_t size, gfp_t flags, int node);
  76. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  77. #ifdef CONFIG_KMEMTRACE
  78. extern void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
  79. gfp_t flags,
  80. int nodeid);
  81. #else
  82. static __always_inline void *
  83. kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
  84. gfp_t flags,
  85. int nodeid)
  86. {
  87. return kmem_cache_alloc_node(cachep, flags, nodeid);
  88. }
  89. #endif
  90. static __always_inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  91. {
  92. struct kmem_cache *cachep;
  93. void *ret;
  94. if (__builtin_constant_p(size)) {
  95. int i = 0;
  96. if (!size)
  97. return ZERO_SIZE_PTR;
  98. #define CACHE(x) \
  99. if (size <= x) \
  100. goto found; \
  101. else \
  102. i++;
  103. #include <linux/kmalloc_sizes.h>
  104. #undef CACHE
  105. {
  106. extern void __you_cannot_kmalloc_that_much(void);
  107. __you_cannot_kmalloc_that_much();
  108. }
  109. found:
  110. #ifdef CONFIG_ZONE_DMA
  111. if (flags & GFP_DMA)
  112. cachep = malloc_sizes[i].cs_dmacachep;
  113. else
  114. #endif
  115. cachep = malloc_sizes[i].cs_cachep;
  116. ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
  117. kmemtrace_mark_alloc_node(KMEMTRACE_TYPE_KMALLOC, _THIS_IP_,
  118. ret, size, slab_buffer_size(cachep),
  119. flags, node);
  120. return ret;
  121. }
  122. return __kmalloc_node(size, flags, node);
  123. }
  124. #endif /* CONFIG_NUMA */
  125. #endif /* _LINUX_SLAB_DEF_H */