slab.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Written by Mark Hemment, 1996 (markhe@nextd.demon.co.uk).
  3. *
  4. * (C) SGI 2006, Christoph Lameter <clameter@sgi.com>
  5. * Cleaned up and restructured to ease the addition of alternative
  6. * implementations of SLAB allocators.
  7. */
  8. #ifndef _LINUX_SLAB_H
  9. #define _LINUX_SLAB_H
  10. #ifdef __KERNEL__
  11. #include <linux/gfp.h>
  12. #include <linux/types.h>
  13. typedef struct kmem_cache kmem_cache_t __deprecated;
  14. /*
  15. * Flags to pass to kmem_cache_create().
  16. * The ones marked DEBUG are only valid if CONFIG_SLAB_DEBUG is set.
  17. */
  18. #define SLAB_DEBUG_FREE 0x00000100UL /* DEBUG: Perform (expensive) checks on free */
  19. #define SLAB_DEBUG_INITIAL 0x00000200UL /* DEBUG: Call constructor (as verifier) */
  20. #define SLAB_RED_ZONE 0x00000400UL /* DEBUG: Red zone objs in a cache */
  21. #define SLAB_POISON 0x00000800UL /* DEBUG: Poison objects */
  22. #define SLAB_HWCACHE_ALIGN 0x00002000UL /* Align objs on cache lines */
  23. #define SLAB_CACHE_DMA 0x00004000UL /* Use GFP_DMA memory */
  24. #define SLAB_MUST_HWCACHE_ALIGN 0x00008000UL /* Force alignment even if debuggin is active */
  25. #define SLAB_STORE_USER 0x00010000UL /* DEBUG: Store the last owner for bug hunting */
  26. #define SLAB_RECLAIM_ACCOUNT 0x00020000UL /* Objects are reclaimable */
  27. #define SLAB_PANIC 0x00040000UL /* Panic if kmem_cache_create() fails */
  28. #define SLAB_DESTROY_BY_RCU 0x00080000UL /* Defer freeing slabs to RCU */
  29. #define SLAB_MEM_SPREAD 0x00100000UL /* Spread some memory over cpuset */
  30. /* Flags passed to a constructor functions */
  31. #define SLAB_CTOR_CONSTRUCTOR 0x001UL /* If not set, then deconstructor */
  32. #define SLAB_CTOR_ATOMIC 0x002UL /* Tell constructor it can't sleep */
  33. #define SLAB_CTOR_VERIFY 0x004UL /* Tell constructor it's a verify call */
  34. /*
  35. * struct kmem_cache related prototypes
  36. */
  37. void __init kmem_cache_init(void);
  38. extern int slab_is_available(void);
  39. struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
  40. unsigned long,
  41. void (*)(void *, struct kmem_cache *, unsigned long),
  42. void (*)(void *, struct kmem_cache *, unsigned long));
  43. void kmem_cache_destroy(struct kmem_cache *);
  44. int kmem_cache_shrink(struct kmem_cache *);
  45. void *kmem_cache_alloc(struct kmem_cache *, gfp_t);
  46. void *kmem_cache_zalloc(struct kmem_cache *, gfp_t);
  47. void kmem_cache_free(struct kmem_cache *, void *);
  48. unsigned int kmem_cache_size(struct kmem_cache *);
  49. const char *kmem_cache_name(struct kmem_cache *);
  50. int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr);
  51. #ifdef CONFIG_NUMA
  52. extern void *kmem_cache_alloc_node(struct kmem_cache *, gfp_t flags, int node);
  53. #else
  54. static inline void *kmem_cache_alloc_node(struct kmem_cache *cachep,
  55. gfp_t flags, int node)
  56. {
  57. return kmem_cache_alloc(cachep, flags);
  58. }
  59. #endif
  60. /*
  61. * Common kmalloc functions provided by all allocators
  62. */
  63. void *__kmalloc(size_t, gfp_t);
  64. void *__kzalloc(size_t, gfp_t);
  65. void * __must_check krealloc(const void *, size_t, gfp_t);
  66. void kfree(const void *);
  67. size_t ksize(const void *);
  68. /**
  69. * kcalloc - allocate memory for an array. The memory is set to zero.
  70. * @n: number of elements.
  71. * @size: element size.
  72. * @flags: the type of memory to allocate.
  73. */
  74. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)
  75. {
  76. if (n != 0 && size > ULONG_MAX / n)
  77. return NULL;
  78. return __kzalloc(n * size, flags);
  79. }
  80. /*
  81. * Allocator specific definitions. These are mainly used to establish optimized
  82. * ways to convert kmalloc() calls to kmem_cache_alloc() invocations by selecting
  83. * the appropriate general cache at compile time.
  84. */
  85. #ifdef CONFIG_SLAB
  86. #include <linux/slab_def.h>
  87. #else
  88. /*
  89. * Fallback definitions for an allocator not wanting to provide
  90. * its own optimized kmalloc definitions (like SLOB).
  91. */
  92. /**
  93. * kmalloc - allocate memory
  94. * @size: how many bytes of memory are required.
  95. * @flags: the type of memory to allocate.
  96. *
  97. * kmalloc is the normal method of allocating memory
  98. * in the kernel.
  99. *
  100. * The @flags argument may be one of:
  101. *
  102. * %GFP_USER - Allocate memory on behalf of user. May sleep.
  103. *
  104. * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
  105. *
  106. * %GFP_ATOMIC - Allocation will not sleep.
  107. * For example, use this inside interrupt handlers.
  108. *
  109. * %GFP_HIGHUSER - Allocate pages from high memory.
  110. *
  111. * %GFP_NOIO - Do not do any I/O at all while trying to get memory.
  112. *
  113. * %GFP_NOFS - Do not make any fs calls while trying to get memory.
  114. *
  115. * Also it is possible to set different flags by OR'ing
  116. * in one or more of the following additional @flags:
  117. *
  118. * %__GFP_COLD - Request cache-cold pages instead of
  119. * trying to return cache-warm pages.
  120. *
  121. * %__GFP_DMA - Request memory from the DMA-capable zone.
  122. *
  123. * %__GFP_HIGH - This allocation has high priority and may use emergency pools.
  124. *
  125. * %__GFP_HIGHMEM - Allocated memory may be from highmem.
  126. *
  127. * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail
  128. * (think twice before using).
  129. *
  130. * %__GFP_NORETRY - If memory is not immediately available,
  131. * then give up at once.
  132. *
  133. * %__GFP_NOWARN - If allocation fails, don't issue any warnings.
  134. *
  135. * %__GFP_REPEAT - If allocation fails initially, try once more before failing.
  136. */
  137. static inline void *kmalloc(size_t size, gfp_t flags)
  138. {
  139. return __kmalloc(size, flags);
  140. }
  141. /**
  142. * kzalloc - allocate memory. The memory is set to zero.
  143. * @size: how many bytes of memory are required.
  144. * @flags: the type of memory to allocate (see kmalloc).
  145. */
  146. static inline void *kzalloc(size_t size, gfp_t flags)
  147. {
  148. return __kzalloc(size, flags);
  149. }
  150. #endif
  151. #ifndef CONFIG_NUMA
  152. static inline void *kmalloc_node(size_t size, gfp_t flags, int node)
  153. {
  154. return kmalloc(size, flags);
  155. }
  156. static inline void *__kmalloc_node(size_t size, gfp_t flags, int node)
  157. {
  158. return __kmalloc(size, flags);
  159. }
  160. #endif /* !CONFIG_NUMA */
  161. /*
  162. * kmalloc_track_caller is a special version of kmalloc that records the
  163. * calling function of the routine calling it for slab leak tracking instead
  164. * of just the calling function (confusing, eh?).
  165. * It's useful when the call to kmalloc comes from a widely-used standard
  166. * allocator where we care about the real place the memory allocation
  167. * request comes from.
  168. */
  169. #ifdef CONFIG_DEBUG_SLAB
  170. extern void *__kmalloc_track_caller(size_t, gfp_t, void*);
  171. #define kmalloc_track_caller(size, flags) \
  172. __kmalloc_track_caller(size, flags, __builtin_return_address(0))
  173. #else
  174. #define kmalloc_track_caller(size, flags) \
  175. __kmalloc(size, flags)
  176. #endif /* DEBUG_SLAB */
  177. #ifdef CONFIG_NUMA
  178. /*
  179. * kmalloc_node_track_caller is a special version of kmalloc_node that
  180. * records the calling function of the routine calling it for slab leak
  181. * tracking instead of just the calling function (confusing, eh?).
  182. * It's useful when the call to kmalloc_node comes from a widely-used
  183. * standard allocator where we care about the real place the memory
  184. * allocation request comes from.
  185. */
  186. #ifdef CONFIG_DEBUG_SLAB
  187. extern void *__kmalloc_node_track_caller(size_t, gfp_t, int, void *);
  188. #define kmalloc_node_track_caller(size, flags, node) \
  189. __kmalloc_node_track_caller(size, flags, node, \
  190. __builtin_return_address(0))
  191. #else
  192. #define kmalloc_node_track_caller(size, flags, node) \
  193. __kmalloc_node(size, flags, node)
  194. #endif
  195. #else /* CONFIG_NUMA */
  196. #define kmalloc_node_track_caller(size, flags, node) \
  197. kmalloc_track_caller(size, flags)
  198. #endif /* DEBUG_SLAB */
  199. #endif /* __KERNEL__ */
  200. #endif /* _LINUX_SLAB_H */